Skip to main content

oa/ml/nn/
upsample.gen.rs

1// @generated by tool/gen/nn/generate.py; DO NOT EDIT.
2// Source: tool/gen/nn/schema/ml/ml_nn_upsample.json
3// generator_version=8; schema_sha256=0f6ab1946555388276c1eec0c3311e7a35849e32afab5c004cec539fa0643728
4
5use crate::{Matrix, Result};
6
7pub use super::super::matrix::UpsampleMode;
8
9use super::super::{Module, ModuleRegistry, matrix};
10
11// OA donor: oacpp/source/cpp/lib/oa/ml/nn/upsample/upsample.cpp
12// Adaptation: Preserved bilinear default and checked scale; existing Matrix provider owns interpolation and deterministic adjoints
13/// Parameterless spatial upsampling module for FP32 NCHW matrices.
14pub struct Upsample {
15	scale_factor: usize,
16	mode: UpsampleMode,
17	registry: ModuleRegistry,
18}
19
20impl Upsample {
21	/// Construct bilinear Upsample with the requested integer scale factor.
22	///
23	/// # Errors
24	///
25	/// Returns an error when `scale_factor` is zero.
26	pub fn new(scale_factor: usize) -> Result<Self> {
27		Self::new_with_mode(scale_factor, UpsampleMode::Bilinear)
28	}
29
30	/// Construct Upsample with an explicit interpolation mode.
31	///
32	/// # Errors
33	///
34	/// Returns an error when `scale_factor` is zero.
35	pub fn new_with_mode(scale_factor: usize, mode: UpsampleMode) -> Result<Self> {
36		if scale_factor == 0 {
37			return Err(crate::Error::invalid_argument(
38				"Upsample scale factor must be nonzero",
39			));
40		}
41		Ok(Self {
42			scale_factor,
43			mode,
44			registry: ModuleRegistry::new(),
45		})
46	}
47
48	/// Upsample one NCHW Matrix without submitting or waiting.
49	///
50	/// # Errors
51	///
52	/// Returns an error from the underlying Matrix operation.
53	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
54		matrix::upsample_2d(input, self.scale_factor, self.mode)
55	}
56
57	/// Return the integer spatial scale factor.
58	pub const fn scale_factor(&self) -> usize {
59		self.scale_factor
60	}
61
62	/// Return the interpolation mode.
63	pub const fn mode(&self) -> UpsampleMode {
64		self.mode
65	}
66}
67
68impl Module for Upsample {
69	fn forward(&self, input: &Matrix) -> Result<Matrix> {
70		Upsample::forward(self, input)
71	}
72
73	fn registry(&self) -> &ModuleRegistry {
74		&self.registry
75	}
76}