|
| 1 | +#ifndef __FLASH_ATTENTION_DESCRIPTOR_H__ |
| 2 | +#define __FLASH_ATTENTION_DESCRIPTOR_H__ |
| 3 | + |
| 4 | +#include "../../../handle.h" |
| 5 | +#include "../../../operator.h" |
| 6 | +#include "../../../tensor.h" |
| 7 | + |
| 8 | +#include "../../../../../build/ninetoothed/flash_attention.h" |
| 9 | +#include "../../../ninetoothed/utils.h" |
| 10 | + |
| 11 | +namespace op::flash_attention::ninetoothed { |
| 12 | + |
| 13 | +class Descriptor final : public InfiniopDescriptor { |
| 14 | +public: |
| 15 | + Descriptor(infiniopHandle_t handle, |
| 16 | + infiniopTensorDescriptor_t out_desc, |
| 17 | + infiniopTensorDescriptor_t q_desc, |
| 18 | + infiniopTensorDescriptor_t k_desc, |
| 19 | + infiniopTensorDescriptor_t v_desc, |
| 20 | + double scale, |
| 21 | + char is_causal) : InfiniopDescriptor{handle->device, handle->device_id}, |
| 22 | + _query_shape{q_desc->shape()}, |
| 23 | + _query_strides{q_desc->strides()}, |
| 24 | + _key_shape{k_desc->shape()}, |
| 25 | + _key_strides{k_desc->strides()}, |
| 26 | + _value_shape{v_desc->shape()}, |
| 27 | + _value_strides{v_desc->strides()}, |
| 28 | + _output_strides{out_desc->strides()}, |
| 29 | + _dtype{q_desc->dtype()}, |
| 30 | + _scale{scale}, |
| 31 | + _is_causal{is_causal} {} |
| 32 | + |
| 33 | + ~Descriptor() = default; |
| 34 | + |
| 35 | + size_t get_workspace_size() const { |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + infiniStatus_t calculate(void *workspace, |
| 40 | + size_t workspace_size, |
| 41 | + void *out, |
| 42 | + const void *q, |
| 43 | + const void *k, |
| 44 | + const void *v, |
| 45 | + void *stream) const { |
| 46 | + uint64_t empty_shape[4]; |
| 47 | + int64_t empty_strides[4]; |
| 48 | + |
| 49 | + auto query{::ninetoothed::Tensor{q, _query_shape, _query_strides}}; |
| 50 | + auto key{::ninetoothed::Tensor{k, _key_shape, _key_strides}}; |
| 51 | + auto value{::ninetoothed::Tensor{v, _value_shape, _value_strides}}; |
| 52 | + |
| 53 | + NineToothedTensor attn_mask{nullptr, empty_shape, empty_strides}; |
| 54 | + NineToothedTensor is_causal; |
| 55 | + NineToothedTensor scale{const_cast<double *>(&_scale), nullptr, nullptr}; |
| 56 | + auto output{::ninetoothed::Tensor{out, _query_shape, _output_strides}}; |
| 57 | + NineToothedTensor with_attn_mask; |
| 58 | + NineToothedTensor causal_variant; |
| 59 | + |
| 60 | + const auto with_kv_cache_{0}; |
| 61 | + const auto emb_dim_{_query_shape[3]}; |
| 62 | + const auto is_causal_{_is_causal}; |
| 63 | + const auto with_attn_mask_{0}; |
| 64 | + const auto causal_variant_{1}; |
| 65 | + const auto dtype_{_dtype}; |
| 66 | + |
| 67 | + constexpr auto block_size_m_{64}; |
| 68 | + constexpr auto block_size_n_{64}; |
| 69 | + |
| 70 | + launch_flash_attention(stream, |
| 71 | + query, |
| 72 | + key, |
| 73 | + value, |
| 74 | + attn_mask, |
| 75 | + is_causal, |
| 76 | + scale, |
| 77 | + output, |
| 78 | + with_attn_mask, |
| 79 | + causal_variant, |
| 80 | + with_kv_cache_, |
| 81 | + emb_dim_, |
| 82 | + is_causal_, |
| 83 | + with_attn_mask_, |
| 84 | + causal_variant_, |
| 85 | + dtype_, |
| 86 | + block_size_m_, |
| 87 | + block_size_n_); |
| 88 | + |
| 89 | + return INFINI_STATUS_SUCCESS; |
| 90 | + } |
| 91 | + |
| 92 | + static infiniStatus_t create(infiniopHandle_t handle, |
| 93 | + Descriptor **desc, |
| 94 | + infiniopTensorDescriptor_t out_desc, |
| 95 | + infiniopTensorDescriptor_t q_desc, |
| 96 | + infiniopTensorDescriptor_t k_desc, |
| 97 | + infiniopTensorDescriptor_t v_desc, |
| 98 | + double scale, |
| 99 | + char is_causal) { |
| 100 | + *desc = new Descriptor{handle, out_desc, q_desc, k_desc, v_desc, scale, is_causal}; |
| 101 | + |
| 102 | + return INFINI_STATUS_SUCCESS; |
| 103 | + } |
| 104 | + |
| 105 | +private: |
| 106 | + using Size = ::ninetoothed::Tensor<>::Size; |
| 107 | + |
| 108 | + using Stride = ::ninetoothed::Tensor<>::Stride; |
| 109 | + |
| 110 | + std::vector<Size> _query_shape; |
| 111 | + |
| 112 | + std::vector<Stride> _query_strides; |
| 113 | + |
| 114 | + std::vector<Size> _key_shape; |
| 115 | + |
| 116 | + std::vector<Stride> _key_strides; |
| 117 | + |
| 118 | + std::vector<Size> _value_shape; |
| 119 | + |
| 120 | + std::vector<Stride> _value_strides; |
| 121 | + |
| 122 | + std::vector<Stride> _output_strides; |
| 123 | + |
| 124 | + infiniDtype_t _dtype; |
| 125 | + |
| 126 | + double _scale; |
| 127 | + |
| 128 | + char _is_causal; |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace op::flash_attention::ninetoothed |
| 132 | + |
| 133 | +#endif // __FLASH_ATTENTION_DESCRIPTOR_H__ |
0 commit comments