Skip to content

fix(ep): fail MallocSymm with the heap size instead of hipMemset(nullptr) - #900

Merged
jhchouuu merged 1 commit into
ROCm:mainfrom
yelkhamra:amd-automation/mallocsymm-heap-size-error
Nov 9, 2026
Merged

jhchouuu merged 1 commit into
ROCm:mainfrom
yelkhamra:amd-automation/mallocsymm-heap-size-error

Conversation

@yelkhamra

@yelkhamra yelkhamra commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #899

Motivation

Prepared and host-compiled against ROCm/mori main 3711c9235afa4c58def7e13a4fea6177b8fa9203 ("umbp: hugepage-backed ranged scratch arenas (#830)"). One commit, one file (src/ops/dispatch_combine/dispatch_combine.cpp), +27 lines. The patch also applies unchanged to inspected main ff2dd5b30be016480522873513a35924ac4bd7c7, where git diff --check and clang-format 20.1.8 --dry-run --Werror pass. It has since been rebased unchanged, with no conflicts, onto current main c7da62dbcf736a5fbc381af75e4fd888b374839e, where the repository pre-commit hooks (pre-commit run --from-ref origin/main --to-ref HEAD) all pass. Source line citations and the host-compile/harness results below refer to the prepared base; the newer mains were checked for application and style only.

Summary

EpDispatchCombineHandle::MallocSymm (the non-CCO branch) passes the result of ShmemExtMallocWithFlags to hipMemset without checking it. When the shmem symmetric heap is too small, the allocator returns nullptr and every rank exits with

[.../src/ops/dispatch_combine/dispatch_combine.cpp:212] hip failed with invalid argument

That message points at HIP, not at heap sizing. This PR checks the allocation and throws a std::runtime_error, as the CCO branch of the same function already does. The message names the requested size, the rank, the heap size, the heap's free space and largest free block, and MORI_SHMEM_HEAP_SIZE. Heap defaults and buffer sizes are not changed.

Open PR #521 contains a similar check as part of runtime resizing; see "Related" for how this PR differs.

Problem

We served DeepSeek-V4-Pro with ATOM on 8x MI355X (image rocm/atom-dev:nightly_202610111450, ATOM 2479251d, mori wheel amd-mori-nightly 1.2.4.dev20260910). We used DP attention + EP8 and did not set MORI_SHMEM_HEAP_SIZE, so the 4 GiB static default applied. Two configurations failed at MoRI handle creation: one with --enable-tbo and one with --all2all-backend low-latency.

In both runs, all 8 ranks logged

[application] [error] Out of heap memory! Requested: 1915224064 bytes (aligned), Current heap size: 4294967296 bytes. Hint: Increase via MORI_SHMEM_HEAP_SIZE (default: 4GB for Static, 16GB for VMM)

and then exited with [.../src/ops/dispatch_combine/dispatch_combine.cpp:212] hip failed with invalid argument. The serving front end only reported RuntimeError: Engine Core Mgr: Received unexpected SHUTDOWN signal from DP rank N during initialization.

The two lines are written through different paths: the HIP line with an unbuffered fprintf(stderr, ...) and the heap line through the logger. In multi-rank output they interleave. In one of the two runs, the first dispatch_combine.cpp:212 line comes before the first Out of heap memory line. If you read the tail of the log or search for the failing file and line, you find a HIP "invalid argument" error with no mention of the heap.

Technical Details

Root cause

Source reading at the base commit:

  • src/ops/dispatch_combine/dispatch_combine.cpp L211-212: in the !useCcoComm branch, void* buf = ShmemExtMallocWithFlags(size, flags); is followed by HIP_RUNTIME_CHECK(hipMemset(buf, 0, size));. There is no null check.
  • src/application/memory/va_manager.cpp L199-204: when no free block fits, HeapVAManager::Allocate logs Out of heap memory! ... and returns 0. src/shmem/memory.cpp AllocateStaticHeap (L35-49) then returns nullptr. AllocateVMMHeap (L65-78) also returns nullptr when VMMAllocChunk returns an invalid object.
  • include/mori/application/utils/check.hpp L57-65: HIP_RUNTIME_CHECK prints [file:line] hip failed with <error> and calls exit(-1).

So an exhausted heap leads to hipMemset(nullptr, 0, size), which fails with invalid argument, and the process exits at L212. The CCO branch of the same function already throws std::runtime_error("MallocSymm: cco arena exhausted (need ..., have ...)") (L244). Only the shmem branch is unchecked.

Isolation mode is different: SymmMemManager::ExtMallocWithFlags (src/application/memory/symmetric_memory.cpp L93-97) wraps the device allocation in HIP_RUNTIME_CHECK itself, so a real HIP allocation failure there still exits inside the allocator, before MallocSymm sees a result. This PR does not change that path.

Evidence

  1. The allocator line above, 8 ranks in each of the two failed runs, followed by dispatch_combine.cpp:212 hip failed with invalid argument (8 lines per run) and the front-end RuntimeError (one per run). These are from our server-initialisation logs of the two runs.
  2. In the low-latency run, the first dispatch_combine.cpp:212 line is the log line directly before the first Out of heap memory line.
  3. L210-215 of dispatch_combine.cpp in the mori wheel of that image are identical to main, so the failing L212 is the unchecked hipMemset above. The MoRI version was read from the image's installed packages.
  4. One check covers all EP kernel types. At the base commit every symmetric buffer in src/ops (IntraNode, IntraNodeLL, InterNode, InterNodeV1/V1LL, AsyncLL) is allocated through MallocSymm. ShmemExtMallocWithFlags is called only inside MallocSymm (dispatch_combine.cpp L211), and nothing in src/ops calls ShmemMalloc* directly.
  5. For context, one unverified explanation is that these configurations need more heap than the default because two-batch overlap creates a second dispatch/combine handle and the low-latency path allocates larger buffers. The 4 GiB static default dates from Feat(shmem): increase default static heap to 4GB and VMM heap to 16GB #204 (5b0ed757).

Fix

In src/ops/dispatch_combine/dispatch_combine.cpp:

  • In MallocSymm (non-CCO branch), if ShmemExtMallocWithFlags returns nullptr for a non-zero size, throw
    std::runtime_error("MallocSymm: symmetric heap allocation of <size> bytes failed on rank <rank> (<heap description>)").
  • A new file-local helper, DescribeShmemHeapForAllocFailure(), builds the heap description from existing shmem state:
    • heap size: MemoryStates::staticHeapSize (static heap) or MemoryStates::vmmHeapVirtualSize (VMM heap);
    • free bytes and largest free block from the existing HeapVAManager::GetStats;
    • The hint if the heap is too small, increase MORI_SHMEM_HEAP_SIZE. It is conditional because a VMM allocation can also fail for reasons other than capacity, such as the physical chunk allocation in SymmMemManager::VMMAllocChunk;
    • In isolation mode there is no shared heap, so the message says that the device allocation itself failed and gives no heap hint. As noted above, a real HIP failure in isolation mode exits inside the allocator first, so this branch is defensive: it is only reached if the isolation allocator returns no buffer without exiting.

Example from the CPU check below (real constructor, 4 GiB static heap, an illustrative IntraNode EP8 configuration that does not fit):

MallocSymm: symmetric heap allocation of 2818572288 bytes failed on rank 3 (current heap size 4294967296 bytes, free 1418985472 bytes, largest free block 1418985472 bytes; if the heap is too small, increase MORI_SHMEM_HEAP_SIZE)

size == 0 behaves as before: ShmemExtMallocWithFlags returns nullptr for 0 bytes by design (memory.cpp L190-192), and that is not a heap failure.

Why it works

  • The null pointer is caught before it reaches HIP, so hipMemset(nullptr) no longer runs and the misleading invalid argument exit no longer happens.
  • The exception is raised inside the EpDispatchCombineHandle constructor (InitializeShmemBuf / InitializeTokenNumSignalBuf / InitializeBarrier -> MallocSymm) and leaves the constructor.
  • Python: the binding declares the constructor with pybind11::init<EpDispatchCombineConfig, uintptr_t>() (src/pybind/pybind_ops.cpp L307-309). pybind11 translates a std::runtime_error into a Python RuntimeError that carries what() (translate_exception in pybind11's detail/internals.h). The CPU check below confirms this for the real constructor: Python receives RuntimeError: MallocSymm: symmetric heap allocation of .... We did not test how a particular serving framework displays that error for each rank. We expect it to appear in the traceback that the framework reports for the failing rank, instead of only in a separate allocator log line.
  • XLA: EpDispatchCombineInstantiate (src/pybind/pybind_xla_ffi_ops.cpp L321-361) catches std::exception and returns Error::Internal(e.what()), so the message is carried in the FFI error. This path was read, not run.
  • Free space and largest free block show whether the heap is too small or already mostly used by earlier allocations. In our runs the request (1915224064 bytes) was less than half the heap (4294967296 bytes), so the heap size alone does not explain the failure.
  • The change follows the error idiom already used in this function (CCO branch) and in the collective classes (throw std::runtime_error("...: out ShmemMalloc failed") in include/mori/collective/allgather/).

Risk / compatibility

  • Behaviour changes only on a path that already ended the process: exit(-1) from HIP_RUNTIME_CHECK becomes a C++ exception (a Python RuntimeError). Callers that let exceptions propagate still stop, with a readable message.
  • Failed construction is not exception-safe, so recovery or retry can keep resources. The constructor's cleanup is in the destructor (L190-206), which does not run when the constructor throws. SymmMemObjPtr is a pair of raw pointers with no owning destructor. Buffers that MallocSymm allocated before the failure therefore stay allocated in the heap. The CPU check shows this: one heap block is still allocated after the throw. Before this change the process exited, so these buffers did not matter. Now a caller that catches the error and keeps running keeps them. An example is the XLA path above: it returns the error, and its handle cache entry stays empty, so if instantiation is attempted again it constructs a new handle while the earlier buffers are still held. The CCO branch already throws from the same place with the same property. We left cleanup on a failed constructor out of this minimal fix. If you want it here, we can add it.
  • No change to heap defaults, allocation sizes, the CCO branch, kernels or the public API. The success path gains one pointer comparison. GetStats (which takes the VA manager's mutex) runs only on the failure path.

Related

Test Plan

GPU validation plan

Not required for merge in our view: the changed code is host-only, and it was executed above through the real constructor. As a smoke test on one 8-GPU MI300X/MI355X node with this branch built:

  1. Regression: pytest tests/python/ops/test_dispatch_combine_intranode.py (32G heap from conftest). Expect the same pass/fail set as main.
  2. Failure path: run tests/python/ops/bench_dispatch_combine.py with MORI_SHMEM_HEAP_SIZE below what the configuration needs. The bench uses os.environ.setdefault, so the override applies. Before: dispatch_combine.cpp:<line>] hip failed with invalid argument with exit code 255 (line 212 at the prepared base and in our runs; line 240 on current main). After: RuntimeError: MallocSymm: symmetric heap allocation of N bytes failed on rank R (current heap size ...; if the heap is too small, increase MORI_SHMEM_HEAP_SIZE) on each rank.

CPU: the host-compile, harness and style checks are listed under Test Result.

Test Result

CPU only; no GPU was used. Exact output lines:

  • Host compile of the base and patched file with ROCm 7.0.1 hipcc (HIP version 7.0.51831-a3e329ad8). The file is built as plain CXX (src/ops/CMakeLists.txt), with the CMake Release flags -std=c++17 -O3 (CMAKE_CXX_FLAGS_RELEASE "-O3", so asserts stay on), plus -Wall -Wextra:
    base_obj_rc=0, patched_obj_rc=0, base: 19 warnings generated. | patched: 19 warnings generated., SAME_WARNINGS. None of the warnings are in the new lines.

  • git apply --check of the patch on the base commit: apply_check_rc=0. git diff --check: clean.

  • Behaviour check: a harness that is not part of this PR. It is linked without any unresolved-symbol override, and every object in it is created by a constructor.

    • Real code: the compiled dispatch_combine.cpp (base or patched), src/shmem/memory.cpp (ShmemExtMallocWithFlags, ShmemQueryMemObjPtr, ShmemFree) and src/application/memory/va_manager.cpp. Each case runs new EpDispatchCombineHandle(config) (the real constructor) and, if that succeeds, delete.
    • Test doubles: symmetric_memory.cpp and context.cpp are not linked. Instead the harness defines the SymmMemManager and Context members that this path needs. VMM VA comes from the real HeapVAManager::Allocate(size, 256), as in SymmMemManager::AllocateVirtualAddress; physical chunk mapping is not modelled. The bootstrap is a small subclass. The HIP runtime functions are host stubs, and hipMemset returns hipErrorInvalidValue for (nullptr, size > 0). Functions that must not be reached abort with TRAP <name>; none fired.
    • The configuration is an illustrative IntraNode EP8 shape (hidden 7168, 8 experts per token). Its buffer sizes are not those of our failed runs.

    The base build is expected to reproduce the bug (exit 255 with the L212 line), and the patched build to throw (exit 3):

    ===== base (harness rc=0)
    [mori/src/ops/dispatch_combine/dispatch_combine.cpp:212] hip failed with invalid argument
    RESULT ctor_static_exhaustion exit=255 want=255 PASS
    RESULT ctor_vmm_exhaustion exit=255 want=255 PASS
    RESULT ctor_isolation_injected_invalid exit=255 want=255 PASS
    RESULT ctor_static_fits exit=0 want=0 PASS
    RESULT ctor_static_large_heap_fits exit=0 want=0 PASS
    RESULT ctor_isolation_fits exit=0 want=0 PASS
    RESULT ctor_isolation_hip_oom_unchanged exit=255 want=255 PASS
    RESULT size_zero_unchanged exit=134 want=134 PASS
    SUMMARY base failed_cases=0
    ===== patched (harness rc=0)
      what(): MallocSymm: symmetric heap allocation of 2818572288 bytes failed on rank 3 (current heap size 4294967296 bytes, free 1418985472 bytes, largest free block 1418985472 bytes; if the heap is too small, increase MORI_SHMEM_HEAP_SIZE)
      after the throw: 1 heap blocks still allocated, free 1418985472 bytes
    RESULT ctor_static_exhaustion exit=3 want=3 PASS
      what(): MallocSymm: symmetric heap allocation of 11274289152 bytes failed on rank 3 (current heap size 17179869184 bytes, free 5608833024 bytes, largest free block 5608833024 bytes; if the heap is too small, increase MORI_SHMEM_HEAP_SIZE)
      after the throw: 1 heap blocks still allocated, free 5608833024 bytes
    RESULT ctor_vmm_exhaustion exit=3 want=3 PASS
      what(): MallocSymm: symmetric heap allocation of 119832576 bytes failed on rank 3 (isolation mode: the device allocation itself failed)
    RESULT ctor_isolation_injected_invalid exit=3 want=3 PASS
    RESULT ctor_static_fits exit=0 want=0 PASS
    RESULT ctor_static_large_heap_fits exit=0 want=0 PASS
    RESULT ctor_isolation_fits exit=0 want=0 PASS
    RESULT ctor_isolation_hip_oom_unchanged exit=255 want=255 PASS
    RESULT size_zero_unchanged exit=134 want=134 PASS
    SUMMARY patched failed_cases=0
    

    In the base build, the static case prints the same pair of lines as our runs (with this configuration's sizes): Out of heap memory! Requested: ... Current heap size: 4294967296 bytes followed by dispatch_combine.cpp:212] hip failed with invalid argument.

    Scope of each case:

    • ctor_static_exhaustion and ctor_vmm_exhaustion are the target of this PR: heap capacity runs out.
    • ctor_isolation_injected_invalid is artificial. The double returns an invalid object, which the real isolation allocator does not do for a HIP failure. It only exercises the isolation wording of the message.
    • ctor_isolation_hip_oom_unchanged models a real HIP out-of-memory in isolation mode. It exits inside the allocator (exit 255) in both builds, so this PR does not change that case.
    • size_zero_unchanged calls MallocSymm(0) on a constructed handle. Exit 134 is assert(obj.IsValid()), as on base. This case assumes hipMemset(nullptr, 0, 0) returns success.
  • Python check: the same objects, built into a pybind11 module (pybind11 3.1.0 headers) that declares the handle exactly as pybind_ops.cpp L307-309, then EpDispatchCombineHandle(cfg) from Python on a 4 GiB static heap. Base: the interpreter exits with status 255 after the L212 line. Patched:

    PYRESULT builtins.RuntimeError: MallocSymm: symmetric heap allocation of 2818572288 bytes failed on rank 3 (current heap size 4294967296 bytes, free 1418985472 bytes, largest free block 1418985472 bytes; if the heap is too small, increase MORI_SHMEM_HEAP_SIZE)
    

Not run: the full CMake build and the GPU test suite. No test is added to the repository. The ops pytest session (tests/python/ops/conftest.py L48) pins MORI_SHMEM_HEAP_SIZE=32G for a shared worker pool, so a heap-exhaustion test would need its own process pool. We can add one if you want it.

Submission Checklist

Credit

@yelkhamra
yelkhamra marked this pull request as ready for review November 2, 2026 20:24
@jhchouuu jhchouuu self-assigned this Nov 3, 2026
…ptr)

EpDispatchCombineHandle::MallocSymm (non-CCO path) passed the result of
ShmemExtMallocWithFlags straight to hipMemset. When the shmem symmetric
heap is exhausted the allocator logs "Out of heap memory" and returns
nullptr, and every rank then exits with

  [.../dispatch_combine.cpp:212] hip failed with invalid argument

which points at HIP instead of heap sizing.

- Check the allocation and throw std::runtime_error (as the CCO branch of
  MallocSymm already does) naming the requested size, the rank, the heap
  size, its free space / largest free block and MORI_SHMEM_HEAP_SIZE.
- Every symmetric buffer in src/ops goes through MallocSymm, so this one
  check covers all EP kernel types. size == 0 keeps its old behaviour.
- No change to heap defaults or to the allocation sizes.

Found and prepared by the AMD automation team.

Signed-off-by: Yaakoub El Khamra <[email protected]>
@yelkhamra

Copy link
Copy Markdown
Contributor Author

Rebased onto current main to keep this mergeable; the patch is unchanged (same patch-id), new head 5e75e85114d6. Ready for review.

@yelkhamra
yelkhamra force-pushed the amd-automation/mallocsymm-heap-size-error branch from c50de92 to 5e75e85 Compare November 8, 2026 03:00
@yelkhamra

Copy link
Copy Markdown
Contributor Author

CI note: CCO unit test (MI355X_AINIC) failed on 5e75e851 in test_asyncll.py with ccoMemAlloc: hipMemCreate failed: 2 (out of memory) on one rank, after the earlier asyncll cases had completed. That is the CCO allocator path (src/cco/cco_init.cpp); this PR only adds an error message on the non-CCO MallocSymm path when ShmemExtMallocWithFlags has already returned null, so it can't change that allocation. The same job passes on main and passed on this PR's previous head. It looks like device memory pressure on the shared runner. The branch is already on top of current main; could a maintainer re-run the failed job?

@jhchouuu jhchouuu 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.

LGTM

@yelkhamra

Copy link
Copy Markdown
Contributor Author

LGTM

👍 . Will it be merged by someone from your side or is there something for me to do?

@jhchouuu

jhchouuu commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Will it be merged by someone from your side or is there something for me to do?

I will merge after CI green. And it seems that the apparent ci error has no correlation with your changes.

@jhchouuu
jhchouuu merged commit 333ae5f into ROCm:main Nov 9, 2026
16 of 17 checks passed
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.

[Issue]: EP handle creation exits with "hip failed with invalid argument" when the shmem symmetric heap is exhausted

2 participants