Skip to main content

coeus_ops/fuse/
ops_impl.rs

1use crate::fuse::expr_node::{BinaryExpr, Expr, ScalarVal, UnaryExpr};
2use crate::fuse::op_tags::{
3    Abs, Add, Ceil, Cos, Div, Elu, EluGrad, Exp, Floor, Gelu, GeluGrad, GeluTanh, GeluTanhGrad,
4    Log, Mish, MishGrad, Mul, Neg, Recip, Relu, Round, Sigmoid, Sign, Silu, SiluGrad, Sin,
5    Softplus, SoftplusGrad, Sqrt, Sub, Tanh, Trunc,
6};
7use coeus_core::Scalar;
8
9// ── Operator Overloading ──
10
11impl<L, R> std::ops::Add<Expr<R>> for Expr<L> {
12    type Output = Expr<BinaryExpr<Add, L, R>>;
13    #[inline(always)]
14    fn add(self, rhs: Expr<R>) -> Self::Output {
15        Expr(BinaryExpr {
16            op: Add,
17            left: self.0,
18            right: rhs.0,
19        })
20    }
21}
22
23impl<L, R> std::ops::Sub<Expr<R>> for Expr<L> {
24    type Output = Expr<BinaryExpr<Sub, L, R>>;
25    #[inline(always)]
26    fn sub(self, rhs: Expr<R>) -> Self::Output {
27        Expr(BinaryExpr {
28            op: Sub,
29            left: self.0,
30            right: rhs.0,
31        })
32    }
33}
34
35impl<L, R> std::ops::Mul<Expr<R>> for Expr<L> {
36    type Output = Expr<BinaryExpr<Mul, L, R>>;
37    #[inline(always)]
38    fn mul(self, rhs: Expr<R>) -> Self::Output {
39        Expr(BinaryExpr {
40            op: Mul,
41            left: self.0,
42            right: rhs.0,
43        })
44    }
45}
46
47impl<L, R> std::ops::Div<Expr<R>> for Expr<L> {
48    type Output = Expr<BinaryExpr<Div, L, R>>;
49    #[inline(always)]
50    fn div(self, rhs: Expr<R>) -> Self::Output {
51        Expr(BinaryExpr {
52            op: Div,
53            left: self.0,
54            right: rhs.0,
55        })
56    }
57}
58
59impl<L, T: Scalar> std::ops::Add<T> for Expr<L> {
60    type Output = Expr<BinaryExpr<Add, L, ScalarVal<T>>>;
61    #[inline(always)]
62    fn add(self, rhs: T) -> Self::Output {
63        Expr(BinaryExpr {
64            op: Add,
65            left: self.0,
66            right: ScalarVal(rhs),
67        })
68    }
69}
70
71impl<L, T: Scalar> std::ops::Sub<T> for Expr<L> {
72    type Output = Expr<BinaryExpr<Sub, L, ScalarVal<T>>>;
73    #[inline(always)]
74    fn sub(self, rhs: T) -> Self::Output {
75        Expr(BinaryExpr {
76            op: Sub,
77            left: self.0,
78            right: ScalarVal(rhs),
79        })
80    }
81}
82
83impl<L, T: Scalar> std::ops::Mul<T> for Expr<L> {
84    type Output = Expr<BinaryExpr<Mul, L, ScalarVal<T>>>;
85    #[inline(always)]
86    fn mul(self, rhs: T) -> Self::Output {
87        Expr(BinaryExpr {
88            op: Mul,
89            left: self.0,
90            right: ScalarVal(rhs),
91        })
92    }
93}
94
95impl<L, T: Scalar> std::ops::Div<T> for Expr<L> {
96    type Output = Expr<BinaryExpr<Div, L, ScalarVal<T>>>;
97    #[inline(always)]
98    fn div(self, rhs: T) -> Self::Output {
99        Expr(BinaryExpr {
100            op: Div,
101            left: self.0,
102            right: ScalarVal(rhs),
103        })
104    }
105}
106
107impl<L> std::ops::Neg for Expr<L> {
108    type Output = Expr<UnaryExpr<Neg, L>>;
109    #[inline(always)]
110    fn neg(self) -> Self::Output {
111        Expr(UnaryExpr {
112            op: Neg,
113            child: self.0,
114        })
115    }
116}
117
118// ── Activation methods ──
119
120impl<L> Expr<L> {
121    /// Apply ReLU to this expression.
122    #[inline(always)]
123    pub fn relu(self) -> Expr<UnaryExpr<Relu, L>> {
124        Expr(UnaryExpr {
125            op: Relu,
126            child: self.0,
127        })
128    }
129
130    /// Apply sigmoid to this expression.
131    #[inline(always)]
132    pub fn sigmoid(self) -> Expr<UnaryExpr<Sigmoid, L>> {
133        Expr(UnaryExpr {
134            op: Sigmoid,
135            child: self.0,
136        })
137    }
138
139    /// Apply tanh to this expression.
140    #[inline(always)]
141    pub fn tanh(self) -> Expr<UnaryExpr<Tanh, L>> {
142        Expr(UnaryExpr {
143            op: Tanh,
144            child: self.0,
145        })
146    }
147
148    /// Apply exact GELU to this expression.
149    #[inline(always)]
150    pub fn gelu(self) -> Expr<UnaryExpr<Gelu, L>> {
151        Expr(UnaryExpr {
152            op: Gelu,
153            child: self.0,
154        })
155    }
156
157    /// Apply exact GELU gradient to this expression.
158    #[inline(always)]
159    pub fn gelu_grad(self) -> Expr<UnaryExpr<GeluGrad, L>> {
160        Expr(UnaryExpr {
161            op: GeluGrad,
162            child: self.0,
163        })
164    }
165
166    /// Apply sine to this expression.
167    #[inline(always)]
168    pub fn sin(self) -> Expr<UnaryExpr<Sin, L>> {
169        Expr(UnaryExpr {
170            op: Sin,
171            child: self.0,
172        })
173    }
174
175    /// Apply cosine to this expression.
176    #[inline(always)]
177    pub fn cos(self) -> Expr<UnaryExpr<Cos, L>> {
178        Expr(UnaryExpr {
179            op: Cos,
180            child: self.0,
181        })
182    }
183
184    /// Apply exponential to this expression.
185    #[inline(always)]
186    pub fn exp(self) -> Expr<UnaryExpr<Exp, L>> {
187        Expr(UnaryExpr {
188            op: Exp,
189            child: self.0,
190        })
191    }
192
193    /// Apply natural log to this expression.
194    #[inline(always)]
195    pub fn log(self) -> Expr<UnaryExpr<Log, L>> {
196        Expr(UnaryExpr {
197            op: Log,
198            child: self.0,
199        })
200    }
201
202    /// Apply absolute value to this expression.
203    #[inline(always)]
204    pub fn abs(self) -> Expr<UnaryExpr<Abs, L>> {
205        Expr(UnaryExpr {
206            op: Abs,
207            child: self.0,
208        })
209    }
210
211    /// Apply square root to this expression.
212    #[inline(always)]
213    pub fn sqrt(self) -> Expr<UnaryExpr<Sqrt, L>> {
214        Expr(UnaryExpr {
215            op: Sqrt,
216            child: self.0,
217        })
218    }
219
220    /// Apply SiLU to this expression.
221    #[inline(always)]
222    pub fn silu(self) -> Expr<UnaryExpr<Silu, L>> {
223        Expr(UnaryExpr {
224            op: Silu,
225            child: self.0,
226        })
227    }
228
229    /// Apply SiLU gradient to this expression.
230    #[inline(always)]
231    pub fn silu_grad(self) -> Expr<UnaryExpr<SiluGrad, L>> {
232        Expr(UnaryExpr {
233            op: SiluGrad,
234            child: self.0,
235        })
236    }
237
238    /// Apply Mish to this expression.
239    #[inline(always)]
240    pub fn mish(self) -> Expr<UnaryExpr<Mish, L>> {
241        Expr(UnaryExpr {
242            op: Mish,
243            child: self.0,
244        })
245    }
246
247    /// Apply Mish gradient to this expression.
248    #[inline(always)]
249    pub fn mish_grad(self) -> Expr<UnaryExpr<MishGrad, L>> {
250        Expr(UnaryExpr {
251            op: MishGrad,
252            child: self.0,
253        })
254    }
255
256    /// Apply ELU to this expression.
257    #[inline(always)]
258    pub fn elu(self) -> Expr<UnaryExpr<Elu, L>> {
259        Expr(UnaryExpr {
260            op: Elu,
261            child: self.0,
262        })
263    }
264
265    /// Apply ELU gradient to this expression.
266    #[inline(always)]
267    pub fn elu_grad(self) -> Expr<UnaryExpr<EluGrad, L>> {
268        Expr(UnaryExpr {
269            op: EluGrad,
270            child: self.0,
271        })
272    }
273
274    /// Apply softplus to this expression.
275    #[inline(always)]
276    pub fn softplus(self) -> Expr<UnaryExpr<Softplus, L>> {
277        Expr(UnaryExpr {
278            op: Softplus,
279            child: self.0,
280        })
281    }
282
283    /// Apply softplus gradient to this expression.
284    #[inline(always)]
285    pub fn softplus_grad(self) -> Expr<UnaryExpr<SoftplusGrad, L>> {
286        Expr(UnaryExpr {
287            op: SoftplusGrad,
288            child: self.0,
289        })
290    }
291
292    /// Apply tanh-approximation GELU to this expression.
293    #[inline(always)]
294    pub fn gelu_tanh(self) -> Expr<UnaryExpr<GeluTanh, L>> {
295        Expr(UnaryExpr {
296            op: GeluTanh,
297            child: self.0,
298        })
299    }
300
301    /// Apply tanh-approximation GELU gradient to this expression.
302    #[inline(always)]
303    pub fn gelu_tanh_grad(self) -> Expr<UnaryExpr<GeluTanhGrad, L>> {
304        Expr(UnaryExpr {
305            op: GeluTanhGrad,
306            child: self.0,
307        })
308    }
309
310    /// Apply reciprocal (1/x) to this expression.
311    #[inline(always)]
312    pub fn recip(self) -> Expr<UnaryExpr<Recip, L>> {
313        Expr(UnaryExpr {
314            op: Recip,
315            child: self.0,
316        })
317    }
318
319    /// Apply signum to this expression.
320    #[inline(always)]
321    pub fn sign(self) -> Expr<UnaryExpr<Sign, L>> {
322        Expr(UnaryExpr {
323            op: Sign,
324            child: self.0,
325        })
326    }
327
328    /// Apply floor to this expression.
329    #[inline(always)]
330    pub fn floor(self) -> Expr<UnaryExpr<Floor, L>> {
331        Expr(UnaryExpr {
332            op: Floor,
333            child: self.0,
334        })
335    }
336
337    /// Apply ceil to this expression.
338    #[inline(always)]
339    pub fn ceil(self) -> Expr<UnaryExpr<Ceil, L>> {
340        Expr(UnaryExpr {
341            op: Ceil,
342            child: self.0,
343        })
344    }
345
346    /// Apply round to this expression.
347    #[inline(always)]
348    pub fn round(self) -> Expr<UnaryExpr<Round, L>> {
349        Expr(UnaryExpr {
350            op: Round,
351            child: self.0,
352        })
353    }
354
355    /// Apply truncation toward zero to this expression.
356    #[inline(always)]
357    pub fn trunc(self) -> Expr<UnaryExpr<Trunc, L>> {
358        Expr(UnaryExpr {
359            op: Trunc,
360            child: self.0,
361        })
362    }
363}