|
| 1 | +""" |
| 2 | +Copyright (c) 2024 by SageAttention, The HuggingFace team. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the |
| 5 | +License. You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an |
| 10 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +specific language governing permissions and limitations under the License. |
| 12 | +""" |
| 13 | + |
| 14 | +""" |
| 15 | +Modified from |
| 16 | +https://github.com/thu-ml/SageAttention/blob/68de3797d163b89d28f9a38026c3b7313f6940d2/sageattention/core.py |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +import torch # noqa |
| 21 | + |
| 22 | + |
| 23 | +SAGE_ATTENTION_DISPATCH = { |
| 24 | + "sm80": { |
| 25 | + "func": "sageattn_qk_int8_pv_fp16_cuda", |
| 26 | + "kwargs": { |
| 27 | + "tensor_layout": "NHD", |
| 28 | + "is_causal": False, |
| 29 | + "sm_scale": None, |
| 30 | + "return_lse": False, |
| 31 | + "pv_accum_dtype": "fp32", |
| 32 | + }, |
| 33 | + }, |
| 34 | + "sm89": { |
| 35 | + "func": "sageattn_qk_int8_pv_fp8_cuda", |
| 36 | + "kwargs": { |
| 37 | + "tensor_layout": "NHD", |
| 38 | + "is_causal": False, |
| 39 | + "sm_scale": None, |
| 40 | + "return_lse": False, |
| 41 | + "pv_accum_dtype": "fp32+fp16", |
| 42 | + }, |
| 43 | + }, |
| 44 | + "sm90": { |
| 45 | + "func": "sageattn_qk_int8_pv_fp8_cuda_sm90", |
| 46 | + "kwargs": { |
| 47 | + "tensor_layout": "NHD", |
| 48 | + "is_causal": False, |
| 49 | + "sm_scale": None, |
| 50 | + "return_lse": False, |
| 51 | + "pv_accum_dtype": "fp32+fp32", |
| 52 | + }, |
| 53 | + }, |
| 54 | + "sm120": { |
| 55 | + "func": "sageattn_qk_int8_pv_fp8_cuda", |
| 56 | + "kwargs": { |
| 57 | + "tensor_layout": "NHD", |
| 58 | + "is_causal": False, |
| 59 | + "qk_quant_gran": "per_warp", |
| 60 | + "sm_scale": None, |
| 61 | + "return_lse": False, |
| 62 | + "pv_accum_dtype": "fp32+fp16", |
| 63 | + }, |
| 64 | + }, |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +def get_cuda_version(): |
| 69 | + if torch.cuda.is_available(): |
| 70 | + major, minor = torch.cuda.get_device_capability() |
| 71 | + return major, minor |
| 72 | + else: |
| 73 | + raise EnvironmentError("CUDA not found.") |
| 74 | + |
| 75 | + |
| 76 | +def get_cuda_arch_versions(): |
| 77 | + if not torch.cuda.is_available(): |
| 78 | + EnvironmentError("CUDA not found.") |
| 79 | + cuda_archs = [] |
| 80 | + for i in range(torch.cuda.device_count()): |
| 81 | + major, minor = torch.cuda.get_device_capability(i) |
| 82 | + cuda_archs.append(f"sm{major}{minor}") |
| 83 | + return cuda_archs |
| 84 | + |
| 85 | + |
| 86 | +# Unlike the actual implementation, we just maintain function names rather than actual |
| 87 | +# implementations. |
| 88 | +def _get_sage_attn_fn_for_device(): |
| 89 | + """ |
| 90 | + Automatically selects the appropriate implementation of the SageAttention kernel based on the GPU compute |
| 91 | + capability. |
| 92 | +
|
| 93 | + Parameters ---------- q : torch.Tensor |
| 94 | + The query tensor. Shape: |
| 95 | + - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. |
| 96 | + - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``. |
| 97 | +
|
| 98 | + k : torch.Tensor |
| 99 | + The key tensor. Shape: |
| 100 | + - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. |
| 101 | + - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``. |
| 102 | +
|
| 103 | + v : torch.Tensor |
| 104 | + The value tensor. Shape: |
| 105 | + - If `tensor_layout` is "HND": ``[batch_size, num_kv_heads, kv_len, head_dim]``. |
| 106 | + - If `tensor_layout` is "NHD": ``[batch_size, kv_len, num_kv_heads, head_dim]``. |
| 107 | +
|
| 108 | + tensor_layout : str |
| 109 | + The tensor layout, either "HND" or "NHD". Default: "HND". |
| 110 | +
|
| 111 | + is_causal : bool |
| 112 | + Whether to apply causal mask to the attention matrix. Only applicable when qo_len == kv_len. Default: False. |
| 113 | +
|
| 114 | + sm_scale : Optional[float] |
| 115 | + The scale used in softmax, if not provided, will be set to ``1.0 / sqrt(head_dim)``. |
| 116 | +
|
| 117 | + return_lse : bool |
| 118 | + Whether to return the log sum of the exponentiated attention weights. Used for cases like Ring Attention. |
| 119 | + Default: False. |
| 120 | +
|
| 121 | + Returns ------- torch.Tensor |
| 122 | + The output tensor. Shape: |
| 123 | + - If `tensor_layout` is "HND": ``[batch_size, num_qo_heads, qo_len, head_dim]``. |
| 124 | + - If `tensor_layout` is "NHD": ``[batch_size, qo_len, num_qo_heads, head_dim]``. |
| 125 | +
|
| 126 | + torch.Tensor |
| 127 | + The logsumexp of each row of the matrix QK^T * scaling (e.g., log of the softmax normalization factor). Shape: |
| 128 | + ``[batch_size, num_qo_heads, qo_len]``. Only returned if `return_lse` is True. |
| 129 | +
|
| 130 | + Note ---- |
| 131 | + - ``num_qo_heads`` must be divisible by ``num_kv_heads``. |
| 132 | + - The tensors `q`, `k`, and `v` must have the dtype ``torch.float16`` or ``torch.bfloat16`` |
| 133 | + - All tensors must be on the same cuda device. |
| 134 | + """ |
| 135 | + device_index = torch.cuda.current_device() |
| 136 | + arch = get_cuda_arch_versions()[device_index] |
| 137 | + return SAGE_ATTENTION_DISPATCH[arch] |
0 commit comments