policy
policy
¶
Policy-facing helpers shared by attribution methods.
EncoderCacheDisabled
¶
Disable decoder encoder-prefix caches during attribution forwards.
Some VLM decoders cache encoded visual prefixes for autoregressive inference. Attribution re-runs the policy with hooks and activation replacements, so reusing a cached prefix can detach the current visual target from the scored prediction. This context manager clears the cache, suppresses the cache toggles while attribution runs, then restores the decoder's prior cache state.
Store the decoder whose cache controls should be suppressed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decoder
|
ActionDecoder
|
Policy decoder implementing the encoder-cache contract; decoders without a cache inherit the base no-op behavior. |
required |
Source code in src/versatil/explainability/attribution/policy.py
__enter__
¶
Clear the cache and freeze its toggles.
__exit__
¶
Unfreeze the toggles and restore the prior cache state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception_type
|
type[BaseException] | None
|
Exception class raised inside the context, if any. |
required |
exception
|
BaseException | None
|
Exception instance raised inside the context, if any. |
required |
traceback
|
TracebackType | None
|
Traceback raised inside the context, if any. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/versatil/explainability/attribution/policy.py
prepare_policy_observation_for_explanation
¶
Prepare observations with the policy's inference preprocessing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
Policy
|
Policy whose preprocessing stack should be reused. |
required |
observation
|
ObservationBatch
|
Observation values keyed by observation-space names. |
required |
preprocess_observation
|
bool
|
Whether to apply the same normalization and
tokenization path used by |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Observation tensors ready for |
Source code in src/versatil/explainability/attribution/policy.py
run_policy_for_explanation
¶
Return normalized predictions without using decoder encoder caches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
Policy
|
Policy being explained. |
required |
observation
|
ObservationBatch
|
Observation values keyed by observation-space names. |
required |
preprocess_observation
|
bool
|
Whether to normalize/tokenize observations before building policy features. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Policy predictions in the normalized action space used by attribution. |
Source code in src/versatil/explainability/attribution/policy.py
default_output_selector
¶
Return the normalized action-vector norm used as default objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Normalized policy predictions keyed by action component. Each tensor must use its action-component dimension as the final axis and share all leading batch/horizon dimensions with the other tensors. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Per-sample action norm after concatenating all prediction tensors along |
Tensor
|
the final axis. Attribution methods average this tensor to obtain the |
Tensor
|
scalar objective for gradients or ablation score drops. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If a prediction tensor is scalar. |
ValueError
|
If prediction tensors do not share leading dimensions. |