concepts.dsl.tensor_value.TensorValue#

class TensorValue[source]#

Bases: ValueBase

A value object with an internal torch.Tensor storage.

Methods

clone([clone_tensor])

expand(batch_variables, batch_sizes)

expand_as(other)

fast_index(arguments[, wrap])

format([content, short])

from_optimistic_value(value)

from_optimistic_value_int(identifier, dtype)

from_scalar(value[, dtype])

from_tensor(value[, dtype, batch_variables])

from_tensorized_pyobj(value[, dtype, ...])

from_values(*args[, dtype])

get_variable_size(variable_name_or_index)

has_optimistic_value()

index(key)

init_tensor_optimistic_values()

is_simple_quantizable()

is_single_optimistic_value()

item()

make_empty(dtype[, batch_variables, ...])

rename_batch_variables(new_variables[, clone])

set_index(key, value)

set_print_options([max_tensor_size])

short_str()

simple_quantize()

single_elem()

to_masked_tensor_storage([clone_tensor])

Attributes

FALSE

STR_MAX_TENSOR_SIZE

TRUE

is_scalar

is_single_elem

is_tensorized_pyobj

is_torch_tensor

nr_variables

total_batch_dims

dtype

The data type of the Value object.

batch_variables

The list of batch variable names.

batch_dims

Additional batch dimensions at the beginning.

quantized

Whether the values in self.tensor is quantized.

tensor

The internal tensor storage.

tensor_mask

A mask of the tensor, indicating which elements are valid.

tensor_optimistic_values

The optimistic values for the tensor.

tensor_quantized_values

The quantized values for the tensor.

__getitem__(item)[source]#
Return type:

TensorValue

__init__(dtype, batch_variables, tensor, batch_dims=0, quantized=None, *, _check_tensor=False, _mask_certified_flag=True)[source]#

Instantiate a Value object for storing intermediate computation results.

The tensor is assumed to have the following layout: tensor[B1, B2, B3, ..., V1, V2, V3, ..., D1, D2, D3, ...].

  • The first batch_dims dimensions are “batch”.

  • The next len(batch_variables) dimensions are “variables”.

  • The next dtype.ndim() dimensions are data dimensions (e.g., images, vectors).

  • A special case is that dtype.ndim() can be zero (scalar).

Parameters:
  • dtype (TensorValueTypeBase | PyObjValueType) – The data type of the Value object.

  • batch_variables (Iterable[str] | int) – A sequence of variables that are processed in “batch.” This typically corresponds to “quantified variables.” It can also be a single integer, indicating the number of batched variables.

  • tensor (Tensor | TensorizedPyObjValues | MaskedTensorStorage) – The actual tensor

  • batch_dims (int) – The additional batch dimensions at the beginning. Defaults to 0.

  • _check_tensor (bool) – internal flag, whether to run the tensor shape/type sanity check.

  • _mask_certified_flag (bool) – internal flag, indicating whether self.tensor_mask is guaranteed to be the correct mask. This flag will be marked false when we do expand_as.

  • quantized (bool | None) –

__new__(**kwargs)#
clone(clone_tensor=True)[source]#
expand(batch_variables, batch_sizes)[source]#
Parameters:
Return type:

TensorValue

expand_as(other)[source]#
Parameters:

other (TensorValue) –

Return type:

TensorValue

fast_index(arguments, wrap=True)[source]#
Parameters:
Return type:

Tensor | Any | OptimisticValue

format(content=True, short=False)[source]#
Parameters:
Return type:

str

classmethod from_optimistic_value(value)[source]#
Parameters:

value (OptimisticValue) –

Return type:

TensorValue

classmethod from_optimistic_value_int(identifier, dtype)[source]#
Parameters:
Return type:

TensorValue

classmethod from_scalar(value, dtype=None)[source]#
Parameters:
Return type:

TensorValue

classmethod from_tensor(value, dtype=None, batch_variables=None)[source]#
Parameters:
Return type:

TensorValue

classmethod from_tensorized_pyobj(value, dtype=None, batch_variables=None)[source]#
Parameters:
Return type:

TensorValue

classmethod from_values(*args, dtype=None)[source]#
Parameters:
Return type:

TensorValue

get_variable_size(variable_name_or_index)[source]#
Parameters:

variable_name_or_index (str | int) –

Return type:

int

has_optimistic_value()[source]#
Return type:

bool

index(key)[source]#
Parameters:

key (int | slice | List[int] | Tensor | Tuple[int | slice | List[int] | Tensor, ...] | None) –

Return type:

TensorValue

init_tensor_optimistic_values()[source]#
is_simple_quantizable()[source]#
Return type:

bool

is_single_optimistic_value()[source]#
Return type:

bool

item()[source]#
Return type:

Tensor | Any | OptimisticValue

classmethod make_empty(dtype, batch_variables=tuple(), batch_sizes=tuple(), batch_dims=0)[source]#
Parameters:
rename_batch_variables(new_variables, clone=False)[source]#
Parameters:
set_index(key, value)[source]#
Parameters:
Return type:

TensorValue

classmethod set_print_options(max_tensor_size=STR_MAX_TENSOR_SIZE)[source]#
short_str()[source]#
simple_quantize()[source]#
Return type:

TensorValue

single_elem()[source]#
Return type:

Tensor | Any | OptimisticValue

to_masked_tensor_storage(clone_tensor=False)[source]#
Parameters:

clone_tensor (bool) –

Return type:

MaskedTensorStorage

FALSE: TensorValue = Value[bool, axes=[], tdtype=torch.int64, tdshape=(), quantized]{tensor(0)}#
STR_MAX_TENSOR_SIZE = 100#
TRUE: TensorValue = Value[bool, axes=[], tdtype=torch.int64, tdshape=(), quantized]{tensor(1)}#
batch_dims: int#

Additional batch dimensions at the beginning.

batch_variables: Tuple[str, ...]#

The list of batch variable names.

dtype: TensorValueTypeBase | PyObjValueType#

The data type of the Value object.

property is_scalar#
property is_single_elem#
property is_tensorized_pyobj#
property is_torch_tensor#
property nr_variables#
quantized: bool#

Whether the values in self.tensor is quantized.

tensor: Tensor | TensorizedPyObjValues#

The internal tensor storage.

tensor_mask: Tensor | None#

A mask of the tensor, indicating which elements are valid.

tensor_optimistic_values: Tensor | None#

The optimistic values for the tensor. 0 for non-optimistic values.

tensor_quantized_values: Tensor | None#

The quantized values for the tensor. -1 for non-quantized values.

property total_batch_dims#