-
Notifications
You must be signed in to change notification settings - Fork 474
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
e61b026
commit 99f0f56
Showing
10 changed files
with
199 additions
and
3 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,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,117 @@ | ||
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::{ | ||
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> { | ||
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