Skip to content

Commit c0b8f6a

Browse files
committed
feat(webgl): add exp and stack op
1 parent c757296 commit c0b8f6a

8 files changed

Lines changed: 198 additions & 3 deletions

File tree

packages/paddlejs-backend-webgl/src/ops/atom/common_func.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,16 @@ float tanh_func(float x, float y, float z) {
6969
return tanh_calc(x);
7070
}`;
7171

72+
const exp = `
73+
float exp(float x, float y, float z) {
74+
float result = exp(x);
75+
return result;
76+
}`;
77+
7278
export {
7379
prelu,
7480
relu6,
81+
exp,
7582
leakyRelu,
7683
scale,
7784
sigmoid,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file concat dynamic inputs
3-
* @description concat inputs X supports no more than 4 tensors, eg. [a1, a2, a3, a4]
3+
* @description concat inputs X supports no more than 15 tensors, eg. [a1, a2, a3, a4, ... , a15]
44
*/
55

66
/* eslint-disable max-lines */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file concat_mul dynamic inputs
3-
* @description concat inputs X supports no more than 4 tensors, eg. [a1, a2, a3, a4]
3+
* @description concat inputs X supports no more than 15 tensors, eg. [a1, a2, a3, a4, ... , a15]
44
*/
55

66
/* eslint-disable max-lines */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const commonFuncBehaviors = {
1111
sigmoid: ['transToSigmoid'],
1212
hard_sigmoid: ['transToHardSigmoid'],
1313
pow: ['transToPow'],
14+
exp: ['transToExp'],
1415
sqrt: ['transToSqrt'],
1516
tanh: ['transToTanh']
1617
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @file stack dynamic inputs
3+
* @description stack inputs X supports no more than 15 tensors, eg. [a1, a2, a3, a4, ... , a15]
4+
* @detail https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/operators/stack_op.h#L56
5+
* @detail https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/stack_cn.html#stack
6+
*/
7+
8+
/* eslint-disable max-lines */
9+
function mainFunc(
10+
{ out, ...inputs },
11+
attrs
12+
) {
13+
const origin_tensor = inputs['origin'];
14+
const {
15+
width_shape,
16+
height_shape,
17+
channel,
18+
total_shape,
19+
length_unformatted_shape
20+
} = origin_tensor;
21+
const batch = total_shape / (width_shape * height_shape * channel);
22+
23+
const tensor_shape = [batch, channel, height_shape, width_shape];
24+
const origin_shape = tensor_shape.slice(length_unformatted_shape);
25+
26+
const inputs_num = Object.keys(inputs).length;
27+
28+
const axis = attrs.axis < 0 ? attrs.axis + origin_shape.length + 1 : attrs.axis;
29+
30+
let pre = 1;
31+
let post = 1;
32+
for (let index = 0; index < axis; index++) {
33+
pre *= origin_shape[index];
34+
}
35+
for (let index = axis; index < origin_shape.length; index++) {
36+
post *= origin_shape[index];
37+
}
38+
39+
const out_total_shape = out.total_shape;
40+
41+
const pre_every_num = out_total_shape / pre;
42+
43+
let getMultiInputsValue = '';
44+
getMultiInputsValue = Array.from(Array(inputs_num).keys()).reduce((acc, cur) => {
45+
return acc + (cur === 0
46+
? `
47+
if (i == 0) {
48+
ivec4 co = getTensorPosFromArrayIndex_origin(j);
49+
o = getValueFromTensorPos_origin(co.r, co.g, co.b, co.a);
50+
}`
51+
: `
52+
else if (i == ${cur}) {
53+
ivec4 co = getTensorPosFromArrayIndex_origin_${cur}(j);
54+
o = getValueFromTensorPos_origin_${cur}(co.r, co.g, co.b, co.a);
55+
}`);
56+
}, getMultiInputsValue);
57+
58+
59+
return `
60+
// start函数
61+
void main(void) {
62+
ivec4 oPos = getOutputTensorPos();
63+
// 输出坐标转换为输入坐标
64+
int sumVal = oPos.a
65+
+ oPos.b * ${out.width_shape}
66+
+ oPos.g * ${out.height_shape} * ${out.width_shape}
67+
+ oPos.r * ${out.channel} * ${out.width_shape} * ${out.height_shape};
68+
69+
int index = sumVal % ${pre_every_num};
70+
71+
int layer = sumVal / ${pre_every_num};
72+
73+
int i = index / ${post};
74+
int j = index % ${post} + layer * ${post};
75+
76+
77+
float o = 0.0;
78+
${getMultiInputsValue}
79+
setOutput(float(o));
80+
}
81+
`;
82+
}
83+
export default {
84+
mainFunc,
85+
textureFuncConf: {
86+
'@all': ['getValueFromTensorPos', 'getTensorPosFromArrayIndex']
87+
}
88+
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"chunkNum": 0,
3+
"ops": [
4+
{
5+
"attrs": {},
6+
"inputs": {
7+
"X": [
8+
"feed"
9+
]
10+
},
11+
"outputs": {
12+
"Out": [
13+
"image"
14+
]
15+
},
16+
"type": "feed"
17+
},
18+
{
19+
"attrs": {
20+
"axis": 2
21+
},
22+
"inputs": {
23+
"X": [
24+
"stack_in_2", "stack_in", "stack_in_1"
25+
]
26+
},
27+
"outputs": {
28+
"Out": [
29+
"stack_out"
30+
]
31+
},
32+
"type": "stack"
33+
},
34+
{
35+
"attrs": {
36+
"op_device": ""
37+
},
38+
"inputs": {
39+
"X": [
40+
"stack_out"
41+
]
42+
},
43+
"outputs": {
44+
"Out": [
45+
"fetch"
46+
]
47+
},
48+
"type": "fetch"
49+
}
50+
],
51+
"vars": [
52+
{
53+
"name": "stack_in_2",
54+
"shape": [
55+
3,
56+
3
57+
],
58+
"data": [
59+
1, 2, 3, 4, 5, 6, 7, 8, 9
60+
],
61+
"persistable": true
62+
},
63+
{
64+
"name": "stack_in",
65+
"shape": [
66+
3,
67+
3
68+
],
69+
"data": [
70+
11, 12, 13, 14, 15, 16, 17, 18, 19
71+
],
72+
"persistable": true
73+
},
74+
{
75+
"name": "stack_in_1",
76+
"shape": [
77+
3,
78+
3
79+
],
80+
"data": [
81+
21, 22, 23, 24, 25, 26, 27, 28, 29
82+
],
83+
"persistable": true
84+
},
85+
{
86+
"name": "stack_out",
87+
"shape": [3, 3, 3],
88+
"data": [
89+
1, 2, 3, 4, 5, 6, 7, 8, 9,
90+
11, 12, 13, 14, 15, 16, 17, 18, 19,
91+
21, 22, 23, 24, 25, 26, 27, 28, 29
92+
]
93+
}
94+
]
95+
}

packages/paddlejs-core/src/opFactory/opBehaviors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ const behaviors : Behaviors = {
139139
this.processedAttrs['active_function'] = 'tanh_func';
140140
},
141141

142+
transToExp() {
143+
this.processedAttrs['active_function'] = 'exp';
144+
},
145+
142146
transToScale() {
143147
const scale = this.processedAttrs['scale'];
144148
this.processedAttrs['multi_value'] = scale !== undefined ? scale : 1;

packages/paddlejs-core/src/transform/formatInputsX.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class FormatInputsX extends Transformer {
1212
transform(...args: any) {
1313
const [originOp] = args;
1414

15-
const transformOpList = ['concat', 'connect', 'fc', 'rnn_origin', 'rnn_matmul'];
15+
const transformOpList = ['concat', 'connect', 'fc', 'rnn_origin', 'rnn_matmul', 'stack'];
1616
if (!transformOpList.includes(originOp.type)) {
1717
return;
1818
}

0 commit comments

Comments
 (0)