From 3a6130ea90bb0b62bb60dba4f9f6067746e3114e Mon Sep 17 00:00:00 2001 From: siyavash Date: Mon, 1 Feb 2021 12:53:33 +0330 Subject: [PATCH 1/3] Add Scope::with_kernel_label --- src/scope.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/scope.rs b/src/scope.rs index e8c9e0ae0f..9dbc142332 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -102,6 +102,7 @@ pub struct Scope { op_names: Rc>>, device: String, control_deps: Vec, + kernel_label: String, } impl Scope { @@ -117,6 +118,7 @@ impl Scope { op_names: Rc::new(RefCell::new(HashMap::new())), device: "".to_string(), control_deps: Vec::new(), + kernel_label: "".to_string(), } } @@ -160,6 +162,7 @@ impl Scope { }, device: self.device.clone(), control_deps: self.control_deps.clone(), + kernel_label: "".to_string(), } } @@ -174,6 +177,7 @@ impl Scope { op_names: self.op_names.clone(), device: self.device.clone(), control_deps: self.control_deps.clone(), + kernel_label: "".to_string(), } } @@ -213,6 +217,7 @@ impl Scope { op_names: self.op_names.clone(), device: device.to_string(), control_deps: self.control_deps.clone(), + kernel_label: "".to_string(), } } @@ -233,6 +238,7 @@ impl Scope { .chain(control_deps.iter()) .cloned() .collect(), + kernel_label: "".to_string(), } } @@ -247,6 +253,22 @@ impl Scope { op_names: self.op_names.clone(), device: self.device.clone(), control_deps: vec![], + kernel_label: "".to_string(), + } + } + + /// Return a new scope. All ops created with the new scope will have + /// kernel_label as the value for their '_kernel' attribute; + pub fn with_kernel_label(&self, kernel_label: &str) -> Scope { + Scope { + graph: self.graph.clone(), + name: self.name.clone(), + children_names: self.children_names.clone(), + op_name: self.op_name.clone(), + op_names: self.op_names.clone(), + device: self.device.clone(), + control_deps: self.control_deps.clone(), + kernel_label: kernel_label.to_string(), } } @@ -263,6 +285,9 @@ impl Scope { for control_dep in &self.control_deps { nd.add_control_input(control_dep); } + if !self.kernel_label.is_empty() { + nd.set_attr_string("_kernel", &self.kernel_label)?; + } f(&mut nd)?; Ok(nd.finish()?) } @@ -333,6 +358,19 @@ mod tests { ); } + #[test] + fn kernel_label() { + assert_eq!( + Scope::new_root_scope() + .with_kernel_label("foo") + .new_operation("NoOp", |_| Ok(())) + .unwrap() + .get_attr_string("_kernel") + .unwrap(), + "foo" + ); + } + #[test] fn control_dependencies() { let mut scope = Scope::new_root_scope(); From 148edb114d95562419bf4f781ff05785c0f6fc1f Mon Sep 17 00:00:00 2001 From: siyavash Date: Mon, 1 Feb 2021 13:02:00 +0330 Subject: [PATCH 2/3] Update doc. --- src/scope.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scope.rs b/src/scope.rs index 9dbc142332..d25f7daeea 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -258,7 +258,7 @@ impl Scope { } /// Return a new scope. All ops created with the new scope will have - /// kernel_label as the value for their '_kernel' attribute; + /// kernel_label as the value for their '_kernel' attribute. pub fn with_kernel_label(&self, kernel_label: &str) -> Scope { Scope { graph: self.graph.clone(), From 90e9254c302186d56265d614262a5aef6e5a0fea Mon Sep 17 00:00:00 2001 From: siyavash Date: Mon, 8 Feb 2021 13:55:54 +0330 Subject: [PATCH 3/3] Fix kernel_label in other withs --- src/scope.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/scope.rs b/src/scope.rs index d25f7daeea..edd90b7a0d 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -162,7 +162,7 @@ impl Scope { }, device: self.device.clone(), control_deps: self.control_deps.clone(), - kernel_label: "".to_string(), + kernel_label: self.kernel_label.clone(), } } @@ -177,7 +177,7 @@ impl Scope { op_names: self.op_names.clone(), device: self.device.clone(), control_deps: self.control_deps.clone(), - kernel_label: "".to_string(), + kernel_label: self.kernel_label.clone(), } } @@ -217,7 +217,7 @@ impl Scope { op_names: self.op_names.clone(), device: device.to_string(), control_deps: self.control_deps.clone(), - kernel_label: "".to_string(), + kernel_label: self.kernel_label.clone(), } } @@ -238,7 +238,7 @@ impl Scope { .chain(control_deps.iter()) .cloned() .collect(), - kernel_label: "".to_string(), + kernel_label: self.kernel_label.clone(), } } @@ -253,7 +253,7 @@ impl Scope { op_names: self.op_names.clone(), device: self.device.clone(), control_deps: vec![], - kernel_label: "".to_string(), + kernel_label: self.kernel_label.clone(), } }