From e65fda1e9a50622c5b152e8edc9e4f34c5011378 Mon Sep 17 00:00:00 2001 From: Liu Hui Zhi Fen Si <2363918549@qq.com> Date: Fri, 7 May 2021 18:34:56 +0800 Subject: [PATCH 1/2] feat(paddlejs-backend-cpu): add flatten_contiguous_range and pool2dmax --- .../src/ops/flatten_contiguous_range.ts | 74 ++++++++++ .../src/ops/pool2d_max.ts | 129 ++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 packages/paddlejs-backend-cpu/src/ops/flatten_contiguous_range.ts create mode 100644 packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts diff --git a/packages/paddlejs-backend-cpu/src/ops/flatten_contiguous_range.ts b/packages/paddlejs-backend-cpu/src/ops/flatten_contiguous_range.ts new file mode 100644 index 00000000..51261b23 --- /dev/null +++ b/packages/paddlejs-backend-cpu/src/ops/flatten_contiguous_range.ts @@ -0,0 +1,74 @@ +import { Tensor } from './Tensor'; +import { getInt } from './utils'; + +/* eslint-disable max-statements */ +function main(tensorMap: Map, attrs: Attrs, runtime: i32): f32[] { + // v1 paddle.fluid parameter + let axis = attrs.axis; + + // v2 paddle.nn parameter + let start_axis = attrs.start_axis; + let stop_axis = attrs.stop_axis; + + // generic parameters + const origin = tensorMap.get('origin') as Tensor; + const out = tensorMap.get('out_' + runtime) as Tensor; + + const outShape: i32[] = (out as Tensor).shape; + const startIndex = out.runtime * length; + const originReducedShape: i32[] = (origin as Tensor).shapeReduced; + const outReducedShape: i32[] = (out as Tensor).shapeReduced; + + const originData: f32[] = (origin as Tensor).data; + + const outN = outShape[0]; + const outC = outShape[1]; + const outH = outShape[2]; + const outW = outShape[3]; + + const originS0 = originReducedShape[0]; + const originS1 = originReducedShape[1]; + const originS2 = originReducedShape[2]; + + const outS0 = outReducedShape[0]; + const outS1 = outReducedShape[1]; + const outS2 = outReducedShape[2]; + + const result = new Array(out.total); + + for (let n = 0; n < outN; n++) { + for (let c = 0; c < outC; c++) { + for (let h = 0; h < outH; h++) { + for (let w = 0; w < outW; w++) { + + result[n * originS0 + c * originS1 + h * originS2 + w] + = originData[n * originS0 + c * originS1 + h * originS2 + w]; + + } + } + } + } + return result; +} +/* eslint-enable max-statements */ + +class Attrs { + axis: i32 = 1; + + start_axis : i32 = 0; + stop_axis : i32 = -1; + + constructor(data: Obj) { + this.start_axis = getInt('start_axis', data); + this.stop_axis = getInt('stop_axis', data); + this.axis = getInt('axis', data); + } +} + +const behaviors = []; + +export { + main, + Attrs, + behaviors +}; \ No newline at end of file diff --git a/packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts b/packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts new file mode 100644 index 00000000..6bf69b03 --- /dev/null +++ b/packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts @@ -0,0 +1,129 @@ +import { Tensor } from './Tensor'; +import { getIntArray, getInt } from './utils'; + +/* eslint-disable max-statements, max-depth */ +function main(tensorMap: Map, attrs: Attrs, runtime: i32): f32[] { + const origin = tensorMap.get('origin') as Tensor; + const out = tensorMap.get('out_' + runtime) as Tensor; + + const originShape: i32[] = (origin as Tensor).shape; + const outShape: i32[] = (out as Tensor).shape; + + const originData: f32[] = (origin as Tensor).data; + + const originReducedShape: i32[] = (origin as Tensor).shapeReduced; + const outReducedShape: i32[] = (out as Tensor).shapeReduced; + + const strides = attrs.strides; + const paddings = attrs.paddings; + const ksizes = attrs.ksize; + const pooling_type = attrs.pooling_type; + + const stride_v = strides[0] || 1; + const stride_h = strides[1] || 1; + const ksize_x = ksizes[0] || 1; + const ksize_y = ksizes[1] || 1; + const padTop = paddings[0] || 0; + const padLeft = paddings[1] || 0; + + const outB = outShape[0]; + const outC = outShape[1]; + const outH = outShape[2]; + const outW = outShape[3]; + + + const originH = originShape[2]; + const originW = originShape[3]; + + const originS0 = originReducedShape[0]; + const originS1 = originReducedShape[1]; + const originS2 = originReducedShape[2]; + + const outS0 = outReducedShape[0]; + const outS1 = outReducedShape[1]; + const outS2 = outReducedShape[2]; + + const result = new Array(out.total); + + for (let n = 0; n < outB; n++) { + for (let c = 0; c < outC; c++) { + for (let h = 0; h < outH; h++) { + for (let w = 0; w < outW; w++) { + let res = 0.0; + let count_pool = 0; + const oy_base = h * stride_v - padTop; + const ox_base = w * stride_h - padLeft; + for (let fy = 0; fy < ksize_y; fy++) { + const oy = oy_base + fy; + if (oy >= originH) { + break; + } + if (oy < 0) { + continue; + } + for (let fx = 0; fx < ksize_x; fx++) { + const ox = ox_base + fx; + if (ox >= originW) { + break; + } + if (ox < 0) { + continue; + } + // origin数据 + const curr = originData[n * originS0 + c * originS1 + oy * originS2 + ox]; + /* eslint-disable-next-line */ + if (pooling_type == 1) { + // max-pool update + if (count_pool == 0){ + res = curr; + count_pool++; + } + if (curr > res) { + res = curr; + } + } + else { + res += curr; + // 在平均池化模式忽略填充值(exclusive默认为true) + count_pool++; + } + } + } + /* eslint-disable-next-line */ + if (pooling_type != 1) { + res = res / count_pool; + } + result[n * outS0 + c * outS1 + h * outS2 + w] = f32(res); + } + } + } + } + + return result; +} +/* eslint-enable max-statements */ + +class Attrs { + strides: i32[] = []; + paddings: i32[] = []; + ksize: i32[] = []; + pooling_type: i32; + constructor(data: Obj) { + this.strides = getIntArray('strides', data); + this.paddings = getIntArray('paddings', data); + this.ksize = getIntArray('dilations', data); + this.pooling_type = getInt('groups', data); + } +} + +const behaviors = [ + 'isMax', + 'setPacked', + 'isGlobalPooling' +]; + +export { + main, + Attrs, + behaviors +}; \ No newline at end of file From 3adc790110aa59244c9e38bd37c15e8c07e8864e Mon Sep 17 00:00:00 2001 From: Liu Hui Zhi Fen Si <2363918549@qq.com> Date: Fri, 7 May 2021 18:39:28 +0800 Subject: [PATCH 2/2] feat(paddlejs-backend-cpu): add pool2d_max and flatten_continuous_range ops --- packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts b/packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts index 6bf69b03..dd32a67d 100644 --- a/packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts +++ b/packages/paddlejs-backend-cpu/src/ops/pool2d_max.ts @@ -73,7 +73,7 @@ function main(tensorMap: Map, attrs: Attrs, runtime: i32): f32[] const curr = originData[n * originS0 + c * originS1 + oy * originS2 + ox]; /* eslint-disable-next-line */ if (pooling_type == 1) { - // max-pool update + // max-pool update if (count_pool == 0){ res = curr; count_pool++;