Skip to content

Commit 029674f

Browse files
committed
chore(webgl): update op
1 parent 13c6d04 commit 029674f

24 files changed

+570
-119
lines changed

packages/paddlejs-backend-webgl/src/ops/index.ts

Lines changed: 10 additions & 1 deletion
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

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

Lines changed: 4 additions & 4 deletions
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
Lines changed: 53 additions & 0 deletions
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+
};

packages/paddlejs-backend-webgl/src/ops/shader/elementwise_add.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
function mainFunc(
66
{},
77
{
8+
originPos,
89
counterPos,
910
Scale_y = 1.0,
1011
Scale_x = 1.0,
@@ -15,7 +16,7 @@ function mainFunc(
1516
void main(void) {
1617
// 输出数据
1718
ivec4 oPos = getOutputTensorPos();
18-
float o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
19+
float o = getValueFromTensorPos_origin(${originPos});
1920
2021
float c = getValueFromTensorPos_counter(${counterPos});
2122
float res = float(${Scale_out / Scale_y}) * c + float(${Scale_out / Scale_x}) * o;

packages/paddlejs-backend-webgl/src/ops/shader/elementwise_div.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,22 @@
33
*/
44

55
function mainFunc(
6-
{ counter },
6+
{ },
77
{
8+
originPos,
89
counterPos,
910
Scale_y = 1.0,
1011
Scale_x = 1.0,
1112
Scale_out = 1.0
1213
}
1314
) {
1415
return `
15-
ivec4 formatNCHW(int n, int c, int h, int w) {
16-
int newN = n;
17-
int newC = c;
18-
int newH = h;
19-
int newW = w;
20-
21-
if (n >= ${counter.height_texture / counter.height_shape}) {
22-
newN = int(${counter.height_texture / counter.height_shape});
23-
}
24-
if (c >= ${counter.channel}) {
25-
newC = int(${counter.channel - 1});
26-
}
27-
if (h >= ${counter.height_shape}) {
28-
newH = ${counter.height_shape - 1};
29-
}
30-
if (w >= ${counter.width_shape}) {
31-
newW = ${counter.width_shape - 1};
32-
}
33-
return ivec4(newN, newC, newH, newW);
34-
}
3516
3617
// start函数
3718
void main() {
3819
// 输出数据
39-
ivec4 oPos1 = getOutputTensorPos();
40-
float o = getValueFromTensorPos_origin(oPos1.r, oPos1.g, oPos1.b, oPos1.a);
41-
ivec4 oPos = formatNCHW(oPos1.r, oPos1.g, oPos1.b, oPos1.a);
20+
ivec4 oPos = getOutputTensorPos();
21+
float o = getValueFromTensorPos_origin(${originPos});
4222
4323
float c = getValueFromTensorPos_counter(${counterPos});
4424
float res = float(${Scale_out}) * (float(${1 / Scale_x}) * o / (float(${1 / Scale_y}) * c));

packages/paddlejs-backend-webgl/src/ops/shader/elementwise_mul.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,22 @@
33
*/
44

55
function mainFunc(
6-
{ counter },
6+
{ },
77
{
8+
originPos,
89
counterPos,
910
Scale_y = 1.0,
1011
Scale_x = 1.0,
1112
Scale_out = 1.0
1213
}
1314
) {
1415
return `
15-
ivec4 formatNCHW(int n, int c, int h, int w) {
16-
int newN = n;
17-
int newC = c;
18-
int newH = h;
19-
int newW = w;
20-
21-
if (n >= ${counter.height_texture / counter.height_shape}) {
22-
newN = int(${counter.height_texture / counter.height_shape});
23-
}
24-
if (c >= ${counter.channel}) {
25-
newC = int(${counter.channel - 1});
26-
}
27-
if (h >= ${counter.height_shape}) {
28-
newH = ${counter.height_shape - 1};
29-
}
30-
if (w >= ${counter.width_shape}) {
31-
newW = ${counter.width_shape - 1};
32-
}
33-
return ivec4(newN, newC, newH, newW);
34-
}
3516
3617
// start函数
3718
void main() {
3819
// 输出数据
39-
ivec4 oPos1 = getOutputTensorPos();
40-
float o = getValueFromTensorPos_origin(oPos1.r, oPos1.g, oPos1.b, oPos1.a);
41-
ivec4 oPos = formatNCHW(oPos1.r, oPos1.g, oPos1.b, oPos1.a);
20+
ivec4 oPos = getOutputTensorPos();
21+
float o = getValueFromTensorPos_origin(${originPos});
4222
4323
float c = getValueFromTensorPos_counter(${counterPos});
4424
float res = float(${Scale_out / Scale_x}) * o * float(${1 / Scale_y}) * c;

packages/paddlejs-backend-webgl/src/ops/shader/elementwise_pow.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
function mainFunc(
66
{},
77
{
8+
originPos,
89
counterPos,
910
Scale_y = 1.0,
1011
Scale_x = 1.0,
@@ -15,7 +16,7 @@ function mainFunc(
1516
void main(void) {
1617
// 输出数据
1718
ivec4 oPos = getOutputTensorPos();
18-
float o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
19+
float o = getValueFromTensorPos_origin(${originPos});
1920
2021
float c = getValueFromTensorPos_counter(${counterPos});
2122
float res = pow(float(${Scale_out / Scale_x}) * o, float(${Scale_out / Scale_y}) * c);

packages/paddlejs-backend-webgl/src/ops/shader/elementwise_sub.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
function mainFunc(
66
{},
77
{
8+
originPos,
89
counterPos,
910
Scale_y = 1.0,
1011
Scale_x = 1.0,
@@ -15,7 +16,7 @@ function mainFunc(
1516
void main(void) {
1617
// 输出数据
1718
ivec4 oPos = getOutputTensorPos();
18-
float o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
19+
float o = getValueFromTensorPos_origin(${originPos});
1920
2021
float c = getValueFromTensorPos_counter(${counterPos});
2122
float res = float(${Scale_out / Scale_x}) * o - float(${Scale_out / Scale_y}) * c;

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

Lines changed: 3 additions & 6 deletions
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
}
Lines changed: 42 additions & 0 deletions
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+
};

0 commit comments

Comments
 (0)