Skip to content

Commit 30c4201

Browse files
junhaha666valarLipCopilot
authored
fix moe_fused_gate meet nan data bug (ROCm#1086)
* fix moe_fused_gate meet nan data bug * update asm_topksoftmax for large M * Update csrc/kernels/moe_fused_gate.cu Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Lingpeng Jin <103567126+valarLip@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c2b8031 commit 30c4201

6 files changed

Lines changed: 60 additions & 21 deletions

File tree

csrc/kernels/moe_fused_gate.cu

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ using float32_t = float;
5151
template <typename T>
5252
__device__ inline bool cmp_gt(const T& a, const T& b)
5353
{
54-
if constexpr(std::is_same<T, float16_t>::value || std::is_same<T, bfloat16_t>::value)
54+
if constexpr(std::is_same<T, bfloat16_t>::value)
5555
{
5656
// at::Half (or float16_t in our native case) causes ambiguity, so we cast to float.
5757
return ck_tile::type_convert<float>(a) > ck_tile::type_convert<float>(b);
@@ -186,7 +186,7 @@ __device__ void moe_fused_gate_impl(void* input,
186186
#pragma unroll
187187
for(int ii = 0; ii < params.VPT; ++ii)
188188
{
189-
row_chunk[ii] = 1.0f / (1.0f + expf(-row_chunk[ii]));
189+
row_chunk[ii] = ::isnan(row_chunk[ii]) ? 0.0f : (1.0f / (1.0f + expf(-row_chunk[ii])));
190190
}
191191
// __syncthreads();
192192

@@ -548,6 +548,8 @@ std::vector<at::Tensor> moe_fused_gate(at::Tensor& input,
548548
auto output = topk_weights;
549549
auto indices = topk_ids;
550550
const int out_stride = topk_ids.stride(0);
551+
TORCH_CHECK(topk_weights.stride(0) == out_stride,
552+
"topk_weights and topk_ids must have the same stride in dim 0");
551553

552554
// Compute grid dimensions based on runtime value for num_expert_group.
553555
int64_t rows_per_warp = std::max<int64_t>(1, WARP_SIZE / num_expert_group);

csrc/py_itfs_cu/asm_topksoftmax.cu

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void topk_softmax_asm(torch::Tensor& topk_weights, // [num_tokens, topk]
3636
const uint num_tokens = gating_output.numel() / num_experts;
3737
const uint topk = topk_weights.size(-1);
3838
const uint out_stride = topk_weights.stride(0);
39-
const uint SUBM = 4;
39+
const uint SUBM = num_tokens < 10000 ? 4 : 12;
4040

4141
KernelArgs args;
4242
size_t arg_size = sizeof(args);
@@ -53,36 +53,73 @@ void topk_softmax_asm(torch::Tensor& topk_weights, // [num_tokens, topk]
5353
AiterAsmKernel* impl_ptr = nullptr;
5454
if(num_experts == 128 && topk == 8)
5555
{
56-
static AiterAsmKernel impl_topksoftmax_4x128x8("_ZN5aiter19topksoftmax_4x128x8E",
57-
"/topksoftmax/topksoftmax_4x128x8.co");
58-
impl_ptr = &impl_topksoftmax_4x128x8;
56+
if(SUBM == 4)
57+
{
58+
static AiterAsmKernel impl_topksoftmax_4x128x8("_ZN5aiter19topksoftmax_4x128x8E",
59+
"/topksoftmax/topksoftmax_4x128x8.co");
60+
impl_ptr = &impl_topksoftmax_4x128x8;
61+
}
62+
else
63+
{
64+
static AiterAsmKernel impl_topksoftmax_12x128x8("_ZN5aiter20topksoftmax_12x128x8E",
65+
"/topksoftmax/topksoftmax_12x128x8.co");
66+
impl_ptr = &impl_topksoftmax_12x128x8;
67+
}
5968
}
6069
else if(num_experts == 256 && topk == 8)
6170
{
62-
static AiterAsmKernel impl_topksoftmax_4x256x8("_ZN5aiter19topksoftmax_4x256x8E",
63-
"/topksoftmax/topksoftmax_4x256x8.co");
64-
impl_ptr = &impl_topksoftmax_4x256x8;
71+
if(SUBM == 4)
72+
{
73+
static AiterAsmKernel impl_topksoftmax_4x256x8("_ZN5aiter19topksoftmax_4x256x8E",
74+
"/topksoftmax/topksoftmax_4x256x8.co");
75+
impl_ptr = &impl_topksoftmax_4x256x8;
76+
}
77+
else
78+
{
79+
static AiterAsmKernel impl_topksoftmax_12x256x8("_ZN5aiter20topksoftmax_12x256x8E",
80+
"/topksoftmax/topksoftmax_12x256x8.co");
81+
impl_ptr = &impl_topksoftmax_12x256x8;
82+
}
6583
}
6684
else if(num_experts == 128 && topk == 6)
6785
{
68-
static AiterAsmKernel impl_topksoftmax_4x128x6("_ZN5aiter19topksoftmax_4x128x6E",
69-
"/topksoftmax/topksoftmax_4x128x6.co");
70-
impl_ptr = &impl_topksoftmax_4x128x6;
86+
if(SUBM == 4)
87+
{
88+
static AiterAsmKernel impl_topksoftmax_4x128x6("_ZN5aiter19topksoftmax_4x128x6E",
89+
"/topksoftmax/topksoftmax_4x128x6.co");
90+
impl_ptr = &impl_topksoftmax_4x128x6;
91+
}
92+
else
93+
{
94+
static AiterAsmKernel impl_topksoftmax_12x128x6("_ZN5aiter20topksoftmax_12x128x6E",
95+
"/topksoftmax/topksoftmax_12x128x6.co");
96+
impl_ptr = &impl_topksoftmax_12x128x6;
97+
}
7198
}
7299
else if(num_experts == 256 && topk == 6)
73100
{
74-
static AiterAsmKernel impl_topksoftmax_4x256x6("_ZN5aiter19topksoftmax_4x256x6E",
75-
"/topksoftmax/topksoftmax_4x256x6.co");
76-
impl_ptr = &impl_topksoftmax_4x256x6;
101+
if(SUBM == 4)
102+
{
103+
static AiterAsmKernel impl_topksoftmax_4x256x6("_ZN5aiter19topksoftmax_4x256x6E",
104+
"/topksoftmax/topksoftmax_4x256x6.co");
105+
impl_ptr = &impl_topksoftmax_4x256x6;
106+
}
107+
else
108+
{
109+
static AiterAsmKernel impl_topksoftmax_12x256x6("_ZN5aiter20topksoftmax_12x256x6E",
110+
"/topksoftmax/topksoftmax_12x256x6.co");
111+
impl_ptr = &impl_topksoftmax_12x256x6;
112+
}
77113
}
78114
else
79115
{
80-
TORCH_CHECK(false,
81-
__func__,
82-
" only support num_experts/topk in [128/6, 128/8, 256/6, 256/8], but get num_experts: ",
83-
num_experts,
84-
" , topk: ",
85-
topk);
116+
TORCH_CHECK(
117+
false,
118+
__func__,
119+
" only support num_experts/topk in [128/6, 128/8, 256/6, 256/8], but get num_experts: ",
120+
num_experts,
121+
" , topk: ",
122+
topk);
86123
}
87124

88125
const at::cuda::OptionalCUDAGuard device_guard(device_of(gating_output));
12.8 KB
Binary file not shown.
15.3 KB
Binary file not shown.
15.5 KB
Binary file not shown.
18.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)