pub struct TensorsValues(pub Vec<TensorValues>);

Tuple Fields§

§0: Vec<TensorValues>

Implementations§

Examples found in repository?
src/tensor.rs (line 289)
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
pub fn retrieve_or_make_inputs(
    tract: &dyn Model,
    params: &RunParams,
) -> TractResult<Vec<TVec<TValue>>> {
    let mut tmp: TVec<Vec<TValue>> = tvec![];
    for (ix, input) in tract.input_outlets().iter().enumerate() {
        let name = tract.node_name(input.node);
        let fact = tract.outlet_typedfact(*input)?;
        if let Some(mut value) = params.tensors_values.by_name(name).and_then(|t| t.values.clone())
        {
            if !value[0].datum_type().is_quantized()
                && fact.datum_type.is_quantized()
                && value[0].datum_type() == fact.datum_type.unquantized()
            {
                value = value
                    .iter()
                    .map(|v| {
                        let mut v = v.clone().into_tensor();
                        unsafe { v.set_datum_type(fact.datum_type) };
                        v.into()
                    })
                    .collect();
            }
            if TypedFact::from(&*value[0]).compatible_with(&fact) {
                info!("Using fixed input for input called {} ({} turn(s))", name, value.len());
                tmp.push(value.iter().map(|t| t.clone().into_tensor().into()).collect())
            } else if fact.datum_type == f16::datum_type()
                && value[0].datum_type() == f32::datum_type()
                && params.allow_float_casts
            {
                tmp.push(
                    value.iter().map(|t| t.cast_to::<f16>().unwrap().into_owned().into()).collect(),
                )
            } else if value.len() == 1 && tract.properties().contains_key("pulse.delay") {
                let value = &value[0];
                let input_pulse_axis = tract
                    .properties()
                    .get("pulse.input_axes")
                    .context("Expect pulse.input_axes property")?
                    .cast_to::<i64>()?
                    .as_slice::<i64>()?[ix] as usize;
                let input_pulse = fact.shape.get(input_pulse_axis).unwrap().to_usize().unwrap();
                let input_len = value.shape()[input_pulse_axis];

                // how many pulses do we need to push full result out ?
                // guess by looking at len and delay of the first output
                let output_pulse_axis = tract
                    .properties()
                    .get("pulse.output_axes")
                    .context("Expect pulse.output_axes property")?
                    .cast_to::<i64>()?
                    .as_slice::<i64>()?[0] as usize;
                let output_fact = tract.outlet_typedfact(tract.output_outlets()[0])?;
                let output_pulse =
                    output_fact.shape.get(output_pulse_axis).unwrap().to_usize().unwrap();
                let output_len = input_len * output_pulse / input_pulse;
                let output_delay = tract.properties()["pulse.delay"].as_slice::<i64>()?[0] as usize;
                let last_frame = output_len + output_delay;
                let needed_pulses = last_frame.divceil(output_pulse);
                let mut values = vec![];
                for ix in 0..needed_pulses {
                    let mut t =
                        Tensor::zero_dt(fact.datum_type, fact.shape.as_concrete().unwrap())?;
                    let start = ix * input_pulse;
                    let end = (start + input_pulse).min(input_len);
                    if end > start {
                        t.assign_slice(0..end - start, value, start..end, input_pulse_axis)?;
                    }
                    values.push(t.into());
                }
                info!(
                    "Generated {} pulses of shape {:?} for input {}.",
                    needed_pulses, fact.shape, ix
                );
                tmp.push(values);
            } else {
                bail!("For input {}, can not reconcile model input fact {:?} with provided input {:?}", name, fact, value[0]);
            };
        } else if params.allow_random_input {
            let fact = tract.outlet_typedfact(*input)?;
            warn_once(format!("Using random input for input called {:?}: {:?}", name, fact));
            let tv = params
                .tensors_values
                .by_name(name)
                .or_else(|| params.tensors_values.by_input_ix(ix));
            tmp.push(vec![crate::tensor::tensor_for_fact(&fact, None, tv)?.into()]);
        } else {
            bail!("Unmatched tensor {}. Fix the input or use \"--allow-random-input\" if this was intended", name);
        }
    }
    Ok((0..tmp[0].len()).map(|turn| tmp.iter().map(|t| t[turn].clone()).collect()).collect())
}
Examples found in repository?
src/tensor.rs (line 22)
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
    pub fn by_name_mut_with_default(&mut self, name: &str) -> &mut TensorValues {
        if self.by_name_mut(name).is_none() {
            self.add(TensorValues { name: Some(name.to_string()), ..TensorValues::default() });
        }
        self.by_name_mut(name).unwrap()
    }

    pub fn by_input_ix(&self, ix: usize) -> Option<&TensorValues> {
        self.0.iter().find(|t| t.input_index == Some(ix))
    }
    pub fn by_input_ix_mut(&mut self, ix: usize) -> Option<&mut TensorValues> {
        self.0.iter_mut().find(|t| t.input_index == Some(ix))
    }
    pub fn by_input_ix_mut_with_default(&mut self, ix: usize) -> &mut TensorValues {
        if self.by_input_ix_mut(ix).is_none() {
            self.add(TensorValues { input_index: Some(ix), ..TensorValues::default() });
        }
        self.by_input_ix_mut(ix).unwrap()
    }

    pub fn add(&mut self, other: TensorValues) {
        let mut tensor = other.input_index.and_then(|ix| self.by_input_ix_mut(ix));

        if tensor.is_none() {
            tensor = other.name.as_deref().and_then(|ix| self.by_name_mut(ix))
        }

        if let Some(tensor) = tensor {
            if tensor.fact.is_none() {
                tensor.fact = other.fact;
            }
            if tensor.values.is_none() {
                tensor.values = other.values;
            }
        } else {
            self.0.push(other.clone());
        };
    }
Examples found in repository?
src/tensor.rs (line 365)
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
pub fn retrieve_or_make_inputs(
    tract: &dyn Model,
    params: &RunParams,
) -> TractResult<Vec<TVec<TValue>>> {
    let mut tmp: TVec<Vec<TValue>> = tvec![];
    for (ix, input) in tract.input_outlets().iter().enumerate() {
        let name = tract.node_name(input.node);
        let fact = tract.outlet_typedfact(*input)?;
        if let Some(mut value) = params.tensors_values.by_name(name).and_then(|t| t.values.clone())
        {
            if !value[0].datum_type().is_quantized()
                && fact.datum_type.is_quantized()
                && value[0].datum_type() == fact.datum_type.unquantized()
            {
                value = value
                    .iter()
                    .map(|v| {
                        let mut v = v.clone().into_tensor();
                        unsafe { v.set_datum_type(fact.datum_type) };
                        v.into()
                    })
                    .collect();
            }
            if TypedFact::from(&*value[0]).compatible_with(&fact) {
                info!("Using fixed input for input called {} ({} turn(s))", name, value.len());
                tmp.push(value.iter().map(|t| t.clone().into_tensor().into()).collect())
            } else if fact.datum_type == f16::datum_type()
                && value[0].datum_type() == f32::datum_type()
                && params.allow_float_casts
            {
                tmp.push(
                    value.iter().map(|t| t.cast_to::<f16>().unwrap().into_owned().into()).collect(),
                )
            } else if value.len() == 1 && tract.properties().contains_key("pulse.delay") {
                let value = &value[0];
                let input_pulse_axis = tract
                    .properties()
                    .get("pulse.input_axes")
                    .context("Expect pulse.input_axes property")?
                    .cast_to::<i64>()?
                    .as_slice::<i64>()?[ix] as usize;
                let input_pulse = fact.shape.get(input_pulse_axis).unwrap().to_usize().unwrap();
                let input_len = value.shape()[input_pulse_axis];

                // how many pulses do we need to push full result out ?
                // guess by looking at len and delay of the first output
                let output_pulse_axis = tract
                    .properties()
                    .get("pulse.output_axes")
                    .context("Expect pulse.output_axes property")?
                    .cast_to::<i64>()?
                    .as_slice::<i64>()?[0] as usize;
                let output_fact = tract.outlet_typedfact(tract.output_outlets()[0])?;
                let output_pulse =
                    output_fact.shape.get(output_pulse_axis).unwrap().to_usize().unwrap();
                let output_len = input_len * output_pulse / input_pulse;
                let output_delay = tract.properties()["pulse.delay"].as_slice::<i64>()?[0] as usize;
                let last_frame = output_len + output_delay;
                let needed_pulses = last_frame.divceil(output_pulse);
                let mut values = vec![];
                for ix in 0..needed_pulses {
                    let mut t =
                        Tensor::zero_dt(fact.datum_type, fact.shape.as_concrete().unwrap())?;
                    let start = ix * input_pulse;
                    let end = (start + input_pulse).min(input_len);
                    if end > start {
                        t.assign_slice(0..end - start, value, start..end, input_pulse_axis)?;
                    }
                    values.push(t.into());
                }
                info!(
                    "Generated {} pulses of shape {:?} for input {}.",
                    needed_pulses, fact.shape, ix
                );
                tmp.push(values);
            } else {
                bail!("For input {}, can not reconcile model input fact {:?} with provided input {:?}", name, fact, value[0]);
            };
        } else if params.allow_random_input {
            let fact = tract.outlet_typedfact(*input)?;
            warn_once(format!("Using random input for input called {:?}: {:?}", name, fact));
            let tv = params
                .tensors_values
                .by_name(name)
                .or_else(|| params.tensors_values.by_input_ix(ix));
            tmp.push(vec![crate::tensor::tensor_for_fact(&fact, None, tv)?.into()]);
        } else {
            bail!("Unmatched tensor {}. Fix the input or use \"--allow-random-input\" if this was intended", name);
        }
    }
    Ok((0..tmp[0].len()).map(|turn| tmp.iter().map(|t| t[turn].clone()).collect()).collect())
}
Examples found in repository?
src/tensor.rs (line 35)
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
    pub fn by_input_ix_mut_with_default(&mut self, ix: usize) -> &mut TensorValues {
        if self.by_input_ix_mut(ix).is_none() {
            self.add(TensorValues { input_index: Some(ix), ..TensorValues::default() });
        }
        self.by_input_ix_mut(ix).unwrap()
    }

    pub fn add(&mut self, other: TensorValues) {
        let mut tensor = other.input_index.and_then(|ix| self.by_input_ix_mut(ix));

        if tensor.is_none() {
            tensor = other.name.as_deref().and_then(|ix| self.by_name_mut(ix))
        }

        if let Some(tensor) = tensor {
            if tensor.fact.is_none() {
                tensor.fact = other.fact;
            }
            if tensor.values.is_none() {
                tensor.values = other.values;
            }
        } else {
            self.0.push(other.clone());
        };
    }
Examples found in repository?
src/tensor.rs (line 23)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    pub fn by_name_mut_with_default(&mut self, name: &str) -> &mut TensorValues {
        if self.by_name_mut(name).is_none() {
            self.add(TensorValues { name: Some(name.to_string()), ..TensorValues::default() });
        }
        self.by_name_mut(name).unwrap()
    }

    pub fn by_input_ix(&self, ix: usize) -> Option<&TensorValues> {
        self.0.iter().find(|t| t.input_index == Some(ix))
    }
    pub fn by_input_ix_mut(&mut self, ix: usize) -> Option<&mut TensorValues> {
        self.0.iter_mut().find(|t| t.input_index == Some(ix))
    }
    pub fn by_input_ix_mut_with_default(&mut self, ix: usize) -> &mut TensorValues {
        if self.by_input_ix_mut(ix).is_none() {
            self.add(TensorValues { input_index: Some(ix), ..TensorValues::default() });
        }
        self.by_input_ix_mut(ix).unwrap()
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
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.
Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.