Skip to content

Commit cc0ae99

Browse files
committed
feat(converter): del nchw2nhwc transpose and publish paddlejsconverter
1 parent 2f686ae commit cc0ae99

File tree

10 files changed

+20
-18
lines changed

10 files changed

+20
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default {
3030
origin: ['getValueFromTensorPos']
3131
},
3232
behaviors: [
33-
'processAxis',
33+
'processElementwiseAxis',
3434
'genElementwiseCounterPos'
3535
]
3636
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default {
5454
origin: ['getValueFromTensorPos']
5555
},
5656
behaviors: [
57-
'processAxis',
57+
'processElementwiseAxis',
5858
'genElementwiseCounterPos'
5959
]
6060
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default {
5454
origin: ['getValueFromTensorPos']
5555
},
5656
behaviors: [
57-
'processAxis',
57+
'processElementwiseAxis',
5858
'genElementwiseCounterPos'
5959
]
6060
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default {
3030
origin: ['getValueFromTensorPos']
3131
},
3232
behaviors: [
33-
'processAxis',
33+
'processElementwiseAxis',
3434
'genElementwiseCounterPos'
3535
]
3636
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default {
3030
origin: ['getValueFromTensorPos']
3131
},
3232
behaviors: [
33-
'processAxis',
33+
'processElementwiseAxis',
3434
'genElementwiseCounterPos'
3535
]
3636
};

packages/paddlejs-converter/convertModel.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# paddlepaddle运行程序实例
3636
program = None
3737
# 存放模型结构
38-
modelInfo = {"vars": {}, "ops": [], "chunkNum": 0, "dataLayout": "nhwc", "feedShape": None}
38+
modelInfo = {"vars": {}, "ops": [], "chunkNum": 0, "dataLayout": "nchw", "feedShape": None}
3939
# 存放参数数值(未排序)
4040
paramValuesDict = {}
4141

@@ -193,12 +193,6 @@ def organizeModelVariableInfo(result):
193193
# persistable数据存入paramValuesDict,等待排序
194194
if v.persistable:
195195
tensor = np.array(fluid.global_scope().find_var(v.name).get_tensor())
196-
# nchw 转 nhwc
197-
if len(tensor.shape) == 3:
198-
tensor = np.transpose(tensor, (1, 2, 0))
199-
elif len(tensor.shape) == 4:
200-
tensor = np.transpose(tensor, (0, 2, 3, 1))
201-
202196
data = tensor.flatten().tolist()
203197
paramValuesDict[v.name] = data
204198

packages/paddlejs-converter/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setuptools.setup(
99
name="paddlejsconverter",
10-
version="1.0.3",
10+
version="1.0.4",
1111
author="paddlejs",
1212
author_email="382248373@qq.com",
1313
description="Paddlejs model converter",

packages/paddlejs-core/__tests__/spec/behaviors.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,12 @@ describe('test op behaviors', () => {
190190
expect(mergedOp.processedAttrs['active_function']).toBe('leakyRelu');
191191
});
192192

193-
it('test behavior processAxis', () => {
194-
behaviors.processAxis.call(elementwiseAddOp as unknown as OpData);
193+
it('test behavior processElementwiseAxis', () => {
194+
behaviors.processElementwiseAxis.call(elementwiseAddOp as unknown as OpData);
195195
expect(elementwiseAddOp.processedAttrs.axis).toBe(0);
196196
});
197197

198198
it('test behavior flattenShape', () => {
199-
console.log(JSON.stringify(fcOp));
200199
behaviors.flattenShape.call(fcOp as unknown as OpData);
201200
const origin = fcOp.tensorDataMap['origin'];
202201
expect(origin.shape).toEqual([2, 9]);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ const behaviors : Behaviors = {
213213
this.processedAttrs.dim = axis;
214214
},
215215

216-
processAxis() {
216+
processElementwiseAxis() {
217217
const shape_x = this.tensorDataMap['origin'].shape;
218218
const shape_y = this.tensorDataMap['counter'].shape;
219219
let axis = this.processedAttrs.axis || -1;
220220

221221
this.processedAttrs.counterLen = shape_y.length;
222222
// shape x === shape y
223-
if (Utils.accShape(shape_x) === Utils.accShape(shape_y)) {
223+
if (Utils.getSumOfShape(shape_x) === Utils.getSumOfShape(shape_y)) {
224224
this.processedAttrs.axis = 0;
225225
this.processedAttrs.counterLen = 4;
226226
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ export function formatShape(shape: number[]): number[] {
2626
* @returns {number} total length of shape
2727
*/
2828
export function accShape(shape: number[]): number {
29+
return shape.reduce((all, num) => all * num);
30+
}
31+
32+
/**
33+
* calculate the sum of shape
34+
* @param {Array} shape shape of tensor
35+
* @returns {number} sum of shape
36+
*/
37+
export function getSumOfShape(shape: number[]): number {
2938
return shape.reduce((all, num) => all + num);
3039
}
3140

0 commit comments

Comments
 (0)