gpt-5.5-priority · popcorn · l123_single_gpt
compile_fail 8_DeepSeekMoEGroundedTop2Routing.py
model
gpt-5.5-priority
turns / max
1 / 1
tool calls
1
tokens (total)
0
wall clock
—
truncated
no
backend
cuda fp32
variant
popcorn
runtime
—
ref runtime
-1.00 μs
speedup
—
started
2026-05-05T00:25:14Z
compile error: Error building extension 'grounded_top2_moe_router_ext': [1/3] /opt/conda/bin/x86_64-conda-linux-gnu-c++ -MMD -MF main.o.d -DTORCH_EXTENSION_NAME=grounded_top2_moe_router_ext -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -fPIC -std=c++17 -O3 -c /scratch/abaner/KernelBench/runs/l123_single_gpt/popcorn/gpt-5.5-priority/level_2_problem_8_cache/k_c7
timingkernelbench.timing
kernel runtime
—
ref runtime
—
speedup
—
kernel mean / median / std
—
ref mean / median / std
—
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn


class Model(nn.Module):
    """
    Grounded top-2 MoE router.

    Router logits are biased by token-expert similarity against learned
    grounding embeddings, then normalized over the selected top-2 experts.
    """

    def __init__(self):
        super().__init__()

    def forward(
        self,
        token_hidden: torch.Tensor,
        router_logits: torch.Tensor,
        expert_ground: torch.Tensor,
        alpha: float,
    ) -> torch.Tensor:
        grounded = router_logits + alpha * (token_hidden @ expert_ground.t())
        top_vals, top_idx = torch.topk(grounded, k=2, dim=-1)
        top_weights = torch.softmax(top_vals, dim=-1)
        return torch.stack((top_idx.to(torch.float32), top_weights), dim=-1)


num_tokens = 4096
hidden_dim = 128
num_experts = 16
alpha = 0.35


def get_inputs():
    p = popcorn_pri
    nt = p.jitter_int(num_tokens)
    hd = p.jitter_int(hidden_dim, align=16)
    ne = p.jitter_int(num_experts, minimum=2)
    token_hidden = torch.randn(nt, hd, dtype=torch.float32)
    router_logits = torch.randn(nt, ne, dtype=torch.float32)
    expert_ground = torch.randn(ne, hd, dtype=torch.float32)
    return [token_hidden, router_logits, expert_ground, alpha]



def get_init_inputs():
    return []
turn 0 · FINAL · 1 tool call(s) · 45.5s turn · 45.5s LLM · 9,131 chars (click to expand)
turn 045.5s turn · 45.5s LLM · 0.0s tools · FINAL
reasoning
**Evaluating warp configurations** I'm considering a setup with 16 lanes per token, possibly allowing 8 warps per block, and thinking each warp manages all experts simultaneously. If each lane computes one expert's operations sequentially, that translates to a significant amount of processing per block. But, I wonder about efficiency in loading shared data among lanes. A consistent memory access pattern could help, yet arranging experts might lead to coalescing issues. I see options here, but it's a bit complex! **Analyzing loop structures** I'm thinking about this loop with lane and expert configurations. For each loop iteration, having 16 contiguous lanes per expert seems efficient, especially with global addresses separated for each group. Having warps that contain two expert groups could help with data coalescing, but I need to consider the overhead for blocks per token. With 4096 blocks, that feels like a lot, but perhaps the occupancy of 256 threads could still make it manageable, right?
call → submit_kernel
callsubmit_kernel { "kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n\n_cpp_src = r\"\"\"\n#include <torch/extension.h>\n\ntorch::Tensor grounded_top2_forward(torch::Tensor token_hidden,\n torch::Tensor router_logits,\n torch::Tensor expert_ground,\n double alpha);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__global__ __launch_bounds__(256, 2)\nvoid grounded_top2_kernel(const float* __restrict__ token_hidden,\n const float* __restrict__ router_logits,\n const float* __restrict__ expert_ground,\n float* __restrict__ out,\n int T,\n float alpha) {\n const int tok = blockIdx.x;\n if (tok >= T) return;\n\n const int tid = threadIdx.x; // 0..255\n const int expert = tid >> 4; // 0..15\n const int lane = tid & 15; // 0..15 within expert group\n\n __shared__ float vals[16];\n\n const float* th = token_hidden + ((long long)tok) * 128;\n const float* eg = expert_ground + expert * 128;\n\n float sum = 0.0f;\n#pragma unroll\n for (int k = 0; k < 8; ++k) {\n int h = lane + (k << 4);\n sum = fmaf(th[h], eg[h], sum);\n }\n\n // Reduce the 16 lanes assigned to each expert. Masks are warp-local.\n const unsigned mask = (threadIdx.x & 16) ? 0xffff0000u : 0x0000ffffu;\n#pragma unroll\n for (int off = 8; off > 0; off >>= 1) {\n sum += __shfl_down_sync(mask, sum, off, 16);\n }\n\n if (lane == 0) {\n vals[expert] = router_logits[((long long)tok) * 16 + expert] + alpha * sum;\n }\n __syncthreads();\n\n if (tid == 0) {\n float best1 = -CUDART_INF_F;\n float best2 = -CUDART_INF_F;\n int idx1 = 0;\n int idx2 = 0;\n\n#pragma unroll\n for (int e = 0; e < 16; ++e) {\n float v = vals[e];\n if (v > best1) {\n best2 = best1;\n idx2 = idx1;\n best1 = v;\n idx1 = e;\n } else if (v > best2) {\n best2 = v;\n idx2 = e;\n }\n }\n\n float r = expf(best2 - best1);\n float w1 = 1.0f / (1.0f + r);\n float w2 = r * w1;\n\n long long base = ((long long)tok) * 4;\n out[base + 0] = (float)idx1;\n out[base + 1] = w1;\n out[base + 2] = (float)idx2;\n out[base + 3] = w2;\n }\n}\n\ntorch::Tensor grounded_top2_forward(torch::Tensor token_hidden,\n torch::Tensor router_logits,\n torch::Tensor expert_ground,\n double alpha) {\n const int T = (int)token_hidden.size(0);\n auto out = torch::empty({T, 2, 2}, token_hidden.options());\n\n const dim3 block(256);\n const dim3 grid(T);\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n grounded_top2_kernel<<<grid, block, 0, stream>>>(\n token_hidden.data_ptr<float>(),\n router_logits.data_ptr<float>(),\n expert_ground.data_ptr<float>(),\n out.data_ptr<float>(),\n T,\n (float)alpha);\n return out;\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"grounded_top2_moe_router_ext\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"grounded_top2_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\n with_cuda=True,\n verbose=False,\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n\n def forward(self, token_hidden: torch.Tensor, router_logits: torch.Tensor, expert_ground: torch.Tensor, alpha: float) -> torch.Tensor:\n return _ext.grounded_top2_forward(token_hidden, router_logits, expert_ground, float(alpha))\n" }
result ← submit_kernel (fail)
failsubmit_kernel submit_kernel FAILED: kernel did not compile. Error building extension 'grounded_top2_moe_router_ext': [1/3] /opt/conda/bin/x86_64-conda-linux-gnu-c++ -MMD -MF main.o.d -DTORCH_EXTENSION_NAME=grounded_top2_moe_router_ext -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -fPIC -std=c++17 -O3 -c /scratch/abaner/KernelBench/runs/l123_single_gpt/popcorn/gpt-5.5-priority/level_2_problem_8_cache/k_c77a5e727275/grounded_top2_moe_router_ext/main.cpp -o main.o [2/3] /usr/local/cuda/bin/nvcc -MD -MF cuda.cuda.o.d -ccbin /opt/conda/bin/x86_64-conda-linux-gnu-cc -DTORCH_EXTENSION_NAME=grounded_top2_moe_router_ext -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_90,code=compute_90 -gencode=arch=compute_90,code=sm_90 --compiler-options '-fPIC' -O3 --use_fast_math -std=c++17 -c /scratch/abaner/KernelBench/runs/l123_single_gpt/popcorn/gpt-5.5-priority/level_2_problem_8_cache/k_c77a5e727275/grounded_top2_moe_router_ext/cuda.cu -o cuda.cuda.o FAILED: [code=2] cuda.cuda.o /usr/local/cuda/bin/nvcc -MD -MF cuda.cuda.o.d -ccbin /opt/conda/bin/x86_64-conda-linux-gnu-cc -DTORCH_EXTENSION_NAME=grounded_top2_moe_router_ext -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_90,code=compute_90 -gencode=arch=compute_90,code=sm_90 --compiler-options '-fPIC' -O3 --use_fast_math -std=c++17 -c /scratch/abaner/KernelBench/runs/l123_single_gpt/popcorn/gpt-5.5-priority/level_2_problem_8_cache/k_c77a5e727275/grounded_top2_moe_router_ext/cuda.cu -o cuda.cuda.o nvcc warning : incompatible redefinition for option 'compiler-bindir', the last value of this option was used /scratch/abaner/KernelBench/runs/l123_single_gpt/popcorn/gpt-5.5-priority/level_2_problem_8_cache/k_c77a5e727275/grounded_top2_moe_router_ext/cuda.cu(50): error: identifier "CUDART_INF_F" is undefined float best1 = -CUDART_INF_F; ^ 1 error detected in the compilation of "/scratch/abaner/KernelBench/runs/l123_single_gpt/popcorn/gpt-5.5-priority/level_2_problem_8_cache/k_c77a5e727275/grounded_top2_moe_router_ext/cuda.cu". ninja: build stopped: subcommand failed.
final submitted kernel (131 lines, 3,905 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline


_cpp_src = r"""
#include <torch/extension.h>

torch::Tensor grounded_top2_forward(torch::Tensor token_hidden,
                                    torch::Tensor router_logits,
                                    torch::Tensor expert_ground,
                                    double alpha);
"""

_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cmath>

__global__ __launch_bounds__(256, 2)
void grounded_top2_kernel(const float* __restrict__ token_hidden,
                          const float* __restrict__ router_logits,
                          const float* __restrict__ expert_ground,
                          float* __restrict__ out,
                          int T,
                          float alpha) {
    const int tok = blockIdx.x;
    if (tok >= T) return;

    const int tid = threadIdx.x;       // 0..255
    const int expert = tid >> 4;       // 0..15
    const int lane = tid & 15;         // 0..15 within expert group

    __shared__ float vals[16];

    const float* th = token_hidden + ((long long)tok) * 128;
    const float* eg = expert_ground + expert * 128;

    float sum = 0.0f;
#pragma unroll
    for (int k = 0; k < 8; ++k) {
        int h = lane + (k << 4);
        sum = fmaf(th[h], eg[h], sum);
    }

    // Reduce the 16 lanes assigned to each expert.  Masks are warp-local.
    const unsigned mask = (threadIdx.x & 16) ? 0xffff0000u : 0x0000ffffu;
#pragma unroll
    for (int off = 8; off > 0; off >>= 1) {
        sum += __shfl_down_sync(mask, sum, off, 16);
    }

    if (lane == 0) {
        vals[expert] = router_logits[((long long)tok) * 16 + expert] + alpha * sum;
    }
    __syncthreads();

    if (tid == 0) {
        float best1 = -CUDART_INF_F;
        float best2 = -CUDART_INF_F;
        int idx1 = 0;
        int idx2 = 0;

#pragma unroll
        for (int e = 0; e < 16; ++e) {
            float v = vals[e];
            if (v > best1) {
                best2 = best1;
                idx2 = idx1;
                best1 = v;
                idx1 = e;
            } else if (v > best2) {
                best2 = v;
                idx2 = e;
            }
        }

        float r = expf(best2 - best1);
        float w1 = 1.0f / (1.0f + r);
        float w2 = r * w1;

        long long base = ((long long)tok) * 4;
        out[base + 0] = (float)idx1;
        out[base + 1] = w1;
        out[base + 2] = (float)idx2;
        out[base + 3] = w2;
    }
}

torch::Tensor grounded_top2_forward(torch::Tensor token_hidden,
                                    torch::Tensor router_logits,
                                    torch::Tensor expert_ground,
                                    double alpha) {
    const int T = (int)token_hidden.size(0);
    auto out = torch::empty({T, 2, 2}, token_hidden.options());

    const dim3 block(256);
    const dim3 grid(T);
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    grounded_top2_kernel<<<grid, block, 0, stream>>>(
        token_hidden.data_ptr<float>(),
        router_logits.data_ptr<float>(),
        expert_ground.data_ptr<float>(),
        out.data_ptr<float>(),
        T,
        (float)alpha);
    return out;
}
"""

_ext = load_inline(
    name="grounded_top2_moe_router_ext",
    cpp_sources=_cpp_src,
    cuda_sources=_cuda_src,
    functions=["grounded_top2_forward"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3", "--use_fast_math"],
    with_cuda=True,
    verbose=False,
)


class ModelNew(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, token_hidden: torch.Tensor, router_logits: torch.Tensor, expert_ground: torch.Tensor, alpha: float) -> torch.Tensor:
        return _ext.grounded_top2_forward(token_hidden, router_logits, expert_ground, float(alpha))