Skip to content

Commit cb6c33b

Browse files
committed
chore(webgl): updata op
1 parent 13c6d04 commit cb6c33b

File tree

17 files changed

+488
-56
lines changed

17 files changed

+488
-56
lines changed

Diff for: packages/paddlejs-backend-webgl/src/ops/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ import {
6464
imgFeed, pack_out, nhwc_2_nchw, unpacked_2_packed,
6565
packed_2_unpacked, feedPost
6666
} from './shader/custom';
67+
import connect_mul from './shader/connect_mul';
68+
import instancenorm from './shader/instancenorm';
69+
import instancenorm_variance from './shader/instancenorm_variance';
70+
import instancenorm_mean from './shader/instancenorm_mean';
6771

6872

6973
const ops = {
@@ -140,7 +144,12 @@ const ops = {
140144
density_prior_box,
141145
prior_box,
142146
stack,
143-
slice
147+
slice,
148+
'conv2d-elementwise_add-leaky_relu': conv2d_elementwise_add,
149+
connect_mul,
150+
instance_norm: instancenorm,
151+
instance_norm_mean: instancenorm_mean,
152+
instance_norm_variance: instancenorm_variance
144153
};
145154
export {
146155
ops

Diff for: packages/paddlejs-backend-webgl/src/ops/shader/batchnorm.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ function mainFunc(
1414
float o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
1515
1616
// 归一化数据
17-
vec4 scale = getPixelsFromTexturePos_scale(vec2( float(oPos.g) / float(${scale.width_texture}) + 0.00001, 0.0));
18-
vec4 bias = getPixelsFromTexturePos_bias(vec2( float(oPos.g) / float(${bias.width_texture}) + 0.00001, 0.0));
19-
vec4 mean = getPixelsFromTexturePos_mean(vec2((float(oPos.g)) / float(${mean.width_texture}) + 0.00001, 0.0));
17+
vec4 scale = getPixelsFromTexturePos_scale(vec2(float(oPos.g) / float(${scale.width_texture}) + 0.00001, 0.0));
18+
vec4 bias = getPixelsFromTexturePos_bias(vec2(float(oPos.g) / float(${bias.width_texture}) + 0.00001, 0.0));
19+
vec4 mean = getPixelsFromTexturePos_mean(vec2(float(oPos.g) / float(${mean.width_texture}) + 0.00001, 0.0));
2020
vec4 variance = getPixelsFromTexturePos_variance(
21-
vec2((float(oPos.g)) / float(${variance.width_texture}) + 0.00001,
21+
vec2(float(oPos.g) / float(${variance.width_texture}) + 0.00001,
2222
0.0)
2323
);
2424
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
/**
3+
* @file concat
4+
*/
5+
6+
import { reduceShape } from '../../utils/dataProcess';
7+
8+
function mainFunc(
9+
{ origin, counter, out },
10+
{}
11+
) {
12+
const { total_shape, width_shape, height_shape, channel } = out;
13+
const reducedShape = reduceShape([
14+
total_shape / (width_shape * height_shape * channel),
15+
channel,
16+
height_shape,
17+
width_shape
18+
]);
19+
return `
20+
// start函数
21+
void main(void) {
22+
ivec4 oPos = getOutputTensorPos();
23+
float o = 0.0;
24+
ivec4 co;
25+
int sumVal = oPos.b * ${reducedShape[2]} + oPos.a;
26+
if (sumVal < ${origin.total_shape}) {
27+
// from origin
28+
co = getTensorPosFromArrayIndex_origin(sumVal);
29+
o = getValueFromTensorPos_origin(co.r, co.g, co.b, co.a);
30+
31+
}
32+
else if (sumVal > ${origin.total_shape} && sumVal < ${origin.total_shape + counter.total_shape}) {
33+
co = getTensorPosFromArrayIndex_counter(sumVal - ${origin.total_shape});
34+
o = getValueFromTensorPos_counter(co.r, co.g, co.b, co.a);
35+
}
36+
else {
37+
// from appender
38+
co = getTensorPosFromArrayIndex_appender(sumVal - ${origin.total_shape + counter.total_shape});
39+
o = getValueFromTensorPos_appender(co.r, co.g, co.b, co.a);
40+
}
41+
setOutput(float(o));
42+
}
43+
`;
44+
}
45+
export default {
46+
mainFunc,
47+
params: [],
48+
textureFuncConf: {
49+
origin: ['getValueFromTensorPos', 'getTensorPosFromArrayIndex'],
50+
counter: ['getValueFromTensorPos', 'getTensorPosFromArrayIndex'],
51+
appender: ['getValueFromTensorPos', 'getTensorPosFromArrayIndex']
52+
}
53+
};

Diff for: packages/paddlejs-backend-webgl/src/ops/shader/greater_than.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
* @file greater_than return x >= y
44
*/
55

6-
function mainFunc(
7-
{},
8-
{}
9-
) {
6+
function mainFunc() {
107
return `
118
// start函数
129
void main(void) {
1310
ivec4 oPos = getOutputTensorPos();
1411
// 输出坐标转换为输入坐标
15-
float x = getValueFromTensorPos_input(oPos.r, oPos.g, oPos.b, oPos.a);
12+
float x = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
1613
float y = getValueFromTensorPos_counter(oPos.r, oPos.g, oPos.b, oPos.a);
1714
18-
setOutput(bool(x >= y));
15+
setOutput(float(bool(x >= y)));
1916
}
2017
`;
2118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @file batchnorm
3+
*/
4+
5+
function mainFunc(
6+
{ bias, scale },
7+
{ }
8+
) {
9+
return `
10+
11+
// start函数
12+
void main(void) {
13+
// 输出数据
14+
ivec4 oPos = getOutputTensorPos();
15+
float o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
16+
17+
// 归一化数据
18+
vec4 scale = getPixelsFromTexturePos_scale(vec2(float(oPos.g) / float(${scale.width_texture}) + 0.00001, 0.0));
19+
vec4 bias = getPixelsFromTexturePos_bias(vec2(float(oPos.g) / float(${bias.width_texture}) + 0.00001, 0.0));
20+
float mean = getValueFromTensorPos_mean(0, 0, oPos.r, oPos.g);
21+
float variance = getValueFromTensorPos_variance(0, 0, oPos.r, oPos.g);
22+
23+
float res = (o - mean) * variance;
24+
// setOutput(res);
25+
26+
setOutput(res);
27+
}
28+
`;
29+
}
30+
export default {
31+
mainFunc,
32+
params: [
33+
'epsilon'
34+
],
35+
textureFuncConf: {
36+
origin: ['getValueFromTensorPos'],
37+
scale: ['getPixelsFromTexturePos'],
38+
bias: ['getPixelsFromTexturePos'],
39+
mean: ['getValueFromTensorPos'],
40+
variance: ['getValueFromTensorPos']
41+
}
42+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @file batchnorm
3+
*/
4+
5+
function mainFunc(
6+
{ origin },
7+
{}
8+
) {
9+
const { height_shape, width_shape } = origin;
10+
11+
return `
12+
13+
// start函数
14+
void main(void) {
15+
// 输出数据
16+
ivec4 oPos = getOutputTensorPos();
17+
float o = 0.0;
18+
for (int i = 0; i < ${height_shape}; i++) {
19+
float inner = 0.0;
20+
for (int j = 0; j < ${width_shape}; j++) {
21+
inner += getValueFromTensorPos_origin(oPos.b, oPos.a, i, j);
22+
}
23+
24+
o += (inner / float(${width_shape}));
25+
}
26+
27+
o = o / float(${height_shape});
28+
setOutput(o);
29+
}
30+
`;
31+
}
32+
export default {
33+
mainFunc,
34+
params: [
35+
],
36+
textureFuncConf: {
37+
origin: ['getValueFromTensorPos']
38+
}
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file batchnorm
3+
*/
4+
5+
function mainFunc(
6+
{ origin },
7+
{ epsilon }
8+
) {
9+
const { height_shape, width_shape } = origin;
10+
11+
return `
12+
13+
// start函数
14+
void main(void) {
15+
// 输出数据
16+
ivec4 oPos = getOutputTensorPos();
17+
18+
float variance = 0.0;
19+
float sum = 0.0;
20+
for (int i = 0; i < ${height_shape}; i++) {
21+
float inner = 0.0;
22+
for (int j = 0; j < ${width_shape}; j++) {
23+
float o = getValueFromTensorPos_origin(oPos.b, oPos.a, i, j);
24+
float m = getValueFromTensorPos_mean(oPos.r, oPos.g, oPos.b, oPos.a);
25+
float diff = o - m;
26+
inner += diff * diff;
27+
}
28+
29+
sum += inner / float(${width_shape});
30+
}
31+
variance = 1.0 / sqrt(sum / float(${height_shape}) + ${epsilon});
32+
33+
setOutput(variance);
34+
}
35+
`;
36+
}
37+
export default {
38+
mainFunc,
39+
params: [
40+
'epsilon'
41+
],
42+
textureFuncConf: {
43+
origin: ['getValueFromTensorPos'],
44+
mean: ['getValueFromTensorPos']
45+
}
46+
};

Diff for: packages/paddlejs-backend-webgl/src/ops/shader/reduce_mean.ts

+58-8
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,81 @@
33
* @file reduce_mean
44
*/
55

6+
function getGcd(a, b) {
7+
const max = Math.max(a, b);
8+
const min = Math.min(a, b);
9+
if (max % min === 0) {
10+
return min;
11+
}
12+
return getGcd(max % min, min);
13+
}
14+
15+
function getLcm(a, b) {
16+
return a * b / getGcd(a, b);
17+
}
618
function mainFunc(
7-
{},
8-
{ inputs_dim, dim }
19+
{ origin },
20+
{ dim }
921
) {
22+
const { total_shape, height_shape, width_shape, channel } = origin;
23+
const batch_shape = total_shape / (width_shape * height_shape * channel);
24+
const shape = [batch_shape, channel, height_shape, width_shape];
25+
let dimArr = [];
26+
if (dim instanceof Array) {
27+
dimArr = dim;
28+
}
29+
else {
30+
dimArr.push(dim);
31+
}
32+
const dimShape = dimArr.map(item => shape[item]);
33+
const totalDimShape = dimShape.reduce((prev, cur) => prev * cur);
34+
const arrGcd = dimShape.reduce((prev, cur) => getLcm(prev, cur));
35+
const remainV = totalDimShape / arrGcd;
36+
37+
let codeStr = 'float sum = 0.0;';
38+
const strArr = [`
39+
sum += getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a) / float(${arrGcd});
40+
`];
41+
for (let i = 0; i < dimArr.length; i++) {
42+
const curDim = dimArr[i];
43+
const curDimShape = shape[dimArr[i]];
44+
const vname = `i${i}`;
45+
strArr.unshift(`
46+
for (int ${vname} = 0; ${vname} < ${curDimShape}; ${vname}++) {
47+
oPos[${curDim}] = ${vname};
48+
`);
49+
strArr.push(
50+
`
51+
}
52+
`
53+
);
54+
}
55+
56+
codeStr += strArr.join('\n');
57+
codeStr += `
58+
o = sum / float(${remainV});
59+
`;
60+
1061
return `
1162
// start函数
1263
void main(void) {
1364
ivec4 oPos = getOutputTensorPos();
1465
// 输出坐标转换为输入坐标
1566
float o = 0.0;
16-
for (int i = 0; i < ${inputs_dim}; i++) {
17-
oPos[${dim}] = i;
18-
o += getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
19-
}
20-
o = o / float(${inputs_dim});
67+
${codeStr}
2168
setOutput(o);
2269
}
2370
`;
2471
}
2572
export default {
2673
mainFunc,
74+
params: [
75+
'dim'
76+
],
2777
textureFuncConf: {
2878
origin: ['getValueFromTensorPos']
2979
},
3080
behaviors: [
31-
'normalizeDim'
81+
3282
]
3383
};

0 commit comments

Comments
 (0)