Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * @file q4_0_tensor.h
4 : * @date 22 July 2025
5 : * @brief This is Q4_0_Tensor class for Q4_0 quantized tensor.
6 : * @see https://github.com/nnstreamer/nntrainer
7 : * @author Eunju Yang <ej.yang@samsung.com>
8 : * @bug No known bugs except for NYI items
9 : */
10 :
11 : #ifndef __Q4_0_TENSOR_H__
12 : #define __Q4_0_TENSOR_H__
13 : #ifdef __cplusplus
14 :
15 : #include <quantizer.h>
16 : #include <tensor_base.h>
17 :
18 : namespace nntrainer {
19 :
20 : #define QK4_0 32
21 : /**
22 : * @brief Q4_0 Block
23 : * @note This is a structure for Q4_0 quantization.
24 : * This struct is not for use, only for reference.
25 : */
26 : struct block_q4_0 {
27 : uint16_t d; // delta
28 : uint8_t qs[QK4_0 / 2]; // nibbles / quants
29 : };
30 :
31 : #define Q4_0_SIZE sizeof(struct block_q4_0)
32 :
33 : /**
34 : * @class Q4_0_Tensor class
35 : * @brief Q4_0_Tensor class for Q4_0 quantized tensor
36 : */
37 0 : class Q4_0_Tensor : public TensorBase {
38 :
39 : public:
40 : /**
41 : * @brief Basic Constructor of Tensor
42 : */
43 : Q4_0_Tensor(std::string name_ = "", Tformat fm = Tformat::NCHW);
44 :
45 : /**
46 : * @brief Construct a new Q4_0_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 : Q4_0_Tensor(const TensorDim &d, bool alloc_now,
54 : Initializer init = Initializer::NONE, std::string name = "");
55 :
56 : /**
57 : * @brief Construct a new Q4_0_Tensor object
58 : *
59 : * @param d Tensor dim for this tensor
60 : * @param buf buffer
61 : */
62 : Q4_0_Tensor(const TensorDim &d, const void *buf = nullptr);
63 :
64 : /**
65 : * @brief Construct a new Q4_0_Tensor object
66 : * @param rhs TensorBase object to copy
67 : */
68 0 : Q4_0_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 : "Q4_0_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("Q4_0_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("Q4_0_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("Q4_0_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("Q4_0_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("Q4_0_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("Q4_0_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("Q4_0_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("Q4_0_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("Q4_0_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 : "Q4_0_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("Q4_0_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("Q4_0_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("Q4_0_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_q40(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 "Q4_0"; }
228 :
229 : /**
230 : * @copydoc Tensor::isValid()
231 : */
232 0 : bool isValid() const override { return true; }
233 : };
234 :
235 : } // namespace nntrainer
236 :
237 : #endif /* __cplusplus */
238 : #endif /* __Q4_0_TENSOR_H__ */
|