-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e407c76
commit bad96c3
Showing
12 changed files
with
290 additions
and
6 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
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,22 @@ | ||
sum-model:� | ||
% | ||
input1 | ||
input2 | ||
input3output"SumSumGraphZ | ||
input1 | ||
|
||
Z | ||
input2 | ||
|
||
Z | ||
input3 | ||
|
||
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,41 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: onnx-tests/tests/sum/sum.onnx | ||
|
||
import onnx | ||
import onnx.helper | ||
import onnx.checker | ||
import numpy as np | ||
|
||
# Create input tensors | ||
input1 = onnx.helper.make_tensor_value_info('input1', onnx.TensorProto.FLOAT, [3]) | ||
input2 = onnx.helper.make_tensor_value_info('input2', onnx.TensorProto.FLOAT, [3]) | ||
input3 = onnx.helper.make_tensor_value_info('input3', onnx.TensorProto.FLOAT, [3]) | ||
|
||
# Create output tensor | ||
output = onnx.helper.make_tensor_value_info('output', onnx.TensorProto.FLOAT, [3]) | ||
|
||
# Create the Sum node | ||
sum_node = onnx.helper.make_node( | ||
'Sum', | ||
inputs=['input1', 'input2', 'input3'], | ||
outputs=['output'] | ||
) | ||
|
||
# Create the graph (GraphProto) | ||
graph_def = onnx.helper.make_graph( | ||
nodes=[sum_node], | ||
name='SumGraph', | ||
inputs=[input1, input2, input3], | ||
outputs=[output] | ||
) | ||
|
||
# Create the model (ModelProto) | ||
model_def = onnx.helper.make_model(graph_def, producer_name='sum-model') | ||
onnx.checker.check_model(model_def) | ||
|
||
# Save the ONNX model | ||
onnx.save(model_def, 'sum.onnx') | ||
|
||
print("ONNX model 'sum.onnx' generated successfully.") | ||
|
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,22 @@ | ||
sum-model:� | ||
% | ||
input1 | ||
input2 | ||
input3output"SumSumGraphZ | ||
input1 | ||
|
||
Z | ||
input2 | ||
|
||
Z | ||
input3 | ||
|
||
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,40 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: onnx-tests/tests/sum/sum.onnx | ||
|
||
import onnx | ||
import onnx.helper | ||
import onnx.checker | ||
import numpy as np | ||
|
||
# Create input tensors | ||
input1 = onnx.helper.make_tensor_value_info('input1', onnx.TensorProto.INT64, [3]) | ||
input2 = onnx.helper.make_tensor_value_info('input2', onnx.TensorProto.INT64, [3]) | ||
input3 = onnx.helper.make_tensor_value_info('input3', onnx.TensorProto.INT64, [3]) | ||
|
||
# Create output tensor | ||
output = onnx.helper.make_tensor_value_info('output', onnx.TensorProto.INT64, [3]) | ||
|
||
# Create the Sum node | ||
sum_node = onnx.helper.make_node( | ||
'Sum', | ||
inputs=['input1', 'input2', 'input3'], | ||
outputs=['output'] | ||
) | ||
|
||
# Create the graph (GraphProto) | ||
graph_def = onnx.helper.make_graph( | ||
nodes=[sum_node], | ||
name='SumGraph', | ||
inputs=[input1, input2, input3], | ||
outputs=[output] | ||
) | ||
|
||
# Create the model (ModelProto) | ||
model_def = onnx.helper.make_model(graph_def, producer_name='sum-model') | ||
onnx.checker.check_model(model_def) | ||
|
||
# Save the ONNX model | ||
onnx.save(model_def, 'sum_int.onnx') | ||
|
||
print("ONNX model 'sum_int.onnx' generated successfully.") |
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,108 @@ | ||
use super::{Node, NodeCodegen}; | ||
use crate::burn::{Scope, TensorType, Type}; | ||
|
||
use burn::record::PrecisionSettings; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
#[derive(Debug, Clone, new)] | ||
pub struct SumNode { | ||
pub inputs: Vec<TensorType>, | ||
pub output: TensorType, | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for SumNode { | ||
fn output_types(&self) -> Vec<Type> { | ||
vec![Type::Tensor(self.output.clone())] | ||
} | ||
|
||
fn input_types(&self) -> Vec<Type> { | ||
self.inputs | ||
.iter() | ||
.map(|t| Type::Tensor(t.clone())) | ||
.collect() | ||
} | ||
|
||
fn forward(&self, scope: &mut Scope, node_position: usize) -> TokenStream { | ||
let inputs = self | ||
.inputs | ||
.iter() | ||
.map(|t| scope.tensor_use_owned(t, node_position)); | ||
|
||
let output = &self.output.name; | ||
|
||
quote! { | ||
let #output = #(#inputs)+*; | ||
} | ||
} | ||
|
||
fn into_node(self) -> Node<PS> { | ||
Node::Sum(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use burn::record::FullPrecisionSettings; | ||
|
||
use super::*; | ||
use crate::burn::{ | ||
graph::BurnGraph, | ||
node::{sum::SumNode, test::assert_tokens}, | ||
TensorType, | ||
}; | ||
|
||
#[test] | ||
fn test_codegen_concat() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
|
||
graph.register(SumNode::new( | ||
vec![ | ||
TensorType::new_float("tensor1", 4), | ||
TensorType::new_float("tensor2", 4), | ||
], | ||
TensorType::new_float("tensor3", 4), | ||
)); | ||
|
||
graph.register_input_output( | ||
vec!["tensor1".to_string(), "tensor2".to_string()], | ||
vec!["tensor3".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>, | ||
tensor2: Tensor<B, 4> | ||
) -> Tensor<B, 4> { | ||
let tensor3 = tensor1 + tensor2; | ||
|
||
tensor3 | ||
} | ||
} | ||
}; | ||
|
||
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