Skip to content

Commit 124c3d2

Browse files
committed
2 parents 12fc5c9 + 1c21440 commit 124c3d2

File tree

23 files changed

+410
-65
lines changed

23 files changed

+410
-65
lines changed

packages/paddlejs-backend-cpu/src/ops/elementwise_add.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Attrs {
7575
}
7676

7777
const behaviors = [
78-
'processAxis'
78+
'processElementwiseAxis'
7979
];
8080

8181
const inputsName = [

packages/paddlejs-backend-wasm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@paddlejs/paddlejs-backend-wasm",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "",
55
"main": "lib/index",
66
"scripts": {

packages/paddlejs-backend-wasm/src/ops.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,25 @@ export default {
6060
},
6161
elementwise_add: {
6262
behaviors: [
63-
'processAxis',
63+
'processElementwiseAxis',
6464
'genElementwiseCounterPos'
6565
]
6666
},
6767
elementwise_div: {
6868
behaviors: [
69-
'processAxis',
69+
'processElementwiseAxis',
7070
'genElementwiseCounterPos'
7171
]
7272
},
7373
elementwise_mul: {
7474
behaviors: [
75-
'processAxis',
75+
'processElementwiseAxis',
7676
'genElementwiseCounterPos'
7777
]
7878
},
7979
elementwise_sub: {
8080
behaviors: [
81-
'processAxis',
81+
'processElementwiseAxis',
8282
'genElementwiseCounterPos'
8383
]
8484
},

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ interface OpParams {
2828
prelu?: {};
2929
sqrt?: {};
3030
tanh?: {};
31+
hard_swish?: {
32+
offset?: number;
33+
scale?: number;
34+
threshold?: number;
35+
};
36+
dropout?: {
37+
dropout_implementation: string;
38+
dropout_prob: number;
39+
}
3140
}
3241
}
3342

@@ -83,8 +92,32 @@ export default function genFuseOpCode(params: OpParams) {
8392
default:
8493
break;
8594
}
86-
activation_func += func[act_name];
87-
calculation_str += `res = ${act_name}(x, float(${multi_value}), float(${bias_value}));`;
95+
96+
if (fuse === 'hard_swish') {
97+
const offset = params.fuse_opt.hard_swish.offset !== undefined
98+
? params.fuse_opt.hard_swish.offset
99+
: 3;
100+
const scale = params.fuse_opt.hard_swish.scale !== undefined
101+
? params.fuse_opt.hard_swish.scale
102+
: 6;
103+
const threshold = params.fuse_opt.hard_swish.threshold !== undefined
104+
? params.fuse_opt.hard_swish.threshold
105+
: 6;
106+
// eslint-disable-next-line max-len
107+
calculation_str += `res = res * min(max(0.0, res + float(${offset})), float(${threshold})) / float(${scale});`;
108+
}
109+
else if (fuse === 'dropout') {
110+
const dropout_implementation = params.fuse_opt.dropout.dropout_implementation;
111+
const dropout_prob = params.fuse_opt.dropout.dropout_prob;
112+
calculation_str += `
113+
if (${dropout_implementation === 'downgrade_in_infer'}) {
114+
res = res * (1.0 - float(${dropout_prob}));
115+
}`;
116+
}
117+
else {
118+
activation_func += func[act_name];
119+
calculation_str += `res = ${act_name}(res, float(${multi_value}), float(${bias_value}));`;
120+
}
88121
}
89122
}
90123

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
function mainFunc({
66
origin,
77
filter,
8-
out
8+
out,
9+
bias
910
}, {
1011
groups = 1,
1112
strides = [],
1213
paddings = [],
13-
dilations = []
14+
dilations = [],
15+
fuse_relu,
16+
act_type
1417
}) {
1518
const [stride_v = 1, stride_h = 1] = strides;
1619
let [padLeft = 0, padTop = 0] = paddings;
@@ -71,6 +74,16 @@ function mainFunc({
7174
}
7275
oy += ${dilation_v};
7376
}
77+
78+
${bias ? 'res += getValueFromTensorPos_bias(0, 0, 0, c);' : ''}
79+
80+
if (${fuse_relu}) {
81+
res = max(0.0, res);
82+
}
83+
else if (${act_type === 'relu6'}) {
84+
res = min(max(0.0, res), 6.0);
85+
}
86+
7487
setOutput(float(res));
7588
}
7689
`;
@@ -79,7 +92,8 @@ export default {
7992
mainFunc,
8093
textureFuncConf: {
8194
filter: ['getValueFromTensorPos'],
82-
origin: ['getValueFromTensorPos']
95+
origin: ['getValueFromTensorPos'],
96+
bias: ['getValueFromTensorPos']
8397
},
8498
behaviors: [
8599
'adaptPaddings',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function mainFunc(
1616
float o = 0.0;
1717
o = getValueFromTensorPos_origin(oPos.r, oPos.g, oPos.b, oPos.a);
1818
if (${dropout_implementation === 'downgrade_in_infer'}) {
19-
o = o * (1.0 - ${dropout_prob});
19+
o = o * (1.0 - float(${dropout_prob}));
2020
}
2121
setOutput(float(o));
2222
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const ops = {
7575
main: elementwise_add_main,
7676
deps: elementwise_add_deps,
7777
behaviors: [
78-
'processAxis'
78+
'processElementwiseAxis'
7979
]
8080
},
8181
split: {

packages/paddlejs-converter/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
System Requirements:
66

77
* paddlepaddle >= 2.0.0
8-
* paddlelite == 2.7.1
8+
* paddlejslite >= 0.0.2
99
* Python3: 3.5.1+ / 3.6 / 3.7
1010
* Python2: 2.7.15+
1111

@@ -26,4 +26,4 @@ pip3 install paddlejsconverter
2626
```shell
2727
paddlejsconverter --modelPath=user_model_path --paramPath=user_model_params_path --outputDir=model_saved_path --useGPUOpt=True
2828
```
29-
注意:useGPUOpt 选项默认不开启,如果模型用在 gpu backend(webgl/webgpu),则开启 useGPUOpt,如果模型运行在(wasm/plain js)则不要开启。
29+
注意:useGPUOpt 选项默认不开启,如果模型用在 gpu backend(webgl/webgpu),则开启 useGPUOpt,如果模型运行在(wasm/plain js)则不要开启。

packages/paddlejs-converter/fuseOps.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def opListFuse(ops):
1212
'hard_sigmoid',
1313
'pow',
1414
'sqrt',
15-
'tanh'
15+
'tanh',
16+
'hard_swish',
17+
'dropout'
1618
]
1719

1820
# 判断op是否为单节点
@@ -38,8 +40,23 @@ def opExistSingleNode(opName):
3840

3941
for index in reversed(range(len(ops))):
4042
if index > 0:
43+
op = ops[index]
44+
45+
# 兼容paddlelite 算子融合字段
46+
if 'act_type' in op['attrs']:
47+
name = op['attrs']['act_type']
48+
op['attrs']['fuse_opt'] = {}
49+
op['attrs']['fuse_opt'][name] = {}
50+
51+
if name == 'hard_swish':
52+
op['attrs']['fuse_opt'][name]['offset'] = op['attrs']['hard_swish_offset']
53+
op['attrs']['fuse_opt'][name]['scale'] = op['attrs']['hard_swish_scale']
54+
op['attrs']['fuse_opt'][name]['threshold'] = op['attrs']['hard_swish_threshold']
55+
56+
if name == 'relu6':
57+
op['attrs']['fuse_opt'][name]['threshold'] = op['attrs']['fuse_brelu_threshold']
58+
4159
for fuse in fuseOpList:
42-
op = ops[index]
4360
if op['type'] == fuse:
4461
prevOp = ops[index - 1]
4562

@@ -53,3 +70,6 @@ def opExistSingleNode(opName):
5370
prevOp['outputs']['Out'] = op['outputs']['Out']
5471

5572
del ops[index]
73+
74+
75+

packages/paddlejs-converter/optimizeModel.py

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import collections
55
import argparse
66
import traceback
7-
from paddlelite import lite
7+
from paddlejslite import lite
88
import pkg_resources
99
from packaging import version
1010

@@ -24,46 +24,6 @@ def optimizeModel(inputDir, modelPath, paramPath, outputDir):
2424
opt.set_valid_places("arm")
2525
opt.set_model_type("protobuf")
2626
opt.set_optimize_out(outputDir)
27-
28-
if version.parse(lite_version) <= version.parse('2.7.1'):
29-
print("python paddlelite version: " + lite_version)
30-
31-
optimize_passes = [
32-
"lite_conv_elementwise_fuse_pass",
33-
"lite_conv_bn_fuse_pass",
34-
"lite_conv_elementwise_fuse_pass",
35-
"lite_conv_activation_fuse_pass",
36-
"lite_var_conv_2d_activation_fuse_pass",
37-
"lite_fc_fuse_pass",
38-
"lite_shuffle_channel_fuse_pass",
39-
"lite_transpose_softmax_transpose_fuse_pass",
40-
"lite_interpolate_fuse_pass",
41-
"identity_scale_eliminate_pass",
42-
"elementwise_mul_constant_eliminate_pass",
43-
"lite_sequence_pool_concat_fuse_pass",
44-
"lite_elementwise_add_activation_fuse_pass",
45-
"static_kernel_pick_pass",
46-
"variable_place_inference_pass",
47-
"argument_type_display_pass",
48-
"type_target_cast_pass",
49-
"variable_place_inference_pass",
50-
"argument_type_display_pass",
51-
"io_copy_kernel_pick_pass",
52-
"argument_type_display_pass",
53-
"variable_place_inference_pass",
54-
"argument_type_display_pass",
55-
"type_precision_cast_pass",
56-
"variable_place_inference_pass",
57-
"argument_type_display_pass",
58-
"type_layout_cast_pass",
59-
"argument_type_display_pass",
60-
"variable_place_inference_pass",
61-
"argument_type_display_pass",
62-
"runtime_context_assign_pass",
63-
"argument_type_display_pass"
64-
]
65-
opt.set_passes_internal(optimize_passes)
66-
6727
opt.run()
6828

6929

@@ -74,20 +34,20 @@ def main():
7434
p.add_argument('--modelPath', help='fluid模型文件所在路径,使用合并参数文件时使用该参数', required=False)
7535
p.add_argument('--paramPath', help='fluid参数文件所在路径,使用合并参数文件时使用该参数', required=False)
7636
p.add_argument("--outputDir", help='优化后fluid模型目录,必要参数', required=True)
77-
37+
7838
args = p.parse_args()
7939
inputDir = args.inputDir
8040
modelPath = args.modelPath
8141
paramPath = args.paramPath
8242
outputDir = args.outputDir
8343

8444
optimizeModel(inputDir, modelPath, paramPath, outputDir)
85-
45+
8646
except Exception as identifier:
8747
print("\033[31mA fetal error occured. Failed to optimize model.\033[0m")
8848
print(traceback.format_exc())
8949
pass
9050

9151

9252
if __name__ == "__main__":
93-
main()
53+
main()

0 commit comments

Comments
 (0)