Skip to content
Merged
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
17 changes: 13 additions & 4 deletions tunix/sft/peft_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def __init__(
self._buffered_eval_metrics: MetricsBuffer | None = None
self.training_hooks = None
self.data_hooks = None
self._jit_cache = set()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This cache is used to track JIT compilation cache sizes to avoid verbose logging. However, it appears this set is never cleared during the lifetime of the trainer instance. If the trainer is reused in a way that causes recompilations (e.g., by calling with_loss_fn or with_gen_model_input_fn), this cache will not be reset. This could lead to misleading logs, as new compilations might not be reported if their cache size has been seen before.

Consider clearing this set in the clear_jit_cache method, alongside resetting _jitted_train_step_fn and _jitted_eval_step_fn.


def with_training_hooks(self, training_hooks: hooks.TrainingHooks):
self.training_hooks = training_hooks
Expand Down Expand Up @@ -565,13 +566,21 @@ def train(
cache_nnx_graph: bool = False,
) -> None:
"""Training loop."""
logging.log_first_n(
logging.INFO,
f"Training with mesh: {pxla.thread_resources.env.physical_mesh}",
1,
)
train_step, eval_step = self.jit_train_and_eval_step(skip_jit)
if not skip_jit:
logging.info(
"Training with mesh: %s. Compiled train_step cache size: %s",
pxla.thread_resources.env.physical_mesh,
train_step.jitted_fn._cache_size(), # pytype: disable=attribute-error,protected-access
cache_size = train_step.jitted_fn._cache_size() # pytype: disable=attribute-error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The pytype: disable comment is missing protected-access. Accessing _cache_size is an access to a protected member. It would be good to add it back for correctness of static analysis suppression.

Suggested change
cache_size = train_step.jitted_fn._cache_size() # pytype: disable=attribute-error
cache_size = train_step.jitted_fn._cache_size() # pytype: disable=attribute-error,protected-access

logging.log_if(
logging.INFO,
f"Compiled train_step cache size: {cache_size}",
lambda: cache_size not in self._jit_cache,
)
self._jit_cache.add(cache_size)

if cache_nnx_graph:
# For performance, cache the nnx graph traversals. However, the training
# loop must _not_ modify the model or optimizer graph in this case. For
Expand Down