concepts.dsl.executors.tensor_value_executor#

Tensor-based expression executor.

The high-level interface for tensor-based expression is that we can execute an expression with a given state and a set of bounded variables. The executor will return a tensor value.

The state is represented using concepts.dsl.tensor_state.TensorState or concepts.dsl.tensor_state.NamedObjectTensorState, which internally stores a dictionary mapping from string (the state variable name, e.g., is_hot) to a concepts.dsl.tensor_value.TensorValue class.

The bounded variables are essentially a dictionary mapping from strings (the variable name, e.g., x) to its value. There are two types of values: (1) a concepts.dsl.tensor_value.TensorValue class, which represents an actual value (e.g., a vector representation); (2) a StateObjectReference instance or a QINDEX (a.k.a., slice(None)), which represents a reference to an object in the state.

With the bounded variables, the expressions can have variables, which are essentially placeholders for the actual values. For example,

domain = FunctionDomain()
# Define an object type `person`.
domain.define_type(ObjectType('person'))
# Define a state variable `is_friend` with type `person x person -> bool`.
domain.define_function(Function('is_friend', FunctionType([ObjectType('person'), ObjectType('person')], BOOL)))

x = VariableExpression(Variable('x', ObjectType('person')))
y = VariableExpression(Variable('y', ObjectType('person')))
relation = FunctionApplication(domain.functions['is_friend'], [x, y])

Then we can execute the expression with a given state and bounded variables:

# See the documentation for namedObjectTensorState for more details.
state = NamedObjectTensorState({
    'is_friend': TensorValue(BOOL, ['x', 'y'], torch.tensor([[1, 1, 1], [1, 1, 0], [1, 0, 1]], dtype=torch.bool))
}, object_names={
    'Alice': ObjectType('person'),
    'Bob': ObjectType('person'),
    'Charlie': ObjectType('person'),
})
executor = SimpleFunctionTensorValueExecutor(domain)

# For both of the following lines, the result is a tensor value with value `True`.
# Use the constructed expression:
executor.execute(relation, state, {'x': 'Alice', 'y': 'Bob'})
# To use the default parser:
executor.execute('is_friend(x, y)', state, {'x': 'Alice', 'y': 'Bob'})

Module attributes

BoundedVariablesDict

Internal representation of a bounded variable dictionary.

BoundedVariablesDictCompatible

Compatible types with BoundedVariablesDict.

Functions

compose_bvdict(input_dict[, state])

Compose a bounded variable dict from raw inputs.

compose_bvdict_args(arguments_def, arguments)

Compose a bounded variable dict, but from a list of arguments.

get_bvdict(bvdict, variable)

Get the value of a variable from a bounded variable dict.

Classes

FunctionDomainTensorValueExecutor

Similar to FunctionDomainExecutor, but works for TensorValue.

TensorValueExecutorBase

The base class for tensor value executors.