Author: Augusto Leogrande
Date: June 2026
Repository: https://github.com/LeograndeCode/EchoViT
Starting from the ideas introduced in TBKV and its possible extensions, we propose a novel approach for efficient video transformer inference based on object-level token matching and KV caching.
The main idea is to perform token matching and caching at the prototype level rather than applying token reduction independently to each frame. The objective is to preserve important foreground information while aggressively reducing redundant tokens, such as stable background regions.
To achieve this, the original matching ratio hyperparameter (
- Reuse threshold: determines whether a token/prototype is sufficiently stable to be reused from the KV cache without recomputation.
- Reduce threshold: determines whether a stable prototype can be merged and reduced.
The decision process is based on a stability metric defined through a simple hit-rate measurement. A hit occurs when the similarity between a new token and an existing prototype is higher than the reuse threshold.
Using this mechanism, frequently changing objects or rarely observed regions will have a lower stability score and will therefore be treated conservatively. Instead of performing aggressive reduction, these tokens will reuse their cached KV representation. Conversely, highly stable regions (e.g., static background areas) can be safely reduced through prototype merging.
- Merged tokens are renamed as prototypes, emphasizing their role as persistent representations of the scene.
- The single matching parameter is replaced with separate reuse and reduce thresholds.
- A prototype update mechanism is introduced to adapt stored representations to changing environmental conditions.
- An eviction mechanism based on prototype age is introduced to remove outdated representations.
This design aims to overcome limitations of previous approaches while providing a more elegant and adaptive formulation of token reduction for streaming video transformers.
Note: The prototype update mechanism is currently under development. If its implementation requires excessive additional time, we will temporarily revert to the previous formulation in order to prioritize experimental evaluation.
- Linux/macOS system (Windows users should use WSL2)
- ~50GB free disk space (for datasets and results)
- GPU with CUDA support recommended (NVIDIA GPUs with compute capability 7.0+)
git clone https://github.com/LeograndeCode/EchoViT.git
cd EchoViTIf you don't have Miniconda/Anaconda installed, download and install it:
# For Linux (x86_64)
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda3
# For macOS (Intel)
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
bash Miniconda3-latest-MacOSX-x86_64.sh -b -p $HOME/miniconda3
# For macOS (Apple Silicon)
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
bash Miniconda3-latest-MacOSX-arm64.sh -b -p $HOME/miniconda3
# Activate conda
source $HOME/miniconda3/bin/activate# Create environment from the provided environment.yml file
conda env create -f environment.yml
# Activate the environment
conda activate eventful-transformerThe environment.yml file includes:
- Python 3.10
- PyTorch 2.0 with CUDA 11.8 support
- Detectron2 (built from source)
- Required dependencies (OpenCV, FFmpeg, TensorBoard, etc.)
python -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}')"The evaluation framework requires the following folder structure:
EchoViT/
├── weights/ # Pre-trained model weights (create manually)
├── data/ # Datasets (auto-created during evaluation)
├── results/ # Evaluation outputs
└── configs/ # Configuration files (included in repo)
Create the weights folder:
mkdir -p weightsThe repository maintains the same overall structure regardless of whether the original or the proposed algorithm is used.
The src folder contains all source files required for the algorithm:
-
src/core/: Original implementation of the base modelbase.py- Base transformer module classesblocks.py- Standard transformer blockbackbones.py- Vision transformer backboneutils.py- Utility layers (DropPath, PositionEncoding, etc.)
-
src/evit/: EVIT-specific modifications and new modulesblocks.py- Modified transformer block with token matchingcache.py- Prototype KV cache implementationmodules.py- Token matching and caching algorithmsmerge.py- Token merging strategiesbackbones.py- EVIT backbone architecture
-
src/models/: High-level model implementationsvivit.py- ViViT (Factorized Vision Video Transformer)evit_vivit.py- EVIT ViViT with token optimizationvitdet.py- Vision Transformer for Detection
-
src/datasets/: Dataset utilitieskinetics400.py- Kinetics-400 loaderepic_kitchens.py- Epic Kitchens loadervid.py- Video utilities
-
src/utils/: Utility modulesevaluate.py- Evaluation utilitiesconfig.py- Configuration managementmisc.py- Miscellaneous utilities
configs/: Configuration files for models and datasetsscripts/: Evaluation scripts for different tasksscripts/evaluate/vivit_kinetics400.py- Main evaluation scriptscripts/convert/- Weight conversion scripts
The initial experimental validation focuses on:
- Model: ViViT-B pretrained on Kinetics-400
- Dataset: Kinetics-400
- Evaluation script:
scripts/evaluate/vivit_kinetics400.py
-
Download Pre-trained Weights
Download the pre-trained ViViT-B checkpoint from the Eventful Transformer repository:
# Place the downloaded weights in the weights folder mv vivit_b_kinetics400_final_24.pth weights/ -
Run Evaluation
conda activate eventful-transformer python scripts/evaluate/vivit_kinetics400.py
The script will:
- Automatically download the Kinetics-400 dataset (5.2GB)
- Perform inference on the dataset
- Save results in
results/evaluate/vivit_kinetics400/
Note: First run will take time due to dataset download and preprocessing.
Evaluation results are saved to:
results/evaluate/vivit_kinetics400/base/
├── config.yml # Evaluation configuration
├── predictions.json # Model predictions
└── metrics.json # Accuracy and performance metrics
The evaluation compares EVIT against state-of-the-art methods:
- Token Merging (ToMe): Token reduction via similarity-based merging
- DynamicViT: Dynamic token pruning using learned importance
- Eventful Transformer: Original event-driven baseline
Evaluation metrics:
- Accuracy preservation after token reduction
- Computational efficiency (FLOPs reduction)
- Inference throughput
- GPU memory consumption
- ViTDet for object detection
- EPIC-Kitchens for long-form egocentric video
- Ablation studies on:
- Prototype KV caching
- Stability metric
- Reuse and reduction thresholds
- Prototype update mechanism
- Eviction strategies
1. CUDA out of memory
# Reduce batch size in config
python scripts/evaluate/vivit_kinetics400.py --batch_size 82. Dataset download fails
# Manually download Kinetics-400 and place in data/kinetics400/
# Dataset structure should be:
# data/kinetics400/
# ├── train_meta.csv
# ├── val_meta.csv
# └── videos/3. ModuleNotFoundError
# Ensure conda environment is activated
conda activate eventful-transformer
# Verify imports
python -c "import src.core.base; print('✓ Imports successful')"# List installed packages
conda list
# Check GPU
python -c "import torch; print(torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'No GPU')"- Create a new script in
scripts/evaluate/ - Use configuration files from
configs/evaluate/ - Import from
srcmodules following the established pattern
# Convert ViViT weights
./scripts/convert/vivit.py input_weights.pth output_weights.pth ./configs/convert/vivit_b.txt
# Convert ViTDet weights
./scripts/convert/vitdet.py input_weights.pkl output_weights.pth ./configs/convert/vitdet_b.txt- Original Eventful Transformer: GitHub Repository
- ViViT Paper: Factorized Video Transformers with Reuse and Decomposition
- Kinetics-400 Dataset: Official Website
[Specify your license here]
If you use this code, please cite:
@article{leogrande2026evit,
title={EVIT: Object-Level Token Matching and KV Cache for Efficient Video Transformers},
author={Leogrande, Augusto},
year={2026}
}For questions or issues, please open an issue on the GitHub repository.