-
Notifications
You must be signed in to change notification settings - Fork 12
test(huntsman-integration): Add neural network tasks. #377
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
efa28fd
Add core
sitaowang1998 8004dbe
Add task wrapper
sitaowang1998 f2d26ff
Fix toml lint
sitaowang1998 8588bfa
Add client
sitaowang1998 776a5a6
Fix docstring
sitaowang1998 14720ca
Replace print with logging
sitaowang1998 83f6e93
Fix library and bin name
sitaowang1998 1e3cc5e
polish
sitaowang1998 e723a62
Fix wiring
sitaowang1998 430c859
Fix docstring and logging
sitaowang1998 910371a
Fix style
sitaowang1998 01385f3
Remove client component
sitaowang1998 76e8f08
Merge branch 'main' into e2e-nn
sitaowang1998 1750df1
Merge branch 'main' into e2e-nn
sitaowang1998 5f197a1
Merge branch 'main' into e2e-nn
sitaowang1998 bff5834
Merge branch 'main' into e2e-nn
sitaowang1998 304e0d1
Minor fixes.
LinZhihao-723 f6bbbed
Fix lint
sitaowang1998 b2878fa
Merge branch 'e2e-nn' of github.com:sitaowang1998/spider into e2e-nn
sitaowang1998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [package] | ||
| name = "huntsman-nn-core" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| publish = false | ||
|
|
||
| [lib] | ||
| name = "huntsman_nn_core" | ||
| path = "src/lib.rs" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| //! Pure neuron math for the Spider end-to-end neural-network test workload. | ||
| //! | ||
| //! A dense-layer neuron computes `activation(weighted_sum(inputs) + bias)` over a fixed fan-in of | ||
| //! 25 scalar `double` inputs. | ||
|
|
||
| /// The fixed neuron fan-in: each neuron consumes exactly this many scalar inputs. | ||
| pub const NUM_INPUTS: usize = 25; | ||
|
|
||
| /// The fixed per-input weights, one per input position. Deterministic values calculated as | ||
| /// (`WEIGHTS[k] = (k + 1) * 0.01 * (-1)^k`), alternating in sign starting positive. | ||
| pub const WEIGHTS: [f64; NUM_INPUTS] = [ | ||
| 0.01, -0.02, 0.03, -0.04, 0.05, -0.06, 0.07, -0.08, 0.09, -0.10, 0.11, -0.12, 0.13, -0.14, | ||
| 0.15, -0.16, 0.17, -0.18, 0.19, -0.20, 0.21, -0.22, 0.23, -0.24, 0.25, | ||
| ]; | ||
|
|
||
| /// The fixed bias added to the weighted sum before the activation. | ||
| pub const BIAS: f64 = 0.5; | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The rectified-linear activation `max(0.0, x)`. | ||
| #[must_use] | ||
| pub const fn relu(x: f64) -> f64 { | ||
| f64::max(0.0, x) | ||
| } | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The logistic sigmoid activation `1.0 / (1.0 + exp(-x))`. | ||
| #[must_use] | ||
| pub fn sigmoid(x: f64) -> f64 { | ||
| 1.0 / (1.0 + f64::exp(-x)) | ||
| } | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The identity activation `x`. | ||
| #[must_use] | ||
| pub const fn identity(x: f64) -> f64 { | ||
| x | ||
| } | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The rectified-linear activation of the weighted sum of `inputs` plus [`BIAS`]. | ||
| #[must_use] | ||
| pub fn dense_relu(inputs: &[f64; NUM_INPUTS]) -> f64 { | ||
| relu(weighted_sum(inputs)) | ||
| } | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The logistic sigmoid of the weighted sum of `inputs` plus [`BIAS`]. | ||
| #[must_use] | ||
| pub fn dense_sigmoid(inputs: &[f64; NUM_INPUTS]) -> f64 { | ||
| sigmoid(weighted_sum(inputs)) | ||
| } | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The weighted sum of `inputs` plus [`BIAS`], unchanged by the activation. | ||
| #[must_use] | ||
| pub fn dense_identity(inputs: &[f64; NUM_INPUTS]) -> f64 { | ||
| identity(weighted_sum(inputs)) | ||
| } | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The weighted sum `sum(WEIGHTS[k] * inputs[k]) + BIAS`. | ||
| fn weighted_sum(inputs: &[f64; NUM_INPUTS]) -> f64 { | ||
| let mut acc = BIAS; | ||
| for (w, x) in WEIGHTS.iter().zip(inputs.iter()) { | ||
| acc += w * x; | ||
| } | ||
| acc | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| /// Relative-tolerance float equality used to compare hand-computed and computed values. | ||
| fn assert_approx_eq(actual: f64, expected: f64) { | ||
| let diff = (actual - expected).abs(); | ||
| let tol = 1.0e-12_f64 * (1.0 + expected.abs()); | ||
| assert!( | ||
| diff <= tol, | ||
| "actual={actual}, expected={expected}, diff={diff}, tol={tol}", | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_relu() { | ||
| assert_approx_eq(relu(-1.0), 0.0); | ||
| assert_approx_eq(relu(0.0), 0.0); | ||
| assert_approx_eq(relu(2.5), 2.5); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sigmoid() { | ||
| assert_approx_eq(sigmoid(0.0), 0.5); | ||
| assert_approx_eq(sigmoid(100.0), 1.0); | ||
| assert_approx_eq(sigmoid(-100.0), 0.0); | ||
| assert!(sigmoid(-1.0) < sigmoid(0.0)); | ||
| assert!(sigmoid(0.0) < sigmoid(1.0)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_identity() { | ||
| assert_approx_eq(identity(-3.0), -3.0); | ||
| assert_approx_eq(identity(0.0), 0.0); | ||
| assert_approx_eq(identity(7.25), 7.25); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_weighted_sum_all_zero_inputs_equals_bias() { | ||
| let inputs = [0.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(weighted_sum(&inputs), BIAS); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_weighted_sum_all_one_inputs() { | ||
| let inputs = [1.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(weighted_sum(&inputs), 0.63); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_dense_relu() { | ||
| let zero = [0.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(dense_relu(&zero), 0.5); | ||
|
|
||
| let ones = [1.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(dense_relu(&ones), 0.63); | ||
|
|
||
| // Negative weighted sum (large negative inputs) clamps to 0 under relu. | ||
| let neg = [-1000.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(dense_relu(&neg), 0.0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_dense_sigmoid() { | ||
| let zero = [0.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(dense_sigmoid(&zero), sigmoid(BIAS)); | ||
|
|
||
| let ones = [1.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(dense_sigmoid(&ones), sigmoid(0.63)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_dense_identity() { | ||
| let zero = [0.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(dense_identity(&zero), 0.5); | ||
|
|
||
| let ones = [1.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(dense_identity(&ones), 0.63); | ||
|
|
||
| let neg = [-1000.0_f64; NUM_INPUTS]; | ||
| assert_approx_eq(dense_identity(&neg), weighted_sum(&neg)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [package] | ||
| name = "huntsman-nn-tasks" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| publish = false | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
| name = "nn" | ||
| path = "src/lib.rs" | ||
|
|
||
| [dependencies] | ||
| huntsman-nn-core = { path = "../core" } | ||
| serde = { version = "1.0.228", features = ["derive"] } | ||
| spider-tdl = { | ||
| path = "../../../../components/spider-tdl", | ||
| features = ["derive"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| //! Reference TDL package: dense-neuron computation. | ||
|
|
||
| #![allow(clippy::too_many_arguments)] | ||
|
|
||
| mod task_decl { | ||
| use spider_tdl::TaskContext; | ||
| use spider_tdl::TdlError; | ||
| use spider_tdl::r#std::double; | ||
| use spider_tdl::task; | ||
|
|
||
| #[task(name = "neuron::dense_relu")] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renaming
|
||
| pub fn dense_relu( | ||
| _ctx: TaskContext, | ||
| x0: double, | ||
| x1: double, | ||
| x2: double, | ||
| x3: double, | ||
| x4: double, | ||
| x5: double, | ||
| x6: double, | ||
| x7: double, | ||
| x8: double, | ||
| x9: double, | ||
| x10: double, | ||
| x11: double, | ||
| x12: double, | ||
| x13: double, | ||
| x14: double, | ||
| x15: double, | ||
| x16: double, | ||
| x17: double, | ||
| x18: double, | ||
| x19: double, | ||
| x20: double, | ||
| x21: double, | ||
| x22: double, | ||
| x23: double, | ||
| x24: double, | ||
| ) -> Result<double, TdlError> { | ||
| Ok(huntsman_nn_core::dense_relu(&[ | ||
| x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, | ||
| x19, x20, x21, x22, x23, x24, | ||
| ])) | ||
| } | ||
|
|
||
| #[task(name = "neuron::dense_sigmoid")] | ||
| pub fn dense_sigmoid( | ||
| _ctx: TaskContext, | ||
| x0: double, | ||
| x1: double, | ||
| x2: double, | ||
| x3: double, | ||
| x4: double, | ||
| x5: double, | ||
| x6: double, | ||
| x7: double, | ||
| x8: double, | ||
| x9: double, | ||
| x10: double, | ||
| x11: double, | ||
| x12: double, | ||
| x13: double, | ||
| x14: double, | ||
| x15: double, | ||
| x16: double, | ||
| x17: double, | ||
| x18: double, | ||
| x19: double, | ||
| x20: double, | ||
| x21: double, | ||
| x22: double, | ||
| x23: double, | ||
| x24: double, | ||
| ) -> Result<double, TdlError> { | ||
| Ok(huntsman_nn_core::dense_sigmoid(&[ | ||
| x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, | ||
| x19, x20, x21, x22, x23, x24, | ||
| ])) | ||
| } | ||
|
|
||
| #[task(name = "neuron::dense_identity")] | ||
| pub fn dense_identity( | ||
| _ctx: TaskContext, | ||
| x0: double, | ||
| x1: double, | ||
| x2: double, | ||
| x3: double, | ||
| x4: double, | ||
| x5: double, | ||
| x6: double, | ||
| x7: double, | ||
| x8: double, | ||
| x9: double, | ||
| x10: double, | ||
| x11: double, | ||
| x12: double, | ||
| x13: double, | ||
| x14: double, | ||
| x15: double, | ||
| x16: double, | ||
| x17: double, | ||
| x18: double, | ||
| x19: double, | ||
| x20: double, | ||
| x21: double, | ||
| x22: double, | ||
| x23: double, | ||
| x24: double, | ||
| ) -> Result<double, TdlError> { | ||
| Ok(huntsman_nn_core::dense_identity(&[ | ||
| x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, | ||
| x19, x20, x21, x22, x23, x24, | ||
| ])) | ||
| } | ||
| } | ||
|
|
||
| spider_tdl::register_tdl_package! { | ||
| package_name: "nn", | ||
| tasks: [ | ||
| task_decl::dense_relu, | ||
| task_decl::dense_sigmoid, | ||
| task_decl::dense_identity, | ||
| ], | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added negative values.