-
Notifications
You must be signed in to change notification settings - Fork 1
/
amx.amx_matrix.h
227 lines (202 loc) · 6.17 KB
/
amx.amx_matrix.h
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#pragma once
#include <bit>
#include <cstdint>
#include <cstdlib>
#include <intrin.h>
#include <iomanip> //setw
#include <ios> //hex
#include <iosfwd>
#include <iostream> //std::cout
#include <random>
#include <sstream>
#include <string>
#include <utility>
#include "amx.print.h"
#include "amx.tile.h"
#include "amx.tile_array.h"
#include "amx.tools.h"
#include "amx.types.h"
namespace amx {
template <typename STORAGE>
class AmxMatrix {
private:
TileArray data_;
public:
int n_cols_;
int n_rows_;
int n_cols_tile_; // number of tile columns in the data structure
int n_rows_tile_; // number of tile rows in the data structure
AmxMatrix() = delete;
AmxMatrix(int n_cols, int n_rows) :
data_(TileArray(calc_n_cols_tile<STORAGE>(n_cols), calc_n_rows_tile(n_rows))),
n_cols_tile_(calc_n_cols_tile<STORAGE>(n_cols)),
n_rows_tile_(calc_n_rows_tile(n_rows)),
n_cols_(n_cols),
n_rows_(n_rows)
{}
bool operator==(const AmxMatrix<STORAGE>& other) const
{
if ((this->n_cols_ != other.n_cols_) || (this->n_rows_ != other.n_rows_)) {
return false;
}
const STORAGE* ptr1 = this->data();
const STORAGE* ptr2 = other.data();
for (int i = 0; i < this->n_cols_ * this->n_rows_; ++i) {
if (ptr1[i] != ptr2[i]) {
return false;
}
}
return true;
}
[[nodiscard]] Tile<STORAGE> get_tile(int col, int row) const {
// const_cast will haunt you!
STORAGE* ptr = const_cast<STORAGE*>(this->data_.get_ptr<STORAGE>(col, row));
return Tile<STORAGE>(ptr);
}
[[nodiscard]] int get_offset(MatrixKey key) const {
return this->data_.get_offset(get_col(key), get_row(key));
}
template <typename T = STORAGE>
[[nodiscard]] T* data() {
return reinterpret_cast<T*>(this->data_.data());
}
template <typename T = STORAGE>
[[nodiscard]] const T* data() const {
return reinterpret_cast<const T*>(this->data_.data());
}
template <bool CHECK = true>
[[nodiscard]] STORAGE get(int col, int row) const
{
int tile_col, col_in_tile;
if constexpr (sizeof(STORAGE) == 1) {
tile_col = col >> 6;
col_in_tile = col & 0b11'1111;
}
else if constexpr (sizeof(STORAGE) == 2) {
tile_col = col >> 5;
col_in_tile = col & 0b1'1111;
}
else if constexpr (sizeof(STORAGE) == 4) {
tile_col = col >> 4;
col_in_tile = col & 0b1111;
}
else {
__debugbreak();
}
const int tile_row = row >> 4;
const int row_in_tile = row & 0b1111;
return this->get_tile(tile_col, tile_row).get<CHECK>(col_in_tile, row_in_tile);
}
template <bool CHECK = true>
void set(int col, int row, STORAGE value)
{
int tile_col, col_in_tile;
if constexpr (sizeof(STORAGE) == 1) {
tile_col = col >> 6;
col_in_tile = col & 0b11'1111;
}
else if constexpr (sizeof(STORAGE) == 2) {
tile_col = col >> 5;
col_in_tile = col & 0b1'1111;
}
else if constexpr (sizeof(STORAGE) == 4) {
tile_col = col >> 4;
col_in_tile = col & 0b1111;
}
else {
__debugbreak();
}
const int tile_row = row >> 4;
const int row_in_tile = row & 0b1111;
this->get_tile(tile_col, tile_row).set<CHECK>(col_in_tile, row_in_tile, value);
}
void clear() {
std::memset(this->data_.data(), 0, static_cast<size_t>(this->n_cols_tile_) * static_cast<size_t>(this->n_rows_tile_) * 1024);
}
[[nodiscard]] std::string pretty_print(bool colour = false, tools::PrintType pt = tools::PrintType::dec) const
{
std::stringstream ss;
{
ss << "(#columns=" << (this->n_cols_tile_ * 16) << ", #rows=" << (this->n_rows_tile_ * 16) << "); tiles: " << std::endl;
int counter = 0;
for (int row = 0; row < this->n_rows_tile_; ++row) {
for (int col = 0; col < this->n_cols_tile_; ++col) {
ss << std::dec << std::setw(2) << std::setfill(' ') << counter << " ";
counter++;
}
ss << std::endl;
}
ss << std::endl;
}
{
int counter = 0;
for (int row = 0; row < this->n_rows_tile_; ++row) {
for (int col = 0; col < this->n_cols_tile_; ++col) {
ss << "tile" << counter << ": (" << col << "," << row << ") ";
ss << this->get_tile(col, row).pretty_print(colour, pt);
counter++;
}
}
}
return ss.str();
}
};
inline [[nodiscard]] bool approx_equal(const AmxMatrix<FP32>& a, const AmxMatrix<FP32>& b, float precision_percentage)
{
if ((a.n_cols_ != b.n_cols_) || (a.n_rows_ != b.n_rows_)) {
return false;
}
const uint32_t* ptr_a = a.data<uint32_t>();
const uint32_t* ptr_b = b.data<uint32_t>();
for (int i = 0; i < a.n_cols_ * a.n_rows_; ++i) {
if (ptr_a[i] != ptr_b[i]) {
const FP32 av = std::bit_cast<FP32>(ptr_a[i]);
const FP32 bv = std::bit_cast<FP32>(ptr_b[i]);
const float sub = av - bv;
const float diff = std::abs(sub);
if (diff > precision_percentage) {
const float threshold = precision_percentage * std::max(std::abs(av), std::abs(bv));
if (diff > threshold) {
std::cout << "diff = " << diff << "; threshold = " << threshold << std::endl;
return false;
}
}
}
}
return true;
}
inline void fill_random(INOUT AmxMatrix<BF16>& m, float min_value = 0.f, float max_value = 1.f, int seed = 0) {
std::random_device rd;
std::mt19937 gen((seed == 0) ? rd() : seed);
std::uniform_real_distribution<> dist(min_value, max_value);
BF16* ptr = m.data();
for (int pos = 0; pos < m.n_cols_ * m.n_rows_; ++pos) {
ptr[pos] = float_to_bf16(static_cast<float>(dist(rd)));
}
}
inline void fill_random(INOUT AmxMatrix<Int8>& m, int min_value = 0, int max_value = 127, int seed = 0) {
std::random_device rd;
std::mt19937 gen((seed == 0) ? rd() : seed);
std::uniform_int_distribution<> dist(min_value, max_value);
Int8* ptr = m.data();
for (int pos = 0; pos < m.n_cols_ * m.n_rows_; ++pos) {
ptr[pos] = static_cast<Int8>(dist(rd));
}
}
template <typename T>
void subtract(INOUT AmxMatrix<T>& a, const AmxMatrix<T>& b) {
if constexpr (DEBUG) {
if ((a.n_cols_ != b.n_cols_) || (a.n_rows_ != b.n_rows_)) {
__debugbreak();
}
}
for (int col = 0; col < a.n_rows_; ++col) {
for (int row = 0; row < b.n_cols_; ++row) {
const T av = a.get<DEBUG>(col, row);
const T bv = b. get<DEBUG>(col, row);
const T sub = av - bv;
a.set<DEBUG>(col, row, sub);
}
}
}
}