42e commited on
Commit
3ee87d5
·
verified ·
1 Parent(s): d77f1d4

Upload folder using huggingface_hub

Browse files
src/mathcompose/eval/runners.py CHANGED
@@ -35,8 +35,15 @@ class HFRunner:
35
  self.max_context = max_context
36
  self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
37
 
38
- torch_dtype = {"auto": "auto", "bf16": torch.bfloat16, "fp16": torch.float16,
39
- "fp32": torch.float32}.get(dtype, "auto")
 
 
 
 
 
 
 
40
  # transformers 5.x: `dtype=` (torch_dtype is deprecated)
41
  model = AutoModelForCausalLM.from_pretrained(base_id, dtype=torch_dtype)
42
  if adapter_path:
 
35
  self.max_context = max_context
36
  self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
37
 
38
+ if dtype == "auto":
39
+ if self.device == "cuda":
40
+ bf16_ok = torch.cuda.get_device_capability()[0] >= 8 # Ampere+; T4 is fp16
41
+ torch_dtype = torch.bfloat16 if bf16_ok else torch.float16
42
+ else:
43
+ torch_dtype = torch.float32
44
+ else:
45
+ torch_dtype = {"bf16": torch.bfloat16, "fp16": torch.float16,
46
+ "fp32": torch.float32}.get(dtype, torch.float32)
47
  # transformers 5.x: `dtype=` (torch_dtype is deprecated)
48
  model = AutoModelForCausalLM.from_pretrained(base_id, dtype=torch_dtype)
49
  if adapter_path:
src/mathcompose/train/lora.py CHANGED
@@ -17,13 +17,14 @@ def build_lora_config(cfg: dict):
17
  )
18
 
19
 
20
- def build_bnb_config(cfg: dict):
21
- """4-bit NF4 config. Requires bitsandbytes + CUDA."""
 
22
  import torch
23
  from transformers import BitsAndBytesConfig
24
 
25
  q = cfg["quant"]
26
- compute_dtype = getattr(torch, q.get("bnb_4bit_compute_dtype", "bfloat16"))
27
  return BitsAndBytesConfig(
28
  load_in_4bit=q.get("load_in_4bit", True),
29
  bnb_4bit_quant_type=q.get("bnb_4bit_quant_type", "nf4"),
 
17
  )
18
 
19
 
20
+ def build_bnb_config(cfg: dict, compute_dtype=None):
21
+ """4-bit NF4 config. Requires bitsandbytes + CUDA. ``compute_dtype`` overrides
22
+ the config value (used to force fp16 on GPUs without bf16, e.g. T4)."""
23
  import torch
24
  from transformers import BitsAndBytesConfig
25
 
26
  q = cfg["quant"]
27
+ compute_dtype = compute_dtype or getattr(torch, q.get("bnb_4bit_compute_dtype", "bfloat16"))
28
  return BitsAndBytesConfig(
29
  load_in_4bit=q.get("load_in_4bit", True),
30
  bnb_4bit_quant_type=q.get("bnb_4bit_quant_type", "nf4"),
src/mathcompose/train/train.py CHANGED
@@ -43,6 +43,13 @@ def build_and_train(args) -> str:
43
  val_file = args.val_file or cfg["data"].get("val_path")
44
 
45
  has_cuda = torch.cuda.is_available()
 
 
 
 
 
 
 
46
  use_4bit = (not args.no_4bit) and cfg["quant"]["load_in_4bit"] and has_cuda
47
  if cfg["quant"]["load_in_4bit"] and not has_cuda and not args.no_4bit:
48
  print("[warn] 4-bit requested but no CUDA — falling back to full-precision "
@@ -50,12 +57,9 @@ def build_and_train(args) -> str:
50
 
51
  tok = load_tokenizer(base_id, pad_token=cfg["model"]["pad_token"])
52
 
53
- model_kwargs = {}
54
  if use_4bit:
55
- model_kwargs["quantization_config"] = build_bnb_config(cfg)
56
- model_kwargs["dtype"] = torch.bfloat16
57
- else:
58
- model_kwargs["dtype"] = torch.bfloat16 if has_cuda else torch.float32
59
  model = AutoModelForCausalLM.from_pretrained(base_id, **model_kwargs)
60
 
61
  data_files = {"train": train_file}
@@ -84,7 +88,8 @@ def build_and_train(args) -> str:
84
  packing=t.get("packing", False),
85
  gradient_checkpointing=grad_ckpt,
86
  optim=optim,
87
- bf16=has_cuda,
 
88
  seed=t.get("seed", 0),
89
  report_to=(["wandb"] if args.wandb else "none"),
90
  max_steps=args.max_steps if args.max_steps else -1,
 
43
  val_file = args.val_file or cfg["data"].get("val_path")
44
 
45
  has_cuda = torch.cuda.is_available()
46
+ # bf16 needs Ampere+ (sm_80): A100/L4/H100 yes, T4 (sm_75) no -> use fp16 there.
47
+ bf16_ok = has_cuda and torch.cuda.get_device_capability()[0] >= 8
48
+ compute_dtype = torch.bfloat16 if bf16_ok else (torch.float16 if has_cuda else torch.float32)
49
+ if has_cuda:
50
+ cap = torch.cuda.get_device_capability()
51
+ print(f"[gpu] {torch.cuda.get_device_name(0)} (sm_{cap[0]}{cap[1]}) -> "
52
+ f"{'bf16' if bf16_ok else 'fp16'} compute")
53
  use_4bit = (not args.no_4bit) and cfg["quant"]["load_in_4bit"] and has_cuda
54
  if cfg["quant"]["load_in_4bit"] and not has_cuda and not args.no_4bit:
55
  print("[warn] 4-bit requested but no CUDA — falling back to full-precision "
 
57
 
58
  tok = load_tokenizer(base_id, pad_token=cfg["model"]["pad_token"])
59
 
60
+ model_kwargs = {"dtype": compute_dtype}
61
  if use_4bit:
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}
 
88
  packing=t.get("packing", False),
89
  gradient_checkpointing=grad_ckpt,
90
  optim=optim,
91
+ bf16=bf16_ok,
92
+ fp16=(has_cuda and not bf16_ok),
93
  seed=t.get("seed", 0),
94
  report_to=(["wandb"] if args.wandb else "none"),
95
  max_steps=args.max_steps if args.max_steps else -1,