Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * @file q4_k_tensor.h
4 : * @date 23 April 2025
5 : * @brief This is Q4_K_Tensor class for Q4_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 __Q4_K_TENSOR_H__
12 : #define __Q4_K_TENSOR_H__
13 : #ifdef __cplusplus
14 :
15 : #include <quantizer.h>
16 : #include <uint4_tensor.h>
17 :
18 : namespace nntrainer {
19 :
20 : /**
21 : * @brief Q4_K Block
22 : * @note This is a structure for Q4_K quantization.
23 : * This struct is not for use, only for reference.
24 : */
25 : struct block_q4_K {
26 : int16_t d[1]; // super-block scale for quantized scales
27 : int16_t dmin[1]; // super-block scale for quantized mins
28 : uint8_t scales[12]; // scales and mins, quantized with 6 bits
29 : uint8_t qs[128]; // 4--bit quants
30 : };
31 :
32 : #define Q4_K_SIZE 144 // sizeof(block_q4_K)
33 : #define Q4_Kx8_SIZE 1152 // Q4_K_SIZE * 8
34 :
35 : /**
36 : * @class Q4_K_Tensor class
37 : * @brief Q4_K_Tensor class for Q4_K quantized tensor
38 : */
39 0 : class Q4_K_Tensor : public Uint4QTensor {
40 : public:
41 : /**
42 : * @brief Basic Constructor of Tensor
43 : */
44 : Q4_K_Tensor(std::string name_ = "", Tformat fm = Tformat::NCHW,
45 : QScheme qscheme_ = QScheme::Q4_Kx8);
46 :
47 : /**
48 : * @brief Construct a new Q4_K_Tensor object
49 : *
50 : * @param d Tensor dim for this q4_k tensor
51 : * @param alloc_now Allocate memory to this tensor or not
52 : * @param init Initializer for the tensor
53 : * @param name Name of the tensor
54 : */
55 : Q4_K_Tensor(const TensorDim &d, bool alloc_now,
56 : Initializer init = Initializer::NONE, std::string name = "",
57 : QScheme qscheme_ = QScheme::Q4_Kx8);
58 :
59 : /**
60 : * @brief Construct a new Q4_K_Tensor object
61 : *
62 : * @param d Tensor dim for this tensor
63 : * @param buf buffer
64 : */
65 : Q4_K_Tensor(const TensorDim &d, const void *buf = nullptr,
66 : QScheme qscheme_ = QScheme::Q4_Kx8);
67 :
68 : /**
69 : * @brief Construct a new Q4_K_Tensor object
70 : * @param rhs TensorBase object to copy
71 : */
72 0 : Q4_K_Tensor(TensorBase &rhs) : Uint4QTensor(rhs, QScheme::Q4_Kx8) {}
73 :
74 : /**
75 : * @copydoc Tensor::allocate()
76 : */
77 : void allocate() override;
78 :
79 : /**
80 : * @copydoc TensorBase::size()
81 : */
82 : size_t size() const override;
83 :
84 : /**
85 : * @copydoc Tensor::getMemoryBytes()
86 : */
87 : size_t getMemoryBytes() const override;
88 :
89 : /**
90 : * @copydoc Tensor::scale_size()
91 : */
92 : size_t scale_size() const override;
93 :
94 : private:
95 : /**
96 : * @brief copy a buffer to @a this, the caller has to ensure that @a this is
97 : * initialized otherwise undefined behavior
98 : *
99 : * @param buf buffer to copy from
100 : */
101 : void copy_q4k(const void *buf);
102 :
103 : /**
104 : * @brief Get the Data Type String object
105 : * @return std::string of tensor data type (Q4_K)
106 : */
107 0 : std::string getStringDataType() const override { return "Q4_K"; }
108 :
109 : /**
110 : * @copydoc Tensor::isValid()
111 : */
112 0 : bool isValid() const override { return true; }
113 :
114 : }; // class Q4_K_Tensor
115 :
116 : } // namespace nntrainer
117 :
118 : #endif /* __cplusplus */
119 : #endif /* __Q4_K_TENSOR_H__ */
|