concepts.dsl.executors.value_quantizers.ValueQuantizer#

class ValueQuantizer[source]#

Bases: object

Methods

checkpoint()

A context manager that can be used to checkpoint all the quantized values.

quantize(typename, value)

Quantize a single value.

quantize_dict_list(continuous_values)

Quantize a dictionary of lists of values.

quantize_state(state[, includes, excludes])

Quantize a TensorState object.

quantize_tensor(dtype, tensor)

Quantize a PyTorch tensor.

quantize_value(value)

Quantize a single TensorValue object.

unquantize(typename, value)

The lowest-level API to unquantize a single value.

unquantize_tensor(dtype, tensor)

Unquantize a PyTorch tensor.

unquantize_value(value)

Unquantize a single TensorValue object.

Attributes

values

the value dictionary is a mapping from typename to a list of values.

__init__(executor)[source]#

Initialize a value quantizer.

Parameters:

executor (TensorValueExecutorBase) – the executor for the domain to use. We need the executor to access the underlying domain and the corresponding hash / equality functions.

__new__(**kwargs)#
checkpoint()[source]#

A context manager that can be used to checkpoint all the quantized values. This is useful when you performs a series of executions and want to restore the quantized values to save memory.

quantize(typename, value)[source]#

Quantize a single value. This API is used to quantize a single value, and it is the lowest level API in this class. Most of the time, you should use other higher-level APIs such as quantize_tensor() and quantize_value() instead.

Parameters:
  • typename (str) – the typename of the value.

  • value (Tensor | TensorValue) – the value to quantize.

Returns:

the quantized value, as a single integer.

Return type:

int

quantize_dict_list(continuous_values)[source]#

Quantize a dictionary of lists of values. The return is a dictionary that maps from the same keys to a list of quantized values. Note that the return type is a dictionary of lists of concepts.dsl.tensor_value.TensorValue, not a dictionary of lists of integers.

Parameters:

continuous_values (Mapping[str, Sequence[Tensor | TensorValue]]) – the dictionary of lists of values to quantize.

Returns:

the quantized dictionary of lists of values.

Return type:

Mapping[str, Sequence[TensorValue]]

quantize_state(state, includes=None, excludes=None)[source]#

Quantize a TensorState object. Users can specify which variables to quantize by using the includes and excludes arguments. Meanwhile, this function will read the function definition in the domain to determine whether the feature is a state variable. If the feature is not a state variable, it will not be quantized.

Parameters:
  • state (TensorState) – the state to quantize.

  • includes – the variables to include in the quantization. If this argument is not None, only the variables in this list will be quantized.

  • excludes – the variables to exclude in the quantization. If this argument is not None, the variables in this list will not be quantized.

Returns:

the quantized state.

Return type:

TensorState

quantize_tensor(dtype, tensor)[source]#

Quantize a PyTorch tensor. The main difference between this function and the quantize() function is that this function will quantize all the elements in the tensor.

Parameters:
Returns:

the quantized tensor.

Return type:

Tensor

quantize_value(value)[source]#

Quantize a single TensorValue object.

Parameters:

value (TensorValue) – the value to quantize.

Returns:

the quantized value.

Return type:

TensorValue

unquantize(typename, value)[source]#

The lowest-level API to unquantize a single value. Most of the time, you should use other higher-level APIs.

Parameters:
  • typename (str) – the typename of the value.

  • value (int) – the value to unquantize.

Returns:

the unquantized value.

Return type:

TensorValue

unquantize_tensor(dtype, tensor)[source]#

Unquantize a PyTorch tensor. The main difference between this function and the unquantize() function is that this function will unquantize all the elements in the tensor.

Parameters:
Returns:

the unquantized tensor.

Return type:

Tensor

unquantize_value(value)[source]#

Unquantize a single TensorValue object.

Parameters:

value (TensorValue) – the value to unquantize.

Returns:

the unquantized value.

Return type:

TensorValue

values: Dict[str, List[Any] | Dict[Any, int]]#

the value dictionary is a mapping from typename to a list of values. When we quantize a value v, we basically return the index of v in the list.