Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub struct Scope {
op_names: Rc<RefCell<HashMap<String, i32>>>,
device: String,
control_deps: Vec<Operation>,
kernel_label: String,
}

impl Scope {
Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -160,6 +162,7 @@ impl Scope {
},
device: self.device.clone(),
control_deps: self.control_deps.clone(),
kernel_label: self.kernel_label.clone(),
}
}

Expand All @@ -174,6 +177,7 @@ impl Scope {
op_names: self.op_names.clone(),
device: self.device.clone(),
control_deps: self.control_deps.clone(),
kernel_label: self.kernel_label.clone(),
}
}

Expand Down Expand Up @@ -213,6 +217,7 @@ impl Scope {
op_names: self.op_names.clone(),
device: device.to_string(),
control_deps: self.control_deps.clone(),
kernel_label: self.kernel_label.clone(),
}
}

Expand All @@ -233,6 +238,7 @@ impl Scope {
.chain(control_deps.iter())
.cloned()
.collect(),
kernel_label: self.kernel_label.clone(),
}
}

Expand All @@ -247,6 +253,22 @@ impl Scope {
op_names: self.op_names.clone(),
device: self.device.clone(),
control_deps: vec![],
kernel_label: self.kernel_label.clone(),
}
}

/// 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(),
}
}

Expand All @@ -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()?)
}
Expand Down Expand Up @@ -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();
Expand Down