-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmatrix.metal
212 lines (171 loc) · 6.29 KB
/
matrix.metal
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
//
// matrix.metal
// effort
//
// Created 26/01/2024.
//
#include <metal_stdlib>
using namespace metal;
kernel void silu(device const half *x1 [[buffer(0)]],
device const half *x3 [[buffer(1)]],
device half *out [[buffer(2)]],
uint id [[thread_position_in_grid]]) {
out[id] = float(x3[id]) * float(x1[id]) / (1 + exp(-float(x1[id])));
}
kernel void silu32(device const float *x1 [[buffer(0)]],
device const float *x3 [[buffer(1)]],
device float *out [[buffer(2)]],
uint id [[thread_position_in_grid]]) {
out[id] = x3[id] * x1[id] / (1 + exp(-x1[id]));
}
kernel void silu32b(device const float *x1 [[buffer(0)]],
device const float *x3 [[buffer(1)]],
device float *out [[buffer(2)]],
uint id [[thread_position_in_grid]]) {
uint begin = id*64;
uint end = id*64+64;
for (uint i=begin; i<end; i++) {
out[i] = x3[i] * x1[i] / (1 + exp(-x1[i]));
}
}
kernel void findCutoff(device const float *v [[buffer(0)]],
device const half *probes [[buffer(1)]],
device const uint *expNo [[buffer(2)]],
device half* out[[buffer(3)]],
constant uint &_effort [[buffer(4)]],
uint id [[thread_position_in_grid]],
uint tiisg [[thread_index_in_simdgroup]],
uint siitg [[simdgroup_index_in_threadgroup]],
uint tpg [[threads_per_grid]]) {
// const uint probesCount = 4096;
uint effort = 4096-_effort;
half myMax = -999;
half myMin = 999;
half4 myVal;
for (int i = 0; i<4; i++) {
myVal[i] = abs(v[4*id+i]*probes[4*id+i+expNo[0]*4096]);
myMax = max(myMax, myVal[i]);
myMin = min(myMin, myVal[i]);
}
//half myVal = abs(v[id]*probes[id+expNo[0]*4096]);
half sgMin = simd_min(myMin);
half sgMax = simd_max(myMax);
threadgroup half tgMin[32] = {999};
threadgroup half tgMax[32] = {-999};
threadgroup half minBound = 999;
threadgroup half maxBound = -999;
threadgroup half newBound = -999;
threadgroup short minCount = 0;
threadgroup short maxCount = 0;
if (tiisg == 0) {
tgMin[siitg] = sgMin;
tgMax[siitg] = sgMax;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
if (siitg == 0) {
sgMin = tgMin[tiisg];
sgMax = tgMax[tiisg];
minBound = simd_min(sgMin);
maxBound = simd_max(sgMax);
}
threadgroup short tgAbove[32] = {0};
threadgroup_barrier(mem_flags::mem_threadgroup);
newBound = (minBound + maxBound)/2;
ushort loops = 0;
minCount = 4096;
while (true) {
loops += 1;
ushort countAbove = 0;
ushort myAbove = 0;
threadgroup ushort globalCount = 0;
for (int i = 0; i<4; i++) {
myAbove += myVal[i] > newBound ? 1 : 0;
}
tgAbove[siitg] = simd_sum(myAbove);
threadgroup_barrier(mem_flags::mem_threadgroup);
if (siitg == 0) {
countAbove = tgAbove[tiisg];
countAbove = simd_sum(countAbove);
if (countAbove < effort) {
maxBound = newBound;
maxCount = countAbove;
} else {
minBound = newBound;
minCount = countAbove;
}
newBound = (maxBound+minBound)/2;
globalCount = countAbove;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
if ((globalCount == effort) ||
(maxBound - minBound < 0.00001) ||
(abs(maxCount - minCount) < 2)) {
if (id == 0){
out[0] = newBound;
}
return;
}
if (loops>100) {
return;
}
}
}
kernel void getVal(device const half* vector [[buffer(0)]],
device half *val [[buffer(1)]],
const device uint &pos [[buffer(2)]],
uint id [[thread_position_in_grid]]) {
if (id == pos) {
val[0] = vector[pos];
}
}
kernel void basicMul(device const float* v[[buffer(0)]],
device const half* m[[buffer(1)]],
device float* out[[buffer(2)]],
const device uint &numCols[[buffer(3)]],
uint rowId [[thread_position_in_grid]]) {
float sum = 0;
uint offset = rowId*numCols;
for (uint i=0; i<numCols; i++) {
sum += v[i]*m[i+offset];
}
out[rowId] = sum;
}
kernel void basicMul2(device const float* v[[buffer(0)]],
device const half* m[[buffer(1)]],
device atomic_float* out[[buffer(2)]],
const device uint &numCols[[buffer(3)]],
const device uint &chunkSize[[buffer(4)]],
uint2 rowId [[thread_position_in_grid]]) {
float sum = 0;
uint offset = rowId.x*numCols + rowId.y*chunkSize;
for (uint i=0; i<chunkSize; i++) {
sum += v[rowId.y*chunkSize+i]*m[i+offset];
}
atomic_fetch_add_explicit(out+rowId.x, sum, memory_order_relaxed);
// out[rowId.x] = sum;
}
/*
bucketMul
*/
kernel void basicBitonicSort(device half *floats [[ buffer(0) ]],
constant int &p [[ buffer(1) ]],
constant int &q [[ buffer(2) ]],
uint gid [[ thread_position_in_grid ]])
{
// taken from https://developer.apple.com/forums/thread/674181
// https://github.com/tgymnich/MetalSort
int pMinusQ = p-q;
int distance = 1 << pMinusQ;
uint gidShiftedByP = gid >> p;
// True: Increasing / False: Descreasing
bool direction = (gidShiftedByP & 2) == 0;
uint gidDistance = (gid & distance);
bool isGidDistanceZero = (gidDistance == 0);
uint gidPlusDistance = (gid | distance);
bool isLowerIndexGreaterThanHigher = (floats[gid] > floats[gidPlusDistance]);
if (isGidDistanceZero && isLowerIndexGreaterThanHigher == direction) {
float temp = floats[gid];
floats[gid] = floats[gidPlusDistance];
floats[gidPlusDistance] = temp;
}
}