Struct tract_libcli::draw::DrawingState
source · pub struct DrawingState {
pub current_color: Style,
pub latest_node_color: Style,
pub wires: Vec<Wire>,
}Fields§
§current_color: Style§latest_node_color: Style§wires: Vec<Wire>Implementations§
source§impl DrawingState
impl DrawingState
sourcepub fn draw_node_vprefix(
&mut self,
model: &dyn Model,
node: usize,
_opts: &DisplayParams
) -> TractResult<Vec<String>>
pub fn draw_node_vprefix(
&mut self,
model: &dyn Model,
node: usize,
_opts: &DisplayParams
) -> TractResult<Vec<String>>
Examples found in repository?
src/terminal.rs (line 67)
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 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 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
fn render_prefixed(
model: &dyn Model,
prefix: &str,
scope: &[(usize, String)],
annotations: &Annotations,
options: &DisplayParams,
) -> TractResult<()> {
let mut drawing_state =
if options.should_draw() { Some(DrawingState::default()) } else { None };
let node_ids = if options.natural_order {
(0..model.nodes_len()).collect()
} else {
model.eval_order()?
};
for node in node_ids {
if options.filter(model, scope, node)? {
render_node_prefixed(
model,
prefix,
scope,
node,
drawing_state.as_mut(),
annotations,
options,
)?
} else if let Some(ref mut ds) = drawing_state {
let _prefix = ds.draw_node_vprefix(model, node, options)?;
let _body = ds.draw_node_body(model, node, options)?;
let _suffix = ds.draw_node_vsuffix(model, node, options)?;
}
}
Ok(())
}
fn render_node_prefixed(
model: &dyn Model,
prefix: &str,
scope: &[(usize, String)],
node_id: usize,
mut drawing_state: Option<&mut DrawingState>,
annotations: &Annotations,
options: &DisplayParams,
) -> TractResult<()> {
let qid = NodeQId(scope.into(), node_id);
let tags = annotations.tags.get(&qid).cloned().unwrap_or_default();
let name_color = tags.style.unwrap_or_else(|| White.into());
let node_name = model.node_name(node_id);
let node_op_name = model.node_op_name(node_id);
let profile_column_pad = format!("{:>1$}", "", options.profile as usize * 20);
let cost_column_pad = format!("{:>1$}", "", options.cost as usize * 25);
let flops_column_pad = format!("{:>1$}", "", (options.profile && options.cost) as usize * 20);
if let Some(ref mut ds) = &mut drawing_state {
for l in ds.draw_node_vprefix(model, node_id, options)? {
println!(
"{}{}{}{}{} ",
cost_column_pad, profile_column_pad, flops_column_pad, prefix, l
);
}
}
// profile column
let mut profile_column = tags.profile.map(|measure| {
let profile_summary = annotations.profile_summary.as_ref().unwrap();
let use_micros = profile_summary.sum < Duration::from_millis(1);
let ratio = measure.as_secs_f64() / profile_summary.sum.as_secs_f64();
let ratio_for_color = measure.as_secs_f64() / profile_summary.max.as_secs_f64();
let color = colorous::RED_YELLOW_GREEN.eval_continuous(1.0 - ratio_for_color);
let color = ansi_term::Color::RGB(color.r, color.g, color.b);
let label = format!(
"{:7.3} {}s/i {} ",
measure.as_secs_f64() * if use_micros { 1e6 } else { 1e3 },
if use_micros { "µ" } else { "m" },
color.bold().paint(format!("{:>4.1}%", ratio * 100.0))
);
std::iter::once(label)
});
// cost column
let mut cost_column = if options.cost {
Some(
tags.cost
.iter()
.map(|c| {
let key = format!("{:?}", c.0);
let value = render_tdim(&c.1);
let value_visible_len = c.1.to_string().len();
let padding = 24usize.saturating_sub(value_visible_len + key.len());
key + &*std::iter::repeat(' ').take(padding).join("") + &value + " "
})
.peekable(),
)
} else {
None
};
// flops column
let mut flops_column = if options.profile && options.cost {
let timing: f64 = tags.profile.as_ref().unwrap().as_secs_f64();
let flops_column_pad = flops_column_pad.clone();
let it = tags.cost.iter().map(move |c| {
if c.0.is_compute() {
let flops = c.1.to_usize().unwrap_or(0) as f64 / timing;
let unpadded = if flops > 1e9 {
format!("{:.3} GF/s", flops / 1e9)
} else if flops > 1e6 {
format!("{:.3} MF/s", flops / 1e6)
} else if flops > 1e3 {
format!("{:.3} kF/s", flops / 1e3)
} else {
format!("{:.3} F/s", flops)
};
format!("{:>1$} ", unpadded, 19)
} else {
flops_column_pad.clone()
}
});
Some(it)
} else {
None
};
// drawing column
let mut drawing_lines: Box<dyn Iterator<Item = String>> =
if let Some(ds) = drawing_state.as_mut() {
let body = ds.draw_node_body(model, node_id, options)?;
let suffix = ds.draw_node_vsuffix(model, node_id, options)?;
let filler = ds.draw_node_vfiller(model, node_id)?;
Box::new(body.into_iter().chain(suffix.into_iter()).chain(std::iter::repeat(filler)))
} else {
Box::new(std::iter::repeat(cost_column_pad.clone()))
};
macro_rules! prefix {
() => {
let cost = cost_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| cost_column_pad.to_string()))
.unwrap_or("".to_string());
let profile = profile_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| profile_column_pad.to_string()))
.unwrap_or("".to_string());
let flops = flops_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| flops_column_pad.to_string()))
.unwrap_or("".to_string());
print!("{}{}{}{}{} ", profile, cost, flops, prefix, drawing_lines.next().unwrap(),)
};
}
prefix!();
println!(
"{} {} {}",
White.bold().paint(format!("{}", node_id)),
(if node_name == "UnimplementedOp" { Red.bold() } else { Blue.bold() }).paint(node_op_name),
name_color.italic().paint(node_name)
);
for label in tags.labels.iter() {
prefix!();
println!(" * {}", label);
}
if let Io::Long = options.io {
for (ix, i) in model.node_inputs(node_id).iter().enumerate() {
let star = if ix == 0 { '*' } else { ' ' };
prefix!();
println!(
" {} input fact #{}: {} {}",
star,
ix,
White.bold().paint(format!("{:?}", i)),
model.outlet_fact_format(*i),
);
}
for slot in 0..model.node_output_count(node_id) {
let star = if slot == 0 { '*' } else { ' ' };
let outlet = OutletId::new(node_id, slot);
let mut model_io = vec![];
for (ix, _) in model.input_outlets().iter().enumerate().filter(|(_, o)| **o == outlet) {
model_io.push(Cyan.bold().paint(format!("MODEL INPUT #{}", ix)).to_string());
}
if let Some(t) = &tags.model_input {
model_io.push(t.to_string());
}
for (ix, _) in model.output_outlets().iter().enumerate().filter(|(_, o)| **o == outlet)
{
model_io.push(Yellow.bold().paint(format!("MODEL OUTPUT #{}", ix)).to_string());
}
if let Some(t) = &tags.model_output {
model_io.push(t.to_string());
}
let successors = model.outlet_successors(outlet);
prefix!();
let mut axes =
tags.outlet_axes.get(slot).map(|s| s.join(",")).unwrap_or_else(|| "".to_string());
if !axes.is_empty() {
axes.push(' ')
}
println!(
" {} output fact #{}: {}{} {} {} {}",
star,
slot,
Green.bold().italic().paint(axes),
model.outlet_fact_format(outlet),
White.bold().paint(successors.iter().map(|s| format!("{:?}", s)).join(" ")),
model_io.join(", "),
Blue.bold().italic().paint(
tags.outlet_labels
.get(slot)
.map(|s| s.join(","))
.unwrap_or_else(|| "".to_string())
)
);
if options.outlet_labels {
if let Some(label) = model.outlet_label(OutletId::new(node_id, slot)) {
prefix!();
println!(" {} ", White.italic().paint(label));
}
}
}
}
if options.info {
for info in model.node_op(node_id).info()? {
prefix!();
println!(" * {}", info);
}
}
if options.invariants {
if let Some(typed) = model.downcast_ref::<TypedModel>() {
let node = typed.node(node_id);
let (inputs, outputs) = typed.node_facts(node.id)?;
let invariants = node.op().as_typed().unwrap().invariants(&inputs, &outputs)?;
prefix!();
println!(" * {:?}", invariants);
}
}
if options.debug_op {
prefix!();
println!(" * {:?}", model.node_op(node_id));
}
for section in tags.sections {
if section.is_empty() {
continue;
}
prefix!();
println!(" * {}", section[0]);
for s in §ion[1..] {
prefix!();
println!(" {}", s);
}
}
for (label, sub) in model.nested_models(node_id) {
let prefix = drawing_lines.next().unwrap();
let mut scope: TVec<_> = scope.into();
scope.push((node_id, label.to_string()));
render_prefixed(sub, &format!("{} [{}] ", prefix, label), &scope, annotations, options)?
}
if let Io::Short = options.io {
let same = !model.node_inputs(node_id).is_empty()
&& model.node_output_count(node_id) == 1
&& model.outlet_fact_format(node_id.into())
== model.outlet_fact_format(model.node_inputs(node_id)[0]);
if !same || model.output_outlets().iter().any(|o| o.node == node_id) {
let style = drawing_state
.map(|s| s.wires.last().and_then(|w| w.color).unwrap_or(s.latest_node_color))
.unwrap_or_else(|| White.into());
for ix in 0..model.node_output_count(node_id) {
prefix!();
println!(
" {}{}{} {}",
style.paint(box_drawing::heavy::HORIZONTAL),
style.paint(box_drawing::heavy::HORIZONTAL),
style.paint(box_drawing::heavy::HORIZONTAL),
model.outlet_fact_format((node_id, ix).into())
);
}
}
}
while cost_column.as_mut().map(|cost| cost.peek().is_some()).unwrap_or(false) {
prefix!();
println!();
}
Ok(())
}sourcepub fn draw_node_body(
&mut self,
model: &dyn Model,
node: usize,
opts: &DisplayParams
) -> TractResult<Vec<String>>
pub fn draw_node_body(
&mut self,
model: &dyn Model,
node: usize,
opts: &DisplayParams
) -> TractResult<Vec<String>>
Examples found in repository?
src/terminal.rs (line 68)
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 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 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
fn render_prefixed(
model: &dyn Model,
prefix: &str,
scope: &[(usize, String)],
annotations: &Annotations,
options: &DisplayParams,
) -> TractResult<()> {
let mut drawing_state =
if options.should_draw() { Some(DrawingState::default()) } else { None };
let node_ids = if options.natural_order {
(0..model.nodes_len()).collect()
} else {
model.eval_order()?
};
for node in node_ids {
if options.filter(model, scope, node)? {
render_node_prefixed(
model,
prefix,
scope,
node,
drawing_state.as_mut(),
annotations,
options,
)?
} else if let Some(ref mut ds) = drawing_state {
let _prefix = ds.draw_node_vprefix(model, node, options)?;
let _body = ds.draw_node_body(model, node, options)?;
let _suffix = ds.draw_node_vsuffix(model, node, options)?;
}
}
Ok(())
}
fn render_node_prefixed(
model: &dyn Model,
prefix: &str,
scope: &[(usize, String)],
node_id: usize,
mut drawing_state: Option<&mut DrawingState>,
annotations: &Annotations,
options: &DisplayParams,
) -> TractResult<()> {
let qid = NodeQId(scope.into(), node_id);
let tags = annotations.tags.get(&qid).cloned().unwrap_or_default();
let name_color = tags.style.unwrap_or_else(|| White.into());
let node_name = model.node_name(node_id);
let node_op_name = model.node_op_name(node_id);
let profile_column_pad = format!("{:>1$}", "", options.profile as usize * 20);
let cost_column_pad = format!("{:>1$}", "", options.cost as usize * 25);
let flops_column_pad = format!("{:>1$}", "", (options.profile && options.cost) as usize * 20);
if let Some(ref mut ds) = &mut drawing_state {
for l in ds.draw_node_vprefix(model, node_id, options)? {
println!(
"{}{}{}{}{} ",
cost_column_pad, profile_column_pad, flops_column_pad, prefix, l
);
}
}
// profile column
let mut profile_column = tags.profile.map(|measure| {
let profile_summary = annotations.profile_summary.as_ref().unwrap();
let use_micros = profile_summary.sum < Duration::from_millis(1);
let ratio = measure.as_secs_f64() / profile_summary.sum.as_secs_f64();
let ratio_for_color = measure.as_secs_f64() / profile_summary.max.as_secs_f64();
let color = colorous::RED_YELLOW_GREEN.eval_continuous(1.0 - ratio_for_color);
let color = ansi_term::Color::RGB(color.r, color.g, color.b);
let label = format!(
"{:7.3} {}s/i {} ",
measure.as_secs_f64() * if use_micros { 1e6 } else { 1e3 },
if use_micros { "µ" } else { "m" },
color.bold().paint(format!("{:>4.1}%", ratio * 100.0))
);
std::iter::once(label)
});
// cost column
let mut cost_column = if options.cost {
Some(
tags.cost
.iter()
.map(|c| {
let key = format!("{:?}", c.0);
let value = render_tdim(&c.1);
let value_visible_len = c.1.to_string().len();
let padding = 24usize.saturating_sub(value_visible_len + key.len());
key + &*std::iter::repeat(' ').take(padding).join("") + &value + " "
})
.peekable(),
)
} else {
None
};
// flops column
let mut flops_column = if options.profile && options.cost {
let timing: f64 = tags.profile.as_ref().unwrap().as_secs_f64();
let flops_column_pad = flops_column_pad.clone();
let it = tags.cost.iter().map(move |c| {
if c.0.is_compute() {
let flops = c.1.to_usize().unwrap_or(0) as f64 / timing;
let unpadded = if flops > 1e9 {
format!("{:.3} GF/s", flops / 1e9)
} else if flops > 1e6 {
format!("{:.3} MF/s", flops / 1e6)
} else if flops > 1e3 {
format!("{:.3} kF/s", flops / 1e3)
} else {
format!("{:.3} F/s", flops)
};
format!("{:>1$} ", unpadded, 19)
} else {
flops_column_pad.clone()
}
});
Some(it)
} else {
None
};
// drawing column
let mut drawing_lines: Box<dyn Iterator<Item = String>> =
if let Some(ds) = drawing_state.as_mut() {
let body = ds.draw_node_body(model, node_id, options)?;
let suffix = ds.draw_node_vsuffix(model, node_id, options)?;
let filler = ds.draw_node_vfiller(model, node_id)?;
Box::new(body.into_iter().chain(suffix.into_iter()).chain(std::iter::repeat(filler)))
} else {
Box::new(std::iter::repeat(cost_column_pad.clone()))
};
macro_rules! prefix {
() => {
let cost = cost_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| cost_column_pad.to_string()))
.unwrap_or("".to_string());
let profile = profile_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| profile_column_pad.to_string()))
.unwrap_or("".to_string());
let flops = flops_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| flops_column_pad.to_string()))
.unwrap_or("".to_string());
print!("{}{}{}{}{} ", profile, cost, flops, prefix, drawing_lines.next().unwrap(),)
};
}
prefix!();
println!(
"{} {} {}",
White.bold().paint(format!("{}", node_id)),
(if node_name == "UnimplementedOp" { Red.bold() } else { Blue.bold() }).paint(node_op_name),
name_color.italic().paint(node_name)
);
for label in tags.labels.iter() {
prefix!();
println!(" * {}", label);
}
if let Io::Long = options.io {
for (ix, i) in model.node_inputs(node_id).iter().enumerate() {
let star = if ix == 0 { '*' } else { ' ' };
prefix!();
println!(
" {} input fact #{}: {} {}",
star,
ix,
White.bold().paint(format!("{:?}", i)),
model.outlet_fact_format(*i),
);
}
for slot in 0..model.node_output_count(node_id) {
let star = if slot == 0 { '*' } else { ' ' };
let outlet = OutletId::new(node_id, slot);
let mut model_io = vec![];
for (ix, _) in model.input_outlets().iter().enumerate().filter(|(_, o)| **o == outlet) {
model_io.push(Cyan.bold().paint(format!("MODEL INPUT #{}", ix)).to_string());
}
if let Some(t) = &tags.model_input {
model_io.push(t.to_string());
}
for (ix, _) in model.output_outlets().iter().enumerate().filter(|(_, o)| **o == outlet)
{
model_io.push(Yellow.bold().paint(format!("MODEL OUTPUT #{}", ix)).to_string());
}
if let Some(t) = &tags.model_output {
model_io.push(t.to_string());
}
let successors = model.outlet_successors(outlet);
prefix!();
let mut axes =
tags.outlet_axes.get(slot).map(|s| s.join(",")).unwrap_or_else(|| "".to_string());
if !axes.is_empty() {
axes.push(' ')
}
println!(
" {} output fact #{}: {}{} {} {} {}",
star,
slot,
Green.bold().italic().paint(axes),
model.outlet_fact_format(outlet),
White.bold().paint(successors.iter().map(|s| format!("{:?}", s)).join(" ")),
model_io.join(", "),
Blue.bold().italic().paint(
tags.outlet_labels
.get(slot)
.map(|s| s.join(","))
.unwrap_or_else(|| "".to_string())
)
);
if options.outlet_labels {
if let Some(label) = model.outlet_label(OutletId::new(node_id, slot)) {
prefix!();
println!(" {} ", White.italic().paint(label));
}
}
}
}
if options.info {
for info in model.node_op(node_id).info()? {
prefix!();
println!(" * {}", info);
}
}
if options.invariants {
if let Some(typed) = model.downcast_ref::<TypedModel>() {
let node = typed.node(node_id);
let (inputs, outputs) = typed.node_facts(node.id)?;
let invariants = node.op().as_typed().unwrap().invariants(&inputs, &outputs)?;
prefix!();
println!(" * {:?}", invariants);
}
}
if options.debug_op {
prefix!();
println!(" * {:?}", model.node_op(node_id));
}
for section in tags.sections {
if section.is_empty() {
continue;
}
prefix!();
println!(" * {}", section[0]);
for s in §ion[1..] {
prefix!();
println!(" {}", s);
}
}
for (label, sub) in model.nested_models(node_id) {
let prefix = drawing_lines.next().unwrap();
let mut scope: TVec<_> = scope.into();
scope.push((node_id, label.to_string()));
render_prefixed(sub, &format!("{} [{}] ", prefix, label), &scope, annotations, options)?
}
if let Io::Short = options.io {
let same = !model.node_inputs(node_id).is_empty()
&& model.node_output_count(node_id) == 1
&& model.outlet_fact_format(node_id.into())
== model.outlet_fact_format(model.node_inputs(node_id)[0]);
if !same || model.output_outlets().iter().any(|o| o.node == node_id) {
let style = drawing_state
.map(|s| s.wires.last().and_then(|w| w.color).unwrap_or(s.latest_node_color))
.unwrap_or_else(|| White.into());
for ix in 0..model.node_output_count(node_id) {
prefix!();
println!(
" {}{}{} {}",
style.paint(box_drawing::heavy::HORIZONTAL),
style.paint(box_drawing::heavy::HORIZONTAL),
style.paint(box_drawing::heavy::HORIZONTAL),
model.outlet_fact_format((node_id, ix).into())
);
}
}
}
while cost_column.as_mut().map(|cost| cost.peek().is_some()).unwrap_or(false) {
prefix!();
println!();
}
Ok(())
}sourcepub fn draw_node_vfiller(
&self,
model: &dyn Model,
node: usize
) -> TractResult<String>
pub fn draw_node_vfiller(
&self,
model: &dyn Model,
node: usize
) -> TractResult<String>
Examples found in repository?
src/terminal.rs (line 168)
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 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 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
fn render_node_prefixed(
model: &dyn Model,
prefix: &str,
scope: &[(usize, String)],
node_id: usize,
mut drawing_state: Option<&mut DrawingState>,
annotations: &Annotations,
options: &DisplayParams,
) -> TractResult<()> {
let qid = NodeQId(scope.into(), node_id);
let tags = annotations.tags.get(&qid).cloned().unwrap_or_default();
let name_color = tags.style.unwrap_or_else(|| White.into());
let node_name = model.node_name(node_id);
let node_op_name = model.node_op_name(node_id);
let profile_column_pad = format!("{:>1$}", "", options.profile as usize * 20);
let cost_column_pad = format!("{:>1$}", "", options.cost as usize * 25);
let flops_column_pad = format!("{:>1$}", "", (options.profile && options.cost) as usize * 20);
if let Some(ref mut ds) = &mut drawing_state {
for l in ds.draw_node_vprefix(model, node_id, options)? {
println!(
"{}{}{}{}{} ",
cost_column_pad, profile_column_pad, flops_column_pad, prefix, l
);
}
}
// profile column
let mut profile_column = tags.profile.map(|measure| {
let profile_summary = annotations.profile_summary.as_ref().unwrap();
let use_micros = profile_summary.sum < Duration::from_millis(1);
let ratio = measure.as_secs_f64() / profile_summary.sum.as_secs_f64();
let ratio_for_color = measure.as_secs_f64() / profile_summary.max.as_secs_f64();
let color = colorous::RED_YELLOW_GREEN.eval_continuous(1.0 - ratio_for_color);
let color = ansi_term::Color::RGB(color.r, color.g, color.b);
let label = format!(
"{:7.3} {}s/i {} ",
measure.as_secs_f64() * if use_micros { 1e6 } else { 1e3 },
if use_micros { "µ" } else { "m" },
color.bold().paint(format!("{:>4.1}%", ratio * 100.0))
);
std::iter::once(label)
});
// cost column
let mut cost_column = if options.cost {
Some(
tags.cost
.iter()
.map(|c| {
let key = format!("{:?}", c.0);
let value = render_tdim(&c.1);
let value_visible_len = c.1.to_string().len();
let padding = 24usize.saturating_sub(value_visible_len + key.len());
key + &*std::iter::repeat(' ').take(padding).join("") + &value + " "
})
.peekable(),
)
} else {
None
};
// flops column
let mut flops_column = if options.profile && options.cost {
let timing: f64 = tags.profile.as_ref().unwrap().as_secs_f64();
let flops_column_pad = flops_column_pad.clone();
let it = tags.cost.iter().map(move |c| {
if c.0.is_compute() {
let flops = c.1.to_usize().unwrap_or(0) as f64 / timing;
let unpadded = if flops > 1e9 {
format!("{:.3} GF/s", flops / 1e9)
} else if flops > 1e6 {
format!("{:.3} MF/s", flops / 1e6)
} else if flops > 1e3 {
format!("{:.3} kF/s", flops / 1e3)
} else {
format!("{:.3} F/s", flops)
};
format!("{:>1$} ", unpadded, 19)
} else {
flops_column_pad.clone()
}
});
Some(it)
} else {
None
};
// drawing column
let mut drawing_lines: Box<dyn Iterator<Item = String>> =
if let Some(ds) = drawing_state.as_mut() {
let body = ds.draw_node_body(model, node_id, options)?;
let suffix = ds.draw_node_vsuffix(model, node_id, options)?;
let filler = ds.draw_node_vfiller(model, node_id)?;
Box::new(body.into_iter().chain(suffix.into_iter()).chain(std::iter::repeat(filler)))
} else {
Box::new(std::iter::repeat(cost_column_pad.clone()))
};
macro_rules! prefix {
() => {
let cost = cost_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| cost_column_pad.to_string()))
.unwrap_or("".to_string());
let profile = profile_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| profile_column_pad.to_string()))
.unwrap_or("".to_string());
let flops = flops_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| flops_column_pad.to_string()))
.unwrap_or("".to_string());
print!("{}{}{}{}{} ", profile, cost, flops, prefix, drawing_lines.next().unwrap(),)
};
}
prefix!();
println!(
"{} {} {}",
White.bold().paint(format!("{}", node_id)),
(if node_name == "UnimplementedOp" { Red.bold() } else { Blue.bold() }).paint(node_op_name),
name_color.italic().paint(node_name)
);
for label in tags.labels.iter() {
prefix!();
println!(" * {}", label);
}
if let Io::Long = options.io {
for (ix, i) in model.node_inputs(node_id).iter().enumerate() {
let star = if ix == 0 { '*' } else { ' ' };
prefix!();
println!(
" {} input fact #{}: {} {}",
star,
ix,
White.bold().paint(format!("{:?}", i)),
model.outlet_fact_format(*i),
);
}
for slot in 0..model.node_output_count(node_id) {
let star = if slot == 0 { '*' } else { ' ' };
let outlet = OutletId::new(node_id, slot);
let mut model_io = vec![];
for (ix, _) in model.input_outlets().iter().enumerate().filter(|(_, o)| **o == outlet) {
model_io.push(Cyan.bold().paint(format!("MODEL INPUT #{}", ix)).to_string());
}
if let Some(t) = &tags.model_input {
model_io.push(t.to_string());
}
for (ix, _) in model.output_outlets().iter().enumerate().filter(|(_, o)| **o == outlet)
{
model_io.push(Yellow.bold().paint(format!("MODEL OUTPUT #{}", ix)).to_string());
}
if let Some(t) = &tags.model_output {
model_io.push(t.to_string());
}
let successors = model.outlet_successors(outlet);
prefix!();
let mut axes =
tags.outlet_axes.get(slot).map(|s| s.join(",")).unwrap_or_else(|| "".to_string());
if !axes.is_empty() {
axes.push(' ')
}
println!(
" {} output fact #{}: {}{} {} {} {}",
star,
slot,
Green.bold().italic().paint(axes),
model.outlet_fact_format(outlet),
White.bold().paint(successors.iter().map(|s| format!("{:?}", s)).join(" ")),
model_io.join(", "),
Blue.bold().italic().paint(
tags.outlet_labels
.get(slot)
.map(|s| s.join(","))
.unwrap_or_else(|| "".to_string())
)
);
if options.outlet_labels {
if let Some(label) = model.outlet_label(OutletId::new(node_id, slot)) {
prefix!();
println!(" {} ", White.italic().paint(label));
}
}
}
}
if options.info {
for info in model.node_op(node_id).info()? {
prefix!();
println!(" * {}", info);
}
}
if options.invariants {
if let Some(typed) = model.downcast_ref::<TypedModel>() {
let node = typed.node(node_id);
let (inputs, outputs) = typed.node_facts(node.id)?;
let invariants = node.op().as_typed().unwrap().invariants(&inputs, &outputs)?;
prefix!();
println!(" * {:?}", invariants);
}
}
if options.debug_op {
prefix!();
println!(" * {:?}", model.node_op(node_id));
}
for section in tags.sections {
if section.is_empty() {
continue;
}
prefix!();
println!(" * {}", section[0]);
for s in §ion[1..] {
prefix!();
println!(" {}", s);
}
}
for (label, sub) in model.nested_models(node_id) {
let prefix = drawing_lines.next().unwrap();
let mut scope: TVec<_> = scope.into();
scope.push((node_id, label.to_string()));
render_prefixed(sub, &format!("{} [{}] ", prefix, label), &scope, annotations, options)?
}
if let Io::Short = options.io {
let same = !model.node_inputs(node_id).is_empty()
&& model.node_output_count(node_id) == 1
&& model.outlet_fact_format(node_id.into())
== model.outlet_fact_format(model.node_inputs(node_id)[0]);
if !same || model.output_outlets().iter().any(|o| o.node == node_id) {
let style = drawing_state
.map(|s| s.wires.last().and_then(|w| w.color).unwrap_or(s.latest_node_color))
.unwrap_or_else(|| White.into());
for ix in 0..model.node_output_count(node_id) {
prefix!();
println!(
" {}{}{} {}",
style.paint(box_drawing::heavy::HORIZONTAL),
style.paint(box_drawing::heavy::HORIZONTAL),
style.paint(box_drawing::heavy::HORIZONTAL),
model.outlet_fact_format((node_id, ix).into())
);
}
}
}
while cost_column.as_mut().map(|cost| cost.peek().is_some()).unwrap_or(false) {
prefix!();
println!();
}
Ok(())
}sourcepub fn draw_node_vsuffix(
&mut self,
model: &dyn Model,
node: usize,
opts: &DisplayParams
) -> TractResult<Vec<String>>
pub fn draw_node_vsuffix(
&mut self,
model: &dyn Model,
node: usize,
opts: &DisplayParams
) -> TractResult<Vec<String>>
Examples found in repository?
src/terminal.rs (line 69)
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 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 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
fn render_prefixed(
model: &dyn Model,
prefix: &str,
scope: &[(usize, String)],
annotations: &Annotations,
options: &DisplayParams,
) -> TractResult<()> {
let mut drawing_state =
if options.should_draw() { Some(DrawingState::default()) } else { None };
let node_ids = if options.natural_order {
(0..model.nodes_len()).collect()
} else {
model.eval_order()?
};
for node in node_ids {
if options.filter(model, scope, node)? {
render_node_prefixed(
model,
prefix,
scope,
node,
drawing_state.as_mut(),
annotations,
options,
)?
} else if let Some(ref mut ds) = drawing_state {
let _prefix = ds.draw_node_vprefix(model, node, options)?;
let _body = ds.draw_node_body(model, node, options)?;
let _suffix = ds.draw_node_vsuffix(model, node, options)?;
}
}
Ok(())
}
fn render_node_prefixed(
model: &dyn Model,
prefix: &str,
scope: &[(usize, String)],
node_id: usize,
mut drawing_state: Option<&mut DrawingState>,
annotations: &Annotations,
options: &DisplayParams,
) -> TractResult<()> {
let qid = NodeQId(scope.into(), node_id);
let tags = annotations.tags.get(&qid).cloned().unwrap_or_default();
let name_color = tags.style.unwrap_or_else(|| White.into());
let node_name = model.node_name(node_id);
let node_op_name = model.node_op_name(node_id);
let profile_column_pad = format!("{:>1$}", "", options.profile as usize * 20);
let cost_column_pad = format!("{:>1$}", "", options.cost as usize * 25);
let flops_column_pad = format!("{:>1$}", "", (options.profile && options.cost) as usize * 20);
if let Some(ref mut ds) = &mut drawing_state {
for l in ds.draw_node_vprefix(model, node_id, options)? {
println!(
"{}{}{}{}{} ",
cost_column_pad, profile_column_pad, flops_column_pad, prefix, l
);
}
}
// profile column
let mut profile_column = tags.profile.map(|measure| {
let profile_summary = annotations.profile_summary.as_ref().unwrap();
let use_micros = profile_summary.sum < Duration::from_millis(1);
let ratio = measure.as_secs_f64() / profile_summary.sum.as_secs_f64();
let ratio_for_color = measure.as_secs_f64() / profile_summary.max.as_secs_f64();
let color = colorous::RED_YELLOW_GREEN.eval_continuous(1.0 - ratio_for_color);
let color = ansi_term::Color::RGB(color.r, color.g, color.b);
let label = format!(
"{:7.3} {}s/i {} ",
measure.as_secs_f64() * if use_micros { 1e6 } else { 1e3 },
if use_micros { "µ" } else { "m" },
color.bold().paint(format!("{:>4.1}%", ratio * 100.0))
);
std::iter::once(label)
});
// cost column
let mut cost_column = if options.cost {
Some(
tags.cost
.iter()
.map(|c| {
let key = format!("{:?}", c.0);
let value = render_tdim(&c.1);
let value_visible_len = c.1.to_string().len();
let padding = 24usize.saturating_sub(value_visible_len + key.len());
key + &*std::iter::repeat(' ').take(padding).join("") + &value + " "
})
.peekable(),
)
} else {
None
};
// flops column
let mut flops_column = if options.profile && options.cost {
let timing: f64 = tags.profile.as_ref().unwrap().as_secs_f64();
let flops_column_pad = flops_column_pad.clone();
let it = tags.cost.iter().map(move |c| {
if c.0.is_compute() {
let flops = c.1.to_usize().unwrap_or(0) as f64 / timing;
let unpadded = if flops > 1e9 {
format!("{:.3} GF/s", flops / 1e9)
} else if flops > 1e6 {
format!("{:.3} MF/s", flops / 1e6)
} else if flops > 1e3 {
format!("{:.3} kF/s", flops / 1e3)
} else {
format!("{:.3} F/s", flops)
};
format!("{:>1$} ", unpadded, 19)
} else {
flops_column_pad.clone()
}
});
Some(it)
} else {
None
};
// drawing column
let mut drawing_lines: Box<dyn Iterator<Item = String>> =
if let Some(ds) = drawing_state.as_mut() {
let body = ds.draw_node_body(model, node_id, options)?;
let suffix = ds.draw_node_vsuffix(model, node_id, options)?;
let filler = ds.draw_node_vfiller(model, node_id)?;
Box::new(body.into_iter().chain(suffix.into_iter()).chain(std::iter::repeat(filler)))
} else {
Box::new(std::iter::repeat(cost_column_pad.clone()))
};
macro_rules! prefix {
() => {
let cost = cost_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| cost_column_pad.to_string()))
.unwrap_or("".to_string());
let profile = profile_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| profile_column_pad.to_string()))
.unwrap_or("".to_string());
let flops = flops_column
.as_mut()
.map(|it| it.next().unwrap_or_else(|| flops_column_pad.to_string()))
.unwrap_or("".to_string());
print!("{}{}{}{}{} ", profile, cost, flops, prefix, drawing_lines.next().unwrap(),)
};
}
prefix!();
println!(
"{} {} {}",
White.bold().paint(format!("{}", node_id)),
(if node_name == "UnimplementedOp" { Red.bold() } else { Blue.bold() }).paint(node_op_name),
name_color.italic().paint(node_name)
);
for label in tags.labels.iter() {
prefix!();
println!(" * {}", label);
}
if let Io::Long = options.io {
for (ix, i) in model.node_inputs(node_id).iter().enumerate() {
let star = if ix == 0 { '*' } else { ' ' };
prefix!();
println!(
" {} input fact #{}: {} {}",
star,
ix,
White.bold().paint(format!("{:?}", i)),
model.outlet_fact_format(*i),
);
}
for slot in 0..model.node_output_count(node_id) {
let star = if slot == 0 { '*' } else { ' ' };
let outlet = OutletId::new(node_id, slot);
let mut model_io = vec![];
for (ix, _) in model.input_outlets().iter().enumerate().filter(|(_, o)| **o == outlet) {
model_io.push(Cyan.bold().paint(format!("MODEL INPUT #{}", ix)).to_string());
}
if let Some(t) = &tags.model_input {
model_io.push(t.to_string());
}
for (ix, _) in model.output_outlets().iter().enumerate().filter(|(_, o)| **o == outlet)
{
model_io.push(Yellow.bold().paint(format!("MODEL OUTPUT #{}", ix)).to_string());
}
if let Some(t) = &tags.model_output {
model_io.push(t.to_string());
}
let successors = model.outlet_successors(outlet);
prefix!();
let mut axes =
tags.outlet_axes.get(slot).map(|s| s.join(",")).unwrap_or_else(|| "".to_string());
if !axes.is_empty() {
axes.push(' ')
}
println!(
" {} output fact #{}: {}{} {} {} {}",
star,
slot,
Green.bold().italic().paint(axes),
model.outlet_fact_format(outlet),
White.bold().paint(successors.iter().map(|s| format!("{:?}", s)).join(" ")),
model_io.join(", "),
Blue.bold().italic().paint(
tags.outlet_labels
.get(slot)
.map(|s| s.join(","))
.unwrap_or_else(|| "".to_string())
)
);
if options.outlet_labels {
if let Some(label) = model.outlet_label(OutletId::new(node_id, slot)) {
prefix!();
println!(" {} ", White.italic().paint(label));
}
}
}
}
if options.info {
for info in model.node_op(node_id).info()? {
prefix!();
println!(" * {}", info);
}
}
if options.invariants {
if let Some(typed) = model.downcast_ref::<TypedModel>() {
let node = typed.node(node_id);
let (inputs, outputs) = typed.node_facts(node.id)?;
let invariants = node.op().as_typed().unwrap().invariants(&inputs, &outputs)?;
prefix!();
println!(" * {:?}", invariants);
}
}
if options.debug_op {
prefix!();
println!(" * {:?}", model.node_op(node_id));
}
for section in tags.sections {
if section.is_empty() {
continue;
}
prefix!();
println!(" * {}", section[0]);
for s in §ion[1..] {
prefix!();
println!(" {}", s);
}
}
for (label, sub) in model.nested_models(node_id) {
let prefix = drawing_lines.next().unwrap();
let mut scope: TVec<_> = scope.into();
scope.push((node_id, label.to_string()));
render_prefixed(sub, &format!("{} [{}] ", prefix, label), &scope, annotations, options)?
}
if let Io::Short = options.io {
let same = !model.node_inputs(node_id).is_empty()
&& model.node_output_count(node_id) == 1
&& model.outlet_fact_format(node_id.into())
== model.outlet_fact_format(model.node_inputs(node_id)[0]);
if !same || model.output_outlets().iter().any(|o| o.node == node_id) {
let style = drawing_state
.map(|s| s.wires.last().and_then(|w| w.color).unwrap_or(s.latest_node_color))
.unwrap_or_else(|| White.into());
for ix in 0..model.node_output_count(node_id) {
prefix!();
println!(
" {}{}{} {}",
style.paint(box_drawing::heavy::HORIZONTAL),
style.paint(box_drawing::heavy::HORIZONTAL),
style.paint(box_drawing::heavy::HORIZONTAL),
model.outlet_fact_format((node_id, ix).into())
);
}
}
}
while cost_column.as_mut().map(|cost| cost.peek().is_some()).unwrap_or(false) {
prefix!();
println!();
}
Ok(())
}Trait Implementations§
source§impl Clone for DrawingState
impl Clone for DrawingState
source§fn clone(&self) -> DrawingState
fn clone(&self) -> DrawingState
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl Default for DrawingState
impl Default for DrawingState
source§fn default() -> DrawingState
fn default() -> DrawingState
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl RefUnwindSafe for DrawingState
impl Send for DrawingState
impl Sync for DrawingState
impl Unpin for DrawingState
impl UnwindSafe for DrawingState
Blanket Implementations§
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§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.§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.§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.§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.