Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#79 from JingyuanZhang/master
Browse files Browse the repository at this point in the history
feat(webgl): update Paddlejs 2.0 op: elementwise_ops, leaky_relu and scale
  • Loading branch information
zhongkai authored Feb 10, 2021
2 parents 9bd351c + 6b8043c commit a851fa0
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 17 deletions.
19 changes: 16 additions & 3 deletions packages/paddlejs-backend-webgl/src/ops/atom/common_func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ float prelu(float x, float p, float b) {

const relu6 = `
float relu6(float x, float threshold, float b) {
float result = max(0.0,x);
result = min(result,threshold);
float result = max(0.0, x);
result = min(result, threshold);
return result;
}`;

const leakyRelu = `
float leakyRelu(float x, float p, float b) {
float result = max(x, x * p);
float result = 0.0
if (x > 0.0) {
result = x;
}
else {
result = p * x;
}
return result;
}`;

Expand All @@ -34,6 +40,12 @@ float scale(float x, float p, float b) {
return result;
}`;

const scaleWidthBias = `
float scaleWidthBias(float x, float p, float b) {
float result = p * (x + b);
return result;
}`;

const sigmoid = `
float sigmoid(float x, float y, float z) {
float result = 1.0 / (1.0 + exp(-x));
Expand All @@ -54,6 +66,7 @@ export {
scale,
sigmoid,
hardSigmoid,
scaleWidthBias,
transferFromNHWCtoNCHW
};

16 changes: 12 additions & 4 deletions packages/paddlejs-backend-webgl/src/ops/shader/elementwise_add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

function mainFunc(
{},
{ axis }
{
axis,
Scale_y = 1.0,
Scale_x = 1.0,
Scale_out = 1.0
}
) {

return `
// start函数
void main(void) {
// 输出数据
ivec4 oPos = getOutputTensorPos();
Expand All @@ -26,15 +31,18 @@ function mainFunc(
else {
c = getValueFromTensorPos_counter(oPos.r, oPos.g, oPos.b, oPos.a);
}
float res = c + o;
float res = float(${Scale_out / Scale_y}) * c + float(${Scale_out / Scale_x}) * o;
setOutput(float(res));
}
`;
}
export default {
mainFunc,
params: [
'axis'
'axis',
'Scale_y',
'Scale_x',
'Scale_out'
],
textureFuncConf: {
counter: ['getValueFromTensorPos'],
Expand Down
14 changes: 11 additions & 3 deletions packages/paddlejs-backend-webgl/src/ops/shader/elementwise_div.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

function mainFunc(
{ counter },
{ axis }
{
axis,
Scale_y = 1.0,
Scale_x = 1.0,
Scale_out = 1.0
}
) {
return `
ivec4 formatNCHW(int n, int c, int h, int w) {
Expand Down Expand Up @@ -48,7 +53,7 @@ function mainFunc(
else {
c = getValueFromTensorPos_counter(oPos.r, oPos.g, oPos.b, oPos.a);
}
float res = o / c;
float res = float(${Scale_out}) * (float(${1 / Scale_x}) * o / (float(${1 / Scale_y}) * c));
setOutput(float(res));
}
Expand All @@ -57,7 +62,10 @@ function mainFunc(
export default {
mainFunc,
params: [
'axis'
'axis',
'Scale_y',
'Scale_x',
'Scale_out'
],
textureFuncConf: {
counter: ['getValueFromTensorPos'],
Expand Down
14 changes: 11 additions & 3 deletions packages/paddlejs-backend-webgl/src/ops/shader/elementwise_mul.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

function mainFunc(
{ counter },
{ axis }
{
axis,
Scale_y = 1.0,
Scale_x = 1.0,
Scale_out = 1.0
}
) {
return `
ivec4 formatNCHW(int n, int c, int h, int w) {
Expand Down Expand Up @@ -48,7 +53,7 @@ function mainFunc(
else {
c = getValueFromTensorPos_counter(oPos.r, oPos.g, oPos.b, oPos.a);
}
float res = o * c;
float res = float(${Scale_out / Scale_x}) * o * float(${1 / Scale_y}) * c;
setOutput(float(res));
}
Expand All @@ -57,7 +62,10 @@ function mainFunc(
export default {
mainFunc,
params: [
'axis'
'axis',
'Scale_y',
'Scale_x',
'Scale_out'
],
textureFuncConf: {
counter: ['getValueFromTensorPos'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"attrs": {
"axis": 1,
"Scale_y": 2,
"use_mkldnn": false,
"x_data_format": "",
"y_data_format": ""
Expand Down Expand Up @@ -47,7 +48,7 @@
]
},
{
"data": [2, 4, 3, 4, 5, 6, 17, 8, 9, 10, 11, 24],
"data": [1.5, 3, 3, 4, 5, 6, 12, 8, 9, 10, 11, 18],
"name": "fc_0.tmp_1",
"shape": [
3, 2 ,2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"attrs": {
"axis": 0,
"Scale_y": 1,
"use_mkldnn": false,
"x_data_format": "",
"y_data_format": ""
Expand Down Expand Up @@ -56,7 +57,7 @@
]
},
{
"data": [1, 2, 3, 4, 2, 2, 4, 4, 3, 3, 3, 3],
"data": [1, 2, 3, 4, 0.5, 0.5, 1, 1, 0.25, 0.25, 0.25, 0.25],
"name": "fc_0.tmp_1",
"shape": [
3, 2 ,2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"attrs": {
"axis": 0,
"Scale_y": 2.0,
"use_mkldnn": false,
"x_data_format": "",
"y_data_format": ""
Expand Down Expand Up @@ -56,7 +57,7 @@
]
},
{
"data": [1, 2, 3, 4, 2, 2, 4, 4, 3, 3, 3, 3],
"data": [0.5, 1, 1.5, 2, 1, 1, 2, 2, 1.5, 1.5, 1.5, 1.5],
"name": "fc_0.tmp_1",
"shape": [
3, 2 ,2
Expand Down
5 changes: 4 additions & 1 deletion packages/paddlejs-core/src/opFactory/opBehaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ const behaviors : Behaviors = {
transToScale() {
this.data['multi_value'] = this.attrs['scale'] || 1;
this.data['bias_value'] = this.attrs['bias'] || 0;
this.data['active_function'] = 'scale';
const bias_after_scale = this.attrs['bias_after_scale'];
this.data['active_function'] = (bias_after_scale || bias_after_scale === undefined)
? 'scale'
: 'scaleWidthBias';
},

setActiveFunc() {
Expand Down

0 comments on commit a851fa0

Please sign in to comment.