Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <string>
#include <utility>
 
#include "core/providers/webgpu/program.h"
#include "core/providers/webgpu/webgpu_kernel.h"
 
namespace onnxruntime {
namespace contrib {
namespace webgpu {
 
using namespace onnxruntime::webgpu;
using onnxruntime::webgpu::ComputeContext;
 
class GatherBlockQuantizedProgram final : public Program<GatherBlockQuantizedProgram> {
 public:
  GatherBlockQuantizedProgram(const bool is_signed, const bool is_uint8, size_t indices_rank, int gather_axis, int bits, bool has_zeropoint,
                              TensorShape x_shape, TensorShape output_shape, bool is_fp_quantized = false,
                              int32_t fp_elem_type = 0, std::string scale_broadcast_axes = {})
      : Program<GatherBlockQuantizedProgram>{"GatherBlockQuantized"},
        is_signed_{is_signed},
        is_uint8_{is_uint8},
        indices_rank_{indices_rank},
        gather_axis_{gather_axis},
        bits_{bits},
        has_zeropoint_{has_zeropoint},
        x_shape_{x_shape},
        output_shape_{output_shape},
        is_fp_quantized_{is_fp_quantized},
        fp_elem_type_{fp_elem_type},
        scale_broadcast_axes_{std::move(scale_broadcast_axes)} {}
 
  Status GenerateShaderCode(ShaderHelper& sh) const override;
 
  WEBGPU_PROGRAM_DEFINE_UNIFORM_VARIABLES({"output_size", ProgramUniformVariableDataType::Uint32},
                                          {"quantize_axis", ProgramUniformVariableDataType::Uint32},
                                          {"gather_axis", ProgramUniformVariableDataType::Uint32},
                                          {"block_size", ProgramUniformVariableDataType::Uint32},
                                          {"scale_qaxis_dim", ProgramUniformVariableDataType::Uint32},
                                          {"zp_packed_qaxis_dim", ProgramUniformVariableDataType::Uint32});
 
 private:
  bool is_signed_;
  bool is_uint8_;
  size_t indices_rank_;
  int gather_axis_;
  int bits_;
  bool has_zeropoint_;
  TensorShape x_shape_;
  TensorShape output_shape_;
  // When true, `data` holds FP8 or FP4 codes (rather than integer block-quantized codes) and
  // GenerateShaderCode emits a dequantization lookup table (indexed on the raw bit pattern)
  // instead of the (code - zero_point) integer formula. `fp_elem_type_` is the
  // ONNX_TENSOR_ELEMENT_DATA_TYPE_* value identifying which FP8/FP4 variant to build the table for.
  bool is_fp_quantized_;
  int32_t fp_elem_type_;
  // Entry `i` is '1' when axis `i` of `scales` is broadcast (dim == 1 while `data`'s dim is > 1);
  // only possible for FP8/FP4 data on axes other than quantize_axis.
  std::string scale_broadcast_axes_;
};
 
class GatherBlockQuantized final : public WebGpuKernel {
 public:
  GatherBlockQuantized(const OpKernelInfo& info) : WebGpuKernel(info) {
    gather_axis_ = static_cast<int>(info.GetAttrOrDefault<int64_t>("gather_axis", 0));
    block_size_ = static_cast<int>(info.GetAttrOrDefault<int64_t>("block_size", 128));
    quantize_axis_ = static_cast<int>(info.GetAttrOrDefault<int64_t>("quantize_axis", 1));
    bits_ = static_cast<int>(info.GetAttrOrDefault<int64_t>("bits", 4));
 
    // block_size == 0 is only valid for FP8/FP4 `data`, which is validated (against the actual
    // input element type) in ComputeInternal, since the element type isn't known here.
    ORT_ENFORCE(block_size_ == 0 || (block_size_ >= 16 && ((block_size_ - 1) & block_size_) == 0),
                "'block_size' must be 0, or 2's power and not less than 16.");
  }
  Status ComputeInternal(ComputeContext& context) const override;
 
 private:
  int gather_axis_;
  int quantize_axis_;
  int block_size_;
  int bits_;
};
 
}  // namespace webgpu
}  // namespace contrib
}  // namespace onnxruntime