Repository navigation
feat(mars): add llama flash attention operators #1055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wooway777
wants to merge
2
commits into
master
Choose a base branch
from
feat/mars-llama-flash-attn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| { | ||
| "add": { | ||
| "implementations": "all" | ||
| }, | ||
| "argmax": { | ||
| "implementations": [ | ||
| 8 | ||
| ] | ||
| }, | ||
| "cast": { | ||
| "public_api_only": true | ||
| }, | ||
| "cat": { | ||
| "public_api_only": true | ||
| }, | ||
| "causal_conv1d": { | ||
| "public_api_only": true | ||
| }, | ||
| "chunk_gated_delta_rule": { | ||
| "public_api_only": true | ||
| }, | ||
| "convolution": { | ||
| "public_api_only": true | ||
| }, | ||
| "copy": { | ||
| "implementations": "all" | ||
| }, | ||
| "deepseek_moe": { | ||
| "public_api_only": true | ||
| }, | ||
| "embedding": { | ||
| "implementations": "all" | ||
| }, | ||
| "fill": { | ||
| "implementations": "all" | ||
| }, | ||
| "flash_attn_varlen_func": { | ||
| "implementations": "all" | ||
| }, | ||
| "flash_attn_with_kvcache": { | ||
| "implementations": "all" | ||
| }, | ||
| "fused_add_rms_norm": { | ||
| "implementations": "all" | ||
| }, | ||
| "fused_gated_delta_net_gating": { | ||
| "public_api_only": true | ||
| }, | ||
| "fused_qk_norm": { | ||
| "public_api_only": true | ||
| }, | ||
| "fused_qk_rms_norm": { | ||
| "public_api_only": true | ||
| }, | ||
| "gelu": { | ||
| "public_api_only": true | ||
| }, | ||
| "gemm": { | ||
| "implementations": "all" | ||
| }, | ||
| "i8_gemm": { | ||
| "public_api_only": true | ||
| }, | ||
| "layer_norm": { | ||
| "public_api_only": true | ||
| }, | ||
| "mamba_selective_scan": { | ||
| "public_api_only": true | ||
| }, | ||
| "moe_align_block_size": { | ||
| "public_api_only": true | ||
| }, | ||
| "moe_fused_dense": { | ||
| "public_api_only": true | ||
| }, | ||
| "moe_fused_gate": { | ||
| "public_api_only": true | ||
| }, | ||
| "mrope": { | ||
| "public_api_only": true | ||
| }, | ||
| "mul": { | ||
| "implementations": "all" | ||
| }, | ||
| "pad": { | ||
| "public_api_only": true | ||
| }, | ||
| "paged_attention_infinilm": { | ||
| "implementations": "all" | ||
| }, | ||
| "paged_attention_prefill_infinilm": { | ||
| "implementations": "all" | ||
| }, | ||
| "per_channel_quant_i8": { | ||
| "public_api_only": true | ||
| }, | ||
| "per_tensor_dequant_i8": { | ||
| "public_api_only": true | ||
| }, | ||
| "per_tensor_quant_i8": { | ||
| "public_api_only": true | ||
| }, | ||
| "quick_gelu": { | ||
| "public_api_only": true | ||
| }, | ||
| "recurrent_gated_delta_rule": { | ||
| "public_api_only": true | ||
| }, | ||
| "relu": { | ||
| "public_api_only": true | ||
| }, | ||
| "reshape_and_cache_flash": { | ||
| "implementations": "all" | ||
| }, | ||
| "rms_norm": { | ||
| "implementations": "all" | ||
| }, | ||
| "rotary_embedding": { | ||
| "implementations": "all" | ||
| }, | ||
| "row_l2_normalize": { | ||
| "public_api_only": true | ||
| }, | ||
| "separate_table_rope": { | ||
| "public_api_only": true | ||
| }, | ||
| "sigmoid": { | ||
| "public_api_only": true | ||
| }, | ||
| "silu": { | ||
| "public_api_only": true | ||
| }, | ||
| "silu_and_mul": { | ||
| "implementations": "all" | ||
| }, | ||
| "softmax": { | ||
| "public_api_only": true | ||
| }, | ||
| "sum": { | ||
| "public_api_only": true | ||
| }, | ||
| "take": { | ||
| "public_api_only": true | ||
| }, | ||
| "top_k_top_p_sampling_from_logits": { | ||
| "public_api_only": true | ||
| }, | ||
| "topk_sigmoid": { | ||
| "public_api_only": true | ||
| }, | ||
| "topk_softmax": { | ||
| "public_api_only": true | ||
| }, | ||
| "tril": { | ||
| "public_api_only": true | ||
| }, | ||
| "triu": { | ||
| "public_api_only": true | ||
| }, | ||
| "upsample_bicubic2d": { | ||
| "public_api_only": true | ||
| }, | ||
| "upsample_bilinear2d": { | ||
| "public_api_only": true | ||
| }, | ||
| "w4_marlin_gemm": { | ||
| "public_api_only": true | ||
| }, | ||
| "w4_marlin_parameters": { | ||
| "public_api_only": true | ||
| }, | ||
| "w4_marlin_repack": { | ||
| "public_api_only": true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #ifndef INFINI_OPS_MARS_FLASH_ATTN_COMMON_H_ | ||
| #define INFINI_OPS_MARS_FLASH_ATTN_COMMON_H_ | ||
|
|
||
| #include <flash_attn/flash_attn.h> | ||
|
|
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| #include "data_type.h" | ||
| #include "tensor.h" | ||
|
|
||
| namespace infini::ops::mars_flash_attn { | ||
|
|
||
| inline hcflashattnDataType_t ToHcDataType(DataType dtype) { | ||
| switch (dtype) { | ||
| case DataType::kFloat16: | ||
| return HCFLASHATTN_DATATYPE_FP16; | ||
| case DataType::kBFloat16: | ||
| return HCFLASHATTN_DATATYPE_BF16; | ||
| case DataType::kInt32: | ||
| return HCFLASHATTN_DATATYPE_INT32; | ||
| case DataType::kInt64: | ||
| return HCFLASHATTN_DATATYPE_INT64; | ||
| case DataType::kFloat32: | ||
| return HCFLASHATTN_DATATYPE_FP32; | ||
| case DataType::kFloat64: | ||
| return HCFLASHATTN_DATATYPE_FP64; | ||
| default: | ||
| throw std::invalid_argument("unsupported HPCC FlashAttention dtype"); | ||
| } | ||
| } | ||
|
|
||
| inline Tensor_t MakeHcTensor(const Tensor& tensor) { | ||
| void* data = const_cast<void*>(tensor.data()); | ||
| const auto dtype = ToHcDataType(tensor.dtype()); | ||
| const auto size = [&tensor](std::size_t index) { | ||
| return static_cast<int>(tensor.size(index)); | ||
| }; | ||
| const auto stride = [&tensor](std::size_t index) { | ||
| return static_cast<int>(tensor.stride(index)); | ||
| }; | ||
| switch (tensor.ndim()) { | ||
| case 1: | ||
| return make_tensor1d(data, dtype, size(0), stride(0)); | ||
| case 2: | ||
| return make_tensor2d(data, dtype, size(0), size(1), stride(0), stride(1)); | ||
| case 3: | ||
| return make_tensor3d(data, dtype, size(0), size(1), size(2), stride(0), | ||
| stride(1), stride(2)); | ||
| case 4: | ||
| return make_tensor4d(data, dtype, size(0), size(1), size(2), size(3), | ||
| stride(0), stride(1), stride(2), stride(3)); | ||
| default: | ||
| throw std::invalid_argument("HPCC FlashAttention requires 1D-4D tensors"); | ||
| } | ||
| } | ||
|
|
||
| inline void ReleaseHcTensor(Tensor_t& tensor) { | ||
| if (tensor) { | ||
| release_tensor(tensor); | ||
| tensor = nullptr; | ||
| } | ||
| } | ||
|
|
||
| inline hcStream_t ToHcStream(void* stream) { | ||
| return reinterpret_cast<hcStream_t>(stream); | ||
| } | ||
|
|
||
| inline void CheckHcStatus(hcflashattnStatus_t status, const char* call) { | ||
| if (status != HCFLASHATTN_STATUS_SUCCESS) | ||
| throw std::runtime_error(std::string{"HPCC FlashAttention failed: "} + | ||
| call); | ||
| } | ||
|
|
||
| inline float SoftmaxScaleOrDefault(const std::optional<double>& scale, | ||
| std::size_t head_size) { | ||
| return static_cast<float>( | ||
| scale.value_or(1.0 / std::sqrt(static_cast<double>(head_size)))); | ||
| } | ||
|
|
||
| } // namespace infini::ops::mars_flash_attn | ||
|
|
||
| #endif // INFINI_OPS_MARS_FLASH_ATTN_COMMON_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #include "native/cuda/mars/ops/flash_attn_varlen_func/kernel.h" | ||
|
|
||
| namespace infini::ops { | ||
|
|
||
| void Operator<FlashAttnVarlenFunc, Device::Type::kMars, 0>::operator()( | ||
| const Tensor q, const Tensor k, const Tensor v, const Tensor cu_seqlens_q, | ||
| const Tensor cu_seqlens_k, const std::optional<Tensor> alibi_slopes, | ||
| const std::optional<Tensor> block_table, const int64_t max_seqlen_q, | ||
| const int64_t max_seqlen_k, const double dropout_p, | ||
| const std::optional<double> softmax_scale, const bool causal, | ||
| const std::vector<int64_t> window_size, const double softcap, | ||
| const bool deterministic, const bool return_attn_probs, Tensor out, | ||
| std::optional<Tensor> softmax_lse, std::optional<Tensor> s_dmask) const { | ||
| if (!block_table.has_value()) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| throw std::invalid_argument( | ||
| "Mars FlashAttnVarlenFunc requires a paged block table"); | ||
| if (alibi_slopes || softmax_lse || s_dmask || return_attn_probs || | ||
| deterministic || dropout_p != 0.0 || softcap != 0.0) | ||
| throw std::invalid_argument( | ||
| "Mars FlashAttnVarlenFunc supports inference only"); | ||
| if (q.dtype() != DataType::kFloat16 && q.dtype() != DataType::kBFloat16) | ||
| throw std::invalid_argument( | ||
| "Mars FlashAttnVarlenFunc requires float16 or bfloat16"); | ||
|
|
||
| Tensor_t hc_q = mars_flash_attn::MakeHcTensor(q); | ||
| Tensor_t hc_k = mars_flash_attn::MakeHcTensor(k); | ||
| Tensor_t hc_v = mars_flash_attn::MakeHcTensor(v); | ||
| Tensor_t hc_out = mars_flash_attn::MakeHcTensor(out); | ||
| Tensor_t hc_cu_q = mars_flash_attn::MakeHcTensor(cu_seqlens_q); | ||
| Tensor_t hc_cu_k = mars_flash_attn::MakeHcTensor(cu_seqlens_k); | ||
| Tensor_t hc_block_table = mars_flash_attn::MakeHcTensor(*block_table); | ||
| hcflashattnExtendParameter_t extend = make_extend_param(); | ||
| set_extend_parameter_block_table(extend, | ||
| const_cast<void*>(block_table->data()), | ||
| static_cast<int>(block_table->size(0)), | ||
| static_cast<int>(block_table->size(1))); | ||
|
|
||
| const int64_t batch_size = cu_seqlens_q.numel() - 1; | ||
| const int64_t total_k = k.size(0) * k.size(1); | ||
| const float scale = mars_flash_attn::SoftmaxScaleOrDefault( | ||
| softmax_scale, static_cast<std::size_t>(q.size(2))); | ||
| const auto status = mha_varlen_fwd( | ||
| batch_size, q.size(0), q.size(1), total_k, k.size(2), q.size(2), hc_q, | ||
| hc_k, hc_v, hc_out, hc_cu_q, hc_cu_k, nullptr, nullptr, nullptr, nullptr, | ||
| nullptr, static_cast<int>(max_seqlen_q), static_cast<int>(max_seqlen_k), | ||
| 0.0f, scale, causal, static_cast<int>(window_size.at(0)), | ||
| static_cast<int>(window_size.at(1)), | ||
| mars_flash_attn::ToHcStream(stream_ ? stream_ : nullptr), extend); | ||
|
|
||
| release_extend_param(extend); | ||
| mars_flash_attn::ReleaseHcTensor(hc_block_table); | ||
| mars_flash_attn::ReleaseHcTensor(hc_cu_k); | ||
| mars_flash_attn::ReleaseHcTensor(hc_cu_q); | ||
| mars_flash_attn::ReleaseHcTensor(hc_out); | ||
| mars_flash_attn::ReleaseHcTensor(hc_v); | ||
| mars_flash_attn::ReleaseHcTensor(hc_k); | ||
| mars_flash_attn::ReleaseHcTensor(hc_q); | ||
| mars_flash_attn::CheckHcStatus(status, "mha_varlen_fwd"); | ||
| } | ||
|
|
||
| } // namespace infini::ops | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果
instantiate.h不改,这里应该也可以不改