-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
236 additions
and
2 deletions.
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
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,15 @@ | ||
expand:� | ||
>shapeshape_constant"Constant* | ||
value*:Bshape� | ||
. | ||
input_tensor | ||
shapeoutput/Expand"ExpandExpandGraphZ | ||
input_tensor | ||
b | ||
output | ||
B |
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,53 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: onnx-tests/tests/expand/expand.onnx | ||
|
||
import onnx | ||
from onnx import helper, TensorProto | ||
|
||
def main() -> None: | ||
# Define the shape tensor as a constant node | ||
shape_value = [2, 2] # Example shape value | ||
shape_tensor = helper.make_tensor( | ||
name='shape', | ||
data_type=TensorProto.INT64, | ||
dims=[len(shape_value)], | ||
vals=shape_value, | ||
) | ||
|
||
shape_node = helper.make_node( | ||
'Constant', | ||
name='shape_constant', | ||
inputs=[], | ||
outputs=['shape'], | ||
value=shape_tensor, | ||
) | ||
|
||
# Define the Expand node that uses the outputs from the constant nodes | ||
expand_node = helper.make_node( | ||
'Expand', | ||
name='/Expand', | ||
inputs=['input_tensor', 'shape'], | ||
outputs=['output'] | ||
) | ||
|
||
# Create the graph | ||
graph_def = helper.make_graph( | ||
nodes=[shape_node, expand_node], | ||
name='ExpandGraph', | ||
inputs=[ | ||
helper.make_tensor_value_info('input_tensor', TensorProto.FLOAT, [2, 1]), | ||
], | ||
outputs=[ | ||
helper.make_tensor_value_info('output', TensorProto.FLOAT, [2, 2]) | ||
], | ||
) | ||
|
||
# Create the model | ||
model_def = helper.make_model(graph_def, producer_name='expand') | ||
|
||
# Save the model to a file | ||
onnx.save(model_def, 'expand.onnx') | ||
|
||
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,92 @@ | ||
use super::{Node, NodeCodegen}; | ||
use crate::burn::{Scope, TensorType, ToTokens, Type}; | ||
use burn::record::PrecisionSettings; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
#[derive(Debug, Clone, new)] | ||
pub struct ExpandNode { | ||
pub input: TensorType, | ||
pub output: TensorType, | ||
pub shape: Vec<i64>, | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for ExpandNode { | ||
fn output_types(&self) -> Vec<Type> { | ||
vec![Type::Tensor(self.output.clone())] | ||
} | ||
|
||
fn input_types(&self) -> Vec<Type> { | ||
vec![Type::Tensor(self.input.clone())] | ||
} | ||
|
||
fn forward(&self, scope: &mut Scope, node_position: usize) -> TokenStream { | ||
let input = scope.tensor_use_owned(&self.input, node_position); | ||
let shape = &self.shape.to_tokens(); | ||
let output = &self.output.name; | ||
|
||
quote! { | ||
let #output = #input.expand(#shape); | ||
} | ||
} | ||
|
||
fn into_node(self) -> Node<PS> { | ||
Node::Expand(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use burn::record::FullPrecisionSettings; | ||
|
||
use super::*; | ||
use crate::burn::{ | ||
graph::BurnGraph, | ||
node::{expand::ExpandNode, test::assert_tokens}, | ||
TensorType, | ||
}; | ||
|
||
#[test] | ||
fn test_codegen_nodes() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
|
||
graph.register(ExpandNode::new( | ||
TensorType::new_float("tensor1", 4), | ||
TensorType::new_float("tensor2", 4), | ||
[4, 4, 4, 4].into(), | ||
)); | ||
|
||
graph.register_input_output(vec!["tensor1".to_string()], vec!["tensor2".to_string()]); | ||
|
||
let expected = quote! { | ||
use burn::{ | ||
module::Module, | ||
tensor::{backend::Backend, Tensor}, | ||
}; | ||
|
||
#[derive(Module, Debug)] | ||
pub struct Model<B: Backend> { | ||
phantom: core::marker::PhantomData<B>, | ||
device: burn::module::Ignored<B::Device>, | ||
} | ||
|
||
impl<B: Backend> Model <B> { | ||
#[allow(unused_variables)] | ||
pub fn new(device: &B::Device) -> Self { | ||
Self { | ||
phantom: core::marker::PhantomData, | ||
device: burn::module::Ignored(device.clone()), | ||
} | ||
} | ||
#[allow(clippy::let_and_return, clippy::approx_constant)] | ||
pub fn forward(&self, tensor1: Tensor<B, 4>) -> Tensor<B, 4> { | ||
let tensor2 = tensor1.expand([4,4,4,4]); | ||
|
||
tensor2 | ||
} | ||
} | ||
}; | ||
|
||
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
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