forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix-multiply-precision.js
More file actions
202 lines (172 loc) · 5.61 KB
/
matrix-multiply-precision.js
File metadata and controls
202 lines (172 loc) · 5.61 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
const { assert, skip, test, module: describe, only } = require('qunit');
const { GPU } = require('../../src');
describe('internal: matrix multiply precision');
function vanillaMatrixMultiply(a, b) {
const width = a.length;
const height = b.length;
const result = new Array(height);
for (let y = 0; y < height; y++) {
const row = new Float32Array(width);
for (let x = 0; x < width; x++) {
let sum = 0;
for (let i = 0; i < width; i++) {
sum += a[y][i] * b[i][x];
}
row[x] = sum;
}
result[y] = row;
}
return result;
}
function filledMatrix(width, height) {
const matrix = new Array(height);
for (let y = 0; y < height; y++) {
const row = matrix[y] = new Float32Array(width);
for (let x = 0; x < width; x++) {
row[x] = Math.random() * 10;
}
}
return matrix;
}
function test512x512Matrix(precision, mode) {
const width = 512;
const height = 512;
const a = filledMatrix(width, height);
const b = filledMatrix(width, height);
const gpu = new GPU({ mode });
const kernel = gpu.createKernel(function(a, b) {
let sum = 0;
for (let i = 0; i < this.constants.width; i++) {
sum += a[this.thread.y][i] * b[i][this.thread.x];
}
return sum;
}, {
output: [width, height],
precision,
constants: {
width
}
});
const cpuResult = vanillaMatrixMultiply(a, b, width, height);
const gpuResult = kernel(a, b);
let closeEnough = true;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const singleGPUResult = gpuResult[y][x];
const singleCPUResult = cpuResult[y][x];
if (Math.abs(singleGPUResult - singleCPUResult) > 1) {
closeEnough = false;
break;
}
}
}
assert.ok(closeEnough);
gpu.destroy();
}
test('512x512 unsigned precision auto', () => {
test512x512Matrix('unsigned');
});
test('512x512 unsigned precision gpu', () => {
test512x512Matrix('unsigned', 'gpu');
});
(GPU.isWebGLSupported ? test : skip)('512x512 unsigned precision webgl', () => {
test512x512Matrix('unsigned', 'webgl');
});
(GPU.isWebGL2Supported ? test : skip)('512x512 unsigned precision webgl2', () => {
test512x512Matrix('unsigned', 'webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('512x512 unsigned precision headlessgl', () => {
test512x512Matrix('unsigned', 'headlessgl');
});
test('512x512 unsigned precision cpu', () => {
test512x512Matrix('unsigned', 'cpu');
});
function test10x512Matrix(precision, mode) {
const width = 10;
const height = 512;
const a = filledMatrix(width, height);
const b = filledMatrix(width, height);
const gpu = new GPU({ mode });
const kernel = gpu.createKernel(function(a, b) {
let sum = 0;
for (let i = 0; i < this.constants.width; i++) {
sum += a[this.thread.y][i] * b[i][this.thread.x];
}
return sum;
}, {
output: [width, height],
precision,
constants: {
width
}
});
const cpuResult = vanillaMatrixMultiply(a, b, width, height);
const gpuResult = kernel(a, b);
let closeEnough = true;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const singleGPUResult = gpuResult[y][x];
const singleCPUResult = cpuResult[y][x];
if (Math.abs(singleGPUResult - singleCPUResult) > 1) {
closeEnough = false;
break;
}
}
}
assert.ok(closeEnough);
gpu.destroy();
}
test('10x512 unsigned precision auto', () => {
test10x512Matrix('unsigned');
});
test('10x512 unsigned precision gpu', () => {
test10x512Matrix('unsigned', 'gpu');
});
(GPU.isWebGLSupported ? test : skip)('10x512 unsigned precision webgl', () => {
test10x512Matrix('unsigned', 'webgl');
});
(GPU.isWebGL2Supported ? test : skip)('10x512 unsigned precision webgl2', () => {
test10x512Matrix('unsigned', 'webgl2');
});
(GPU.isHeadlessGLSupported ? test : skip)('10x512 unsigned precision headlessgl', () => {
test10x512Matrix('unsigned', 'headlessgl');
});
test('10x512 unsigned precision cpu', () => {
test10x512Matrix('unsigned', 'cpu');
});
(GPU.isSinglePrecisionSupported ? test : skip)('512x512 single precision auto', () => {
test512x512Matrix('single');
});
(GPU.isSinglePrecisionSupported ? test : skip)('512x512 single precision gpu', () => {
test512x512Matrix('single', 'gpu');
});
(GPU.isWebGLSupported && GPU.isSinglePrecisionSupported ? test : skip)('512x512 single precision webgl', () => {
test512x512Matrix('single', 'webgl');
});
(GPU.isWebGL2Supported && GPU.isSinglePrecisionSupported ? test : skip)('512x512 single precision webgl2', () => {
test512x512Matrix('single', 'webgl2');
});
(GPU.isHeadlessGLSupported && GPU.isSinglePrecisionSupported ? test : skip)('512x512 single precision headlessgl', () => {
test512x512Matrix('single', 'headlessgl');
});
test('512x512 single precision cpu', () => {
test512x512Matrix('single', 'cpu');
});
(GPU.isSinglePrecisionSupported ? test : skip)('10x512 single precision auto', () => {
test10x512Matrix('single');
});
(GPU.isSinglePrecisionSupported ? test : skip)('10x512 single precision gpu', () => {
test10x512Matrix('single', 'gpu');
});
(GPU.isWebGLSupported && GPU.isSinglePrecisionSupported ? test : skip)('10x512 single precision webgl', () => {
test10x512Matrix('single', 'webgl');
});
(GPU.isWebGL2Supported && GPU.isSinglePrecisionSupported ? test : skip)('10x512 single precision webgl2', () => {
test10x512Matrix('single', 'webgl2');
});
(GPU.isHeadlessGLSupported && GPU.isSinglePrecisionSupported ? test : skip)('10x512 single precision headlessgl', () => {
test10x512Matrix('single', 'headlessgl');
});
test('10x512 single precision cpu', () => {
test10x512Matrix('precision', 'cpu');
});