-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pre-test * implementing argmax for burn-import from onnx * tidying * fixing return types and tests * addressing feedback * only warn when select_last_index!=0
- Loading branch information
1 parent
de0b49e
commit 13a6f84
Showing
11 changed files
with
255 additions
and
9 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
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,41 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: onnx-tests/tests/argmax/argmax.onnx | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
class Model(nn.Module): | ||
def __init__(self, argmax_dim: int = 0): | ||
super(Model, self).__init__() | ||
self._argmax_dim = argmax_dim | ||
|
||
def forward(self, x): | ||
# Note: only keepdim=True is supported in burn | ||
y = torch.argmax(input=x, dim=self._argmax_dim, keepdim=True) | ||
return y | ||
|
||
def main(): | ||
|
||
# Export to onnx | ||
model = Model(1) | ||
model.eval() | ||
device = torch.device("cpu") | ||
onnx_name = "argmax.onnx" | ||
dummy_input = torch.randn((3, 4), device=device) | ||
torch.onnx.export(model, dummy_input, 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.randn((2, 3), device=device) | ||
print("Test input data shape: {}".format(test_input.shape)) | ||
output = model.forward(test_input) | ||
|
||
print("Test output data shape: {}".format(output.shape)) | ||
|
||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use super::{Node, NodeCodegen}; | ||
use crate::burn::{TensorKind, TensorType, ToTokens, Type}; | ||
|
||
use burn::record::PrecisionSettings; | ||
use quote::quote; | ||
|
||
#[derive(Debug, Clone, new)] | ||
pub struct ArgMaxNode { | ||
pub input: TensorType, | ||
pub output: TensorType, | ||
pub axis: usize, | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for ArgMaxNode { | ||
fn output_types(&self) -> Vec<Type> { | ||
let mut output = self.output.clone(); | ||
output.kind = TensorKind::Int; | ||
vec![Type::Tensor(output)] | ||
} | ||
|
||
fn input_types(&self) -> Vec<crate::burn::Type> { | ||
vec![Type::Tensor(self.input.clone())] | ||
} | ||
|
||
fn forward( | ||
&self, | ||
scope: &mut crate::burn::Scope, | ||
node_position: usize, | ||
) -> proc_macro2::TokenStream { | ||
//NOTE: select_last_index and keep_dims are not supported | ||
let axis = self.axis.to_tokens(); | ||
|
||
let input = scope.tensor_use_owned(&self.input, node_position); | ||
let output = &self.output.name; | ||
|
||
quote! { | ||
let #output = #input.argmax(#axis); | ||
} | ||
} | ||
|
||
fn into_node(self) -> super::Node<PS> { | ||
Node::ArgMax(self) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use burn::record::FullPrecisionSettings; | ||
|
||
use super::*; | ||
use crate::burn::{graph::BurnGraph, node::test::assert_tokens, TensorType}; | ||
|
||
#[test] | ||
fn test_codegen_argmax() { | ||
let mut graph = BurnGraph::<FullPrecisionSettings>::default(); | ||
|
||
graph.register(ArgMaxNode::new( | ||
TensorType::new_float("tensor1", 2), | ||
TensorType::new_int("tensor2", 2), | ||
1, | ||
)); | ||
|
||
graph.register_input_output(vec!["tensor1".to_string()], vec!["tensor2".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, | ||
tensor1: Tensor<B, 2> | ||
) -> Tensor<B, 2, Int> { | ||
let tensor2 = tensor1.argmax(1); | ||
|
||
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
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