Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tvm wasm export #582

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 5 additions & 57 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- master
- debug/tvm
paths-ignore:
- 'docker/**'
- 'docs/**'
Expand Down Expand Up @@ -67,67 +68,14 @@ jobs:
yarn build
yarn init-dev
du -h -d 1
- name: Running unit tests
run: |
npm test
- name: run cov
if: ${{ runner.os == 'Linux' && matrix.node_version == 12 }}
run: |
npm run cov
- name: Coveralls
if: ${{ runner.os == 'Linux' && matrix.node_version == 12 }}
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: packages/daemon/coverage/lcov.info
- name: migration
if: ${{ runner.os == 'Linux' }}
run: |
mkdir -p ~/.pipcook/db && cp ./packages/daemon/test/res/baseline-v1.db ~/.pipcook/db/pipcook.db
- name: daemon start
run: |
./packages/cli/dist/bin/pipcook daemon start
- name: Testing help commands
run: |
./packages/cli/dist/bin/pipcook -v
./packages/cli/dist/bin/pipcook -h
./packages/cli/dist/bin/pipcook init --help
./packages/cli/dist/bin/pipcook run --help
./packages/cli/dist/bin/pipcook plugin-dev --help
./packages/cli/dist/bin/pipcook serve --help
- name: pipcook sdk test
run: |
npx ts-node ./run_tests.ts
- name: install plugins from specific pipeline
run: |
python3 --version
python --version
which python
echo $PATH
./packages/cli/dist/bin/pipcook pipeline install ./example/pipelines/chinese-poem-creation.json
./packages/cli/dist/bin/pipcook pipeline install https://raw.githubusercontent.com/alibaba/pipcook/master/example/pipelines/text-bayes-classification.json
- name: Running an example pipeline
run: |
yarn test:pipeline -- text-bayes-classification
- name: install a new plugin and install a installed plugin
run: |
./packages/cli/dist/bin/pipcook plugin install @pipcook/plugins-chinese-poem-data-collect
./packages/cli/dist/bin/pipcook plugin install @pipcook/plugins-chinese-poem-data-collect
- name: install plugin from local project
run: |
./packages/cli/dist/bin/pipcook plugin install ./packages/costa/test/plugins/nodejs-simple
npm pack ./packages/costa/test/plugins/nodejs-simple
./packages/cli/dist/bin/pipcook plugin install ./nodejs-simple-1.0.0.tgz
rm ./nodejs-simple-1.0.0.tgz
- name: pipcook run from local filename
run: |
./packages/cli/dist/bin/pipcook run ./example/pipelines/text-bayes-classification.json
ls ./output && rm -rf ./output
./packages/cli/dist/bin/pipcook run ./example/pipelines/text-bayes-classification.json --output mydir
ls ./mydir && rm -rf ./mydir
- name: pipcook run from url
run: |
./packages/cli/dist/bin/pipcook run https://raw.githubusercontent.com/alibaba/pipcook/master/example/pipelines/text-bayes-classification.json
ls ./output && rm -rf ./output
- name: remove pipelines
run: |
./packages/cli/dist/bin/pipcook pipeline remove all -y
- name: "pipcook plugin-dev"
run: |
./packages/cli/dist/bin/pipcook plugin-dev -t dataCollect
6 changes: 6 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ jobs:
./packages/cli/dist/bin/pipcook daemon start
- name: pipeline
run: yarn test:pipeline -- ${{ matrix.pipeline }}
- name: wasm export pipeline
run: |
git clone https://github.com/emscripten-core/emsdk.git && cd emsdk
./emsdk install latest && ./emsdk activate latest && source ./emsdk_env.sh
./packages/cli/dist/bin/pipcook daemon stop
export WASM=1 && ./packages/cli/dist/bin/pipcook run ./example/pipelines/databinding-image-classification-mobilenet.json
2 changes: 2 additions & 0 deletions docs/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Before starting the installation, please make sure the following environments ar
- macOS, Linux
- Node.js 12

**Note:** To use `wasm` output format, you need to manually install [`emsdk`](https://emscripten.org/docs/introducing_emscripten/index.html) and export the `emcc` and `emsdk` to the environmental variable.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we include the emcc/emsdk inside Pipcook?


## Install via NPM

Installing [Pipcook][] via NPM is easy, just run:
Expand Down
1 change: 1 addition & 0 deletions packages/boa/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ function _internalWrap(T, src={}) {
writable: false,
value: () => T.__hash__(),
},

/**
* @method [PyGetAttrSymbol]
* @public
Expand Down
5 changes: 4 additions & 1 deletion packages/costa/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export class CostaRuntime {
}
const stdio = { stdout: opts.stdout, stderr: opts.stderr, prefix: 'NODE' };
const npmExecOpts = { cwd: this.options.installDir };
const npmArgs = [ 'install', pluginAbsName, '-E', '--production' ];
const npmArgs = [ 'install', pluginAbsName, '-E', '--production', '--silly' ];

if (this.options.npmRegistryPrefix) {
npmArgs.push(`--registry=${this.options.npmRegistryPrefix}`);
Expand All @@ -328,6 +328,9 @@ export class CostaRuntime {
// if not init for plugin directory, just run `npm init` and install boa firstly.
await spawnAsync('npm', [ 'init', '-y' ], npmExecOpts, stdio);
}
spawnAsync('echo', ['$PATH'], npmExecOpts, stdio);
spawnAsync('which', ['python'], npmExecOpts, stdio);
spawnAsync('node', ['-e', 'console.log(process.env)'], npmExecOpts, stdio);
return spawnAsync('npm', npmArgs, npmExecOpts, stdio);
}

Expand Down
Empty file modified packages/daemon/bootstrap.js
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions packages/daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "pipcook daemon",
"dependencies": {
"@pipcook/app": "^1.2.0",
"@pipcook/boa": "^1.2.0",
"@pipcook/costa": "^1.2.0",
"@pipcook/pipcook-core": "^1.2.0",
"axios": "^0.18.1",
Expand Down
121 changes: 100 additions & 21 deletions packages/daemon/src/service/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class PipelineService {
const verifyPlugin = (name: string): void => {
if (!plugins[name]) {
this.runnableMap[job.id].destroy();
throw new TypeError(`"${name}" plugin is required`);
throw new TypeError(`'${name}' plugin is required`);
}
};
const dispatchJobEvent = (jobStatus: PipelineStatus, step?: PluginTypeI, stepAction?: 'start' | 'end') => {
Expand Down Expand Up @@ -271,7 +271,7 @@ export class PipelineService {
datasetProcess,
pipeline,
workingDir: runnable.workingDir,
template: 'node' // set node by default
template: process.env.WASM ? 'wasm' : 'node' // set node by default
});

await JobModel.saveJob(job);
Expand Down Expand Up @@ -317,6 +317,66 @@ export class PipelineService {
return path.join(CoreConstants.PIPCOOK_RUN, id, 'output.tar.gz');
}

// private _generateWASMOutput(dist: string, opts: GenerateOptions, fileQueue: Array<Promise<void | string>>): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WenheLI @FeelyChau Boa is working with worker_threads at #602, this function could be reimplemented with worker_threads to avoid blocking the server.

// const relay = boa.import('tvm.relay');
// const emcc = boa.import('tvm.contrib.emcc');
// const keras = boa.import('tensorflow.keras');
// const {dict, open} = boa.builtins();

// // download tvm runtime from oss
// const tvmjsPromise = execAsync(`wget http://ai-sample.oss-cn-hangzhou.aliyuncs.com/tvmjs/dist/tvmjs.bundle.js`, {cwd: dist});
// fileQueue.push(tvmjsPromise);

// const model = keras.models.load_model(path.join(opts.modelPath, 'model.h5'));

// const inputName = 'input_1';
// const inputShape = model.layers[0].input_shape[0];
// const shape = [1];
// shape.push(inputShape[3]);
// shape.push(inputShape[1]);
// shape.push(inputShape[2]);

// const [ mod, params ] = relay.frontend.from_keras(model, dict(boa.kwargs({[inputName]: shape})));
// const [ graph, lib, param ] = relay.build(mod, boa.kwargs({
// params,
// target: 'llvm -mtriple=wasm32--unknown-emcc -system-lib'
// }));

// lib.save(path.join(dist, 'model.bc'));

// const jsonWriter = open(path.join(dist, 'modelDesc.json'), 'w');
// jsonWriter.write(graph);
// const paramWriter = open(path.join(dist, 'modelParams.parmas'), 'wb');
// paramWriter.write(relay.save_param_dict(param));
// emcc.create_tvmjs_wasm(path.join(dist, 'model.wasi.js'), path.join(dist, 'model.bc'), boa.kwargs({
// options: ['-O3', '-std=c++14', '-Wno-ignored-attributes', '-s', 'ALLOW_MEMORY_GROWTH=1', '-s', 'STANDALONE_WASM=1', '-s', 'ERROR_ON_UNDEFINED_SYMBOLS=0', '-s', 'ASSERTIONS=1', '--no-entry', '--pre-js', './packages/daemon/binary/preload.js']
// }));

// const templateHead = `function EmccWASI() {`;
// const templateTail = `
// this.Module = Module;
// this.start = Module.wasmLibraryProvider.start;
// this.imports = Module.wasmLibraryProvider.imports;
// this.wasiImport = this.imports['wasi_snapshot_preview1'];
// }

// if (typeof module !== 'undefined' && module.exports) {
// module.exports = EmccWASI;
// }
// `;

// const result = templateHead + open(path.join(dist, 'model.wasi.js')).read() + templateTail;
// const resultWriter = open(path.join(dist, 'model.wasi.js'), 'w');
// resultWriter.write(result);

// const jsonPromise = fs.writeJSON(path.join(dist, 'modelSpec.json'), {
// shape,
// inputName
// });

// fileQueue.push(jsonPromise);
// }

/**
* Generate the output package for a given job.
* @param job the job model for output.
Expand All @@ -327,18 +387,34 @@ export class PipelineService {
const dist = path.join(opts.workingDir, 'output');
await fs.remove(dist);
await fs.ensureDir(dist);

const fileQueue: Array<Promise<void | string>> = new Array();

// Only support tensorflow at this moment.
// if (opts.template === 'wasm' && opts.modelPlugin.name.includes('tensorflow')) {
// this._generateWASMOutput(dist, opts, fileQueue);
// }

await execAsync('npm init -y', { cwd: dist });

// post processing the package.json
const projPackage = await fs.readJSON(dist + '/package.json');
projPackage.dependencies = {
[opts.modelPlugin.name]: opts.modelPlugin.version,
};
projPackage.scripts = {
postinstall: 'node boapkg.js'
};
if (opts.dataProcess) {
projPackage.dependencies[opts.dataProcess.name] = opts.dataProcess.version;

if (opts.template === 'node') {
projPackage.dependencies = {
[opts.modelPlugin.name]: opts.modelPlugin.version,
};
projPackage.scripts = {
postinstall: 'node boapkg.js'
};
if (opts.dataProcess) {
projPackage.dependencies[opts.dataProcess.name] = opts.dataProcess.version;
}
} else {
projPackage.main = 'index.js';
projPackage.dependencies = {
ws: '^7.3.1'
};
}

const jsonWriteOpts = { spaces: 2 } as fs.WriteOptions;
Expand All @@ -347,18 +423,21 @@ export class PipelineService {
output: job,
};

await Promise.all([
if (opts.template === 'node') {
// copy base components
fs.copy(opts.modelPath, dist + '/model'),
fs.copy(path.join(__dirname, `../../templates/${opts.template}/predict.js`), `${dist}/index.js`),
fs.copy(path.join(__dirname, '../../templates/boapkg.js'), `${dist}/boapkg.js`),
// copy logs
fs.copy(opts.workingDir + '/logs', `${dist}/logs`),
// write package.json
fs.outputJSON(dist + '/package.json', projPackage, jsonWriteOpts),
// write metadata.json
fs.outputJSON(dist + '/metadata.json', metadata, jsonWriteOpts),
]);
fileQueue.push(fs.copy(opts.modelPath, dist + '/model'));
fileQueue.push(fs.copy(path.join(__dirname, '../../templates/boapkg.js'), `${dist}/boapkg.js`));
}

fileQueue.push(fs.copy(path.join(__dirname, `../../templates/${opts.template}/predict.js`), `${dist}/index.js`));
// copy logs
fileQueue.push(fs.copy(opts.workingDir + '/logs', `${dist}/logs`));
// write package.json
fileQueue.push(fs.outputJSON(dist + '/package.json', projPackage, jsonWriteOpts));
// write metadata.json
fileQueue.push(fs.outputJSON(dist + '/metadata.json', metadata, jsonWriteOpts));

await Promise.all(fileQueue);
console.info(`trained the model to ${dist}`);

// packing the output directory.
Expand Down
41 changes: 41 additions & 0 deletions packages/daemon/templates/wasm/predict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const tvmjs = require("./tvmjs.bundle");
const EmccWASI = require("./model.wasi");
const fs = require('fs');
const modelSpec = require("./modelSpec.json");

const loadModel = async () => {
const wasmSource = fs.readFileSync('./model.wasi.wasm');
const tvm = await tvmjs.instantiate(wasmSource, new EmccWASI());

const graph = JSON.parse(fs.readFileSync('./modelDesc.json'));
const param = new Uint8Array(fs.readFileSync('./modelParams.parmas'));

const ctx = tvm.cpu(0);
const sysLib = tvm.systemLib();
model = tvm.createGraphRuntime(JSON.stringify(graph), sysLib, ctx);
model.loadParams(param);

return {model, tvm, ctx};
}

let model, tvm, ctx;

const predict = async (input) => {
if (!model) {
const rets = await loadModel();
model = rets.model;
tvm = rets.tvm;
ctx = rets.ctx;
}

const inputData = tvm.empty(modelSpec.shape, "float32", tvm.cpu());
const output = model.getOutput(0);
inputData.copyFrom(input);
model.setInput(modelSpec.inputName, inputData);
model.run();
await ctx.sync();
console.log(output.toArray())
return output.toArray();
}

module.exports = predict;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOF