Skip to content

Fix TrackioCallback fails to log evaluation metrics after training ends#46935

Merged
lewtun merged 10 commits into
mainfrom
fix-trackio-sync
Jul 21, 2026
Merged

Fix TrackioCallback fails to log evaluation metrics after training ends#46935
lewtun merged 10 commits into
mainfrom
fix-trackio-sync

Conversation

@lewtun

@lewtun lewtun commented Jun 27, 2026

Copy link
Copy Markdown
Member

CI

What does this PR do?

This PR fixes the following issue: TrackioCallback.on_train_end() calls trackio.finish(), which clears Trackio’s active run. However, the callback keeps _initialized=True. If user code calls trainer.evaluate() or trainer.predict() after trainer.train(), the callback later receives on_log()/on_predict(). Because _initialized is still True, it skips setup() and calls trackio.log() without an active Trackio run, raising:

RuntimeError: Call trackio.init() before trackio.log().

The fix is to reset TrackioCallback._initialized after trackio.finish() in on_train_end(). Then subsequent logging re-runs setup() and reinitializes/resumes the Trackio run before logging metrics.

I've also added a regression test which triggers the problem on main and verified the fix resolves it.

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.

PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.

This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read CONTRIBUTING.md.

  • I confirm that this is not a pure code agent PR.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline and the
    Pull Request checks?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes according to the guidelines?
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@lewtun lewtun changed the title Fix trackio sync Fix TrackioCallback fails to log evaluation metrics after training ends Jun 27, 2026
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

is_torch_available,
)
from transformers.integrations.integration_utils import KubeflowCallback, SwanLabCallback
from transformers.integrations.integration_utils import KubeflowCallback, SwanLabCallback, TrackioCallback

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added a regression test since we didn't have any at all for trackio. Happy to remove it if that's preferred

@lewtun

lewtun commented Jun 27, 2026

Copy link
Copy Markdown
Member Author

The failing tests seem unrelated to my changes

@qgallouedec qgallouedec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Didn't try myself but the justification and fix lgtm

callback.on_train_end(args, state, control, model=model)
callback.on_log(args, state, control, model=model, logs={"eval_loss": 0.5})

self.assertEqual(fake_trackio.init.call_count, 2)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is init called twice? 👀

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah because it's called once on .setup() and then again on .on_log() because .on_train_end() sets self._initialized = False

Happy to change to test to something more explicit / instructive - I took a best guess here :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not a big deal at all but maybe we just move the .on_log() above the .on_train_end(), which would be the more standard usage anyways?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh I was trying to simulate the scenario where a script has finished training and then afterwards tries to log a new value to the same trackio job (e.g. running trainer.evaluate() after trainer.train()). That's why the .on_log() appears after .on_train_end() but let me add an explicit log step for training to make it clearer

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Clarified in 8c2febe

@abidlabs abidlabs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks very reasonable! Just a qq above

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Feel free to merge 🤗

@lewtun
lewtun added this pull request to the merge queue Jul 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29828915901:1
Result: success | Jobs: 15 | Tests: 172,445 | Failures: 0 | Duration: 15h 18m

Merged via the queue into main with commit 504a5fa Jul 21, 2026
104 checks passed
@lewtun
lewtun deleted the fix-trackio-sync branch July 21, 2026 12:39
SangbumChoi added a commit to SangbumChoi/transformers that referenced this pull request Jul 22, 2026
* upstream/main: (39 commits)
  Remove deprecated training args and `is_fast` property (huggingface#46917)
  Consistent output shape from `get_image_features` (huggingface#46405)
  Fix multi-device mxfp4 dequantization race in `_convert_moe_packed_tensors` (huggingface#47423)
  fix failed test cases for qwen3_omni_moe model (huggingface#47449)
  Fix Hunyuan-VL PIL image resize parity with reference preprocessing (huggingface#47233)
  Move `value` padding into the attention interfaces that need it (huggingface#47451)
  Simplify function dispatch for linear attention (huggingface#47450)
  [cache] Allow sliding window layers to be roll-backed for speculative decoding (huggingface#47447)
  Fix double-shifted training loss in GitForCausalLM (huggingface#47395)
  Fix CohereASR training-loss double-shift (same as Moonshine fix huggingface#46784) (huggingface#46895)
  Warn when `group_by_length` is silently ignored for iterable datasets (huggingface#47379)
  Update bug report list (huggingface#46607)
  Fix shape mismatch in KyutaiSpeechToText `generate()` last window (huggingface#46952)
  Optimize flash attention max seqlen computation in vision attention (huggingface#47170)
  fix: remove unreachable return in special token builder (huggingface#47420)
  Add Harry to slow CI (huggingface#47454)
  BLT: vectorize patch length processing (huggingface#47385)
  Fix `TrackioCallback` fails to log evaluation metrics after training ends (huggingface#46935)
  [Kimi] add integration tests (huggingface#47383)
  Fix typo in `MusicgenForCausalLM.generate()` (huggingface#46974)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants