Skip to content

Add webapp for angle calculation#813

Merged
gkielian merged 3 commits into
ReaLLMASIC:masterfrom
klei22:add-webapp-for-angle-calculation
May 11, 2026
Merged

Add webapp for angle calculation#813
gkielian merged 3 commits into
ReaLLMASIC:masterfrom
klei22:add-webapp-for-angle-calculation

Conversation

@klei22
Copy link
Copy Markdown
Collaborator

@klei22 klei22 commented May 11, 2026

This pull request introduces a new Streamlit web application for interactively exploring the LM-head vector geometry of the Gemma 3 270M language model. The webapp allows users to inspect pairwise angles and vector magnitudes between vocabulary tokens, as well as to search and sort tokens by geometric relationships. Documentation for the new tool is also added to the model's README.

New LM-head angle exploration tool:

  • Added lm_head_angle_webapp.py, a Streamlit application to compute and visualize angles and magnitudes between LM-head vectors for any two tokens, and to list all vocabulary tokens sorted by angle to a selected anchor token. Features include efficient token search (by text or ID), CUDA support, and CSV export of results.

Documentation update:

  • Updated README.md with usage instructions, feature descriptions, and notes for the new LM-head angle webapp, including installation and launch guidance.

@klei22 klei22 requested review from Copilot and gkielian May 11, 2026 14:39
@gkielian gkielian merged commit 2209c6f into ReaLLMASIC:master May 11, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new Streamlit-based web tool to interactively explore Gemma 3 270M’s LM-head geometry (pairwise token angles/magnitudes and full-vocab angle neighborhoods), and documents how to run it from the model folder.

Changes:

  • Added a Streamlit app to compute pairwise LM-head angles and generate full-vocab angle-sorted neighborhoods with CSV export.
  • Extended the Gemma 270M README with usage instructions and feature notes for the new webapp.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
huggingface_model/gemma/270M/README.md Documents the new LM-head angle exploration Streamlit app and how to launch it.
huggingface_model/gemma/270M/lm_head_angle_webapp.py Implements the Streamlit UI and LM-head angle/neighborhood computations over the model vocabulary.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +68 to +71
matches = [info for info in infos if q in info.normalized]
return matches[:max_results]


Comment on lines +109 to +120
if method == "Enter token id":
token_id = st.number_input(
f"{prefix} token id",
min_value=0,
max_value=len(infos) - 1,
value=0,
step=1,
key=f"{prefix}_token_id",
)
selected_id = int(token_id)
st.caption(f"Selected: {label(infos[selected_id])}")
return "", selected_id
Comment on lines +148 to +151
device = st.selectbox("Device", ["cpu", "cuda"], index=0)
if device == "cuda" and not torch.cuda.is_available():
st.warning("CUDA not available in this runtime; using CPU.")
device = "cpu"
Comment on lines +50 to +62
@lru_cache(maxsize=4)
def load_model_assets(model_name: str, device: str):
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, attn_implementation="eager")
model.to(device)
model.eval()

weight = model.lm_head.weight.detach().to(device=device, dtype=torch.float32)
norms = torch.linalg.norm(weight, dim=1)
token_infos = _build_token_infos(tokenizer)

return tokenizer, token_infos, weight, norms

Comment on lines +193 to +196
df = neighborhood(weight, norms, anchor_id)
df["token_raw"] = [infos[i].raw for i in df["token_id"].tolist()]
df["token_display"] = [infos[i].display for i in df["token_id"].tolist()]
st.dataframe(df.head(int(top_n)), use_container_width=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants