Skip to main content

oa/ml/nn/
softmax.gen.rs

1// @generated by tool/gen/nn/generate.py; DO NOT EDIT.
2// Source: tool/gen/nn/schema/ml/ml_nn_softmax.json
3// generator_version=8; schema_sha256=d733db02905d6952e4a537742dd04991a4596a5f7fad51b723228a35c21c3282
4
5use crate::{Matrix, Result, matrix};
6
7use super::super::{Module, ModuleRegistry};
8
9// OA donor: oacpp/source/cpp/lib/oa/ml/nn/softmax/softmax.cpp
10// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
11/// Parameterless axis-aware Softmax module.
12pub struct Softmax {
13	dim: i32,
14	registry: ModuleRegistry,
15}
16
17impl Softmax {
18	/// Construct a Softmax over `dim`, where `-1` selects the last dimension.
19	pub const fn new(dim: i32) -> Self {
20		Self {
21			dim,
22			registry: ModuleRegistry::new(),
23		}
24	}
25
26	/// Return the selected dimension.
27	pub const fn dim(&self) -> i32 {
28		self.dim
29	}
30
31	/// Apply Softmax without submitting or waiting.
32	///
33	/// # Errors
34	///
35	/// Returns an error from the underlying Matrix operation.
36	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
37		matrix::softmax(input, self.dim)
38	}
39}
40
41impl Default for Softmax {
42	fn default() -> Self {
43		Self::new(-1)
44	}
45}
46
47impl Module for Softmax {
48	fn forward(&self, input: &Matrix) -> Result<Matrix> {
49		Softmax::forward(self, input)
50	}
51
52	fn registry(&self) -> &ModuleRegistry {
53		&self.registry
54	}
55}
56
57// OA donor: oacpp/source/cpp/lib/oa/ml/nn/softmax/softmax.cpp
58// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
59/// Parameterless axis-aware LogSoftmax module.
60pub struct LogSoftmax {
61	dim: i32,
62	registry: ModuleRegistry,
63}
64
65impl LogSoftmax {
66	/// Construct a LogSoftmax over `dim`, where `-1` selects the last dimension.
67	pub const fn new(dim: i32) -> Self {
68		Self {
69			dim,
70			registry: ModuleRegistry::new(),
71		}
72	}
73
74	/// Return the selected dimension.
75	pub const fn dim(&self) -> i32 {
76		self.dim
77	}
78
79	/// Apply LogSoftmax without submitting or waiting.
80	///
81	/// # Errors
82	///
83	/// Returns an error from the underlying Matrix operation.
84	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
85		matrix::log_softmax(input, self.dim)
86	}
87}
88
89impl Default for LogSoftmax {
90	fn default() -> Self {
91		Self::new(-1)
92	}
93}
94
95impl Module for LogSoftmax {
96	fn forward(&self, input: &Matrix) -> Result<Matrix> {
97		LogSoftmax::forward(self, input)
98	}
99
100	fn registry(&self) -> &ModuleRegistry {
101		&self.registry
102	}
103}