mirror of
https://github.com/kvcache-ai/ktransformers.git
synced 2026-11-05 01:07:28 +00:00
[GH-ISSUE #2145] kt-sft + FSDP2: DTensor _foreach_mul_ crash when kt expert-LoRA params mix with DTensor lists in grad clipping #1288
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
kvcache-ai/ktransformers#1288
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @PUMA-SPz on GitHub (Aug 8, 2026).
Original GitHub issue: https://github.com/kvcache-ai/ktransformers/issues/2145
[BUG] kt-sft under FSDP2: expert-LoRA params stay plain
torch.Tensorwhile FSDP2 params areDTensor, so any_foreach_op overmodel.parameters()failsComponent: kt-sft expert-LoRA parameter registration (interaction with FSDP2 + HF Trainer)
Version: kt-kernel / ktransformers 0.6.4, torch 2.9.1+cu128, transformers-kt 5.6.0,
accelerate-kt 1.14.0, LLaMA-Factory 0.9.6.dev0
Config: the shipped
examples/ktransformers/accelerate/fsdp2_kt_bf16.yaml(also reproduced with
num_processes: 1)Symptom
Why it is unavoidable today
module must have its parameters and buffers on device cuda:0 but found one of them on device: cpuDTensor, but kt's expert-LoRAparams remain plain
torch.Tensor._foreach_op over the combinedmodel.parameters()list then raises.There are TWO call sites, and both must be handled
This is the part that makes it look unfixable — disabling one leaves the other:
Trainer._clip_grad_norm-> real clipping. Only runs whenmax_grad_norm > 0.Trainer._get_grad_norm-> callsclip_grad_norm_(model.parameters(), float("inf"))purely to log the gradient norm, and therefore fires even when clipping is disabled:
Setting
max_grad_norm: 0.0alone still crashes (site 2). Patching site 2 alone still crashesonce clipping is on (site 1).
Current workaround (not a fix)
max_grad_norm: 0.0, and_get_grad_normcall intry/except RuntimeErrorand returnNoneon a DTensor error.With both, LoRA SFT trains successfully: DeepSeek-V2-Lite, 20 steps in 474 s, ~12.5 s/step steady
state, loss 11.78 -> 10.81, 305 MB adapter written. But gradient clipping is disabled, which is
not acceptable for real training runs.
Proposed proper fix
Register kt's expert-LoRA parameters as DTensor (replicated placement is sufficient) when
running under FSDP2, so
model.parameters()is type-homogeneous. That fixes both call sites atonce, restores gradient clipping, and should also unblock multi-GPU (currently only
num_processes: 1has been validated here).Alternative/complementary: have kt exclude its expert-LoRA params from the parameter list handed
to the Trainer/accelerator, and clip them separately.
Note for
transformers_get_grad_normperforming a distributed collective purely for logging is arguably fragile inits own right — a
try/exceptdegrade-to-Nonethere would make mixed-param setups survivableregardless of what the backend does.