From f38274f334c9a5241a11fbf03013075b15653c09 Mon Sep 17 00:00:00 2001 From: Braden Everson Date: Wed, 27 Mar 2024 07:57:25 -0500 Subject: [PATCH 1/6] Create more complex CTE test and prevent same node replacement --- src/core/graph/subterm.rs | 76 ++++++++++++++++++------------------- src/core/graph/tests_cpu.rs | 30 +++++++++++++++ 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/src/core/graph/subterm.rs b/src/core/graph/subterm.rs index a33a293..150b5ec 100644 --- a/src/core/graph/subterm.rs +++ b/src/core/graph/subterm.rs @@ -28,56 +28,56 @@ impl Context { continue; } - if node_map.contains_key(&self.nodes[node_id]) { + if node_map.contains_key(&self.nodes[node_id]) && node_map[&self.nodes[node_id]] != node_id { self.replace_index(node_id, node_map[&self.nodes[node_id]])?; modifications += 1; changed = true; } else { - node_map.insert(self.nodes[node_id].clone(), node_id); - } - - visited.insert(node_id); - //Add operation nodes to the queue - match self.nodes[node_id].operation { - Operation::Add(a, b) - | Operation::Sub(a, b) - | Operation::Mul(a, b) - | Operation::Div(a, b) - | Operation::NotEqual(a, b) - | Operation::Equal(a, b) - | Operation::LessThan(a, b) - | Operation::GreaterThan(a, b) - | Operation::GreaterThanEq(a, b) - | Operation::LessThanEq(a, b) - | Operation::MatMul(a, b) - | Operation::Pow(a, b) => { - to_visit.push(a); - to_visit.push(b); - } - Operation::Neg(a) - | Operation::StopGradient(a) - | Operation::Log(a) - | Operation::Exp(a) - | Operation::TypeCast(a, _) - | Operation::Transpose(a, _) - | Operation::SliceInDim { node: a, start: _, stop: _, stride: _, dim: _ } + visited.insert(node_id); + //Add operation nodes to the queue + match self.nodes[node_id].operation { + Operation::Add(a, b) + | Operation::Sub(a, b) + | Operation::Mul(a, b) + | Operation::Div(a, b) + | Operation::NotEqual(a, b) + | Operation::Equal(a, b) + | Operation::LessThan(a, b) + | Operation::GreaterThan(a, b) + | Operation::GreaterThanEq(a, b) + | Operation::LessThanEq(a, b) + | Operation::MatMul(a, b) + | Operation::Pow(a, b) => { + to_visit.push(a); + to_visit.push(b); + } + Operation::Neg(a) + | Operation::StopGradient(a) + | Operation::Log(a) + | Operation::Exp(a) + | Operation::TypeCast(a, _) + | Operation::Transpose(a, _) + | Operation::SliceInDim { node: a, start: _, stop: _, stride: _, dim: _ } | Operation::TileInDim { node: a, n_tiles: _, dim: _ } | Operation::Reshape(a) - | Operation::ZerosLike(a) => { - to_visit.push(a); - } - Operation::ReduceMax { node, dim: _ } + | Operation::ZerosLike(a) => { + to_visit.push(a); + } + Operation::ReduceMax { node, dim: _ } | Operation::ReduceMean { node, dim: _ } | Operation::ReduceSum { node, dim: _ } => { to_visit.push(node); } - Operation::Select { pred, on_true, on_false } => { - to_visit.push(pred); - to_visit.push(on_true); - to_visit.push(on_false); + Operation::Select { pred, on_true, on_false } => { + to_visit.push(pred); + to_visit.push(on_true); + to_visit.push(on_false); + } + Operation::Constant(_) | Operation::Parameter(_) => {} } - Operation::Constant(_) | Operation::Parameter(_) => {} + node_map.insert(self.nodes[node_id].clone(), node_id); } + } //Recursive recall if we changed something and modifications are still available diff --git a/src/core/graph/tests_cpu.rs b/src/core/graph/tests_cpu.rs index d4828da..c7275e7 100644 --- a/src/core/graph/tests_cpu.rs +++ b/src/core/graph/tests_cpu.rs @@ -68,6 +68,36 @@ mod tests { create_test!(test_add_1_2, add, F32, 1f32, 2f32, 3f32); create_test!(test_sub_1_2, sub, F32, 1f32, 2f32, -1f32); + #[test] + fn test_large_cte() { + let mut ctx = Context::new(); + let a = ctx.parameter("a", [], xla::ElementType::F32).expect("a"); + let two = ctx.scalar(2, xla::ElementType::F32).expect("2"); + + let a_2 = ctx.pow(a, two).expect("a^2"); + let a_21 = ctx.pow(a, two).expect("a^2"); + let a_22 = ctx.pow(a, two).expect("a^2"); + let a_23 = ctx.pow(a, two).expect("a^2"); + let a_24 = ctx.pow(a, two).expect("a^2"); + let a_25 = ctx.pow(a, two).expect("a^2"); + let a_26 = ctx.pow(a, two).expect("a^2"); + let a_27 = ctx.pow(a, two).expect("a^2"); + + + let sum1 = ctx.add(a_2, a_21).expect("a^2 + a^2"); + let sum2 = ctx.add(a_22, a_23).expect("a^2 + a^2"); + let sum3 = ctx.add(a_24, a_25).expect("a^2 + a^2"); + let sum4 = ctx.add(a_26, a_27).expect("a^2 + a^2"); + + let nest_sum1 = ctx.add(sum1, sum2).expect("(a^2 + a^2) + (a^2 + a^2)"); + let nest_sum2 = ctx.add(sum3, sum4).expect("(a^2 + a^2) + (a^2 + a^2)"); + + let res = ctx.add(nest_sum1, nest_sum2).expect("((a^2 + a^2) + (a^2 + a^2)) + ((a^2 + a^2) + (a^2 + a^2))"); + let subterm_extract = ctx.extract_subterms(&[res], usize::MAX).expect("CTE"); + + assert!(subterm_extract); + } + #[test] fn test_cte_happened() { let mut ctx = Context::new(); From 53e99b377289c29d4eff3fa90322122711aff152 Mon Sep 17 00:00:00 2001 From: Braden Everson Date: Wed, 27 Mar 2024 07:58:14 -0500 Subject: [PATCH 2/6] Clippy suggestions --- src/core/graph/constant.rs | 2 +- src/core/graph/consteval.rs | 4 ++-- src/core/graph/operation.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/graph/constant.rs b/src/core/graph/constant.rs index 28a300e..97af992 100644 --- a/src/core/graph/constant.rs +++ b/src/core/graph/constant.rs @@ -1,5 +1,5 @@ use super::*; -use std::{path::Path, hash::Hash}; +use std::{path::Path}; use xla::FromRawBytes; #[derive(Debug, Clone)] diff --git a/src/core/graph/consteval.rs b/src/core/graph/consteval.rs index 768082a..ee1e376 100644 --- a/src/core/graph/consteval.rs +++ b/src/core/graph/consteval.rs @@ -5,9 +5,9 @@ use super::*; impl Context { fn collect_deps(&self, node: NodeIdentifier) -> Vec { if self.dependent_nodes.contains_key(&node) { - return self.dependent_nodes[&node].to_vec(); + self.dependent_nodes[&node].to_vec() } else { - return vec![]; + vec![] } } diff --git a/src/core/graph/operation.rs b/src/core/graph/operation.rs index 36222e9..5017d26 100644 --- a/src/core/graph/operation.rs +++ b/src/core/graph/operation.rs @@ -114,7 +114,7 @@ impl Hash for Operation { impl PartialEq for Operation { fn eq(&self, other: &Self) -> bool { - return match (&self, &other) { + match (&self, &other) { //Order not matering. Ex: 1 + 2 equals 2 + 1, but 1 / 2 doesnt equal 2 /1 so we can //check these separately (&Self::Mul(a, b), &Self::Mul(c, d)) From 56046764e02f80b9b1a2d8078d4e84780911dc45 Mon Sep 17 00:00:00 2001 From: Braden Everson Date: Wed, 27 Mar 2024 12:58:29 -0500 Subject: [PATCH 3/6] Rename LICENSE to LICENSE-APACHE --- LICENSE => LICENSE-APACHE | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename LICENSE => LICENSE-APACHE (100%) diff --git a/LICENSE b/LICENSE-APACHE similarity index 100% rename from LICENSE rename to LICENSE-APACHE From 3158b2c6dc1a74d94c44acda598d95fb79265ca7 Mon Sep 17 00:00:00 2001 From: Braden Everson Date: Wed, 27 Mar 2024 12:58:58 -0500 Subject: [PATCH 4/6] Create LICENSE-MIT --- LICENSE-MIT | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE-MIT diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..a535e1b --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Braden Everson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From ed8729d40e97b7ebc8352a56af75035f719aa6b4 Mon Sep 17 00:00:00 2001 From: Braden Everson Date: Wed, 27 Mar 2024 13:00:09 -0500 Subject: [PATCH 5/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 593e469..3b33e75 100644 --- a/README.md +++ b/README.md @@ -90,4 +90,4 @@ Gradient descent currently can happen both syncronously as stochastic gradient d #### If open source development is your thing, we at Unda would love additional work on anything that can be implemented, please contact **eversonb@msoe.edu** if you'd like to help out! # License -Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0. This file may not be copied, modified, or distributed except according to those terms. +Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 or the MIT license http://opensource.org/licenses/MIT, at your option. This file may not be copied, modified, or distributed except according to those terms. From a569ba5516d97cacd80776de27db13ebf1a40510 Mon Sep 17 00:00:00 2001 From: Braden Everson Date: Fri, 29 Mar 2024 13:34:49 -0500 Subject: [PATCH 6/6] Rename repo reference --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9ce3623..78cd90f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["BradenEverson ", "Ebanflo42", "Atlas Dostal description = "General purpose machine learning crate for neural network development and analysis" edition = "2021" license = "MIT" -repository = "https://github.com/BradenEverson/unda" +repository = "https://github.com/unda-ai/unda" categories = ["science"] keywords = [ "machine-learning",