|
| 1 | +use super::{Node, NodeCodegen}; |
| 2 | +use crate::burn::{OtherType, Scope, TensorType, Type}; |
| 3 | +use burn::module::Module; |
| 4 | +use burn::record::PrecisionSettings; |
| 5 | +use proc_macro2::TokenStream; |
| 6 | +use quote::quote; |
| 7 | + |
| 8 | +#[derive(Module, Debug, Clone)] |
| 9 | +pub enum ResizeMode { |
| 10 | + Nearest, |
| 11 | + Linear, |
| 12 | + Cubic, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(new, Module, Debug, Clone)] |
| 16 | +pub struct ResizeOptions { |
| 17 | + pub mode: ResizeMode, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Clone)] |
| 21 | +pub struct ResizeNode { |
| 22 | + pub field: OtherType, |
| 23 | + pub input: TensorType, |
| 24 | + pub output: TensorType, |
| 25 | + pub output_size: TensorType, |
| 26 | + pub config: ResizeOptions, |
| 27 | +} |
| 28 | + |
| 29 | +impl ResizeNode { |
| 30 | + pub fn new<S: AsRef<str>>( |
| 31 | + name: S, |
| 32 | + input: TensorType, |
| 33 | + output: TensorType, |
| 34 | + output_size: TensorType, |
| 35 | + config: ResizeOptions, |
| 36 | + ) -> Self { |
| 37 | + Self { |
| 38 | + field: OtherType::new( |
| 39 | + name, |
| 40 | + quote! { |
| 41 | + burn::module::Ignored<InterpolateOptions> |
| 42 | + }, |
| 43 | + ), |
| 44 | + input, |
| 45 | + output, |
| 46 | + output_size, |
| 47 | + config, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<PS: PrecisionSettings> NodeCodegen<PS> for ResizeNode { |
| 53 | + fn output_types(&self) -> Vec<Type> { |
| 54 | + vec![Type::Tensor(self.output.clone())] |
| 55 | + } |
| 56 | + |
| 57 | + fn input_types(&self) -> Vec<Type> { |
| 58 | + vec![ |
| 59 | + Type::Tensor(self.input.clone()), |
| 60 | + Type::Tensor(self.output_size.clone()), |
| 61 | + ] |
| 62 | + } |
| 63 | + |
| 64 | + fn field_type(&self) -> Option<Type> { |
| 65 | + Some(Type::Other(self.field.clone())) |
| 66 | + } |
| 67 | + |
| 68 | + fn field_init(&self) -> Option<TokenStream> { |
| 69 | + let name = &self.field.name; |
| 70 | + |
| 71 | + let mode = match self.config.mode { |
| 72 | + ResizeMode::Linear => quote! { InterpolateMode::Bilinear }, |
| 73 | + ResizeMode::Nearest => quote! { InterpolateMode::Nearest }, |
| 74 | + ResizeMode::Cubic => quote! { InterpolateMode::Bicubic }, |
| 75 | + }; |
| 76 | + |
| 77 | + let tokens = quote! { |
| 78 | + let #name = InterpolateOptions { |
| 79 | + mode: #mode, |
| 80 | + }; |
| 81 | + let #name = burn::module::Ignored(#name); |
| 82 | + }; |
| 83 | + |
| 84 | + Some(tokens) |
| 85 | + } |
| 86 | + |
| 87 | + fn field_serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 88 | + S::serialize_none(serializer) |
| 89 | + } |
| 90 | + |
| 91 | + fn forward(&self, scope: &mut Scope, node_position: usize) -> TokenStream { |
| 92 | + let input = scope.tensor_use_owned(&self.input, node_position); |
| 93 | + let output_size = scope.tensor_use_owned(&self.output_size, node_position); |
| 94 | + let output = &self.output.name; |
| 95 | + |
| 96 | + let field = &self.field.name; |
| 97 | + |
| 98 | + quote! { |
| 99 | + let output_size_raw = #output_size.to_data().value; |
| 100 | + let mut output_size = [0usize; 2]; |
| 101 | + |
| 102 | + for (i, &x) in output_size_raw.iter().rev().take(2).rev().enumerate() { |
| 103 | + output_size[i] = x.elem::<i64>() as usize; |
| 104 | + } |
| 105 | + |
| 106 | + let #output = interpolate( |
| 107 | + #input, |
| 108 | + output_size, |
| 109 | + self.#field.0.clone(), |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + fn into_node(self) -> Node<PS> { |
| 115 | + Node::Resize(self) |
| 116 | + } |
| 117 | + |
| 118 | + fn register_imports(&self, imports: &mut crate::burn::BurnImports) { |
| 119 | + imports.register("burn::tensor::ElementConversion"); |
| 120 | + imports.register("burn::tensor::module::interpolate"); |
| 121 | + imports.register("burn::tensor::ops::InterpolateMode"); |
| 122 | + imports.register("burn::tensor::ops::InterpolateOptions"); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(test)] |
| 127 | +mod tests { |
| 128 | + use burn::record::FullPrecisionSettings; |
| 129 | + |
| 130 | + use super::*; |
| 131 | + use crate::burn::{ |
| 132 | + graph::BurnGraph, |
| 133 | + node::{resize::ResizeNode, test::assert_tokens}, |
| 134 | + TensorType, |
| 135 | + }; |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn test_codegen_nodes() { |
| 139 | + let mut graph = BurnGraph::<FullPrecisionSettings>::default(); |
| 140 | + |
| 141 | + graph.register(ResizeNode::new( |
| 142 | + "resize", |
| 143 | + TensorType::new_float("tensor1", 4), |
| 144 | + TensorType::new_float("tensor2", 4), |
| 145 | + TensorType::new_int("output_size", 1), |
| 146 | + ResizeOptions::new(ResizeMode::Linear), |
| 147 | + )); |
| 148 | + |
| 149 | + graph.register_input_output( |
| 150 | + vec!["tensor1".to_string(), "output_size".to_string()], |
| 151 | + vec!["tensor2".to_string()], |
| 152 | + ); |
| 153 | + |
| 154 | + let expected = quote! { |
| 155 | + use burn::tensor::module::interpolate; |
| 156 | + use burn::tensor::ops::InterpolateMode; |
| 157 | + use burn::tensor::ops::InterpolateOptions; |
| 158 | + use burn::tensor::ElementConversion; |
| 159 | + use burn::tensor::Int; |
| 160 | + use burn::{ |
| 161 | + module::Module, |
| 162 | + tensor::{backend::Backend, Tensor}, |
| 163 | + }; |
| 164 | + |
| 165 | + #[derive(Module, Debug)] |
| 166 | + pub struct Model<B: Backend> { |
| 167 | + resize: burn::module::Ignored<InterpolateOptions>, |
| 168 | + phantom: core::marker::PhantomData<B>, |
| 169 | + device: burn::module::Ignored<B::Device>, |
| 170 | + } |
| 171 | + |
| 172 | + impl<B: Backend> Model <B> { |
| 173 | + #[allow(unused_variables)] |
| 174 | + pub fn new(device: &B::Device) -> Self { |
| 175 | + let resize = InterpolateOptions { |
| 176 | + mode: InterpolateMode::Bilinear, |
| 177 | + }; |
| 178 | + let resize = burn::module::Ignored(resize); |
| 179 | + Self { |
| 180 | + resize, |
| 181 | + phantom: core::marker::PhantomData, |
| 182 | + device: burn::module::Ignored(device.clone()), |
| 183 | + } |
| 184 | + } |
| 185 | + #[allow(clippy::let_and_return, clippy::approx_constant)] |
| 186 | + pub fn forward( |
| 187 | + &self, |
| 188 | + tensor1: Tensor<B, 4>, |
| 189 | + output_size: Tensor<B, 1, Int> |
| 190 | + ) -> Tensor<B, 4> { |
| 191 | + let output_size_raw = output_size.to_data().value; |
| 192 | + let mut output_size = [0usize; 2]; |
| 193 | + |
| 194 | + for (i, &x) in output_size_raw.iter().rev().take(2).rev().enumerate() { |
| 195 | + output_size[i] = x.elem::<i64>() as usize; |
| 196 | + } |
| 197 | + |
| 198 | + let tensor2 = interpolate(tensor1, output_size, self.resize.0.clone()); |
| 199 | + |
| 200 | + tensor2 |
| 201 | + } |
| 202 | + } |
| 203 | + }; |
| 204 | + |
| 205 | + assert_tokens(graph.codegen(), expected); |
| 206 | + } |
| 207 | +} |
0 commit comments