TinyChatEngine
Loading...
Searching...
No Matches
common.h
1#ifndef COMMON_H
2#define COMMON_H
3#include <cstdint>
4#include <cstdlib>
5#include <fstream>
6#include <iostream>
7#include <stdexcept>
8
9#include "model.h"
10
11#define MAX_LINEAR_LENGTH 1024 * 1024 * 16 // 16MB, TO BE REMOVED with better memory allocation!
12#define DEBUG false
13
14#define DEBUG_INS(x) \
15 if (DEBUG) x
16
17#ifdef QM_CUDA
18#define QK 128
19#else
20#define QK 32
21#endif
22
24 uint8_t qx[QK / 2];
25 float scale;
26};
27
29 int8_t qx[QK];
30 float scale;
31};
32
33template <typename T>
34class Matrix3D {
35 public:
36 Matrix3D(T *data, int dim_x, int dim_y, int dim_z) : m_data(data), m_dim_x(dim_x), m_dim_y(dim_y), m_dim_z(dim_z) {}
37
38#if defined(__CUDACC__)
39 __host__ __device__ T &operator()(int x, int y, int z) { return m_data[x * m_dim_y * m_dim_z + y * m_dim_z + z]; }
40
41 __host__ __device__ const T &operator()(int x, int y, int z) const {
42 return m_data[x * m_dim_y * m_dim_z + y * m_dim_z + z];
43 }
44#else
45 T &operator()(int x, int y, int z) {
46 if (x < 0 || x >= m_dim_x || y < 0 || y >= m_dim_y || z < 0 || z >= m_dim_z) {
47 printf("%d, %d, %d\n", x, y, z);
48 printf("%d, %d, %d\n", m_dim_x, m_dim_y, m_dim_z);
49 throw std::out_of_range("Matrix3D: Indices out of range.");
50 }
51 return m_data[x * m_dim_y * m_dim_z + y * m_dim_z + z];
52 }
53
54 const T &operator()(int x, int y, int z) const {
55 if (x < 0 || x >= m_dim_x || y < 0 || y >= m_dim_y || z < 0 || z >= m_dim_z) {
56 printf("%d, %d, %d\n", x, y, z);
57 printf("%d, %d, %d\n", m_dim_x, m_dim_y, m_dim_z);
58 throw std::out_of_range("Matrix3D: Indices out of range.");
59 }
60 return m_data[x * m_dim_y * m_dim_z + y * m_dim_z + z];
61 }
62#endif
63
64 bool operator==(const Matrix3D<T> &other) const {
65 if (m_dim_x != other.m_dim_x || m_dim_y != other.m_dim_y || m_dim_z != other.m_dim_z) {
66 return false;
67 }
68
69 for (int x = 0; x < m_dim_x; ++x) {
70 for (int y = 0; y < m_dim_y; ++y) {
71 for (int z = 0; z < m_dim_z; ++z) {
72 if ((*this)(x, y, z) != other(x, y, z)) {
73 return false;
74 }
75 }
76 }
77 }
78
79 return true;
80 }
81
82#if defined(__CUDACC__)
83 __host__ __device__ int length() const { return m_dim_x * m_dim_y * m_dim_z; }
84#else
85 int length() const { return m_dim_x * m_dim_y * m_dim_z; }
86#endif
87
88 T sum() const {
89 T sum = 0;
90 for (int i = 0; i < this->length(); i++) {
91 sum += this->m_data[i];
92 }
93 return sum;
94 }
95 T sum(int size) const {
96 T sum = 0;
97 for (int i = 0; i < size; i++) {
98 sum += this->m_data[i];
99 }
100 return sum;
101 }
102
103 T sum(int size, int start_idx) const {
104 T sum = 0;
105 for (int i = 0; i < size; i++) {
106 sum += this->m_data[start_idx + i];
107 }
108 return sum;
109 }
110
111 void load(const char *path) {
112 std::ifstream infile(path, std::ios::binary | std::ios::in);
113 if (infile.fail()) {
114 std::cout << strerror(errno) << ": " << path << std::endl;
115 throw("Expected error...");
116 } else {
117 infile.read(reinterpret_cast<char *>(this->m_data), this->length() * sizeof(T));
118 infile.close();
119 }
120 }
121 T *m_data;
122 int m_dim_x, m_dim_y, m_dim_z;
123
124 // Default constructor
125 Matrix3D() { m_data = NULL; }
126};
127
128template <typename T>
129class Matrix4D {
130 public:
131 Matrix4D(T *data, int dim_w, int dim_x, int dim_y, int dim_z) :
132 m_data(data), m_dim_w(dim_w), m_dim_x(dim_x), m_dim_y(dim_y), m_dim_z(dim_z) {}
133
134#if defined(__CUDACC__)
135 __host__ __device__ T &operator()(int w, int x, int y, int z) {
136 return m_data[w * m_dim_x * m_dim_y * m_dim_z + x * m_dim_y * m_dim_z + y * m_dim_z + z];
137 }
138
139 __host__ __device__ const T &operator()(int w, int x, int y, int z) const {
140 return m_data[w * m_dim_x * m_dim_y * m_dim_z + x * m_dim_y * m_dim_z + y * m_dim_z + z];
141 }
142#else
143 T &operator()(int w, int x, int y, int z) {
144 if (w < 0 || w >= m_dim_w || x < 0 || x >= m_dim_x || y < 0 || y >= m_dim_y || z < 0 || z >= m_dim_z) {
145 printf("%d, %d, %d, %d\n", w, x, y, z);
146 printf("%d, %d, %d, %d\n", m_dim_w, m_dim_x, m_dim_y, m_dim_z);
147 throw std::out_of_range("Matrix4D: Indices out of range.");
148 }
149 return m_data[w * m_dim_x * m_dim_y * m_dim_z + x * m_dim_y * m_dim_z + y * m_dim_z + z];
150 }
151
152 const T &operator()(int w, int x, int y, int z) const {
153 if (w < 0 || w >= m_dim_w || x < 0 || x >= m_dim_x || y < 0 || y >= m_dim_y || z < 0 || z >= m_dim_z) {
154 printf("%d, %d, %d, %d\n", w, x, y, z);
155 printf("%d, %d, %d, %d\n", m_dim_w, m_dim_x, m_dim_y, m_dim_z);
156 throw std::out_of_range("Matrix4D: Indices out of range.");
157 }
158 return m_data[w * m_dim_x * m_dim_y * m_dim_z + x * m_dim_y * m_dim_z + y * m_dim_z + z];
159 }
160#endif
161
162 bool operator==(const Matrix4D<T> &other) const {
163 if (m_dim_w != other.m_dim_w || m_dim_x != other.m_dim_x || m_dim_y != other.m_dim_y || m_dim_z != other.m_dim_z) {
164 return false;
165 }
166
167 for (int w = 0; w < m_dim_w; ++w) {
168 for (int x = 0; x < m_dim_x; ++x) {
169 for (int y = 0; y < m_dim_y; ++y) {
170 for (int z = 0; z < m_dim_z; ++z) {
171 if ((*this)(w, x, y, z) != other(w, x, y, z)) {
172 return false;
173 }
174 }
175 }
176 }
177 }
178
179 return true;
180 }
181
182#if defined(__CUDACC__)
183 __host__ __device__ int length() const { return m_dim_w * m_dim_x * m_dim_y * m_dim_z; }
184#else
185 int length() const { return m_dim_w * m_dim_x * m_dim_y * m_dim_z; }
186#endif
187
188 T sum() const {
189 T sum = 0;
190 for (int i = 0; i < this->length(); i++) {
191 sum += this->m_data[i];
192 }
193 return sum;
194 }
195 T sum(int size) const {
196 T sum = 0;
197 for (int i = 0; i < size; i++) {
198 sum += this->m_data[i];
199 }
200 return sum;
201 }
202
203 T sum(int size, int start_idx) const {
204 T sum = 0;
205 for (int i = 0; i < size; i++) {
206 sum += this->m_data[start_idx + i];
207 }
208 return sum;
209 }
210
211 void load(const char *path) {
212 std::ifstream infile(path, std::ios::binary | std::ios::in);
213 if (infile.fail()) {
214 std::cout << strerror(errno) << ": " << path << std::endl;
215 throw("Expected error...");
216 } else {
217 infile.read(reinterpret_cast<char *>(this->m_data), this->length() * sizeof(T));
218 infile.close();
219 }
220 }
221 T *m_data;
222 int m_dim_w, m_dim_x, m_dim_y, m_dim_z;
223
224 // Default constructor
225 Matrix4D() { m_data = NULL; }
226};
227
228static inline void debug_info(std::string s) {
229#ifdef DEBUG
230 std::cout << s << std::endl;
231#endif
232}
233#endif
Definition common.h:34
Definition common.h:129
Definition common.h:23
Definition common.h:28