|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 by FlashInfer team. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#ifndef FLASHINFER_ATTENTION_HOPPER_VARIANT_HELPER_H |
| 17 | +#define FLASHINFER_ATTENTION_HOPPER_VARIANT_HELPER_H |
| 18 | + |
| 19 | +#include <cuda_runtime.h> |
| 20 | + |
| 21 | +#include <cstdint> |
| 22 | + |
| 23 | +namespace flashinfer { |
| 24 | + |
| 25 | +#define REGISTER_QUERY_TRANSFORM(params, q, ...) \ |
| 26 | + template <typename MainloopParams, typename T> \ |
| 27 | + __device__ __forceinline__ T QueryTransform(const MainloopParams& params, void* q_smem) { \ |
| 28 | + __VA_ARGS__ \ |
| 29 | + } |
| 30 | + |
| 31 | +#define REGISTER_KEY_TRANSFORM(params, k, ...) \ |
| 32 | + template <typename MainloopParams, typename T> \ |
| 33 | + __device__ __forceinline__ T KeyTransform(const MainloopParams& params, void* k_smem) { \ |
| 34 | + __VA_ARGS__ \ |
| 35 | + } |
| 36 | + |
| 37 | +#define REGISTER_LOGITS_TRANSFORM(params, logits, batch_idx, qo_idx, kv_idx, qo_head_idx, \ |
| 38 | + kv_head_idx, ...) \ |
| 39 | + template <typename MainloopParams, typename T> \ |
| 40 | + __device__ __forceinline__ T LogitsTransform( \ |
| 41 | + const MainloopParams& params, T logits, uint32_t batch_idx, uint32_t qo_idx, \ |
| 42 | + uint32_t kv_idx, uint32_t qo_head_idx, uint32_t kv_head_idx) { \ |
| 43 | + __VA_ARGS__ \ |
| 44 | + } |
| 45 | + |
| 46 | +#define REGISTER_LOGITS_MASK(params, batch_idx, qo_idx, kv_idx, qo_head_idx, kv_head_idx, ...) \ |
| 47 | + template <typename MainloopParams> \ |
| 48 | + __device__ __forceinline__ bool LogitsMask(const MainloopParams& params, uint32_t batch_idx, \ |
| 49 | + uint32_t qo_idx, uint32_t kv_idx, \ |
| 50 | + uint32_t qo_head_idx, uint32_t kv_head_idx) { \ |
| 51 | + __VA_ARGS__ \ |
| 52 | + } |
| 53 | + |
| 54 | +struct AttentionVariantBase { |
| 55 | + REGISTER_LOGITS_TRANSFORM(params, logits, batch_idx, qo_idx, kv_idx, qo_head_idx, kv_head_idx, |
| 56 | + { return logits; }) |
| 57 | + |
| 58 | + REGISTER_LOGITS_MASK(params, batch_idx, qo_idx, kv_idx, qo_head_idx, kv_head_idx, |
| 59 | + { return true; }) |
| 60 | +}; |
| 61 | + |
| 62 | +} // namespace flashinfer |
| 63 | + |
| 64 | +#endif // FLASHINFER_ATTENTION_HOPPER_VARIANT_HELPER_H |
0 commit comments