Skip to content

fix(decision-transformer): cast tensor calibration rows to the model dtype - #5038

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Lumea-art:fix/dt-calibration-dtype
Nov 18, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Lumea-art:fix/dt-calibration-dtype

Conversation

@Lumea-art

Copy link
Copy Markdown
Contributor

Summary

The tensor-calibration path added in #4894 hands the caller's own tensors to DecisionTransformerModel.forward. Loading at the default bfloat16 and passing torch.randn rows - float32, the natural spelling - fails inside the first matmul during input capture:

$ GPTQModel.load(ckpt, quantize_config=QuantizeConfig(bits=4, group_size=32, device="cpu"),
                 dtype=torch.bfloat16).quantize(CalibrationConfig(dataset=rows), backend=BACKEND.TORCH)
RuntimeError: mat1 and mat2 must have the same dtype, but got Float and BFloat16

The error comes from torch's kernel, not from the loader, so it names neither the calibration key nor the dtype pair. The shipped end-to-end test loads the model as fp32, which is why the path was green.

Change

move_input_capture_example casts floating rows to the model's weight dtype when the definition declares calibration_inputs = "tensor_kwargs":

if getattr(self, "calibration_inputs", None) == "tensor_kwargs":
    example = _cast_floating_inputs(example, _module_weight_dtype(getattr(self, "model", None)))
  • Integer rows (timesteps, attention masks) keep their dtype.
  • Every other definition is untouched: the cast is gated on the flag, and the token path produces integer ids anyway.
  • No silent dtype change for text models was possible to introduce here; the regression tests assert the token path is not cast.

Same three combinations, before and after:

model dtype sample dtype before after
fp32 fp32 ok ok
bf16 bf16 ok ok
bf16 fp32 RuntimeError in the first matmul ok, finite action_preds

Tests

tests/test_decision_transformer_calibration_dtype_4755.py   7 passed
tests/test_decision_transformer_4755.py                     5 passed
tests/module_tree/test_decision_transformer_model_tree.py   6 passed
tests/test_calibration_data_device.py                      17 passed, 5 skipped
format/check.sh                                            clean

The end-to-end test is the reported failure: bf16 load, float32 rows, quantize, then action_preds must be finite and shaped (1, 8, 3). The rest pin the boundaries - integer rows stay integer, an fp32 model is left alone, the cast is a no-op when dtypes agree, and a definition without the flag is not cast.

Note

Related to #4755, which is closed by #4894. This is the one gap my evaluation did not catch: I tested the shipped feature with the model loaded at its default dtype and float32 samples and hit it, which is likely how a user meets it too.

…dtype

The tensor-calibration path added in ModelCloud#4894 hands the caller's own tensors to
`DecisionTransformerModel.forward`. Loading at the default bfloat16 and passing
`torch.randn` rows - float32, the natural spelling - fails inside the first
matmul:

    RuntimeError: mat1 and mat2 must have the same dtype, but got Float and BFloat16

The error comes from torch's kernel at capture time, not from the loader, and it
says nothing about which calibration key or which dtype pair is wrong.

`move_input_capture_example` now casts floating rows to the model's weight dtype
for definitions that declare `calibration_inputs = "tensor_kwargs"`. Integer rows
(timesteps, attention masks) keep their dtype, and every other definition is
untouched because the cast is gated on that flag.

    tests/test_decision_transformer_calibration_dtype_4755.py   7 passed
    tests/test_decision_transformer_4755.py                     5 passed
    tests/module_tree/test_decision_transformer_model_tree.py   6 passed
    tests/test_calibration_data_device.py                      17 passed, 5 skipped
    format/check.sh                                            clean

The new tests cover the reported failure end to end (bf16 model, float32 rows,
quantize, finite `action_preds`), the integer rows staying integer, an fp32 model
being left alone, the same cast being a no-op when the dtypes already agree, and
the token path not being cast.
@Qubitium
Qubitium merged commit c25762b into ModelCloud:main Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants