Skip to content

feat: Auto-detect and convert Tunix post-training checkpoints on load - #4951

Open
hsuan-lun-chiang wants to merge 1 commit into
mainfrom
feat/convert-post-training-tunix-onload
Open

feat: Auto-detect and convert Tunix post-training checkpoints on load#4951
hsuan-lun-chiang wants to merge 1 commit into
mainfrom
feat/convert-post-training-tunix-onload

Conversation

@hsuan-lun-chiang

@hsuan-lun-chiang hsuan-lun-chiang commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Overview

This PR updates the checkpoint loader in MaxText to auto-detect and dynamically convert Tunix post-training checkpoints on load, allowing downstream MaxText pre-training runs to restore from Tunix DPO/SFT checkpoints seamlessly.

Key Changes

  • Auto-Detection: Identifies Tunix-formatted checkpoints by inspecting the checkpoint layout (presence of model_params collection).
  • On-the-fly PyTree Reshaping:
    • Dynamically unwraps Optax inject_hyperparams wrapper states (_drop_inject_hyperparams).
    • Strips LoRA adapter layers (_drop_adapter_level) when mapping from adapter states to base pre-training states, handling dicts, lists, tuples, and namedtuples.
    • Correctly repacks base adapter levels (_add_adapter_level) when querying metadata or loading checkpoints with has_base=True.
  • model_creation_utils.from_pretrained: Directs parameter inspection and single-item PyTree restore to model_params when loading Tunix checkpoints.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

@google-cla

google-cla Bot commented Aug 20, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request implements on-the-fly detection and conversion of Tunix-formatted checkpoints during the pre-training load process, avoiding manual checkpoint conversion. The feedback highlights several critical issues: first, setting metadata to None on exception in model_creation_utils.py will cause an AttributeError when accessing metadata.item_metadata later; second, _drop_adapter_level must recurse into lists, tuples, and namedtuples to handle nested optimizer states; and third, an _add_adapter_level helper is required to correctly wrap optimizer state fields in base when restoring adapter checkpoints to prevent structure mismatches.

Comment thread src/maxtext/utils/model_creation_utils.py Outdated
Comment thread src/maxtext/common/checkpointing.py
Comment thread src/maxtext/common/checkpointing.py
Comment thread src/maxtext/common/checkpointing.py Outdated
@hsuan-lun-chiang
hsuan-lun-chiang force-pushed the feat/convert-post-training-tunix-onload branch from 0494c94 to 90aea05 Compare August 20, 2026 07:21
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 26.74419% with 126 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/maxtext/common/checkpointing.py 20.14% 104 Missing and 7 partials ⚠️
src/maxtext/utils/model_creation_utils.py 51.61% 8 Missing and 7 partials ⚠️

📢 Thoughts on this report? Let us know!

@hsuan-lun-chiang
hsuan-lun-chiang force-pushed the feat/convert-post-training-tunix-onload branch 6 times, most recently from b89495f to 8343327 Compare August 24, 2026 08:14
- Add auto-detection for Tunix-formatted checkpoints (checks for `model_params`)
- Implement on-the-fly conversion logic (`_load_tunix_full_state_from_path`) during load
- Seamlessly reshape Tunix `TrainState` in memory (strip `inject_hyperparams`, drop `base` adapter)
- Support both full state and parameters-only restores
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.

1 participant