Skip to content
KernelIndex
Search⌘K

gpt-o3 / cudae3d1f4

gpt-o3_cuda_e3d1f4 · gpt-o3 · cuda · Apache-2.0

Use it

Vendorable · source mirrored · Apache-2.0View source →

No package. Vendor the mirrored source: 87 lines, Apache-2.0, pinned at da91508.

main.cpp
curl "https://kernelindex.com/api/v1/implementations/flashinfer-gpt-o3-cuda-e3d1f4?include=source"
interfacecuda
revisionda915083d4c7
symbolrun
pathmain.cpp
Compatibility
declared hardwareNVIDIA B200
architecturessm_100
dtypesfp32, int32

Benchmark evidence

No published measurement for this revision.

No evidence · How evidence levels are derived →

Source and license

sourcehttps://huggingface.co/datasets/flashinfer-ai/flashinfer-trace
commitda915083d4c7c5e61aa3005e3d17ae488e0fc71c
revision digestsha256:d806689b8430bd7d2d37d159cc38e88352bc09878a45a97f7853366cfa3aa517
license declaredApache-2.0
license concludedApache-2.0
authorsgpt-o3
imported2026-08-20

Kernel source

main.cpp87 lines
#include "kernel.h"

#include <torch/extension.h>
#include <ATen/ATen.h>
#include <ATen/cuda/CUDAContext.h>

#include <vector>
#include <cstdint>

/* -------------------------------------------------------------------------- */
/*  C++ implementation that exactly mirrors the Python reference              */
/*  – correctness first, while still executing entirely on the GPU            */
/*    through existing highly-optimised PyTorch ops.                          */
/* -------------------------------------------------------------------------- */
torch::Tensor run(torch::Tensor probs,
                  torch::Tensor top_k)
{
    /* ---------------- sanity checks --------------------------------------- */
    TORCH_CHECK(probs.is_cuda(), "probs must reside on CUDA");
    TORCH_CHECK(top_k.is_cuda(), "top_k must reside on CUDA");
    TORCH_CHECK(probs.dtype()  == torch::kFloat32,
                "probs must be float32");
    TORCH_CHECK(top_k.dtype()  == torch::kInt32,
                "top_k must be int32");
    TORCH_CHECK(probs.dim() == 2 && probs.size(1) == VOCAB_SIZE,
                "probs must have shape [batch_size, 129280]");
    TORCH_CHECK(probs.size(0) == top_k.size(0),
                "probs and top_k must share batch size");

    const int64_t batch_size = probs.size(0);
    auto device   = probs.device();

    auto samples  = torch::empty({batch_size},
                                 torch::TensorOptions()
                                     .dtype(torch::kInt64)
                                     .device(device));

    /* ensure we work with float32 ------------------------------------------------- */
    auto probs_f = probs.to(torch::kFloat32);

    /* process every row independently – this keeps the logic identical to the
       reference implementation while letting all heavy ops execute on the GPU.    */
    for (int64_t row = 0; row < batch_size; ++row)
    {
        int k = top_k[row].item<int>();

        /* view of the current row (1-D CUDA tensor) ---------------------------- */
        auto row_probs = probs_f[row];

        if (0 < k && k < VOCAB_SIZE)
        {
            /* retain the top-k probabilities ---------------------------------- */
            auto tk        = torch::topk(row_probs, k, /*dim=*/0,
                                         /*largest=*/true,
                                         /*sorted=*/false);
            auto keep_idx  = std::get<1>(tk);

            auto filtered  = torch::zeros_like(row_probs);
            filtered.index_put_({keep_idx},
                                row_probs.index_select(0, keep_idx));

            /* renormalise so probabilities sum to one ------------------------ */
            row_probs = filtered / filtered.sum();
        }

        /* multinomial sampling – relies on PyTorch’s RNG, hence guarantees
           bit-for-bit reproducibility w.r.t. the Python reference.            */
        auto tok = torch::multinomial(row_probs,
                                      /*num_samples=*/1,
                                      /*replacement=*/true)
                       .squeeze(0)
                       .to(torch::kInt64);
        samples[row] = tok;
    }

    return samples;
}

/* -------------------------------------------------------------------------- */
/*  PyBind11 glue                                                             */
/* -------------------------------------------------------------------------- */
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
    m.def("run", &run,
          "top_k_sampling_from_probs_v129280",
          pybind11::arg("probs"),
          pybind11::arg("top_k"));
}
scrolls · 87 lines total

Source code from the importing source · Apache-2.0

No published measurement for this revision

JSON