Add webapp for angle calculation#813
Merged
gkielian merged 3 commits intoMay 11, 2026
Merged
Conversation
gkielian
approved these changes
May 11, 2026
There was a problem hiding this comment.
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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:
README.mdwith usage instructions, feature descriptions, and notes for the new LM-head angle webapp, including installation and launch guidance.