Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "contrib_ops/webgpu/bert/kv_cache_block_quant_int8.h"
#include "contrib_ops/webgpu/bert/kv_cache_quantization.h"
#include "core/providers/webgpu/webgpu_supported_types.h"
 
using namespace onnxruntime::webgpu;
using namespace ::onnxruntime::common;
 
namespace onnxruntime {
namespace contrib {
namespace webgpu {
 
Status KvCacheBlockQuantInt8Program::GenerateShaderCode(ShaderHelper& shader) const {
  const auto& key = shader.AddInput("key", ShaderUsage::UseUniform | ShaderUsage::UseValueTypeAlias |
                                               ShaderUsage::UseElementTypeAlias | ShaderUsage::UseIndicesTypeAlias);
  const auto& value = shader.AddInput("value", ShaderUsage::UseUniform);
  const auto& present_key = shader.AddOutput("present_key", ShaderUsage::UseUniform);
  const auto& present_value = shader.AddOutput("present_value", ShaderUsage::UseUniform);
 
  if (use_seqlen_k_) {
    shader.AddInput("seqlen_k", ShaderUsage::None);
  }
  if (prepare_indirect_dispatch_) {
    shader.AddInput("total_sequence_length_input", ShaderUsage::None);
    shader.AddOutput("indirect_buffer", ShaderUsage::None);
  }
 
  const ShaderVariableHelper* past_key = nullptr;
  const ShaderVariableHelper* past_value = nullptr;
  if (has_past_) {
    past_key = &shader.AddInput("past_key", ShaderUsage::UseUniform);
    past_value = &shader.AddInput("past_value", ShaderUsage::UseUniform);
  }
 
  return WGSL_TEMPLATE_APPLY(shader, "bert/kv_cache_block_quant_int8.wgsl.template",
                             WGSL_TEMPLATE_PARAMETER(components, components_),
                             WGSL_TEMPLATE_PARAMETER(compressed_head_size_u32, compressed_head_size_u32_),
                             WGSL_TEMPLATE_PARAMETER(has_past, has_past_),
                             WGSL_TEMPLATE_PARAMETER(head_size, head_size_),
                             WGSL_TEMPLATE_PARAMETER(kv_BNSH, kv_BNSH_),
                             WGSL_TEMPLATE_PARAMETER(past_present_share_buffer, past_present_share_buffer_),
                             WGSL_TEMPLATE_PARAMETER(prepare_indirect_dispatch, prepare_indirect_dispatch_),
                             WGSL_TEMPLATE_PARAMETER(use_seqlen_k, use_seqlen_k_),
                             WGSL_TEMPLATE_VARIABLE(key, key),
                             WGSL_TEMPLATE_OPTIONAL_VARIABLE(past_key, past_key),
                             WGSL_TEMPLATE_OPTIONAL_VARIABLE(past_value, past_value),
                             WGSL_TEMPLATE_VARIABLE(present_key, present_key),
                             WGSL_TEMPLATE_VARIABLE(present_value, present_value),
                             WGSL_TEMPLATE_VARIABLE(value, value));
}
 
Status BlockQuantInt8CopyToKvCache(onnxruntime::webgpu::ComputeContext& context,
                                   const WebgpuAttentionParameters& parameters,
                                   const Tensor* K, const Tensor* past_key, Tensor* present_key,
                                   const Tensor* V, const Tensor* past_value, Tensor* present_value,
                                   uint32_t tile_size, const Tensor* seqlen_k, Tensor* indirect_buffer,
                                   uint32_t num_q_tiles, const Tensor* total_seqlen) {
  constexpr uint32_t bit_width = 8;
  const int head_size = parameters.head_size_;
  ORT_ENFORCE(head_size >= 4 && head_size % 4 == 0,
              "Q8 block KV cache quantization requires head_size to be divisible by 4, got ", head_size);
  ORT_ENFORCE(context.KvCacheQuantizationBits() == bit_width,
              "Q8 block quantization requires an 8-bit KV cache.");
 
  constexpr int components = 4;
  const int compressed_head_size_u32 = KvCacheQuantizedHeadSizeU32(head_size, bit_width);
  const bool has_past = !parameters.past_present_share_buffer_ &&
                        past_key != nullptr && past_value != nullptr && past_key->SizeInBytes() > 0;
  const int kv_num_heads = parameters.is_gqa_ ? parameters.kv_num_heads_ : parameters.num_heads_;
  const int copy_sequence_length =
      parameters.past_present_share_buffer_ ? parameters.kv_sequence_length_ : parameters.total_sequence_length_;
  const uint32_t num_slices_per_kv =
      static_cast<uint32_t>(parameters.batch_size_ * kv_num_heads * copy_sequence_length);
  const uint32_t total_workgroups = 2 * num_slices_per_kv;
  constexpr uint32_t workgroup_size = 64;
 
  const bool prepare_indirect_dispatch = indirect_buffer != nullptr;
  const bool use_seqlen_k = seqlen_k != nullptr;
  const bool kv_BNSH =
      parameters.qkv_format_ == Q_K_V_BSNH_BNSH_BNSH || parameters.qkv_format_ == Q_K_V_BNSH;
 
  KvCacheBlockQuantInt8Program program{has_past, kv_BNSH, parameters.past_present_share_buffer_,
                                       head_size, components, compressed_head_size_u32,
                                       prepare_indirect_dispatch, use_seqlen_k};
  if (kv_BNSH) {
    program.AddInputs({{K, ProgramTensorMetadataDependency::TypeAndRank, components},
                       {V, ProgramTensorMetadataDependency::TypeAndRank, components}});
  } else {
    ORT_RETURN_IF_ERROR(
        (parameters.qkv_format_ == Q_K_V_BSNH)
            ? Status::OK()
            : ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT,
                              "qkv format ", parameters.qkv_format_, " is not supported yet."));
    TensorShape reshaped_KV_shape{
        parameters.batch_size_, parameters.kv_sequence_length_, kv_num_heads, head_size / components};
    program.AddInputs({{K, ProgramTensorMetadataDependency::TypeAndRank, reshaped_KV_shape, components},
                       {V, ProgramTensorMetadataDependency::TypeAndRank, reshaped_KV_shape, components}});
  }
 
  if (use_seqlen_k) {
    program.AddInput({seqlen_k, ProgramTensorMetadataDependency::None});
  }
  if (prepare_indirect_dispatch) {
    program.AddInput({total_seqlen, ProgramTensorMetadataDependency::None});
  }
  if (has_past) {
    program.AddInputs({{past_key, ProgramTensorMetadataDependency::TypeAndRank},
                       {past_value, ProgramTensorMetadataDependency::TypeAndRank}});
  }
  program.AddOutputs({{present_key, ProgramTensorMetadataDependency::Rank},
                      {present_value, ProgramTensorMetadataDependency::Rank}});
  if (prepare_indirect_dispatch) {
    program.AddOutput({indirect_buffer, ProgramTensorMetadataDependency::None});
  }
 
  const uint32_t past_input_seq_length =
      has_past ? static_cast<uint32_t>(past_key->Shape()[2]) : 0u;
  const uint32_t present_seq_length = static_cast<uint32_t>(present_key->Shape()[2]);
 
  program.SetDispatchGroupSize(total_workgroups)
      .SetWorkgroupSize(workgroup_size)
      .CacheHint(has_past, parameters.qkv_format_, parameters.past_present_share_buffer_,
                 prepare_indirect_dispatch, use_seqlen_k, head_size, components,
                 compressed_head_size_u32)
      .AddUniformVariables({{static_cast<uint32_t>(parameters.batch_size_)},
                            {static_cast<uint32_t>(compressed_head_size_u32)},
                            {static_cast<uint32_t>(copy_sequence_length)},
                            {static_cast<uint32_t>(kv_num_heads)},
                            {static_cast<uint32_t>(parameters.kv_sequence_length_)},
                            {static_cast<uint32_t>(parameters.num_heads_)},
                            {num_q_tiles},
                            {num_slices_per_kv},
                            {past_input_seq_length},
                            {present_seq_length},
                            {tile_size},
                            {static_cast<uint32_t>(parameters.total_sequence_length_)}});
 
  return context.RunProgram(program);
}
 
Status KvCacheBlockQuantInt8FusedRotaryProgram::GenerateShaderCode(ShaderHelper& shader) const {
  const auto& packed_qkv = shader.AddInput("packed_qkv", ShaderUsage::UseUniform);
  const auto& cos_cache = shader.AddInput(
      "cos_cache", ShaderUsage::UseUniform | ShaderUsage::UseValueTypeAlias);
  const auto& sin_cache = shader.AddInput("sin_cache", ShaderUsage::UseUniform);
 
  if (use_seqlen_k_) {
    shader.AddInput("seqlen_k", ShaderUsage::None);
  }
  if (prepare_indirect_dispatch_) {
    shader.AddInput("total_sequence_length_input", ShaderUsage::None);
  }
 
  const auto& query = shader.AddOutput("query", ShaderUsage::UseUniform);
  const auto& present_key = shader.AddOutput("present_key", ShaderUsage::UseUniform);
  const auto& present_value = shader.AddOutput("present_value", ShaderUsage::UseUniform);
  if (prepare_indirect_dispatch_) {
    shader.AddOutput("indirect_buffer", ShaderUsage::None);
  }
 
  return WGSL_TEMPLATE_APPLY(shader, "bert/kv_cache_block_quant_int8_fused_rotary.wgsl.template",
                             WGSL_TEMPLATE_PARAMETER(compressed_head_size_u32, compressed_head_size_u32_),
                             WGSL_TEMPLATE_PARAMETER(half_rotary_dim, half_rotary_dim_),
                             WGSL_TEMPLATE_PARAMETER(head_size, head_size_),
                             WGSL_TEMPLATE_PARAMETER(multi_rotary_cache_concat_offset,
                                                     multi_rotary_cache_concat_offset_),
                             WGSL_TEMPLATE_PARAMETER(past_present_share_buffer,
                                                     past_present_share_buffer_),
                             WGSL_TEMPLATE_PARAMETER(prepare_indirect_dispatch,
                                                     prepare_indirect_dispatch_),
                             WGSL_TEMPLATE_PARAMETER(use_multi_rotary_cache_concat,
                                                     multi_rotary_cache_concat_offset_ > 0),
                             WGSL_TEMPLATE_PARAMETER(use_seqlen_k, use_seqlen_k_),
                             WGSL_TEMPLATE_VARIABLE(cos_cache, cos_cache),
                             WGSL_TEMPLATE_VARIABLE(packed_qkv, packed_qkv),
                             WGSL_TEMPLATE_VARIABLE(present_key, present_key),
                             WGSL_TEMPLATE_VARIABLE(present_value, present_value),
                             WGSL_TEMPLATE_VARIABLE(query, query),
                             WGSL_TEMPLATE_VARIABLE(sin_cache, sin_cache));
}
 
Status BlockQuantInt8ApplyRotaryAndCopyToKvCache(
    onnxruntime::webgpu::ComputeContext& context,
    const WebgpuAttentionParameters& parameters,
    const Tensor* packedQKV,
    const Tensor* seqlen_k,
    const Tensor* cos_cache,
    const Tensor* sin_cache,
    Tensor* query,
    Tensor* present_key,
    Tensor* present_value,
    Tensor* indirect_buffer,
    uint32_t tile_size,
    uint32_t num_q_tiles,
    const Tensor* total_seqlen) {
  constexpr uint32_t bit_width = 8;
  const int head_size = parameters.head_size_;
  ORT_ENFORCE(head_size >= 4 && head_size % 4 == 0,
              "Q8 block KV cache quantization requires head_size to be divisible by 4, got ", head_size);
  ORT_ENFORCE(context.KvCacheQuantizationBits() == bit_width,
              "Q8 block quantization requires an 8-bit KV cache.");
 
  const int compressed_head_size_u32 = KvCacheQuantizedHeadSizeU32(head_size, bit_width);
  const int kv_num_heads = parameters.is_gqa_ ? parameters.kv_num_heads_ : parameters.num_heads_;
  const int half_rotary_dim = static_cast<int>(cos_cache->Shape()[1]);
  const uint32_t num_kv_slices =
      static_cast<uint32_t>(parameters.batch_size_ * kv_num_heads * parameters.kv_sequence_length_);
  const uint32_t num_q_slices =
      static_cast<uint32_t>(parameters.batch_size_ * parameters.num_heads_ * parameters.kv_sequence_length_);
  const uint32_t total_workgroups = 2 * num_kv_slices + num_q_slices;
  constexpr uint32_t workgroup_size = 64;
 
  const bool prepare_indirect_dispatch = indirect_buffer != nullptr;
  const bool use_seqlen_k = seqlen_k != nullptr;
  const uint32_t multi_rotary_cache_concat_offset = context.MultiRotaryCacheConcatOffset();
 
  KvCacheBlockQuantInt8FusedRotaryProgram program{
      head_size, half_rotary_dim, compressed_head_size_u32,
      parameters.past_present_share_buffer_, prepare_indirect_dispatch, use_seqlen_k,
      multi_rotary_cache_concat_offset};
  program.AddInput({packedQKV, ProgramTensorMetadataDependency::TypeAndRank});
  program.AddInputs({
      {cos_cache, ProgramTensorMetadataDependency::TypeAndRank},
      {sin_cache, ProgramTensorMetadataDependency::Rank},
  });
  if (use_seqlen_k) {
    program.AddInput({seqlen_k, ProgramTensorMetadataDependency::None});
  }
  if (prepare_indirect_dispatch) {
    program.AddInput({total_seqlen, ProgramTensorMetadataDependency::None});
  }
  program.AddOutputs({{query, ProgramTensorMetadataDependency::None},
                      {present_key, ProgramTensorMetadataDependency::Rank},
                      {present_value, ProgramTensorMetadataDependency::Rank}});
  if (prepare_indirect_dispatch) {
    program.AddOutput({indirect_buffer, ProgramTensorMetadataDependency::None});
  }
 
  const uint32_t present_seq_length = static_cast<uint32_t>(present_key->Shape()[2]);
  program.SetDispatchGroupSize(total_workgroups)
      .SetWorkgroupSize(workgroup_size)
      .CacheHint(parameters.past_present_share_buffer_, prepare_indirect_dispatch,
                 use_seqlen_k, head_size, half_rotary_dim, compressed_head_size_u32,
                 multi_rotary_cache_concat_offset)
      .AddUniformVariables({{static_cast<uint32_t>(parameters.batch_size_)},
                            {static_cast<uint32_t>(compressed_head_size_u32)},
                            {static_cast<uint32_t>(parameters.hidden_size_)},
                            {static_cast<uint32_t>(parameters.kv_hidden_size_)},
                            {static_cast<uint32_t>(kv_num_heads)},
                            {static_cast<uint32_t>(parameters.kv_sequence_length_)},
                            {static_cast<uint32_t>(parameters.num_heads_)},
                            {num_kv_slices},
                            {num_q_slices},
                            {num_q_tiles},
                            {present_seq_length},
                            {tile_size},
                            {static_cast<uint32_t>(parameters.total_sequence_length_)}});
 
  return context.RunProgram(program);
}
 
}  // namespace webgpu
}  // namespace contrib
}  // namespace onnxruntime