Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 4c5d979

Browse files
author
Tixxx
committed
fixed correctness for both depthToSpace and new packed matmul
migrating this to the new repo...
1 parent d2dc883 commit 4c5d979

7 files changed

Lines changed: 302 additions & 41 deletions

File tree

benchmark/super_resolution_model_zoo/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module.exports = function(config) {
5959
printMatches: false,
6060
// To enable pack, run 'PACK=1 npm run test'
6161
usePackedGlTexture: config.usePackedGlTexture==1 ? true : false,
62+
runIteration: config.runIteration ? config.runIteration : 10,
6263
profile: config.profile
6364
},
6465
browsers: ['ChromeTest', 'ChromeDebug', 'Edge', 'Safari'],

benchmark/super_resolution_model_zoo/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"build": "webpack --config ./webpack.conf.js --mode production",
88
"build-debug": "webpack --config ./webpack.conf.js --mode development",
9-
"test": "karma start --browsers ChromeTest --single-run --usePackedGlTexture=$PACK",
10-
"profile": "karma start --browsers ChromeTest --single-run --profile --usePackedGlTexture=$PACK",
9+
"test": "karma start --browsers ChromeTest --single-run --usePackedGlTexture=$PACK --runIteration=$RUNCOUNT",
10+
"profile": "karma start --browsers ChromeTest --single-run --profile --usePackedGlTexture=$PACK --runIteration=$RUNCOUNT",
1111
"test-debug": "karma start --browsers ChromeDebug",
1212
"test-edge": "karma start --browsers Edge --single-run",
1313
"test-safari": "karma start --browsers Safari --single-run"

benchmark/super_resolution_model_zoo/src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ async function runBenchmark(benchmarkData, backend, imageSize) {
9999
const imageLoader = new ImageLoader(imageSize, imageSize);
100100
const durations = [];
101101
for(const input of benchmarkData.inputs) {
102-
console.log(`Running ${input.name}`)
102+
console.log(`Running ${input.name} for ${runIteration} iterations.`)
103103
const imageData = await imageLoader.getImageData(input.url);
104-
for(let i = 0 ; i < 10; i++) {
104+
for(let i = 0 ; i < runIteration; i++) {
105105
const outputData = await impl.runModel(imageData.data);
106106
durations.push(impl.duration);
107107
}
@@ -125,6 +125,7 @@ class TensorFlowResnetBenchmark {
125125
this.imageSize = imageSize;
126126
tf.disposeVariables();
127127
tf.env().set('WEBGL_PACK', pack_texture);
128+
128129
console.log(`Pack mode enabled: ${tf.env().getBool('WEBGL_PACK')}`);
129130
if(backend) {
130131
console.log(`Setting the backend to ${backend}`);
@@ -261,6 +262,7 @@ const results = [];
261262
const browser = __karma__.config.browser[0];
262263
const profile = __karma__.config.profile;
263264
const pack_texture = __karma__.config.usePackedGlTexture;
265+
const runIteration = __karma__.config.runIteration;
264266

265267
console.log(`browser: ${browser}`)
266268
describe('super resolution Tests', ()=> {

lib/backends/webgl/ops/conv-pack.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {WebGLReshapePacked} from './reshape-packed';
1515
export class WebGLConvPacked extends Conv {
1616
protected artifacts: Artifact[];
1717
protected programInfo: ProgramInfo[];
18-
18+
protected outputShape: number[];
1919
run(inferenceHandler: WebGLInferenceHandler, inputs: Tensor[]): Tensor[] {
2020
const programManager = inferenceHandler.session.programManager;
2121
const xshape = inputs[0].dims.slice();
@@ -33,8 +33,8 @@ export class WebGLConvPacked extends Conv {
3333
`autpPad:${this.autoPad}, dilations:${this.dilations}, group:${this.group}, kernelShape:${
3434
this.kernelShape}, pads:${this.pads}, strides:${this.strides}`);
3535

36-
const outputShape = WebGLConv.calcOutputShape(xshape, kshape, this.dilations, this.pads, this.strides);
37-
const im2col = new WebGLIm2ColPacked(outputShape, kshape, this.dilations, this.pads, this.strides);
36+
this.outputShape = WebGLConv.calcOutputShape(xshape, kshape, this.dilations, this.pads, this.strides);
37+
const im2col = new WebGLIm2ColPacked(this.outputShape, kshape, this.dilations, this.pads, this.strides);
3838
const matmul = new WebGLMatMulPacked();
3939
const reshape = new WebGLReshapePacked();
4040
// shape for kernel reshape
@@ -76,11 +76,10 @@ export class WebGLConvPacked extends Conv {
7676
inferenceHandler.checkAndUpdateTextureForm(this.artifacts[2], runDataMatmul);
7777
programManager.run(this.artifacts[2], runDataMatmul);
7878
const matmulOutput = runDataMatmul.outputTextureData.tensor;
79-
8079
// reshape output
8180
const outputShapeTensor = new Tensor(
82-
[outputShape.length], 'int32', undefined, undefined,
83-
new Int32Array([outputShape[0], outputShape[1], outputShape[2], outputShape[3]]));
81+
[this.outputShape.length], 'int32', undefined, undefined,
82+
new Int32Array([this.outputShape[0], this.outputShape[1], this.outputShape[2], this.outputShape[3]]));
8483

8584
assert(this.artifacts.length > 2, () => 'expect at least 3 artifacts created');
8685
if (this.artifacts.length === 3) {

lib/backends/webgl/ops/matmul-pack.ts

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,62 @@
44
import {MatMul} from '../../../ops/matmul';
55
import {Tensor} from '../../../tensor';
66
import {BroadcastUtil} from '../../../util';
7+
import {getGlsl} from '../glsl-source';
78
import {WebGLInferenceHandler} from '../inference-handler';
89
import {ProgramInfo, RunData, WebGLOperator} from '../types';
10+
import {getCoordsDataType} from '../utils';
911

1012
export class WebGLMatMulPacked extends MatMul implements WebGLOperator {
1113
run(inferenceHandler: WebGLInferenceHandler, inputs: Tensor[]): Tensor[] {
1214
return inferenceHandler.run(this, inputs);
1315
}
1416
createProgramInfo(handler: WebGLInferenceHandler, inputs: Tensor[]): ProgramInfo {
1517
const hasBias = inputs.length > 2;
16-
const processBias = hasBias ? `value += vec4(getBias(a[0]*2).xx, getBias(a[0]*2).yy);` : ``;
18+
const processBias = hasBias ? 'result += getBiasAtOutCoords();' : '';
1719
const aShape = inputs[0].dims;
1820
const bShape = inputs[1].dims;
1921
const outputShape = BroadcastUtil.calcShape(aShape, bShape, true);
2022

2123
if (!outputShape) {
2224
throw new Error('Can\'t use matmul on the given tensors');
2325
}
24-
const rank = outputShape.length;
26+
27+
const sharedDim = aShape[aShape.length - 1];
28+
const sharedDimIndex = Math.ceil(sharedDim / 2);
29+
2530
const aRank = aShape.length;
2631
const bRank = bShape.length;
27-
const sharedDim = aShape[aShape.length - 1];
28-
// TODO:fix broadcasting
32+
33+
const glsl = getGlsl(handler.session.backend.glContext.version);
34+
const coordsDataType = getCoordsDataType(outputShape.length);
35+
const allGlChannels = ['x', 'y', 'z', 'w', 'u', 'v'];
36+
2937
const shaderSource = `
30-
vec4 process(int indices[${rank}]) {
31-
int a[${aRank}];
32-
int b[${bRank}];
33-
bcastMatmulIndices_A(indices, a);
34-
bcastMatmulIndices_B(indices, b);
38+
void main() {
39+
${coordsDataType} rc = getOutputCoords();
40+
41+
vec4 result = vec4(0);
42+
43+
for (int i = 0; i < ${sharedDimIndex}; i++) {
44+
vec4 a = getA(${getA(allGlChannels, aRank)});
45+
vec4 b = getB(${getB(allGlChannels, bRank)});
46+
47+
result += (a.rrbb * b.rgrg);
48+
result += (a.ggaa * b.baba);
49+
}
50+
51+
${processBias}
52+
53+
${glsl.output} = result;
54+
}`;
3555

36-
vec4 value;
37-
for (int k=0; k<((${sharedDim}+1)/2); ++k) {
38-
a[${aRank - 1}] = k;
39-
b[${bRank - 2}] = k;
40-
value += ${getA(aRank)}.rrbb * ${getB(bRank)}.rgrg;
41-
value += ${getA(aRank)}.ggaa * ${getB(bRank)}.baba;
42-
}
43-
${processBias}
44-
return value;
45-
}`;
4656
return {
4757
inputLayouts: inputs.map((t, i) => handler.getOrCreateTextureLayout(t, 4, true, inputs[i].dims, true)),
4858
outputLayout:
4959
handler.createTextureLayoutFromShape(outputShape, 4, outputShape, {isPacked: true, reverseWH: true}),
5060
samplers: hasBias ? ['A', 'B', 'Bias'] : ['A', 'B'],
5161
shaderSource,
62+
hasMain: true,
5263
expectPackedInputs: true,
5364
expectPackedOutputs: true,
5465
};
@@ -64,22 +75,22 @@ export class WebGLMatMulPacked extends MatMul implements WebGLOperator {
6475
}
6576
}
6677

67-
function getA(outputRank: number): string {
68-
let res = 'getA(';
69-
for (let i = 0; i < outputRank - 2; i++) {
70-
res += `a[${i}], `;
78+
function getA(allGlChannels: string[], rank: number): string {
79+
let res = '';
80+
for (let i = 0; i < rank - 2; i++) {
81+
res += `rc.${allGlChannels[i]}, `;
7182
}
72-
res += `a[${outputRank - 2}]*2, ` +
73-
'k*2)';
83+
res += `rc.${allGlChannels[rank - 2]}, ` +
84+
'i<<1';
7485
return res;
7586
}
7687

77-
function getB(outputRank: number): string {
78-
let res = 'getB(';
79-
for (let i = 0; i < outputRank - 2; i++) {
80-
res += `b[${i}], `;
88+
function getB(allGlChannels: string[], rank: number): string {
89+
let res = '';
90+
for (let i = 0; i < rank - 2; i++) {
91+
res += `rc.${allGlChannels[i]}, `;
8192
}
82-
res += 'k*2, ' +
83-
`b[${outputRank - 1}]*2)`;
93+
res += 'i<<1, ' +
94+
`rc.${allGlChannels[rank - 1]}`;
8495
return res;
8596
}

0 commit comments

Comments
 (0)