/*
 * Copyright (C) 2024-2025 by FlashInfer team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "sampling.cuh"


#define FUNCTION_DEFINE                                      \
    void {{func_name}}(void* probs_ptr,                      \
                      void* output_ptr,                      \
                      void* indices_ptr,                     \
                      void* top_k_arr_ptr,                   \
                      void* top_p_arr_ptr,                   \
                      int batch_size,                        \
                      int top_k_val,                         \
                      float top_p_val,                       \
                      int philox_seed,                       \
                      int philox_offset,                     \
                      int vocab_size,                        \
                      void* stream)

extern "C" {
FUNCTION_DEFINE;
}
                        
FUNCTION_DEFINE
{
    const uint32_t smem_size = sizeof(aiter::sampling::SamplingTempStorage<aiter::sampling::BLOCK_THREADS, aiter::sampling::SCAN_ALGO, aiter::sampling::REDUCE_ALGO>);
    dim3 nblks(batch_size);
    dim3 nthrs(aiter::sampling::BLOCK_THREADS);
    auto kernel = aiter::sampling::TopKTopPSamplingFromProbKernel<aiter::sampling::BLOCK_THREADS, aiter::sampling::SCAN_ALGO, aiter::sampling::REDUCE_ALGO,
                                                {{vec_size}}, {{"true" if deterministic else "false"}}, float, int>;
    hipFuncSetAttribute(reinterpret_cast<const void*>(kernel), hipFuncAttributeMaxDynamicSharedMemorySize, smem_size);
    kernel<<<nblks, nthrs, smem_size, reinterpret_cast<hipStream_t>(stream)>>>(reinterpret_cast<float*>(probs_ptr), reinterpret_cast<int*>(top_k_arr_ptr), reinterpret_cast<float*>(top_p_arr_ptr), reinterpret_cast<int*>(output_ptr), reinterpret_cast<int*>(indices_ptr), top_k_val, top_p_val, vocab_size, static_cast<uint64_t>(philox_seed), static_cast<uint64_t>(philox_offset));
}