tract-cli 0.23.0

Tiny, no-nonsense, self contained, TensorFlow and ONNX inference
1
2
3
4
5
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
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
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
use fs::File;
use std::fmt::{Debug, Display};
use std::path::PathBuf;
use std::str::FromStr;

use crate::TractResult;
use crate::{Model, Parameters};
use fs_err as fs;
use ndarray_npy::NpzWriter;
use nu_ansi_term::Color::*;
use tract_core::ops::cnn::conv::Im2Col;
use tract_core::ops::matmul::pack::OptMatMulPack;
use tract_core::tract_data::itertools::izip;
use tract_hir::internal::*;
use tract_libcli::tensor::{RunTensors, get_or_make_inputs};
use tract_nnef::tensors::write_tensor;
#[cfg(feature = "pulse")]
use tract_pulse::internal::*;

/// Add a tensor entry into a npz file.
fn npz_add_tensor(npz: &mut NpzWriter<File>, name: String, tensor: &Tensor) -> TractResult<()> {
    match tensor.datum_type() {
        DatumType::F16 => {
            npz.add_array(name, &tensor.cast_to::<f32>()?.to_plain_array_view::<f32>()?)?
        }
        DatumType::Bool => npz.add_array(name, &tensor.to_plain_array_view::<bool>()?)?,
        DatumType::U8 => npz.add_array(name, &tensor.to_plain_array_view::<u8>()?)?,
        DatumType::U16 => npz.add_array(name, &tensor.to_plain_array_view::<u16>()?)?,
        DatumType::U32 => npz.add_array(name, &tensor.to_plain_array_view::<u32>()?)?,
        DatumType::U64 => npz.add_array(name, &tensor.to_plain_array_view::<u64>()?)?,
        DatumType::I8 => npz.add_array(name, &tensor.to_plain_array_view::<i8>()?)?,
        DatumType::I16 => npz.add_array(name, &tensor.to_plain_array_view::<i16>()?)?,
        DatumType::I32 => npz.add_array(name, &tensor.to_plain_array_view::<i32>()?)?,
        DatumType::I64 => npz.add_array(name, &tensor.to_plain_array_view::<i64>()?)?,
        DatumType::F32 => npz.add_array(name, &tensor.to_plain_array_view::<f32>()?)?,
        DatumType::F64 => npz.add_array(name, &tensor.to_plain_array_view::<f64>()?)?,
        DatumType::QI8(_) => npz.add_array(name, &tensor.to_plain_array_view::<i8>()?)?,
        DatumType::QU8(_) => npz.add_array(name, &tensor.to_plain_array_view::<u8>()?)?,
        DatumType::QI32(_) => npz.add_array(name, &tensor.to_plain_array_view::<i32>()?)?,
        _ => warn!("Not writing {name}, {tensor:?}, unsupported type"),
    }

    Ok(())
}

pub fn handle(
    params: &Parameters,
    matches: &clap::ArgMatches,
    sub_matches: &clap::ArgMatches,
) -> TractResult<()> {
    let dump = sub_matches.get_flag("dump");
    // let outputs = dispatch_model!(&(Arc::clone(params.tract_model) as _), |m| run_regular(
    //     m,
    //     params,
    //     matches,
    //     sub_matches
    // ))?;
    let outputs = run_regular(&params.tract_model, params, matches, sub_matches)?;

    if dump {
        for (ix, output) in outputs.iter().enumerate() {
            for (turn, output) in output.iter().enumerate() {
                println!("output #{}, turn #{}\n{}\n", ix, turn, output.dump(true)?);
            }
        }
    }

    if let Some(file_path) = sub_matches.get_one::<String>("save-outputs-nnef") {
        fs::create_dir_all(file_path).with_context(|| format!("Creating {file_path} directory"))?;
        for (ix, outputs) in outputs.iter().enumerate() {
            let name = params
                .tract_model
                .outlet_label(params.tract_model.output_outlets()[ix])
                .map(|name| format!("{name}.dat"))
                .unwrap_or_else(|| format!("output_{ix}.dat"));

            if outputs.len() == 1 {
                let mut f = fs::File::create(PathBuf::from_str(file_path)?.join(&name))?;
                write_tensor(&mut f, &outputs[0])?;
            } else {
                for (turn, output) in outputs.iter().enumerate() {
                    let name = format!("turn_{turn}/{name}");
                    let mut f = fs::File::open(PathBuf::from_str(file_path)?.join(name))?;
                    write_tensor(&mut f, output)?;
                }
            }
        }
    }

    if let Some(file_path) = sub_matches.get_one::<String>("save-outputs-npz") {
        let file = fs::File::create(file_path).with_context(|| format!("Creating {file_path}"))?;
        let mut npz = ndarray_npy::NpzWriter::new_compressed(file);

        for (ix, outputs) in outputs.iter().enumerate() {
            let name = params
                .tract_model
                .outlet_label(params.tract_model.output_outlets()[ix])
                .map(|name| name.to_string())
                .unwrap_or_else(|| format!("output_{ix}"));
            if outputs.len() == 1 {
                npz_add_tensor(&mut npz, name, &outputs[0])?;
            } else {
                for (turn, output) in outputs.iter().enumerate() {
                    let name = format!("turn_{turn}/{name}");
                    npz_add_tensor(&mut npz, name, output)?;
                }
            }
        }
    }

    if let Some(count) = sub_matches.get_one::<String>("assert-output-count") {
        let count = count.parse::<usize>()?;
        if count != outputs.len() {
            bail!(
                "Wrong number of outputs, command line expected {}, found {:?}",
                count,
                outputs.len()
            );
        }
    }

    if params.assertions.assert_outputs {
        crate::utils::check_outputs(&outputs, params)?;
    }

    if let Some(facts) = &params.assertions.assert_output_facts {
        let outputs: Vec<InferenceFact> =
            outputs.iter().map(|t| t[0].datum_type().fact(t[0].shape()).into()).collect();
        crate::utils::check_inferred(&outputs, facts)?;
    }

    if let Some(asserts) = &params.assertions.assert_op_count {
        for (name, expected) in asserts {
            let count = crate::utils::count_op(&*params.tract_model, name)?;
            if count != *expected {
                bail!("Wrong number of {} operators: expected {}, got {}", name, expected, count);
            }
        }
    }

    if let Some(patterns) = &params.assertions.assert_op_only {
        crate::utils::check_op_only(&*params.tract_model, patterns)?;
    }

    Ok(())
}

fn run_regular_t<F, O>(
    state: &mut SimpleState<F, O>,
    inputs: RunTensors,
    steps: bool,
    check_f16_overflow: bool,
    assert_sane_floats: bool,
    mut npz: Option<NpzWriter<File>>,
) -> TractResult<TVec<Vec<TValue>>>
where
    F: Fact + Clone + 'static,
    O: Debug + Display + AsRef<dyn Op> + AsMut<dyn Op> + Clone + 'static,
{
    let mut results = tvec!(vec!(); state.model().outputs.len());
    let multiturn = inputs.sources.len() > 1;

    // Build output→input cache mapping for unfolded KV cache threading.
    // Output labeled "{name}_concat" feeds input named "{name}".
    let cache_output_to_input: Vec<(usize, usize)> = if multiturn {
        let model = state.model();
        let mut mapping = Vec::new();
        for (out_ix, out_outlet) in model.outputs.iter().enumerate() {
            let out_label = model.outlet_label(*out_outlet).unwrap_or("");
            if let Some(base) = out_label.strip_suffix("_concat") {
                for (in_ix, in_outlet) in model.inputs.iter().enumerate() {
                    let in_name = &model.nodes[in_outlet.node].name;
                    if in_name == base {
                        mapping.push((out_ix, in_ix));
                        break;
                    }
                }
            }
        }
        mapping
    } else {
        Vec::new()
    };

    // Pre-compute the streaming-symbol binding so we can re-apply it every
    // turn (run_plan_with_eval calls reset_turn at the end, wiping
    // resolved_symbols).  Without this, PulsePad's `end_input.eval(...)`
    // hits the `usize::MAX` fallback and the tail-pad fill silently never
    // triggers — pulse output then disagrees with batch on the boundary.
    let pulse_sym_binding: Option<(Symbol, i64)> =
        if let (Some(sym_name), Some(stream_len), Some(first_input)) = (
            state
                .model()
                .properties
                .get("pulse.streaming_symbol")
                .and_then(|t| t.to_plain_array_view::<String>().ok())
                .and_then(|a| a.first().cloned()),
            inputs.streaming_input_len,
            inputs.sources.first().and_then(|t| t.first()),
        ) {
            let input_axes = state
                .model()
                .properties
                .get("pulse.input_axes")
                .context("Expect pulse.input_axes when pulse.streaming_symbol is set")?
                .cast_to::<i64>()?;
            let input_axis = input_axes.try_as_plain()?.as_slice::<i64>()?[0] as usize;
            let pulse_value = first_input.shape()[input_axis];
            // Linear case: stream.dim = pulse_value · symbol.  For non-linear
            // dims (e.g. `4·s + 1`) this would be wrong, but blockified models
            // arrive here with a chunk-aligned linear dim by construction.
            if pulse_value > 0 && stream_len % pulse_value == 0 {
                let sym = state.model().symbols.sym(&sym_name);
                Some((sym, (stream_len / pulse_value) as i64))
            } else {
                None
            }
        } else {
            None
        };

    let mut sources = inputs.sources;
    for turn in 0..sources.len() {
        let inputs = std::mem::replace(&mut sources[turn], TVec::new());
        if let Some((sym, val)) = &pulse_sym_binding {
            state.turn_state.resolved_symbols.set(sym, *val);
        }
        let turn_results =
            state.run_plan_with_eval(inputs, |session_state, state, node, input| {
                if steps {
                    for (ix, i) in input.iter().enumerate() {
                        eprintln!(
                            "{} {}{}{:?}",
                            White.bold().paint(node.to_string()),
                            ix,
                            Blue.bold().paint("<< "),
                            i
                        );
                    }
                }
                let r = tract_core::plan::eval(session_state, state, node, input)?;

                if steps || npz.is_some() || check_f16_overflow || assert_sane_floats {
                    let clarified_r = crate::utils::clarify_tvalues(&r)?;
                    if steps {
                        for (ix, o) in clarified_r.iter().enumerate() {
                            eprintln!(
                                "{} {}{}{:?}",
                                White.bold().paint(node.to_string()),
                                ix,
                                Yellow.bold().paint(">> "),
                                o
                            );
                        }
                    }
                    if let Some(npz) = npz.as_mut() {
                        for (ix, t) in clarified_r.iter().enumerate() {
                            let mut name = if ix == 0 {
                                node.name.to_string()
                            } else {
                                format!("{}:{}", node.name, ix)
                            };
                            if multiturn {
                                name = format!("turn_{turn}/{name}");
                            }
                            npz_add_tensor(npz, name, t)?;
                        }
                    }
                    if check_f16_overflow {
                        for (ix, o) in clarified_r.iter().enumerate() {
                            if let Ok(plain) = o.try_as_plain() {
                                if let Ok(f32s) = plain.as_slice::<f32>() {
                                    if f32s.iter().any(|f| f.abs() > f16::MAX.to_f32()) {
                                        warn!("{node}, output {ix} overflows f16");
                                    }
                                }
                            }
                        }
                    }
                    if assert_sane_floats {
                        for (ix, o) in clarified_r.iter().enumerate() {
                            if node.op_is::<Im2Col>() || node.op_is::<OptMatMulPack>() {
                                continue;
                            }
                            if let Ok(plain) = o.try_as_plain() {
                                if let Ok(floats) = plain.as_slice::<f32>() {
                                    if let Some(pos) = floats.iter().position(|f| !f.is_finite()) {
                                        eprintln!("{floats:?}");
                                        bail!("Found {} in output {} of {}", floats[pos], ix, node);
                                    }
                                } else if let Ok(floats) = plain.as_slice::<f16>() {
                                    if let Some(pos) = floats.iter().position(|f| !f.is_finite()) {
                                        eprintln!("{floats:?}");
                                        bail!("Found {} in output {} of {}", floats[pos], ix, node);
                                    }
                                }
                            }
                        }
                    }
                }

                Ok(r)
            })?;
        // Thread cache outputs into next turn's inputs
        if turn + 1 < sources.len() && !cache_output_to_input.is_empty() {
            for &(out_ix, in_ix) in &cache_output_to_input {
                sources[turn + 1][in_ix] = turn_results[out_ix].clone();
            }
        }

        izip!(&mut results, turn_results).for_each(|(r, tr)| r.push(tr));
    }
    Ok(results)
}

fn run_regular(
    tract: &Arc<dyn Model>,
    params: &Parameters,
    _matches: &clap::ArgMatches,
    sub_matches: &clap::ArgMatches,
) -> TractResult<TVec<Vec<TValue>>> {
    let run_params = crate::tensor::run_params_from_subcommand(params, sub_matches)?;

    let steps = sub_matches.get_flag("steps");
    let check_f16_overflow = sub_matches.get_flag("check-f16-overflow");
    let assert_sane_floats = sub_matches.get_flag("assert-sane-floats");
    let npz = if let Some(npz) = sub_matches.get_one::<String>("save-steps") {
        let npz = fs::File::create(npz).with_context(|| format!("Creating {npz}"))?;
        Some(ndarray_npy::NpzWriter::new_compressed(npz))
    } else {
        None
    };

    let inputs = get_or_make_inputs(tract, &run_params)?;
    if let Some(runnable) = &params.runnable {
        if let Some(plan) = runnable.typed_plan() {
            let mut state = plan.spawn()?;
            let results = run_regular_t(
                &mut state,
                inputs,
                steps,
                check_f16_overflow,
                assert_sane_floats,
                npz,
            )?;
            Ok(results)
        } else {
            todo!("Run handler for abstract runtime/runnable");
        }
    } else {
        dispatch_model!(tract, |m| {
            let plan_options = crate::plan_options::plan_options_from_subcommand(sub_matches)?;
            let plan = SimplePlan::new_with_options(m, &plan_options)?;
            let mut state = plan.spawn()?;

            let results = run_regular_t(
                &mut state,
                inputs,
                steps,
                check_f16_overflow,
                assert_sane_floats,
                npz,
            )?;
            Ok(results)
        })
    }
}

/*
#[cfg(feature = "pulse")]
fn run_pulse_t(model: &PulsedModel, params: &Parameters) -> TractResult<TVec<TValue>> {
let input_fact = model.input_fact(0)?;
let output_fact = model.output_fact(0)?;

let output_pulse = output_fact.pulse();
//    println!("output_fact: {:?}", output_fact);
let axis = input_fact.axis;
let name = model.node_name(model.input_outlets()?[0].node);
let input: &Tensor = &params.tensors_values.by_name(name).unwrap().values.as_ref().unwrap()[0];
//    println!("input_shape: {:?}", input.shape());
let input_dim = input.shape()[axis];
//    println!("output_fact: {:?}", output_fact);
let output_dim = output_fact
.dim
.eval(&SymbolValues::default().with(stream_symbol(), input_dim as i64))
.to_usize()?;
let mut output_shape = output_fact.shape.to_vec();
output_shape[output_fact.axis] =
(output_dim as usize + output_fact.delay + 4 * output_fact.pulse()).to_dim();
let output_shape: TVec<usize> = output_shape.iter().map(|d| d.to_usize().unwrap()).collect();
let plan = SimplePlan::new(model)?;
let mut state = ::tract_core::plan::SimpleState::new(&plan)?;
//    println!("output_shape: {:?}", output_shape);
let pulse = input_fact.pulse();
let mut result = tract_ndarray::ArrayD::<f32>::default(&*output_shape);
let input = input.to_array_view::<f32>()?;
for ix in 0..input_dim.divceil(pulse) {
let chunk =
input.slice_axis(tract_ndarray::Axis(axis), (ix * pulse..(ix + 1) * pulse).into());
let input = if chunk.shape()[input_fact.axis] < pulse {
let mut chunk_shape = chunk.shape().to_vec();
chunk_shape[input_fact.axis] = pulse;
let mut padded_chunk = tract_ndarray::ArrayD::<f32>::default(chunk_shape);
padded_chunk
.slice_axis_mut(
tract_ndarray::Axis(input_fact.axis),
(..chunk.shape()[input_fact.axis]).into(),
)
.assign(&chunk);
padded_chunk
} else {
chunk.to_owned()
};
let outputs = state.run(tvec!(input.into_tensor().into()))?;
let result_chunk = outputs[0].to_array_view::<f32>()?;
result
.slice_axis_mut(
tract_ndarray::Axis(output_fact.axis),
((output_pulse * ix)..(output_pulse * (ix + 1))).into(),
)
.assign(&result_chunk);
}
result.slice_axis_inplace(tract_ndarray::Axis(output_fact.axis), (output_fact.delay..).into());
result
.slice_axis_inplace(tract_ndarray::Axis(output_fact.axis), (..output_dim as usize).into());
Ok(tvec!(result.into_tvalue()))
}
*/