M16: mixed-precision training - #31
Merged
Merged
Conversation
Add Emily.MixedPrecision with dynamic loss scaling (LossScaler), cast_params/accumulate_grad for bf16/f32 conversion, and overflow detection. Fix Backend.wrap coerce to handle all MLX/Nx type mismatches (not just pred→u8), which surfaces when Nx.Defn.grad promotes output metadata to f32 while the MLX buffer stays bf16. Tests: 33 unit tests, bf16 grad equivalence (8 zoo functions), bf16 MLP curve-matching (50 steps), bf16 MNIST canary (training_full).
- Eliminate deep_cast: express as deep_apply + Nx.as_type closure
- Extract close?/4 and flunk_trajectory/5 to TrainingHelper (was
copy-pasted across 3 curve tests)
- Extract MNIST load/evaluate helpers to MnistHelper (was duplicated
between f32 and bf16 test modules)
- Collapse bf16/f16 tensor generators into a single clause that
delegates to the f32 generator + as_type
- Restore coerce fast path for {:u, 8} (avoids Native.dtype NIF
call on the most common mismatch type)
- Remove section comments on self-describing private functions
- Single-source LossScaler default scale via @default_scale attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Emily.MixedPrecisionmodule withLossScalerstruct,cast_params/2,accumulate_grad/2,loss_scale/1,scale_loss/2,unscale/2,update/2, andhas_overflow?/1— the standard bf16 activations + f32 master weights + dynamic loss scaling recipeEmily.Backend.wrapcoerce to detect and correct all MLX buffer vs Nx metadata type mismatches (previously only handled pred→u8), unblocking bf16 grad computationTest plan
mix precommit— 0 failures (438 tests, 73 properties, 1 doctest)test/emily/mixed_precision_test.exs— 33 unit tests covering all public API functionstest/emily/grad/bf16_grad_equivalence_test.exs— 8 zoo functions, Emily.Compiler vs BinaryBackend Evaluator in bf16test/emily/training/bf16_mlp_curve_test.exs— 50-step mixed-precision MLP, Emily vs BinaryBackend within rtol 5e-2mix test --only training_full— bf16 MNIST convergence canary (target ≥ 95.5%)