1use crate::matrix::{CompactRowsResult, MoeExpertPlan, PadMode, TopKResult};
5use crate::{Matrix, Result};
6
7impl Matrix {
8 pub fn add(&self, right: &Matrix) -> Result<Self> {
12 crate::matrix::add(self, right)
13 }
14
15 pub fn bias_add(&self, right: &Matrix) -> Result<Self> {
19 crate::matrix::bias_add(self, right)
20 }
21
22 pub fn sub(&self, right: &Matrix) -> Result<Self> {
26 crate::matrix::sub(self, right)
27 }
28
29 pub fn mul(&self, right: &Matrix) -> Result<Self> {
33 crate::matrix::mul(self, right)
34 }
35
36 pub fn div(&self, right: &Matrix) -> Result<Self> {
40 crate::matrix::div(self, right)
41 }
42
43 pub fn scale(&self, scalar: f32) -> Result<Self> {
47 crate::matrix::scale(self, scalar)
48 }
49
50 pub fn neg(&self) -> Result<Self> {
54 crate::matrix::neg(self)
55 }
56
57 pub fn abs(&self) -> Result<Self> {
61 crate::matrix::abs(self)
62 }
63
64 pub fn log(&self) -> Result<Self> {
68 crate::matrix::log(self)
69 }
70
71 pub fn sqrt(&self) -> Result<Self> {
75 crate::matrix::sqrt(self)
76 }
77
78 pub fn pow(&self, exponent: f32) -> Result<Self> {
82 crate::matrix::pow(self, exponent)
83 }
84
85 pub fn add_scalar(&self, scalar: f32) -> Result<Self> {
89 crate::matrix::add_scalar(self, scalar)
90 }
91
92 pub fn sub_scalar(&self, scalar: f32) -> Result<Self> {
96 crate::matrix::sub_scalar(self, scalar)
97 }
98
99 pub fn div_scalar(&self, scalar: f32) -> Result<Self> {
103 crate::matrix::div_scalar(self, scalar)
104 }
105
106 pub fn exp(&self) -> Result<Self> {
110 crate::matrix::exp(self)
111 }
112
113 pub fn sin(&self) -> Result<Self> {
117 crate::matrix::sin(self)
118 }
119
120 pub fn cos(&self) -> Result<Self> {
124 crate::matrix::cos(self)
125 }
126
127 pub fn reciprocal(&self) -> Result<Self> {
131 crate::matrix::reciprocal(self)
132 }
133
134 pub fn clamp_max(&self, max: f32) -> Result<Self> {
138 crate::matrix::clamp_max(self, max)
139 }
140
141 pub fn clamp_min(&self, min: f32) -> Result<Self> {
145 crate::matrix::clamp_min(self, min)
146 }
147
148 pub fn copy(&self) -> Result<Self> {
152 crate::matrix::copy(self)
153 }
154
155 pub fn matmul(&self, right: &Matrix) -> Result<Self> {
159 crate::matrix::matmul(self, right)
160 }
161
162 pub fn softmax(&self, dim: i32) -> Result<Self> {
164 crate::matrix::softmax(self, dim)
165 }
166
167 pub fn log_softmax(&self, dim: i32) -> Result<Self> {
169 crate::matrix::log_softmax(self, dim)
170 }
171
172 pub fn sum(&self, dim: i32) -> Result<Self> {
174 crate::matrix::sum(self, dim)
175 }
176
177 pub fn mean(&self, dim: i32) -> Result<Self> {
179 crate::matrix::mean(self, dim)
180 }
181
182 pub fn max(&self, dim: i32) -> Result<Self> {
184 crate::matrix::max(self, dim)
185 }
186
187 pub fn argmax(&self, dim: i32) -> Result<i64> {
189 crate::matrix::argmax(self, dim)
190 }
191
192 pub fn categorical_accuracy_count(&self, labels: &Matrix) -> Result<Self> {
194 crate::matrix::categorical_accuracy_count(self, labels)
195 }
196
197 pub fn masked_categorical_accuracy_count(&self, labels: &Matrix, mask: &Matrix) -> Result<Self> {
199 crate::matrix::masked_categorical_accuracy_count(self, labels, mask)
200 }
201
202 pub fn philox_uniform(&self, low: f32, high: f32, seed: u64) -> Result<Self> {
204 crate::matrix::philox_uniform(self, low, high, seed)
205 }
206
207 pub fn philox_normal(&self, mean: f32, stddev: f32, seed: u64) -> Result<Self> {
209 crate::matrix::philox_normal(self, mean, stddev, seed)
210 }
211
212 pub fn dropout(&self, probability: f32, seed: u64) -> Result<Self> {
214 crate::matrix::dropout(self, probability, seed)
215 }
216
217 pub fn sample_logits(&self, temperature: f32, top_k: i32, top_p: f32, seed: u64) -> Result<Self> {
219 crate::matrix::sample_logits(self, temperature, top_k, top_p, seed)
220 }
221
222 pub fn top_k(&self, k: i32, dim: i32) -> Result<TopKResult> {
224 crate::matrix::top_k(self, k, dim)
225 }
226
227 pub fn top_k_mask(&self, num_experts: usize) -> Result<Self> {
229 crate::matrix::top_k_mask(self, num_experts)
230 }
231
232 pub fn moe_expert_plan(&self, num_experts: usize) -> Result<MoeExpertPlan> {
234 crate::matrix::moe_expert_plan(self, num_experts)
235 }
236
237 pub fn moe_routing_bias_update(
239 &self,
240 bias: &Matrix,
241 experts_per_token: usize,
242 gamma: f32,
243 ) -> Result<()> {
244 crate::matrix::moe_routing_bias_update(self, bias, experts_per_token, gamma)
245 }
246
247 pub fn slice(&self, dim: i32, start: i64, end: i64) -> Result<Self> {
249 crate::matrix::slice(self, dim, start, end)
250 }
251
252 pub fn pad(&self, pads: &[usize], mode: PadMode, fill: f32) -> Result<Self> {
254 crate::matrix::pad(self, pads, mode, fill)
255 }
256
257 pub fn repeat_interleave(&self, repeats: usize, dim: i32) -> Result<Self> {
259 crate::matrix::repeat_interleave(self, repeats, dim)
260 }
261
262 pub fn gather(&self, indices: &Matrix) -> Result<Self> {
264 crate::matrix::gather(self, indices)
265 }
266
267 pub fn gather_last_dim(&self, indices: &Matrix) -> Result<Self> {
269 crate::matrix::gather_last_dim(self, indices)
270 }
271
272 pub fn transpose(&self, dim0: i32, dim1: i32) -> Result<Self> {
274 crate::matrix::transpose(self, dim0, dim1)
275 }
276
277 pub fn equal(&self, value: f32) -> Result<Self> {
279 crate::matrix::equal(self, value)
280 }
281
282 pub fn causal_mask(&self) -> Result<Self> {
284 crate::matrix::causal_mask(self)
285 }
286
287 pub fn greater_equal(&self, value: f32) -> Result<Self> {
289 crate::matrix::greater_equal(self, value)
290 }
291
292 pub fn split(&self, sizes: &[i64], dim: i32) -> Result<Vec<Matrix>> {
294 crate::matrix::split(self, sizes, dim)
295 }
296
297 pub fn compact_rows(&self, mask: &Matrix) -> Result<CompactRowsResult> {
299 crate::matrix::compact_rows(self, mask)
300 }
301
302 pub fn scatter_rows(&self, source: &Matrix, row_map: &Matrix, count: &Matrix) -> Result<Self> {
304 crate::matrix::scatter_rows(self, source, row_map, count)
305 }
306
307 pub fn scatter_rows_indirect(&self, source: &Matrix, plan: &CompactRowsResult) -> Result<Self> {
309 crate::matrix::scatter_rows_indirect(self, source, plan)
310 }
311
312 pub fn cast_f32_i32(&self) -> Result<Self> {
314 crate::matrix::cast_f32_i32(self)
315 }
316
317 pub fn cast_i32_f32(&self) -> Result<Self> {
319 crate::matrix::cast_i32_f32(self)
320 }
321
322 pub fn reshape(&self, shape: impl Into<Vec<usize>>) -> Result<Self> {
324 crate::matrix::reshape(self, shape)
325 }
326}
327
328#[allow(clippy::type_complexity)]
329const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::add;
330#[allow(clippy::type_complexity)]
331const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::add;
332#[allow(clippy::type_complexity)]
333const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::bias_add;
334#[allow(clippy::type_complexity)]
335const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::bias_add;
336#[allow(clippy::type_complexity)]
337const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::sub;
338#[allow(clippy::type_complexity)]
339const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::sub;
340#[allow(clippy::type_complexity)]
341const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::mul;
342#[allow(clippy::type_complexity)]
343const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::mul;
344#[allow(clippy::type_complexity)]
345const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::div;
346#[allow(clippy::type_complexity)]
347const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::div;
348#[allow(clippy::type_complexity)]
349const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::scale;
350#[allow(clippy::type_complexity)]
351const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::scale;
352#[allow(clippy::type_complexity)]
353const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::neg;
354#[allow(clippy::type_complexity)]
355const _: fn(&Matrix) -> Result<Matrix> = Matrix::neg;
356#[allow(clippy::type_complexity)]
357const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::abs;
358#[allow(clippy::type_complexity)]
359const _: fn(&Matrix) -> Result<Matrix> = Matrix::abs;
360#[allow(clippy::type_complexity)]
361const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::log;
362#[allow(clippy::type_complexity)]
363const _: fn(&Matrix) -> Result<Matrix> = Matrix::log;
364#[allow(clippy::type_complexity)]
365const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::sqrt;
366#[allow(clippy::type_complexity)]
367const _: fn(&Matrix) -> Result<Matrix> = Matrix::sqrt;
368#[allow(clippy::type_complexity)]
369const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::pow;
370#[allow(clippy::type_complexity)]
371const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::pow;
372#[allow(clippy::type_complexity)]
373const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::add_scalar;
374#[allow(clippy::type_complexity)]
375const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::add_scalar;
376#[allow(clippy::type_complexity)]
377const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::sub_scalar;
378#[allow(clippy::type_complexity)]
379const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::sub_scalar;
380#[allow(clippy::type_complexity)]
381const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::div_scalar;
382#[allow(clippy::type_complexity)]
383const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::div_scalar;
384#[allow(clippy::type_complexity)]
385const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::exp;
386#[allow(clippy::type_complexity)]
387const _: fn(&Matrix) -> Result<Matrix> = Matrix::exp;
388#[allow(clippy::type_complexity)]
389const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::sin;
390#[allow(clippy::type_complexity)]
391const _: fn(&Matrix) -> Result<Matrix> = Matrix::sin;
392#[allow(clippy::type_complexity)]
393const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::cos;
394#[allow(clippy::type_complexity)]
395const _: fn(&Matrix) -> Result<Matrix> = Matrix::cos;
396#[allow(clippy::type_complexity)]
397const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::reciprocal;
398#[allow(clippy::type_complexity)]
399const _: fn(&Matrix) -> Result<Matrix> = Matrix::reciprocal;
400#[allow(clippy::type_complexity)]
401const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::clamp_max;
402#[allow(clippy::type_complexity)]
403const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::clamp_max;
404#[allow(clippy::type_complexity)]
405const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::clamp_min;
406#[allow(clippy::type_complexity)]
407const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::clamp_min;
408#[allow(clippy::type_complexity)]
409const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::copy;
410#[allow(clippy::type_complexity)]
411const _: fn(&Matrix) -> Result<Matrix> = Matrix::copy;
412#[allow(clippy::type_complexity)]
413const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::matmul;
414#[allow(clippy::type_complexity)]
415const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::matmul;
416#[allow(clippy::type_complexity)]
417const _: fn(&Matrix, i32) -> Result<Matrix> = crate::matrix::softmax;
418#[allow(clippy::type_complexity)]
419const _: fn(&Matrix, i32) -> Result<Matrix> = Matrix::softmax;
420#[allow(clippy::type_complexity)]
421const _: fn(&Matrix, i32) -> Result<Matrix> = crate::matrix::log_softmax;
422#[allow(clippy::type_complexity)]
423const _: fn(&Matrix, i32) -> Result<Matrix> = Matrix::log_softmax;
424#[allow(clippy::type_complexity)]
425const _: fn(&Matrix, i32) -> Result<Matrix> = crate::matrix::sum;
426#[allow(clippy::type_complexity)]
427const _: fn(&Matrix, i32) -> Result<Matrix> = Matrix::sum;
428#[allow(clippy::type_complexity)]
429const _: fn(&Matrix, i32) -> Result<Matrix> = crate::matrix::mean;
430#[allow(clippy::type_complexity)]
431const _: fn(&Matrix, i32) -> Result<Matrix> = Matrix::mean;
432#[allow(clippy::type_complexity)]
433const _: fn(&Matrix, i32) -> Result<Matrix> = crate::matrix::max;
434#[allow(clippy::type_complexity)]
435const _: fn(&Matrix, i32) -> Result<Matrix> = Matrix::max;
436#[allow(clippy::type_complexity)]
437const _: fn(&Matrix, i32) -> Result<i64> = crate::matrix::argmax;
438#[allow(clippy::type_complexity)]
439const _: fn(&Matrix, i32) -> Result<i64> = Matrix::argmax;
440#[allow(clippy::type_complexity)]
441const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::categorical_accuracy_count;
442#[allow(clippy::type_complexity)]
443const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::categorical_accuracy_count;
444#[allow(clippy::type_complexity)]
445const _: fn(&Matrix, &Matrix, &Matrix) -> Result<Matrix> =
446 crate::matrix::masked_categorical_accuracy_count;
447#[allow(clippy::type_complexity)]
448const _: fn(&Matrix, &Matrix, &Matrix) -> Result<Matrix> =
449 Matrix::masked_categorical_accuracy_count;
450#[allow(clippy::type_complexity)]
451const _: fn(&Matrix, f32, f32, u64) -> Result<Matrix> = crate::matrix::philox_uniform;
452#[allow(clippy::type_complexity)]
453const _: fn(&Matrix, f32, f32, u64) -> Result<Matrix> = Matrix::philox_uniform;
454#[allow(clippy::type_complexity)]
455const _: fn(&Matrix, f32, f32, u64) -> Result<Matrix> = crate::matrix::philox_normal;
456#[allow(clippy::type_complexity)]
457const _: fn(&Matrix, f32, f32, u64) -> Result<Matrix> = Matrix::philox_normal;
458#[allow(clippy::type_complexity)]
459const _: fn(&Matrix, f32, u64) -> Result<Matrix> = crate::matrix::dropout;
460#[allow(clippy::type_complexity)]
461const _: fn(&Matrix, f32, u64) -> Result<Matrix> = Matrix::dropout;
462#[allow(clippy::type_complexity)]
463const _: fn(&Matrix, f32, i32, f32, u64) -> Result<Matrix> = crate::matrix::sample_logits;
464#[allow(clippy::type_complexity)]
465const _: fn(&Matrix, f32, i32, f32, u64) -> Result<Matrix> = Matrix::sample_logits;
466#[allow(clippy::type_complexity)]
467const _: fn(&Matrix, i32, i32) -> Result<TopKResult> = crate::matrix::top_k;
468#[allow(clippy::type_complexity)]
469const _: fn(&Matrix, i32, i32) -> Result<TopKResult> = Matrix::top_k;
470#[allow(clippy::type_complexity)]
471const _: fn(&Matrix, usize) -> Result<Matrix> = crate::matrix::top_k_mask;
472#[allow(clippy::type_complexity)]
473const _: fn(&Matrix, usize) -> Result<Matrix> = Matrix::top_k_mask;
474#[allow(clippy::type_complexity)]
475const _: fn(&Matrix, usize) -> Result<MoeExpertPlan> = crate::matrix::moe_expert_plan;
476#[allow(clippy::type_complexity)]
477const _: fn(&Matrix, usize) -> Result<MoeExpertPlan> = Matrix::moe_expert_plan;
478#[allow(clippy::type_complexity)]
479const _: fn(&Matrix, &Matrix, usize, f32) -> Result<()> = crate::matrix::moe_routing_bias_update;
480#[allow(clippy::type_complexity)]
481const _: fn(&Matrix, &Matrix, usize, f32) -> Result<()> = Matrix::moe_routing_bias_update;
482#[allow(clippy::type_complexity)]
483const _: fn(&Matrix, i32, i64, i64) -> Result<Matrix> = crate::matrix::slice;
484#[allow(clippy::type_complexity)]
485const _: fn(&Matrix, i32, i64, i64) -> Result<Matrix> = Matrix::slice;
486#[allow(clippy::type_complexity)]
487const _: fn(&Matrix, &[usize], PadMode, f32) -> Result<Matrix> = crate::matrix::pad;
488#[allow(clippy::type_complexity)]
489const _: fn(&Matrix, &[usize], PadMode, f32) -> Result<Matrix> = Matrix::pad;
490#[allow(clippy::type_complexity)]
491const _: fn(&Matrix, usize, i32) -> Result<Matrix> = crate::matrix::repeat_interleave;
492#[allow(clippy::type_complexity)]
493const _: fn(&Matrix, usize, i32) -> Result<Matrix> = Matrix::repeat_interleave;
494#[allow(clippy::type_complexity)]
495const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::gather;
496#[allow(clippy::type_complexity)]
497const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::gather;
498#[allow(clippy::type_complexity)]
499const _: fn(&Matrix, &Matrix) -> Result<Matrix> = crate::matrix::gather_last_dim;
500#[allow(clippy::type_complexity)]
501const _: fn(&Matrix, &Matrix) -> Result<Matrix> = Matrix::gather_last_dim;
502#[allow(clippy::type_complexity)]
503const _: fn(&Matrix, i32, i32) -> Result<Matrix> = crate::matrix::transpose;
504#[allow(clippy::type_complexity)]
505const _: fn(&Matrix, i32, i32) -> Result<Matrix> = Matrix::transpose;
506#[allow(clippy::type_complexity)]
507const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::equal;
508#[allow(clippy::type_complexity)]
509const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::equal;
510#[allow(clippy::type_complexity)]
511const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::causal_mask;
512#[allow(clippy::type_complexity)]
513const _: fn(&Matrix) -> Result<Matrix> = Matrix::causal_mask;
514#[allow(clippy::type_complexity)]
515const _: fn(&Matrix, f32) -> Result<Matrix> = crate::matrix::greater_equal;
516#[allow(clippy::type_complexity)]
517const _: fn(&Matrix, f32) -> Result<Matrix> = Matrix::greater_equal;
518#[allow(clippy::type_complexity)]
519const _: fn(&Matrix, &[i64], i32) -> Result<Vec<Matrix>> = crate::matrix::split;
520#[allow(clippy::type_complexity)]
521const _: fn(&Matrix, &[i64], i32) -> Result<Vec<Matrix>> = Matrix::split;
522#[allow(clippy::type_complexity)]
523const _: fn(&Matrix, &Matrix) -> Result<CompactRowsResult> = crate::matrix::compact_rows;
524#[allow(clippy::type_complexity)]
525const _: fn(&Matrix, &Matrix) -> Result<CompactRowsResult> = Matrix::compact_rows;
526#[allow(clippy::type_complexity)]
527const _: fn(&Matrix, &Matrix, &Matrix, &Matrix) -> Result<Matrix> = crate::matrix::scatter_rows;
528#[allow(clippy::type_complexity)]
529const _: fn(&Matrix, &Matrix, &Matrix, &Matrix) -> Result<Matrix> = Matrix::scatter_rows;
530#[allow(clippy::type_complexity)]
531const _: fn(&Matrix, &Matrix, &CompactRowsResult) -> Result<Matrix> =
532 crate::matrix::scatter_rows_indirect;
533#[allow(clippy::type_complexity)]
534const _: fn(&Matrix, &Matrix, &CompactRowsResult) -> Result<Matrix> = Matrix::scatter_rows_indirect;
535#[allow(clippy::type_complexity)]
536const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::cast_f32_i32;
537#[allow(clippy::type_complexity)]
538const _: fn(&Matrix) -> Result<Matrix> = Matrix::cast_f32_i32;
539#[allow(clippy::type_complexity)]
540const _: fn(&Matrix) -> Result<Matrix> = crate::matrix::cast_i32_f32;
541#[allow(clippy::type_complexity)]
542const _: fn(&Matrix) -> Result<Matrix> = Matrix::cast_i32_f32;