| 1 | /* |
| 2 | * Adapted from |
| 3 | * https://github.com/NVIDIA/FasterTransformer/blob/release/v5.3_tag/src/fastertransformer/kernels/decoder_masked_multihead_attention_utils.h |
| 4 | * Copyright (c) 2023, The vLLM team. |
| 5 | * Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | #pragma once |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | namespace vllm { |
| 24 | |
| 25 | // A vector type to store Q, K, V elements. |
| 26 | template <typename T, int VEC_SIZE> |
| 27 | struct Vec {}; |
| 28 | |
| 29 | // A vector type to store FP32 accumulators. |
| 30 | template <typename T> |
| 31 | struct FloatVec {}; |
| 32 | |
| 33 | // Template vector operations. |
| 34 | template <typename Acc, typename A, typename B> |
| 35 | inline __device__ Acc mul(A a, B b); |
| 36 | |
| 37 | template <typename T> |
| 38 | inline __device__ float sum(T v); |
| 39 | |
| 40 | template <typename T> |
| 41 | inline __device__ float dot(T a, T b) { |
| 42 | return sum(mul<T, T, T>(a, b)); |
| 43 | } |
| 44 | |
| 45 | template <typename A, typename T> |
| 46 | inline __device__ float dot(T a, T b) { |
| 47 | return sum(mul<A, T, T>(a, b)); |
| 48 | } |
| 49 | |
| 50 | template <typename T> |
| 51 | inline __device__ void zero(T& dst) { |
| 52 | constexpr int WORDS = sizeof(T) / 4; |
| 53 | union { |
| 54 | T raw; |
| 55 | uint32_t words[WORDS]; |
| 56 | } tmp; |
| 57 | |
| 58 | #pragma unroll |
| 59 | for (int ii = 0; ii < WORDS; ++ii) { |
| 60 | tmp.words[ii] = 0u; |
| 61 | } |
| 62 | dst = tmp.raw; |
| 63 | } |
| 64 | |
| 65 | } // namespace vllm |