Skip to content
This repository was archived by the owner on Aug 15, 2019. It is now read-only.

Add skeleton for the WASM prototype #1863

Merged
merged 20 commits into from
Aug 7, 2019
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ bazel-out/
yalc.lock
.rpt2_cache/
package/

tfjs-backend-wasm/dist
tfjs-backend-wasm/wasm-out
20 changes: 20 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"~/emsdk/fastcomp/emscripten/system/include/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"**/.rpt2_cache/": true,
"**/.yalc/**/*": true
},
"tslint.enable": true,
"tslint.run": "onType",
"tslint.configFile": "tslint.json",
"files.trimTrailingWhitespace": true,
"editor.tabSize": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"**/.rpt2_cache/": true,
"**/.yalc/**/*": true
},
"tslint.enable": true,
"tslint.run": "onType",
"tslint.configFile": "tslint.json",
"files.trimTrailingWhitespace": true,
"editor.tabSize": 2,
Expand Down
1 change: 1 addition & 0 deletions tfjs-backend-wasm/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
17 changes: 17 additions & 0 deletions tfjs-backend-wasm/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/src/**",
"~/emsdk/fastcomp/emscripten/system/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
36 changes: 36 additions & 0 deletions tfjs-backend-wasm/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Place your settings in this file to overwrite default and user settings.
{
"search.exclude": {
"**/node_modules": true,
"**/coverage/": true,
"**/dist/": true,
"**/yarn.lock": true,
"**/.rpt2_cache/": true,
"**/.yalc/**/*": true
},
"tslint.configFile": "tslint.json",
"files.trimTrailingWhitespace": true,
"editor.tabSize": 2,
"editor.insertSpaces": true,
"[typescript]": {
"editor.formatOnSave": true
},
"[javascript]": {
"editor.formatOnSave": true
},
"[cpp]": {
"editor.formatOnSave": true
},
"editor.rulers": [80],
"clang-format.style": "Google",
"files.insertFinalNewline": true,
"editor.detectIndentation": false,
"editor.wrappingIndent": "none",
"typescript.tsdk": "./node_modules/typescript/lib",
"clang-format.executable": "${workspaceRoot}/node_modules/.bin/clang-format",
"files.associations": {
"vector": "cpp",
"map": "cpp",
"algorithm": "cpp"
}
}
22 changes: 22 additions & 0 deletions tfjs-backend-wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Emscripten installation

Install emscripten [here](https://emscripten.org/docs/getting_started/downloads.html)


# Building

```sh
yarn build
```

# Testing

```sh
yarn test
```

# Deployment
```sh
./scripts/build-npm.sh
npm publish
```
72 changes: 72 additions & 0 deletions tfjs-backend-wasm/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

const karmaTypescriptConfig = {
tsconfig: 'tsconfig.json',
compilerOptions: {allowJs: true, declaration: false},
bundlerOptions: {
sourceMap: true,
// Ignore the import of the `worker_threads` package used in a core test
// meant to run in node.
exclude: ['worker_threads']
},
// Disable coverage reports and instrumentation by default for tests
coverageOptions: {instrumentation: false},
reports: {},
include: ['src/', 'wasm-out/']
};

module.exports = function(config) {
const args = [];
if (config.grep) {
args.push('--grep', config.grep);
}
if (config.flags) {
args.push('--flags', config.flags);
}
config.set({
basePath: '',
frameworks: ['jasmine', 'karma-typescript'],
files: [
// Setup the environment for the tests.
'src/setup_test.ts',
// Serve the wasm file as a static resource.
{pattern: 'wasm-out/**/*.wasm', included: false},
// Import the generated js library from emscripten.
{pattern: 'wasm-out/**/*.js'},
// Import the rest of the sources.
{pattern: 'src/**/*.ts'},
],
preprocessors: {
'wasm-out/**/*.js': ['karma-typescript'],
'**/*.ts': ['karma-typescript']
},
karmaTypescriptConfig,
// Redirect the request for the wasm file so karma can find it.
proxies: {
'/base/node_modules/karma-typescript/dist/client/tfjs-backend-wasm.wasm':
'/base/wasm-out/tfjs-backend-wasm.wasm',
},
reporters: ['progress', 'karma-typescript'],
port: 9876,
colors: true,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
client: {jasmine: {random: false}, args: args}
})
}
41 changes: 41 additions & 0 deletions tfjs-backend-wasm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@tensorflow/tfjs-backend-wasm",
"version": "0.0.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"jsnext:main": "dist/tf-wasm.esm.js",
"module": "dist/tf-wasm.esm.js",
"unpkg": "dist/tf-wasm.min.js",
"jsdelivr": "dist/tf-wasm.min.js",
"scripts": {
"build": "rimraf dist/ && ./scripts/build-wasm.sh && tsc && cp wasm-out/*.wasm dist/",
"lint": "tslint -p . -t verbose",
"test": "./scripts/build-wasm.sh && karma start"
},
"peerDependencies": {
"@tensorflow/tfjs-core": "~1.2.7"
},
"devDependencies": {
"@tensorflow/tfjs-core": "~1.2.7",
"@types/emscripten": "~0.0.34",
"clang-format": "^1.2.4",
"jasmine-core": "~3.1.0",
"karma": "~4.0.0",
"karma-browserstack-launcher": "~1.4.0",
"karma-chrome-launcher": "~2.2.0",
"karma-firefox-launcher": "~1.1.0",
"karma-jasmine": "~1.1.1",
"karma-typescript": "~4.0.0",
"rimraf": "~2.6.2",
"rollup": "^1.17.0",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.1",
"rollup-plugin-typescript2": "^0.22.1",
"tslint": "~5.11.0",
"tslint-no-circular-imports": "^0.5.0",
"typescript": "3.3.3333",
"yalc": "~1.0.0-pre.21"
},
"license": "Apache-2.0"
}
110 changes: 110 additions & 0 deletions tfjs-backend-wasm/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import commonjs from 'rollup-plugin-commonjs';
import node from 'rollup-plugin-node-resolve';
import {terser} from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';

const PREAMBLE = `/**
* @license
* Copyright ${(new Date).getFullYear()} Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/`;

function config({plugins = [], output = {}}) {
return {
input: 'src/index.ts',
plugins: [
typescript({
tsconfigOverride: {compilerOptions: {module: 'ES2015'}},
}),
node(),
// Polyfill require() from dependencies.
commonjs({
ignore: ['crypto', 'node-fetch', 'util'],
include: ['node_modules/**', 'wasm-out/**']
}),
...plugins
],
output: {
banner: PREAMBLE,
sourcemap: true,
globals: {'@tensorflow/tfjs-core': 'tf'},
...output,
},
external: ['crypto', '@tensorflow/tfjs-core'],
onwarn: warning => {
let {code} = warning;
if (code === 'CIRCULAR_DEPENDENCY' || code === 'CIRCULAR' ||
code === 'THIS_IS_UNDEFINED') {
return;
}
console.warn('WARNING: ', warning.message);
}
};
}

module.exports = cmdOptions => {
const bundles = [];

if (!cmdOptions.ci) {
// tf-backend-wasm.js
bundles.push(config({
output: {
format: 'umd',
name: 'tf',
extend: true,
file: 'dist/tf-backend-wasm.js',
}
}));
}

// tf-backend-wasm.min.js
bundles.push(config({
plugins: [terser({output: {preamble: PREAMBLE}})],
output: {
format: 'umd',
name: 'tf',
extend: true,
file: 'dist/tf-backend-wasm.min.js',
},
}));

if (!cmdOptions.ci) {
// tf-backend-wasm.esm.js
bundles.push(config({
plugins: [terser({output: {preamble: PREAMBLE}})],
output: {
format: 'es',
file: 'dist/tf-backend-wasm.esm.js',
}
}));
}
return bundles;
};
24 changes: 24 additions & 0 deletions tfjs-backend-wasm/scripts/build-npm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# Copyright 2019 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================

set -e

yarn rimraf dist/
yarn
yarn build
yarn rollup -c

echo "Stored standalone library at dist/tf-backend-wasm(.min).js"
Loading