Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include "blockwise_quant_block_bnb4.h"
 
#include <vector>
 
#include "core/common/safeint.h"
#include "core/common/float16.h"
#include "core/platform/threadpool.h"
#include <iostream>
 
namespace onnxruntime {
namespace contrib {
 
template <typename T, int32_t block_size, int32_t DATA_TYPE>
void QuantizeBlockwiseBnb4(
    uint8_t* dst,  // shape: [(N * K + 1) / 2]
    const T* src,  // shape: [N, K]
    T* absmax,     // shape: [(N * K + block_size - 1) / block_size]
    int32_t N,
    int32_t K,
    onnxruntime::concurrency::ThreadPool* thread_pool) {
  int32_t numel = N * K;
  int32_t total_block_count = (numel + block_size - 1) / block_size;
 
  concurrency::ThreadPool::TryBatchParallelFor(
      thread_pool,
      total_block_count,
      [&](ptrdiff_t block_idx) {
        QuantizeBlockBnb4<T, block_size, DATA_TYPE>(
            src,
            dst,
            absmax[block_idx],
            static_cast<int32_t>(block_idx),
            numel);
      },
      0);
}
 
#define QuantizeBlockwiseBn4DataTyped(block_size, quant_type)                       \
  if (quant_type == FP4)                                                            \
    QuantizeBlockwiseBnb4<T, block_size, FP4>(dst, src, absmax, N, K, thread_pool); \
  else                                                                              \
    QuantizeBlockwiseBnb4<T, block_size, NF4>(dst, src, absmax, N, K, thread_pool);
 
template <typename T>
void QuantizeBlockwiseBnb4(
    uint8_t* dst,  // shape: [(N * K + 1) / 2]
    const T* src,  // shape: [N, K]
    T* absmax,     // shape: [(N * K + block_size - 1) / block_size]
    int32_t block_size,
    int32_t quant_type,
    int32_t N,
    int32_t K,
    onnxruntime::concurrency::ThreadPool* thread_pool) {
  ORT_ENFORCE(
      quant_type == FP4 || quant_type == NF4,
      "Invalid quant_type, only 0 (FP4) and 1 (NF4) are supported.");
 
  if (block_size == 16) {
    QuantizeBlockwiseBn4DataTyped(16, quant_type);
  } else if (block_size == 32) {
    QuantizeBlockwiseBn4DataTyped(32, quant_type);
  } else if (block_size == 64) {
    QuantizeBlockwiseBn4DataTyped(64, quant_type);
  } else if (block_size == 128) {
    QuantizeBlockwiseBn4DataTyped(128, quant_type);
  } else if (block_size == 256) {
    QuantizeBlockwiseBn4DataTyped(256, quant_type);
  } else {
    ORT_NOT_IMPLEMENTED("only block size 16, 32, 64, 128, 256 are supported.");
  }
}
 
#undef QuantizeBlockwiseBn4DataTyped
 
template <typename T, int32_t block_size, int32_t DATA_TYPE>
void DequantizeBlockwiseBnb4(
    T* dst,              // shape: [N, K]
    const uint8_t* src,  // shape: [(N * K + 1) / 2)]
    const T* absmax,     // shape: [(N * K + block_size - 1) / block_size]
    int32_t N,
    int32_t K,
    onnxruntime::concurrency::ThreadPool* thread_pool) {
  int32_t numel = N * K;
  int32_t total_block_count = (numel + block_size - 1) / block_size;
 
  concurrency::ThreadPool::TryBatchParallelFor(
      thread_pool,
      total_block_count,
      [&](ptrdiff_t block_idx) {
        DequantizeBlockBnb4<T, block_size, DATA_TYPE>(
            src,
            dst,
            absmax[block_idx],
            static_cast<int32_t>(block_idx),
            numel);
      },
      0);
}
 
#define DequantizeBlockwiseBn4DataTyped(block_size, quant_type)                       \
  if (quant_type == FP4)                                                              \
    DequantizeBlockwiseBnb4<T, block_size, FP4>(dst, src, absmax, N, K, thread_pool); \
  else                                                                                \
    DequantizeBlockwiseBnb4<T, block_size, NF4>(dst, src, absmax, N, K, thread_pool);
 
template <typename T>
void DequantizeBlockwiseBnb4(
    T* dst,              // shape: [N, K]
    const uint8_t* src,  // shape: [(N * K + 1) / 2)]
    const T* absmax,     // shape: [(N * K + block_size - 1) / block_size]
    int32_t block_size,
    int32_t quant_type,
    int32_t N,
    int32_t K,
    onnxruntime::concurrency::ThreadPool* thread_pool) {
  ORT_ENFORCE(
      quant_type == FP4 || quant_type == NF4,
      "Invalid quant_type, only 0 (FP4) and 1 (NF4) are supported.");
 
  if (block_size == 16) {
    DequantizeBlockwiseBn4DataTyped(16, quant_type);
  } else if (block_size == 32) {
    DequantizeBlockwiseBn4DataTyped(32, quant_type);
  } else if (block_size == 64) {
    DequantizeBlockwiseBn4DataTyped(64, quant_type);
  } else if (block_size == 128) {
    DequantizeBlockwiseBn4DataTyped(128, quant_type);
  } else if (block_size == 256) {
    DequantizeBlockwiseBn4DataTyped(256, quant_type);
  } else {
    ORT_NOT_IMPLEMENTED("only block size 16, 32, 64, 128, 256 are supported.");
  }
}
 
#undef DequantizeBlockwiseBn4DataTyped
 
}  // namespace contrib
}  // namespace onnxruntime