Find the hidden meaning of LLMs
Project description
MLX-LM-LENS
Find the hidden meaning of LLMs
MLX-LM-LENS provides a simple wrapper to inspect hidden states of MLX-based language models.
This package is mainly intended as a research tool, though it can also be used to create real-world models such as the "abliterated" models. Beyond hidden states it lets you inspect attention scores and embedding layer outputs. MLX-LM-LENS is built on top of the MLX-LM framework, so every model supported in MLX-LM works here as well.
Installation
pip install mlx-lm-lens
Quick Start
import mlx.core as mx
from mlx_lm_lens.lens import open_lens
model_lens, tokenizer = open_lens("Goekdeniz-Guelmez/Josiefied-Qwen2.5-0.5B-Instruct-abliterated-v1")
# When loading it will trow a debug print like "Identified components: Embeddings=Embedding, Layers=24, Norm=True, LM Head=Embedding, Tied=True"
tokens = mx.array([[9707]]) # <-- "Hello"
lens_data = model_lens(
tokens,
return_dict=True
)
embeds = model_lens.get_embeds(tokens)
print(lens_data) # Results in:
# {'logits': array([[[5.71875, 4.78125, 0.542969, ..., -3.3125, -3.3125, -3.3125]]], dtype=bfloat16), 'hidden_states': [array([[[-0.0256348, 0.00537109, -0.010376, ..., 0.00285339, -0.00860596, 0.00369263]]], dtype=bfloat16), array([[[-0.0256348, 0.00537109, -0.010376, ..., 0.00285339, -0.00860596, 0.00369263]]], dtype=bfloat16), array([[[0.0966797, 0.0878906, 0.302734, ..., 0.357422, 0.0361328, 0.105957]]], dtype=bfloat16), array([[[0.0966797, 0.0878906, 0.302734, ..., 0.357422, 0.0361328, 0.105957]]], dtype=bfloat16), array([[[-0.03125, 0.230469, 0.275391, ..., 0.255859, 0.0395508, 0.0361328]]], dtype=bfloat16), array([[[-0.03125, 0.230469, 0.275391, ..., 0.255859, 0.0395508, 0.0361328]]], dtype=bfloat16), array([[[-0.945312, -6.09375, -2.34375, ..., -0.71875, -1.03125, 2.73438]]], dtype=bfloat16), array([[[-0.945312, -6.09375, -2.34375, ..., -0.71875, -1.03125, 2.73438]]], dtype=bfloat16), array([[[-2.54688, -10.0625, -4.9375, ..., -0.078125, -0.546875, 1.42188]]], dtype=bfloat16), array([[[-2.54688, -10.0625, -4.9375, ..., -0.078125, -0.546875, 1.42188]]], dtype=bfloat16), array([[[-2.59375, -10, -5.03125, ..., -0.0366211, -0.558594, 1.4375]]], dtype=bfloat16), array([[[-2.59375, -10, -5.03125, ..., -0.0366211, -0.558594, 1.4375]]], dtype=bfloat16), array([[[-2.64062, -10.1875, -4.9375, ..., 0.0859375, -0.392578, 1.375]]], dtype=bfloat16), array([[[-2.64062, -10.1875, -4.9375, ..., 0.0859375, -0.392578, 1.375]]], dtype=bfloat16), array([[[-2.67188, -10.4375, -4.90625, ..., -0.043457, -0.396484, 1.48438]]], dtype=bfloat16), array([[[-2.67188, -10.4375, -4.90625, ..., -0.043457, -0.396484, 1.48438]]], dtype=bfloat16), array([[[-2.65625, -10.5625, -4.90625, ..., 0.0253906, -0.371094, 1.45312]]], dtype=bfloat16), array([[[-2.65625, -10.5625, -4.90625, ..., 0.0253906, -0.371094, 1.45312]]], dtype=bfloat16), array([[[-2.70312, -10.625, -4.8125, ..., 0.181641, -0.382812, 1.36719]]], dtype=bfloat16), array([[[-2.70312, -10.625, -4.8125, ..., 0.181641, -0.382812, 1.36719]]], dtype=bfloat16), array([[[-2.73438, -10.625, -4.875, ..., 0.345703, -0.449219, 1.22656]]], dtype=bfloat16), array([[[-2.73438, -10.625, -4.875, ..., 0.345703, -0.449219, 1.22656]]], dtype=bfloat16), array([[[-2.6875, -10.5, -4.875, ..., 0.337891, -0.390625, 1.1875]]], dtype=bfloat16), array([[[-2.6875, -10.5, -4.875, ..., 0.337891, -0.390625, 1.1875]]], dtype=bfloat16), array([[[-2.65625, -10.5, -4.71875, ..., 0.476562, -0.332031, 1.21094]]], dtype=bfloat16), array([[[-2.65625, -10.5, -4.71875, ..., 0.476562, -0.332031, 1.21094]]], dtype=bfloat16), array([[[-2.6875, -10.5625, -4.71875, ..., 0.447266, -0.337891, 1.05469]]], dtype=bfloat16), array([[[-2.6875, -10.5625, -4.71875, ..., 0.447266, -0.337891, 1.05469]]], dtype=bfloat16), array([[[-2.73438, -10.625, -4.71875, ..., 0.566406, -0.242188, 0.871094]]], dtype=bfloat16), array([[[-2.73438, -10.625, -4.71875, ..., 0.566406, -0.242188, 0.871094]]], dtype=bfloat16), array([[[-2.75, -10.5625, -4.75, ..., 0.582031, -0.188477, 0.792969]]], dtype=bfloat16), array([[[-2.75, -10.5625, -4.75, ..., 0.582031, -0.188477, 0.792969]]], dtype=bfloat16), array([[[-2.78125, -10.5625, -4.75, ..., 0.570312, -0.152344, 0.796875]]], dtype=bfloat16), array([[[-2.78125, -10.5625, -4.75, ..., 0.570312, -0.152344, 0.796875]]], dtype=bfloat16), array([[[-2.8125, -10.625, -4.71875, ..., 0.707031, -0.296875, 0.75]]], dtype=bfloat16), array([[[-2.8125, -10.625, -4.71875, ..., 0.707031, -0.296875, 0.75]]], dtype=bfloat16), array([[[-2.875, -10.625, -4.75, ..., 0.714844, -0.178711, 0.566406]]], dtype=bfloat16), array([[[-2.875, -10.625, -4.75, ..., 0.714844, -0.178711, 0.566406]]], dtype=bfloat16), array([[[-2.82812, -10.625, -4.65625, ..., 0.710938, -0.234375, 0.566406]]], dtype=bfloat16), array([[[-2.82812, -10.625, -4.65625, ..., 0.710938, -0.234375, 0.566406]]], dtype=bfloat16), array([[[-2.6875, -10.625, -4.71875, ..., 0.828125, -0.208008, 0.0917969]]], dtype=bfloat16), array([[[-2.6875, -10.625, -4.71875, ..., 0.828125, -0.208008, 0.0917969]]], dtype=bfloat16), array([[[-2.65625, -10.625, -4.625, ..., 0.976562, -0.162109, 0.417969]]], dtype=bfloat16), array([[[-2.65625, -10.625, -4.625, ..., 0.976562, -0.162109, 0.417969]]], dtype=bfloat16), array([[[0.078125, -1.125, 0.09375, ..., 0.488281, -0.279297, -0.0859375]]], dtype=bfloat16), array([[[0.078125, -1.125, 0.09375, ..., 0.488281, -0.279297, -0.0859375]]], dtype=bfloat16), array([[[-1.85938, -0.402344, 1.96875, ..., 2.20312, -1.94531, -0.427734]]], dtype=bfloat16), array([[[-1.85938, -0.402344, 1.96875, ..., 2.20312, -1.94531, -0.427734]]], dtype=bfloat16), array([[[0.539062, -0.316406, 2.15625, ..., 1.35156, -1.95312, -1.60938]]], dtype=bfloat16), array([[[1.39062, -0.742188, 5.3125, ..., 3.3125, -4.71875, -5.15625]]], dtype=bfloat16)]}
print(embeds) # array([[[-0.0256348, 0.00537109, -0.010376, ..., 0.00285339, -0.00860596, 0.00369263]]], dtype=bfloat16)
Examples
The examples/ directory contains additional scripts illustrating various uses:
abliterate.pyvisualize_attentions.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mlx_lm_lens-0.0.1.tar.gz.
File metadata
- Download URL: mlx_lm_lens-0.0.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76017e1501daf749675e3e183eca03612711ee2201c763817bca3232b145002a
|
|
| MD5 |
13beb343b5fd1d9e95c4bd1119dc8609
|
|
| BLAKE2b-256 |
1a24a9a85520f93dac6c3f093b93d5611d09ffbbfb0c3005664a3ff828228bbf
|
File details
Details for the file mlx_lm_lens-0.0.1-py3-none-any.whl.
File metadata
- Download URL: mlx_lm_lens-0.0.1-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b0eb5ffc252ca0bacd46a6d36d3e4bb23d86d2cbf45d915a8bbb88418a59b8d
|
|
| MD5 |
078297a26f7a5647be67e46b187e078e
|
|
| BLAKE2b-256 |
c74e9d3eac3fcad304f37ea706ca1e9f9316875100cae25606cc924dc36e8c53
|