Skip to content

Commit 516ea79

Browse files
committed
support wasm in javascript library
1 parent aa80c56 commit 516ea79

File tree

133 files changed

+33063
-1605
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+33063
-1605
lines changed

.prettierrc.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"useTabs": true
2+
"arrowParens": "avoid",
3+
"bracketSpacing": true,
4+
"jsxBracketSameLine": false,
5+
"semi": false,
6+
"trailingComma": "all",
7+
"useTabs": true
38
}

Cargo.lock

Lines changed: 55 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
resolver = "2"
33
members = [
44
"crates/*",
5-
"crates/www/pages/**/server",
6-
"crates/www/pages/**/client",
75
"languages/c",
86
"languages/elixir",
9-
"languages/node",
7+
"languages/javascript/node",
8+
"languages/javascript/wasm",
109
"languages/python",
1110
"languages/rust",
11+
"languages/rust/examples/*",
1212
]
1313

1414
[profile.dev.build-override]

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Tangram is an automated machine learning framework designed for programmers.
88

99
- Run `tangram train` to train a model from a CSV file on the command line.
10-
- Make predictions with libraries for [Elixir](https://hex.pm/packages/tangram), [Go](https://pkg.go.dev/github.com/tangramxyz/tangram-go), [Node.js](https://www.npmjs.com/package/@tangramxyz/tangram-node), [Python](https://pypi.org/project/tangram), [Ruby](https://rubygems.org/gems/tangram), and [Rust](lib.rs/tangram).
10+
- Make predictions with libraries for [Elixir](https://hex.pm/packages/tangram), [Go](https://pkg.go.dev/github.com/tangramxyz/tangram-go), [JavaScript](https://www.npmjs.com/package/@tangramxyz/tangram), [Python](https://pypi.org/project/tangram), [Ruby](https://rubygems.org/gems/tangram), and [Rust](lib.rs/tangram).
1111
- Run `tangram app` to learn more about your models and monitor them in production.
1212

1313
### Install
@@ -30,21 +30,21 @@ The CLI automatically transforms your data into features, trains a number of mod
3030

3131
### Predict
3232

33-
Make predictions with libraries for [Elixir](https://hex.pm/packages/tangram), [Go](https://pkg.go.dev/github.com/tangramxyz/tangram-go), [Node.js](https://www.npmjs.com/package/@tangramxyz/tangram-node), [Python](https://pypi.org/project/tangram), [Ruby](https://rubygems.org/gems/tangram), and [Rust](https://lib.rs/tangram).
33+
Make predictions with libraries for [Elixir](https://hex.pm/packages/tangram), [Go](https://pkg.go.dev/github.com/tangramxyz/tangram-go), [JavaScript](https://www.npmjs.com/package/@tangramxyz/tangram), [Python](https://pypi.org/project/tangram), [Ruby](https://rubygems.org/gems/tangram), and [Rust](https://lib.rs/tangram).
3434

3535
```javascript
36-
let tangram = require("@tangramxyz/tangram-node");
36+
let tangram = require("@tangramxyz/tangram")
3737

38-
let model = new tangram.Model("./heart_disease.tangram");
38+
let model = new tangram.Model("./heart_disease.tangram")
3939

4040
let input = {
4141
age: 63,
4242
gender: "male",
4343
// ...
44-
};
44+
}
4545

46-
let output = model.predictSync(input);
47-
console.log(output);
46+
let output = model.predict(input)
47+
console.log(output)
4848
```
4949

5050
```javascript
@@ -70,7 +70,7 @@ model.logPrediction({
7070
input,
7171
options,
7272
output,
73-
});
73+
})
7474
```
7575

7676
Later on, if you find out the true value for a prediction, call `logTrueValue`.
@@ -80,7 +80,7 @@ Later on, if you find out the true value for a prediction, call `logTrueValue`.
8080
model.logTrueValue({
8181
identifier: "6c955d4f-be61-4ca7-bba9-8fe32d03f801",
8282
trueValue: "Positive",
83-
});
83+
})
8484
```
8585

8686
Now you can:

crates/app/pages/track/server/post.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ pub async fn post(request: &mut http::Request<hyper::Body>) -> Result<http::Resp
3131
let context = request.extensions().get::<Arc<Context>>().unwrap().clone();
3232
let bytes = match hyper::body::to_bytes(request.body_mut()).await {
3333
Ok(bytes) => bytes,
34-
Err(_) => return Ok(bad_request()),
34+
Err(e) => {
35+
error!(%e);
36+
return Ok(bad_request());
37+
}
3538
};
3639
let monitor_events: MonitorEventSet = match serde_json::from_slice(&bytes) {
3740
Ok(monitor_events) => monitor_events,
@@ -59,7 +62,8 @@ pub async fn post(request: &mut http::Request<hyper::Body>) -> Result<http::Resp
5962
monitor_event,
6063
)
6164
.await;
62-
if handle_prediction_result.is_err() {
65+
if let Err(e) = handle_prediction_result {
66+
error!(%e);
6367
return Ok(bad_request());
6468
}
6569
}

crates/build/build_pkgs.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ fn alpine(
119119
arch = match arch {
120120
Arch::X8664 => "x86_64",
121121
Arch::AArch64 => "aarch64",
122+
Arch::Wasm32 => unreachable!(),
122123
},
123124
);
124125
std::fs::write(&apkbuild_path, &apkbuild).unwrap();
125126
let tangram_cli_dst_path = repo_path.join("tangram");
126127
let target = match arch {
127128
Arch::X8664 => Target::X8664UnknownLinuxGnu,
128129
Arch::AArch64 => Target::AArch64UnknownLinuxGnu,
130+
Arch::Wasm32 => unreachable!(),
129131
};
130132
let tangram_cli_path = dist_path
131133
.join(target.as_str())
@@ -204,7 +206,11 @@ fn deb(
204206
let _ = components.next().unwrap();
205207
let version = components.next().unwrap().to_owned();
206208
let arch = components.next().unwrap().to_owned();
207-
debs.push(Deb { arch, version, path });
209+
debs.push(Deb {
210+
arch,
211+
version,
212+
path,
213+
});
208214
}
209215
let distributions = &["debian", "ubuntu"];
210216
let debian_versions = vec!["sid", "bullseye", "buster", "stretch"];

crates/build/compile.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ pub fn run(args: Args) {
5555
Target::X8664PcWindowsGnu => {
5656
build_gnu(target, Some(vec!["g++-mingw-w64-x86-64".to_owned()]));
5757
}
58+
Target::Wasm32UnknownUnknown => {
59+
build_wasm(target);
60+
}
61+
}
62+
63+
if matches!(target, Target::Wasm32UnknownUnknown) {
64+
let cargo_artifact_path = tangram_path
65+
.join("target")
66+
.join(target.as_str())
67+
.join("release");
68+
let tangram_wasm_artifact_path = cargo_artifact_path.join("tangram_wasm.wasm");
69+
std::fs::copy(
70+
tangram_wasm_artifact_path,
71+
dist_target_path.join("tangram_wasm.wasm"),
72+
)
73+
.unwrap();
74+
return;
5875
}
5976

6077
eprintln!("generating tangram.h");
@@ -105,6 +122,7 @@ pub fn run(args: Args) {
105122
cargo_artifact_path_dynamic.join(target_file_names.tangram_node_src_file_name),
106123
)
107124
}
125+
Target::Wasm32UnknownUnknown => unreachable!(),
108126
};
109127
std::fs::copy(
110128
tangram_cli_artifact_path,
@@ -142,6 +160,7 @@ pub fn run(args: Args) {
142160
Target::AArch64AppleDarwin => build_python_macos(),
143161
Target::X8664PcWindowsMsvc => build_python_windows(),
144162
Target::X8664PcWindowsGnu => {}
163+
Target::Wasm32UnknownUnknown => unreachable!(),
145164
}
146165

147166
// Move the python wheels to the dist target path.
@@ -165,6 +184,7 @@ pub fn run(args: Args) {
165184
Target::X8664UnknownLinuxMusl
166185
| Target::AArch64UnknownLinuxMusl
167186
| Target::X8664PcWindowsGnu => {}
187+
Target::Wasm32UnknownUnknown => unreachable!(),
168188
}
169189
}
170190

@@ -197,6 +217,7 @@ fn build_gnu(target: Target, apt_packages: Option<Vec<String>>) {
197217
let docker_platform = match arch {
198218
Arch::X8664 => "linux/amd64",
199219
Arch::AArch64 => "linux/arm64",
220+
Arch::Wasm32 => unreachable!(),
200221
};
201222
let apt_packages = apt_packages
202223
.map(|apt_packages| apt_packages.join(" "))
@@ -259,6 +280,7 @@ fn build_musl(target: Target) {
259280
let docker_platform = match arch {
260281
Arch::X8664 => "linux/amd64",
261282
Arch::AArch64 => "linux/arm64",
283+
Arch::Wasm32 => unreachable!(),
262284
};
263285
let script = format!(
264286
r#"
@@ -323,6 +345,7 @@ fn build_python_manylinux(arch: Arch) {
323345
let docker_platform = match arch {
324346
Arch::X8664 => "linux/amd64",
325347
Arch::AArch64 => "linux/arm64",
348+
Arch::Wasm32 => unreachable!(),
326349
};
327350
let script = r#"
328351
set -ex
@@ -431,3 +454,17 @@ fn build_python_windows() {
431454
.run()
432455
.unwrap();
433456
}
457+
458+
fn build_wasm(target: Target) {
459+
cmd!(
460+
which("cargo").unwrap(),
461+
"build",
462+
"--release",
463+
"--target",
464+
target.as_str(),
465+
"--package",
466+
"tangram_wasm",
467+
)
468+
.run()
469+
.unwrap();
470+
}

crates/build/distribute_native_extensions.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ pub fn run(_args: Args) {
8383
.unwrap();
8484
}
8585

86-
eprintln!("node");
87-
let node_dist_path = tangram_path.join("languages/node/native");
86+
eprintln!("javascript");
87+
let node_dist_path = tangram_path.join("languages/javascript/node/dist");
8888
if std::fs::metadata(&node_dist_path)
8989
.map(|m| m.is_dir())
9090
.unwrap_or(false)
@@ -111,6 +111,20 @@ pub fn run(_args: Args) {
111111
)
112112
.unwrap();
113113
}
114+
let wasm_dist_path = tangram_path.join("languages/javascript/wasm/dist");
115+
if std::fs::metadata(&wasm_dist_path)
116+
.map(|m| m.is_dir())
117+
.unwrap_or(false)
118+
{
119+
std::fs::remove_dir_all(&wasm_dist_path).unwrap();
120+
}
121+
install(
122+
&dist_path
123+
.join("wasm32-unknown-unknown")
124+
.join("tangram_wasm.wasm"),
125+
&wasm_dist_path.join("tangram_wasm.wasm"),
126+
)
127+
.unwrap();
114128

115129
eprintln!("python");
116130
let python_dist_path = tangram_path.join("languages/python/dist");

0 commit comments

Comments
 (0)