Skip to content

Commit 78b7a5f

Browse files
authored
fix(EPv2): use last-arrival-resets for combine wide-EP grid barrier (#709)
1 parent 2dc3251 commit 78b7a5f

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

‎src/ops/dispatch_combine_v2/ep_intranode_1250x.hpp‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ __device__ void EpDispatch1250xBody(EpArgs args) {
385385
index_t n = s_N[p];
386386
if (_blkMapNeeded) _cusplit_blkCount[(size_t)blockIdx.x * npes + p] = n;
387387
if (n > 0) {
388-
s_base[p] = __hip_atomic_fetch_add(EpTokOff(args, p), n,
389-
__ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM);
388+
s_base[p] =
389+
__hip_atomic_fetch_add(EpTokOff(args, p), n, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM);
390390
if (_blkMapNeeded) _cusplit_blkBase[(size_t)blockIdx.x * npes + p] = s_base[p];
391391
atomicAdd(&args.destPeTokenCounter[p], n);
392392
}
@@ -643,9 +643,9 @@ __device__ void EpDispatch1250xBody(EpArgs args) {
643643
if (spS.body) TdmIssueStore<int>(reinterpret_cast<int*>(dS + spS.head), tS, gS);
644644
if (!_pfSent && _pfN > 0 && _pfDst != nullptr) {
645645
const int _mTokB = (int)(hiddenDim * sizeof(T));
646-
TdmIssueStore<int>(
647-
reinterpret_cast<int*>(_pfDst + (size_t)_pfSlot0 * hiddenDim),
648-
reinterpret_cast<int*>(_tdmTile), TdmShape<int>(_pfN * (_mTokB / 4)));
646+
TdmIssueStore<int>(reinterpret_cast<int*>(_pfDst + (size_t)_pfSlot0 * hiddenDim),
647+
reinterpret_cast<int*>(_tdmTile),
648+
TdmShape<int>(_pfN * (_mTokB / 4)));
649649
_pfSent = true;
650650
}
651651
_mPend = true;
@@ -692,8 +692,7 @@ __device__ void EpDispatch1250xBody(EpArgs args) {
692692

693693
if (args.tokenIndices && args.inpTokenBuf) {
694694
constexpr int kTokB = kCfg.hiddenDim * (int)sizeof(T);
695-
constexpr bool kFp4Pack4 =
696-
kCfg.dtype == EpDType::Fp4x2 && kSlabBytes >= 4 * kTokB;
695+
constexpr bool kFp4Pack4 = kCfg.dtype == EpDType::Fp4x2 && kSlabBytes >= 4 * kTokB;
697696
constexpr int kPack = kFp4Pack4 ? 4 : 1;
698697
static_assert(!kFp4Pack4 || kTokB % 128 == 0,
699698
"each packed FP4 token must occupy whole TDM rows");
@@ -709,8 +708,8 @@ __device__ void EpDispatch1250xBody(EpArgs args) {
709708
const index_t part = cntAll / (index_t)nUnits;
710709
const index_t rem = cntAll - part * (index_t)nUnits;
711710
const index_t myCnt = part + (((index_t)unit < rem) ? (index_t)1 : (index_t)0);
712-
const index_t myBase = s_base[destPe] + (index_t)unit * part +
713-
(((index_t)unit < rem) ? (index_t)unit : rem);
711+
const index_t myBase =
712+
s_base[destPe] + (index_t)unit * part + (((index_t)unit < rem) ? (index_t)unit : rem);
714713
T* const dst = EpPeer<T>(win, destPe, args.offDispOut);
715714
const T* const src = reinterpret_cast<const T*>(args.inpTokenBuf);
716715
const index_t* const srcMap = _cusplit_stgSrc + (size_t)destPe * _stgCap;
@@ -723,14 +722,13 @@ __device__ void EpDispatch1250xBody(EpArgs args) {
723722
for (int k = 0; k < kPack; ++k) {
724723
if (k < n) {
725724
const int srcTok = (int)srcMap[slot0 + k] % kCfg.maxTokPerRank;
726-
TdmIssueLoad<T>(_tdmTile + (size_t)k * hiddenDim,
727-
src + (size_t)srcTok * hiddenDim, _tdmG1);
725+
TdmIssueLoad<T>(_tdmTile + (size_t)k * hiddenDim, src + (size_t)srcTok * hiddenDim,
726+
_tdmG1);
728727
}
729728
}
730729
__builtin_amdgcn_s_wait_tensorcnt(0);
731730
TdmIssueStore<int>(reinterpret_cast<int*>(dst + (size_t)slot0 * hiddenDim),
732-
reinterpret_cast<int*>(_tdmTile),
733-
TdmShape<int>(n * (kTokB / 4)));
731+
reinterpret_cast<int*>(_tdmTile), TdmShape<int>(n * (kTokB / 4)));
734732
__builtin_amdgcn_s_wait_tensorcnt(0);
735733
}
736734
}
@@ -842,18 +840,21 @@ __device__ __forceinline__ void EpCrossDeviceBarrier1250x(EpArgs args, bool need
842840
const unsigned long long phase = args.xdbFlag[blockIdx.x];
843841

844842
if (needGridRendezvous) {
845-
if (thdId == 0) atomicAdd(args.gridBarrier, 1u);
843+
unsigned _gridArvl = 0;
844+
if (thdId == 0) _gridArvl = atomicAdd(args.gridBarrier, 1u);
846845
if constexpr (!EpIsWideEp(kCfg)) {
847846
if (globalThdId < npes) {
848847
EpWaitEq(args.gridBarrier, static_cast<unsigned int>(gridDim.x));
849848
__hip_atomic_store(args.gridBarrier, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
850849
}
851850
} else {
852-
// Wide EP: single-thread wait+reset avoids the race where warp 0 resets
853-
// the barrier before warp 1 reads gridDim.x.
851+
// Wide EP: last-arrival resets to avoid the inter-block race where a late
852+
// block misses the transient gridDim.x value and deadlocks on 0.
854853
if (thdId == 0) {
855-
EpWaitEq(args.gridBarrier, static_cast<unsigned int>(gridDim.x));
856-
__hip_atomic_store(args.gridBarrier, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
854+
if (_gridArvl == static_cast<unsigned>(gridDim.x) - 1)
855+
__hip_atomic_store(args.gridBarrier, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
856+
else
857+
EpWaitEq(args.gridBarrier, 0u);
857858
}
858859
__syncthreads();
859860
}

‎src/ops/dispatch_combine_v2/ep_intranode_kernel.hpp‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ __device__ __forceinline__ void EpCrossDeviceBarrier(EpArgs args, unsigned long
297297
// back wrong at 80 blocks. Both are kept -- that failure was found with neither
298298
// present, so which one alone sufficed was never isolated.
299299
__threadfence_system();
300-
if (thdId == 0) atomicAdd(args.gridBarrier, 1u);
300+
unsigned _gridArvl = 0;
301+
if (thdId == 0) _gridArvl = atomicAdd(args.gridBarrier, 1u);
301302

302303
if constexpr (!EpIsWideEp(kCfg)) {
303304
// Narrow path: all participating threads are in one warp, no multi-warp race.
@@ -310,11 +311,13 @@ __device__ __forceinline__ void EpCrossDeviceBarrier(EpArgs args, unsigned long
310311
flag, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM);
311312
}
312313
} else {
313-
// Wide path: peers span multiple warps — single-thread wait+reset avoids the
314-
// race where warp 0 resets the barrier before warp 1 reads gridDim.x.
314+
// Wide path: last-arrival resets to avoid the inter-block race where a late
315+
// block misses the transient gridDim.x value and deadlocks on 0.
315316
if (thdId == 0) {
316-
EpWaitEq(args.gridBarrier, static_cast<unsigned int>(gridDim.x));
317-
__hip_atomic_store(args.gridBarrier, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
317+
if (_gridArvl == static_cast<unsigned>(gridDim.x) - 1)
318+
__hip_atomic_store(args.gridBarrier, 0u, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT);
319+
else
320+
EpWaitEq(args.gridBarrier, 0u);
318321
}
319322
__syncthreads();
320323

0 commit comments

Comments
 (0)