Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include "contrib_ops/webgpu/bert/attention_common.h"
#include "core/providers/webgpu/compute_context.h"
#include "core/providers/webgpu/program.h"
#include "core/providers/webgpu/shader_helper.h"
 
namespace onnxruntime {
namespace contrib {
namespace webgpu {
 
using onnxruntime::webgpu::Program;
using onnxruntime::webgpu::ProgramUniformVariableDataType;
using onnxruntime::webgpu::ShaderHelper;
 
class KvCacheBlockQuantInt8Program final : public Program<KvCacheBlockQuantInt8Program> {
 public:
  KvCacheBlockQuantInt8Program(bool has_past, bool kv_BNSH, bool past_present_share_buffer,
                               int head_size, int components, int compressed_head_size_u32,
                               bool prepare_indirect_dispatch, bool use_seqlen_k)
      : Program{"KvCacheBlockQuantInt8Copy"},
        has_past_(has_past),
        kv_BNSH_(kv_BNSH),
        past_present_share_buffer_(past_present_share_buffer),
        head_size_(head_size),
        components_(components),
        compressed_head_size_u32_(compressed_head_size_u32),
        prepare_indirect_dispatch_(prepare_indirect_dispatch),
        use_seqlen_k_(use_seqlen_k) {}
 
  Status GenerateShaderCode(ShaderHelper& sh) const override;
 
  WEBGPU_PROGRAM_DEFINE_UNIFORM_VARIABLES({"batch_size", ProgramUniformVariableDataType::Uint32},
                                          {"compressed_head_size_u32", ProgramUniformVariableDataType::Uint32},
                                          {"copy_sequence_length", ProgramUniformVariableDataType::Uint32},
                                          {"kv_num_heads", ProgramUniformVariableDataType::Uint32},
                                          {"kv_sequence_length", ProgramUniformVariableDataType::Uint32},
                                          {"num_heads", ProgramUniformVariableDataType::Uint32},
                                          {"num_q_tiles", ProgramUniformVariableDataType::Uint32},
                                          {"num_slices_per_kv", ProgramUniformVariableDataType::Uint32},
                                          {"past_input_seq_length", ProgramUniformVariableDataType::Uint32},
                                          {"present_seq_length", ProgramUniformVariableDataType::Uint32},
                                          {"tile_size", ProgramUniformVariableDataType::Uint32},
                                          {"total_sequence_length", ProgramUniformVariableDataType::Uint32});
 
 private:
  bool has_past_;
  bool kv_BNSH_;
  bool past_present_share_buffer_;
  int head_size_;
  int components_;
  int compressed_head_size_u32_;
  bool prepare_indirect_dispatch_;
  bool use_seqlen_k_;
};
 
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);
 
class KvCacheBlockQuantInt8FusedRotaryProgram final
    : public Program<KvCacheBlockQuantInt8FusedRotaryProgram> {
 public:
  KvCacheBlockQuantInt8FusedRotaryProgram(int head_size, int half_rotary_dim,
                                          int compressed_head_size_u32,
                                          bool past_present_share_buffer,
                                          bool prepare_indirect_dispatch, bool use_seqlen_k,
                                          uint32_t multi_rotary_cache_concat_offset)
      : Program{"KvCacheBlockQuantInt8FusedRotary"},
        head_size_(head_size),
        half_rotary_dim_(half_rotary_dim),
        compressed_head_size_u32_(compressed_head_size_u32),
        past_present_share_buffer_(past_present_share_buffer),
        prepare_indirect_dispatch_(prepare_indirect_dispatch),
        use_seqlen_k_(use_seqlen_k),
        multi_rotary_cache_concat_offset_(multi_rotary_cache_concat_offset) {}
 
  Status GenerateShaderCode(ShaderHelper& sh) const override;
 
  WEBGPU_PROGRAM_DEFINE_UNIFORM_VARIABLES({"batch_size", ProgramUniformVariableDataType::Uint32},
                                          {"compressed_head_size_u32", ProgramUniformVariableDataType::Uint32},
                                          {"hidden_size", ProgramUniformVariableDataType::Uint32},
                                          {"kv_hidden_size", ProgramUniformVariableDataType::Uint32},
                                          {"kv_num_heads", ProgramUniformVariableDataType::Uint32},
                                          {"kv_sequence_length", ProgramUniformVariableDataType::Uint32},
                                          {"num_heads", ProgramUniformVariableDataType::Uint32},
                                          {"num_kv_slices", ProgramUniformVariableDataType::Uint32},
                                          {"num_q_slices", ProgramUniformVariableDataType::Uint32},
                                          {"num_q_tiles", ProgramUniformVariableDataType::Uint32},
                                          {"present_seq_length", ProgramUniformVariableDataType::Uint32},
                                          {"tile_size", ProgramUniformVariableDataType::Uint32},
                                          {"total_sequence_length", ProgramUniformVariableDataType::Uint32});
 
 private:
  int head_size_;
  int half_rotary_dim_;
  int compressed_head_size_u32_;
  bool past_present_share_buffer_;
  bool prepare_indirect_dispatch_;
  bool use_seqlen_k_;
  uint32_t multi_rotary_cache_concat_offset_;
};
 
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);
 
}  // namespace webgpu
}  // namespace contrib
}  // namespace onnxruntime