|
| 1 | +#include "linked/torch/metax/ops/flash_attn_with_kvcache/flash_attn.h" |
| 2 | + |
| 3 | +#include "linked/torch/ops/flash_attn_with_kvcache.h" |
| 4 | +#include "torch/metax/c10.h" |
| 5 | + |
| 6 | +std::vector<at::Tensor> mha_fwd_kvcache( |
| 7 | + at::Tensor& q, const at::Tensor& k_cache, const at::Tensor& v_cache, |
| 8 | + std::optional<const at::Tensor>& k, std::optional<const at::Tensor>& v, |
| 9 | + std::optional<const at::Tensor>& cache_seqlens, |
| 10 | + std::optional<const at::Tensor>& rotary_cos, |
| 11 | + std::optional<const at::Tensor>& rotary_sin, |
| 12 | + std::optional<const at::Tensor>& cache_batch_idx, |
| 13 | + std::optional<const at::Tensor>& cache_leftpad, |
| 14 | + std::optional<at::Tensor>& block_table, |
| 15 | + std::optional<at::Tensor>& alibi_slopes, std::optional<at::Tensor>& out, |
| 16 | + float softmax_scale, bool causal, int window_size_left, |
| 17 | + int window_size_right, float softcap, bool rotary_interleaved, |
| 18 | + int num_splits, std::optional<at::Tensor>& flash_attn_mars_ext); |
| 19 | + |
| 20 | +namespace infini::ops::linked::torch::metax { |
| 21 | + |
| 22 | +struct FlashAttnKvcache : C10<Device::Type::kMetax> { |
| 23 | + static std::vector<at::Tensor> Call( |
| 24 | + at::Tensor& q, const at::Tensor& k_cache, const at::Tensor& v_cache, |
| 25 | + std::optional<const at::Tensor>& k, std::optional<const at::Tensor>& v, |
| 26 | + std::optional<const at::Tensor>& cache_seqlens, |
| 27 | + std::optional<const at::Tensor>& rotary_cos, |
| 28 | + std::optional<const at::Tensor>& rotary_sin, |
| 29 | + std::optional<const at::Tensor>& cache_batch_idx, |
| 30 | + std::optional<const at::Tensor>& cache_leftpad, |
| 31 | + std::optional<at::Tensor>& block_table, |
| 32 | + std::optional<at::Tensor>& alibi_slopes, std::optional<at::Tensor>& out, |
| 33 | + float softmax_scale, bool causal, int window_size_left, |
| 34 | + int window_size_right, float softcap, bool rotary_interleaved, |
| 35 | + int num_splits) { |
| 36 | + std::optional<at::Tensor> flash_attn_mars_ext; |
| 37 | + return ::mha_fwd_kvcache( |
| 38 | + q, k_cache, v_cache, k, v, cache_seqlens, rotary_cos, rotary_sin, |
| 39 | + cache_batch_idx, cache_leftpad, block_table, alibi_slopes, out, |
| 40 | + softmax_scale, causal, window_size_left, window_size_right, softcap, |
| 41 | + rotary_interleaved, num_splits, flash_attn_mars_ext); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +} // namespace infini::ops::linked::torch::metax |
| 46 | + |
| 47 | +namespace infini::ops { |
| 48 | + |
| 49 | +void Operator<FlashAttnWithKvcache, Device::Type::kMetax, 16>::operator()( |
| 50 | + const Tensor q, Tensor k_cache, Tensor v_cache, |
| 51 | + const std::optional<Tensor> k, const std::optional<Tensor> v, |
| 52 | + const std::optional<Tensor> rotary_cos, |
| 53 | + const std::optional<Tensor> rotary_sin, const int64_t cache_seqlens, |
| 54 | + const std::optional<Tensor> cache_batch_idx, |
| 55 | + const std::optional<Tensor> cache_leftpad, |
| 56 | + const std::optional<Tensor> block_table, |
| 57 | + const std::optional<Tensor> alibi_slopes, |
| 58 | + const std::optional<double> softmax_scale, const bool causal, |
| 59 | + const std::vector<int64_t> window_size, const double softcap, |
| 60 | + const bool rotary_interleaved, const int64_t num_splits, |
| 61 | + const bool return_softmax_lse, Tensor out, |
| 62 | + std::optional<Tensor> softmax_lse) const { |
| 63 | + using Delegate = linked::torch::TorchFlashAttnWithKvcache< |
| 64 | + linked::torch::metax::FlashAttnKvcache>; |
| 65 | + if (!delegate_) { |
| 66 | + delegate_ = std::make_unique<Delegate>( |
| 67 | + q, k_cache, v_cache, k, v, rotary_cos, rotary_sin, cache_seqlens, |
| 68 | + cache_batch_idx, cache_leftpad, block_table, alibi_slopes, |
| 69 | + softmax_scale, causal, window_size, softcap, rotary_interleaved, |
| 70 | + num_splits, return_softmax_lse, out, softmax_lse); |
| 71 | + } |
| 72 | + delegate_->set_stream(stream_); |
| 73 | + (*delegate_)(q, k_cache, v_cache, k, v, rotary_cos, rotary_sin, cache_seqlens, |
| 74 | + cache_batch_idx, cache_leftpad, block_table, alibi_slopes, |
| 75 | + softmax_scale, causal, window_size, softcap, rotary_interleaved, |
| 76 | + num_splits, return_softmax_lse, out, softmax_lse); |
| 77 | +} |
| 78 | + |
| 79 | +void Operator<FlashAttnWithKvcache, Device::Type::kMetax, 16>::operator()( |
| 80 | + const Tensor q, Tensor k_cache, Tensor v_cache, |
| 81 | + const std::optional<Tensor> k, const std::optional<Tensor> v, |
| 82 | + const std::optional<Tensor> rotary_cos, |
| 83 | + const std::optional<Tensor> rotary_sin, |
| 84 | + const std::optional<Tensor> cache_seqlens, |
| 85 | + const std::optional<Tensor> cache_batch_idx, |
| 86 | + const std::optional<Tensor> cache_leftpad, |
| 87 | + const std::optional<Tensor> block_table, |
| 88 | + const std::optional<Tensor> alibi_slopes, |
| 89 | + const std::optional<double> softmax_scale, const bool causal, |
| 90 | + const std::vector<int64_t> window_size, const double softcap, |
| 91 | + const bool rotary_interleaved, const int64_t num_splits, |
| 92 | + const bool return_softmax_lse, Tensor out, |
| 93 | + std::optional<Tensor> softmax_lse) const { |
| 94 | + using Delegate = linked::torch::TorchFlashAttnWithKvcache< |
| 95 | + linked::torch::metax::FlashAttnKvcache>; |
| 96 | + if (!delegate_) { |
| 97 | + delegate_ = std::make_unique<Delegate>( |
| 98 | + q, k_cache, v_cache, k, v, rotary_cos, rotary_sin, cache_seqlens, |
| 99 | + cache_batch_idx, cache_leftpad, block_table, alibi_slopes, |
| 100 | + softmax_scale, causal, window_size, softcap, rotary_interleaved, |
| 101 | + num_splits, return_softmax_lse, out, softmax_lse); |
| 102 | + } |
| 103 | + delegate_->set_stream(stream_); |
| 104 | + (*delegate_)(q, k_cache, v_cache, k, v, rotary_cos, rotary_sin, cache_seqlens, |
| 105 | + cache_batch_idx, cache_leftpad, block_table, alibi_slopes, |
| 106 | + softmax_scale, causal, window_size, softcap, rotary_interleaved, |
| 107 | + num_splits, return_softmax_lse, out, softmax_lse); |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace infini::ops |
0 commit comments