Skip to content

[EP] Make input_splits D2H non-blocking and simplify _permute - #3459

Merged
aditvenk merged 1 commit into
mainfrom
aditvenk/non-blocking
Jun 3, 2026
Merged

aditvenk merged 1 commit into
mainfrom
aditvenk/non-blocking

Conversation

@aditvenk

@aditvenk aditvenk commented May 29, 2026 •

Copy link
Copy Markdown
Contributor
  • Always use non_blocking=True for the input_splits device-to-host copy
  • Reuse the repeat_interleave unbacked symint (seg_ids.shape[0]) instead of recomputing the token total in _permute

Verifies fix made for pytorch/pytorch#182190

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 29, 2026
@aditvenk aditvenk added the ciflow/h100.8 Trigger H100.8 CI label May 29, 2026
@aditvenk

aditvenk commented May 29, 2026 •

Copy link
Copy Markdown
Contributor Author

TLParse: https://manifold.edge.x2p.facebook.net/v0/read/tree/logs/avenkataraman/4e3798e5-bd05-4f04-ab26-4d60a90a3eea/custom/rank_0/index.html?bucketName=tlparse_reports&apiKey=tlparse_reports-key&withPayload=1&timeoutMsec=10000

sync after copy_(.., True):

        buf64.copy_(buf63, True)
        _d2h_event_buf64 = torch.Event()
        _d2h_event_buf64.record()
        _d2h_event_buf64.synchronize()
        with torch.cuda._DeviceGuard(0):
            torch.cuda.set_device(0)
            buf65 = buf63; del buf63  # reuse
            # Topologically Sorted Source Nodes: [view_15, sum_2, output_splits], Original ATen: [aten.view, aten.sum, aten._to_copy]
            raw_stream0 = get_raw_stream(0)
            triton_poi_fused__to_copy_sum_view_12.run(buf58, buf65, 4, stream=raw_stream0)

@aditvenk
aditvenk marked this pull request as ready for review May 29, 2026 21:22

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@sanketpurandare mentioned that there are multiple unnecessary D2H syncs in this code.

There was an attempt by @xmfan to remove some, but that got reverted #3335


# For each output position, find its input position:
# output[p] = input[input_starts[seg] + (p - output_starts[seg])]
seg_ids = torch.arange(segment_lens.shape[0], device=device).repeat_interleave(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This line itself creates a cpu-gpu sync since we have not specified output_size to repeat_interleave

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can now pass the the output_size defined above to repeat_interleave.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought @xmfan tried this already but had to be reverted as Syed ran into some issues with 671B. #3335

input_starts[seg_ids]
+ torch.arange(total, device=device)
+ torch.arange(seg_ids.shape[0], device=device)
- output_starts[seg_ids]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is correct, using total here would introduce another cpu-gpu sync since total is a gpu scalar

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can use the output_size as defined above here instead.

num_global_tokens_per_local_expert_E.view(ep_size, -1)
.sum(dim=1)
.to(torch.device("cpu"), non_blocking=False)
)

@sanketpurandare sanketpurandare May 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should add here: output_size = output_splits.sum().item(). Since this is a cpu tensor, calling item here is not harmful and the benefit is, it will only create a single sym-int with correct range and not a single sym-int expression with a large range.

@sanketpurandare sanketpurandare left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I tried for quite a while I am unable to repro Syed's error but I think that can be addressed in future PR. So approving this!

@aditvenk aditvenk added ciflow/h100.8 Trigger H100.8 CI and removed ciflow/h100.8 Trigger H100.8 CI labels Jun 1, 2026
@aditvenk
aditvenk force-pushed the aditvenk/non-blocking branch from 4b631fb to 0574705 Compare June 1, 2026 20:38
- Always use non_blocking=True for the input_splits device-to-host copy
- Reuse the repeat_interleave unbacked symint (seg_ids.shape[0]) instead
  of recomputing the token total in _permute
@aditvenk
aditvenk force-pushed the aditvenk/non-blocking branch from 0574705 to d992479 Compare June 2, 2026 23:59
@aditvenk
aditvenk merged commit 1ab5a72 into main Jun 3, 2026
21 of 22 checks passed
SherlockNoMad added a commit that referenced this pull request Jun 4, 2026
…erics test (#3528)

## Why

Both GraphTrainer scheduled CI workflows were red. The workflows run
their test stages under `set -eux`, so the first failing command aborts
the rest — which means fixing one failure unmasks the next.

### Default workflow — gzip trace regression (real bug, masked by a
stale test)

[#3483](#3483) switched the
profiler to write gzip-compressed traces (`rank0_trace.json.gz`), but
the consumer wasn't audited. `_cudagraph_annotate_trace_file` in
`cudagraph.py` — attached to **every** graph_trainer run via
`configs.py` — still read the trace with plain `open()` + `json.load()`,
which raises `UnicodeDecodeError` on the gzip magic byte (`0x8b`). So
any profiled run with CUDA-graph annotations would crash in the
post-processor.

`test_profiler.py` only flagged the path-suffix change (`.json` →
`.json.gz`) because its `TestTracePostProcessorConfig` test used a
**dummy callback** that just records the path and never exercises the
real post-processor — so it masked the actual regression.

### Default workflow — `test_trace_module.py` optimizer Config drift
(unmasked by the fix above)

Once the profiler test stopped aborting the script early, the next stage
surfaced its own failure:
`test_trace_module.py::TestReparametrizeOptimizer::test_titan_optimizers_container`
raised `TypeError: OptimizersContainer.Config.__init__() got an
unexpected keyword argument 'name'`.
[#3269](#3269) ("support mixed
optimizers") reworked `OptimizersContainer.Config`: the flat
`name=`/`lr=` fields were replaced by a `param_groups` list of
`ParamGroupConfig` (`pattern` + `optimizer_name` + `optimizer_kwargs`).
The test still used the old API.

### H100 workflow — flaky DSv3 MoE-EP crash

`test_moe_dsv3_aot_fx_trace_vs_eager` fails as a flaky single-rank
crash: aot_fx_trace losses match the eager baseline **bitwise through
~step 12**, then one EP rank hard-crashes (others SIGTERM). Root cause
is unconfirmed — the crashing rank's traceback isn't captured because
the numerics tests run through `loss_compare.py` → `run_train.sh
--local-ranks-filter 0`, which tees only rank 0. (Note: the earlier
"input_splits D2H race" theory is ruled out —
[#3459](#3459) made that copy
non-blocking intentionally, and the following blocking `output_splits`
copy fences the stream.)

## What

- `cudagraph.py`: make `_cudagraph_annotate_trace_file` read/write
`.json.gz` via `gzip` (falls back to plain text for non-`.gz` paths).
- `test_profiler.py`: fix the stale assertion and add
`test_annotate_post_processor_round_trips_gzip_trace`, which drives the
**real** post-processor over a gzip trace so this path is actually
covered.
- `test_trace_module.py`: update `test_titan_optimizers_container` to
the new mixed-optimizer `OptimizersContainer.Config` API
(`param_groups=[ParamGroupConfig(pattern=r".*", optimizer_name="AdamW",
optimizer_kwargs={"lr": 1e-3})]`).
- `test_numerics.py`: `@unittest.skip` the flaky
`test_moe_dsv3_aot_fx_trace_vs_eager` to unblock CI, with a documented
reason for re-enabling after diagnosis.

## Test

- Default GraphTrainer 8-GPU workflow is **green** on this branch (all
previously-masked stages now run: `test_trace_module`,
`test_precompile`, precompile integration, `test_bitwise_deterministic`,
`test_sac_peak_memory`).
- Locally: `test_profiler.py` post-processor tests pass (E2E skips on
CUDA < 13.1); `test_titan_optimizers_container` passes;
`test_moe_dsv3_aot_fx_trace_vs_eager` skips. Verified the old `open()`
path raises `UnicodeDecodeError` on the gzip file and the new path reads
it.
- `pre-commit` clean on all changed files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants