Skip to content

Commit

Permalink
feat: add paddlejs-core
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangjingyuan02 committed Nov 25, 2020
1 parent 305e018 commit b8ea8e8
Show file tree
Hide file tree
Showing 20 changed files with 1,861 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.d.ts
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

.DS_Store
node_modules
dist
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "paddlejs",
"scripts": {
"test": "jest",
"lint": "eslint packages/**/*.ts",
"lint": "eslint packages/**/*.ts --fix",
"changelog": "conventional-changelog -p eslint -i CHANGELOG.md -s -r 0"
},
"devDependencies": {
Expand Down
Empty file.
3 changes: 2 additions & 1 deletion packages/paddlejs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "paddlejs-core",
"version": "0.0.1",
"description": "",
"main": "index.ts",
"main": "src/index.ts",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
Expand Down
91 changes: 91 additions & 0 deletions packages/paddlejs-core/src/commons/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
export interface ModelOp {
type: string;
attrs?: OpAttrs;
'sub-attrs'?: OpAttrs[];
inputs: OpInputs;
outputs: OpOutputs;
}

export interface ModelVar {
name: string;
shape: number[];
data?: number[] | Float32Array;
persistable?: boolean;
needBatch?: boolean;
tensorName?: string;
}

export interface Model {
ops: ModelOp[];
vars: ModelVar[];
}

export interface OpInputs {
[key: string]: any[];
}

export interface OpOutputs {
[key: string]: any[]
}

export interface OpAttrs {
[key: string]: any[]
}

export interface OpExecutor {
id: string;
type: string;
inputs: OpInputs;
outputs: OpOutputs;
attrs: OpAttrs;
subAttrs: OpAttrs[];
next: string | null;
opData: OpData;
isPacked: boolean;
finish: boolean;
inputsName: string[];
outputsName: string[];
execute: Function;
}

export interface Tensor {

}

interface Behavior {
[key: string]: Function;
}

export interface OpInfo {
name: string;
conf: object;
inputsName: string[];
outputsName: string[];
params: string;
main: string;
main_packed?: string;
behavior?: Behavior;
behaviors?: string[]
}


export interface OpData {
name: string;
isPackedOp: boolean;
input: OpInputs;
output: OpOutputs;
attrs: any;
data: AttrsData;
inputTensors: any;
outputTensors: any;
fShaderParams: any;
vars: ModelVar[];
iLayer: number;
program: any;
renderData: any[];
}


export interface AttrsData {
[key: string]: any
}
27 changes: 27 additions & 0 deletions packages/paddlejs-core/src/dataProcess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file 数据处理
*/


export default {
// 将nhwc排布数据转为nchw排布数据
nhwc2nchw(data: number[] | Float32Array, shape: number[]) {
const N = shape[0];
const H = shape[1];
const W = shape[2];
const C = shape[3];
const WXC = W * C;
const HXWXC = H * W * C;
const nchwData: number[] | Float32Array = [];
for (let n = 0; n < N; n++) {
for (let c = 0; c < C; c++) {
for (let h = 0; h < H; h++) {
for (let w = 0; w < W; w++) {
nchwData.push(data[n * HXWXC + h * WXC + w * C + c]);
}
}
}
}
return nchwData;
}
};
74 changes: 74 additions & 0 deletions packages/paddlejs-core/src/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { OpInfo } from './commons/interface';

interface OpRegistry {
ops: {
// key => backend_name
[key: string]: OpInfo;
};
opsBehavior?: {
// key => backend_hehavior
[key: string]: Function;
};
}

interface GLOBALS_INTERFACE {
opRegistry: OpRegistry;
backend: string;
backend_version: number;
backend_instance: any; // todo Class Backend
}

export const GLOBALS: GLOBALS_INTERFACE = {
opRegistry: {
ops: {}
},
backend: 'webgl',
backend_version: 2,
backend_instance: null
};


export function registerOpsBehaviors(opsBehavior) {
GLOBALS.opRegistry.opsBehavior = opsBehavior;
}

export function registerOp(opInfo: OpInfo) {
const {
name,
conf,
params,
main,
inputsName,
outputsName,
main_packed = '',
behaviors = []
} = opInfo;

const opKey = `${GLOBALS.backend}_${name}`;
if (GLOBALS.opRegistry.ops[opKey]) {
return;
}

GLOBALS.opRegistry.ops[opKey] = {
name,
conf,
inputsName,
outputsName,
params,
main,
main_packed,
behaviors
};
}

export function registerBackend(backend: string, backendInstance: any, version?: number) {
if (backend) {
GLOBALS.backend = backend;
}
if (version) {
GLOBALS.backend_version = version;
}
if (backendInstance) {
GLOBALS.backend_instance = backendInstance;
}
}
Loading

0 comments on commit b8ea8e8

Please sign in to comment.