vanishingradient commited on
Commit
4b87130
·
verified ·
1 Parent(s): f5cb9ca

Add kaggle/train_grpo.py

Browse files
Files changed (1) hide show
  1. kaggle/train_grpo.py +23 -17
kaggle/train_grpo.py CHANGED
@@ -782,23 +782,24 @@ def main(cfg: Cfg | None = None) -> None:
782
  train, eval_problems = _load_swe_splits(cfg)
783
  _vlog("main.dataset_ready", train_n=len(train), eval_n=len(eval_problems))
784
 
785
- # T4 is sm75 — TRL chunked bf16 logprob kernels need sm80+ (A100/L4/…).
786
- # torch.cuda.is_bf16_supported() can still be True on T4 and then crash in backward.
787
- def _use_bf16() -> bool:
788
- if not torch.cuda.is_available():
789
- return False
790
- major, _minor = torch.cuda.get_device_capability(0)
791
- ok = major >= 8 and torch.cuda.is_bf16_supported()
792
- _vlog(
793
- "main.dtype_choice",
794
- bf16=ok,
795
- capability=torch.cuda.get_device_capability(0),
796
- device=torch.cuda.get_device_name(0),
797
- )
798
- return ok
799
-
800
- use_bf16 = _use_bf16()
801
  compute_dtype = torch.bfloat16 if use_bf16 else torch.float16
 
 
 
 
 
 
 
 
802
  bnb = BitsAndBytesConfig(
803
  load_in_4bit=True,
804
  bnb_4bit_compute_dtype=compute_dtype,
@@ -840,6 +841,11 @@ def main(cfg: Cfg | None = None) -> None:
840
  ],
841
  )
842
  model = get_peft_model(model, lora)
 
 
 
 
 
843
 
844
  output_dir = "/kaggle/working/grpo-out"
845
  Path(output_dir).mkdir(parents=True, exist_ok=True)
@@ -936,7 +942,7 @@ def main(cfg: Cfg | None = None) -> None:
936
  save_total_limit=3,
937
  report_to=["wandb"],
938
  bf16=use_bf16,
939
- fp16=not use_bf16,
940
  remove_unused_columns=False,
941
  hub_model_id=cfg.hub_model_id,
942
  hub_token=os.environ["HF_TOKEN"],
 
782
  train, eval_problems = _load_swe_splits(cfg)
783
  _vlog("main.dataset_ready", train_n=len(train), eval_n=len(eval_problems))
784
 
785
+ # T4 (sm75) cannot run TRL's bf16 chunked logprob kernels (need sm80+).
786
+ # Also: fp16 AMP GradScaler crashes if any grads stay bf16
787
+ # (`_amp_foreach_non_finite_check_and_unscale_cuda` not implemented for BFloat16).
788
+ # On T4: no AMP (fp16=False, bf16=False); keep 4-bit weights + fp16 compute dtype.
789
+ major, minor = (0, 0)
790
+ if torch.cuda.is_available():
791
+ major, minor = torch.cuda.get_device_capability(0)
792
+ use_bf16 = major >= 8 and torch.cuda.is_bf16_supported()
793
+ use_fp16 = False # avoid GradScaler + QLoRA dtype mismatch on T4
 
 
 
 
 
 
 
794
  compute_dtype = torch.bfloat16 if use_bf16 else torch.float16
795
+ _vlog(
796
+ "main.dtype_choice",
797
+ bf16=use_bf16,
798
+ fp16=use_fp16,
799
+ compute_dtype=str(compute_dtype),
800
+ capability=(major, minor),
801
+ device=torch.cuda.get_device_name(0) if torch.cuda.is_available() else None,
802
+ )
803
  bnb = BitsAndBytesConfig(
804
  load_in_4bit=True,
805
  bnb_4bit_compute_dtype=compute_dtype,
 
841
  ],
842
  )
843
  model = get_peft_model(model, lora)
844
+ # Keep trainable LoRA weights in fp32 so optimizer/grad-norm never sees bf16 under AMP-off.
845
+ for name, param in model.named_parameters():
846
+ if param.requires_grad and param.dtype != torch.float32:
847
+ _vlog("main.cast_trainable_fp32", name=name, from_dtype=str(param.dtype))
848
+ param.data = param.data.float()
849
 
850
  output_dir = "/kaggle/working/grpo-out"
851
  Path(output_dir).mkdir(parents=True, exist_ok=True)
 
942
  save_total_limit=3,
943
  report_to=["wandb"],
944
  bf16=use_bf16,
945
+ fp16=use_fp16,
946
  remove_unused_columns=False,
947
  hub_model_id=cfg.hub_model_id,
948
  hub_token=os.environ["HF_TOKEN"],