forked from tambien/deeplearnjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulmat_gpu_benchmark.ts
95 lines (77 loc) · 3.74 KB
/
mulmat_gpu_benchmark.ts
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
/* Copyright 2017 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
import {MatrixOrientation} from '../../src/math/math';
import {Array2D} from '../../src/math/ndarray';
import {GPGPUContext} from '../../src/math/webgl/gpgpu_context';
import {MatMulProgram} from '../../src/math/webgl/mulmat_gpu';
import * as gpgpu_math from '../../src/math/webgl/gpgpu_math';
import * as mulmat_packed_gpu from '../../src/math/webgl/mulmat_packed_gpu';
import * as test_util from '../../src/test_util';
import {BenchmarkTest} from './benchmark';
const OP_RUNS = 40;
export const BENCHMARK_TEST: BenchmarkTest = (size: number) => {
const gpgpu = new GPGPUContext();
const aTexture = gpgpu.createMatrixTexture(size, size);
const bTexture = gpgpu.createMatrixTexture(size, size);
const resultTexture = gpgpu.createMatrixTexture(size, size);
const aArr = new Array2D(
[size, size], {texture: aTexture, textureShapeRC: [size, size]});
const bArr = new Array2D(
[size, size], {texture: bTexture, textureShapeRC: [size, size]});
const resArr = new Array2D(
[size, size], {texture: resultTexture, textureShapeRC: [size, size]});
const program = new MatMulProgram(aArr.shape, bArr.shape);
const binary =
gpgpu_math.compileProgram(gpgpu, program, [aArr, bArr], resArr);
const a = test_util.randomArrayInRange(size * size, -1, 1);
const b = test_util.randomArrayInRange(size * size, -1, 1);
gpgpu.uploadMatrixToTexture(aTexture, size, size, a);
gpgpu.uploadMatrixToTexture(bTexture, size, size, b);
const start = performance.now();
for (let i = 0; i < OP_RUNS; i++) {
gpgpu_math.runProgram(binary, [aArr, bArr], resArr);
}
gpgpu.downloadMatrixFromTexture(resultTexture, size, size);
const avgTime = (performance.now() - start) / OP_RUNS;
gpgpu.deleteMatrixTexture(aTexture);
gpgpu.deleteMatrixTexture(bTexture);
gpgpu.deleteMatrixTexture(resultTexture);
gpgpu.deleteProgram(binary.webGLProgram);
gpgpu.dispose();
return avgTime;
};
export const BENCHMARK_TEST_PACKED: BenchmarkTest = (size: number) => {
const gpgpu = new GPGPUContext();
const program: WebGLProgram =
gpgpu.createProgram(mulmat_packed_gpu.getFragmentShaderSource(
size, MatrixOrientation.REGULAR, MatrixOrientation.REGULAR));
const aTexture = gpgpu.createPackedMatrixTexture(size, size);
const bTexture = gpgpu.createPackedMatrixTexture(size, size);
const resultTexture = gpgpu.createPackedMatrixTexture(size, size);
const a = test_util.randomArrayInRange(size * size, -1, 1);
const b = test_util.randomArrayInRange(size * size, -1, 1);
gpgpu.uploadMatrixToPackedTexture(aTexture, size, size, a);
gpgpu.uploadMatrixToPackedTexture(bTexture, size, size, b);
const start = performance.now();
for (let i = 0; i < OP_RUNS; i++) {
mulmat_packed_gpu.multiplyMatrixPacked(
gpgpu, program, aTexture, bTexture, resultTexture, [size, size]);
}
gpgpu.downloadMatrixFromPackedTexture(resultTexture, size, size);
const avgTime = (performance.now() - start) / OP_RUNS;
gpgpu.deleteMatrixTexture(aTexture);
gpgpu.deleteMatrixTexture(bTexture);
gpgpu.deleteMatrixTexture(resultTexture);
gpgpu.deleteProgram(program);
gpgpu.dispose();
return avgTime;
};