1use crate::internal::*;
2use crate::ops::cast::{Cast, cast};
3use crate::ops::change_axes::wire_with_rank_broadcast;
4use crate::ops::element_wise::ElementWiseOp;
5use crate::ops::nn::LeakyRelu;
6use ndarray::*;
7use tract_itertools::Itertools;
8
9use tract_linalg::mmm::{
10 AsInputValue, EagerPackedInput, FusedSpec, MMMInputValue, MatMatMul, OutputStore,
11 OutputStoreSpec, PackedMatrixStorage, PanelExtractInput, PanelExtractor,
12};
13use tract_linalg::pack::PackedFormat;
14use tract_linalg::{BinOp, Scaler};
15use tract_smallvec::ToSmallVec;
16
17use super::ModePicker;
18
19fn pure_squeeze_removed(old: &[usize], new: &[usize]) -> Option<TVec<usize>> {
23 let mut removed: TVec<usize> = tvec!();
24 let mut j = 0;
25 for (i, &d) in old.iter().enumerate() {
26 if j < new.len() && d == new[j] {
27 j += 1;
28 } else if d == 1 {
29 removed.push(i);
30 } else {
31 return None;
32 }
33 }
34 (j == new.len() && !removed.is_empty()).then_some(removed)
35}
36
37#[derive(Clone, Debug, PartialEq, Eq)]
41pub enum MatMulOperand {
42 Input(usize),
43 Const(Box<dyn MMMInputValue>),
44}
45
46impl MatMulOperand {
47 #[inline]
49 unsafe fn trivial_value<'t>(&'t self, inputs: &'t [TValue]) -> &'t dyn MMMInputValue {
50 match self {
51 MatMulOperand::Input(i) => unsafe {
52 inputs
53 .get_unchecked(*i)
54 .try_storage_as::<PackedMatrixStorage>()
55 .unwrap_unchecked()
56 .value()
57 },
58 MatMulOperand::Const(v) => &**v,
59 }
60 }
61}
62
63#[derive(Clone, Debug, PartialEq, Eq)]
64pub enum ProtoFusedSpec {
65 AddMatMul {
66 geo: AddMatMulGeometry,
67 a: MatMulOperand,
68 b: MatMulOperand,
69 packings: Vec<(usize, Option<PanelExtractor>)>,
70 },
71 BinScalar(usize, BinOp),
72 LeakyRelu(usize),
73 BinPerRow(usize, BinOp, MapOutputAxisToInput),
74 BinPerCol(usize, BinOp, MapOutputAxisToInput),
75 AddRowColProducts(usize, usize),
76 AddUnicast(OutputStoreSpec, usize, MapOutputAxisToInput),
77 Scaler(Scaler),
78 Store(Vec<OutputStoreSpec>),
79}
80
81impl ProtoFusedSpec {
82 pub fn format(&self, mmm: &dyn MatMatMul, mode: usize) -> String {
83 use ProtoFusedSpec::*;
84 match self {
85 AddMatMul { geo, packings: packing, .. } => {
86 let (a, b) = &mmm.packings()[packing[mode].0];
87 format!("matmul(k={}, {a:?}•{b:?})", geo.k)
88 }
89 BinScalar(_, op) => format!("scalar{op:?}"),
90 LeakyRelu(alpha) => format!("leaky_relu({alpha:?})"),
91 BinPerRow(_, op, _) => format!("row{op:?}"),
92 BinPerCol(_, op, _) => format!("col{op:?}"),
93 AddRowColProducts(_, _) => "add_row_col_product".to_string(),
94 AddUnicast(_, _, _) => "add_to_matrix".to_string(),
95 Scaler(s) => format!("scale({})", 1f32 * *s),
96 Store(_oss) => "store".to_string(),
97 }
98 }
99
100 pub fn resolve<'t>(
101 &'t self,
102 inputs: &'t [TValue],
103 output_coords: &[usize],
104 output: &Tensor,
105 mmm: &dyn MatMatMul,
106 mode: usize,
107 ) -> FusedSpec<'t> {
108 #[allow(clippy::let_and_return)]
109 let fs = match self {
110 ProtoFusedSpec::AddMatMul { geo, a, b, packings } => {
111 let resolve =
112 |operand: &'t MatMulOperand, mapping: &MapOutputAxisToInput| match operand {
113 MatMulOperand::Input(i) => {
114 let storage =
115 inputs[*i].try_storage_as::<PackedMatrixStorage>().unwrap();
116 let idx = mapping.flat_index(output_coords, storage.batch_strides());
117 storage.value_at_flat(idx)
118 }
119 MatMulOperand::Const(v) => &**v,
120 };
121 let a = resolve(a, &geo.c_to_a_axis_mapping);
122 let b = resolve(b, &geo.c_to_b_axis_mapping);
123
124 let (_a_packing, b_packing) = &mmm.packings()[packings[mode].0];
125 let pa = if let Some(extractor) = &packings[mode].1 {
126 let data = a.downcast_ref::<EagerPackedInput>().unwrap();
127 AsInputValue::Owned(Box::new(PanelExtractInput {
128 format: extractor.clone(),
129 data: data.clone(),
130 }))
131 } else {
132 AsInputValue::Borrowed(a)
133 };
134 assert!(
135 b_packing.dyn_eq(b.format())
136 || (b_packing.is::<PackedFormat>() && b_packing.r() == b.format().r())
137 );
138 debug_assert!(pa.k().to_dim().compatible_with(&geo.k.to_dim()));
139 debug_assert!(b.k().to_dim().compatible_with(&geo.k.to_dim()));
140 FusedSpec::AddMatMul {
141 a: pa,
142 b: AsInputValue::Borrowed(b),
143 packing: packings[mode].0,
144 }
145 }
146 ProtoFusedSpec::BinScalar(v, op) => FusedSpec::BinScalar(&inputs[*v], *op),
147 ProtoFusedSpec::LeakyRelu(v) => FusedSpec::LeakyRelu(&inputs[*v]),
148 ProtoFusedSpec::BinPerRow(v, op, map) => {
149 let mut v = inputs[*v].view();
150 unsafe { map.translate_view(output_coords, &mut v) }
151 FusedSpec::BinPerRow(v, *op)
152 }
153 ProtoFusedSpec::BinPerCol(v, op, map) => {
154 let mut v = inputs[*v].view();
155 unsafe { map.translate_view(output_coords, &mut v) }
156 FusedSpec::BinPerCol(v, *op)
157 }
158 ProtoFusedSpec::AddRowColProducts(row, col) => {
159 FusedSpec::AddRowColProducts(&inputs[*row], &inputs[*col])
160 }
161 ProtoFusedSpec::AddUnicast(store, v, map) => unsafe {
162 let mut view = inputs[*v].view();
163 map.translate_view(output_coords, &mut view);
164 FusedSpec::AddUnicast(store.wrap(&view))
165 },
166 ProtoFusedSpec::Scaler(scaler) => scaler.as_fused_spec(),
167 ProtoFusedSpec::Store(oss) => unsafe {
168 let view = output.view_offsetting_unchecked(output_coords);
169 FusedSpec::Store(oss[mode].wrap(&view))
170 },
171 };
172 fs
173 }
174
175 pub fn is_trivial(&self) -> bool {
176 match self {
177 ProtoFusedSpec::AddMatMul { geo, .. } => geo.k.as_i64().is_some(),
178 _ => true,
179 }
180 }
181
182 pub fn resolve_trivial<'t>(
183 &'t self,
184 inputs: &'t [TValue],
185 output: &mut Tensor,
186 _mmm: &dyn MatMatMul,
187 mode: usize,
188 ) -> FusedSpec<'t> {
189 #[allow(clippy::let_and_return)]
190 let fs = match self {
191 ProtoFusedSpec::AddMatMul { a, b, packings, .. } => unsafe {
192 let a = a.trivial_value(inputs);
193 let b = b.trivial_value(inputs);
194 debug_assert!(packings.len() == 1);
195 debug_assert!(packings[0].1.is_none()); #[cfg(debug_assertions)]
197 {
198 let (a_packing, b_packing) = &_mmm.packings()[packings[mode].0];
199 debug_assert!(
200 a_packing.dyn_eq(a.format())
201 || (a_packing.is::<PackedFormat>() && a_packing.r() == a.format().r())
202 );
203 debug_assert!(
204 b_packing.dyn_eq(b.format())
205 || (b_packing.is::<PackedFormat>() && b_packing.r() == b.format().r())
206 );
207 }
208 FusedSpec::AddMatMul {
209 a: AsInputValue::Borrowed(a),
210 b: AsInputValue::Borrowed(b),
211 packing: packings[mode].0,
212 }
213 },
214 ProtoFusedSpec::BinScalar(v, op) => FusedSpec::BinScalar(&inputs[*v], *op),
215 ProtoFusedSpec::LeakyRelu(v) => FusedSpec::LeakyRelu(&inputs[*v]),
216 ProtoFusedSpec::BinPerRow(v, op, _) => {
217 let v = inputs[*v].view();
218 FusedSpec::BinPerRow(v, *op)
219 }
220 ProtoFusedSpec::BinPerCol(v, op, _) => {
221 let v = inputs[*v].view();
222 FusedSpec::BinPerCol(v, *op)
223 }
224 ProtoFusedSpec::AddRowColProducts(row, col) => {
225 FusedSpec::AddRowColProducts(&inputs[*row], &inputs[*col])
226 }
227 ProtoFusedSpec::AddUnicast(store, v, _) => unsafe {
228 let view = inputs[*v].view();
229 FusedSpec::AddUnicast(store.wrap(&view))
230 },
231 ProtoFusedSpec::Scaler(scaler) => scaler.as_fused_spec(),
232 ProtoFusedSpec::Store(oss) => unsafe {
233 FusedSpec::Store(oss[mode].wrap(&output.view_mut()))
234 },
235 };
236 fs
237 }
238
239 fn resolve_trivial_cached<'t>(
244 &'t self,
245 inputs: &'t [TValue],
246 output: &mut Tensor,
247 mmm: &dyn MatMatMul,
248 mode: usize,
249 store: Option<OutputStore>,
250 ) -> FusedSpec<'t> {
251 match self {
252 ProtoFusedSpec::Store(oss) => unsafe {
253 FusedSpec::Store(match store {
254 Some(cached) => cached.with_tensor(&output.view()),
255 None => oss[mode].wrap(&output.view_mut()),
256 })
257 },
258 _ => self.resolve_trivial(inputs, output, mmm, mode),
259 }
260 }
261
262 fn check_inputs(&self, inputs: &[&TypedFact]) -> TractResult<()> {
263 use ProtoFusedSpec::*;
264 match self {
265 AddMatMul { a, b, .. } => {
266 for operand in [a, b] {
267 if let MatMulOperand::Input(ix) = operand {
268 ensure!(inputs[*ix].is_exotic());
269 }
270 }
271 }
272 BinScalar(v, _)
273 | LeakyRelu(v)
274 | BinPerCol(v, _, _)
275 | BinPerRow(v, _, _)
276 | AddUnicast(_, v, _) => {
277 ensure!(inputs[*v].datum_type.is_number());
278 }
279 AddRowColProducts(row, col) => {
280 ensure!(inputs[*row].datum_type.is_number());
281 ensure!(inputs[*col].datum_type.is_number());
282 }
283 _ => (),
284 };
285 Ok(())
286 }
287
288 fn cost(&self, m: &TDim, n: &TDim, idt: DatumType) -> TVec<(Cost, TDim)> {
289 match self {
290 ProtoFusedSpec::AddMatMul { geo, .. } => {
291 tvec!((Cost::FMA(idt), m.clone() * n * &geo.k))
292 }
293 _ => tvec!(), }
295 }
296
297 fn push_mapped_c_axes(&self, out: &mut TVec<usize>) {
302 use ProtoFusedSpec::*;
303 match self {
304 AddMatMul { geo, .. } => {
305 out.extend(geo.c_to_a_axis_mapping.0.iter().map(|(c, _)| *c));
306 out.extend(geo.c_to_b_axis_mapping.0.iter().map(|(c, _)| *c));
307 }
308 BinPerRow(_, _, map) | BinPerCol(_, _, map) | AddUnicast(_, _, map) => {
309 out.extend(map.0.iter().map(|(c, _)| *c));
310 }
311 BinScalar(..) | Scaler(..) | AddRowColProducts(_, _) | LeakyRelu(_) | Store(..) => {}
312 }
313 }
314
315 fn rm_c_axis(&mut self, axis: usize) {
316 use ProtoFusedSpec::*;
317 match self {
318 AddMatMul { geo, .. } => {
319 geo.c_to_a_axis_mapping.rm_c_axis(axis);
320 geo.c_to_b_axis_mapping.rm_c_axis(axis);
321 }
322 BinScalar(..) | Scaler(..) | AddRowColProducts(_, _) | LeakyRelu(_) => {}
323 BinPerRow(_, _, map) | BinPerCol(_, _, map) => map.rm_c_axis(axis),
324 AddUnicast(_, _, map) => {
325 map.rm_c_axis(axis);
326 }
327 Store(oss, ..) => {
328 for oss in oss {
329 match oss {
330 OutputStoreSpec::View { m_axis, n_axis, .. } => {
331 if let Some(m) = m_axis {
332 *m -= (*m > axis) as usize
333 };
334 if let Some(n) = n_axis {
335 *n -= (*n > axis) as usize
336 }
337 }
338 OutputStoreSpec::Strides { .. } => {}
339 }
340 }
341 }
342 }
343 }
344}
345
346#[derive(Clone, Debug, PartialEq, Eq)]
347pub struct MapOutputAxisToInput(pub TVec<(usize, usize)>);
348
349impl MapOutputAxisToInput {
350 #[inline]
351 unsafe fn translate_view(&self, output_coords: &[usize], v: &mut TensorView) {
352 for &(out_axis, in_axis) in &self.0 {
353 unsafe { v.offset_axis(in_axis, output_coords[out_axis] as isize) }
354 }
355 }
356
357 #[inline]
358 fn rm_c_axis(&mut self, axis: usize) {
359 for (c, _) in &mut self.0 {
360 *c -= (*c > axis) as usize;
361 }
362 }
363
364 #[inline]
366 pub fn flat_index(&self, output_coords: &[usize], batch_strides: &[isize]) -> usize {
367 self.0
368 .iter()
369 .map(|&(out_axis, in_axis)| output_coords[out_axis] * batch_strides[in_axis] as usize)
370 .sum()
371 }
372}
373
374#[derive(Clone, Debug, PartialEq, Eq)]
375pub struct AddMatMulGeometry {
376 pub k: TDim,
377 pub c_to_a_axis_mapping: MapOutputAxisToInput,
378 pub c_to_b_axis_mapping: MapOutputAxisToInput,
379}
380
381#[derive(Clone, Debug, PartialEq, Eq)]
382pub struct OptMatMul {
383 pub c_fact: TypedFact,
384 pub micro_ops: Vec<ProtoFusedSpec>,
385 pub mmm: Vec<Box<dyn MatMatMul>>,
386 pub mode_picker: ModePicker,
387 pub c_m_axis: Option<usize>,
388 pub c_n_axis: Option<usize>,
389 pub trivial_packing: bool,
390 pub trivial_path: bool,
391}
392
393impl Op for OptMatMul {
394 fn name(&self) -> StaticName {
395 "OptMatMul".into()
396 }
397
398 fn info(&self) -> TractResult<Vec<String>> {
399 let m = self.c_m_axis.map(|ix| &self.c_fact.shape[ix]).unwrap_or(&TDim::Val(1));
400 let n = self.c_n_axis.map(|ix| &self.c_fact.shape[ix]).unwrap_or(&TDim::Val(1));
401 let mut infos = vec![format!(
402 "c_shape:{:?}, c_m_axis:{:?} c_n_axis:{:?} m:{} n:{}",
403 self.c_fact, self.c_m_axis, self.c_n_axis, m, n,
404 )];
405 if let Some(k) = self.guess_k() {
406 infos.push(format!("Mult: m:{} k:{} n:{} with {:?}", m, k, n, self.mmm));
407 } else {
408 infos.push(format!("Mult: {:?}", self.mmm));
409 }
410 for (mode, mmm) in self.mmm.iter().enumerate() {
411 infos.push(format!(
412 "Ops: {}",
413 self.micro_ops.iter().map(|o| o.format(&**mmm, mode)).join(" >>> ")
414 ));
415 }
416 Ok(infos)
417 }
418
419 op_as_typed_op!();
420}
421
422#[derive(Default)]
434struct MmmScratch {
435 space: Option<Box<dyn tract_linalg::mmm::ScratchSpace>>,
436 stores: HashMap<usize, StoreMemo>,
437}
438
439struct StoreMemo {
443 c_shape: TVec<usize>,
444 mode: usize,
445 stores: TVec<Option<OutputStore>>,
446}
447
448thread_local! {
449 static MMM_SCRATCH: std::cell::RefCell<HashMap<SessionId, MmmScratch>> =
450 std::cell::RefCell::new(HashMap::new());
451}
452
453impl EvalOp for OptMatMul {
454 not_out_of_plan!();
455
456 fn eval(&self, ctx: &EvalContext, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
457 MMM_SCRATCH.with_borrow_mut(|per_session| {
458 self.eval_with_scratch(ctx, inputs, per_session.entry(ctx.session).or_default())
459 })
460 }
461
462 fn drop_session(&self, session: SessionId, _node_id: usize) {
463 MMM_SCRATCH.with_borrow_mut(|per_session| per_session.remove(&session));
464 }
465}
466
467impl OptMatMul {
468 fn eval_with_scratch(
469 &self,
470 ctx: &EvalContext,
471 inputs: TVec<TValue>,
472 scratch: &mut MmmScratch,
473 ) -> TractResult<TVec<TValue>> {
474 unsafe {
475 let c_shape = self.c_fact.shape.eval_to_usize(ctx.symbols)?;
476 let mut c = Tensor::uninitialized_dt(self.c_fact.datum_type, &c_shape)?;
477 let m = self.c_m_axis.map(|c_m| c.shape()[c_m]).unwrap_or(1);
478 let n = self.c_n_axis.map(|c_n| c.shape()[c_n]).unwrap_or(1);
479 let mode = self.mode_picker.pick(n)?;
480 let mmm = &*self.mmm[mode];
481 let MmmScratch { space, stores } = scratch;
482 if !space.as_ref().is_some_and(|s| mmm.can_use_scratch_space(&**s)) {
483 *space = Some(mmm.allocate_scratch_space());
484 }
485 let scratch = space.as_mut().unwrap();
486 if self.trivial_path {
487 let memo = || StoreMemo {
488 c_shape: c.shape().into(),
489 mode,
490 stores: self
491 .micro_ops
492 .iter()
493 .map(|o| match o {
494 ProtoFusedSpec::Store(oss) => Some(oss[mode].wrap(&c.view())),
495 _ => None,
496 })
497 .collect(),
498 };
499 let stores = stores.entry(ctx.node_id).or_insert_with(memo);
500 if stores.c_shape.as_slice() != c.shape() || stores.mode != mode {
501 *stores = memo();
502 }
503 let uops: TVec<FusedSpec> = self
504 .micro_ops
505 .iter()
506 .zip(stores.stores.iter())
507 .map(|(o, store)| o.resolve_trivial_cached(&inputs, &mut c, mmm, mode, *store))
508 .collect();
509 mmm.run_with_scratch_space(m, n, scratch.as_mut(), &uops)?;
510 Ok(tvec!(c.into_tvalue()))
511 } else {
512 let mut uops = vec![FusedSpec::ShiftLeft(0); self.micro_ops.len()];
513 let mut looping_shape: TVec<usize> = c_shape.to_smallvec();
514 if let Some(ax) = self.c_m_axis {
515 looping_shape[ax] = 1;
516 }
517 if let Some(ax) = self.c_n_axis {
518 looping_shape[ax] = 1;
519 }
520 for c_coords in indices(&*looping_shape) {
521 for ix in 0..self.micro_ops.len() {
522 *uops.get_unchecked_mut(ix) = self.micro_ops.get_unchecked(ix).resolve(
523 &inputs,
524 c_coords.slice(),
525 &c,
526 mmm,
527 mode,
528 );
529 }
530 mmm.run_with_scratch_space(m, n, scratch.as_mut(), &uops)
531 .context("In mmm.run_with_scratch_space")?;
532 }
533 Ok(tvec!(c.into_tvalue()))
534 }
535 }
536 }
537}
538
539impl TypedOp for OptMatMul {
540 fn output_facts(&self, inputs: &[&TypedFact]) -> TractResult<TVec<TypedFact>> {
541 ensure!(self.c_m_axis.map(|ax| ax < self.c_fact.rank()).unwrap_or(true));
542 ensure!(self.c_n_axis.map(|ax| ax < self.c_fact.rank()).unwrap_or(true));
543 ensure!(self.trivial_path == self.can_use_trivial_path());
544 ensure!(self.mmm.iter().map(|mmm| mmm.internal_type()).all_equal());
545 for op in &self.micro_ops {
546 op.check_inputs(inputs)?;
547 }
548 Ok(tvec!(self.c_fact.clone()))
549 }
550
551 fn cost(&self, _inputs: &[&TypedFact]) -> TractResult<TVec<(Cost, TDim)>> {
552 let mut sums = HashMap::new();
553 for op in &self.micro_ops {
554 for (cost, count) in op.cost(self.m(), self.n(), self.mmm[0].internal_type()) {
555 *sums.entry(cost).or_default() += count;
556 }
557 }
558 let loops = self
559 .c_fact
560 .shape
561 .iter()
562 .enumerate()
563 .map(|(ix, d)| {
564 if Some(ix) == self.c_m_axis || Some(ix) == self.c_n_axis {
565 1.to_dim()
566 } else {
567 d.clone()
568 }
569 })
570 .product::<TDim>();
571 for s in &mut sums.values_mut() {
572 *s *= &loops;
573 }
574 Ok(sums.into_iter().collect())
575 }
576
577 fn fuse(&self, model: &TypedModel, node: &TypedNode) -> TractResult<Option<TypedModelPatch>> {
578 use crate::ops;
579 if let Some(patch) = self.bake_const_operands(model, node)? {
580 return Ok(Some(patch));
581 }
582 rule_if!(node.outputs.len() == 1);
583 rule_if!(node.outputs[0].successors.len() == 1);
584 rule_if!(!model.output_outlets()?.contains(&node.id.into()));
585 let succ = model.node(node.outputs[0].successors[0].node);
586 let mut patch = TypedModelPatch::new(format!("fusing {succ}"));
587
588 if let Some(op) = succ.op_as::<ops::binary::TypedBinOp>() {
589 rule_if_some!(mut binop = op.0.as_linalg_binop());
590 let flipped = succ.inputs[0].node == node.id;
591 if flipped {
592 binop = binop.flip();
593 }
594 let other_outlet = succ.inputs[flipped as usize];
595 return self.fuse_binary(model, node, patch, other_outlet, binop);
596 }
597 if let Some(op) = succ.op_as::<ops::binary::OptBinByScalar>() {
598 rule_if_some!(mut binop = op.binop.as_linalg_binop());
599 let flipped = succ.inputs[0].node == node.id;
600 if flipped {
601 binop = binop.flip();
602 }
603 let other_outlet = succ.inputs[flipped as usize];
604 return self.fuse_binary(model, node, patch, other_outlet, binop);
605 }
606
607 if let Some(op) = succ.op_as::<ops::element_wise::ElementWiseOp>().map(|ew| ew.0.as_ref()) {
608 if let Some(op) = op.downcast_ref::<ops::math::QScale>() {
609 return self.fuse_op(
610 model,
611 node,
612 patch,
613 vec![ProtoFusedSpec::Scaler(op.scaler)],
614 &[],
615 );
616 }
617 if let Some(op) = op.downcast_ref::<LeakyRelu>() {
618 rule_if!(
619 self.mmm
620 .iter()
621 .all(|mmm| mmm.can_fuse(&FusedSpec::LeakyRelu(&tensor0(op.alpha))))
622 );
623 let alpha = patch.add_const(
624 node.name.to_string() + ".alpha",
625 tensor0(op.alpha).cast_to_dt(self.mmm[0].internal_type())?.into_owned(),
626 )?;
627 return self.fuse_op(
628 model,
629 node,
630 patch,
631 vec![ProtoFusedSpec::LeakyRelu(node.inputs.len())],
632 &[alpha],
633 );
634 }
635 }
636 if let Some(cast_to) = succ.op_as::<ops::cast::Cast>().map(|cast| cast.to)
637 && (((cast_to.unquantized() == i8::datum_type()
638 || cast_to.unquantized() == u8::datum_type())
639 && self.c_fact.datum_type == i32::datum_type())
640 || self.mmm.iter().all(|m| m.stores().contains(&cast_to)))
641 && let Some(ProtoFusedSpec::Store(stores)) = self.micro_ops.last()
642 {
643 rule_if!(stores.iter().all(|s| !matches!(s, OutputStoreSpec::Strides { .. })));
644 let c_fact = cast_to.fact(self.c_fact.shape.clone());
645 let mut patch =
646 TypedModelPatch::fuse_with_next(model, node, Self { c_fact, ..self.clone() })?;
647 patch.dont_apply_twice = Some(format!("Fuse {succ} into {node}"));
648 return Ok(Some(patch));
649 }
650 if let Some(AxisOp::Rm(axis)) = succ.op_as::<ops::AxisOp>() {
651 rule_if!(Some(*axis) != self.c_m_axis);
652 rule_if!(Some(*axis) != self.c_n_axis);
653 let mut new_op = self.clone();
654 new_op.c_fact.shape.remove_axis(*axis)?;
655 if let Some(c_m_axis) = &mut new_op.c_m_axis {
656 *c_m_axis -= (*c_m_axis > *axis) as usize;
657 }
658 if let Some(c_n_axis) = &mut new_op.c_n_axis {
659 *c_n_axis -= (*c_n_axis > *axis) as usize;
660 }
661 for uop in &mut new_op.micro_ops {
662 uop.rm_c_axis(*axis);
663 }
664 let mut patch = TypedModelPatch::fuse_with_next(model, node, new_op)?;
665 patch.dont_apply_twice = Some(format!("Fuse {succ} into {node}"));
666 return Ok(Some(patch));
667 }
668 if let Some(into) = succ.op_as::<IntoShape>()
669 && let Some(new_op) = self.absorb_squeeze(into)
670 {
671 let mut patch = TypedModelPatch::fuse_with_next(model, node, new_op)?;
672 patch.dont_apply_twice = Some(format!("Fuse {succ} into {node}"));
673 return Ok(Some(patch));
674 }
675 if (succ.op_is::<ElementWiseOp>() || succ.op_is::<Cast>())
680 && succ.outputs.len() == 1
681 && let &[next] = &*succ.outputs[0].successors
682 {
683 let into_node = model.node(next.node);
684 if let Some(into) = into_node.op_as::<IntoShape>()
685 && let Some(new_op) = self.absorb_squeeze(into)
686 {
687 let mut patch = TypedModelPatch::default();
688 let inputs = node
689 .inputs
690 .iter()
691 .map(|i| patch.tap_model(model, *i))
692 .collect::<TractResult<TVec<_>>>()?;
693 let mm = patch.wire_node(&node.name, new_op, &inputs)?[0];
694 let ew = patch.wire_node(&succ.name, succ.op.clone(), &[mm])?[0];
695 patch.shunt_outside(model, into_node.id.into(), ew)?;
696 patch.dont_apply_twice = Some(format!("Reach {into_node} into {node}"));
697 return Ok(Some(patch));
698 }
699 }
700 if (succ.op_is::<AxisOp>() || succ.op_is::<IntoShape>())
701 && let &[next] = &*succ.outputs[0].successors
702 {
703 let next_node = model.node(next.node);
704 if let Some(cast) = next_node.op_as::<Cast>() {
705 let mut patch = TypedModelPatch::default();
706 let mut wire = patch.tap_model(model, node.id.into())?;
707 wire = patch.wire_node(&next_node.name, cast.clone(), &[wire])?[0];
708 wire = patch.wire_node(&succ.name, succ.op.clone(), &[wire])?[0];
709 patch.shunt_outside(model, next_node.id.into(), wire)?;
710 return Ok(Some(patch));
711 } else if let Some(op) = next_node.op_as::<ops::binary::TypedBinOp>() {
712 rule_if!(op.0.as_linalg_binop().is_some());
713 let flipped = succ.inputs[0].node == node.id;
714 let other_outlet = next_node.inputs[flipped as usize];
715 if let Some(uni) = &model.outlet_fact(other_outlet)?.uniform {
716 let mut patch = TypedModelPatch::default();
717 let cst = patch.add_const(&model.node(other_outlet.node).name, uni.clone())?;
718 let output = patch.tap_model(model, node.id.into())?;
719 let wire = wire_with_rank_broadcast(
720 &next_node.name,
721 &mut patch,
722 op.clone(),
723 &if flipped { [output, cst] } else { [cst, output] },
724 )?;
725 let wire = patch.wire_node(&succ.name, succ.op.clone(), &wire)?[0];
726 patch.shunt_outside(model, next_node.id.into(), wire)?;
727 return Ok(Some(patch));
728 }
729 }
730 }
731 if let Some(op) = succ.op_as::<ops::binary::OptBinUnicast>() {
732 let in_1_fact = model.outlet_fact(succ.inputs[0])?;
733 let in_2_fact = model.outlet_fact(succ.inputs[1])?;
734 if op.binop.is::<ops::math::Add>()
735 && self.mmm.len() == 1
736 && in_1_fact.without_value() == in_2_fact.without_value()
737 {
738 let other_slot = 1 - node.outputs[0].successors[0].slot;
739 let other_input = succ.inputs[other_slot];
740 let other_input = patch.tap_model(model, other_input)?;
741 let other_fact = patch.outlet_fact(other_input)?;
742
743 if other_fact.shape == self.c_fact.shape {
744 let other_storage = unsafe { self.mmm[0].c_view(self.c_m_axis, self.c_n_axis) };
745 let mapping =
746 MapOutputAxisToInput((0..other_fact.rank()).map(|x| (x, x)).collect());
747 return self.fuse_op(
748 model,
749 node,
750 patch,
751 vec![ProtoFusedSpec::AddUnicast(other_storage, node.inputs.len(), mapping)],
752 &[other_input],
753 );
754 }
755 } else {
756 rule_if_some!(mut binop = op.binop.as_linalg_binop());
757 let flipped = succ.inputs[0].node == node.id;
758 if flipped {
759 binop = binop.flip();
760 }
761 let other_outlet = succ.inputs[flipped as usize];
762 return self.fuse_binary(model, node, patch, other_outlet, binop);
763 }
764 };
765 Ok(None)
766 }
767
768 as_op!();
769}
770
771impl OptMatMul {
772 pub fn new(
773 mmm: Vec<Box<dyn MatMatMul>>,
774 mode_picker: ModePicker,
775 c_fact: TypedFact,
776 c_m_axis: Option<usize>,
777 c_n_axis: Option<usize>,
778 micro_ops: Vec<ProtoFusedSpec>,
779 trivial_packing: bool,
780 ) -> TractResult<Self> {
781 if let Some(m) = c_m_axis {
782 ensure!(m < c_fact.rank());
783 }
784 if let Some(n) = c_n_axis {
785 ensure!(n < c_fact.rank());
786 }
787 let mut it = OptMatMul {
788 mmm,
789 mode_picker,
790 c_fact,
791 c_m_axis,
792 c_n_axis,
793 micro_ops,
794 trivial_path: false,
795 trivial_packing,
796 };
797 it.update_trivial_path();
798 Ok(it)
799 }
800
801 pub fn guess_k(&self) -> Option<TDim> {
803 self.micro_ops
804 .iter()
805 .find_map(
806 |o| {
807 if let ProtoFusedSpec::AddMatMul { geo, .. } = o { Some(geo) } else { None }
808 },
809 )
810 .map(|geo| geo.k.clone())
811 }
812
813 #[inline]
814 pub fn m(&self) -> &TDim {
815 self.c_m_axis.map(|ax| &self.c_fact.shape[ax]).unwrap_or(&TDim::Val(1))
816 }
817
818 #[inline]
819 pub fn n(&self) -> &TDim {
820 self.c_n_axis.map(|ax| &self.c_fact.shape[ax]).unwrap_or(&TDim::Val(1))
821 }
822
823 fn update_trivial_path(&mut self) {
824 self.trivial_path = self.can_use_trivial_path();
825 }
826
827 fn absorb_squeeze(&self, into: &IntoShape) -> Option<Self> {
831 if into.strides != Tensor::natural_strides(&into.dims) {
832 return None;
833 }
834 let old = self.c_fact.shape.as_concrete()?;
835 let removed = pure_squeeze_removed(old, &into.dims)?;
836 if removed.iter().any(|ax| Some(*ax) == self.c_m_axis || Some(*ax) == self.c_n_axis) {
837 return None;
838 }
839 let mut batch_axes: TVec<usize> = tvec!();
843 self.micro_ops.iter().for_each(|uop| uop.push_mapped_c_axes(&mut batch_axes));
844 if batch_axes.iter().any(|ax| old.get(*ax).copied().unwrap_or(1) > 1) {
845 return None;
846 }
847 let mut new_op = self.clone();
848 for axis in removed.iter().rev() {
849 new_op.c_fact.shape.remove_axis(*axis).ok()?;
850 if let Some(c_m_axis) = &mut new_op.c_m_axis {
851 *c_m_axis -= (*c_m_axis > *axis) as usize;
852 }
853 if let Some(c_n_axis) = &mut new_op.c_n_axis {
854 *c_n_axis -= (*c_n_axis > *axis) as usize;
855 }
856 for uop in &mut new_op.micro_ops {
857 uop.rm_c_axis(*axis);
858 }
859 }
860 Some(new_op)
861 }
862
863 fn can_use_trivial_path(&self) -> bool {
864 self.c_fact.shape.is_concrete()
865 && self.c_fact.shape.iter().enumerate().all(|(ax, dim)| {
866 Some(ax) == self.c_m_axis || Some(ax) == self.c_n_axis || dim.is_one()
867 })
868 && self.trivial_packing
869 && self.micro_ops.iter().all(|o| o.is_trivial())
870 }
871
872 fn bake_const_operands(
878 &self,
879 model: &TypedModel,
880 node: &TypedNode,
881 ) -> TractResult<Option<TypedModelPatch>> {
882 let bakeable = |operand: &MatMulOperand, mapping: &MapOutputAxisToInput| -> bool {
883 if let MatMulOperand::Input(i) = operand {
884 mapping.0.is_empty()
885 && model.outlet_fact(node.inputs[*i]).is_ok_and(|f| {
886 f.konst
887 .as_ref()
888 .and_then(|k| k.try_storage_as::<PackedMatrixStorage>().ok())
889 .is_some()
890 })
891 } else {
892 false
893 }
894 };
895 let mut baked: TVec<usize> = tvec!();
896 for op in &self.micro_ops {
897 if let ProtoFusedSpec::AddMatMul { geo, a, b, .. } = op {
898 if bakeable(a, &geo.c_to_a_axis_mapping) {
899 let MatMulOperand::Input(i) = a else { unreachable!() };
900 baked.push(*i);
901 }
902 if bakeable(b, &geo.c_to_b_axis_mapping) {
903 let MatMulOperand::Input(i) = b else { unreachable!() };
904 baked.push(*i);
905 }
906 }
907 }
908 if baked.is_empty() {
909 return Ok(None);
910 }
911 baked.sort();
912 baked.dedup();
913 let remap: Vec<Option<usize>> = {
914 let mut ni = 0;
915 (0..node.inputs.len())
916 .map(|i| {
917 (!baked.contains(&i)).then(|| {
918 let cur = ni;
919 ni += 1;
920 cur
921 })
922 })
923 .collect()
924 };
925 let const_value = |i: usize| -> TractResult<Box<dyn MMMInputValue>> {
926 let konst = model.outlet_fact(node.inputs[i])?.konst.clone().unwrap();
927 Ok(dyn_clone::clone_box(konst.try_storage_as::<PackedMatrixStorage>()?.value()))
928 };
929 let map_operand = |operand: &MatMulOperand| -> TractResult<MatMulOperand> {
930 Ok(match operand {
931 MatMulOperand::Input(i) if baked.contains(i) => {
932 MatMulOperand::Const(const_value(*i)?)
933 }
934 MatMulOperand::Input(i) => MatMulOperand::Input(remap[*i].unwrap()),
935 MatMulOperand::Const(v) => MatMulOperand::Const(v.clone()),
936 })
937 };
938 let micro_ops = self
939 .micro_ops
940 .iter()
941 .map(|op| -> TractResult<ProtoFusedSpec> {
942 use ProtoFusedSpec::*;
943 Ok(match op {
944 AddMatMul { geo, a, b, packings } => AddMatMul {
945 geo: geo.clone(),
946 a: map_operand(a)?,
947 b: map_operand(b)?,
948 packings: packings.clone(),
949 },
950 BinScalar(v, op) => BinScalar(remap[*v].unwrap(), *op),
951 LeakyRelu(v) => LeakyRelu(remap[*v].unwrap()),
952 BinPerRow(v, op, m) => BinPerRow(remap[*v].unwrap(), *op, m.clone()),
953 BinPerCol(v, op, m) => BinPerCol(remap[*v].unwrap(), *op, m.clone()),
954 AddRowColProducts(r, c) => {
955 AddRowColProducts(remap[*r].unwrap(), remap[*c].unwrap())
956 }
957 AddUnicast(s, v, m) => AddUnicast(*s, remap[*v].unwrap(), m.clone()),
958 Scaler(s) => Scaler(*s),
959 Store(o) => Store(o.clone()),
960 })
961 })
962 .collect::<TractResult<Vec<_>>>()?;
963 let new_op = OptMatMul { micro_ops, ..self.clone() };
964 let kept: TVec<OutletId> =
965 (0..node.inputs.len()).filter(|i| !baked.contains(i)).map(|i| node.inputs[i]).collect();
966 let mut patch = TypedModelPatch::new(format!("bake const operands into {}", node.name));
967 let taps = patch.taps(model, &kept)?;
968 let output = patch.wire_node(&node.name, new_op, &taps)?;
969 patch.shunt_outside(model, node.id.into(), output[0])?;
970 Ok(Some(patch))
971 }
972
973 fn fuse_op(
974 &self,
975 model: &TypedModel,
976 node: &TypedNode,
977 mut patch: TypedModelPatch,
978 fused_micro_op: Vec<ProtoFusedSpec>,
979 additional_inputs: &[OutletId],
980 ) -> TractResult<Option<TypedModelPatch>> {
981 let succ = model.node(node.outputs[0].successors[0].node);
982 let mut new_op = self.clone();
983 let before_last = new_op.micro_ops.len() - 1..new_op.micro_ops.len() - 1;
984 new_op.micro_ops.splice(before_last, fused_micro_op);
985 new_op.c_fact = succ.outputs[0].fact.clone();
986 new_op.update_trivial_path();
987 let mut inputs = patch.taps(model, &node.inputs)?;
988 inputs.extend(additional_inputs.iter().cloned());
989 let output = patch.wire_node(&succ.name, new_op, &inputs)?;
990 patch.shunt_outside(model, succ.id.into(), output[0])?;
991 Ok(Some(patch))
992 }
993
994 fn fuse_binary(
995 &self,
996 model: &TypedModel,
997 node: &TypedNode,
998 mut patch: TypedModelPatch,
999 value: OutletId,
1000 binop: BinOp,
1001 ) -> TractResult<Option<TypedModelPatch>> {
1002 let fact = model.outlet_fact(value)?;
1003 let mut v = patch.tap_model(model, value)?;
1004 if fact.datum_type != self.mmm[0].internal_type() {
1005 v = patch.wire_node(
1006 format!("{}.cast-input-{}", node.name, node.inputs.len()),
1007 cast(self.mmm[0].internal_type()),
1008 &[v],
1009 )?[0];
1010 }
1011 let value = node.inputs.len();
1012 let additional_input = tvec!(v);
1013 if fact.shape.volume() == 1.to_dim() {
1014 return self.fuse_op(
1015 model,
1016 node,
1017 patch,
1018 vec![ProtoFusedSpec::BinScalar(value, binop)],
1019 &additional_input,
1020 );
1021 }
1022 let other_shape = fact.shape.to_owned();
1023 if self.c_m_axis.is_some_and(|ax| {
1024 other_shape[ax] == self.c_fact.shape[ax] && other_shape[ax] == other_shape.volume()
1025 }) {
1026 return self.fuse_op(
1027 model,
1028 node,
1029 patch,
1030 vec![ProtoFusedSpec::BinPerRow(
1031 value,
1032 binop,
1033 MapOutputAxisToInput(tvec!((self.c_m_axis.unwrap(), self.c_m_axis.unwrap()))),
1034 )],
1035 &additional_input,
1036 );
1037 }
1038 if self.c_n_axis.is_some_and(|ax| {
1039 other_shape[ax] == self.c_fact.shape[ax] && other_shape[ax] == other_shape.volume()
1040 }) {
1041 return self.fuse_op(
1042 model,
1043 node,
1044 patch,
1045 vec![ProtoFusedSpec::BinPerCol(
1046 value,
1047 binop,
1048 MapOutputAxisToInput(tvec!((self.c_n_axis.unwrap(), self.c_n_axis.unwrap()))),
1049 )],
1050 &additional_input,
1051 );
1052 }
1053 Ok(None)
1054 }
1055}