forked from tracel-ai/burn
-
Notifications
You must be signed in to change notification settings - Fork 0
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
831335a
commit cb4c23b
Showing
11 changed files
with
252 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pytorch2.1.0:� | ||
a | ||
onnx::GatherElements_0 | ||
onnx::GatherElements_12/GatherElements"GatherElements* | ||
axis� | ||
main_graphZ( | ||
onnx::GatherElements_0 | ||
Z( | ||
onnx::GatherElements_1 | ||
b | ||
2 | ||
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,47 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: onnx-tests/tests/gather/gather.onnx | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class Model(nn.Module): | ||
def __init__(self): | ||
super(Model, self).__init__() | ||
|
||
def forward(self, x, index): | ||
x = torch.gather(x, 1, index) | ||
return x | ||
|
||
|
||
def main(): | ||
# Set random seed for reproducibility | ||
torch.manual_seed(0) | ||
|
||
# Export to onnx | ||
model = Model() | ||
model.eval() | ||
device = torch.device("cpu") | ||
onnx_name = "gather.onnx" | ||
dummy_input = torch.randn(2, 2, device=device) | ||
dummy_index = torch.randint(high=2, size=(2, 2), device=device, dtype=torch.int64) | ||
|
||
torch.onnx.export(model, (dummy_input, dummy_index), onnx_name, | ||
verbose=False, opset_version=16) | ||
|
||
print("Finished exporting model to {}".format(onnx_name)) | ||
|
||
# Output some test data for use in the test | ||
test_input = torch.tensor([[1.0, 2.0], | ||
[3.0, 4.0]]) | ||
test_index = torch.tensor([[0, 0], | ||
[1, 0]]) | ||
|
||
print("Test input data: {}, {}".format(test_input, test_index)) | ||
output = model.forward(test_input, test_index) | ||
print("Test output data: {}".format(output)) | ||
|
||
|
||
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,106 @@ | ||
use super::{Node, NodeCodegen}; | ||
use crate::burn::{TensorType, ToTokens, Type}; | ||
|
||
use burn::record::PrecisionSettings; | ||
use quote::quote; | ||
|
||
#[derive(Debug, Clone, new)] | ||
pub struct GatherNode { | ||
pub input: TensorType, | ||
pub index: TensorType, | ||
pub output: TensorType, | ||
pub dim: usize, | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for GatherNode { | ||
fn output_types(&self) -> Vec<Type> { | ||
vec![Type::Tensor(self.output.clone())] | ||
} | ||
|
||
fn input_types(&self) -> Vec<crate::burn::Type> { | ||
vec![ | ||
Type::Tensor(self.input.clone()), | ||
Type::Tensor(self.index.clone()), | ||
] | ||
} | ||
|
||
fn forward( | ||
&self, | ||
scope: &mut crate::burn::Scope, | ||
node_position: usize, | ||
) -> proc_macro2::TokenStream { | ||
let dim = self.dim.to_tokens(); | ||
let input = scope.tensor_use_owned(&self.input, node_position); | ||
let index = scope.tensor_use_owned(&self.index, node_position); | ||
let output = &self.output.name; | ||
|
||
quote! { | ||
let #output = #input.gather(#dim, #index); | ||
} | ||
} | ||
|
||
fn into_node(self) -> super::Node<PS> { | ||
Node::Gather(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use burn::record::FullPrecisionSettings; | ||
|
||
use super::*; | ||
use crate::burn::{ | ||
graph::BurnGraph, | ||
node::{gather::GatherNode, test::assert_tokens}, | ||
TensorType, | ||
}; | ||
|
||
#[test] | ||
fn test_codegen_gather() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
|
||
graph.register(GatherNode::new( | ||
TensorType::new_float("tensor1", 2), | ||
TensorType::new_int("tensor2", 2), | ||
TensorType::new_float("tensor3", 2), | ||
1, | ||
)); | ||
|
||
graph.register_input_output( | ||
vec!["tensor1".to_string(), "tensor2".to_string()], | ||
vec!["tensor3".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>, | ||
} | ||
|
||
impl<B: Backend> Model <B> { | ||
#[allow(unused_variables)] | ||
pub fn new_with(record: ModelRecord<B>) -> Self { | ||
Self { | ||
phantom: core::marker::PhantomData, | ||
} | ||
} | ||
|
||
#[allow(clippy::let_and_return)] | ||
pub fn forward(&self, tensor1: Tensor<B, 2>, tensor2: Tensor<B, 2, Int>) -> Tensor<B, 2> { | ||
let tensor3 = tensor1.gather(1, 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
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