Skip to content

Commit dafb6dc

Browse files
committed
feat(core&webgl): support multiple models run in one backend
1 parent 227f637 commit dafb6dc

File tree

14 files changed

+89
-126
lines changed

14 files changed

+89
-126
lines changed

.npmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
PUPPETEER_SKIP_DOWNLOAD
1+
PUPPETEER_SKIP_DOWNLOAD

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.2",
3+
"version": "1.0.3",
44
"description": "",
55
"main": "lib/index",
66
"scripts": {

packages/paddlejs-backend-webgl/src/backend.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export default class WebGLBackend extends PaddlejsBackend {
126126
program.setProgram(this.gl, this.vertexBuffer, isRendered);
127127
this.program = program;
128128

129-
this.render(opData.inputTensors, opData.iLayer, isRendered, index, isPacked);
129+
this.render(opData.inputTensors, opData.iLayer, isRendered, index, isPacked, opData.modelName);
130130
});
131131

132132
if (query) {
@@ -285,49 +285,50 @@ export default class WebGLBackend extends PaddlejsBackend {
285285
return this.frameBuffer;
286286
}
287287

288-
render(data: any = [], iLayer: number = 0, isRendered: Boolean = false, index: number, isPacked: Boolean = false) {
288+
render(data: any = [], iLayer: number = 0, isRendered: Boolean = false, index: number, isPacked: Boolean = false, modelName: string) {
289289
const gl = this.gl;
290290
const that = this;
291291
let textureIndex = 0;
292292
data.forEach(item => {
293-
const loc = that.getUniformLoc('texture_' + item.name, iLayer, isRendered, index);
293+
const loc = that.getUniformLoc('texture_' + item.name, iLayer, isRendered, index, modelName);
294294
if (!loc) {
295295
return;
296296
}
297-
that.initTexture(textureIndex, item, iLayer, isRendered, isPacked);
297+
that.initTexture(textureIndex, item, iLayer, isRendered, isPacked, modelName);
298298
gl.uniform1i(loc, textureIndex++);
299299
});
300300
// gl.clearColor(.0, .0, .0, 1);
301301
// gl.clear(gl.COLOR_BUFFER_BIT);
302302
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
303303
}
304304

305-
initTexture(index, item, iLayer, isRendered, isPacked) {
305+
initTexture(index, item, iLayer, isRendered, isPacked, modelName) {
306306
const gl = this.gl;
307307
const textureConf = this.textureConf as TextureConfig;
308308
const tensorName = item.opts.type;
309+
const prefix = modelName + '_';
309310
let texture;
310311
if (!item.data) {
311312
// texture = this.prevTexture;
312313
texture = this.texturesMap[item.opts.type];
313314
}
314315
else {
315316
// texture = gl.createTexture();
316-
if (isRendered && (iLayer > 1 || (iLayer === 1 && tensorName !== 'image'))) {
317-
const tData = this.cacheTextures['' + iLayer];
317+
if (isRendered && (iLayer > 1 || (iLayer === 1 && !tensorName.endsWith('_image')))) {
318+
const tData = this.cacheTextures[prefix + iLayer];
318319
texture = tData['texture_' + tensorName];
319320
}
320321
else {
321322
texture = gl.createTexture();
322-
this.cacheTextures['' + iLayer] = this.cacheTextures['' + iLayer] || {};
323-
this.cacheTextures['' + iLayer]['texture_' + tensorName] = texture;
323+
this.cacheTextures[prefix + iLayer] = this.cacheTextures[prefix + iLayer] || {};
324+
this.cacheTextures[prefix + iLayer]['texture_' + tensorName] = texture;
324325
}
325326
}
326327

327328
gl.activeTexture(gl[`TEXTURE${index}`]);
328329
gl.bindTexture(gl.TEXTURE_2D, texture);
329330

330-
if (item.data && (!isRendered || (isRendered && iLayer === 1 && tensorName === 'image'))) {
331+
if (item.data && (!isRendered || (isRendered && iLayer === 1 && tensorName.endsWith('_image')))) {
331332
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
332333
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
333334
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
@@ -374,14 +375,15 @@ export default class WebGLBackend extends PaddlejsBackend {
374375
}
375376
}
376377

377-
getUniformLoc(name, ilayer, isRendered, index) {
378+
getUniformLoc(name, ilayer, isRendered, index, modelName) {
379+
const prefix = modelName + '_';
378380
if (isRendered) {
379-
return this.uniformLocations['' + ilayer][name + index];
381+
return this.uniformLocations[prefix + ilayer][name + index];
380382
}
381383
const loc = this.gl.getUniformLocation((this.program as GLProgram).program as WebGLProgram, name);
382384
// 缓存
383-
this.uniformLocations['' + ilayer] = this.uniformLocations['' + ilayer] || {};
384-
this.uniformLocations['' + ilayer][name + index] = loc;
385+
this.uniformLocations[prefix + ilayer] = this.uniformLocations[prefix + ilayer] || {};
386+
this.uniformLocations[prefix + ilayer][name + index] = loc;
385387
return loc;
386388
}
387389

packages/paddlejs-backend-webgl/src/index.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@ import WebGLBackend from './backend';
88
import { ops } from './ops';
99
import { GLHelper } from './webgl/WebGLUtils';
1010
import * as webgl_types from './webgl/webgl_types';
11-
import { GLOBALS } from '@paddlejs/paddlejs-core/globals';
1211

13-
let glInstance;
14-
function registerWebGLBackend(name?: string) {
15-
glInstance = new WebGLBackend();
12+
const glInstance = new WebGLBackend();
13+
14+
function registerWebGLBackend() {
1615
registerBackend(
17-
name || 'webgl',
16+
'webgl',
1817
glInstance,
19-
ops,
20-
'webgl'
18+
ops
2119
);
2220
return glInstance;
2321
}
2422

2523
registerWebGLBackend();
26-
GLOBALS.registerTypedBackend = registerWebGLBackend;
2724

2825
export {
2926
glInstance,

packages/paddlejs-benchmark/src/config.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export default Vue.extend({
224224
remainWholeT += t;
225225
// this.remainOthersT = +(remainWholeT / (curTimes - 2).toFixed(4));
226226
227-
const queryList = GLOBALS[GLOBALS.backend].backendInstance.queryList;
227+
const queryList = GLOBALS.backendInstance.queryList;
228228
229229
230230
if (queryList && queryList.length) {
@@ -344,4 +344,4 @@ export default Vue.extend({
344344
.el-table .detail-index-row {
345345
background-color: #f0f9eb;
346346
}
347-
</style>
347+
</style>

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

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -58,45 +58,3 @@ describe('test runnrer', () => {
5858
expect(res).toBeUndefined();
5959
});
6060
});
61-
62-
describe('test three runnrer', () => {
63-
it('test runner init', async () => {
64-
const runner1 = new Runner({
65-
modelPath,
66-
feedShape: {
67-
fw: 5,
68-
fh: 3
69-
},
70-
fileCount: 0
71-
});
72-
73-
const runner2 = new Runner({
74-
modelPath,
75-
feedShape: {
76-
fw: 6,
77-
fh: 3
78-
},
79-
fileCount: 0
80-
});
81-
82-
const runner3 = new Runner({
83-
modelPath,
84-
feedShape: {
85-
fw: 7,
86-
fh: 3
87-
},
88-
fileCount: 0
89-
});
90-
registerBackend(
91-
'webgpu',
92-
new Backend(),
93-
ops as any
94-
);
95-
await runner1.init();
96-
expect(runner1.weightMap.length).toBe(7);
97-
await runner2.init();
98-
expect(runner2.weightMap.length).toBe(7);
99-
await runner3.init();
100-
expect(runner3.weightMap.length).toBe(7);
101-
});
102-
});

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.2",
3+
"version": "2.0.3",
44
"description": "",
55
"main": "lib/index",
66
"scripts": {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export interface OpData {
9090
program: any;
9191
renderData: any[];
9292
tensorData: any[];
93+
modelName: string;
9394
}
9495

9596

packages/paddlejs-core/src/globals.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ interface GLOBALS_INTERFACE {
88
opRegistry: OpRegistry;
99
backend: string;
1010
backendInstance: any; // todo Class Backend
11-
backendType: string;
12-
backendCount: number;
13-
registerTypedBackend?: Function;
1411
}
1512

1613
let GLOBALS: GLOBALS_INTERFACE = {
1714
opRegistry: {
1815
ops: {}
1916
},
2017
backend: '',
21-
backendInstance: null,
22-
backendType: '',
23-
backendCount: 0
18+
backendInstance: null
2419
};
2520

2621
function registerOp(opInfo: OpInfo, key: string) {
@@ -47,18 +42,13 @@ function registerOp(opInfo: OpInfo, key: string) {
4742
};
4843
}
4944

50-
function registerBackend(backend: string, backendInstance: any, ops: Ops, backendType?: string) {
45+
function registerBackend(backend: string, backendInstance: any, ops: Ops) {
5146
if (backend) {
5247
GLOBALS.backend = backend;
53-
GLOBALS.backendType = backendType || backend;
54-
}
55-
56-
if (!GLOBALS[GLOBALS.backend]) {
57-
GLOBALS[GLOBALS.backend] = {};
5848
}
5949

6050
if (backendInstance) {
61-
GLOBALS[GLOBALS.backend].backendInstance = backendInstance;
51+
GLOBALS.backendInstance = backendInstance;
6252
}
6353

6454
if (ops) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ export default class OpData {
2424
renderData: object[] = [];
2525
tensorData: ModelVar[] = [];
2626
isFinalOp: boolean = false;
27+
modelName: string;
2728

28-
constructor(op: OpExecutor, iLayer: number, vars: ModelVar[], isFinalOp: boolean) {
29+
constructor(op: OpExecutor, iLayer: number, vars: ModelVar[], isFinalOp: boolean, modelName: string) {
2930
const {
3031
type,
3132
inputs,
@@ -34,6 +35,7 @@ export default class OpData {
3435
isPacked
3536
} = op;
3637

38+
this.modelName = modelName;
3739
this.attrs = attrs;
3840
this.subAttrs = op.subAttrs;
3941
this.name = type;
@@ -146,8 +148,7 @@ export default class OpData {
146148
buildProgram() {
147149
const name = this.name;
148150
const inputTensors = this.inputTensors;
149-
const backend = GLOBALS.backend;
150-
this.program = this.outputTensors.map((outTensor, index) => GLOBALS[backend]?.backendInstance.createProgram({
151+
this.program = this.outputTensors.map((outTensor, index) => GLOBALS.backendInstance.createProgram({
151152
name,
152153
outTensor,
153154
inputTensors,
@@ -192,13 +193,13 @@ export default class OpData {
192193
tensorData.forEach((data: ModelVar, index: number) => {
193194
const tensorName = data.tensorName as string;
194195
const tensor = new Tensor({
195-
type: data.name,
196+
type: this.modelName + '_' + data.name,
196197
name: tensorName,
197198
shape: data.shape,
198199
data: data.data || null,
199200
isPacked: this.isPackedOp || false,
200201
binding: index,
201-
noLayout: GLOBALS[GLOBALS.backend].backendInstance?.noLayout
202+
noLayout: GLOBALS.backendInstance?.noLayout
202203
});
203204
if (tensorName === 'out') {
204205
this.outputTensors.push(tensor);

0 commit comments

Comments
 (0)