-
Notifications
You must be signed in to change notification settings - Fork 474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added range onnx import #1834
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,34 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: onnx-tests/tests/range/range.onnx | ||
|
||
import onnx | ||
from onnx import helper, TensorProto | ||
|
||
def main(): | ||
node = onnx.helper.make_node( | ||
'Range', | ||
name='range', | ||
inputs=['start', 'end', 'step'], | ||
outputs=['output'] | ||
) | ||
|
||
graph_def = helper.make_graph( | ||
nodes=[node], | ||
name='RangeGraph', | ||
inputs=[ | ||
helper.make_tensor_value_info('start', TensorProto.INT64, []), | ||
helper.make_tensor_value_info('end', TensorProto.INT64, []), | ||
helper.make_tensor_value_info('step', TensorProto.INT64, []) | ||
], | ||
outputs=[ | ||
helper.make_tensor_value_info('output', TensorProto.INT64, [5]) | ||
], | ||
) | ||
|
||
model_def = helper.make_model(graph_def, producer_name='range') | ||
|
||
onnx.save(model_def, 'range.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,118 @@ | ||
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 RangeNode { | ||
pub start: Type, | ||
pub end: Type, | ||
pub step: Type, | ||
pub output: TensorType, | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for RangeNode { | ||
fn output_types(&self) -> Vec<Type> { | ||
vec![Type::Tensor(self.output.clone())] | ||
} | ||
|
||
fn input_types(&self) -> Vec<Type> { | ||
vec![self.start.clone(), self.end.clone(), self.step.clone()] | ||
} | ||
|
||
fn forward(&self, _scope: &mut Scope, _node_position: usize) -> TokenStream { | ||
let output = &self.output.name; | ||
|
||
let start = match &self.start { | ||
Type::Scalar(s) => { | ||
let name = s.name.clone(); | ||
quote! { #name } | ||
} | ||
_ => panic!("Start must be a scalar"), | ||
}; | ||
|
||
let end = match &self.end { | ||
Type::Scalar(s) => { | ||
let name = s.name.clone(); | ||
quote! { #name } | ||
} | ||
_ => panic!("End must be a scalar"), | ||
}; | ||
|
||
let step = match &self.step { | ||
Type::Scalar(s) => { | ||
let name = s.name.clone(); | ||
quote! { #name } | ||
} | ||
_ => panic!("Step must be a scalar"), | ||
}; | ||
|
||
quote! { | ||
let #output = Tensor::arange_step(#start..#end, #step as usize, &*self.device); | ||
} | ||
} | ||
fn into_node(self) -> Node<PS> { | ||
Node::Range(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::burn::graph::BurnGraph; | ||
use crate::burn::node::test::assert_tokens; | ||
use crate::burn::{ScalarKind, ScalarType}; | ||
use burn::record::FullPrecisionSettings; | ||
|
||
#[test] | ||
fn codegen_nodes_range() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
|
||
graph.register( | ||
RangeNode::new( | ||
Type::Scalar(ScalarType::new("start", ScalarKind::Int64)), | ||
Type::Scalar(ScalarType::new("end", ScalarKind::Int64)), | ||
Type::Scalar(ScalarType::new("step", ScalarKind::Int64)), | ||
TensorType::new_int("output", 1), | ||
) | ||
.into_node(), | ||
); | ||
graph.register_input_output( | ||
vec!["start".to_string(), "end".to_string(), "step".to_string()], | ||
vec!["output".to_string()], | ||
); | ||
|
||
let expected = quote! { | ||
use burn::tensor::Int; | ||
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, start: i64, end: i64, step: i64) -> Tensor<B, 1, Int> { | ||
let output = Tensor::arange_step(start..end, step as usize, &*self.device); | ||
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the three fields are all supposed to be scalar, I suggest we change the node fields to:
and the logic to check the extract the scalar types can go in the
range_conversion
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well yes, scalars are simply represented as 0-dim tensors. Basically what I am suggesting is just to move the type handling outside of the forward node method to the
range_conversion
function instead, a bit like this: