-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommon.h
More file actions
233 lines (186 loc) · 6.48 KB
/
common.h
File metadata and controls
233 lines (186 loc) · 6.48 KB
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
228
229
230
231
232
233
#ifndef INFINI_OPS_CAMBRICON_COMMON_H_
#define INFINI_OPS_CAMBRICON_COMMON_H_
#include <cnnl.h>
#include <cnrt.h>
#include "data_type.h"
#include "device.h"
#define NRAM_MAX_SIZE (1024 * 240)
#ifdef __BANG__
namespace infini::ops::reduce {
constexpr int batch_size = 128 / sizeof(float);
__mlu_func__ void SumInternal(float* dst, float* src, int max_batch) {
const int width = max_batch / batch_size;
if (width >= 4) {
__bang_sumpool(dst, src, batch_size, 1, width, 1, width, 1, 1);
__bang_reduce_sum(dst, dst, batch_size);
} else {
float sum = 0.0f;
for (int i = 0; i < max_batch; ++i) {
sum += src[i];
}
dst[0] = sum;
}
}
template <typename T>
__mlu_func__ void SumTyped(float *result, T *data, size_t len) {
if constexpr (std::is_same_v<T, __half>) {
__bang_half2float((float *)data, reinterpret_cast<half *>(data) + len, len);
SumInternal(result, (float *)data, len);
} else if constexpr (std::is_same_v<T, __bang_bfloat16>) {
__bang_bfloat162float((float *)data, data + len, len);
SumInternal(result, (float *)data, len);
} else {
SumInternal(result, data, len);
}
}
template <typename T>
__mlu_func__ float Sum(const T *source, T *src, float *dst, int num_elements,
int max_batch) {
float res = 0.0f;
int offset = (sizeof(T) == 2 ? max_batch : 0);
size_t processed = 0;
while (processed < num_elements) {
size_t curr_batch = std::min<size_t>(max_batch, num_elements - processed);
if (curr_batch < max_batch) {
__bang_write_value(src, max_batch + offset, 0);
}
__memcpy(src + offset, source + processed, curr_batch * sizeof(T),
GDRAM2NRAM);
SumTyped(dst, src, max_batch);
res += dst[0];
processed += curr_batch;
}
return res;
}
template <typename T>
__mlu_func__ float SumBatched(const T *source, T *src, float *dst,
int num_elements, int max_batch) {
constexpr int min_vector_size = 32;
if (num_elements < min_vector_size) {
return Sum(source, src, dst, num_elements, max_batch);
}
float res = 0.0f;
int offset = (sizeof(T) == 2 ? max_batch : 0);
size_t processed = 0;
while (processed < num_elements) {
size_t curr_batch = std::min<size_t>(max_batch, num_elements - processed);
size_t aligned_batch = (curr_batch / batch_size) * batch_size;
size_t remainder = curr_batch % batch_size;
// Ensure NRAM buffer is zeroed.
__bang_write_value(src, max_batch + offset, 0);
// Copy data to NRAM.
__memcpy(src + offset, source + processed, curr_batch * sizeof(T),
GDRAM2NRAM);
if constexpr (std::is_same_v<T, __half>) {
__bang_half2float((float *)(src + offset),
reinterpret_cast<half *>(src) + offset, curr_batch);
} else if constexpr (std::is_same_v<T, __bang_bfloat16>) {
__bang_bfloat162float((float *)(src + offset), src + offset, curr_batch);
}
if (aligned_batch > 0) {
SumInternal(dst, (float *)(src + offset), aligned_batch);
res += dst[0];
}
if (remainder > 0) {
for (size_t i = aligned_batch; i < curr_batch; ++i) {
res += ((float *)(src + offset))[i];
}
}
processed += curr_batch;
}
return res;
}
__mlu_func__ void MaxInternal(float *dst, float *src, int max_batch) {
__bang_maxpool(dst, src, batch_size, 1, max_batch / batch_size, 1,
max_batch / batch_size, 1, 1);
__bang_argmax(dst, dst, batch_size);
}
template <typename T>
__mlu_func__ void MaxTyped(float *result, T *data, size_t len) {
if constexpr (std::is_same_v<T, __half>) {
__bang_half2float((float *)data, reinterpret_cast<half *>(data) + len, len);
MaxInternal(result, (float *)data, len);
} else if constexpr (std::is_same_v<T, __bang_bfloat16>) {
__bang_bfloat162float((float *)data, data + len, len);
MaxInternal(result, (float *)data, len);
} else {
MaxInternal(result, data, len);
}
}
template <typename T>
__mlu_func__ float Max(const T *source, T *src, float *dst, int num_elements,
int max_batch) {
float max_val = -INFINITY;
int offset = (sizeof(T) == 2 ? max_batch : 0);
size_t processed = 0;
while (processed < num_elements) {
size_t curr_batch = std::min<size_t>(max_batch, num_elements - processed);
if (curr_batch < max_batch) {
__bang_write_value(src, max_batch + offset, 0);
}
__memcpy(src + offset, source + processed, curr_batch * sizeof(T),
GDRAM2NRAM);
MaxTyped(dst, src, max_batch);
max_val = std::max(max_val, dst[0]);
processed += curr_batch;
}
return max_val;
}
template <typename T>
__mlu_func__ float MaxBatched(const T *source, T *src, float *dst,
int num_elements, int max_batch) {
constexpr int min_vector_size = 32;
if (num_elements < min_vector_size) {
return Max(source, src, dst, num_elements, max_batch);
}
float max_val = -INFINITY;
int offset = (sizeof(T) == 2 ? max_batch : 0);
size_t processed = 0;
while (processed < num_elements) {
size_t curr_batch = std::min<size_t>(max_batch, num_elements - processed);
if (curr_batch < max_batch) {
__bang_write_value(src, max_batch + offset, 0);
}
__memcpy(src + offset, source + processed, curr_batch * sizeof(T),
GDRAM2NRAM);
MaxTyped(dst, src, max_batch);
max_val = std::max(max_val, dst[0]);
processed += curr_batch;
}
return max_val;
}
} // namespace infini::ops::reduce
#endif // __BANG__
namespace infini::ops::cnnl_utils {
inline cnnlDataType_t GetDataType(DataType dtype) {
switch (dtype) {
case DataType::kInt8:
return CNNL_DTYPE_INT8;
case DataType::kUInt8:
return CNNL_DTYPE_UINT8;
case DataType::kInt32:
return CNNL_DTYPE_INT32;
case DataType::kInt64:
return CNNL_DTYPE_INT64;
case DataType::kFloat16:
return CNNL_DTYPE_HALF;
case DataType::kFloat32:
return CNNL_DTYPE_FLOAT;
case DataType::kBFloat16:
return CNNL_DTYPE_BFLOAT16;
case DataType::kFloat64:
return CNNL_DTYPE_DOUBLE;
default:
return CNNL_DTYPE_INVALID;
}
}
} // namespace infini::ops::cnnl_utils
namespace infini::ops::cnrt_utils {
inline void GetLaunchConfig(const Device& device, int* core_per_cluster,
int* cluster_count) {
int device_id = device.index();
cnrtDeviceGetAttribute(cluster_count, cnrtAttrClusterCount, device_id);
cnrtDeviceGetAttribute(core_per_cluster, cnrtAttrMcorePerCluster, device_id);
}
} // namespace infini::ops::cnrt_utils
#endif