exportable_policy
exportable_policy
¶
Wrapper making VersatIL Policy compatible with torch.export.export().
ExportablePolicy
¶
Bases: Module
Wraps Policy components for torch.export with positional tensor I/O.
torch.export requires positional tensor args and tuple returns. This wrapper converts between Policy's dict-based interface and the positional interface needed for quantization export.
Initialize with policy components and key orderings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding_pipeline
|
EncodingPipeline
|
The policy's encoding pipeline. |
required |
algorithm
|
DecodingAlgorithm
|
The policy's decoding algorithm. |
required |
decoder
|
ActionDecoder
|
The policy's action decoder. |
required |
observation_keys
|
list[str]
|
Sorted list of observation dict keys. |
required |
action_keys
|
list[str]
|
Action output keys in action-space metadata order. |
required |
Source code in src/versatil/models/exportable_policy.py
forward
¶
Forward pass with positional tensor I/O.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*observation_tensors
|
Tensor
|
Tensors in the same order as observation_keys. |
()
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, ...]
|
Tuple of action tensors in the same order as action_keys. |
Source code in src/versatil/models/exportable_policy.py
from_policy
classmethod
¶
Create ExportablePolicy from a loaded Policy.
Delegates key derivation to Policy.input_keys and Policy.output_keys, keeping the single source of truth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
Policy
|
A loaded, initialized Policy instance. |
required |
Returns:
| Type | Description |
|---|---|
ExportablePolicy
|
ExportablePolicy wrapping the policy's components. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the policy predicts action tokens; exported forward passes return raw tensors and never detokenize, so the exported outputs would not match the action space. |
Source code in src/versatil/models/exportable_policy.py
get_example_inputs
¶
Generate example inputs with correct shapes for torch.export.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observation_shapes
|
dict[str, tuple[int, ...]]
|
Mapping from observation key to shape tuple (excluding batch dimension). Must cover all observation_keys. |
required |
batch_size
|
int
|
Batch dimension size. |
1
|
observation_dtypes
|
dict[str, dtype] | None
|
Optional mapping from observation key to torch dtype. Defaults to torch.float32 for all keys. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, ...]
|
Tuple of tensors matching observation_keys order. |