Skip to content

Commit

Permalink
Use the runtime's RNG in proc-blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jun 15, 2022
1 parent 28a1a65 commit 8aec3b5
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 66 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion accuracy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ description = "calculates accuracy of predicted labels when compared to true lab
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
3 changes: 1 addition & 2 deletions elastic_net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ description = "an extension of linear regression that adds L1 and L2 regularizat
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]

[package.metadata.wapm]
namespace = "hotg-ai"
abi = "none"
abi = "none"
3 changes: 1 addition & 2 deletions f1-score/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ description = "a proc-block used to calculate f1-score"
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]

[package.metadata.wapm]
namespace = "hotg-ai"
abi = "none"
abi = "none"
3 changes: 1 addition & 2 deletions linear_regression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ description = " a linear approach for modelling the relationship between a scala
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]

[package.metadata.wapm]
namespace = "hotg-ai"
abi = "none"
abi = "none"
1 change: 0 additions & 1 deletion logistic_regression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ description = "a statistical model that models the probability of one event taki
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
3 changes: 1 addition & 2 deletions metric/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ description = "for assessing model evaluation and performace of model"
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]

[package.metadata.wapm]
namespace = "hotg-ai"
abi = "none"
abi = "none"
3 changes: 1 addition & 2 deletions prediction_errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ description = "a proc-block to find Mean Absolute Error and Mean Squared Error.
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]

[package.metadata.wapm]
namespace = "hotg-ai"
abi = "none"
abi = "none"
6 changes: 2 additions & 4 deletions support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ edition = "2021"

[dependencies]
bytemuck = "1.9.1"
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"], optional = true }
ndarray = "0.15.4"
once_cell = "1.12.0"
rand = { version = "0.8.5", features = ["small_rng"] }
tracing = { version = "0.1.35", default-features = false, features = ["std"] }
tracing-subscriber = { version = "0.3.11", default-features = false, features = ["registry", "tracing-log"] }
wit-bindgen-rust = { git = "https://github.com/wasmerio/wit-bindgen", branch = "wasmer", version = "0.1.0", optional = true }

[features]
default = ["guest"]
guest = ["wit-bindgen-rust"]
guest = ["wit-bindgen-rust", "getrandom"]
22 changes: 22 additions & 0 deletions support/src/guest/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// Tell the runtime that a WebAssembly module contains a proc-block.
#[macro_export]
macro_rules! export_proc_block {
(metadata: $metadata_func:expr, proc_block: $proc_block:ty $(,)?) => {
#[doc(hidden)]
#[no_mangle]
pub fn __proc_block_metadata() -> $crate::guest::Metadata { $metadata_func() }

#[doc(hidden)]
#[no_mangle]
pub fn __proc_block_new(
args: Vec<$crate::guest::Argument>,
) -> Result<Box<dyn $crate::guest::ProcBlock>, $crate::guest::CreateError> {
fn assert_impl_proc_block(_: &impl $crate::guest::ProcBlock) {}

let proc_block = <$proc_block>::try_from(args)?;
assert_impl_proc_block(&proc_block);

Ok(Box::new(proc_block) as Box<dyn $crate::guest::ProcBlock>)
}
};
}
27 changes: 7 additions & 20 deletions support/src/guest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Types and utilities for implementing a proc-block.

#[macro_use]
mod macros;

pub(crate) mod bindings;
mod element_type;
mod errors;
Expand All @@ -22,25 +25,9 @@ pub use self::{
proc_block::ProcBlock,
};

/// Tell the runtime that a WebAssembly module contains a proc-block.
#[macro_export]
macro_rules! export_proc_block {
(metadata: $metadata_func:expr, proc_block: $proc_block:ty $(,)?) => {
#[doc(hidden)]
#[no_mangle]
pub fn __proc_block_metadata() -> $crate::guest::Metadata { $metadata_func() }

#[doc(hidden)]
#[no_mangle]
pub fn __proc_block_new(
args: Vec<$crate::guest::Argument>,
) -> Result<Box<dyn $crate::guest::ProcBlock>, $crate::guest::CreateError> {
fn assert_impl_proc_block(_: &impl $crate::guest::ProcBlock) {}

let proc_block = <$proc_block>::try_from(args)?;
assert_impl_proc_block(&proc_block);
getrandom::register_custom_getrandom!(host_rng);

Ok(Box::new(proc_block) as Box<dyn $crate::guest::ProcBlock>)
}
};
fn host_rng(buffer: &mut [u8]) -> Result<(), getrandom::Error> {
bindings::get_random(buffer);
Ok(())
}
17 changes: 0 additions & 17 deletions support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,4 @@ mod strings;
#[cfg(feature = "guest")]
pub mod guest;

use std::sync::Mutex;

pub use crate::strings::{decode_strings, StringBuilder};

use once_cell::sync::Lazy;
use rand::{prelude::SmallRng, Rng, SeedableRng};

// Note: getrandom is pulled in by the linfa_logistic crate
getrandom::register_custom_getrandom!(unsupported_rng);

fn unsupported_rng(buffer: &mut [u8]) -> Result<(), getrandom::Error> {
// FIXME: We should probably seed this with something more useful.
static RNG: Lazy<Mutex<SmallRng>> =
Lazy::new(|| Mutex::new(SmallRng::from_seed(Default::default())));

RNG.lock().unwrap().fill(buffer);
Ok(())
}
1 change: 0 additions & 1 deletion support_vector_classifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ description = "a binary classifier that uses an optimal hyperplane to separate t
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
1 change: 0 additions & 1 deletion support_vector_regression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ description = "a supervised learning models with associated learning algorithms
hotg-rune-proc-blocks = { path = "../support" }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen" }
smartcore = { git = "https://github.com/hotg-ai/smartcore", branch = "development" }
getrandom = { version = "0.2.6", default-features = false, features = ["custom"] }

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down

0 comments on commit 8aec3b5

Please sign in to comment.