feat(finetune): add bf16 mixed-precision via amp_dtype config#288
Open
Burton-David wants to merge 1 commit intoshiyu-coder:masterfrom
Open
feat(finetune): add bf16 mixed-precision via amp_dtype config#288Burton-David wants to merge 1 commit intoshiyu-coder:masterfrom
Burton-David wants to merge 1 commit intoshiyu-coder:masterfrom
Conversation
Wraps the forward + loss in torch.autocast for both the tokenizer and predictor training loops, gated by a new Config.amp_dtype field. Setting "bfloat16" enables bf16 autocast; None (default) keeps the existing FP32 path bit-exact. bf16 has the same exponent range as FP32, so AdamW master weights need no scaling and no GradScaler is wired in.
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
Adds optional bf16 mixed-precision to
train_tokenizer.pyandtrain_predictor.py, gated by a newConfig.amp_dtypefield.torch.autocast(device_type="cuda", dtype=torch.bfloat16, ...)wraps the forward and loss. Backward, grad clipping, andoptimizer.stepstay outside autocast. AdamW master weights are FP32 and bf16 keeps FP32's exponent range, so noGradScaleris needed.Numbers
Measured on RTX 4090 and A100 80GB, torch 2.4.1+cu124, against
NeoQuasar/Kronos-baseandNeoQuasar/Kronos-Tokenizer-base. Inputs are SPY 1-min OHLCV bars from yfinance, z-score per window, seq=512. Median of 30 timed iters after 5 warmup.Predictor step (tokenize, forward, loss, backward, clip, optimizer.step):
Peak memory drops 24-25% at every batch size on both GPUs. On A100 80GB, bf16 lets B=100 fit at 32 GB peak; FP32 needs 43 GB.
Tokenizer step (forward, recon + BSQ loss, backward, clip, optimizer.step):
At B=10 on the 4090 the tokenizer runs slower in bf16. The tokenizer is small (5.5M params) and at that batch size the autocast setup cost beats its compute win on Ada; from B=25 up it wins on both GPUs. Tokenizer peak memory drops 31-35% across the sweep.
Single-step loss vs FP32 from identical pretrained weights and identical inputs: predictor 1.3-1.7% rel, tokenizer 0.8-1.2% rel.
Compatibility
amp_dtype = Noneis the default and keeps the loop bit-exact with the current behavior. Opt-in viaConfig.amp_dtype = "bfloat16".