Skip to content

fix(training): return float32 predictions under mixed precision - #540

Merged
mkumar73 merged 2 commits into
mainfrom
fix/bf16-predict
Nov 29, 2026
Merged

mkumar73 merged 2 commits into
mainfrom
fix/bf16-predict

Conversation

@ChrisW09

@ChrisW09 ChrisW09 commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #539

Problem

predict() reuses the fit Trainer. After fit(..., precision="bf16-mixed"), the network outputs were therefore bfloat16 tensors, and the estimators' .cpu().numpy() conversion failed with TypeError: Got unsupported ScalarType BFloat16. This affected predict() of regressors and LSS models, predict_proba() of classifiers, and everything built on them (evaluate(), score(), and InferenceModel.from_estimator(...).predict()/.predict_proba()).

Fix

TaskModel.predict_step (deeptab/training/lightning_module.py) upcasts float16/bfloat16 outputs to float32 before returning them. Every estimator's prediction path goes through it. Full-precision and float64 outputs are untouched, and the Returns section of the docstring documents the upcast.

Tests

tests/integration/test_mixed_precision_predict.py (new file) fits with precision="bf16-mixed" on CPU:

  • test_regressor_predict_after_bf16_training: float32 predictions.
  • test_classifier_predict_proba_after_bf16_training: float32 probabilities summing to 1, and valid predict() labels.
  • test_lss_predict_after_bf16_training: float32 for both raw=False and raw=True.

All three fail on main and pass with this change.

Docs

No change needed. precision is already documented as a Lightning option passed through by fit().

Checks

  • just test: 3239 passed, 46 skipped, 28 xfailed; coverage 91.29%
  • just docs: build succeeded (warnings treated as errors)
  • just check: all hooks passed (ruff, prettier, pyright)

predict() reuses the fit Trainer, so after fit(precision="bf16-mixed") the
network outputs were bfloat16 tensors, and converting them with .numpy()
failed for regressors, classifiers' predict_proba() and LSS models.
Upcast half-precision outputs to float32 in TaskModel.predict_step.

Fixes #539

@jorisstrakeljahn jorisstrakeljahn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the test log of the red Windows job (minimum, core). I think the cast itself is fine, but the three new tests fail before the cast is reached.

They crash in the first forward pass with "RuntimeError: could not create a primitive". The Windows minimum extras job for the same commit uses the same torch 2.13.0 and passes these three tests, so I think it depends on the runner and not on torch.

After these three failures, 91 more tests in the same run fail with the same error, including tests that use the default float32 precision. On main this job is green. It looks like the failed bfloat16 call leaves the process broken.

One idea would be to check bfloat16 support in a separate process first and skip the tests if it fails, but I could not test this on a Windows machine.

The three tests pass on my Mac with this branch, so apart from the Windows job, LGTM.

@mkumar73
mkumar73 merged commit 5f6c5ce into main Nov 29, 2026
35 checks passed
@mkumar73 mkumar73 self-assigned this Nov 29, 2026
@mkumar73
mkumar73 deleted the fix/bf16-predict branch November 29, 2026 22:43
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.

[BUG] predict()/predict_proba() crash after fit(precision="bf16-mixed"): unsupported ScalarType BFloat16

3 participants