Repository navigation
Fix/casting continue pretraining - #1200
Conversation
…ure correct dtype usage
…in mixed precision training
…at32 usage in mixed precision training
…pe usage in mixed precision training
|
Previously I drafted this because somehow I can't use BS 2 just like the example. BUt now I can use BS 2. So opening this PR instead of Draft |
|
Thankyou so much for bringing this up in PR man🙏🙏 |
|
Oh I totally missed float16 cannot be used and only bfloat16 can be used for continued pretraining - nice catch! |
|
I believe this change lead to error while saving model with During saving model with unsloth/save.py (551) For |
* Bring back float32 if float16 instead of bfloat16 * Refactor mixed precision handling for lm_head and embed_tokens to ensure correct dtype usage * Fix dtype retrieval for embed_tokens and lm_head in mixed precision training * Fix dtype retrieval for embed_tokens and lm_head to use weight dtype in mixed precision training * Fix dtype handling for embed_tokens and lm_head to ensure correct float32 usage in mixed precision training * Fix dtype assignment for lm_head modules to ensure correct weight dtype usage in mixed precision training
…g on the SentenceTransformer fast encoder path (unslothai#14150) `FastSentenceTransformer.get_peft_model` on the fast encoder path (BERT/MPNet-style models) built its `LoraConfig` without `layers_to_transform`, `layers_pattern`, `use_rslora` or `modules_to_save`, so they were accepted and silently ignored. `use_rslora=True` trained with alpha/r scaling instead of alpha/sqrt(r), and `layers_to_transform` still adapted every layer. Fix: pass the four arguments through, as the other branch already does. Test: new `tests/test_st_fast_encoder_lora_config.py` on a tiny BERT, CPU. On main it fails (`use_rslora` False; 13 adapters, scaling 2.0). With the fix it passes (6 adapters on layer 1 only, scaling 4.0). The other sentence-transformer tests I ran still pass. Not run on GPU. Follow-up fix: with `modules_to_save` now honoured, a model loaded in float16 (T4, V100) kept its trainable copies in float16 while the LoRA weights were float32, so the first optimizer step raised `ValueError: Attempting to unscale FP16 gradients.` The fast encoder path now calls `_unfreeze_modules_to_save`, the same helper the FastModel path uses, which upcasts float16 copies to float32 (unslothai#1200). Added `test_float16_modules_to_save_copy_trains_in_float32`, which fails without the upcast. Verified on GPU with `sentence-transformers/all-MiniLM-L6-v2` (r=8, alpha=16, layers [4, 5], rsLoRA, modules_to_save, 20 steps): base trains LoRA on every layer plus `pooler.dense` with 337920 trainable parameters at scaling 2.0; after this PR LoRA lands on layers 4 and 5 only, scaling is 16/sqrt(8), and the trainable count matches plain PEFT (111360). On a Colab T4 in float16 the original change crashed with the error above and the fixed head trains and saves a merged model. --------- Co-authored-by: Daniel Han <[email protected]>
Theres' this issue of attempting unscale FP16 gradients
After investigation, this is because of global dtype, which is when we use it on colab, we will use
torch.float16instead oftorch.bfloat16. This error does not happened if we usetorch.bfloat16(my own RTX4090 for example). So we need to usetorch.float32on device that is still usingtorch.float16Here is the result on bfloat16

Abd here's the result on float32 (colab)
