|
| 1 | +""" |
| 2 | +Copyright (c) 2023 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 | + |
| 17 | +import itertools |
| 18 | + |
| 19 | +import torch |
| 20 | + |
| 21 | +import flashinfer |
| 22 | + |
| 23 | + |
| 24 | +def jit_decode_attention_func_args( |
| 25 | + q_dtypes, |
| 26 | + kv_dtypes, |
| 27 | + head_dims, |
| 28 | + pos_encoding_modes, |
| 29 | + use_sliding_window_options, |
| 30 | + use_logits_soft_cap_options, |
| 31 | +): |
| 32 | + load_module_func_args = [] |
| 33 | + |
| 34 | + for ( |
| 35 | + q_dtype, |
| 36 | + kv_dtype, |
| 37 | + head_dim, |
| 38 | + pos_encoding_mode, |
| 39 | + use_sliding_window, |
| 40 | + use_logits_soft_cap, |
| 41 | + ) in itertools.product( |
| 42 | + q_dtypes, |
| 43 | + kv_dtypes, |
| 44 | + head_dims, |
| 45 | + pos_encoding_modes, |
| 46 | + use_sliding_window_options, |
| 47 | + use_logits_soft_cap_options, |
| 48 | + ): |
| 49 | + load_module_func_args.append( |
| 50 | + ( |
| 51 | + flashinfer.decode.get_single_decode_module, |
| 52 | + ( |
| 53 | + q_dtype, |
| 54 | + kv_dtype, |
| 55 | + q_dtype, |
| 56 | + head_dim, |
| 57 | + pos_encoding_mode, |
| 58 | + use_sliding_window, |
| 59 | + use_logits_soft_cap, |
| 60 | + ), |
| 61 | + ) |
| 62 | + ) |
| 63 | + load_module_func_args.append( |
| 64 | + ( |
| 65 | + flashinfer.decode.get_batch_decode_module, |
| 66 | + ( |
| 67 | + q_dtype, |
| 68 | + kv_dtype, |
| 69 | + q_dtype, |
| 70 | + torch.int32, |
| 71 | + head_dim, |
| 72 | + pos_encoding_mode, |
| 73 | + use_sliding_window, |
| 74 | + use_logits_soft_cap, |
| 75 | + ), |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + return load_module_func_args |
| 80 | + |
| 81 | + |
| 82 | +def jit_prefill_attention_func_args( |
| 83 | + q_dtypes, |
| 84 | + kv_dtypes, |
| 85 | + head_dims, |
| 86 | + pos_encoding_modes, |
| 87 | + use_sliding_window_options, |
| 88 | + use_logits_soft_cap_options, |
| 89 | + allow_fp16_qk_reduction_options, |
| 90 | +): |
| 91 | + load_module_func_args = [] |
| 92 | + |
| 93 | + for ( |
| 94 | + q_dtype, |
| 95 | + kv_dtype, |
| 96 | + head_dim, |
| 97 | + pos_encoding_mode, |
| 98 | + use_sliding_window, |
| 99 | + use_logits_soft_cap, |
| 100 | + allow_fp16_qk_reduction, |
| 101 | + ) in itertools.product( |
| 102 | + q_dtypes, |
| 103 | + kv_dtypes, |
| 104 | + head_dims, |
| 105 | + pos_encoding_modes, |
| 106 | + use_sliding_window_options, |
| 107 | + use_logits_soft_cap_options, |
| 108 | + allow_fp16_qk_reduction_options, |
| 109 | + ): |
| 110 | + load_module_func_args.append( |
| 111 | + ( |
| 112 | + flashinfer.prefill.gen_single_prefill_module, |
| 113 | + ( |
| 114 | + q_dtype, |
| 115 | + kv_dtype, |
| 116 | + q_dtype, |
| 117 | + head_dim, |
| 118 | + pos_encoding_mode, |
| 119 | + use_sliding_window, |
| 120 | + use_logits_soft_cap, |
| 121 | + allow_fp16_qk_reduction, |
| 122 | + ), |
| 123 | + ) |
| 124 | + ) |
| 125 | + load_module_func_args.append( |
| 126 | + ( |
| 127 | + flashinfer.prefill.gen_batch_prefill_module, |
| 128 | + ( |
| 129 | + q_dtype, |
| 130 | + kv_dtype, |
| 131 | + q_dtype, |
| 132 | + torch.int32, |
| 133 | + head_dim, |
| 134 | + pos_encoding_mode, |
| 135 | + use_sliding_window, |
| 136 | + use_logits_soft_cap, |
| 137 | + allow_fp16_qk_reduction, |
| 138 | + ), |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + load_module_func_args.append( |
| 143 | + ( |
| 144 | + flashinfer.quantization.get_quantization_module, |
| 145 | + [], |
| 146 | + ) # required for attention with custom mask |
| 147 | + ) |
| 148 | + |
| 149 | + return load_module_func_args |
0 commit comments