From 98c78e2ab3f56d95aa889ea0a5b7da44801c0385 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Thu, 26 Nov 2026 21:59:35 +0530 Subject: [PATCH] fix(raft): use self.tmp_model in gradient clipping and fix vision feature error message RaftTrainer._one_train referenced an undefined 'model' in the gradient clipping branch (a leftover from transformers.Trainer), raising NameError whenever max_grad_norm > 0 without DeepSpeed. Use self.tmp_model, which is what the rest of the method trains. Also report select_vision_feature_type in the ValueError raised by Vision2Seq.vision_feature_select instead of the non-existent self.select_feature. Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- src/lmflow/models/vision2seq_model.py | 2 +- src/lmflow/pipeline/utils/raft_trainer.py | 6 ++-- tests/pipeline/test_raft_trainer_names.py | 42 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 tests/pipeline/test_raft_trainer_names.py diff --git a/src/lmflow/models/vision2seq_model.py b/src/lmflow/models/vision2seq_model.py index cfd92e9a8..b0acd73f8 100644 --- a/src/lmflow/models/vision2seq_model.py +++ b/src/lmflow/models/vision2seq_model.py @@ -140,7 +140,7 @@ def vision_feature_select(self, image_forward_outs): elif self.select_vision_feature_type == "cls_patch": image_features = image_features else: - raise ValueError(f"Unexpected select feature: {self.select_feature}") + raise ValueError(f"Unexpected select feature: {self.select_vision_feature_type}") return image_features def register_prompt_cache(self, prompt_ids, prompt_keys_values): diff --git a/src/lmflow/pipeline/utils/raft_trainer.py b/src/lmflow/pipeline/utils/raft_trainer.py index 0d89b18f2..f26e5f2f8 100644 --- a/src/lmflow/pipeline/utils/raft_trainer.py +++ b/src/lmflow/pipeline/utils/raft_trainer.py @@ -1836,13 +1836,13 @@ def _one_train( elif hasattr(self.optimizer, "clip_grad_norm"): # Some optimizers (like the sharded optimizer) have a specific way to do gradient clipping self.optimizer.clip_grad_norm(args.max_grad_norm) - elif hasattr(model, "clip_grad_norm_"): + elif hasattr(self.tmp_model, "clip_grad_norm_"): # Some models (like FullyShardedDDP) have a specific way to do gradient clipping - model.clip_grad_norm_(args.max_grad_norm) + self.tmp_model.clip_grad_norm_(args.max_grad_norm) else: # Revert to normal clipping otherwise, handling Apex or full precision nn.utils.clip_grad_norm_( - amp.master_params(self.optimizer) if self.use_apex else model.parameters(), + amp.master_params(self.optimizer) if self.use_apex else self.tmp_model.parameters(), args.max_grad_norm, ) diff --git a/tests/pipeline/test_raft_trainer_names.py b/tests/pipeline/test_raft_trainer_names.py new file mode 100644 index 000000000..0e55fd706 --- /dev/null +++ b/tests/pipeline/test_raft_trainer_names.py @@ -0,0 +1,42 @@ +"""Static regression test for undefined names in the RAFT trainer. + +``raft_trainer.py`` is adapted from ``transformers.Trainer``. Its ``_one_train`` method +once referenced a ``model`` variable that only exists in the upstream implementation, +which raised ``NameError`` as soon as gradient clipping was used. The file is parsed +with ``symtable`` so the test doesn't need ``transformers``/GPU. +""" + +import builtins +import symtable +from pathlib import Path + +RAFT_TRAINER = Path(__file__).resolve().parents[2] / "src/lmflow/pipeline/utils/raft_trainer.py" + + +def _find(table, name): + for child in table.get_children(): + if child.get_name() == name: + return child + found = _find(child, name) + if found is not None: + return found + return None + + +def test_one_train_has_no_undefined_global_names(): + source = RAFT_TRAINER.read_text() + module_table = symtable.symtable(source, str(RAFT_TRAINER), "exec") + module_names = {s.get_name() for s in module_table.get_symbols() if s.is_assigned() or s.is_imported()} + + one_train = _find(module_table, "_one_train") + assert one_train is not None + + undefined = [ + s.get_name() + for s in one_train.get_symbols() + if s.is_global() + and s.is_referenced() + and s.get_name() not in module_names + and not hasattr(builtins, s.get_name()) + ] + assert undefined == []