Repository navigation
fix(training): return float32 predictions under mixed precision - #540
Conversation
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
left a comment
There was a problem hiding this comment.
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.
Fixes #539
Problem
predict()reuses the fitTrainer. Afterfit(..., precision="bf16-mixed"), the network outputs were thereforebfloat16tensors, and the estimators'.cpu().numpy()conversion failed withTypeError: Got unsupported ScalarType BFloat16. This affectedpredict()of regressors and LSS models,predict_proba()of classifiers, and everything built on them (evaluate(),score(), andInferenceModel.from_estimator(...).predict()/.predict_proba()).Fix
TaskModel.predict_step(deeptab/training/lightning_module.py) upcastsfloat16/bfloat16outputs tofloat32before returning them. Every estimator's prediction path goes through it. Full-precision andfloat64outputs are untouched, and theReturnssection of the docstring documents the upcast.Tests
tests/integration/test_mixed_precision_predict.py(new file) fits withprecision="bf16-mixed"on CPU:test_regressor_predict_after_bf16_training:float32predictions.test_classifier_predict_proba_after_bf16_training:float32probabilities summing to 1, and validpredict()labels.test_lss_predict_after_bf16_training:float32for bothraw=Falseandraw=True.All three fail on
mainand pass with this change.Docs
No change needed.
precisionis already documented as a Lightning option passed through byfit().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)