Interpretability tools for transformer-based Physical AI and robotics models. Built on LeRobot.
pip install physical-ai-interpretability
Easiest way to use the attention mapper is to run a post-hoc attention analysis of an existing dataset. In this case, we run our pre-trained policy on episodes in the dataset and capture the attention maps. This requires no connection to any robots and should work out of the box.
python examples/visualise_original_data_attention.py --dataset-repo-id lerobot/svla_so101_pickplace --episode-id 29 --policy-path <path to your pre-trained policy> --output-dir ./output/attention_analysis_results
Pre-trained policy part may look something like this: ../lerobot/outputs/train/act_johns_arm/checkpoints/last/pretrained_model
If you get an error with ModuleNotFoundError: No module named 'physical_ai_interpretability', set the PYTHONPATH environment variable to the location of physical-ai-interpretability in your local directory, e.g.
PYTHONPATH=/home/ville/physical-ai-interpretability:$PYTHONPATH.
Use the ACTPolicyWithAttention plugin in your project either by importing it from here or just copying the physical_ai_interpretability/attention_maps/act_attention_mapper.py file over.
See examples/usage_with_act.py for use of the attention mapper with the default LeRobot ACT policy.
I would like to add support for Pi0, SmolVLA, and other foundation models at some point!
Method of applying Dictionary Learning as proposed by Anthropic (and implemented in the famous Golden Gate Claude language model) into robotics and physical AI.
To reproduce this demo, you will need to repeat the following steps:
- Train a small ACT model for your task by changing the
dim_modelhyperparam value to124in LeRobot. You will likely need to increase your batch size and number of training steps as smaller models take longer to converge during training. (required if using RTX 4090 or other consumer GPUs) - Train a sparse autoencoder using
scripts/train_sae.py- The system will automatically infer
num_tokensfrom your ACT model configuration and structure. The total number of tokens in ACT models is calculated astotal_tokens = 2 + Σ(wᵢ/32 × hᵢ/32)where 2 tokens are reserved for the VAE token and joint states, plus tokens from each camera (by dividing width and height by 32 due to ResNet patch size). For example, 2 images with dims (480 × 640) generates2 + 2*((480/32) * (640/32)) = 602tokens. With token sampling (default ratio 8), we getnum_tokens = 600 / 8 = 75. The automatic inference tries multiple methods: model config inspection, positional embedding shapes, and fallback data-based inference. - Note that the training script is currently tested on my LeRobot fork and won't work with the most recent API updates to LeRobot. It should be easy to hack to get it working and if you do please send a PR!
- The system will automatically infer
- Record feature activations using
scripts/record_feature_activations.py. This generates Parquet files showing what features were active at each frame of your dataset - we will use them during manual analysis. - Analyse feature activations using the
scripts/analyse_features.ipynbnotebook. It should construct.jsonfiles describing the rop activating frames for features with the most variance in them. - Move or link the
scripts/feature_analysis_resultsinto theexamples/features_huggingface_spacedirectory, then runexamples/features_huggingface_space/ui.py. It will open in Gradio and allow you to visualise and name individual features. You can even deploy the results into Hugging Face spaces usinggradio deployto share what you found with the world! (change the save button to non-interactive if you don't want other people editing your features!)
The SAE trained for feature extraction also provides a neat implementation for out of distribution detection in robotics. The SAETrainer and OODDetector classes implement this.
scripts/demo_ood_detector.py shows how to test the OOD Detector with a pre-trained SAE model. The call looks something like this:
python scripts/demo_ood_detector.py \
--validation-repo-id villekuosmanen/dAgger_drop_footbag_into_dice_tower_1.7.0 \
--test-repo-id villekuosmanen/eval_dAgger_drop_footbag_into_dice_tower_1.7.0 \
--policy-path ../lerobot/outputs/train/reliable_robot_1.7.0_small_main/checkpoints/last/pretrained_model \
--sae-experiment-path output/sae_drop_footbag_into_di_838a8c8b
You can also upload the saved SAE model to the Hugging Face Hub by setting the --upload-to-hub and --hub-repo-id <model_repo_id> params.
Pikodata is a Data Studio designed for LeRobot Datasets, offering a UI for deleting episodes and frames, as well as editing language descriptions for LeRobot Datasets.
RewACT is a simple reward model built of ACT policies, used to measure the current task progress.
If you find my open-source Robotics and Physical AI work valuable, consider sponsoring me on GitHub!
