Struct tract_core::model::Node
source · pub struct Node<F: Fact + Hash, O: Hash> {
pub id: usize,
pub name: String,
pub inputs: Vec<OutletId>,
pub op: O,
pub outputs: TVec<Outlet<F>>,
}Expand description
A Node in an Model.
Parameterized by a Fact implementation matching the one used in the model.
Fields§
§id: usizenode id in the model
Caution: this id will not be persistent during networks transformation
name: Stringname of the node
This will usually come from the importing framework. tract
transformation try to maintain the names accross transformations.
inputs: Vec<OutletId>A list of incoming tensors, identified by the node outlet that creates them.
op: OThe actual operation the node performs.
outputs: TVec<Outlet<F>>List of ouputs, with their descendant and tensor type information.
Implementations§
source§impl<F, NodeOp> Node<F, NodeOp>where
F: Fact + Hash,
NodeOp: Debug + Display + AsRef<dyn Op> + AsMut<dyn Op> + Hash,
impl<F, NodeOp> Node<F, NodeOp>where
F: Fact + Hash,
NodeOp: Debug + Display + AsRef<dyn Op> + AsMut<dyn Op> + Hash,
sourcepub fn op(&self) -> &dyn Op
pub fn op(&self) -> &dyn Op
Access the op of the node
Examples found in repository?
src/model/node.rs (line 53)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn op_as<O: Op>(&self) -> Option<&O> {
self.op().downcast_ref::<O>()
}
/// Try to downcast the node operation to O.
pub fn op_as_mut<O: Op>(&mut self) -> Option<&mut O> {
self.op.as_mut().downcast_mut::<O>()
}
/// Check if the node operation is of type O.
pub fn op_is<O: Op>(&self) -> bool {
self.op_as::<O>().is_some()
}
/// Check that this node produce the same outputs as `other`.
pub fn same_as(&self, other: &Node<F, NodeOp>) -> bool {
self.inputs == other.inputs && self.op().same_as(other.op())
}More examples
src/plan.rs (line 153)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
pub fn new(plan: P) -> TractResult<SimpleState<F, O, M, P>> {
let values = vec![None; plan.borrow().model.borrow().nodes().len()];
let mut session = SessionState::default();
let model = plan.borrow().model();
let states: Vec<Option<Box<dyn OpState>>> = model
.nodes()
.iter()
.map(|n: &Node<F, O>| n.op().state(&mut session, n.id))
.collect::<TractResult<_>>()?;
Ok(SimpleState { plan, states, session_state: session, values, _phantom: PhantomData })
}
/// Reset wires state.
pub fn reset_turn(&mut self) -> TractResult<()> {
self.values.iter_mut().for_each(|s| *s = None);
Ok(())
}
/// Reset op inner state.
pub fn reset_op_states(&mut self) -> TractResult<()> {
let &mut SimpleState { ref plan, ref mut session_state, ref mut states, .. } = self;
*states = plan
.borrow()
.model()
.nodes()
.iter()
.map(|n| n.op().state(session_state, n.id))
.collect::<TractResult<_>>()?;
Ok(())
}
pub fn run(&mut self, inputs: TVec<TValue>) -> TractResult<TVec<TValue>> {
self.run_plan_with_eval(inputs, self::eval)
}
pub fn exec(&mut self) -> TractResult<()> {
self.exec_plan_with_eval(self::eval)
}
pub fn run_plan_with_eval<Eval, E>(
&mut self,
inputs: TVec<TValue>,
eval: Eval,
) -> TractResult<TVec<TValue>>
where
Eval: for<'a, 'b, 'c> FnMut(
&'a mut SessionState,
Option<&'b mut (dyn OpState + 'static)>,
&'c Node<F, O>,
TVec<TValue>,
) -> Result<TVec<TValue>, E>,
E: Into<anyhow::Error> + Send + Sync + 'static,
{
self.set_inputs(inputs)?;
self.exec_plan_with_eval(eval)?;
let outputs = self.outputs()?;
self.reset_turn()?;
Ok(outputs)
}
pub fn exec_plan_with_eval<Eval, E>(&mut self, mut eval: Eval) -> TractResult<()>
where
Eval: for<'a, 'b, 'c> FnMut(
&'a mut SessionState,
Option<&'b mut (dyn OpState + 'static)>,
&'c Node<F, O>,
TVec<TValue>,
) -> Result<TVec<TValue>, E>,
E: Into<anyhow::Error> + Send + Sync + 'static,
{
{
let &mut SimpleState {
ref plan,
ref mut session_state,
ref mut states,
ref mut values,
..
} = self;
let plan = plan.borrow();
let model = plan.model().borrow();
for (step, n) in plan.order.iter().enumerate() {
let node = model.node(*n);
trace!("Running step {}, node {}", step, node);
let mut inputs: TVec<TValue> = tvec![];
for i in &node.inputs {
trace!(" use input {:?}", i);
let prec_node = model.node(i.node);
let prec = values[i.node].as_ref().ok_or_else(|| {
format_err!("Computing {}, precursor {} not done:", node, prec_node)
})?;
inputs.push(prec[i.slot].clone())
}
for flush in &plan.flush_lists[step] {
trace!(" Ran {} can now flush {}", node, model.node(*flush));
values[*flush] = None;
}
if cfg!(debug_assertions) {
let facts = model.node_input_facts(node.id)?;
if facts.len() != inputs.len() {
bail!(
"Evaluating {}: expected {} inputs, got {}",
node,
facts.len(),
inputs.len()
);
}
for (ix, (v, f)) in inputs.iter().zip(facts.iter()).enumerate() {
if !f.matches(v, Some(&session_state.resolved_symbols))? {
bail!(
"Evaluating {}: input {:?}, expected {:?}, got {:?}",
node,
ix,
f,
v
);
}
}
}
let vs = eval(session_state, states[node.id].as_deref_mut(), node, inputs)
.map_err(|e| e.into())?;
if plan.has_unresolved_symbols {
for (o, v) in node.outputs.iter().zip(vs.iter()) {
if let Ok(f) = o.fact.to_typed_fact() {
for (dim_abstract, dim_concrete) in f.shape.iter().zip(v.shape()) {
Self::resolve(
&mut session_state.resolved_symbols,
&dim_abstract,
*dim_concrete as i64,
);
}
}
}
}
if cfg!(debug_assertions) {
let facts = model.node_output_facts(node.id)?;
if facts.len() != vs.len() {
bail!(
"Evaluating {}: expected {} outputs, got {}",
node,
facts.len(),
vs.len()
);
}
for (ix, (v, f)) in vs.iter().zip(facts.iter()).enumerate() {
if node.outputs[ix].successors.len() == 0 {
continue;
}
if !f.matches(v, Some(&session_state.resolved_symbols))? {
bail!(
"Evaluating {}: output {:?}, expected {:?}, got {:?}",
node,
ix,
f,
v
);
}
}
}
values[node.id] = Some(vs);
}
}
Ok(())
}
pub fn set_inputs(&mut self, inputs: TVec<TValue>) -> TractResult<()> {
ensure!(
inputs.len() == self.model().inputs.len(),
"Wrong number of inputs for model. Expected {} got {}",
self.model().inputs.len(),
inputs.len()
);
for (ix, t) in inputs.into_iter().enumerate() {
self.set_input(ix, t)?
}
Ok(())
}
fn resolve(symbols: &mut SymbolValues, expected: &TDim, provided: i64) {
match expected {
TDim::Sym(s) => symbols[s] = Some(provided),
TDim::MulInt(x, expr) => Self::resolve(symbols, expr, provided / *x),
_ => (),
}
}
pub fn set_input(&mut self, input: usize, t: TValue) -> TractResult<()> {
let outlet: OutletId = *self
.model()
.input_outlets()?
.get(input)
.ok_or_else(|| format_err!("Invalid input id for model ({}).", input))?;
let SimpleState { plan, session_state, .. } = self;
let plan = (*plan).borrow();
let model = plan.model.borrow();
if let Ok(fact) = model.outlet_fact(outlet)?.to_typed_fact() {
for (expected, provided) in fact.shape.iter().zip(t.shape()) {
Self::resolve(&mut session_state.resolved_symbols, &expected, *provided as i64)
}
}
let fact = self.plan.borrow().model().outlet_fact(outlet)?;
ensure!(
fact.matches(&t, Some(&self.session_state.resolved_symbols))
.with_context(|| format!("Setting input {}", input))?,
"Input at index {} has incorrect dtype or shape (got shape {:?} and dtype {:?}, expected to match fact {:?})",
input,
t.shape(),
t.datum_type(),
fact
);
self.session_state.inputs.insert(outlet.node, t);
Ok(())
}
pub fn output(&self, id: usize) -> TractResult<&TValue> {
let outlet = self.model().output_outlets()?.get(id).with_context(|| {
format!(
"Required output {}, only have {}",
id,
self.model().output_outlets().unwrap().len()
)
})?;
let value: &TValue = self
.values
.get(outlet.node)
.context("node id for output beyond node values array")?
.as_ref()
.context("node is not an output")?
.get(outlet.slot)
.context("slot id too high")?;
Ok(value)
}
pub fn outputs(&mut self) -> TractResult<TVec<TValue>> {
let SimpleState { ref plan, ref mut values, .. } = self;
let mut v = tvec![];
for o in plan.borrow().outputs.iter() {
let vs = values[o.node].as_mut().ok_or_else(|| {
format_err!(
"Outputs of {:?} are not computed",
&plan.borrow().model().nodes()[o.node]
)
})?;
v.push(vs[o.slot].clone())
}
Ok(v)
}
pub fn set_values(&mut self, id: usize, values: TVec<TValue>) -> TractResult<()> {
self.values[id] = Some(values);
Ok(())
}
pub fn set_value(&mut self, id: usize, value: TValue) -> TractResult<()> {
self.set_values(id, tvec!(value))
}
pub fn prepare_inputs(&self, node: usize) -> TractResult<TVec<TValue>> {
let SimpleState { ref plan, ref values, .. } = self;
let plan = plan.borrow();
let nodes = plan.model().nodes();
let node = &nodes[node];
let mut inputs: TVec<TValue> = tvec![];
for i in &node.inputs {
let prec_node = &nodes[i.node];
let prec = values[i.node].as_ref().ok_or_else(|| {
format_err!("Computing {}, precursor {} not done.", node, prec_node)
})?;
inputs.push(prec[i.slot].clone())
}
Ok(inputs)
}
pub fn compute_one(&mut self, node: usize) -> TractResult<()> {
let inputs = self.prepare_inputs(node)?;
self.compute_one_with_inputs(node, inputs)
}
pub fn compute_one_with_inputs(
&mut self,
node: usize,
inputs: TVec<TValue>,
) -> TractResult<()> {
let SimpleState { ref plan, ref mut session_state, ref mut values, .. } = self;
let plan = plan.borrow();
let nodes = plan.model().nodes();
let node = &nodes[node];
let vs = match self.states[node.id] {
Some(ref mut state) => state.eval(session_state, node.op(), inputs),
None => node.op().eval(inputs),
}
.with_context(|| format!("Evaluating {}", node))?;
values[node.id] = Some(vs);
Ok(())
}
pub fn compute_recursively(&mut self, node: usize) -> TractResult<&[TValue]> {
let values = {
#[allow(clippy::needless_collect)] // clippy bug ?
let precs: Vec<usize> =
self.model().nodes()[node].inputs.iter().map(|i| i.node).collect();
for i in precs.into_iter() {
if self.values[i].is_none() {
let _ = self.compute_recursively(i)?;
}
}
let mut inputs: TVec<TValue> = tvec![];
{
let node = &self.model().nodes()[node];
for i in &node.inputs {
inputs.push(self.values[i.node].as_ref().unwrap()[i.slot].clone())
}
}
let Self { ref mut states, ref mut session_state, ref plan, .. } = self;
let plan = plan.borrow();
match states[node] {
Some(ref mut state) => {
state.eval(session_state, plan.borrow().model().nodes()[node].op(), inputs)
}
None => plan.borrow().model().nodes()[node].op().eval(inputs),
}
.with_context(|| format!("Evaluating {:?}", node))?
};
self.values[node] = Some(values);
Ok(self.values[node].as_ref().unwrap())
}
pub fn take_by_name(&mut self, name: &str) -> TractResult<TVec<Tensor>> {
let id = self.model().node_by_name(name)?.id;
Self::take(self, id)
}
pub fn take(&mut self, id: usize) -> TractResult<TVec<Tensor>> {
Ok(self.values[id]
.take()
.ok_or_else(|| format_err!("Node is not computed"))?
.into_iter()
.map(|v| v.into_tensor())
.collect())
}
pub fn plan(&self) -> &SimplePlan<F, O, M> {
self.plan.borrow()
}
pub fn model(&self) -> &Graph<F, O> {
self.plan().model()
}
pub fn freeze(&self) -> FrozenSimpleState<F, O, M, P> {
FrozenSimpleState {
plan: self.plan.clone(),
inputs: self
.session_state
.inputs
.iter()
.map(|(ix, t)| (*ix, t.clone().into_tensor()))
.collect(),
resolved_symbols: self.session_state.resolved_symbols.clone(),
tensors: self.session_state.tensors.clone(),
states: self.states.iter().map(|s| s.as_ref().map(|s| s.freeze())).collect(),
values: self
.values
.iter()
.map(|t| t.as_ref().map(|t| t.iter().map(|t| t.clone().into_tensor()).collect()))
.collect(),
_phantom: PhantomData,
}
}
}
pub fn eval<F, O>(
session_state: &mut SessionState,
mut state: Option<&mut (dyn OpState + 'static)>,
node: &Node<F, O>,
input: TVec<TValue>,
) -> TractResult<TVec<TValue>>
where
F: Fact + Hash + Clone + 'static,
O: Debug + Display + AsRef<dyn Op> + AsMut<dyn Op> + Clone + 'static + Hash,
{
let r = match state {
Some(ref mut state) => state.eval(session_state, node.op(), input),
None => node.op().eval(input),
}
.with_context(|| format!("Evaluating {}", node));
r
}src/ops/matmul/mir_unary.rs (line 234)
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
fn declutter_precusor_is_concat(
&self,
model: &TypedModel,
node: &TypedNode,
) -> TractResult<Option<TypedModelPatch>> {
if let Some(concat) = model.nodes()[node.inputs[0].node].op().downcast_ref::<TypedConcat>()
{
let mut patch = TypedModelPatch::new("split over k-concatenated input");
if concat.axis == self.axes.b_k {
let concat_node = model.node(node.inputs[0].node);
let offsets = concat
.offsets(&model.node_input_facts(concat_node.id)?)?
.iter()
.map(|x| x.to_usize())
.collect::<TractResult<Vec<usize>>>()?;
let mut wires = vec![];
for (ix, input) in concat_node.inputs.iter().enumerate() {
let wire = patch.tap_model(model, *input)?;
let a = self.a.slice(self.axes.a_k, offsets[ix], offsets[ix + 1])?;
let wire = patch.wire_node(
format!("{}.k-{}-{}", node.name, offsets[ix], offsets[ix + 1]),
MatMulUnary { a: a.into_arc_tensor(), ..self.clone() },
&[wire],
)?[0];
wires.push(wire)
}
let mut wire = wires[0];
for (ix, w) in wires[1..].iter().enumerate() {
wire = patch.wire_node(
format!("{}.k-add-{}", node.name, ix),
crate::ops::binary::TypedBinOp(Box::new(crate::ops::math::Add)),
&[wire, *w],
)?[0];
}
patch.shunt_outside(model, OutletId::new(node.id, 0), wire)?;
return Ok(Some(patch));
}
}
Ok(None)
}src/model/graph.rs (line 609)
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
for i in 0..self.nodes.len() {
let input_1 = self.nodes[i]
.inputs
.get(0)
.map(|o| format!("{:?}", o))
.unwrap_or_else(|| "".to_string());
let input_2 = self.nodes[i]
.inputs
.get(1)
.map(|o| format!("{:?}", o))
.unwrap_or_else(|| "".to_string());
let output_1 = self
.outlet_successors(OutletId::new(i, 0))
.get(0)
.map(|o| format!("{:?}", o))
.unwrap_or_else(|| "".to_string());
let output_2 = self
.outlet_successors(OutletId::new(i, 0))
.get(1)
.map(|o| format!("{:?}", o))
.unwrap_or_else(|| "".to_string());
writeln!(
fmt,
"{:5} | {:8} {:8} -> {:8} {:8} | {:25} {:50} {:?} => {:?}",
i,
input_1,
input_2,
output_1,
output_2,
self.nodes[i].op().name(),
self.nodes[i].name,
self.node_input_facts(i).unwrap(),
self.node_output_facts(i).unwrap(),
)?;
if self.nodes[i].inputs.len() > 2 {
writeln!(
fmt,
" | * inputs: {}",
self.nodes[i].inputs.iter().map(|s| format!("{:?}", s)).join(", ")
)?;
}
if self.nodes[i].outputs.len() > 1
|| self.outlet_successors((i, 0).into()).len() > 2
|| (self.outlet_label(i.into()).is_some()
&& self.outlet_label(i.into()).unwrap() != self.nodes[i].name)
{
for o in 0..self.nodes[i].outputs.len() {
if self.outlet_successors((i, o).into()).len() > 0 {
writeln!(
fmt,
" | * output #{}: {} {}",
o,
self.outlet_label((i, o).into()).unwrap_or(""),
self.outlet_successors((i, o).into())
.iter()
.map(|s| format!("{:?}", s))
.join(", "),
)?;
}
}
}
}
writeln!(fmt, "outputs: {}", self.outputs.iter().map(|o| format!("{:?}", o)).join(", "))?;
Ok(())
}src/ops/matmul/mir_quant_unary.rs (line 163)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
fn declutter(
&self,
model: &TypedModel,
node: &TypedNode,
) -> TractResult<Option<TypedModelPatch>> {
use crate::ops::array::TypedConcat;
if let Some(concat) = model.nodes()[node.inputs[0].node].op().downcast_ref::<TypedConcat>()
{
let mut patch = TypedModelPatch::new("split over k-concatenated input");
let k_axis = self.axes.a_k;
if concat.axis == self.axes.b_k {
let concat_node = model.node(node.inputs[0].node);
let offsets = concat
.offsets(&model.node_input_facts(concat_node.id)?)?
.iter()
.map(|x| x.to_usize())
.collect::<TractResult<Vec<usize>>>()?;
let mut wires = vec![];
let mut params_for_split = self.params.clone();
params_for_split.a_scale = tensor0(1.0f32).into();
params_for_split.b_scale = tensor0(1.0f32).into();
params_for_split.c_scale = tensor0(1.0f32).into();
params_for_split.c0 = tensor0(0i32).into();
let input_outlets = node
.inputs
.iter()
.skip(1)
.map(|o| patch.tap_model(model, *o))
.collect::<TractResult<TVec<_>>>()?;
let params_outlets = self.params.as_outlet_ids(
&mut patch,
&node.name,
&input_outlets,
self.a.datum_type(),
model.node_input_facts(node.id)?[0].datum_type,
self.output_type,
)?;
let scale = combine_scales(
&mut patch,
&node.name,
params_outlets[1],
params_outlets[3],
params_outlets[5],
)?;
let c0 = params_outlets[4];
for (ix, input) in concat_node.inputs.iter().enumerate() {
let wire = patch.tap_model(model, *input)?;
let a = self.a.slice(k_axis, offsets[ix], offsets[ix + 1])?;
let wire = patch
.wire_node(
format!("{}.k-{}-{}", node.name, offsets[ix], offsets[ix + 1]),
Self {
a: a.into_arc_tensor(),
output_type: DatumType::I32,
bias: self.bias.clone().filter(|_| ix == 0),
params: params_for_split.clone(),
..self.clone()
},
&[wire],
)
.context("wiring new matmulunary")?[0];
wires.push(wire)
}
let mut wire = wires[0];
for (ix, w) in wires[1..].iter().enumerate() {
wire = patch.wire_node(
format!("{}.k-add-{}", node.name, ix),
crate::ops::binary::TypedBinOp(Box::new(crate::ops::math::Add)),
&[wire, *w],
)?[0];
}
wire = requant(&mut patch, &node.name, wire, self.output_type, scale, c0)?;
patch.shunt_outside(model, OutletId::new(node.id, 0), wire)?;
return Ok(Some(patch));
}
}
Ok(None)
}src/model/patch.rs (line 274)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
pub fn apply(self, target: &mut Graph<F, O>) -> TractResult<()> {
let prior_target_inputs = target.input_outlets()?.len();
let prior_target_outputs = target.output_outlets()?.len();
let ModelPatch {
model: patch,
incoming: mut mapping,
shunt_outlet_by,
obliterate,
inputs: replaced_inputs,
..
} = self;
let mut all_inputs = HashMap::new(); // new_node_id_in_model -> [ patch_outlet_id ]
let mut model_input_outlets = target.input_outlets()?.to_vec();
for node in patch.nodes {
if <Graph<F, O>>::is_source(&node.op)
&& mapping.contains_key(&OutletId::new(node.id, 0))
{
// this is a tap
continue;
}
let Node { id: patch_node_id, name, inputs, op, outputs } = node;
let n_outputs = outputs.len();
for dup in 0..target.nodes.len() {
if target.node(dup).op().same_as(op.as_ref())
&& inputs.len() == target.node(dup).inputs.len()
&& inputs
.iter()
.zip(target.node(dup).inputs.iter())
.all(|(patch_input, d)| mapping[patch_input] == *d)
{
for ix in 0..n_outputs {
mapping.insert(OutletId::new(patch_node_id, ix), OutletId::new(dup, ix));
}
continue;
}
}
let facts = outputs.into_iter().map(|of| of.fact).collect();
let added_node_id = target.add_node(name, op, facts)?;
for ix in 0..n_outputs {
mapping.insert(OutletId::new(patch_node_id, ix), OutletId::new(added_node_id, ix));
}
all_inputs.insert(added_node_id, inputs);
if <Graph<F, O>>::is_source(&target.node(added_node_id).op) {
// this is actually an input replacement
model_input_outlets.iter_mut().for_each(|oo| {
if oo.node == replaced_inputs[&patch_node_id] {
oo.node = added_node_id;
}
});
}
}
debug_assert_eq!(target.input_outlets()?.len(), prior_target_inputs);
debug_assert_eq!(target.output_outlets()?.len(), prior_target_outputs);
for (outlet, by) in shunt_outlet_by {
let replace_by = mapping[&by];
let succs = target.nodes()[outlet.node].outputs[outlet.slot].successors.clone();
for succ in succs {
target.add_edge(replace_by, succ)?;
}
for o in target.outputs.iter_mut() {
if *o == outlet {
*o = replace_by;
}
}
if let Some(label) = target.outlet_labels.remove(&outlet) {
target.set_outlet_label(replace_by, label)?;
}
}
if target.outputs.len() > target.outputs.iter().sorted().dedup().count() {
bail!("Duplicate usage of node as output");
}
debug_assert_eq!(target.input_outlets()?.len(), prior_target_inputs);
debug_assert_eq!(target.output_outlets()?.len(), prior_target_outputs);
for (node, inputs) in all_inputs {
for (ix, input) in inputs.into_iter().enumerate() {
target.add_edge(mapping[&input], InletId::new(node, ix))?;
}
}
debug_assert_eq!(target.input_outlets()?.len(), prior_target_inputs);
debug_assert_eq!(target.output_outlets()?.len(), prior_target_outputs);
for node in obliterate {
target.node_mut(node).op = target.create_dummy();
}
debug_assert_eq!(target.input_outlets()?.len(), prior_target_inputs);
debug_assert_eq!(target.output_outlets()?.len(), prior_target_outputs);
target.set_input_outlets(&model_input_outlets)?;
Ok(())
}Additional examples can be found in:
sourcepub fn op_as<O: Op>(&self) -> Option<&O>
pub fn op_as<O: Op>(&self) -> Option<&O>
Try to downcast the node operation to O.
Examples found in repository?
More examples
src/ops/math/mod.rs (line 437)
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
fn declutter_recip(model: &TypedModel, node: &TypedNode) -> TractResult<Option<TypedModelPatch>> {
use super::element_wise::*;
if let Some(prec) = model.single_prec(node.id)? {
if let Some(ew) = prec.op_as::<ElementWiseOp>() {
let repl = if ew.0.is::<Sqrt>() {
Some(rsqrt())
} else if ew.0.is::<Rsqrt>() {
Some(sqrt())
} else {
None
};
if let Some(repl) = repl {
let mut patch = TypedModelPatch::default();
let mut wire = patch.tap_model(model, prec.inputs[0])?;
wire = patch.wire_node(&node.name, repl, &[wire])?[0];
patch.shunt_outside(model, node.id.into(), wire)?;
return Ok(Some(patch));
}
}
}
Ok(None)
}src/optim/slice.rs (line 86)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
pub fn should_slice_output(
model: &TypedModel,
node: &TypedNode,
axis: usize,
) -> TractResult<Option<TVec<usize>>> {
let Some(slice) = node.outputs[0].successors.iter().find_map(|inlet| {
model.node(inlet.node).op_as::<Slice>().filter(|slice| slice.axis == axis).map(|_| inlet.node)
}) else {
return Ok(None)
};
let slice_op = model.node(slice).op_as::<Slice>().unwrap();
let axis = slice_op.axis;
let mut boundaries = tvec!();
for succ in &node.outputs[0].successors {
if let Some(slice) = model.node(succ.node).op_as::<Slice>() {
if slice.axis == axis {
boundaries.push(slice.start.clone());
boundaries.push(slice.end.clone());
}
}
}
let mut boundaries: TVec<usize> = if let Ok(boundaries) =
boundaries.iter().map(|x| x.to_usize()).collect::<TractResult<TVec<_>>>()
{
boundaries
} else {
return Ok(None);
};
let end = if let Ok(x) = node.outputs[0].fact.shape[axis].to_usize() {
x
} else {
return Ok(None);
};
boundaries.push(end);
boundaries.retain(|x| *x > 0);
boundaries.sort();
boundaries.dedup();
Ok(Some(boundaries))
}
pub fn rewire_sliced_outputs(
model: &TypedModel,
node: &TypedNode,
axis: usize,
patch: &mut TypedModelPatch,
boundaries: &[usize],
splits: &[OutletId],
) -> TractResult<()> {
let full = patch.wire_node(
format!("{}.concat-{}", node.name, axis),
crate::ops::array::TypedConcat::new(axis),
splits,
)?[0];
patch.shunt_outside(model, node.id.into(), full)?;
for (ix, succ) in node.outputs[0].successors.iter().enumerate() {
if let Some(slice) =
model.node(succ.node).op_as::<Slice>().filter(|slice| slice.axis == axis)
{
// example: boundaries: 2, 3, wanted: 0..2 -> [0]
let slices: TVec<OutletId> = boundaries
.iter()
.zip(splits.iter())
.filter_map(|(up, split)| {
if *up > slice.start.to_usize().unwrap() && *up <= slice.end.to_usize().unwrap()
{
Some(*split)
} else {
None
}
})
.collect();
let wire = if slices.len() > 1 {
patch.wire_node(
format!("{}.concat-m{}..{}..{}", node.name, ix, slice.start, slice.end),
crate::ops::array::TypedConcat::new(axis),
&slices,
)?[0]
} else {
slices[0]
};
patch.shunt_outside(model, succ.node.into(), wire)?;
}
}
Ok(())
}src/ops/cnn/conv/unary.rs (line 720)
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
fn declutter_precursor_padding(
&self,
model: &TypedModel,
node: &TypedNode,
) -> TractResult<Option<TypedModelPatch>> {
if self.pool_spec.padding != PaddingSpec::Valid
&& !matches!(self.pool_spec.padding, PaddingSpec::Explicit(_, _, _))
{
return Ok(None);
}
let prec = model.node(node.inputs[0].node);
let pad = if let Some(pad) = prec.op_as::<Pad>() { pad } else { return Ok(None) };
let value = if let PadMode::Constant(c) = &pad.mode {
c
} else {
return Ok(None);
};
let shape = self.pool_spec.data_format.shape(&model.outlet_fact(node.inputs[0])?.shape)?;
if value.cast_to_scalar::<i64>()? != 0
|| (self.pool_spec.data_format.has_n() && pad.pads[0] != (0, 0))
|| pad.pads[shape.c_axis()] != (0, 0)
{
return Ok(None);
}
let mut before: TVec<usize> = pad.pads[shape.hw_axes()].iter().map(|pair| pair.0).collect();
let mut after: TVec<usize> = pad.pads[shape.hw_axes()].iter().map(|pair| pair.1).collect();
if let PaddingSpec::Explicit(bef, aft, false) = &self.pool_spec.padding {
izip!(&mut before, bef).for_each(|(pad, cv)| *pad += cv);
izip!(&mut after, aft).for_each(|(pad, cv)| *pad += cv);
}
let padding = PaddingSpec::Explicit(before, after, false);
let mut new = self.clone();
new.pool_spec.padding = padding;
let mut patch = TypedModelPatch::default();
let wire = patch.tap_model(model, prec.inputs[0])?;
let wire = patch.wire_node(&node.name, new, &[wire])?;
patch.shunt_outside(model, node.id.into(), wire[0])?;
Ok(Some(patch))
}src/half.rs (line 20)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
fn translate_node(
&self,
_source: &Graph<TypedFact, Box<dyn TypedOp>>,
node: &Node<TypedFact, Box<dyn TypedOp>>,
target: &mut Graph<TypedFact, Box<dyn TypedOp>>,
mapping: &HashMap<OutletId, OutletId>,
) -> TractResult<TVec<OutletId>> {
let new_op = if let Some(source) = node.op_as::<TypedSource>() {
Box::new(TypedSource::new(fact_f32_to_f16(&source.fact)))
} else if let Some(op) = node.op_as::<ConvUnary>() {
Box::new(ConvUnary {
kernel: tensor_f32_to_f16(&op.kernel),
bias: op.bias.as_ref().map(tensor_f32_to_f16),
..op.clone()
})
} else if let Some(op) = node.op_as::<DeconvUnary>() {
Box::new(DeconvUnary {
kernel: tensor_f32_to_f16(&op.kernel),
bias: op.bias.as_ref().map(tensor_f32_to_f16),
..op.clone()
})
} else if let Some(op) = node.op_as::<MatMulUnary>() {
Box::new(MatMulUnary { a: tensor_f32_to_f16(&op.a), ..op.clone() })
} else if let Some(op) = node.op_as::<Pad>() {
if let PadMode::Constant(t) = &op.mode {
Box::new(Pad {
mode: PadMode::Constant(tensor_f32_to_f16(t)),
..op.clone()
})
} else {
Box::new(op.clone())
}
} else if let Some(op) = node.op_as::<Scan>() {
let mut new = op.clone();
new.body = HalfTranslator.translate_model(&op.body)?;
for im in &mut new.input_mapping {
if let InputMapping::State { initializer: StateInitializer::Value(v) } = im {
*v = tensor_f32_to_f16(v)
}
}
Box::new(new)
} else {
node.op.clone()
};
target.wire_node(
&node.name,
new_op,
&node.inputs.iter().map(|i| mapping[i]).collect::<TVec<_>>(),
)
}src/ops/downsample/mod.rs (line 114)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
fn pull_downsample_up(
model: &TypedModel,
down_node: &TypedNode,
) -> TractResult<Option<TypedModelPatch>> {
model.check_consistency()?;
let down_op = down_node.op_as::<Downsample>().unwrap();
if let Some(prec) = model.single_prec(down_node.id)? {
let (input_facts, output_facts) = model.node_facts(prec.id)?;
let invariants = prec.op.invariants(&input_facts, &output_facts)?;
debug!("Consider pull {:?} over {:?} (invariants: {:?})", down_op, prec, invariants);
if let Some(slice_op) = prec.op_as::<ops::array::Slice>() {
if let Some(p) = array::pull_downsample_over_slice(model, prec, slice_op, down_node, down_op)? {
return Ok(Some(p))
}
} else if let Some(other_op) = prec.op_as::<AxisOp>() {
return array::pull_downsample_over_axis_op(model, prec, other_op, down_node, down_op);
} else if let Some(conv_op) = prec.op_as::<ops::cnn::conv::ConvUnary>() {
return conv::fuse_downsample_into_conv(model, prec, conv_op, down_node, down_op);
} else if let Some(other_op) = prec.op_as::<ops::scan::Scan>() {
return scan::pull_downsample_over_scan(model, prec, other_op, down_node, down_op);
}
if let Some(above_axis) = invariants.unary_track_axis_up(down_op.axis, false) {
let mut patch = TypedModelPatch::default();
let mut inputs = vec![];
for (ix, &oo) in prec.inputs.iter().enumerate() {
let source = patch.tap_model(model, oo)?;
let mut op = down_op.clone();
op.axis = above_axis;
let ds = patch.wire_node(
format!("{}.{}-{}", down_node.name, prec.name, ix),
op,
[source].as_ref(),
)?;
inputs.push(ds[0]);
}
let other = patch.wire_node(&prec.name, prec.op.clone(), &inputs)?;
patch.shunt_outside(model, OutletId::new(down_node.id, 0), other[0])?;
return Ok(Some(patch));
}
}
Ok(None)
}Additional examples can be found in:
sourcepub fn op_as_mut<O: Op>(&mut self) -> Option<&mut O>
pub fn op_as_mut<O: Op>(&mut self) -> Option<&mut O>
Try to downcast the node operation to O.
Examples found in repository?
src/ops/downsample/scan.rs (line 53)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
pub fn pull_downsample_over_scan(
model: &TypedModel,
scan_node: &TypedNode,
scan_op: &ops::scan::Scan,
down_node: &TypedNode,
down_op: &Downsample,
) -> TractResult<Option<TypedModelPatch>> {
if down_op.stride < 0 {
return Ok(None);
}
// introduce downsample at end of body
let mut downsampled_body = scan_op.body.clone();
downsampled_body.check_consistency()?;
let outputs = downsampled_body.output_outlets()?.to_owned();
let downsample_outputs = outputs
.into_iter()
.enumerate()
.map(|(ix, oo)| {
Ok(downsampled_body.wire_node(
format!("{}-{}", &down_node.name, ix),
down_op.clone(),
&[oo],
)?[0])
})
.collect::<TractResult<Vec<_>>>()?;
downsampled_body.set_output_outlets(&downsample_outputs)?;
downsampled_body.declutter()?;
downsampled_body.check_consistency()?;
// check if downsample ops introduced at end have swimmed up to scan inputs during declutter
for input in downsampled_body.input_outlets()? {
let input = downsampled_body.node(input.node);
if input.outputs[0]
.successors
.iter()
.any(|succ| !downsampled_body.node(succ.node).op().same_as(down_op))
{
return Ok(None);
}
}
let inputs = downsampled_body.input_outlets()?.to_vec();
for input in inputs {
let node = &mut downsampled_body.node_mut(input.node);
let fact = &mut node.outputs[0].fact;
*fact = down_op.transform_fact(fact)?;
node.op_as_mut::<crate::ops::source::TypedSource>().unwrap().fact = fact.clone();
let downsamples = downsampled_body.node(input.node).outputs[0].successors.clone();
for ds in downsamples {
TypedModelPatch::shunt_one_op(&downsampled_body as _, downsampled_body.node(ds.node))?
.apply(&mut downsampled_body)?;
}
}
downsampled_body.check_consistency()?;
let inner_model = downsampled_body.into_decluttered()?;
let mut new_scan = scan_op.clone();
new_scan.body = inner_model;
for input in &mut new_scan.input_mapping {
match input {
InputMapping::State { ref mut initializer } => {
if let StateInitializer::Value(ref v) = initializer {
let mut new_v = down_op.eval(tvec!(v.clone().into_tvalue()))?;
*initializer = StateInitializer::Value(new_v.remove(0).into_arc_tensor());
}
}
InputMapping::Scan(info) => {
if info.chunk > 0 && info.chunk as usize % down_op.stride as usize != 0 {
return Ok(None);
}
info.chunk = info.chunk.unsigned_abs().divceil(down_op.stride as usize) as isize
* info.chunk.signum()
}
_ => (),
}
}
for output in &mut new_scan.output_mapping {
if let Some(d) = output.full_dim_hint.as_mut() {
*d = down_op.transform_dim(d)
}
if let Some(info) = &mut output.scan {
if info.chunk as usize % down_op.stride as usize != 0 {
return Ok(None);
}
info.chunk = info.chunk.unsigned_abs().divceil(down_op.stride as usize) as isize
* info.chunk.signum()
}
}
let mut patch = TypedModelPatch::default();
let mut inputs = tvec!();
for (ix, &i) in scan_node.inputs.iter().enumerate() {
let tap = patch.tap_model(model, i)?;
let ds = patch.wire_node(format!("{}-{}", down_node.name, ix), down_op.clone(), &[tap])?[0];
inputs.push(ds);
}
let scan = patch.wire_node(&*scan_node.name, new_scan, &inputs)?;
for ix in 0..scan_node.outputs.len() {
// FIXME need to check earlier on that all output are followed by a ds
let succ = scan_node.outputs[ix].successors[0].node;
patch.shunt_outside(model, OutletId::new(succ, 0), scan[ix])?;
}
Ok(Some(patch))
}sourcepub fn op_is<O: Op>(&self) -> bool
pub fn op_is<O: Op>(&self) -> bool
Check if the node operation is of type O.
Examples found in repository?
src/optim/prop_const.rs (line 22)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
fn next(
&mut self,
_session: &mut OptimizerSession,
model: &TypedModel,
) -> TractResult<Option<TypedModelPatch>> {
let mut patch = TypedModelPatch::default();
for n in model.eval_order()? {
let node = model.node(n);
if node.op.is_stateless() && !node.op_is::<Const>() {
if let Some(inputs) = model
.node_input_facts(n)?
.iter()
.map(|f| f.konst.clone().map(|t| t.into_tvalue()))
.collect()
{
match node.op.eval(inputs) {
Ok(res) => {
for (ix, output) in res.into_iter().enumerate() {
let mut name = node.name.clone();
if ix > 0 {
name = format!("{}.{}", name, ix);
}
let wire = patch.add_const(name, output.into_arc_tensor())?;
patch.shunt_outside(model, (n, ix).into(), wire)?;
}
}
Err(e) => {
if !e.root_cause().is::<UndeterminedSymbol>() {
Err(e).with_context(|| {
format!("Eager eval {} during optimisation", model.node(n))
})?;
}
}
}
}
}
}
Ok(Some(patch).filter(|p| p.nodes.len() > 0))
}More examples
src/ops/change_axes.rs (line 742)
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
pub fn change_axes(
model: &TypedModel,
change: &AxisChange,
locked: &[OutletId],
bounds: &[TVec<OutletId>],
) -> TractResult<Option<(TypedModelPatch, TVec<(InOut, AxisOp)>)>> {
trace!("Considering change {:?}", change);
let mut todo_changes = vec![(change.clone(), None)];
let mut changed_wires = HashMap::new();
changed_wires.insert(change.outlet, change.op.clone());
let mut changed_ops: HashMap<usize, Box<dyn TypedOp>> = HashMap::new();
while let Some((c, emitter)) = todo_changes.pop() {
let outlets = if let Some(group) = bounds.iter().find(|b| b.contains(&c.outlet)) {
group.clone()
} else {
tvec![c.outlet]
};
for outlet in outlets {
if locked.contains(&outlet) {
trace!(" Change {:?} blocked by locked interface {:?}", change, outlet);
return Ok(None);
}
let mut nodes = vec![(outlet.node, InOut::Out(outlet.slot))];
for inlet in model.outlet_successors(outlet) {
nodes.push((inlet.node, InOut::In(inlet.slot)));
}
for (node_id, io) in nodes {
if Some(node_id) == emitter {
continue;
}
let node = model.node(node_id);
let more = node
.op
.change_axes(model, node, io, &c.op)
.with_context(|| format!("Propagating {:?} to node {}", change, node))?;
if more.is_none() {
trace!(" Propagation of {:?} blocked by {}", change, node);
return Ok(None);
}
let AxisChangeConsequence { substitute_op, wire_changes } = more.unwrap();
trace!(" Change {:?} enters {} from {:?}", c.op, node, io);
trace!(" propagates as {:?}", wire_changes);
if let Some(op) = substitute_op {
trace!(" replace op by {:?}", op);
changed_ops.insert(node.id, op);
}
for (wire, op) in wire_changes.into_iter() {
let outlet = wire.as_outlet(node);
match changed_wires.entry(outlet) {
Entry::Vacant(entry) => {
trace!(" {:?} {:?} change on {:?} is new", wire, op, outlet);
entry.insert(op.clone());
todo_changes.push((AxisChange { outlet, op }, Some(node_id)));
}
Entry::Occupied(previous) => {
if *previous.get() == op {
trace!(
" {:?} {:?} change on {:?} already done",
wire,
op,
outlet
);
} else {
trace!(
" {:?} {:?} change on {:?} conflicting with {:?}. Blocked.",
wire,
op,
outlet,
previous
);
return Ok(None);
}
}
}
}
}
}
}
trace!("Translating {:?} to patch", change);
let mut patch = TypedModelPatch::new(format!("{:?}", change));
let mut replaced_wires: HashMap<OutletId, OutletId> = HashMap::default();
let nodes_to_replace = changed_wires
.keys()
.map(|o| o.node)
.chain(changed_ops.keys().copied())
.collect::<std::collections::HashSet<usize>>();
for node_id in model.eval_order()? {
let node = model.node(node_id);
if nodes_to_replace.contains(&node_id) {
let mut inputs = tvec!();
for orig in &node.inputs {
let tgt = replaced_wires
.entry(*orig)
.or_insert_with(|| patch.tap_model(model, *orig).unwrap());
inputs.push(*tgt);
}
let op: Box<dyn TypedOp> =
changed_ops.get(&node_id).cloned().unwrap_or_else(|| node.op.clone());
let new_wires = patch.wire_node(&node.name, op, &inputs)?;
if new_wires.len() == 1
&& patch.node(new_wires[0].node).op_is::<crate::ops::source::TypedSource>()
{
patch.inputs.insert(new_wires[0].node, node_id);
}
for (ix, w) in new_wires.iter().enumerate() {
replaced_wires.insert((node_id, ix).into(), *w);
}
} else {
for orig in &node.inputs {
if let Some(replacement) = replaced_wires.get(orig) {
patch.shunt_outside(model, *orig, *replacement)?;
}
}
}
}
for output in model.output_outlets()? {
if let Some(replacement) = replaced_wires.get(output) {
unsafe {
patch.shunt_outside_unchecked(*output, *replacement)?;
}
}
}
let mut interface_change = tvec!();
for (ix, input) in model.input_outlets()?.iter().enumerate() {
if let Some(change) = changed_wires.get(input) {
interface_change.push((InOut::In(ix), change.clone()));
}
}
for (ix, output) in model.output_outlets()?.iter().enumerate() {
if let Some(change) = changed_wires.get(output) {
interface_change.push((InOut::Out(ix), change.clone()));
}
}
debug_assert!(
patch.model.nodes.iter().map(|n| &n.name).collect::<std::collections::HashSet<_>>().len()
== patch.model.nodes.len()
);
Ok(Some((patch, interface_change)))
}sourcepub fn same_as(&self, other: &Node<F, NodeOp>) -> bool
pub fn same_as(&self, other: &Node<F, NodeOp>) -> bool
Check that this node produce the same outputs as other.
Examples found in repository?
src/optim/push_split_down.rs (line 23)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn next(&mut self, _session: &mut OptimizerSession, model: &TypedModel) -> TractResult<Option<TypedModelPatch>> {
let mut patch = TypedModelPatch::default();
for node in model.eval_order()? {
for output in &model.node(node).outputs {
for (a, b) in output.successors.iter().tuple_combinations() {
if patch.obliterate.contains(&b.node) {
continue;
}
let a = model.node(a.node);
let b = model.node(b.node);
if a.same_as(b) {
for slot in 0..b.outputs.len() {
let tap = patch.tap_model(model, OutletId::new(a.id, slot))?;
patch.shunt_outside(model, OutletId::new(b.id, slot), tap)?;
patch.obliterate(b.id)?;
}
}
}
}
}
Ok(Some(patch).filter(|p| !p.is_empty()))
}Trait Implementations§
Auto Trait Implementations§
impl<F, O> RefUnwindSafe for Node<F, O>where
F: RefUnwindSafe,
O: RefUnwindSafe,
impl<F, O> Send for Node<F, O>where
O: Send,
impl<F, O> Sync for Node<F, O>where
O: Sync,
impl<F, O> Unpin for Node<F, O>where
F: Unpin,
O: Unpin,
impl<F, O> UnwindSafe for Node<F, O>where
F: UnwindSafe + RefUnwindSafe,
O: UnwindSafe,
Blanket Implementations§
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.