Skip to main content

XLens

A Library for Mechanistic Interpretability of Generative Language Models using JAX. Inspired by TransformerLens.

Overview

XLens is designed for mechanistic interpretability of Transformer language models, leveraging the power and efficiency of JAX. The primary goal of mechanistic interpretability is to reverse engineer the algorithms that a model has learned during training, enabling researchers and practitioners to understand the inner workings of generative language models.

Features

⚠️ Please Note: Some features are currently in development and may not yet be fully functional. We appreciate your understanding as we work to improve and stabilize the library.

  • Support for Hooked Modules: Interact with and modify internal model components seamlessly.
  • Model Alignment with Hugging Face: Outputs from XLens are consistent with Hugging Face's implementation, making it easier to integrate and compare results.
  • Caching Mechanism: Cache any internal activation for further analysis or manipulation during model inference.
  • Full Type Annotations: Comprehensive type annotations with generics and jaxtyping for better code completion and type checking.
  • Intuitive API: Designed with ease of use in mind, facilitating quick experimentation and exploration.

Installation

XLens can be installed via pip:

pip install xlens

Examples

Here are some basic examples to get you started with XLens.

Capturing Activations

from xlens import HookedTransformer
from transformers import AutoTokenizer

# Load a pre-trained model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B")
model = HookedTransformer.from_pretrained("meta-llama/Llama-3.2-1B")

# Capture the activations of the model
inputs = tokenizer("Hello, world!", return_tensors="np")
logits, cache = model.run_with_cache(**inputs, hook_names=["blocks.0.hook_attn_out"])
print(cache["blocks.0.hook_attn_out"].shape) # (1, 5, 2048)

Supported Models

XLens currently supports the following models:

Feel free to open an issue or pull request if you would like to see support for additional models.

Metadata

Release files for xlens 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xlens 0.1.1
File Size Uploaded Provenance
xlens-0.1.1.tar.gz 14.0 MB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for xlens 0.1.1
File Interpreter ABI Platform
xlens-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 MB

Release files / xlens-0.1.1.tar.gz

Download URL xlens-0.1.1.tar.gz
Size 14.0 MB
Tags Source
SHA-256 checksum
How to use checksums
2252e854c7dc5c6559c8a7945beff1cba0977915ce45cb4f2944b66555904739
BLAKE2b-256 checksum
How to use checksums
9731855263d07716dad6125a5f499438b7a50faeb0a797e5ff59330fade50ae3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.4.30

Release files / xlens-0.1.1-py3-none-any.whl

Download URL xlens-0.1.1-py3-none-any.whl
Size 35.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e8390e57fd698710060f1e1b3ee34c45bbf845b6d52fb6b060981c46df6a504
BLAKE2b-256 checksum
How to use checksums
cee866e202d6b93341ae9eb1a8c7699aaee8f4ee3a2f2dfcb39ed96a5fc39d8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.4.30

Security for xlens 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page