Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * @file q6_k_tensor.h
4 : * @date 20 May 2025
5 : * @brief This is Q6_K_Tensor class for Q6_K quantized tensor.
6 : * @see https://github.com/nnstreamer/nntrainer
7 : * @author Donghyeon Jeong <dhyeon.jeong@samsung.com>
8 : * @bug No known bugs except for NYI items
9 : */
10 :
11 : #ifndef __Q6_K_TENSOR_H__
12 : #define __Q6_K_TENSOR_H__
13 : #ifdef __cplusplus
14 :
15 : #include <quantizer.h>
16 : #include <tensor_base.h>
17 :
18 : namespace nntrainer {
19 :
20 : /**
21 : * @brief Q6_K Block
22 : * @note This is a structure for Q6_K quantization.
23 : * This struct is not for use, only for reference.
24 : */
25 : struct block_q6_K {
26 : uint8_t ql[128]; // quants, lower 4 bits
27 : uint8_t qh[64]; // quants, upper 2 bits
28 : int8_t scales[16]; // scales, quantized with 8 bits
29 : uint16_t d; // super-block scale
30 : };
31 :
32 : #define Q6_K_SIZE 210 // sizeof(block_q6_K)
33 :
34 : /**
35 : * @class Q6_K_Tensor class
36 : * @brief Q6_K_Tensor class for Q4_K quantized tensor
37 : */
38 0 : class Q6_K_Tensor : public TensorBase {
39 : public:
40 : /**
41 : * @brief Basic Constructor of Tensor
42 : */
43 : Q6_K_Tensor(std::string name_ = "", Tformat fm = Tformat::NCHW);
44 :
45 : /**
46 : * @brief Construct a new Q6_K_Tensor object
47 : *
48 : * @param d Tensor dim for this q4_k tensor
49 : * @param alloc_now Allocate memory to this tensor or not
50 : * @param init Initializer for the tensor
51 : * @param name Name of the tensor
52 : */
53 : Q6_K_Tensor(const TensorDim &d, bool alloc_now,
54 : Initializer init = Initializer::NONE, std::string name = "");
55 :
56 : /**
57 : * @brief Construct a new Q6_K_Tensor object
58 : *
59 : * @param d Tensor dim for this tensor
60 : * @param buf buffer
61 : */
62 : Q6_K_Tensor(const TensorDim &d, const void *buf = nullptr);
63 :
64 : /**
65 : * @brief Construct a new Q6_K_Tensor object
66 : * @param rhs TensorBase object to copy
67 : */
68 0 : Q6_K_Tensor(TensorBase &rhs) : TensorBase(rhs) {}
69 :
70 : /**
71 : * @copydoc Tensor::allocate()
72 : */
73 : void allocate() override;
74 :
75 : /**
76 : * @copydoc Tensor::deallocate()
77 : */
78 0 : void deallocate() override {
79 : data = nullptr;
80 0 : offset = 0;
81 0 : }
82 :
83 : /**
84 : * @copydoc Tensor::getData()
85 : */
86 : void *getData() const override;
87 :
88 : /**
89 : * @copydoc Tensor::getData()
90 : */
91 0 : void *getData(size_t idx) const override {
92 : throw std::invalid_argument(
93 0 : "Q6_K_Tensor::getData() is not supported. Use getData() instead.");
94 : }
95 :
96 : /**
97 : * @copydoc Tensor::getAddress()
98 : */
99 0 : void *getAddress(unsigned int i) override {
100 0 : throw std::invalid_argument("Q6_K_Tensor::getAddress() is not supported.");
101 : }
102 :
103 : /**
104 : * @copydoc Tensor::getAddress()
105 : */
106 0 : const void *getAddress(unsigned int i) const override {
107 0 : throw std::invalid_argument("Q6_K_Tensor::getAddress() is not supported.");
108 : }
109 :
110 : /**
111 : * @copydoc Tensor::setValue()
112 : */
113 0 : void setValue(float value) override {
114 0 : throw std::invalid_argument("Q6_K_Tensor::setValue() is not supported.");
115 : }
116 :
117 : /**
118 : * @copydoc Tensor::setValue()
119 : */
120 0 : void setValue(unsigned int b, unsigned int c, unsigned int h, unsigned int w,
121 : float value) override {
122 0 : throw std::invalid_argument("Q6_K_Tensor::setValue() is not supported.");
123 : }
124 :
125 : /**
126 : * @copydoc Tensor::addValue()
127 : */
128 0 : void addValue(unsigned int b, unsigned int c, unsigned int h, unsigned int w,
129 : float value, float beta) override {
130 0 : throw std::invalid_argument("Q6_K_Tensor::addValue() is not supported.");
131 : }
132 :
133 : /**
134 : * @copydoc Tensor::setZero()
135 : */
136 : void setZero() override;
137 :
138 : /**
139 : * @copydoc Tensor::initialize()
140 : */
141 0 : void initialize(Initializer init) override {
142 0 : throw std::invalid_argument("Q6_K_Tensor::initialize() is not supported.");
143 : }
144 :
145 : /**
146 : * @copydoc Tensor::initialize()
147 : */
148 : void initialize() override;
149 :
150 : /**
151 : * @copydoc Tensor::print()
152 : */
153 0 : void print(std::ostream &out) const override {
154 0 : throw std::invalid_argument("Q6_K_Tensor::print() is not supported.");
155 : }
156 :
157 : /**
158 : * @copydoc Tensor::copy()
159 : */
160 0 : void copy(const Tensor &from) override {
161 0 : throw std::invalid_argument("Q6_K_Tensor::copy() is not supported.");
162 : }
163 : /**
164 : * @copydoc Tensor::copyData()
165 : */
166 0 : void copyData(const Tensor &from) override {
167 0 : throw std::invalid_argument("Q6_K_Tensor::copyData() is not supported.");
168 : }
169 :
170 : /**
171 : * @copydoc Tensor::copy_with_stride()
172 : */
173 0 : void copy_with_stride(const Tensor &input, Tensor &output) override {
174 : throw std::invalid_argument(
175 0 : "Q6_K_Tensor::copy_with_stride() is not supported.");
176 : }
177 :
178 : /**
179 : * @copydoc Tensor::max_abs()
180 : */
181 0 : float max_abs() const override {
182 0 : throw std::invalid_argument("Q6_K_Tensor::max_abs() is not supported.");
183 : }
184 :
185 : /**
186 : * @copydoc Tensor::maxValue()
187 : */
188 0 : float maxValue() const override {
189 0 : throw std::invalid_argument("Q6_K_Tensor::maxValue() is not supported.");
190 : }
191 :
192 : /**
193 : * @copydoc Tensor::minValue()
194 : */
195 0 : float minValue() const override {
196 0 : throw std::invalid_argument("Q6_K_Tensor::minValue() is not supported.");
197 : }
198 :
199 : /**
200 : * @copydoc TensorBase::size()
201 : */
202 : size_t size() const override;
203 :
204 : /**
205 : * @copydoc Tensor::getMemoryBytes()
206 : */
207 : size_t getMemoryBytes() const override;
208 :
209 : /**
210 : * @copydoc Tensor::q_scheme()
211 : */
212 : QScheme q_scheme() const override;
213 :
214 : private:
215 : /**
216 : * @brief copy a buffer to @a this, the caller has to ensure that @a this is
217 : * initialized otherwise undefined behavior
218 : *
219 : * @param buf buffer to copy from
220 : */
221 : void copy_q6k(const void *buf);
222 :
223 : /**
224 : * @brief Get the Data Type String object
225 : * @return std::string of tensor data type (Q6_K)
226 : */
227 0 : std::string getStringDataType() const override { return "Q6_K"; }
228 :
229 : /**
230 : * @copydoc Tensor::isValid()
231 : */
232 0 : bool isValid() const override { return true; }
233 :
234 : }; // class Q6_K_Tensor
235 :
236 : } // namespace nntrainer
237 :
238 : #endif /* __cplusplus */
239 : #endif /* __Q6_K_TENSOR_H__ */
|