42e commited on
Commit
f7f5e56
·
verified ·
1 Parent(s): 169e9ea

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. src/mathcompose/train/train.py +14 -0
src/mathcompose/train/train.py CHANGED
@@ -62,6 +62,20 @@ def build_and_train(args) -> str:
62
  model_kwargs["quantization_config"] = build_bnb_config(cfg, compute_dtype=compute_dtype)
63
  model = AutoModelForCausalLM.from_pretrained(base_id, **model_kwargs)
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  data_files = {"train": train_file}
66
  if val_file and Path(val_file).exists():
67
  data_files["validation"] = val_file
 
62
  model_kwargs["quantization_config"] = build_bnb_config(cfg, compute_dtype=compute_dtype)
63
  model = AutoModelForCausalLM.from_pretrained(base_id, **model_kwargs)
64
 
65
+ if compute_dtype == torch.float16:
66
+ # On T4 the fp16 GradScaler can't unscale bf16 grads. transformers may load
67
+ # the non-quantized weights in the model's default bf16 regardless of dtype=,
68
+ # so LoRA inherits bf16. Force any remaining bf16 params to fp16 and pin the
69
+ # config dtype so PEFT doesn't recreate bf16 adapters from the stale config.
70
+ model.config.torch_dtype = torch.float16
71
+ cast = 0
72
+ for p in model.parameters():
73
+ if p.dtype == torch.bfloat16:
74
+ p.data = p.data.to(torch.float16)
75
+ cast += 1
76
+ if cast:
77
+ print(f"[dtype] cast {cast} bf16 param tensors -> fp16 (T4 AMP compatibility)")
78
+
79
  data_files = {"train": train_file}
80
  if val_file and Path(val_file).exists():
81
  data_files["validation"] = val_file