-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* renaming repeat to repeat_dim * implementing repeat function * renaming repeat files to repeat_dim * renaming part 2 * renaming part 3 * renaming part 4 * renaming part 5 * adding test file * adding unit test * adding rust book documentation * adding function args doc * fixing tests * changing repeat api to match pytorch equivalent * fixing clippy error * implementing tile onnx file * temp * working implementation and test * working e2e test * adding new supported onnx operation to the md file
- Loading branch information
1 parent
6b61ad5
commit d770b1f
Showing
10 changed files
with
222 additions
and
4 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
Binary file not shown.
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,67 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import onnx | ||
import onnx.helper | ||
import onnx.checker | ||
|
||
|
||
def build_model(): | ||
# Define the input tensor as a graph input | ||
input_tensor = onnx.helper.make_tensor_value_info( | ||
name="input_tensor", | ||
elem_type=onnx.TensorProto.FLOAT, | ||
shape=[2, 2] | ||
) | ||
|
||
output_tensor = onnx.helper.make_tensor_value_info( | ||
name="output_tensor", | ||
elem_type=onnx.TensorProto.FLOAT, | ||
shape=[4, 4] | ||
) | ||
|
||
# Define the shape tensor for tiling as an initializer | ||
shape_tensor = onnx.helper.make_tensor( | ||
name="shape_tensor", | ||
data_type=onnx.TensorProto.INT64, | ||
dims=[2], | ||
vals=[2, 2] | ||
) | ||
# Create the Tile node | ||
tile_node = onnx.helper.make_node( | ||
"Tile", | ||
inputs=["input_tensor", "shape_tensor"], | ||
outputs=["output_tensor"] | ||
) | ||
|
||
# Build the graph | ||
graph = onnx.helper.make_graph( | ||
nodes=[tile_node], | ||
name="main_graph", | ||
inputs=[input_tensor], | ||
outputs=[output_tensor], | ||
initializer=[shape_tensor] | ||
) | ||
|
||
# Build the model | ||
model = onnx.helper.make_model( | ||
graph, | ||
ir_version=8, | ||
opset_imports=[onnx.helper.make_operatorsetid("", 16)] | ||
) | ||
|
||
return model | ||
|
||
|
||
def main(): | ||
onnx_model = build_model() | ||
|
||
onnx_model = onnx.shape_inference.infer_shapes(onnx_model) | ||
|
||
file_name = "tile.onnx" | ||
onnx.save(onnx_model, file_name) | ||
onnx.checker.check_model(onnx_model) | ||
print(f"ONNX model saved as {file_name}") | ||
|
||
|
||
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,97 @@ | ||
use super::{Node, NodeCodegen}; | ||
use crate::burn::{Scope, TensorType, ToTokens, Type}; | ||
use burn::config::Config; | ||
use burn::record::PrecisionSettings; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
#[derive(Config, Debug)] | ||
pub struct TileConfig { | ||
pub repeats: Vec<usize>, | ||
} | ||
|
||
#[derive(Debug, Clone, new)] | ||
pub struct TileNode { | ||
pub input: TensorType, | ||
pub output: TensorType, | ||
pub config: TileConfig, | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for TileNode { | ||
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 output = &self.output.name; | ||
|
||
let repeats = self.config.repeats.iter().map(|r| r.to_tokens()); | ||
|
||
quote! { | ||
let #output = #input.repeat(&[#(#repeats),*]); | ||
} | ||
} | ||
|
||
fn into_node(self) -> Node<PS> { | ||
Node::Tile(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use burn::record::FullPrecisionSettings; | ||
|
||
use super::*; | ||
use crate::burn::{ | ||
graph::BurnGraph, | ||
node::{test::assert_tokens, tile::TileConfig, tile::TileNode}, | ||
TensorType, | ||
}; | ||
|
||
#[test] | ||
fn test_codegen_tile() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
let config = TileConfig::new(vec![2, 3, 4]); | ||
graph.register(TileNode::new( | ||
TensorType::new_float("input", 3), | ||
TensorType::new_float("output", 3), | ||
config, | ||
)); | ||
graph.register_input_output(vec!["input".to_string()], vec!["output".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, input: Tensor<B, 3>) -> Tensor<B, 3> { | ||
let output = input.repeat(&[2, 3, 4]); | ||
output | ||
} | ||
} | ||
}; | ||
|
||
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