chore: import upstream snapshot with attribution
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
#pragma once
|
||||
#include <sgl_kernel/math.cuh>
|
||||
#include <sgl_kernel/type.cuh>
|
||||
#include <sgl_kernel/utils.cuh>
|
||||
#include <sgl_kernel/vec.cuh>
|
||||
#include <sgl_kernel/warp.cuh>
|
||||
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
namespace host::norm {
|
||||
|
||||
/**
|
||||
* \brief Check if the given configuration is supported.
|
||||
* \tparam T Element type (only fp16_t/bf16_t is supported)
|
||||
* \tparam kDim Dimension size (usually hidden size)
|
||||
*/
|
||||
template <typename T, int64_t kDim>
|
||||
inline constexpr bool is_config_supported() {
|
||||
if (!std::is_same_v<T, fp16_t> && !std::is_same_v<T, bf16_t>) return false;
|
||||
if (kDim <= 256) {
|
||||
return (kDim == 64 || kDim == 128 || kDim == 256);
|
||||
} else {
|
||||
return (kDim % 256 == 0 && kDim <= 8192);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Determine whether to use cta norm based on dimension size.
|
||||
* TL;DR: use warp norm for dim <= 256, cta norm otherwise.
|
||||
* \tparam T Element type (fp16_t or bf16_t)
|
||||
* \tparam kDim Dimension size (usually hidden size)
|
||||
* \note This function assumes that the configuration is supported.
|
||||
* \see `is_config_supported`
|
||||
*/
|
||||
template <typename T, int64_t kDim>
|
||||
inline constexpr bool should_use_cta() {
|
||||
static_assert(is_config_supported<T, kDim>(), "Unsupported norm configuration");
|
||||
return kDim > 256;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Get the number of threads per CTA for cta norm.
|
||||
* \tparam T Element type (fp16_t or bf16_t)
|
||||
* \tparam kDim Dimension size (usually hidden size)
|
||||
* \return Number of threads per CTA
|
||||
*/
|
||||
template <typename T, int64_t kDim>
|
||||
inline constexpr uint32_t get_cta_threads() {
|
||||
static_assert(should_use_cta<T, kDim>());
|
||||
return (kDim / 256) * device::kWarpThreads;
|
||||
}
|
||||
|
||||
} // namespace host::norm
|
||||
|
||||
namespace device::norm {
|
||||
|
||||
namespace details {
|
||||
|
||||
template <int64_t kDim, bool kUseCTA, typename PackedFloat, std::size_t N>
|
||||
SGL_DEVICE AlignedVector<PackedFloat, N> apply_norm_impl(
|
||||
const AlignedVector<PackedFloat, N> input,
|
||||
const AlignedVector<PackedFloat, N> weight,
|
||||
const float eps,
|
||||
[[maybe_unused]] float* smem_buffer,
|
||||
[[maybe_unused]] uint32_t num_warps) {
|
||||
float sum_of_squares = 0.0f;
|
||||
|
||||
#pragma unroll
|
||||
for (auto i = 0u; i < N; ++i) {
|
||||
const auto fp32_input = cast<fp32x2_t>(input[i]);
|
||||
sum_of_squares += fp32_input.x * fp32_input.x;
|
||||
sum_of_squares += fp32_input.y * fp32_input.y;
|
||||
}
|
||||
|
||||
sum_of_squares = warp::reduce_sum(sum_of_squares);
|
||||
float norm_factor;
|
||||
if constexpr (kUseCTA) {
|
||||
// need to synchronize across the cta
|
||||
const auto warp_id = threadIdx.x / kWarpThreads;
|
||||
smem_buffer[warp_id] = sum_of_squares;
|
||||
__syncthreads();
|
||||
// use the first warp to reduce
|
||||
if (warp_id == 0) {
|
||||
const auto tx = threadIdx.x;
|
||||
const auto local_sum = tx < num_warps ? smem_buffer[tx] : 0.0f;
|
||||
sum_of_squares = warp::reduce_sum(local_sum);
|
||||
smem_buffer[32] = math::rsqrt(sum_of_squares / kDim + eps);
|
||||
}
|
||||
__syncthreads();
|
||||
norm_factor = smem_buffer[32];
|
||||
} else {
|
||||
norm_factor = math::rsqrt(sum_of_squares / kDim + eps);
|
||||
}
|
||||
|
||||
AlignedVector<PackedFloat, N> output;
|
||||
|
||||
#pragma unroll
|
||||
for (auto i = 0u; i < N; ++i) {
|
||||
const auto fp32_input = cast<fp32x2_t>(input[i]);
|
||||
const auto fp32_weight = cast<fp32x2_t>(weight[i]);
|
||||
output[i] = cast<PackedFloat, fp32x2_t>({
|
||||
fp32_input.x * norm_factor * fp32_weight.x,
|
||||
fp32_input.y * norm_factor * fp32_weight.y,
|
||||
});
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
} // namespace details
|
||||
|
||||
/**
|
||||
* \brief Apply norm using warp-level implementation.
|
||||
* \tparam kDim Dimension size
|
||||
* \tparam T Element type (fp16_t or bf16_t)
|
||||
* \param input Input vector
|
||||
* \param weight Weight vector
|
||||
* \param eps Epsilon value for numerical stability
|
||||
* \return Normalized output vector
|
||||
*/
|
||||
template <int64_t kDim, typename T>
|
||||
SGL_DEVICE T apply_norm_warp(const T& input, const T& weight, float eps) {
|
||||
static_assert(kDim <= 256, "Warp norm only supports dim <= 256");
|
||||
return details::apply_norm_impl<kDim, false>(input, weight, eps, nullptr, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Apply norm using CTA-level implementation.
|
||||
* \tparam kDim Dimension size
|
||||
* \tparam T Element type (fp16_t or bf16_t)
|
||||
* \param input Input vector
|
||||
* \param weight Weight vector
|
||||
* \param eps Epsilon value for numerical stability
|
||||
* \param smem Shared memory buffer
|
||||
* \param num_warps Number of warps in the CTA
|
||||
* \return Normalized output vector
|
||||
*/
|
||||
template <int64_t kDim, typename T>
|
||||
SGL_DEVICE T apply_norm_cta(
|
||||
const T& input, const T& weight, float eps, float* smem, uint32_t num_warps = blockDim.x / kWarpThreads) {
|
||||
static_assert(kDim > 256, "CTA norm only supports dim > 256");
|
||||
return details::apply_norm_impl<kDim, true>(input, weight, eps, smem, num_warps);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Storage type for norm operation.
|
||||
* For warp norm, the storage size depends on kDim.
|
||||
* For cta norm, the storage size is fixed to 16B.
|
||||
* We will also pack the input 16-bit floats into 32-bit types
|
||||
* for faster CUDA core operations.
|
||||
*
|
||||
* \tparam T Element type (fp16_t or bf16_t)
|
||||
* \tparam kDim Dimension size
|
||||
*/
|
||||
template <typename T, int64_t kDim>
|
||||
using StorageType = std::conditional_t< // storage type
|
||||
(kDim > 256), // whether to use cta norm
|
||||
AlignedVector<packed_t<T>, 4>, // cta norm storage, fixed to 16B
|
||||
AlignedVector<packed_t<T>, kDim / (2 * kWarpThreads)> // warp norm storage
|
||||
>;
|
||||
|
||||
/**
|
||||
* \brief Minimum shared memory size (in bytes) required for cta norm.
|
||||
*/
|
||||
inline constexpr uint32_t kSmemBufferSize = 33;
|
||||
|
||||
} // namespace device::norm
|
||||
Reference in New Issue
Block a user