Upload code/faz3_v6/meshai_train/lora_inject.py with huggingface_hub
Browse files
code/faz3_v6/meshai_train/lora_inject.py
CHANGED
|
@@ -53,9 +53,18 @@ def inject_lora_into_linears(
|
|
| 53 |
break
|
| 54 |
replaced: list[str] = []
|
| 55 |
for name, lin in picked:
|
|
|
|
|
|
|
|
|
|
| 56 |
w = lin.weight.detach().float().cpu()
|
| 57 |
lora = FrozenLinearLoRA(w, rank=rank, name=name)
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
_set_module_by_name(root, name, lora)
|
| 60 |
replaced.append(name)
|
| 61 |
log_fn(f"[faz3-lora] injected {len(replaced)} LoRA layers (rank={rank})")
|
|
|
|
| 53 |
break
|
| 54 |
replaced: list[str] = []
|
| 55 |
for name, lin in picked:
|
| 56 |
+
# Keep original device/dtype (Paint often fp16)
|
| 57 |
+
device = lin.weight.device
|
| 58 |
+
dtype = lin.weight.dtype
|
| 59 |
w = lin.weight.detach().float().cpu()
|
| 60 |
lora = FrozenLinearLoRA(w, rank=rank, name=name)
|
| 61 |
+
lora = lora.to(device=device, dtype=dtype)
|
| 62 |
+
# Ensure LoRA params stay trainable float32 for Adam stability when base is half
|
| 63 |
+
if dtype != torch.float32:
|
| 64 |
+
lora.lora_A.data = lora.lora_A.data.float()
|
| 65 |
+
lora.lora_B.data = lora.lora_B.data.float()
|
| 66 |
+
# weight buffer matches module dtype for matmul with activations
|
| 67 |
+
lora.weight.data = lora.weight.data.to(dtype=dtype)
|
| 68 |
_set_module_by_name(root, name, lora)
|
| 69 |
replaced.append(name)
|
| 70 |
log_fn(f"[faz3-lora] injected {len(replaced)} LoRA layers (rank={rank})")
|