-
Notifications
You must be signed in to change notification settings - Fork 449
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
ab50143
commit 73ebba5
Showing
9 changed files
with
398 additions
and
1 deletion.
There are no files selected for viewing
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,49 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# used to generate model: prelu.onnx | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class Model(nn.Module): | ||
def __init__(self): | ||
super(Model, self).__init__() | ||
self.relu1 = nn.PReLU() | ||
|
||
def forward(self, x): | ||
x = self.relu1(x) | ||
return x | ||
|
||
|
||
def main(): | ||
|
||
# Set seed for reproducibility | ||
torch.manual_seed(42) | ||
|
||
torch.set_printoptions(precision=8) | ||
|
||
# Export to onnx | ||
model = Model() | ||
model.eval() | ||
device = torch.device("cpu") | ||
|
||
file_name = "prelu.onnx" | ||
test_input = torch.randn(2, 3, device=device) | ||
torch.onnx.export(model, test_input, file_name, | ||
verbose=False, opset_version=16) | ||
|
||
print("Finished exporting model to {}".format(file_name)) | ||
|
||
# Output some test data for use in the test | ||
print("Test input data of ones: {}".format(test_input)) | ||
print("Test input data shape of ones: {}".format(test_input.shape)) | ||
output = model.forward(test_input) | ||
print("Test output data shape: {}".format(output.shape)) | ||
|
||
print("Test output: {}".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,100 @@ | ||
use super::{Node, NodeCodegen, SerializationBackend}; | ||
use crate::burn::{BurnImports, OtherType, Scope, TensorType, ToTokens, Type}; | ||
use burn::{ | ||
module::{Param, ParamId}, | ||
nn::{PReluConfig, PReluRecord}, | ||
record::{PrecisionSettings, Record}, | ||
tensor::{DataSerialize, Tensor}, | ||
}; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use serde::Serialize; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct PReluNode<PS: PrecisionSettings> { | ||
pub field: OtherType, | ||
pub input: TensorType, | ||
pub output: TensorType, | ||
pub alpha: DataSerialize<PS::FloatElem>, | ||
pub config: PReluConfig, | ||
} | ||
|
||
impl<PS: PrecisionSettings> PReluNode<PS> { | ||
pub fn new<S: AsRef<str>>( | ||
name: S, | ||
input: TensorType, | ||
output: TensorType, | ||
alpha: DataSerialize<PS::FloatElem>, | ||
config: PReluConfig, | ||
) -> Self { | ||
Self { | ||
field: OtherType::new( | ||
name, | ||
quote! { | ||
PRelu<B> | ||
}, | ||
), | ||
input, | ||
output, | ||
alpha, | ||
config, | ||
} | ||
} | ||
} | ||
|
||
impl<PS: PrecisionSettings> NodeCodegen<PS> for PReluNode<PS> { | ||
fn input_types(&self) -> Vec<Type> { | ||
vec![Type::Tensor(self.input.clone())] | ||
} | ||
fn output_types(&self) -> Vec<Type> { | ||
vec![Type::Tensor(self.output.clone())] | ||
} | ||
fn field_type(&self) -> Option<Type> { | ||
Some(Type::Other(self.field.clone())) | ||
} | ||
|
||
fn field_init(&self) -> Option<TokenStream> { | ||
let name = &self.field.name; | ||
|
||
let num_parameters = self.config.num_parameters.to_tokens(); | ||
let alpha = self.config.alpha.to_tokens(); | ||
let tokens = quote! { | ||
let #name = PReluConfig::new(#num_parameters, #alpha) | ||
.init(device); | ||
}; | ||
|
||
Some(tokens) | ||
} | ||
|
||
fn field_serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { | ||
let device = Default::default(); | ||
let record = PReluRecord::<SerializationBackend> { | ||
alpha: Param::initialized( | ||
ParamId::new(), | ||
Tensor::from_data(self.alpha.clone().convert(), &device), | ||
), | ||
}; | ||
|
||
let item = Record::into_item::<PS>(record); | ||
item.serialize(serializer) | ||
} | ||
|
||
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 field = &self.field.name; | ||
|
||
quote! { | ||
let #output = self.#field.forward(#input); | ||
} | ||
} | ||
fn register_imports(&self, imports: &mut BurnImports) { | ||
imports.register("burn::nn::PRelu"); | ||
imports.register("burn::nn::prelu::PRelu"); | ||
imports.register("burn::nn::prelu::PReluConfig"); | ||
} | ||
|
||
fn into_node(self) -> Node<PS> { | ||
Node::PRelu(self) | ||
} | ||
} |
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.