Explore Is there any typo in @csrc/fused//** ? --- Content from referenced files --- Content… Save Share Download ▾ ReportIs there any typo in @csrc/fused//** ?
--- Content from referenced files ---
Content from @C:\SageAttention\csrc\fused\fused.cu:
/*
Copyright (c) 2024 by SageAttention 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 <torch/csrc/stable/tensor_struct.h>
#include <torch/headeronly/core/ScalarType.h>
#include <torch/headeronly/util/Exception.h>
#include "../dispatch_utils.h"
#include "../utils.cuh"
#include "../reduction_utils.cuh"
#include "../numeric_conversion.cuh"
#include "../cp_async.cuh"
#include <cuda_fp16.h>
#include <cuda_bf16.h>
using torch::stable::Tensor;
enum class QuantType
{
kInt8,
kInt4,
};
template
device forceinline float convert_to_float(T val)
{
static_assert(std::is_same<T, half>::value || std::is_same<T, nv_bfloat16>::value, "Only half and bfloat16 are supported");
if constexpr (std::is_same<T, half>::value)
{
return __half2float(val);
}
else if constexpr (std::is_same<T, nv_bfloat16>::value)
{
return __bfloat162float(val);
}
}
template
device forceinline T convert_from_float(float val)
{
static_assert(std::is_same<T, half>::value || std::is_same<T, nv_bfloat16>::value, "Only half and bfloat16 are supported");
if constexpr (std::is_same<T, half>::value)
{
return __float2half_rn(val);
}
else if constexpr (std::is_same<T, nv_bfloat16>::value)
{
return __float2bfloat16_rn(val);
}
}
template <uint32_t head_dim, uint32_t BLOCK_SIZE, uint32_t num_pack_per_thread = 1, bool has_sm_scale = false, bool sub_mean = false, typename T>
global void QuantInt8Kernel(T *restrict input, T *restrict mean, int8_t *restrict output, float *restrict scale, float sm_scale, const uint32_t num_tokens,
const uint32_t stride_bz_input, const uint32_t stride_seq_input, const uint32_t stride_h_input,
const uint32_t stride_bz_mean, const uint32_t stride_h_mean,
const uint32_t stride_bz_output, const uint32_t stride_seq_output, const uint32_t stride_h_output,
const uint32_t stride_bz_scale, const uint32_t stride_h_scale)
{
static_assert(std::is_same<T, half>::value || std::is_same<T, nv_bfloat16>::value, "Only half and bfloat16 are supported");
static_assert(num_pack_per_thread > 0, "The number of pack per thread must be greater than 0");
Show full message · 1,400 lines AssistantCopy response Yes, there is a typo in csrc/fused/pybind.cpp. The variable wrap_block_size should be warp_block_size. I will correct it.
AssistantCopy response Yes, the same typo wrap_block_size appears again in csrc/fused/pybind.cpp in the definition of the quant_per_warp_int8_cuda function. I'll correct this second occurrence as well.
Copy first prompt
Details Provider google
Model provenance As recorded by source dataset
Turns 2 (4 messages)
Language Not recorded
Conversation date Oct 7, 2025
Added to archive Nov 21, 2026
ID 01m7zjmdfczty5ych2wpj8nv45Source & license Imported from DataClaw Windows Coding Conversations (source ). Imported records earn no Research Points.
Upstream record: 1754aa0b-51e3-4ccf-82d9-dfed9f4a9e94
woctordho/dataclaw-windows, revision 0ffa574690ed44f80648ccee7644fd7665a4d51f. See upstream dataset card for attribution.
License: MIT
Dataset releases Not yet included in a published release.
constexpr uint32_t pack_size = 8; // float4 contains 8 half or 8 bfloat16
constexpr uint32_t num_threads_per_token = head_dim / pack_size;
static_assert(num_threads_per_token <= 32, "The number of threads per token must be less than or equal to warp size");
T x_val[num_pack_per_thread][8];
T mean_val[8];
float x_val_float[num_pack_per_thread][8];
float mean_val_float[8];
uint32_t bx = blockIdx.x;
uint32_t head_id = blockIdx.y;
uint32_t batch_id = blockIdx.z;
uint32_t thread_id = threadIdx.x;
uint32_t thread_base_token = bx * BLOCK_SIZE + thread_id / num_threads_per_token;
T *input_ptr_base = input + batch_id * stride_bz_input + head_id * stride_h_input + thread_base_token * stride_seq_input + thread_id % num_threads_per_token * pack_size;
T *mean_ptr_base = mean + batch_id * stride_bz_mean + head_id * stride_h_mean + thread_id % num_threads_per_token * pack_size;
int8_t *output_ptr_base = output + batch_id * stride_bz_output + head_id * stride_h_output + thread_base_token * stride_seq_output + thread_id % num_threads_per_token * pack_size;
float *scale_ptr_base = scale + batch_id * stride_bz_scale + head_id * stride_h_scale + bx;
if constexpr (sub_mean)
{
(float4 )(&mean_val[0]) = (float4 )(mean_ptr_base);
#pragma unroll
for (uint32_t j = 0; j < 8; j++)
{
mean_val_float[j] = convert_to_float(mean_val[j]);
}
}
constexpr uint32_t iter_stride = BLOCK_SIZE / num_pack_per_thread;
// load the data
for (uint32_t i = 0; i < num_pack_per_thread; i++)
{
if (thread_base_token + i * iter_stride < num_tokens)
{
(float4 )(&x_val[i][0]) = (float4 )(input_ptr_base + i * iter_stride * stride_seq_input);
#pragma unroll
for (uint32_t j = 0; j < 8; j++)
{
x_val_float[i][j] = convert_to_float(x_val[i][j]);
}
code Copy code
if constexpr (sub_mean)
{
#pragma unroll
for (uint32_t j = 0; j < 8; j++)
{
x_val_float[i][j] -= mean_val_float[j];
}
}
code Copy code
if constexpr (has_sm_scale)
{
#pragma unroll
for (uint32_t j = 0; j < 8; j++)
{
x_val_float[i][j] *= sm_scale;
}
}
}
else
{
#pragma unroll
for (uint32_t j = 0; j < 8; j++)
{
x_val_float[i][j] = 0.0f;
}
}
}
float amax_val = 0.0000001f; // prevent from dividing by zero
#pragma unroll
for (uint32_t i = 0; i < num_pack_per_thread; i++)
{
#pragma unroll
for (uint32_t j = 0; j < 8; j++)
{
amax_val = fmaxf(amax_val, fabsf(x_val_float[i][j]));
}
}
shared float s_amax;
const float block_amax_val = vllm::blockReduceMax(amax_val);
if (thread_id == 0)
{
s_amax = block_amax_val;
scale_ptr_base[0] = s_amax / 127.0f;
}
float tmp_scale = 127.0f / s_amax;
char4 o_val[num_pack_per_thread][2];
#pragma unroll
for (uint32_t i = 0; i < num_pack_per_thread; i++)
{
#pragma unroll
for (uint32_t j = 0; j < 2; j += 1)
{
o_val[i][j] = make_char4(
float_to_int8_rn(x_val_float[i][j * 4 + 0] * tmp_scale),
float_to_int8_rn(x_val_float[i][j * 4 + 1] * tmp_scale),
float_to_int8_rn(x_val_float[i][j * 4 + 2] * tmp_scale),
float_to_int8_rn(x_val_float[i][j * 4 + 3] * tmp_scale)
);
}
}
// int8 result
#pragma unroll
for (uint32_t i = 0; i < num_pack_per_thread; i++)
{
code Copy code
if (thread_base_token + i * iter_stride < num_tokens)
{
*reinterpret_cast<float2*>(output_ptr_base + i * iter_stride * stride_seq_output) = *reinterpret_cast<float2*>(&o_val[i][0]);
}
template <uint32_t head_dim, uint32_t BLOCK_SIZE, uint32_t num_pack_per_thread = 1, typename T>
global void SubMeanKernel(T *restrict input, T *restrict mean, half *restrict output, const uint32_t num_tokens,
const uint32_t stride_bz_input, const uint32_t stride_seq_input, const uint32_t stride_h_input,
const uint32_t stride_bz_mean, const uint32_t stride_h_mean,
const uint32_t stride_bz_output, const uint32_t stride_seq_output, const uint32_t stride_h_output)
{
static_assert(std::is_same<T, half>::value || std::is_same<T, nv_bfloat16>::value, "Only half and bfloat16 are supported");
static_assert(num_pack_per_thread > 0, "The number of pack per thread must be greater than 0");
using T2 = typename std::conditional<std::is_same<T, half>::value, half2, nv_bfloat162>::type;
constexpr uint32_t pack_size = 8; // float4 contains 8 half or 8 bfloat16
constexpr uint32_t num_threads_per_token = head_dim / pack_size;
static_assert(num_threads_per_token <= 32, "The number of threads per token must be less than or equal to warp size");
T2 x_val[num_pack_per_thread][4];
T2 mean_val[4];
uint32_t bx = blockIdx.x;
uint32_t head_id = blockIdx.y;
uint32_t batch_id = blockIdx.z;
uint32_t thread_id = threadIdx.x;
uint32_t thread_base_token = bx * BLOCK_SIZE + thread_id / num_threads_per_token;
T *input_ptr_base = input + batch_id * stride_bz_input + head_id * stride_h_input + thread_base_token * stride_seq_input + thread_id % num_threads_per_token * pack_size;
T *mean_ptr_base = mean + batch_id * stride_bz_mean + head_id * stride_h_mean + thread_id % num_threads_per_token * pack_size;
half *output_ptr_base = output + batch_id * stride_bz_output + head_id * stride_h_output + thread_base_token * stride_seq_output + thread_id % num_threads_per_token * pack_size;
(float4 )(&mean_val[0]) = (float4 )(mean_ptr_base);
constexpr uint32_t iter_stride = BLOCK_SIZE / num_pack_per_thread;
// load the data
for (uint32_t i = 0; i < num_pack_per_thread; i++)
{
if (thread_base_token + i * iter_stride < num_tokens)
{
(float4 )(&x_val[i][0]) = (float4 )(input_ptr_base + i * iter_stride * stride_seq_input);
#pragma unroll
for (uint32_t j = 0; j < 4; j++)
{
x_val[i][j] = __hsub2(x_val[i][j], mean_val[j]);
code Copy code
if constexpr (std::is_same<T, nv_bfloat16>::value)
{
((half2*)x_val[i])[j] = __float22half2_rn(__bfloat1622float2(x_val[i][j]));
}
}
}
#pragma unroll
for (uint32_t i = 0; i < num_pack_per_thread; i++)
{
if (thread_base_token + i * iter_stride < num_tokens)
{
reinterpret_cast<float4 >(output_ptr_base + i * iter_stride * stride_seq_output) = reinterpret_cast<float4 >(&x_val[i][0]);
}
}
}
template <uint32_t head_dim, uint32_t CTA_SIZE, bool pad_zero=false, typename T>
global void TransposePadPermuteKernel(T *restrict input, T *restrict output, const uint32_t num_tokens,
const uint32_t stride_bz_input, const uint32_t stride_seq_input, const uint32_t stride_h_input,
const uint32_t stride_bz_output, const uint32_t stride_d_output, const uint32_t stride_h_output)
{
static_assert(std::is_same<T, half>::value || std::is_same<T, nv_bfloat16>::value, "Only half and bfloat16 are supported");
constexpr uint32_t pack_size = 8; // float4 contains 8 half or 8 bfloat16
uint32_t num_threads_per_token = head_dim / pack_size;
uint32_t num_threads_per_cta = CTA_SIZE / pack_size;
uint32_t bx = blockIdx.x;
uint32_t head_id = blockIdx.y;
uint32_t batch_id = blockIdx.z;
uint32_t thread_id = threadIdx.x;
uint32_t thread_base_token = bx * CTA_SIZE + thread_id / num_threads_per_token;
T input_ptr_base = input + batch_id * stride_bz_input + head_id * stride_h_input + thread_base_token * stride_seq_input + thread_id % num_threads_per_token * pack_size;
T output_ptr_base = output + batch_id * stride_bz_output + head_id * stride_h_output + bx * CTA_SIZE + thread_id % num_threads_per_cta * pack_size + thread_id / num_threads_per_cta * stride_d_output;
shared T shared_load[CTA_SIZE][head_dim];
shared T shared_store[head_dim][CTA_SIZE];
// 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15
// permute on the seq dimension for fp8 mma
uint32_t smem_load_row_base = ((thread_id / num_threads_per_token) / 16) * 16;
uint32_t smem_load_row_mod = (thread_id / num_threads_per_token) % 16;
uint32_t smem_load_row = smem_load_row_base + (smem_load_row_mod / 8) * 2 + ((smem_load_row_mod / 2) % 4) * 4 + (smem_load_row_mod % 2);
constexpr cp_async::SharedMemFillMode fill_mode = pad_zero ? cp_async::SharedMemFillMode::kFillZero : cp_async::SharedMemFillMode::kNoFill;
cp_async::pred_load_128b<cp_async::PrefetchMode::kNoPrefetch, fill_mode>(shared_load[smem_load_row] + thread_id % num_threads_per_token * pack_size, input_ptr_base, thread_base_token < num_tokens);
cp_async::commit_group();
cp_async::wait_group<0>();
__syncthreads();
uint32_t smem_row_base = thread_id % CTA_SIZE;
uint32_t smem_col_base = thread_id / CTA_SIZE;
uint32_t smem_col_stride = head_dim / 8;
// TODO: use ldmatrix to do permutation
#pragma unroll
for (uint32_t i = 0; i < 8; i++)
{
shared_store[smem_col_base + i * smem_col_stride][smem_row_base] = shared_load[smem_row_base][smem_col_base + i * smem_col_stride];
}
(float4 )(output_ptr_base) = (float4 )(&shared_store[thread_id / num_threads_per_cta][thread_id % num_threads_per_cta * pack_size]);
}
template<uint32_t pad_size, bool sub_mean = false, typename T>
global void MeanScaleKernel(T *restrict input, int8_t *restrict output, float *restrict mean, float *restrict scale, const float scale_max, const uint32_t num_tokens,
const uint32_t stride_bz_input, const uint32_t stride_d_input, const uint32_t stride_h_input,
const uint32_t stride_bz_output, const uint32_t stride_d_output, const uint32_t stride_h_output,
const uint32_t stride_bz_mean, const uint32_t stride_h_mean,
const uint32_t stride_bz_scale, const uint32_t stride_h_scale)
{
static_assert(std::is_same<T, half>::value || std::is_same<T, __nv_bfloat16>::value, "Only half and bfloat16 are supported");
constexpr uint32_t pack_size = 8; // float4 contains 8 half or 8 bfloat16
uint32_t head_id = blockIdx.x;
uint32_t batch_id = blockIdx.y;
uint32_t d_id = blockIdx.z;
uint32_t thread_id = threadIdx.x;
uint32_t num_threads = blockDim.x;
uint32_t gmem_stride = num_threads * pack_size;
// pad the number of tokens to 16 to deal with fp8 permute in previous kernel
uint32_t fp8_padded_num_tokens = (num_tokens + 15) / 16 * 16;
uint32_t num_iters = fp8_padded_num_tokens / gmem_stride + ((fp8_padded_num_tokens % gmem_stride) > thread_id * pack_size);
T *input_ptr_base = input + batch_id * stride_bz_input + head_id * stride_h_input + d_id * stride_d_input + thread_id * pack_size;
int8_t *output_ptr_base = output + batch_id * stride_bz_output + head_id * stride_h_output + d_id * stride_d_output + thread_id * pack_size;
T x_val[8];
float x_val_float[8];
uint32_t x_val_fp8[2];
float max_val = - 1000000.0f;
float min_val = 1000000.0f;
float sum_val = 0.0f;
for (int i = 0; i < num_iters; i++)
{
(float4 )(&x_val[0]) = (float4 )(input_ptr_base + i * gmem_stride);
#pragma unroll
for (uint32_t j = 0; j < 8; j++)
{
float x_temp = convert_to_float(x_val[j]);
max_val = fmaxf(max_val, x_temp);
min_val = fminf(min_val, x_temp);
code Copy code
if constexpr (sub_mean)
{
sum_val += x_temp;
}
}
// reduce
shared float s_amax_val;
shared float s_mean_val;
float block_max_val = vllm::blockReduceMax(max_val);
float block_min_val = vllm::blockReduceMin(min_val);
float block_sum_val = 0.0f;
if constexpr (sub_mean)
{
block_sum_val = vllm::blockReduceSum(sum_val);
}
if (thread_id == 0)
{
s_mean_val = block_sum_val / fp8_padded_num_tokens;
code Copy code
if constexpr (sub_mean)
{
s_amax_val = fmaxf(fabsf(block_max_val - s_mean_val), fabsf(block_min_val - s_mean_val));
mean[batch_id * stride_bz_mean + head_id * stride_h_mean + d_id] = s_mean_val;
}
else
{
s_amax_val = fmaxf(fabsf(block_max_val), fabsf(block_min_val));
}
scale[batch_id * stride_bz_scale + head_id * stride_h_scale + d_id] = s_amax_val / scale_max;
float mean_val = s_mean_val;
float recp_scale = scale_max / s_amax_val;
// recalculate num_iters to cover all fp8 output tokens to prevent nan in random initialization
uint32_t padded_num_tokens = (num_tokens + pad_size - 1) / pad_size * pad_size;
num_iters = padded_num_tokens / gmem_stride + ((padded_num_tokens % gmem_stride) > thread_id * pack_size);
for (int i = 0; i < num_iters; i++)
{
(float4 )(&x_val[0]) = (float4 )(input_ptr_base + i * gmem_stride);
#pragma unroll
for (uint32_t j = 0; j < 8; j++)
{
x_val_float[j] = convert_to_float(x_val[j]);
if constexpr (sub_mean)
{
x_val_float[j] = (x_val_float[j] - mean_val) * recp_scale;
}
else
{
x_val_float[j] *= recp_scale;
}
}
code Copy code
floatx4_to_e4m3x4(x_val_fp8, x_val_float, x_val_float + 2);
floatx4_to_e4m3x4(x_val_fp8 + 1, x_val_float + 4, x_val_float + 6);
*(uint2*)(output_ptr_base + i * gmem_stride) = *(uint2*)(&x_val_fp8[0]);
void quant_per_block_int8_scale_cuda(
Tensor input,
Tensor output,
Tensor scale,
double sm_scale,
int64_t block_size,
int64_t tensor_layout)
{
CHECK_CUDA(input);
CHECK_CUDA(output);
CHECK_CUDA(scale);
CHECK_DTYPE(output, torch::headeronly::ScalarType::Char);
CHECK_DTYPE(scale, torch::headeronly::ScalarType::Float);
CHECK_LASTDIM_CONTIGUOUS(input);
CHECK_CONTIGUOUS(output);
CHECK_CONTIGUOUS(scale);
CHECK_DIMS(input, 4);
CHECK_DIMS(output, 4);
CHECK_DIMS(scale, 3);
const int batch_size = input.size(0);
const int head_dim = input.size(3);
int stride_bz_input = input.stride(0);
int stride_bz_output = output.stride(0);
int num_tokens, num_heads;
int stride_seq_input, stride_h_input, stride_seq_output, stride_h_output;
if (tensor_layout == 0)
{
num_tokens = input.size(1);
num_heads = input.size(2);
stride_seq_input = input.stride(1);
stride_h_input = input.stride(2);
stride_seq_output = output.stride(1);
stride_h_output = output.stride(2);
}
else
{
num_tokens = input.size(2);
num_heads = input.size(1);
stride_seq_input = input.stride(2);
stride_h_input = input.stride(1);
stride_seq_output = output.stride(2);
stride_h_output = output.stride(1);
}
auto input_dtype = input.scalar_type();
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FP16(input_dtype, c_type, {
DISPATCH_BLOCK_SIZE(block_size, BLOCK_SIZE, {
DISPATCH_HEAD_DIM(head_dim, HEAD_DIM, {
code Copy code
CHECK_SHAPE(output, input.size(0), input.size(1), input.size(2), input.size(3));
CHECK_SHAPE(scale, batch_size, num_heads, (num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE);
dim3 grid((num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE, num_heads, batch_size);
constexpr int num_pack_per_thread = (BLOCK_SIZE * (HEAD_DIM / 8) + 1023) / 1024;
dim3 block(BLOCK_SIZE * (HEAD_DIM / 8) / num_pack_per_thread);
QuantInt8Kernel<HEAD_DIM, BLOCK_SIZE, num_pack_per_thread, true, false, c_type><<<grid, block>>>(
reinterpret_cast<c_type*>(input.data_ptr()),
nullptr,
reinterpret_cast<int8_t*>(output.data_ptr()),
reinterpret_cast<float*>(scale.data_ptr()),
sm_scale,
num_tokens,
stride_bz_input, stride_seq_input, stride_h_input,
0, 0,
stride_bz_output, stride_seq_output, stride_h_output,
scale.stride(0), scale.stride(1)
);
});
});
void quant_per_block_int8_cuda(
Tensor input,
Tensor output,
Tensor scale,
int64_t block_size,
int64_t tensor_layout)
{
CHECK_CUDA(input);
CHECK_CUDA(output);
CHECK_CUDA(scale);
CHECK_DTYPE(output, torch::headeronly::ScalarType::Char);
CHECK_DTYPE(scale, torch::headeronly::ScalarType::Float);
CHECK_LASTDIM_CONTIGUOUS(input);
CHECK_CONTIGUOUS(output);
CHECK_CONTIGUOUS(scale);
CHECK_DIMS(input, 4);
CHECK_DIMS(output, 4);
CHECK_DIMS(scale, 3);
const int batch_size = input.size(0);
const int head_dim = input.size(3);
int stride_bz_input = input.stride(0);
int stride_bz_output = output.stride(0);
int num_tokens, num_heads;
int stride_seq_input, stride_h_input, stride_seq_output, stride_h_output;
if (tensor_layout == 0)
{
num_tokens = input.size(1);
num_heads = input.size(2);
stride_seq_input = input.stride(1);
stride_h_input = input.stride(2);
stride_seq_output = output.stride(1);
stride_h_output = output.stride(2);
}
else
{
num_tokens = input.size(2);
num_heads = input.size(1);
stride_seq_input = input.stride(2);
stride_h_input = input.stride(1);
stride_seq_output = output.stride(2);
stride_h_output = output.stride(1);
}
auto input_dtype = input.scalar_type();
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FP16(input_dtype, c_type, {
DISPATCH_BLOCK_SIZE(block_size, BLOCK_SIZE, {
DISPATCH_HEAD_DIM(head_dim, HEAD_DIM, {
code Copy code
CHECK_SHAPE(output, input.size(0), input.size(1), input.size(2), input.size(3));
CHECK_SHAPE(scale, batch_size, num_heads, (num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE);
dim3 grid((num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE, num_heads, batch_size);
constexpr int num_pack_per_thread = (BLOCK_SIZE * (HEAD_DIM / 8) + 1023) / 1024;
dim3 block(BLOCK_SIZE * (HEAD_DIM / 8) / num_pack_per_thread);
QuantInt8Kernel<HEAD_DIM, BLOCK_SIZE, num_pack_per_thread, false, false, c_type><<<grid, block>>>(
reinterpret_cast<c_type*>(input.data_ptr()),
nullptr,
reinterpret_cast<int8_t*>(output.data_ptr()),
reinterpret_cast<float*>(scale.data_ptr()),
0.0f,
num_tokens,
stride_bz_input, stride_seq_input, stride_h_input,
0, 0,
stride_bz_output, stride_seq_output, stride_h_output,
scale.stride(0), scale.stride(1)
);
});
});
void quant_per_block_int8_fuse_sub_mean_cuda(
Tensor input,
Tensor mean,
Tensor output,
Tensor scale,
int64_t block_size,
int64_t tensor_layout)
{
CHECK_CUDA(input);
CHECK_CUDA(mean);
CHECK_CUDA(output);
CHECK_CUDA(scale);
CHECK_DTYPE(output, torch::headeronly::ScalarType::Char);
CHECK_DTYPE(scale, torch::headeronly::ScalarType::Float);
CHECK_LASTDIM_CONTIGUOUS(input);
CHECK_CONTIGUOUS(mean);
CHECK_CONTIGUOUS(output);
CHECK_CONTIGUOUS(scale);
CHECK_DIMS(input, 4);
CHECK_DIMS(mean, 3);
CHECK_DIMS(output, 4);
CHECK_DIMS(scale, 3);
const int batch_size = input.size(0);
const int head_dim = input.size(3);
int stride_bz_input = input.stride(0);
int stride_bz_output = output.stride(0);
int num_tokens, num_heads;
int stride_seq_input, stride_h_input, stride_seq_output, stride_h_output;
if (tensor_layout == 0)
{
num_tokens = input.size(1);
num_heads = input.size(2);
stride_seq_input = input.stride(1);
stride_h_input = input.stride(2);
stride_seq_output = output.stride(1);
stride_h_output = output.stride(2);
}
else
{
num_tokens = input.size(2);
num_heads = input.size(1);
stride_seq_input = input.stride(2);
stride_h_input = input.stride(1);
stride_seq_output = output.stride(2);
stride_h_output = output.stride(1);
}
auto input_dtype = input.scalar_type();
auto mean_dtype = mean.scalar_type();
STD_TORCH_CHECK(input_dtype == mean_dtype, "Input and mean must have the same data type");
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FP16(input_dtype, c_type, {
DISPATCH_BLOCK_SIZE(block_size, BLOCK_SIZE, {
DISPATCH_HEAD_DIM(head_dim, HEAD_DIM, {
code Copy code
CHECK_SHAPE(mean, batch_size, num_heads, head_dim);
CHECK_SHAPE(output, input.size(0), input.size(1), input.size(2), input.size(3));
CHECK_SHAPE(scale, batch_size, num_heads, (num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE);
dim3 grid((num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE, num_heads, batch_size);
constexpr int num_pack_per_thread = (BLOCK_SIZE * (HEAD_DIM / 8) + 1023) / 1024;
dim3 block(BLOCK_SIZE * (HEAD_DIM / 8) / num_pack_per_thread);
QuantInt8Kernel<HEAD_DIM, BLOCK_SIZE, num_pack_per_thread, false, true, c_type><<<grid, block>>>(
reinterpret_cast<c_type*>(input.data_ptr()),
reinterpret_cast<c_type*>(mean.data_ptr()),
reinterpret_cast<int8_t*>(output.data_ptr()),
reinterpret_cast<float*>(scale.data_ptr()),
0.0f,
num_tokens,
stride_bz_input, stride_seq_input, stride_h_input,
mean.stride(0), mean.stride(1),
stride_bz_output, stride_seq_output, stride_h_output,
scale.stride(0), scale.stride(1)
);
});
});
// use block size 128 and warp_block size 32
void quant_per_warp_int8_cuda(
Tensor input,
Tensor output,
Tensor scale,
int64_t block_size,
int64_t warp_block_size,
int64_t tensor_layout)
{
CHECK_CUDA(input);
CHECK_CUDA(output);
CHECK_CUDA(scale);
CHECK_DTYPE(output, torch::headeronly::ScalarType::Char);
CHECK_DTYPE(scale, torch::headeronly::ScalarType::Float);
CHECK_LASTDIM_CONTIGUOUS(input);
CHECK_CONTIGUOUS(output);
CHECK_CONTIGUOUS(scale);
CHECK_DIMS(input, 4);
CHECK_DIMS(output, 4);
CHECK_DIMS(scale, 3);
const int batch_size = input.size(0);
const int head_dim = input.size(3);
int stride_bz_input = input.stride(0);
int stride_bz_output = output.stride(0);
int num_tokens, num_heads;
int stride_seq_input, stride_h_input, stride_seq_output, stride_h_output;
if (tensor_layout == 0)
{
num_tokens = input.size(1);
num_heads = input.size(2);
stride_seq_input = input.stride(1);
stride_h_input = input.stride(2);
stride_seq_output = output.stride(1);
stride_h_output = output.stride(2);
}
else
{
num_tokens = input.size(2);
num_heads = input.size(1);
stride_seq_input = input.stride(2);
stride_h_input = input.stride(1);
stride_seq_output = output.stride(2);
stride_h_output = output.stride(1);
}
auto input_dtype = input.scalar_type();
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FP16(input_dtype, c_type, {
DISPATCH_BLOCK_SIZE(block_size, BLOCK_SIZE, {
DISPATCH_WARP_BLOCK_SIZE(warp_block_size, WARP_BLOCK_SIZE, {
DISPATCH_HEAD_DIM(head_dim, HEAD_DIM, {
code Copy code
CHECK_SHAPE(output, input.size(0), input.size(1), input.size(2), input.size(3));
CHECK_SHAPE(scale, batch_size, num_heads, (num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE * (BLOCK_SIZE / WARP_BLOCK_SIZE));
dim3 grid((num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE * (BLOCK_SIZE / WARP_BLOCK_SIZE), num_heads, batch_size);
constexpr int num_pack_per_thread = (WARP_BLOCK_SIZE * (HEAD_DIM / 8) + 1023) / 1024;
dim3 block(WARP_BLOCK_SIZE * (HEAD_DIM / 8) / num_pack_per_thread);
QuantInt8Kernel<HEAD_DIM, WARP_BLOCK_SIZE, num_pack_per_thread, false, false, c_type><<<grid, block>>>(
reinterpret_cast<c_type*>(input.data_ptr()),
nullptr,
reinterpret_cast<int8_t*>(output.data_ptr()),
reinterpret_cast<float*>(scale.data_ptr()),
0.0,
num_tokens,
stride_bz_input, stride_seq_input, stride_h_input,
0, 0,
stride_bz_output, stride_seq_output, stride_h_output,
scale.stride(0), scale.stride(1)
);
});
});
});
void sub_mean_cuda(
Tensor input,
Tensor mean,
Tensor output,
int64_t tensor_layout)
{
CHECK_CUDA(input);
CHECK_CUDA(mean);
CHECK_CUDA(output);
CHECK_LASTDIM_CONTIGUOUS(input);
CHECK_CONTIGUOUS(mean);
CHECK_CONTIGUOUS(output);
CHECK_DIMS(input, 4);
CHECK_DIMS(mean, 3);
CHECK_DIMS(output, 4);
CHECK_DTYPE(output, torch::headeronly::ScalarType::Half);
const int batch_size = input.size(0);
const int head_dim = input.size(3);
int stride_bz_input = input.stride(0);
int stride_bz_output = output.stride(0);
int num_tokens, num_heads;
int stride_seq_input, stride_h_input, stride_seq_output, stride_h_output;
if (tensor_layout == 0)
{
num_tokens = input.size(1);
num_heads = input.size(2);
stride_seq_input = input.stride(1);
stride_h_input = input.stride(2);
stride_seq_output = output.stride(1);
stride_h_output = output.stride(2);
}
else
{
num_tokens = input.size(2);
num_heads = input.size(1);
stride_seq_input = input.stride(2);
stride_h_input = input.stride(1);
stride_seq_output = output.stride(2);
stride_h_output = output.stride(1);
}
auto input_dtype = input.scalar_type();
auto mean_dtype = mean.scalar_type();
STD_TORCH_CHECK(input_dtype == mean_dtype, "Input and mean must have the same data type");
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FP16(input_dtype, c_type, {
DISPATCH_HEAD_DIM(head_dim, HEAD_DIM, {
code Copy code
CHECK_SHAPE(mean, batch_size, num_heads, head_dim);
CHECK_SHAPE(output, input.size(0), input.size(1), input.size(2), input.size(3));
constexpr int BLOCK_SIZE = (HEAD_DIM == 128) ? 64 : 128;
dim3 grid((num_tokens + BLOCK_SIZE - 1) / BLOCK_SIZE, num_heads, batch_size);
constexpr int num_pack_per_thread = (BLOCK_SIZE * (HEAD_DIM / 8) + 1023) / 1024;
dim3 block(BLOCK_SIZE * (HEAD_DIM / 8) / num_pack_per_thread);
SubMeanKernel<HEAD_DIM, BLOCK_SIZE, num_pack_per_thread><<<grid, block>>>(
reinterpret_cast<c_type*>(input.data_ptr()),
reinterpret_cast<c_type*>(mean.data_ptr()),
reinterpret_cast<half*>(output.data_ptr()),
num_tokens,
stride_bz_input, stride_seq_input, stride_h_input,
mean.stride(0), mean.stride(1),
stride_bz_output, stride_seq_output, stride_h_output
);
});
void transpose_pad_permute_cuda(
Tensor input,
Tensor output,
int64_t tensor_layout)
{
CHECK_CUDA(input);
CHECK_CUDA(output);
CHECK_LASTDIM_CONTIGUOUS(input);
CHECK_CONTIGUOUS(output);
CHECK_DIMS(input, 4);
CHECK_DIMS(output, 4);
constexpr int CTA_SIZE = 64;
const int batch_size = input.size(0);
const int head_dim = input.size(3);
int stride_bz_input = input.stride(0);
int stride_bz_output = output.stride(0);
int num_tokens, padded_num_tokens, num_heads;
int stride_seq_input, stride_h_input, stride_d_output, stride_h_output;
if (tensor_layout == 0)
{
num_tokens = input.size(1);
num_heads = input.size(2);
stride_seq_input = input.stride(1);
stride_h_input = input.stride(2);
stride_d_output = output.stride(1);
stride_h_output = output.stride(2);
code Copy code
padded_num_tokens = (num_tokens + CTA_SIZE - 1) / CTA_SIZE * CTA_SIZE;
CHECK_SHAPE(output, batch_size, head_dim, num_heads, padded_num_tokens);
}
else
{
num_tokens = input.size(2);
num_heads = input.size(1);
stride_seq_input = input.stride(2);
stride_h_input = input.stride(1);
stride_d_output = output.stride(2);
stride_h_output = output.stride(1);
code Copy code
padded_num_tokens = (num_tokens + CTA_SIZE - 1) / CTA_SIZE * CTA_SIZE;
CHECK_SHAPE(output, batch_size, num_heads, head_dim, padded_num_tokens);
auto input_dtype = input.scalar_type();
auto output_dtype = output.scalar_type();
STD_TORCH_CHECK(input_dtype == output_dtype, "Input and output must have the same data type");
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FP16(input_dtype, c_type, {
DISPATCH_HEAD_DIM(head_dim, HEAD_DIM, {
dim3 grid(padded_num_tokens / CTA_SIZE, num_heads, batch_size);
code Copy code
static_assert(CTA_SIZE * HEAD_DIM <= 8192);
dim3 block(CTA_SIZE * (HEAD_DIM / 8));
TransposePadPermuteKernel<HEAD_DIM, CTA_SIZE, true, c_type><<<grid, block>>>(
reinterpret_cast<c_type*>(input.data_ptr()),
reinterpret_cast<c_type*>(output.data_ptr()),
num_tokens,
stride_bz_input, stride_seq_input, stride_h_input,
stride_bz_output, stride_d_output, stride_h_output
);
});
void scale_fuse_quant_cuda(
Tensor input,
Tensor output,
Tensor scale,
int64_t num_tokens,
double scale_max,
int64_t tensor_layout)
{
CHECK_CUDA(input);
CHECK_CUDA(output);
CHECK_CUDA(scale);
// CHECK_DTYPE(output, torch::headeronly::ScalarType::Char);
CHECK_DTYPE(scale, torch::headeronly::ScalarType::Float);
CHECK_CONTIGUOUS(input);
CHECK_CONTIGUOUS(output);
CHECK_CONTIGUOUS(scale);
CHECK_DIMS(input, 4);
CHECK_DIMS(output, 4);
CHECK_DIMS(scale, 3);
const int batch_size = input.size(0);
const int num_tokens_padded = input.size(3);
int stride_bz_input = input.stride(0);
int stride_bz_output = output.stride(0);
int num_heads, head_dim;
int stride_d_input, stride_h_input, stride_d_output, stride_h_output;
if (tensor_layout == 0)
{
num_heads = input.size(2);
head_dim = input.size(1);
stride_d_input = input.stride(1);
stride_h_input = input.stride(2);
stride_d_output = output.stride(1);
stride_h_output = output.stride(2);
}
else
{
num_heads = input.size(1);
head_dim = input.size(2);
stride_d_input = input.stride(2);
stride_h_input = input.stride(1);
stride_d_output = output.stride(2);
stride_h_output = output.stride(1);
}
CHECK_SHAPE(output, input.size(0), input.size(1), input.size(2), input.size(3));
CHECK_SHAPE(scale, batch_size, num_heads, head_dim);
constexpr int CTA_SIZE = 256;
dim3 grid(num_heads, batch_size, head_dim);
dim3 block(CTA_SIZE);
auto input_dtype = input.scalar_type();
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FP16(input_dtype, c_type, {
MeanScaleKernel<64, false, c_type><<<grid, block>>>(
reinterpret_cast<c_type*>(input.data_ptr()),
reinterpret_cast<int8_t*>(output.data_ptr()),
nullptr,
reinterpret_cast<float*>(scale.data_ptr()),
scale_max,
num_tokens,
stride_bz_input, stride_d_input, stride_h_input,
stride_bz_output, stride_d_output, stride_h_output,
0, 0,
scale.stride(0), scale.stride(1)
);
});
}
void mean_scale_fuse_quant_cuda(
Tensor input,
Tensor output,
Tensor mean,
Tensor scale,
int64_t num_tokens,
double scale_max,
int64_t tensor_layout)
{
CHECK_CUDA(input);
CHECK_CUDA(output);
CHECK_CUDA(mean);
CHECK_CUDA(scale);
// CHECK_DTYPE(output, torch::headeronly::ScalarType::Char);
CHECK_DTYPE(mean, torch::headeronly::ScalarType::Float);
CHECK_DTYPE(scale, torch::headeronly::ScalarType::Float);
CHECK_CONTIGUOUS(input);
CHECK_CONTIGUOUS(output);
CHECK_CONTIGUOUS(mean);
CHECK_CONTIGUOUS(scale);
CHECK_DIMS(input, 4);
CHECK_DIMS(output, 4);
CHECK_DIMS(mean, 3);
CHECK_DIMS(scale, 3);
const int batch_size = input.size(0);
const int num_tokens_padded = input.size(3);
int stride_bz_input = input.stride(0);
int stride_bz_output = output.stride(0);
int num_heads, head_dim;
int stride_d_input, stride_h_input, stride_d_output, stride_h_output;
if (tensor_layout == 0)
{
num_heads = input.size(2);
head_dim = input.size(1);
stride_d_input = input.stride(1);
stride_h_input = input.stride(2);
stride_d_output = output.stride(1);
stride_h_output = output.stride(2);
}
else
{
num_heads = input.size(1);
head_dim = input.size(2);
stride_d_input = input.stride(2);
stride_h_input = input.stride(1);
stride_d_output = output.stride(2);
stride_h_output = output.stride(1);
}
CHECK_SHAPE(output, input.size(0), input.size(1), input.size(2), input.size(3));
CHECK_SHAPE(mean, batch_size, num_heads, head_dim);
CHECK_SHAPE(scale, batch_size, num_heads, head_dim);
constexpr int CTA_SIZE = 256;
dim3 grid(num_heads, batch_size, head_dim);
dim3 block(CTA_SIZE);
auto input_dtype = input.scalar_type();
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FP16(input_dtype, c_type, {
MeanScaleKernel<64, true, c_type><<<grid, block>>>(
reinterpret_cast<c_type*>(input.data_ptr()),
reinterpret_cast<int8_t*>(output.data_ptr()),
reinterpret_cast<float*>(mean.data_ptr()),
reinterpret_cast<float*>(scale.data_ptr()),
scale_max,
num_tokens,
stride_bz_input, stride_d_input, stride_h_input,
stride_bz_output, stride_d_output, stride_h_output,
mean.stride(0), mean.stride(1),
scale.stride(0), scale.stride(1)
);
});
}
Content from @C:\SageAttention\csrc\fused\fused.h:
/*
Copyright (c) 2024 by SageAttention 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 <torch/csrc/stable/tensor.h>
using torch::stable::Tensor;
void quant_per_block_int8_scale_cuda(
Tensor input,
Tensor output,
Tensor scale,
double sm_scale,
int64_t block_size,
int64_t tensor_layout);
void quant_per_block_int8_cuda(
Tensor input,
Tensor output,
Tensor scale,
int64_t block_size,
int64_t tensor_layout);
void quant_per_block_int8_fuse_sub_mean_cuda(
Tensor input,
Tensor mean,
Tensor output,
Tensor scale,
int64_t block_size,
int64_t tensor_layout);
void quant_per_warp_int8_cuda(
Tensor input,
Tensor output,
Tensor scale,
int64_t block_size,
int64_t warp_block_size,
int64_t tensor_layout);
void sub_mean_cuda(
Tensor input,
Tensor mean,
Tensor output,
int64_t tensor_layout);
void transpose_pad_permute_cuda(
Tensor input,
Tensor output,
int64_t tensor_layout);
void scale_fuse_quant_cuda(
Tensor input,
Tensor output,
Tensor scale,
int64_t num_tokens,
double scale_max,
int64_t tensor_layout);
void mean_scale_fuse_quant_cuda(
Tensor input,
Tensor output,
Tensor mean,
Tensor scale,
int64_t num_tokens,
double scale_max,
int64_t tensor_layout);
Content from @C:\SageAttention\csrc\fused\pybind.cpp:
/*
Copyright (c) 2024 by SageAttention 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 <Python.h>
#include <torch/csrc/stable/library.h>
extern "C" {
/* Creates a dummy empty _C module that can be imported from Python.
The import from Python will load the .so consisting of this file
in this extension, so that the TORCH_LIBRARY static initializers
below are run. /
PyObject PyInit__fused(void)
{
static struct PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"_fused", /* name of module /
NULL, / module documentation, may be NULL /
-1, / size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. /
NULL, / methods */
};
return PyModule_Create(&module_def);
}
}
void boxed_quant_per_block_int8_scale_cuda(
StableIValue* stack,
uint64_t num_args,
uint64_t num_outputs
) {
auto input = to(stack[0]);
auto output = to(stack[1]);
auto scale = to(stack[2]);
auto sm_scale = to(stack[3]);
auto block_size = to<int64_t>(stack[4]);
auto tensor_layout = to<int64_t>(stack[5]);
code Copy code
quant_per_block_int8_scale_cuda(input, output, scale, sm_scale, block_size, tensor_layout);
void boxed_quant_per_block_int8_cuda(
StableIValue* stack,
uint64_t num_args,
uint64_t num_outputs
) {
auto input = to(stack[0]);
auto output = to(stack[1]);
auto scale = to(stack[2]);
auto block_size = to<int64_t>(stack[3]);
auto tensor_layout = to<int64_t>(stack[4]);
code Copy code
quant_per_block_int8_cuda(input, output, scale, block_size, tensor_layout);
void boxed_quant_per_block_int8_fuse_sub_mean_cuda(
StableIValue* stack,
uint64_t num_args,
uint64_t num_outputs
) {
auto input = to(stack[0]);
auto mean = to(stack[1]);
auto output = to(stack[2]);
auto scale = to(stack[3]);
auto block_size = to<int64_t>(stack[4]);
auto tensor_layout = to<int64_t>(stack[5]);
code Copy code
quant_per_block_int8_fuse_sub_mean_cuda(input, mean, output, scale, block_size, tensor_layout);
void boxed_quant_per_warp_int8_cuda(
StableIValue* stack,
uint64_t num_args,
uint64_t num_outputs
) {
auto input = to(stack[0]);
auto output = to(stack[1]);
auto scale = to(stack[2]);
auto block_size = to<int64_t>(stack[3]);
auto wrap_block_size = to<int64_t>(stack[4]);
auto tensor_layout = to<int64_t>(stack[5]);
code Copy code
quant_per_warp_int8_cuda(input, output, scale, block_size, wrap_block_size, tensor_layout);
void boxed_sub_mean_cuda(
StableIValue* stack,
uint64_t num_args,
uint64_t num_outputs
) {
auto input = to(stack[0]);
auto mean = to(stack[1]);
auto output = to(stack[2]);
auto tensor_layout = to<int64_t>(stack[3]);
code Copy code
sub_mean_cuda(input, mean, output, tensor_layout);
void boxed_transpose_pad_permute_cuda(
StableIValue* stack,
uint64_t num_args,
uint64_t num_outputs
) {
auto input = to(stack[0]);
auto output = to(stack[1]);
auto tensor_layout = to<int64_t>(stack[2]);
code Copy code
transpose_pad_permute_cuda(input, output, tensor_layout);
void boxed_scale_fuse_quant_cuda(
StableIValue* stack,
uint64_t num_args,
uint64_t num_outputs
) {
auto input = to(stack[0]);
auto output = to(stack[1]);
auto scale = to(stack[2]);
auto num_tokens = to<int64_t>(stack[3]);
auto scale_max = to(stack[4]);
auto tensor_layout = to<int64_t>(stack[5]);
code Copy code
scale_fuse_quant_cuda(input, output, scale, num_tokens, scale_max, tensor_layout);
void boxed_mean_scale_fuse_quant_cuda(
StableIValue* stack,
uint64_t num_args,
uint64_t num_outputs
) {
auto input = to(stack[0]);
auto output = to(stack[1]);
auto mean = to(stack[2]);
auto scale = to(stack[3]);
auto num_tokens = to<int64_t>(stack[4]);
auto scale_max = to(stack[5]);
auto tensor_layout = to<int64_t>(stack[6]);
code Copy code
mean_scale_fuse_quant_cuda(input, output, mean, scale, num_tokens, scale_max, tensor_layout);
// Defines the operators
STABLE_TORCH_LIBRARY(sageattention_fused, m) {
m.def("quant_per_block_int8_scale_cuda("
"Tensor input, "
"Tensor(a!) output, "
"Tensor scale, "
"float sm_scale, "
"int block_size, "
"int tensor_layout"
") -> ()");
m.def("quant_per_block_int8_cuda("
"Tensor input, "
"Tensor(a!) output, "
"Tensor scale, "
"int block_size, "
"int tensor_layout"
") -> ()");
m.def("quant_per_block_int8_fuse_sub_mean_cuda("
"Tensor input, "
"Tensor mean, "
"Tensor(a!) output, "
"Tensor scale, "
"int block_size, "
"int tensor_layout"
") -> ()");
m.def("quant_per_warp_int8_cuda("
"Tensor input, "
"Tensor(a!) output, "
"Tensor scale, "
"int block_size, "
"int wrap_block_size, "
"int tensor_layout"
") -> ()");
m.def("sub_mean_cuda("
"Tensor input, "
"Tensor mean, "
"Tensor(a!) output, "
"int tensor_layout"
") -> ()");
m.def("transpose_pad_permute_cuda("
"Tensor input, "
"Tensor(a!) output, "
"int tensor_layout"
") -> ()");
m.def("scale_fuse_quant_cuda("
"Tensor input, "
"Tensor(a!) output, "
"Tensor scale, "
"int num_tokens, "
"float scale_max, "
"int tensor_layout"
") -> ()");
m.def("mean_scale_fuse_quant_cuda("
"Tensor input, "
"Tensor(a!) output, "
"Tensor mean, "
"Tensor scale, "
"int num_tokens, "
"float scale_max, "
"int tensor_layout"
") -> ()");
}
// Registers CUDA implementations
STABLE_TORCH_LIBRARY_IMPL(sageattention_fused, CUDA, m) {
m.impl("quant_per_block_int8_scale_cuda", &boxed_quant_per_block_int8_scale_cuda);
m.impl("quant_per_block_int8_cuda", &boxed_quant_per_block_int8_cuda);
m.impl("quant_per_block_int8_fuse_sub_mean_cuda", &boxed_quant_per_block_int8_fuse_sub_mean_cuda);
m.impl("quant_per_warp_int8_cuda", &boxed_quant_per_warp_int8_cuda);
m.impl("sub_mean_cuda", &boxed_sub_mean_cuda);
m.impl("transpose_pad_permute_cuda", &boxed_transpose_pad_permute_cuda);
m.impl("scale_fuse_quant_cuda", &boxed_scale_fuse_quant_cuda);
m.impl("mean_scale_fuse_quant_cuda", &boxed_mean_scale_fuse_quant_cuda);
}
--- End of content ---
Copy first response