Skip to content

Commit ed2c56c

Browse files
Merge pull request #157 from JingyuanZhang/master
feat(webgl): add op shuffle_channel
2 parents 333df1c + eae27ba commit ed2c56c

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
/* eslint-disable */
21
/**
32
* @file get the position of output tensor
43
* @author yueshuangyan
54
*/
65

7-
import {moveTexture2PosToReal} from './common_func_with_texture';
6+
import { moveTexture2PosToReal } from './common_func_with_texture';
87

9-
export default function ({channel, height_shape, width_texture, height_texture}) {
8+
export default function ({ channel, height_shape, width_texture, height_texture }) {
109
return `
11-
${moveTexture2PosToReal('out', {width_texture, height_texture})}
10+
${moveTexture2PosToReal('out', { width_texture, height_texture })}
1211
ivec4 getOutputTensorPos() {
1312
// 获取原始长度
1413
vec2 outCoord = moveTexture2PosToReal_out(vCoord.xy);
@@ -19,6 +18,6 @@ export default function ({channel, height_shape, width_texture, height_texture})
1918
int b = int(outCoord.y / float(${height_shape}));
2019
return ivec4(b, c, y, x);
2120
}
22-
`
21+
`;
2322
};
2423

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import connect from './shader/connect';
4242
import squeeze2 from './shader/squeeze2';
4343
import pad3d from './shader/pad3d';
4444
import reduce_mean from './shader/reduce_mean';
45+
import shuffle_channel from './shader/shuffle_channel';
4546
import hard_swish from './shader/hard_swish';
4647
import nearest_interp from './shader/nearest_interp';
4748
import nearest_interp_v2 from './shader/nearest_interp_v2';
@@ -96,7 +97,8 @@ const ops = {
9697
sqrt: dynamic('sqrt'),
9798
squeeze2,
9899
pad3d,
99-
bilinear_interp_v2
100+
bilinear_interp_v2,
101+
shuffle_channel
100102
};
101103
export {
102104
ops
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @file shuffle_channel
3+
* @description reshape2 transpose2 reshape2
4+
*/
5+
6+
7+
function mainFunc(
8+
{
9+
out
10+
},
11+
{
12+
group = 2
13+
}
14+
) {
15+
const { total_shape, height_shape, width_shape, channel } = out;
16+
const channels_per_group = channel / group;
17+
18+
const [
19+
perm_0,
20+
perm_1,
21+
perm_2,
22+
perm_3
23+
] = [1, 0, 2, 3];
24+
25+
return `
26+
// start函数
27+
void main(void) {
28+
// 输出数据
29+
ivec4 oPos = getOutputTensorPos();
30+
float o = 0.0;
31+
32+
int sumVal = oPos.a
33+
+ oPos.b * ${width_shape}
34+
+ oPos.g * ${height_shape} * ${width_shape}
35+
+ oPos.r * ${channel} * ${width_shape} * ${height_shape};
36+
37+
ivec4 transpose_out_pos = transferFromNHWCtoNCHW(
38+
sumVal,
39+
${group},
40+
${width_shape},
41+
${height_shape},
42+
${total_shape}
43+
);
44+
45+
ivec4 transpose_in_pos = ivec4(transpose_out_pos[${perm_0}],
46+
transpose_out_pos[${perm_1}], transpose_out_pos[${perm_2}], transpose_out_pos[${perm_3}]);
47+
int sumVal2 = transpose_in_pos.a
48+
+ transpose_in_pos.b * ${width_shape}
49+
+ transpose_in_pos.g * ${height_shape} * ${width_shape}
50+
+ transpose_in_pos.r * ${channels_per_group} * ${width_shape} * ${height_shape};
51+
ivec4 origin_oPos = transferFromNHWCtoNCHW(
52+
sumVal2,
53+
${channel},
54+
${width_shape},
55+
${height_shape},
56+
${total_shape}
57+
);
58+
59+
60+
o = getValueFromTensorPos_origin(
61+
origin_oPos[0],
62+
origin_oPos[1],
63+
origin_oPos[2],
64+
origin_oPos[3]
65+
);
66+
67+
setOutput(float(o));
68+
}
69+
`;
70+
}
71+
72+
export default {
73+
mainFunc,
74+
params: [
75+
'group'
76+
],
77+
textureFuncConf: {
78+
origin: ['getValueFromTensorPos']
79+
},
80+
commonFuncConf: ['transferFromNHWCtoNCHW']
81+
};

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* @file squeeze2
43
*/

0 commit comments

Comments
 (0)