Skip to content

Commit eeebcc1

Browse files
Merge pull request #232 from JingyuanZhang/master
chore(v2.1.0): publish core@2.1.0 webgl@1.1.0 and update models version
2 parents 7f3b23a + f5f3e5b commit eeebcc1

File tree

34 files changed

+315
-23617
lines changed

34 files changed

+315
-23617
lines changed

packages/paddlejs-backend-webgl/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/paddlejs-backend-webgl/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-webgl",
3-
"version": "1.0.7",
3+
"version": "1.1.0",
44
"description": "",
55
"main": "lib/index",
66
"scripts": {

packages/paddlejs-benchmark/src/config.vue

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<script lang="ts">
4848
import Vue from 'vue';
4949
import { Runner } from '@paddlejs/paddlejs-core';
50-
import { GLOBALS } from '@paddlejs/paddlejs-core/globals';
50+
import { GLOBALS } from '@paddlejs/paddlejs-core/src/globals';
5151
import '@paddlejs/paddlejs-backend-webgl';
5252
import Utils from './utils';
5353
@@ -64,7 +64,6 @@ export default Vue.extend({
6464
},
6565
fetchShape: [1, 1000, 10, 1],
6666
fill: '#fff',
67-
targetSize: { height: 224, width: 224 },
6867
needPreheat: false
6968
},
7069
{
@@ -75,7 +74,6 @@ export default Vue.extend({
7574
},
7675
fetchShape: [1, 1000, 10, 1],
7776
fill: '#fff',
78-
targetSize: { height: 224, width: 224 },
7977
needPreheat: false
8078
},
8179
{
@@ -86,7 +84,6 @@ export default Vue.extend({
8684
},
8785
fetchShape: [1, 40, 10, 1],
8886
fill: '#fff',
89-
targetSize: { height: 224, width: 224 },
9087
needPreheat: false
9188
},
9289
{
@@ -97,7 +94,6 @@ export default Vue.extend({
9794
},
9895
fetchShape: [1, 1920, 10 , 1],
9996
fill: '#fff',
100-
targetSize: { height: 256, width: 256 },
10197
needPreheat: false
10298
},
10399
{
@@ -108,7 +104,6 @@ export default Vue.extend({
108104
},
109105
fetchShape: [1, 9, 1, 1],
110106
fill: '#fff',
111-
targetSize: { height: 224, width: 224 },
112107
needPreheat: false
113108
},
114109
{
@@ -119,7 +114,6 @@ export default Vue.extend({
119114
},
120115
fetchShape: [1, 2, 192, 192],
121116
fill: '#fff',
122-
targetSize: { height: 224, width: 224 },
123117
needPreheat: false
124118
}
125119
],

packages/paddlejs-core/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,42 @@ As the core part of the Paddle.js ecosystem, this package hosts `@paddlejs/paddl
55
which is responsible for the operation of the inference process of the entire engine,
66
and provides interfaces for backend registration and environment variable registration.
77

8+
9+
## RunnerConfig
10+
11+
When registering the engine you need to configure the engine, you must configure the items `modelPath`, `feedShape`, all items are configured as follows.
12+
13+
```typescript
14+
15+
// model struture
16+
enum GraphType {
17+
SingleOutput = 'single',
18+
MultipleOutput = 'multiple',
19+
MultipleInput = 'multipleInput'
20+
}
21+
22+
interface RunnerConfig {
23+
modelPath: string; // model path (local or web address)
24+
modelName?: string; // model name
25+
feedShape: { // input feed shape
26+
fc?: number; // feed channel, default is 3.
27+
fw: number; // feed width
28+
fh: number; // feed height
29+
};
30+
fill?: Color; // the color used to padding
31+
mean?: number[]; // mean value
32+
std?: number[]; // standard deviation
33+
bgr?: boolean; // whether the image channel alignment is BGR, default is false (RGB)
34+
type?: GraphType; // model structure, default is singleInput and singleOutput
35+
needPreheat?: boolean; // whether to warm up the engine during initialization, default is true
36+
plugins?: { // register model topology transform plugin
37+
preTransforms?: Transformer[]; // transform before creating network topology
38+
transforms?: Transformer[]; // transform when traversing model layers
39+
postTransforms?: Transformer[]; // transform the model topology diagram after it has been created
40+
};
41+
}
42+
43+
```
844
## Importing
945
You can install this package via npm., `@paddlejs/paddlejs-core`
1046

@@ -32,3 +68,56 @@ const res = await runner.predict(mediadata, callback?);
3268
**Note**: If you are importing the Core package, you also need to import a backend (e.g.,
3369
[paddlejs-backend-webgl](/packages/paddlejs-backend-webgl), [paddlejs-backend-webgpu](/packages/paddlejs-backend-webgpu)).
3470
71+
72+
## High-level use
73+
74+
1. `@paddlejs/paddlejs-core` provides the interface `registerOp`, through which developers can register custom operators.
75+
76+
2. `@paddlejs/paddlejs-core` provides the global variable `env` module, through which developers can register environment variables, using the following method:
77+
78+
```js
79+
// set env key/flag and value
80+
env.set(key, value);
81+
82+
// get value by key/flag
83+
env.get(key);
84+
```
85+
86+
3. transform model stucture
87+
88+
By registering the model transformers through `runnerConfig.plugins`, developers can make changes (add, delete, change) to the model structure, such as pruning to remove unnecessary layers to speed up inference, or adding custom layers to the end of the model and turning post-processing into layers in the model to speed up post-processing.
89+
90+
91+
4. Turn on performance flag for acceleration
92+
93+
Paddle.js currently provides five performance `flags`, which can be set to `true` if you want to enable inference acceleration.
94+
95+
96+
```js
97+
env.set('webgl_pack_channel', true);
98+
```
99+
Turn on `webgl_pack_channel` and the eligible conv2d will use packing shader to perform packing transformations to improve performance through vectorization calculations.
100+
101+
102+
```js
103+
env.set('webgl_force_half_float_texture', true);
104+
```
105+
Enable `webgl_force_half_float_texture`, feature map uses half float `HALF_FLOAT`.
106+
107+
108+
```js
109+
env.set('webgl_feed_process', true);
110+
```
111+
Turn on `webgl_feed_process` to convert all pre-processing parts of the model to shader processing, and keep the original image texture.
112+
113+
114+
```js
115+
env.set('webgl_gpu_pipeline', true);
116+
```
117+
Turn on `webgl_gpu_pipeline` to convert all model pre-processing parts to shader processing, and render the model results to `WebGL2RenderingContext` of webgl backend on screen. Developers can perform model post-processing on the output texture and the original image texture to achieve the `GPU_PIPELINE`: pre-processing + inference + post-processing (rendering processing) to get high performance. Take humanseg model case for reference.
118+
119+
120+
```js
121+
env.set('webgl_pack_output', true);
122+
```
123+
Enable `webgl_pack_output` to migrate the `NHWC` to `NCHW` layout transformation of the model output to the GPU, and `pack` to a four-channel layout to reduce loop processing when reading the results from the GPU

packages/paddlejs-core/README_cn.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,43 @@
44

55
是 Paddle.js 推理引擎的核心部分,npm 包名是 `@paddlejs/paddlejs-core`,负责整个引擎的推理流程运行,提供计算方案注册、环境变量注册的接口。
66

7-
## 安装
7+
8+
## 引擎配置 RunnerConfig
9+
注册引擎时需要对引擎进行配置,必须配置项为 `modelPath``feedShape`,所有项目配置如下:
10+
11+
```typescript
12+
13+
// 模型网络图结构
14+
enum GraphType {
15+
SingleOutput = 'single', // 单输出
16+
MultipleOutput = 'multiple', // 多输出
17+
MultipleInput = 'multipleInput' // 多输入
18+
}
19+
20+
interface RunnerConfig {
21+
modelPath: string; // 模型路径(本地路径或者网络地址)
22+
modelName?: string; // 模型名称
23+
feedShape: { // 模型输入 feed shape
24+
fc?: number; // 通道,默认为 3
25+
fw: number; //
26+
fh: number; //
27+
};
28+
fill?: string; // 缩放后用什么颜色填充不足方形部分
29+
mean?: number[]; // 平均值
30+
std?: number[]; // 标准差
31+
bgr?: boolean; // 图片通道排列是否是 BGR,默认为 false,为主流 RGB
32+
type?: GraphType; // 模型网络图结构,默认单输入单输出
33+
needPreheat?: boolean; // 是否在引擎初始化时进行预热,默认为 true
34+
plugins?: { // 注册模型拓扑结构转换插件
35+
preTransforms?: Transformer[]; // 在创建网络拓扑前进行转换
36+
transforms?: Transformer[]; // 在遍历模型层时进行转换
37+
postTransforms?: Transformer[]; // 创建网络拓扑图完成后进行转换
38+
};
39+
}
40+
41+
```
42+
43+
## 安装和使用
844
使用 npm 安装,`@paddlejs/paddlejs-core`
945

1046
```js
@@ -30,3 +66,59 @@ const res = await runner.predict(mediadata, callback?);
3066
3167
**Note**: 如果你引入 paddlejs-core,你仍需引入一个计算方案。(目前我们提供两种方案, webgpu 目前还是实验阶段,需要使用 chrome canary 访问,
3268
[paddlejs-backend-webgl](/packages/paddlejs-backend-webgl), [paddlejs-backend-webgpu](/packages/paddlejs-backend-webgpu)).
69+
70+
71+
72+
## 高阶使用
73+
74+
1. `@paddlejs/paddlejs-core` 提供接口 `registerOp`,开发者可以通过该接口,完成自定义算子注册。
75+
76+
2. `@paddlejs/paddlejs-core` 提供全局变量 `env` 模块,开发者可以通过该模块完成环境变量注册,使用方法如下:
77+
78+
```js
79+
// set env key/flag and value
80+
env.set(key, value);
81+
82+
// get value by key/flag
83+
env.get(key);
84+
```
85+
86+
3. 改变模型结构
87+
88+
通过 `runnerConfig.plugins` 注册模型转换器,开发者可以对模型结构进行改变(增、删、改),比如通过剪枝去除不必要的模型层来加快推理,也可以将自定义层加入模型最后,将后处理变为模型中的层来加速后处理。
89+
90+
91+
4. 开启性能 flag 实现加速
92+
93+
目前 Paddle.js 提供五个性能相关 `flag`,如需开启推理加速,可以将相关 `flag``true`
94+
95+
96+
#### 开启 `webgl_pack_channel`,合适的 conv2d 会使用 packing shader 进行排布变换,通过向量化计算来提高性能。
97+
98+
```js
99+
env.set('webgl_pack_channel', true);
100+
```
101+
102+
#### 开启 `webgl_force_half_float_texture`,feature map 使用半浮点 `HALF_FLOAT`
103+
104+
```js
105+
env.set('webgl_force_half_float_texture', true);
106+
```
107+
108+
#### 开启 `webgl_feed_process`,将模型前处理部分全部转换为 shader GPU 处理,并保留原始图片 texture
109+
110+
```js
111+
env.set('webgl_feed_process', true);
112+
```
113+
114+
#### 开启 `webgl_gpu_pipeline`,将模型前处理部分全部转换为 shader GPU 处理,且将模型推理结果上屏渲染到 webgl backend 的 `WebGL2RenderingContext` 上。开发者可对输出结果 texture 和原始图片 texture 进行模型后处理,来实现 GPU 全流程:前处理+推理+后处理(渲染处理),获得高性能,可参考 humanseg model 案例。
115+
116+
```js
117+
env.set('webgl_gpu_pipeline', true);
118+
```
119+
120+
#### 开启 `webgl_pack_output`,将模型输出结果 `NHWC``NCHW` 排布变换迁移至 GPU 进行,并 `pack` 为四通道排布,从 GPU 读取结果时,可以减少循环处理
121+
122+
```js
123+
env.set('webgl_pack_output', true);
124+
```

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import Graph from '../../src/graph';
2-
import { ModelConfig } from '../../src/commons/interface';
2+
import { RunnerConfig } from '../../src/commons/interface';
33
import modelInfo from '../env/mock/model.json';
44

55
describe('test graph', () => {
66

77
const graphGenerator = new Graph(modelInfo, {
88
type: 'single'
9-
} as ModelConfig);
9+
} as RunnerConfig);
1010
const weightMap = graphGenerator.createGraph();
1111

1212

packages/paddlejs-core/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-core",
3-
"version": "2.0.7",
3+
"version": "2.1.0",
44
"description": "",
55
"main": "lib/index",
66
"scripts": {

packages/paddlejs-core/src/commons/interface.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,14 @@ export interface Model {
3838
multiOutputs?: ModelVar[]
3939
}
4040

41-
export interface ModelConfig {
41+
export interface RunnerConfig {
4242
modelPath: string;
4343
modelName?: string;
4444
feedShape: {
4545
fc?: number;
4646
fw: number;
4747
fh: number;
4848
};
49-
targetSize?: {
50-
height: number;
51-
width: number;
52-
};
5349
fill?: string; // 缩放后用什么颜色填充不足方形部分
5450
mean?: number[];
5551
std?: number[];

packages/paddlejs-core/src/graph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file ModelGraph,graph 生成器
33
*/
44

5-
import { ModelOp, GraphType, Model, ModelVar, ModelConfig } from './commons/interface';
5+
import { ModelOp, GraphType, Model, ModelVar, RunnerConfig } from './commons/interface';
66
import OpExecutor from './opFactory/opExecutor';
77
import transformActions from './transform';
88

@@ -28,11 +28,11 @@ export default class ModelGraph {
2828
weightMap: OpExecutor[] = [];
2929
ops: ModelOp[] = [];
3030
vars: ModelVar[] = [];
31-
config: ModelConfig = {} as ModelConfig;
31+
config: RunnerConfig = {} as RunnerConfig;
3232
type: GraphType = GraphType.SingleOutput;
3333
plugins: GraphPlugins = null;
3434

35-
constructor(model: Model, config: ModelConfig) {
35+
constructor(model: Model, config: RunnerConfig) {
3636
this.ops = model.ops;
3737
this.vars = model.vars;
3838
this.type = config.type || this.type;

packages/paddlejs-core/src/mediaProcessor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,11 @@ export default class MediaProcessor {
109109
*/
110110
allReshapeToRGB(imageData, opt) {
111111
// mean和std是介于0-1之间的
112-
const { mean, std, targetShape, bgr } = opt;
112+
const { mean, std, targetShape, bgr, normalizeType = 0 } = opt;
113113
const [, c, h, w] = targetShape;
114114
const data = imageData.data || imageData;
115115
const result = new Float32Array(h * w * c);
116116
let offset = 0;
117-
// 将数据映射为0~1, 1:映射为-1~1之间
118-
const normalizeType = 0;
119117
// h w c
120118
for (let i = 0; i < h; ++i) {
121119
const iw = i * w;

0 commit comments

Comments
 (0)