Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/build-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,21 @@ jobs:
then
export AS_PLATFORM="cuda"
export AS_CUDA_SM="'70;75;80;86;89;90a'"
export AS_BUILD_R2SK_SAMPLING=ON
bash scripts/release/cpp_build_cuda.sh
else
export AS_PLATFORM="x86"
bash build.sh
fi
fi

- name: Verify R2S-K CUDA test binaries (compile only)
if: matrix.enable_cuda == 1
shell: bash
run: |
test -x build/bin/cpp_r2sk_sampling_test
test -x build/bin/cpp_r2sk_decode_graph_test


build-wheels:
strategy:
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/r2sk-gpu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-License-Identifier: Apache-2.0
name: R2S-K GPU correctness

on:
workflow_dispatch:

permissions:
contents: read

jobs:
r2sk-gpu:
runs-on: [self-hosted, Linux, X64, gpu]
container:
image: docker.cnb.cool/thinksrc/dashinfer/dev-ubi8-cu124:latest
options: --gpus all
steps:
- uses: actions/checkout@v4
with:
lfs: true
- name: Build and run R2S-K on a CUDA device
shell: bash
run: |
git config --global --add safe.directory "$GITHUB_WORKSPACE"
source /root/.bashrc
if [ -f /miniconda/etc/profile.d/conda.sh ]; then
source /miniconda/etc/profile.d/conda.sh
fi
source activate ds_py
nvidia-smi
export AS_PLATFORM=cuda
export AS_CUDA_SM='80;86;89;90a'
export AS_BUILD_R2SK_SAMPLING=ON
export AS_BUILD_PACKAGE=OFF
bash scripts/release/cpp_build_cuda.sh
# Executing directly makes a no-device return of 77 fail this job.
build/bin/cpp_r2sk_sampling_test
build/bin/cpp_r2sk_sampling_test --graph-test
build/bin/cpp_r2sk_decode_graph_test
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ option(ENABLE_ARM_V84_V9 "enable v8.4-a or higher version of Arm instructions, s

option(ENABLE_FP16 "build with fp16 mode" ON)
option(ENABLE_BF16 "build with bf16 mode" ON)
option(ENABLE_R2SK_SAMPLING "build the experimental BF16 R2S-K sampling backend" OFF)
if(ENABLE_R2SK_SAMPLING AND NOT (ENABLE_CUDA AND ENABLE_BF16))
message(FATAL_ERROR "ENABLE_R2SK_SAMPLING requires ENABLE_CUDA and ENABLE_BF16")
endif()
option(ENABLE_FP8 "build with fp8 mode" ON)
option(ENABLE_SPARSE "build with sparse mode" ON)
option(ENABLE_CUSPARSELT "build with CUSPARSELT lib" OFF)
Expand Down Expand Up @@ -200,6 +204,9 @@ endif()
if (ENABLE_BF16)
list(APPEND ALLSPARK_DEFINITION "-DENABLE_BF16")
endif()
if (ENABLE_R2SK_SAMPLING)
list(APPEND ALLSPARK_DEFINITION "-DENABLE_R2SK_SAMPLING")
endif()

if (ALWAYS_READ_LOAD_MODEL)
list(APPEND ALLSPARK_DEFINITION "-DALWAYS_READ_LOAD_MODEL")
Expand Down
18 changes: 8 additions & 10 deletions HIE-DNN/dnn/cuda/prefix_scan/tiled_scan_d1.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,10 @@ hiednnStatus_t TiledPrefixSum(
}

TilePrefixT *tilePrefix;
DeviceWsGuard wsGuard(handle);
wsGuard.GetWorkspace(&tilePrefix, tilePrefixSize * sizeof(TilePrefixT));
if (tilePrefix == nullptr) {
return HIEDNN_STATUS_INTERNAL_ERROR;
}
DeviceWsGuard wsGuard(handle, true);
auto status = wsGuard.GetWorkspace(&tilePrefix,
tilePrefixSize * sizeof(TilePrefixT));
if (status != HIEDNN_STATUS_SUCCESS) return status;

TilePrefixInit<1, TILE_PREFIX_PADDING, TilePrefixT>(
handle.stream, tilePrefix, tilePrefixSize);
Expand Down Expand Up @@ -429,11 +428,10 @@ hiednnStatus_t TiledPrefixSum(
int64_t tilePrefixSize = tilePrefixSizeX * tilePrefixSizeY;

TilePrefixT *tilePrefix;
DeviceWsGuard wsGuard(handle);
wsGuard.GetWorkspace(&tilePrefix, tilePrefixSize * sizeof(TilePrefixT));
if (tilePrefix == nullptr) {
return HIEDNN_STATUS_INTERNAL_ERROR;
}
DeviceWsGuard wsGuard(handle, true);
auto status = wsGuard.GetWorkspace(&tilePrefix,
tilePrefixSize * sizeof(TilePrefixT));
if (status != HIEDNN_STATUS_SUCCESS) return status;

TilePrefixInit<2, TILE_PREFIX_PADDING, TilePrefixT>(
handle.stream, tilePrefix, tilePrefixSizeX, m);
Expand Down
37 changes: 30 additions & 7 deletions HIE-DNN/dnn/include/cuda/cuda_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,28 @@ struct HiednnCudaHandle {

// DeviceWsLock must be called after DeviceWsUnlock
template <typename T>
hiednnStatus_t DeviceWsLock(T **ws, size_t size) const {
hiednnStatus_t DeviceWsLock(T **ws, size_t size, bool capture) const {
if (size > deviceWsSize) {
*ws = nullptr;
return HIEDNN_STATUS_INTERNAL_ERROR;
}
*ws = static_cast<T *>(deviceWs);
CHECK_CUDA_RETURN(cudaStreamWaitEvent(stream, wsMutex, 0));
unsigned int flags = 0;
#if CUDART_VERSION >= 12000
if (capture) flags = cudaEventWaitExternal;
#endif
CHECK_CUDA_RETURN(cudaStreamWaitEvent(stream, wsMutex, flags));
return HIEDNN_STATUS_SUCCESS;
}

hiednnStatus_t DeviceWsUnlock() const {
hiednnStatus_t DeviceWsUnlock(bool capture) const {
#if CUDART_VERSION >= 12000
if (capture) {
CHECK_CUDA_RETURN(cudaEventRecordWithFlags(
wsMutex, stream, cudaEventRecordExternal));
return HIEDNN_STATUS_SUCCESS;
}
#endif
CHECK_CUDA_RETURN(cudaEventRecord(wsMutex, stream));
return HIEDNN_STATUS_SUCCESS;
}
Expand All @@ -48,21 +59,33 @@ namespace cuda {
*/
struct DeviceWsGuard {
const HiednnCudaHandle *handle;
bool captureAware;
bool capture = false;
bool lock = false;

explicit DeviceWsGuard(const HiednnCudaHandle &cudaHandle)
: handle(&cudaHandle) {}
explicit DeviceWsGuard(const HiednnCudaHandle &cudaHandle,
bool captureAware = false)
: handle(&cudaHandle), captureAware(captureAware) {}

template <typename T>
hiednnStatus_t GetWorkspace(T **ws, size_t size) {
auto stat = handle->DeviceWsLock(ws, size);
#if CUDART_VERSION >= 12000
if (captureAware) {
// This event serializes shared workspace between graph and eager
// calls. Preserve it as an event node during capture.
cudaStreamCaptureStatus status;
CHECK_CUDA_RETURN(cudaStreamIsCapturing(handle->stream, &status));
capture = status == cudaStreamCaptureStatusActive;
}
#endif
auto stat = handle->DeviceWsLock(ws, size, capture);
lock = (stat == HIEDNN_STATUS_SUCCESS);
return stat;
}

~DeviceWsGuard() {
if (lock) {
handle->DeviceWsUnlock();
handle->DeviceWsUnlock(capture);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ if [ "${with_platform,,}" == "cuda" ]; then
-DNCCL_VERSION=${NCCL_VERSION} \
-DCUDA_VERSION=${cuda_version} \
-DCMAKE_CUDA_ARCHITECTURES="${cuda_sm}" \
-DENABLE_R2SK_SAMPLING="${AS_BUILD_R2SK_SAMPLING:-OFF}" \
-DUSE_SYSTEM_NV_LIB=${system_nv_lib} \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DENABLE_NV_STATIC_LIB=${cuda_static} \
Expand Down
7 changes: 7 additions & 0 deletions csrc/core/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ if (ENABLE_CUDA)
file(GLOB_RECURSE CUDA_MOE_SRC cuda/moe/*.cu)

list(APPEND KERNEL_SRC ${CUDA_COMMON_KERNEL_SRC} ${CUDA_XFORMER_SRC} ${CUDA_MOE_SRC})
if (ENABLE_R2SK_SAMPLING)
list(APPEND KERNEL_SRC cuda/topk_r2sk/r2sk.cu cuda/topk_r2sk/sampling.cu
cuda/topk_r2sk/decode_graph.cu)
endif()
endif()

if (ENABLE_JSON_MODE)
Expand All @@ -137,6 +141,9 @@ message(STATUS "ALLSPARK_PUBLIC_DEFINITIONS:${ALLSPARK_PUBLIC_DEFINITIONS}")

add_library(allspark_kernel STATIC ${KERNEL_SRC})
set_target_properties(allspark_kernel PROPERTIES CXX_STANDARD ${CXX_STD})
if (ENABLE_R2SK_SAMPLING)
target_compile_features(allspark_kernel PRIVATE cuda_std_17)
endif()


target_compile_definitions(
Expand Down
21 changes: 21 additions & 0 deletions csrc/core/kernel/cuda/topk_r2sk/LICENSE.RadiK
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-present Alibaba Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading