Skip to content
Merged
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
624 changes: 552 additions & 72 deletions csrc/xpu/gdn_attn/causal_conv1d.hpp
100755 → 100644

Large diffs are not rendered by default.

458 changes: 424 additions & 34 deletions csrc/xpu/gdn_attn/gated_delta_rule.hpp

Large diffs are not rendered by default.

381 changes: 299 additions & 82 deletions csrc/xpu/gdn_attn/gdn_attn_interface.cpp

Large diffs are not rendered by default.

43 changes: 31 additions & 12 deletions csrc/xpu/gdn_attn/xe_2/chunk_causal_conv1d_xe2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct chunk_causal_conv1d_kernel {
int* query_start_loc,
int* cache_indices,
bool* has_initial_state,
const int* token_indx,
const ActMode& act_mode,
const int& pad_slot_id,
const int& batch_size,
Expand Down Expand Up @@ -62,6 +63,7 @@ struct chunk_causal_conv1d_kernel {
query_start_loc(query_start_loc),
cache_indices(cache_indices),
has_initial_state(has_initial_state),
token_indx(token_indx),
act_mode(act_mode),
pad_slot_id(pad_slot_id),
batch_size(batch_size),
Expand All @@ -74,6 +76,8 @@ struct chunk_causal_conv1d_kernel {
qkvz_elems(qkvz_elems),
conv_elems(conv_elems) {}

inline int lookup(int t) const { return token_indx ? token_indx[t] : t; }

static inline sycl::nd_range<2> get_nd_range(
const int total_seqlen,
const int num_k_heads,
Expand Down Expand Up @@ -219,11 +223,11 @@ struct chunk_causal_conv1d_kernel {

#pragma unroll
for (int i = 0; i < input_load_len; ++i) {
const int src_token = lookup(token_id - input_load_len + 1 + i);
#pragma unroll
for (int e = 0; e < elems_per_item; ++e) {
local_input[Width * e + states_load_len + i] = mixed_qkvz
[(token_id - input_load_len + 1 + i) * qkvz_elems +
qkvz_elems_offset + e];
local_input[Width * e + states_load_len + i] =
mixed_qkvz[src_token * qkvz_elems + qkvz_elems_offset + e];
}
}

Expand Down Expand Up @@ -329,6 +333,7 @@ struct chunk_causal_conv1d_kernel {
const int32_t* query_start_loc;
const int* cache_indices;
const bool* has_initial_state;
const int* token_indx;
const ActMode act_mode;
const int pad_slot_id;
const int batch_size;
Expand Down Expand Up @@ -360,6 +365,7 @@ struct chunk_reorder_zba_kernel {
const T* mixed_qkvz,
const T* mixed_ba,
const int32_t* query_start_loc,
const int* token_indx,
const int batch_size,
const int& num_virtual_tokens,
const int& num_k_heads,
Expand All @@ -372,13 +378,16 @@ struct chunk_reorder_zba_kernel {
mixed_qkvz(mixed_qkvz),
mixed_ba(mixed_ba),
query_start_loc(query_start_loc),
token_indx(token_indx),
batch_size(batch_size),
num_virtual_tokens(num_virtual_tokens),
num_k_heads(num_k_heads),
head_k_dim(head_k_dim),
num_v_heads(num_v_heads),
head_v_dim(head_v_dim) {}

inline int lookup(int t) const { return token_indx ? token_indx[t] : t; }

static inline sycl::nd_range<2>
get_nd_range(const int total_seqlen, const int num_k_heads, const int z_dim) {
assert(z_dim % load_size == 0);
Expand Down Expand Up @@ -418,7 +427,7 @@ struct chunk_reorder_zba_kernel {
}

if constexpr (ReorderInput) {
int step = token_id * num_v_heads * 2;
int step = lookup(token_id) * num_v_heads * 2;
#pragma unroll
for (int e = 0; e < kv_ratio; ++e) {
float b_value = mixed_ba[step + k_head_id * kv_ratio + e];
Expand All @@ -433,9 +442,9 @@ struct chunk_reorder_zba_kernel {
a_value;
}
} else {
int step =
(token_id * num_v_heads + k_head_id * num_v_heads / num_k_heads) *
2;
int step = (lookup(token_id) * num_v_heads +
k_head_id * num_v_heads / num_k_heads) *
2;
#pragma unroll
for (int e = 0; e < kv_ratio; ++e) {
float b_value = mixed_ba[step + e];
Expand All @@ -452,17 +461,18 @@ struct chunk_reorder_zba_kernel {
}

// reorder z
const int z_src_token = lookup(token_id);
#pragma unroll
for (int e = 0; e < load_size; ++e) {
int z_dim_id = dim_offset * load_size + e;
int mixed_z_id = token_id * num_k_heads * qkvz_dim +
int mixed_z_id = z_src_token * num_k_heads * qkvz_dim +
k_head_id * qkvz_dim + qkv_dim + z_dim_id;
if constexpr (ReorderInput) {
mixed_z_id = token_id * num_k_heads * qkvz_dim +
mixed_z_id = z_src_token * num_k_heads * qkvz_dim +
2 * num_k_heads * head_k_dim + num_v_heads * head_v_dim +
k_head_id * z_dim + z_dim_id;
}
z_out[token_id * num_k_heads * z_dim + k_head_id * z_dim + z_dim_id] =
z_out[z_src_token * num_k_heads * z_dim + k_head_id * z_dim + z_dim_id] =
mixed_qkvz[mixed_z_id];
}
}
Expand All @@ -474,6 +484,7 @@ struct chunk_reorder_zba_kernel {
const T* mixed_qkvz;
const T* mixed_ba;
const int32_t* query_start_loc;
const int* token_indx;
const int batch_size;
const int num_virtual_tokens;
const int num_k_heads;
Expand Down Expand Up @@ -574,6 +585,7 @@ void kernel_launcher(
int* query_start_loc,
int* cache_indices,
bool* has_initial_state,
const int* token_indx,
const ActMode& act_mode,
const int& pad_slot_id,
const int& batch_size,
Expand Down Expand Up @@ -608,6 +620,7 @@ void kernel_launcher(
query_start_loc,
cache_indices,
has_initial_state,
token_indx,
act_mode,
pad_slot_id,
batch_size,
Expand Down Expand Up @@ -638,6 +651,7 @@ void kernel_launcher(
mixed_qkvz,
mixed_ba,
query_start_loc,
token_indx,
batch_size,
num_virtual_tokens,
num_k_heads,
Expand Down Expand Up @@ -696,13 +710,17 @@ void chunk_causal_conv1d_xe2(
const int& pad_slot_id, // -1
const int num_prefills,
const int num_decodes,
const bool reorder_input) {
const bool reorder_input,
const int* token_indx = nullptr,
int num_actual_tokens_override = -1) {
if (num_prefills == 0 && num_decodes == 0) {
return;
}

const int batch_size = query_start_loc.size(0) - 1;
const int num_actual_tokens = mixed_qkvz.size(0);
const int num_actual_tokens = num_actual_tokens_override >= 0
? num_actual_tokens_override
: static_cast<int>(mixed_qkvz.size(0));
const int num_virtual_tokens = q_out.size(0);
const int num_k_heads = q_out.size(1);
const int head_k_dim = q_out.size(2);
Expand Down Expand Up @@ -742,6 +760,7 @@ void chunk_causal_conv1d_xe2(
has_initial_state.has_value() \
? reinterpret_cast<bool*>(has_initial_state->data_ptr()) \
: nullptr, \
token_indx, \
act_mode, \
pad_slot_id, \
batch_size, \
Expand Down
14 changes: 11 additions & 3 deletions csrc/xpu/gdn_attn/xe_2/chunk_gated_delta_rule_kernels_xe2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ CUTE_DEVICE void chunk_fwd_o_kernel(
const int* query_start_loc,
const int* cache_indices,
const bool* has_initial_state,
const int* token_indx,
const int batch_size,
const int total_virtual_seqlen,
const int num_k_heads,
Expand Down Expand Up @@ -1145,8 +1146,11 @@ CUTE_DEVICE void chunk_fwd_o_kernel(
auto U_tensor_T = make_tensor(
make_gmem_ptr(U_ptr),
make_layout(U_tensor_T_shape, make_stride(_1{}, head_v_dim)));
auto O_ptr = core_attn_out + out_chunk_offset * num_v_heads * head_v_dim +
v_head_id * head_v_dim;
auto O_ptr =
core_attn_out +
(token_indx ? token_indx[out_chunk_offset] : out_chunk_offset) *
num_v_heads * head_v_dim +
v_head_id * head_v_dim;
auto O_tensor_shape = make_shape(current_chunk_size, head_v_dim);
auto O_tensor = make_tensor(
make_gmem_ptr(O_ptr),
Expand Down Expand Up @@ -1282,6 +1286,7 @@ void kernel_launcher(
const int* query_start_loc,
const int* cache_indices,
const bool* has_initial_state,
const int* token_indx,
const int batch_size,
const int total_virtual_seqlen,
const int num_k_heads,
Expand Down Expand Up @@ -1510,6 +1515,7 @@ void kernel_launcher(
query_start_loc,
cache_indices,
has_initial_state,
token_indx,
batch_size,
total_virtual_seqlen,
num_k_heads,
Expand Down Expand Up @@ -1537,7 +1543,8 @@ void chunk_gated_delta_rule_impl_xe2(
const std::optional<torch::Tensor>&
has_initial_state, // [batch_size] or None
const int num_prefills,
const int num_decodes) {
const int num_decodes,
const int* token_indx) {
if (num_prefills == 0 && num_decodes == 0) {
return;
}
Expand Down Expand Up @@ -1604,6 +1611,7 @@ void chunk_gated_delta_rule_impl_xe2(
has_initial_state.has_value() \
? reinterpret_cast<bool*>(has_initial_state->data_ptr()) \
: nullptr, \
token_indx, \
batch_size, \
total_virtual_seqlen, \
num_k_heads, \
Expand Down
6 changes: 4 additions & 2 deletions csrc/xpu/gdn_attn/xe_2/chunk_gated_delta_rule_xe2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ void chunk_gated_delta_rule_xe2(
const std::optional<torch::Tensor>&
has_initial_state, // [batch_size] or None
const int num_prefills,
const int num_decodes) {
const int num_decodes,
const int* token_indx) {
gdn::chunk_gated_delta_rule_impl_xe2(
queue,
core_attn_out,
Expand All @@ -37,5 +38,6 @@ void chunk_gated_delta_rule_xe2(
cache_indices,
has_initial_state,
num_prefills,
num_decodes);
num_decodes,
token_indx);
}
3 changes: 2 additions & 1 deletion csrc/xpu/gdn_attn/xe_2/chunk_gated_delta_rule_xe2.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ void chunk_gated_delta_rule_xe2(
const std::optional<torch::Tensor>&
has_initial_state, // [batch_size] or None
const int num_prefills,
const int num_decodes);
const int num_decodes,
const int* token_indx = nullptr);
10 changes: 8 additions & 2 deletions csrc/xpu/ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,15 @@ void gdn_attention(
const torch::Tensor& dt_bias,
const int64_t num_prefills,
const int64_t num_decodes,
const int64_t num_spec_decodes,
const std::optional<torch::Tensor>& has_initial_state,
const torch::Tensor& non_spec_query_start_loc,
const torch::Tensor& non_spec_state_indices_tensor,
const std::optional<torch::Tensor>& non_spec_query_start_loc,
const std::optional<torch::Tensor>& non_spec_token_indx,
const std::optional<torch::Tensor>& non_spec_state_indices_tensor,
const std::optional<torch::Tensor>& spec_query_start_loc,
const std::optional<torch::Tensor>& spec_token_indx,
const std::optional<torch::Tensor>& spec_state_indices_tensor,
const std::optional<torch::Tensor>& num_accepted_tokens,
const int64_t num_actual_tokens,
const int64_t tp_size,
const bool reorder_input);
Expand Down
9 changes: 6 additions & 3 deletions csrc/xpu/torch_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, xpu_ops) {
"int num_k_heads, int num_v_heads, int head_k_dim, int head_v_dim,"
"Tensor! conv_state, Tensor! ssm_state, Tensor conv_weights, Tensor? "
"conv_bias, str activation, Tensor A_log, Tensor dt_bias,"
"int num_prefills, int num_decodes, Tensor? has_initial_state, Tensor "
"non_spec_query_start_loc,"
"Tensor non_spec_state_indices_tensor, int num_actual_tokens, int "
"int num_prefills, int num_decodes, int num_spec_decodes, Tensor? "
"has_initial_state, Tensor? "
"non_spec_query_start_loc, Tensor? non_spec_token_indx,"
"Tensor? non_spec_state_indices_tensor, Tensor? spec_query_start_loc, "
"Tensor? spec_token_indx, Tensor? spec_state_indices_tensor, Tensor? "
"num_accepted_tokens, int num_actual_tokens, int "
"tp_size, bool reorder_input) -> ()");
xpu_ops.impl("gdn_attention", torch::kXPU, &gdn_attention);
#endif
Expand Down
Loading
Loading