Struct tract_libcli::tensor::TensorsValues
source · pub struct TensorsValues(pub Vec<TensorValues>);Tuple Fields§
§0: Vec<TensorValues>Implementations§
source§impl TensorsValues
impl TensorsValues
sourcepub fn by_name(&self, name: &str) -> Option<&TensorValues>
pub fn by_name(&self, name: &str) -> Option<&TensorValues>
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())
}sourcepub fn by_name_mut(&mut self, name: &str) -> Option<&mut TensorValues>
pub fn by_name_mut(&mut self, name: &str) -> Option<&mut TensorValues>
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());
};
}pub fn by_name_mut_with_default(&mut self, name: &str) -> &mut TensorValues
sourcepub fn by_input_ix(&self, ix: usize) -> Option<&TensorValues>
pub fn by_input_ix(&self, ix: usize) -> Option<&TensorValues>
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())
}sourcepub fn by_input_ix_mut(&mut self, ix: usize) -> Option<&mut TensorValues>
pub fn by_input_ix_mut(&mut self, ix: usize) -> Option<&mut TensorValues>
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());
};
}pub fn by_input_ix_mut_with_default(&mut self, ix: usize) -> &mut TensorValues
sourcepub fn add(&mut self, other: TensorValues)
pub fn add(&mut self, other: TensorValues)
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§
source§impl Clone for TensorsValues
impl Clone for TensorsValues
source§fn clone(&self) -> TensorsValues
fn clone(&self) -> TensorsValues
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 Debug for TensorsValues
impl Debug for TensorsValues
source§impl Default for TensorsValues
impl Default for TensorsValues
source§fn default() -> TensorsValues
fn default() -> TensorsValues
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl RefUnwindSafe for TensorsValues
impl !Send for TensorsValues
impl !Sync for TensorsValues
impl Unpin for TensorsValues
impl UnwindSafe for TensorsValues
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.