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
9impl<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
118impl<L> Expr<L> {
121 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[inline(always)]
357 pub fn trunc(self) -> Expr<UnaryExpr<Trunc, L>> {
358 Expr(UnaryExpr {
359 op: Trunc,
360 child: self.0,
361 })
362 }
363}