-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added slice onnx import (#1856)
* feat: added slice onnx import * fix: axes, steps handling
- Loading branch information
1 parent
dd60446
commit 671ec8c
Showing
12 changed files
with
318 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,101 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: onnx-tests/tests/slice/slice.onnx | ||
|
||
import onnx | ||
from onnx import helper, TensorProto | ||
|
||
def main() -> None: | ||
# Starts | ||
starts_val = [0,0] # Example shape value | ||
starts_tensor = helper.make_tensor( | ||
name="starts", | ||
data_type=TensorProto.INT64, | ||
dims=[len(starts_val)], | ||
vals=starts_val, | ||
) | ||
starts_node = helper.make_node( | ||
"Constant", | ||
name="starts_constant", | ||
inputs=[], | ||
outputs=["starts"], | ||
value=starts_tensor, | ||
) | ||
|
||
# Ends | ||
ends_val = [1,5] # Example shape value | ||
ends_tensor = helper.make_tensor( | ||
name="ends", | ||
data_type=TensorProto.INT64, | ||
dims=[len(ends_val)], | ||
vals=ends_val, | ||
) | ||
ends_node = helper.make_node( | ||
"Constant", | ||
name="ends_constant", | ||
inputs=[], | ||
outputs=["ends"], | ||
value=ends_tensor, | ||
) | ||
|
||
# Axes | ||
axes_val = [0,1] # Example shape value | ||
axes_tensor = helper.make_tensor( | ||
name="axes", | ||
data_type=TensorProto.INT64, | ||
dims=[len(axes_val)], | ||
vals=axes_val, | ||
) | ||
axes_node = helper.make_node( | ||
"Constant", | ||
name="axes_constant", | ||
inputs=[], | ||
outputs=["axes"], | ||
value=axes_tensor, | ||
) | ||
|
||
# Steps | ||
steps_val = [1, 1] # Example shape value | ||
steps_tensor = helper.make_tensor( | ||
name="steps", | ||
data_type=TensorProto.INT64, | ||
dims=[len(steps_val)], | ||
vals=steps_val, | ||
) | ||
steps_node = helper.make_node( | ||
"Constant", | ||
name="steps_constant", | ||
inputs=[], | ||
outputs=["steps"], | ||
value=steps_tensor, | ||
) | ||
|
||
# Define the Slice node that uses the outputs from the constant nodes | ||
slice_node = helper.make_node( | ||
"Slice", | ||
name="slice_node", | ||
inputs=["input_tensor", "starts", "ends", "axes", "steps"], | ||
outputs=["output"], | ||
) | ||
|
||
# Create the graph | ||
graph_def = helper.make_graph( | ||
nodes=[starts_node, ends_node, axes_node, steps_node, slice_node], | ||
name="SliceGraph", | ||
inputs=[ | ||
helper.make_tensor_value_info("input_tensor", TensorProto.FLOAT, [2, 10]), | ||
], | ||
outputs=[ | ||
helper.make_tensor_value_info("output", TensorProto.FLOAT, [1, 5]) | ||
], | ||
) | ||
|
||
# Create the model | ||
model_def = helper.make_model(graph_def, producer_name="slice") | ||
|
||
# Save the model to a file | ||
onnx.save(model_def, "slice.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,90 @@ | ||
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 SliceNode { | ||
pub input: TensorType, | ||
pub output: TensorType, | ||
pub starts: Vec<usize>, | ||
pub ends: Vec<usize>, | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for SliceNode { | ||
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 starts = &self.starts; | ||
let ends = &self.ends; | ||
|
||
quote! { | ||
let #output = #input.slice([#(#starts..#ends),*]); | ||
} | ||
} | ||
fn into_node(self) -> Node<PS> { | ||
Node::Slice(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use burn::record::FullPrecisionSettings; | ||
|
||
use super::*; | ||
use crate::burn::{ | ||
graph::BurnGraph, | ||
node::{slice::SliceNode, test::assert_tokens}, | ||
TensorType, | ||
}; | ||
|
||
#[test] | ||
fn test_codegen_slice() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
graph.register(SliceNode::new( | ||
TensorType::new_float("tensor1", 4), | ||
TensorType::new_float("tensor2", 4), | ||
vec![0, 0, 0, 0], | ||
vec![1, 1, 1, 1], | ||
)); | ||
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.slice([0usize..1usize,0usize..1usize,0usize..1usize,0usize..1usize]); | ||
|
||
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
Oops, something went wrong.