Repository navigation
Packed metadata: keep the tensor path under torch.compile, fall back to pageable copies - #15199
Merged
Merged
Conversation
…to pageable copies Under a user torch.compile(dynamic=True) the host tuple of packed lengths specialized the graph on the segment count, and pin_memory cannot be traced. While compiling, use the device tensor path again. When pinning host memory fails, copy from pageable memory instead of raising.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
danielhanchen
added a commit
that referenced
this pull request
Oct 25, 2026
…wrap test _decode_graphs (#15209) ## Summary Two merges left `Repo tests (CPU, rest)` red on main (for example on #15199, base `0705fa58`): 1. **#15174 (tensor parallelism v2)** reads `model.safetensors.index.json` with a bare `open()` at `unsloth/models/tp_native.py:1636` and `:1660`, and its new test does the same at `tests/test_tp_native_oracle.py:697`. #15185 (padded batches vs padding-free) added `tests/test_padding_choice.py:267` with a bare `read_text()`. The two encoding guards flag all four: ``` tests/test_runtime_text_encoding.py::test_shipping_code_names_an_encoding ['unsloth/models/tp_native.py:1660: open()', 'unsloth/models/tp_native.py:1636: open()'] tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding ['tests/test_padding_choice.py:267: read_text()', 'tests/test_tp_native_oracle.py:697: open()'] ``` The two `tp_native.py` sites are product code, and the guard is right about them: a checkpoint index read under a non-UTF-8 Windows locale can fail or mis-decode. 2. **#15188 (decode graphs on by default for RL)** wrapped `unsloth_unwrap_model_for_generation`'s generate in `_decode_graphs.rl_generation()` and calls `_decode_graphs.release_for_training(model)` on the way back to training. `tests/test_ppo_trainer_patches.py` lifts that function out of `rl.py` with `ast` and never gave it `_decode_graphs`, so both unwrap tests stop with `NameError: name '_decode_graphs' is not defined`. ## Change - `tp_native.py`, `test_tp_native_oracle.py`, `test_padding_choice.py`: `encoding = "utf-8"` on the four reads. - `test_ppo_trainer_patches.py`: the lifted function gets a `_decode_graphs` whose `rl_generation` is the real scope from `unsloth/models/_decode_graphs.py` (loaded by file path), with entering it and `release_for_training` recorded in the existing `calls` list. The PPO-policy test now asserts the full order: `for_inference`, `rl_generation`, `release_for_training`, `for_training`. The plain-model test asserts generation happens inside the scope before the release. ## Verification - On main: the two encoding guards fail and both unwrap tests raise `NameError`. With this change the encoding guards give `25 passed`, and `test_ppo_trainer_patches.py`, `test_padding_choice.py` and `test_tp_native_oracle.py` give `114 passed, 4 skipped`. - Sabotage, each against `rl.py`: 1. generate outside `_decode_graphs.rl_generation()`: both unwrap tests fail 2. no `release_for_training` before training: both unwrap tests fail - `tests/test_tp_native_gloo.py::test_tp2_x_fsdp2_dp2_matches_one_process` (5 cases) passes on main locally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
#15133 removed a host sync from
get_packed_info_from_kwargsby reading the packed lengths back once as a host tuple (tuple(lengths.tolist())) and staging host lengths through one pinnednon_blockingcopy. Two side effects:torch.compile(..., dynamic = True)the host tuple specializes the graph on the number of segments, so every new segment count is a recompile. CPU repro over 12 segment counts (3 to 14): 4 graphs before Remove hidden host-device syncs from per-step training paths #15133, 12 after, and the recompile limit is hit.staged.pin_memory()runs inside the traced region on a cold compile and cannot be traced (aten._pin_memoryis not implemented for the tracing tensors). Pinning also had no fallback when host memory cannot be pinned (pinned memory limit, WSL), here and in the DDP present mask inrl.py.Change
unsloth/utils/packing.py: whentorch.compiler.is_compiling(),get_packed_info_from_kwargstakes the pre-Remove hidden host-device syncs from per-step training paths #15133 tensor path (device lengths, device cumsum,lengths.max().item()), with nopin_memoryand no host tuple. The downstream mask helpers then find no cached host lengths and take their existing route. The eager path is unchanged, so the sync removal from Remove hidden host-device syncs from per-step training paths #15133 stays._pinned_to_device: pinnednon_blockingcopy, or a plain pageable copy whenpin_memory()raises.unsloth/models/rl.py_unsloth_average_gradients: the same fallback for the DDP present mask.Tests
tests/utils/test_packing_host_sync.py(CPU):dynamic = Trueover 12 segment counts traces at most 4 graphs and returns the right values;_pinned_to_devicereturns the same tensor whenpin_memoryraises.GPU (Colab T4, torch 2.11.0+cu130). Each case ran in a fresh process so the compile cache starts cold, with the same probe on the
packing.pyfrom main and from this branch:torch.compile(default), 12 segment counts, fwd + bwdNYI: aten._pin_memory.defaulttorch.compile(dynamic = True), sameNYI: aten._pin_memory.defaultpin_memoryraisingRuntimeErrorset_sync_debug_mode("warn")The last row shows the eager path from #15133 still makes no sync.
Not covered
rl.pyDDP fallback has no test of its own (it needs CUDA params plus a process group). It is the same three-line try / except as_pinned_to_device.pin_memorythat raises) was not reproduced.