Samuel Stevens
commited on
Commit
·
d4c84c4
1
Parent(s):
aabf2a7
initial (broken) commit
Browse files- .gitignore +2 -0
- README.md +10 -0
- app.py +520 -0
- ckpts/cfg.json +37 -0
- ckpts/clf.pt +3 -0
- ckpts/sae.pt +3 -0
- data/image_fpaths.json +0 -0
- data/image_labels.json +1 -0
- justfile +9 -0
- pyproject.toml +19 -0
- requirements.txt +211 -0
- uv.lock +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.aider*
|
| 2 |
+
.env
|
README.md
CHANGED
|
@@ -5,6 +5,7 @@ colorFrom: gray
|
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.9.1
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
@@ -12,3 +13,12 @@ short_description: Interpret image classification models using SAEs.
|
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.9.1
|
| 8 |
+
python_version: 3.13
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
|
|
|
| 13 |
---
|
| 14 |
|
| 15 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 16 |
+
|
| 17 |
+
I used [s5cmd](https://github.com/peak/s5cmd) to upload CUB-2011 to Cloudflare R2.
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
```sh
|
| 21 |
+
s5cmd --credentials-file ~/.local/etc/cloudflare/r2-credentials --endpoint-url https://6391ae4399fb354a41cab96372935a6e.r2.cloudflarestorage.com \
|
| 22 |
+
cp test/ s3://saev-cub2011/
|
| 23 |
+
```
|
| 24 |
+
|
app.py
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import math
|
| 5 |
+
import os
|
| 6 |
+
import pathlib
|
| 7 |
+
import random
|
| 8 |
+
|
| 9 |
+
import beartype
|
| 10 |
+
import einops.layers.torch
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import numpy as np
|
| 13 |
+
import open_clip
|
| 14 |
+
import requests
|
| 15 |
+
import saev.nn
|
| 16 |
+
import torch
|
| 17 |
+
from jaxtyping import Float, jaxtyped
|
| 18 |
+
from PIL import Image, ImageDraw
|
| 19 |
+
from torch import Tensor
|
| 20 |
+
from torchvision.transforms import v2
|
| 21 |
+
|
| 22 |
+
log_format = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"
|
| 23 |
+
logging.basicConfig(level=logging.INFO, format=log_format)
|
| 24 |
+
logger = logging.getLogger("app.py")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
####################
|
| 28 |
+
# Global Constants #
|
| 29 |
+
####################
|
| 30 |
+
|
| 31 |
+
DEBUG = True
|
| 32 |
+
"""Whether we are debugging."""
|
| 33 |
+
|
| 34 |
+
n_sae_latents = 3
|
| 35 |
+
"""Number of SAE latents to show."""
|
| 36 |
+
|
| 37 |
+
n_sae_examples = 4
|
| 38 |
+
"""Number of SAE examples per latent to show."""
|
| 39 |
+
|
| 40 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 41 |
+
"""Hardware accelerator, if any."""
|
| 42 |
+
|
| 43 |
+
vit_ckpt = "ViT-B-16/openai"
|
| 44 |
+
"""CLIP checkpoint."""
|
| 45 |
+
|
| 46 |
+
n_patches_per_img: int = 196
|
| 47 |
+
"""Number of patches per image in vit_ckpt."""
|
| 48 |
+
|
| 49 |
+
max_frequency = 1e-2
|
| 50 |
+
"""Maximum frequency. Any feature that fires more than this is ignored."""
|
| 51 |
+
|
| 52 |
+
CWD = pathlib.Path(__file__).parent
|
| 53 |
+
|
| 54 |
+
r2_url = "https://pub-289086e849214430853bc87bd8964988.r2.dev/"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
logger.info("Set global constants.")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
###########
|
| 61 |
+
# Helpers #
|
| 62 |
+
###########
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@beartype.beartype
|
| 66 |
+
def get_cache_dir() -> str:
|
| 67 |
+
"""
|
| 68 |
+
Get cache directory from environment variables, defaulting to the current working directory (.)
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
A path to a cache directory (might not exist yet).
|
| 72 |
+
"""
|
| 73 |
+
cache_dir = ""
|
| 74 |
+
for var in ("HF_HOME", "HF_HUB_CACHE"):
|
| 75 |
+
cache_dir = cache_dir or os.environ.get(var, "")
|
| 76 |
+
return cache_dir or "."
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@beartype.beartype
|
| 80 |
+
def load_model(fpath: str | pathlib.Path, *, device: str = "cpu") -> torch.nn.Module:
|
| 81 |
+
"""
|
| 82 |
+
Loads a linear layer from disk.
|
| 83 |
+
"""
|
| 84 |
+
with open(fpath, "rb") as fd:
|
| 85 |
+
kwargs = json.loads(fd.readline().decode())
|
| 86 |
+
buffer = io.BytesIO(fd.read())
|
| 87 |
+
|
| 88 |
+
model = torch.nn.Linear(**kwargs)
|
| 89 |
+
state_dict = torch.load(buffer, weights_only=True, map_location=device)
|
| 90 |
+
model.load_state_dict(state_dict)
|
| 91 |
+
model = model.to(device)
|
| 92 |
+
return model
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@beartype.beartype
|
| 96 |
+
def get_dataset_img(i: int) -> Image.Image:
|
| 97 |
+
return Image.open(requests.get(r2_url + image_fpaths[i], stream=True).raw)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@beartype.beartype
|
| 101 |
+
def make_img(
|
| 102 |
+
img: Image.Image, patches: Float[Tensor, ""], *, upper: float | None = None
|
| 103 |
+
) -> Image.Image:
|
| 104 |
+
# Resize to 256x256 and crop to 224x224
|
| 105 |
+
resize_size_px = (512, 512)
|
| 106 |
+
resize_w_px, resize_h_px = resize_size_px
|
| 107 |
+
crop_size_px = (448, 448)
|
| 108 |
+
crop_w_px, crop_h_px = crop_size_px
|
| 109 |
+
crop_coords_px = (
|
| 110 |
+
(resize_w_px - crop_w_px) // 2,
|
| 111 |
+
(resize_h_px - crop_h_px) // 2,
|
| 112 |
+
(resize_w_px + crop_w_px) // 2,
|
| 113 |
+
(resize_h_px + crop_h_px) // 2,
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
img = img.resize(resize_size_px).crop(crop_coords_px)
|
| 117 |
+
img = add_highlights(img, patches.numpy(), upper=upper, opacity=0.5)
|
| 118 |
+
return img
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
##########
|
| 122 |
+
# Models #
|
| 123 |
+
##########
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
@jaxtyped(typechecker=beartype.beartype)
|
| 127 |
+
class SplitClip(torch.nn.Module):
|
| 128 |
+
def __init__(self, *, n_end_layers: int):
|
| 129 |
+
super().__init__()
|
| 130 |
+
|
| 131 |
+
if vit_ckpt.startswith("hf-hub:"):
|
| 132 |
+
clip, _ = open_clip.create_model_from_pretrained(
|
| 133 |
+
vit_ckpt, cache_dir=get_cache_dir()
|
| 134 |
+
)
|
| 135 |
+
else:
|
| 136 |
+
arch, ckpt = vit_ckpt.split("/")
|
| 137 |
+
clip, _ = open_clip.create_model_from_pretrained(
|
| 138 |
+
arch, pretrained=ckpt, cache_dir=get_cache_dir()
|
| 139 |
+
)
|
| 140 |
+
model = clip.visual
|
| 141 |
+
model.proj = None
|
| 142 |
+
model.output_tokens = True # type: ignore
|
| 143 |
+
self.vit = model.eval()
|
| 144 |
+
assert not isinstance(self.vit, open_clip.timm_model.TimmModel)
|
| 145 |
+
|
| 146 |
+
self.n_end_layers = n_end_layers
|
| 147 |
+
|
| 148 |
+
@staticmethod
|
| 149 |
+
def _expand_token(token, batch_size: int):
|
| 150 |
+
return token.view(1, 1, -1).expand(batch_size, -1, -1)
|
| 151 |
+
|
| 152 |
+
def forward_start(self, x: Float[Tensor, "batch channels width height"]):
|
| 153 |
+
x = self.vit.conv1(x) # shape = [*, width, grid, grid]
|
| 154 |
+
x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2]
|
| 155 |
+
x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width]
|
| 156 |
+
|
| 157 |
+
# class embeddings and positional embeddings
|
| 158 |
+
x = torch.cat(
|
| 159 |
+
[self._expand_token(self.vit.class_embedding, x.shape[0]).to(x.dtype), x],
|
| 160 |
+
dim=1,
|
| 161 |
+
)
|
| 162 |
+
# shape = [*, grid ** 2 + 1, width]
|
| 163 |
+
x = x + self.vit.positional_embedding.to(x.dtype)
|
| 164 |
+
|
| 165 |
+
x = self.vit.patch_dropout(x)
|
| 166 |
+
x = self.vit.ln_pre(x)
|
| 167 |
+
for r in self.vit.transformer.resblocks[: -self.n_end_layers]:
|
| 168 |
+
x = r(x)
|
| 169 |
+
return x
|
| 170 |
+
|
| 171 |
+
def forward_end(self, x: Float[Tensor, "batch n_patches dim"]):
|
| 172 |
+
for r in self.vit.transformer.resblocks[-self.n_end_layers :]:
|
| 173 |
+
x = r(x)
|
| 174 |
+
|
| 175 |
+
x = self.vit.ln_post(x)
|
| 176 |
+
pooled, _ = self.vit._global_pool(x)
|
| 177 |
+
if self.vit.proj is not None:
|
| 178 |
+
pooled = pooled @ self.vit.proj
|
| 179 |
+
|
| 180 |
+
return pooled
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# ViT
|
| 184 |
+
split_vit = SplitClip(n_end_layers=1)
|
| 185 |
+
split_vit = split_vit.to(device)
|
| 186 |
+
logger.info("Initialized CLIP ViT.")
|
| 187 |
+
|
| 188 |
+
# Linear classifier
|
| 189 |
+
clf_ckpt_fpath = CWD / "ckpts" / "clf.pt"
|
| 190 |
+
clf = load_model(clf_ckpt_fpath)
|
| 191 |
+
clf = clf.to(device).eval()
|
| 192 |
+
logger.info("Loaded linear classifier.")
|
| 193 |
+
|
| 194 |
+
# SAE
|
| 195 |
+
sae_ckpt_fpath = CWD / "ckpts" / "sae.pt"
|
| 196 |
+
sae = saev.nn.load(sae_ckpt_fpath.as_posix())
|
| 197 |
+
sae.to(device).eval()
|
| 198 |
+
logger.info("Loaded SAE.")
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
############
|
| 202 |
+
# Datasets #
|
| 203 |
+
############
|
| 204 |
+
|
| 205 |
+
human_transform = v2.Compose([
|
| 206 |
+
v2.Resize((512, 512), interpolation=v2.InterpolationMode.NEAREST),
|
| 207 |
+
v2.CenterCrop((448, 448)),
|
| 208 |
+
v2.ToImage(),
|
| 209 |
+
einops.layers.torch.Rearrange("channels width height -> width height channels"),
|
| 210 |
+
])
|
| 211 |
+
|
| 212 |
+
arch, ckpt = vit_ckpt.split("/")
|
| 213 |
+
_, vit_transform = open_clip.create_model_from_pretrained(
|
| 214 |
+
arch, pretrained=ckpt, cache_dir=get_cache_dir()
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
with open(CWD / "data" / "image_fpaths.json") as fd:
|
| 219 |
+
image_fpaths = json.load(fd)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
with open(CWD / "data" / "image_labels.json") as fd:
|
| 223 |
+
image_labels = json.load(fd)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
# TODO:
|
| 227 |
+
# This dataset needs to be the CUB2011 dataset. But that means we need to calculate top_img_i based on CUB2011, not on iNat21 train-mini.
|
| 228 |
+
# examples_dataset = saev.activations.ImageFolder(
|
| 229 |
+
# "/research/nfs_su_809/workspace/stevens.994/datasets/inat21/train_mini",
|
| 230 |
+
# transform=v2.Compose([
|
| 231 |
+
# v2.Resize(size=(512, 512)),
|
| 232 |
+
# v2.CenterCrop(size=(448, 448)),
|
| 233 |
+
# ]),
|
| 234 |
+
# )
|
| 235 |
+
|
| 236 |
+
logger.info("Loaded all datasets.")
|
| 237 |
+
|
| 238 |
+
#############
|
| 239 |
+
# Variables #
|
| 240 |
+
#############
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
@beartype.beartype
|
| 244 |
+
def load_tensor(path: str | pathlib.Path) -> Tensor:
|
| 245 |
+
return torch.load(path, weights_only=True, map_location="cpu")
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
top_img_i = load_tensor(CWD / "data" / "top_img_i.pt")
|
| 249 |
+
top_values = load_tensor(CWD / "data" / "top_values.pt")
|
| 250 |
+
sparsity = load_tensor(CWD / "data" / "sparsity.pt")
|
| 251 |
+
|
| 252 |
+
mask = torch.ones((sae.cfg.d_sae), dtype=bool)
|
| 253 |
+
mask = mask & (sparsity < max_frequency)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
#############
|
| 257 |
+
# Inference #
|
| 258 |
+
#############
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
@beartype.beartype
|
| 262 |
+
def get_image(image_i: int) -> list[Image.Image | int]:
|
| 263 |
+
image = get_dataset_img(image_i)
|
| 264 |
+
image = human_transform(image)
|
| 265 |
+
return [Image.fromarray(image.numpy()), image_labels[image_i]]
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
@beartype.beartype
|
| 269 |
+
def get_random_class_image(cls: int) -> Image.Image:
|
| 270 |
+
indices = [i for i, tgt in enumerate(image_labels) if tgt == cls]
|
| 271 |
+
i = random.choice(indices)
|
| 272 |
+
|
| 273 |
+
image = get_dataset_img(i)
|
| 274 |
+
image = human_transform(image)
|
| 275 |
+
return Image.fromarray(image.numpy())
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
@torch.inference_mode
|
| 279 |
+
def get_sae_examples(
|
| 280 |
+
image_i: int, patches: list[int]
|
| 281 |
+
) -> list[None | Image.Image | int]:
|
| 282 |
+
"""
|
| 283 |
+
Given a particular cell, returns some highlighted images showing what feature fires most on this cell.
|
| 284 |
+
"""
|
| 285 |
+
if not patches:
|
| 286 |
+
return [None] * 12 + [-1] * 3
|
| 287 |
+
|
| 288 |
+
img = get_dataset_img(image_i)
|
| 289 |
+
x = vit_transform(img)[None, ...].to(device)
|
| 290 |
+
x_BPD = split_vit.forward_start(x)
|
| 291 |
+
vit_acts_MD = x_BPD[0, patches].to(device)
|
| 292 |
+
|
| 293 |
+
_, f_x_MS, _ = sae(vit_acts_MD)
|
| 294 |
+
f_x_S = f_x_MS.sum(axis=0)
|
| 295 |
+
|
| 296 |
+
latents = torch.argsort(f_x_S, descending=True).cpu()
|
| 297 |
+
latents = latents[mask[latents]][:n_sae_latents].tolist()
|
| 298 |
+
|
| 299 |
+
images = []
|
| 300 |
+
for latent in latents:
|
| 301 |
+
img_patch_pairs, seen_i_im = [], set()
|
| 302 |
+
for i_im, values_p in zip(top_img_i[latent].tolist(), top_values[latent]):
|
| 303 |
+
if i_im in seen_i_im:
|
| 304 |
+
continue
|
| 305 |
+
|
| 306 |
+
# example = examples_dataset[i_im]
|
| 307 |
+
example = None
|
| 308 |
+
img_patch_pairs.append((example["image"], values_p))
|
| 309 |
+
seen_i_im.add(i_im)
|
| 310 |
+
|
| 311 |
+
# How to scale values.
|
| 312 |
+
upper = None
|
| 313 |
+
if top_values[latent].numel() > 0:
|
| 314 |
+
upper = top_values[latent].max().item()
|
| 315 |
+
|
| 316 |
+
latent_images = [
|
| 317 |
+
make_img(img, patches, upper=upper)
|
| 318 |
+
for img, patches in img_patch_pairs[:n_sae_examples]
|
| 319 |
+
]
|
| 320 |
+
|
| 321 |
+
while len(latent_images) < n_sae_examples:
|
| 322 |
+
latent_images += [None]
|
| 323 |
+
|
| 324 |
+
images.extend(latent_images)
|
| 325 |
+
|
| 326 |
+
return images + latents
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
@torch.inference_mode
|
| 330 |
+
def get_pred_dist(i: int) -> dict[int, float]:
|
| 331 |
+
img = get_dataset_img(i)
|
| 332 |
+
x = vit_transform(img)[None, ...].to(device)
|
| 333 |
+
x_BPD = split_vit.forward_start(x)
|
| 334 |
+
x_BD = split_vit.forward_end(x_BPD)
|
| 335 |
+
|
| 336 |
+
logits_BC = clf(x_BD)
|
| 337 |
+
|
| 338 |
+
probs = torch.nn.functional.softmax(logits_BC[0], dim=0).cpu().tolist()
|
| 339 |
+
return {i: prob for i, prob in enumerate(probs)}
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
@torch.inference_mode
|
| 343 |
+
def get_modified_dist(
|
| 344 |
+
image_i: int,
|
| 345 |
+
patches: list[int],
|
| 346 |
+
latent1: int,
|
| 347 |
+
latent2: int,
|
| 348 |
+
latent3: int,
|
| 349 |
+
value1: float,
|
| 350 |
+
value2: float,
|
| 351 |
+
value3: float,
|
| 352 |
+
) -> dict[int, float]:
|
| 353 |
+
img = get_dataset_img(image_i)
|
| 354 |
+
x = vit_transform(img)[None, ...].to(device)
|
| 355 |
+
x_BPD = split_vit.forward_start(x)
|
| 356 |
+
|
| 357 |
+
cls_B1D, x_BPD = x_BPD[:, :1, :], x_BPD[:, 1:, :]
|
| 358 |
+
|
| 359 |
+
x_hat_BPD, f_x_BPS, _ = sae(x_BPD)
|
| 360 |
+
err_BPD = x_BPD - x_hat_BPD
|
| 361 |
+
|
| 362 |
+
values = torch.tensor(
|
| 363 |
+
[
|
| 364 |
+
unscaled(float(value), top_values[latent].max().item())
|
| 365 |
+
for value, latent in [
|
| 366 |
+
(value1, latent1),
|
| 367 |
+
(value2, latent2),
|
| 368 |
+
(value3, latent3),
|
| 369 |
+
]
|
| 370 |
+
],
|
| 371 |
+
device=device,
|
| 372 |
+
)
|
| 373 |
+
patches = torch.tensor(patches, device=device)
|
| 374 |
+
latents = torch.tensor([latent1, latent2, latent3], device=device)
|
| 375 |
+
f_x_BPS[:, patches[:, None], latents[None, :]] = values
|
| 376 |
+
|
| 377 |
+
# Reproduce the SAE forward pass after f_x
|
| 378 |
+
modified_x_hat_BPD = (
|
| 379 |
+
einops.einsum(
|
| 380 |
+
f_x_BPS,
|
| 381 |
+
sae.W_dec,
|
| 382 |
+
"batch patches d_sae, d_sae d_vit -> batch patches d_vit",
|
| 383 |
+
)
|
| 384 |
+
+ sae.b_dec
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
modified_BPD = torch.cat([cls_B1D, err_BPD + modified_x_hat_BPD], axis=1)
|
| 388 |
+
|
| 389 |
+
modified_BD = split_vit.forward_end(modified_BPD)
|
| 390 |
+
logits_BC = clf(modified_BD)
|
| 391 |
+
|
| 392 |
+
probs = torch.nn.functional.softmax(logits_BC[0], dim=0).cpu().tolist()
|
| 393 |
+
return {i: prob for i, prob in enumerate(probs)}
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
@beartype.beartype
|
| 397 |
+
def unscaled(x: float, max_obs: float) -> float:
|
| 398 |
+
"""Scale from [-20, 20] to [20 * -max_obs, 20 * max_obs]."""
|
| 399 |
+
return map_range(x, (-20.0, 20.0), (-20.0 * max_obs, 20.0 * max_obs))
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
@beartype.beartype
|
| 403 |
+
def map_range(
|
| 404 |
+
x: float,
|
| 405 |
+
domain: tuple[float | int, float | int],
|
| 406 |
+
range: tuple[float | int, float | int],
|
| 407 |
+
):
|
| 408 |
+
a, b = domain
|
| 409 |
+
c, d = range
|
| 410 |
+
if not (a <= x <= b):
|
| 411 |
+
raise ValueError(f"x={x:.3f} must be in {[a, b]}.")
|
| 412 |
+
return c + (x - a) * (d - c) / (b - a)
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
@jaxtyped(typechecker=beartype.beartype)
|
| 416 |
+
def add_highlights(
|
| 417 |
+
img: Image.Image,
|
| 418 |
+
patches: Float[np.ndarray, " n_patches"],
|
| 419 |
+
*,
|
| 420 |
+
upper: float | None = None,
|
| 421 |
+
opacity: float = 0.9,
|
| 422 |
+
) -> Image.Image:
|
| 423 |
+
if not len(patches):
|
| 424 |
+
return img
|
| 425 |
+
|
| 426 |
+
iw_np, ih_np = int(math.sqrt(len(patches))), int(math.sqrt(len(patches)))
|
| 427 |
+
iw_px, ih_px = img.size
|
| 428 |
+
pw_px, ph_px = iw_px // iw_np, ih_px // ih_np
|
| 429 |
+
assert iw_np * ih_np == len(patches)
|
| 430 |
+
|
| 431 |
+
# Create a transparent overlay
|
| 432 |
+
overlay = Image.new("RGBA", img.size, (0, 0, 0, 0))
|
| 433 |
+
draw = ImageDraw.Draw(overlay)
|
| 434 |
+
|
| 435 |
+
# Using semi-transparent red (255, 0, 0, alpha)
|
| 436 |
+
for p, val in enumerate(patches):
|
| 437 |
+
assert upper is not None
|
| 438 |
+
val /= upper + 1e-9
|
| 439 |
+
x_np, y_np = p % iw_np, p // ih_np
|
| 440 |
+
draw.rectangle(
|
| 441 |
+
[
|
| 442 |
+
(x_np * pw_px, y_np * ph_px),
|
| 443 |
+
(x_np * pw_px + pw_px, y_np * ph_px + ph_px),
|
| 444 |
+
],
|
| 445 |
+
fill=(int(val * 256), 0, 0, int(opacity * val * 256)),
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
# Composite the original image and the overlay
|
| 449 |
+
return Image.alpha_composite(img.convert("RGBA"), overlay)
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
#############
|
| 453 |
+
# Interface #
|
| 454 |
+
#############
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
with gr.Blocks() as demo:
|
| 458 |
+
image_number = gr.Number(label="Test Example", precision=0)
|
| 459 |
+
class_number = gr.Number(label="Test Class", precision=0)
|
| 460 |
+
input_image = gr.Image(label="Input Image")
|
| 461 |
+
get_input_image_btn = gr.Button(value="Get Input Image")
|
| 462 |
+
get_input_image_btn.click(
|
| 463 |
+
get_image,
|
| 464 |
+
inputs=[image_number],
|
| 465 |
+
outputs=[input_image, class_number],
|
| 466 |
+
api_name="get-image",
|
| 467 |
+
)
|
| 468 |
+
get_random_class_image_btn = gr.Button(value="Get Random Class Image")
|
| 469 |
+
get_input_image_btn.click(
|
| 470 |
+
get_random_class_image,
|
| 471 |
+
inputs=[image_number],
|
| 472 |
+
outputs=[input_image],
|
| 473 |
+
api_name="get-random-class-image",
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
patch_numbers = gr.CheckboxGroup(
|
| 477 |
+
label="Image Patch", choices=list(range(n_patches_per_img))
|
| 478 |
+
)
|
| 479 |
+
top_latent_numbers = gr.CheckboxGroup(label="Top Latents")
|
| 480 |
+
top_latent_numbers = [
|
| 481 |
+
gr.Number(label=f"Top Latents #{j + 1}", precision=0)
|
| 482 |
+
for j in range(n_sae_latents)
|
| 483 |
+
]
|
| 484 |
+
sae_example_images = [
|
| 485 |
+
gr.Image(label=f"Latent #{j}, Example #{i + 1}")
|
| 486 |
+
for i in range(n_sae_examples)
|
| 487 |
+
for j in range(n_sae_latents)
|
| 488 |
+
]
|
| 489 |
+
get_sae_examples_btn = gr.Button(value="Get SAE Examples")
|
| 490 |
+
get_sae_examples_btn.click(
|
| 491 |
+
get_sae_examples,
|
| 492 |
+
inputs=[image_number, patch_numbers],
|
| 493 |
+
outputs=sae_example_images + top_latent_numbers,
|
| 494 |
+
api_name="get-sae-examples",
|
| 495 |
+
)
|
| 496 |
+
|
| 497 |
+
pred_dist = gr.Label(label="Pred. Dist.")
|
| 498 |
+
get_pred_dist_btn = gr.Button(value="Get Pred. Distribution")
|
| 499 |
+
get_pred_dist_btn.click(
|
| 500 |
+
get_pred_dist,
|
| 501 |
+
inputs=[image_number],
|
| 502 |
+
outputs=[pred_dist],
|
| 503 |
+
api_name="get-preds",
|
| 504 |
+
)
|
| 505 |
+
|
| 506 |
+
latent_numbers = [gr.Number(label=f"Latent {i + 1}", precision=0) for i in range(3)]
|
| 507 |
+
value_sliders = [
|
| 508 |
+
gr.Slider(label=f"Value {i + 1}", minimum=-10, maximum=10) for i in range(3)
|
| 509 |
+
]
|
| 510 |
+
get_modified_dist_btn = gr.Button(value="Get Modified Label")
|
| 511 |
+
get_modified_dist_btn.click(
|
| 512 |
+
get_modified_dist,
|
| 513 |
+
inputs=[image_number, patch_numbers] + latent_numbers + value_sliders,
|
| 514 |
+
outputs=[pred_dist],
|
| 515 |
+
api_name="get-modified",
|
| 516 |
+
)
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
if __name__ == "__main__":
|
| 520 |
+
demo.launch()
|
ckpts/cfg.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"data": {
|
| 3 |
+
"shard_root": "/local/scratch/stevens.994/cache/saev/50149a5a12c70d378dc38f1976d676239839b591cadbfc9af5c84268ac30a868/",
|
| 4 |
+
"patches": "patches",
|
| 5 |
+
"layer": -2,
|
| 6 |
+
"clamp": 100000.0,
|
| 7 |
+
"n_random_samples": 524288,
|
| 8 |
+
"scale_mean": true,
|
| 9 |
+
"scale_norm": true
|
| 10 |
+
},
|
| 11 |
+
"n_workers": 32,
|
| 12 |
+
"n_patches": 100000000,
|
| 13 |
+
"sae": {
|
| 14 |
+
"d_vit": 768,
|
| 15 |
+
"exp_factor": 32,
|
| 16 |
+
"sparsity_coeff": 0.0016,
|
| 17 |
+
"n_reinit_samples": 524288,
|
| 18 |
+
"ghost_grads": false,
|
| 19 |
+
"remove_parallel_grads": true,
|
| 20 |
+
"normalize_w_dec": true,
|
| 21 |
+
"seed": 159
|
| 22 |
+
},
|
| 23 |
+
"n_sparsity_warmup": 500,
|
| 24 |
+
"lr": 0.001,
|
| 25 |
+
"n_lr_warmup": 500,
|
| 26 |
+
"sae_batch_size": 16384,
|
| 27 |
+
"track": true,
|
| 28 |
+
"wandb_project": "saev",
|
| 29 |
+
"tag": "baseline-v4.7",
|
| 30 |
+
"log_every": 25,
|
| 31 |
+
"ckpt_path": "./checkpoints",
|
| 32 |
+
"device": "cuda",
|
| 33 |
+
"seed": 59,
|
| 34 |
+
"slurm": false,
|
| 35 |
+
"slurm_acct": "PAS2136",
|
| 36 |
+
"log_to": "./logs"
|
| 37 |
+
}
|
ckpts/clf.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b672e36e8718d6593be0ccf6fbf8a956799e4ce16e6cc3591f340942c129da5b
|
| 3 |
+
size 616642
|
ckpts/sae.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:62cb213777f231ba2de3eaf4a6fd8410e2b9d9f6c95a53dbe0167dd445d1f283
|
| 3 |
+
size 151098370
|
data/image_fpaths.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
data/image_labels.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 107, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 109, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 111, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 112, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 113, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 114, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 118, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 152, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 154, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 155, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 157, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 158, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 161, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 162, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 163, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 164, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 166, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 169, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 171, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 172, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 173, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 174, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 176, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 177, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 178, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 181, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 182, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 183, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 184, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 186, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 187, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 188, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 189, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 194, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 195, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 196, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 198, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199]
|
justfile
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
build: lint
|
| 2 |
+
uv pip compile pyproject.toml > requirements.txt
|
| 3 |
+
|
| 4 |
+
lint: fmt
|
| 5 |
+
git ls-files "*.py" --cached --others --exclude-standard | xargs uv run ruff check
|
| 6 |
+
|
| 7 |
+
fmt:
|
| 8 |
+
git ls-files "*.py" --cached --others --exclude-standard | xargs uv run isort
|
| 9 |
+
git ls-files "*.py" --cached --others --exclude-standard | xargs uv run ruff format --preview
|
pyproject.toml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "saev-image-classification"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Gradio app space for image classification with SAEs"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.12"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"beartype>=0.19.0",
|
| 9 |
+
"einops>=0.8.0",
|
| 10 |
+
"gradio>=5.0.0",
|
| 11 |
+
"jaxtyping>=0.2.36",
|
| 12 |
+
"numpy>=1.26.4",
|
| 13 |
+
"pillow>=10.4.0",
|
| 14 |
+
"torch>=2.4.0",
|
| 15 |
+
"torchvision>=0.19.0",
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
[tool.ruff.lint]
|
| 19 |
+
ignore = ["F722"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# This file was autogenerated by uv via the following command:
|
| 2 |
+
# uv pip compile pyproject.toml
|
| 3 |
+
aiofiles==23.2.1
|
| 4 |
+
# via gradio
|
| 5 |
+
annotated-types==0.7.0
|
| 6 |
+
# via pydantic
|
| 7 |
+
anyio==4.8.0
|
| 8 |
+
# via
|
| 9 |
+
# gradio
|
| 10 |
+
# httpx
|
| 11 |
+
# starlette
|
| 12 |
+
beartype==0.19.0
|
| 13 |
+
# via saev-image-classification (pyproject.toml)
|
| 14 |
+
certifi==2024.12.14
|
| 15 |
+
# via
|
| 16 |
+
# httpcore
|
| 17 |
+
# httpx
|
| 18 |
+
# requests
|
| 19 |
+
charset-normalizer==3.4.1
|
| 20 |
+
# via requests
|
| 21 |
+
click==8.1.8
|
| 22 |
+
# via
|
| 23 |
+
# typer
|
| 24 |
+
# uvicorn
|
| 25 |
+
einops==0.8.0
|
| 26 |
+
# via saev-image-classification (pyproject.toml)
|
| 27 |
+
fastapi==0.115.6
|
| 28 |
+
# via gradio
|
| 29 |
+
ffmpy==0.5.0
|
| 30 |
+
# via gradio
|
| 31 |
+
filelock==3.16.1
|
| 32 |
+
# via
|
| 33 |
+
# huggingface-hub
|
| 34 |
+
# torch
|
| 35 |
+
# triton
|
| 36 |
+
fsspec==2024.12.0
|
| 37 |
+
# via
|
| 38 |
+
# gradio-client
|
| 39 |
+
# huggingface-hub
|
| 40 |
+
# torch
|
| 41 |
+
gradio==5.9.1
|
| 42 |
+
# via saev-image-classification (pyproject.toml)
|
| 43 |
+
gradio-client==1.5.2
|
| 44 |
+
# via gradio
|
| 45 |
+
h11==0.14.0
|
| 46 |
+
# via
|
| 47 |
+
# httpcore
|
| 48 |
+
# uvicorn
|
| 49 |
+
httpcore==1.0.7
|
| 50 |
+
# via httpx
|
| 51 |
+
httpx==0.28.1
|
| 52 |
+
# via
|
| 53 |
+
# gradio
|
| 54 |
+
# gradio-client
|
| 55 |
+
# safehttpx
|
| 56 |
+
huggingface-hub==0.27.1
|
| 57 |
+
# via
|
| 58 |
+
# gradio
|
| 59 |
+
# gradio-client
|
| 60 |
+
idna==3.10
|
| 61 |
+
# via
|
| 62 |
+
# anyio
|
| 63 |
+
# httpx
|
| 64 |
+
# requests
|
| 65 |
+
jaxtyping==0.2.36
|
| 66 |
+
# via saev-image-classification (pyproject.toml)
|
| 67 |
+
jinja2==3.1.5
|
| 68 |
+
# via
|
| 69 |
+
# gradio
|
| 70 |
+
# torch
|
| 71 |
+
markdown-it-py==3.0.0
|
| 72 |
+
# via rich
|
| 73 |
+
markupsafe==2.1.5
|
| 74 |
+
# via
|
| 75 |
+
# gradio
|
| 76 |
+
# jinja2
|
| 77 |
+
mdurl==0.1.2
|
| 78 |
+
# via markdown-it-py
|
| 79 |
+
mpmath==1.3.0
|
| 80 |
+
# via sympy
|
| 81 |
+
networkx==3.4.2
|
| 82 |
+
# via torch
|
| 83 |
+
numpy==2.2.1
|
| 84 |
+
# via
|
| 85 |
+
# saev-image-classification (pyproject.toml)
|
| 86 |
+
# gradio
|
| 87 |
+
# pandas
|
| 88 |
+
# torchvision
|
| 89 |
+
nvidia-cublas-cu12==12.4.5.8
|
| 90 |
+
# via
|
| 91 |
+
# nvidia-cudnn-cu12
|
| 92 |
+
# nvidia-cusolver-cu12
|
| 93 |
+
# torch
|
| 94 |
+
nvidia-cuda-cupti-cu12==12.4.127
|
| 95 |
+
# via torch
|
| 96 |
+
nvidia-cuda-nvrtc-cu12==12.4.127
|
| 97 |
+
# via torch
|
| 98 |
+
nvidia-cuda-runtime-cu12==12.4.127
|
| 99 |
+
# via torch
|
| 100 |
+
nvidia-cudnn-cu12==9.1.0.70
|
| 101 |
+
# via torch
|
| 102 |
+
nvidia-cufft-cu12==11.2.1.3
|
| 103 |
+
# via torch
|
| 104 |
+
nvidia-curand-cu12==10.3.5.147
|
| 105 |
+
# via torch
|
| 106 |
+
nvidia-cusolver-cu12==11.6.1.9
|
| 107 |
+
# via torch
|
| 108 |
+
nvidia-cusparse-cu12==12.3.1.170
|
| 109 |
+
# via
|
| 110 |
+
# nvidia-cusolver-cu12
|
| 111 |
+
# torch
|
| 112 |
+
nvidia-nccl-cu12==2.21.5
|
| 113 |
+
# via torch
|
| 114 |
+
nvidia-nvjitlink-cu12==12.4.127
|
| 115 |
+
# via
|
| 116 |
+
# nvidia-cusolver-cu12
|
| 117 |
+
# nvidia-cusparse-cu12
|
| 118 |
+
# torch
|
| 119 |
+
nvidia-nvtx-cu12==12.4.127
|
| 120 |
+
# via torch
|
| 121 |
+
orjson==3.10.13
|
| 122 |
+
# via gradio
|
| 123 |
+
packaging==24.2
|
| 124 |
+
# via
|
| 125 |
+
# gradio
|
| 126 |
+
# gradio-client
|
| 127 |
+
# huggingface-hub
|
| 128 |
+
pandas==2.2.3
|
| 129 |
+
# via gradio
|
| 130 |
+
pillow==11.1.0
|
| 131 |
+
# via
|
| 132 |
+
# saev-image-classification (pyproject.toml)
|
| 133 |
+
# gradio
|
| 134 |
+
# torchvision
|
| 135 |
+
pydantic==2.10.4
|
| 136 |
+
# via
|
| 137 |
+
# fastapi
|
| 138 |
+
# gradio
|
| 139 |
+
pydantic-core==2.27.2
|
| 140 |
+
# via pydantic
|
| 141 |
+
pydub==0.25.1
|
| 142 |
+
# via gradio
|
| 143 |
+
pygments==2.19.1
|
| 144 |
+
# via rich
|
| 145 |
+
python-dateutil==2.9.0.post0
|
| 146 |
+
# via pandas
|
| 147 |
+
python-multipart==0.0.20
|
| 148 |
+
# via gradio
|
| 149 |
+
pytz==2024.2
|
| 150 |
+
# via pandas
|
| 151 |
+
pyyaml==6.0.2
|
| 152 |
+
# via
|
| 153 |
+
# gradio
|
| 154 |
+
# huggingface-hub
|
| 155 |
+
requests==2.32.3
|
| 156 |
+
# via huggingface-hub
|
| 157 |
+
rich==13.9.4
|
| 158 |
+
# via typer
|
| 159 |
+
ruff==0.8.6
|
| 160 |
+
# via gradio
|
| 161 |
+
safehttpx==0.1.6
|
| 162 |
+
# via gradio
|
| 163 |
+
semantic-version==2.10.0
|
| 164 |
+
# via gradio
|
| 165 |
+
setuptools==75.7.0
|
| 166 |
+
# via torch
|
| 167 |
+
shellingham==1.5.4
|
| 168 |
+
# via typer
|
| 169 |
+
six==1.17.0
|
| 170 |
+
# via python-dateutil
|
| 171 |
+
sniffio==1.3.1
|
| 172 |
+
# via anyio
|
| 173 |
+
starlette==0.41.3
|
| 174 |
+
# via
|
| 175 |
+
# fastapi
|
| 176 |
+
# gradio
|
| 177 |
+
sympy==1.13.1
|
| 178 |
+
# via torch
|
| 179 |
+
tomlkit==0.13.2
|
| 180 |
+
# via gradio
|
| 181 |
+
torch==2.5.1
|
| 182 |
+
# via
|
| 183 |
+
# saev-image-classification (pyproject.toml)
|
| 184 |
+
# torchvision
|
| 185 |
+
torchvision==0.20.1
|
| 186 |
+
# via saev-image-classification (pyproject.toml)
|
| 187 |
+
tqdm==4.67.1
|
| 188 |
+
# via huggingface-hub
|
| 189 |
+
triton==3.1.0
|
| 190 |
+
# via torch
|
| 191 |
+
typer==0.15.1
|
| 192 |
+
# via gradio
|
| 193 |
+
typing-extensions==4.12.2
|
| 194 |
+
# via
|
| 195 |
+
# anyio
|
| 196 |
+
# fastapi
|
| 197 |
+
# gradio
|
| 198 |
+
# gradio-client
|
| 199 |
+
# huggingface-hub
|
| 200 |
+
# pydantic
|
| 201 |
+
# pydantic-core
|
| 202 |
+
# torch
|
| 203 |
+
# typer
|
| 204 |
+
tzdata==2024.2
|
| 205 |
+
# via pandas
|
| 206 |
+
urllib3==2.3.0
|
| 207 |
+
# via requests
|
| 208 |
+
uvicorn==0.34.0
|
| 209 |
+
# via gradio
|
| 210 |
+
websockets==14.1
|
| 211 |
+
# via gradio-client
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|