-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add where onnx op support * Add broadcasting support * Remove broadcasting limitation comment * Fix broadcasting in mask where * Forgot to reflect changes in codegen test * Fix clippy
- Loading branch information
Showing
10 changed files
with
341 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
38 changes: 38 additions & 0 deletions
38
crates/burn-import/onnx-tests/tests/mask_where/mask_where.onnx
This file contains 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
42 changes: 42 additions & 0 deletions
42
crates/burn-import/onnx-tests/tests/mask_where/mask_where.py
This file contains 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,42 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: onnx-tests/tests/mask_where/mask_where.onnx | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class Model(nn.Module): | ||
def __init__(self): | ||
super(Model, self).__init__() | ||
|
||
def forward(self, condition, x1, y1, x2, y2): | ||
return torch.where(condition, x1, y1), torch.where(condition, x2, y2) | ||
|
||
|
||
def main(): | ||
# Set random seed for reproducibility | ||
torch.manual_seed(0) | ||
|
||
# Export to onnx | ||
model = Model() | ||
model.eval() | ||
device = torch.device("cpu") | ||
onnx_name = "mask_where.onnx" | ||
x = torch.ones(2, 2, device=device) | ||
y = torch.zeros(2, 2, device=device) | ||
mask = torch.tensor([[True, False], [False, True]], device=device) | ||
test_input = (mask, x, y, x[0], y[0]) | ||
|
||
torch.onnx.export(model, (test_input), onnx_name, verbose=False, opset_version=16) | ||
|
||
print(f"Finished exporting model to {onnx_name}") | ||
|
||
# Output some test data for use in the test | ||
print(f"Test input data: {test_input}") | ||
output = model.forward(*test_input) | ||
print(f"Test output data: {output}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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 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 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,200 @@ | ||
use core::cmp::max; | ||
|
||
use super::{Node, NodeCodegen}; | ||
use crate::burn::{BurnImports, TensorType, ToTokens, Type}; | ||
|
||
use burn::record::PrecisionSettings; | ||
use quote::quote; | ||
|
||
#[derive(Debug, Clone, new)] | ||
pub struct WhereNode { | ||
/// Bool tensor. When True (nonzero), yield X, otherwise yield Y. | ||
pub condition: TensorType, | ||
/// Values selected at indices where condition is True. | ||
pub x: TensorType, | ||
/// Values selected at indices where condition is False. | ||
pub y: TensorType, | ||
pub output: TensorType, | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for WhereNode { | ||
fn output_types(&self) -> Vec<Type> { | ||
vec![Type::Tensor(self.output.clone())] | ||
} | ||
|
||
fn input_types(&self) -> Vec<crate::burn::Type> { | ||
vec![ | ||
Type::Tensor(self.condition.clone()), | ||
Type::Tensor(self.x.clone()), | ||
Type::Tensor(self.y.clone()), | ||
] | ||
} | ||
|
||
fn forward( | ||
&self, | ||
scope: &mut crate::burn::Scope, | ||
node_position: usize, | ||
) -> proc_macro2::TokenStream { | ||
let mut mask = scope.tensor_use_owned(&self.condition, node_position); | ||
let mut x = scope.tensor_use_owned(&self.x, node_position); | ||
let mut y = scope.tensor_use_owned(&self.y, node_position); | ||
let output = &self.output.name; | ||
|
||
// x, y and condition need to be broadcastable | ||
let broadcasted_dim = max(max(self.x.dim, self.y.dim), self.condition.dim); | ||
let unsqueeze_dims = broadcasted_dim.to_tokens(); | ||
|
||
if self.condition.dim < broadcasted_dim { | ||
mask = quote! { #mask.unsqueeze::<#unsqueeze_dims>()}; | ||
} | ||
|
||
if self.x.dim < broadcasted_dim { | ||
x = quote! { #x.unsqueeze::<#unsqueeze_dims>()}; | ||
} | ||
|
||
if self.y.dim < broadcasted_dim { | ||
y = quote! { #y.unsqueeze::<#unsqueeze_dims>()}; | ||
} | ||
|
||
quote! { | ||
let #output = #y.mask_where(#mask, #x); | ||
} | ||
} | ||
|
||
fn register_imports(&self, imports: &mut BurnImports) { | ||
imports.register("burn::tensor::Bool"); | ||
} | ||
|
||
fn into_node(self) -> super::Node<PS> { | ||
Node::Where(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use burn::record::FullPrecisionSettings; | ||
|
||
use super::*; | ||
use crate::burn::{ | ||
graph::BurnGraph, | ||
node::{mask_where::WhereNode, test::assert_tokens}, | ||
TensorType, | ||
}; | ||
|
||
#[test] | ||
fn test_codegen_where() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
|
||
graph.register(WhereNode::new( | ||
TensorType::new_bool("tensor1", 2), | ||
TensorType::new_float("tensor2", 2), | ||
TensorType::new_float("tensor3", 2), | ||
TensorType::new_float("tensor4", 2), | ||
)); | ||
|
||
graph.register_input_output( | ||
vec![ | ||
"tensor1".to_string(), | ||
"tensor2".to_string(), | ||
"tensor3".to_string(), | ||
], | ||
vec!["tensor4".to_string()], | ||
); | ||
|
||
let expected = quote! { | ||
use burn::tensor::Bool; | ||
use burn::{ | ||
module::Module, | ||
tensor::{backend::Backend, Tensor}, | ||
}; | ||
|
||
#[derive(Module, Debug)] | ||
pub struct Model<B: Backend> { | ||
phantom: core::marker::PhantomData<B>, | ||
} | ||
|
||
impl<B: Backend> Model <B> { | ||
#[allow(unused_variables)] | ||
pub fn new(device: &B::Device) -> Self { | ||
Self { | ||
phantom: core::marker::PhantomData, | ||
} | ||
} | ||
|
||
#[allow(clippy::let_and_return, clippy::approx_constant)] | ||
pub fn forward( | ||
&self, | ||
tensor1: Tensor<B, 2, Bool>, | ||
tensor2: Tensor<B, 2>, | ||
tensor3: Tensor<B, 2> | ||
) -> Tensor<B, 2> { | ||
let tensor4 = tensor3.mask_where(tensor1, tensor2); | ||
|
||
tensor4 | ||
} | ||
} | ||
}; | ||
|
||
assert_tokens(graph.codegen(), expected); | ||
} | ||
|
||
#[test] | ||
fn test_codegen_where_broadcasted() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
|
||
graph.register(WhereNode::new( | ||
TensorType::new_bool("tensor1", 4), | ||
TensorType::new_float("tensor2", 2), | ||
TensorType::new_float("tensor3", 3), | ||
TensorType::new_float("tensor4", 4), | ||
)); | ||
|
||
graph.register_input_output( | ||
vec![ | ||
"tensor1".to_string(), | ||
"tensor2".to_string(), | ||
"tensor3".to_string(), | ||
], | ||
vec!["tensor4".to_string()], | ||
); | ||
|
||
let expected = quote! { | ||
use burn::tensor::Bool; | ||
use burn::{ | ||
module::Module, | ||
tensor::{backend::Backend, Tensor}, | ||
}; | ||
|
||
#[derive(Module, Debug)] | ||
pub struct Model<B: Backend> { | ||
phantom: core::marker::PhantomData<B>, | ||
} | ||
|
||
impl<B: Backend> Model <B> { | ||
#[allow(unused_variables)] | ||
pub fn new(device: &B::Device) -> Self { | ||
Self { | ||
phantom: core::marker::PhantomData, | ||
} | ||
} | ||
|
||
#[allow(clippy::let_and_return, clippy::approx_constant)] | ||
pub fn forward( | ||
&self, | ||
tensor1: Tensor<B, 4, Bool>, | ||
tensor2: Tensor<B, 2>, | ||
tensor3: Tensor<B, 3> | ||
) -> Tensor<B, 4> { | ||
let tensor4 = tensor3 | ||
.unsqueeze::<4>() | ||
.mask_where(tensor1, tensor2.unsqueeze::<4>()); | ||
|
||
tensor4 | ||
} | ||
} | ||
}; | ||
|
||
assert_tokens(graph.codegen(), expected); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.