Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lmflow/models/vision2seq_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions src/lmflow/pipeline/utils/raft_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
42 changes: 42 additions & 0 deletions tests/pipeline/test_raft_trainer_names.py
Original file line number Diff line number Diff line change
@@ -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 == []