Show HN: LLM Attention Visualization
Recorded: Sept. 8, 2026, 5:08 p.m.
| Original | Summarized |
LLM Attention Visualization ishamf.dev Home Sign in LLM Attention Visualization Isham Faizal Sep 7, 2026 One interesting thing about transformer-based large language models are that, during the generation phase, it is able to draw information from any of its previous tokens. But it needs to be selective; if every token affects the generation equally, it won't be very effective. This process needs a mechanism to decide how much a token affects the next token.Turns out, we can visualize this mechanism!You can tap or hover over any of the generated tokens to see the past tokens that affected* the generation. Loading... (JavaScript required) * "Affected" might not be fully accurate, as this visualization is highly simplified. It's calculating the attention weight, scaled by the magnitude of the value vector, aggregated across all attention heads, and summed across all layers. This is then used to control the opacity of the previous tokens. The largest values always have an opacity of 1 and the rest are interpolated. Share this post Facebook X Reddit LinkedIn Daylight and Timezone Globe An interactive map to convert between time zones, with a daylight visualization, rendered on a globe. May 4, 2025 Daylight and Timezone Map An interactive map to convert between time zones, with a daylight visualization. Mar 30, 2025 IP Block Subtraction Calculator A tool to subtract a set of IP blocks from another, primarily used to help write WireGuard config. Dec 30, 2024 ishamf.dev © 2026 RSS Powered by Ghost |
The visualization of attention in large language models addresses the mechanism by which these models draw information from previous tokens during the generation phase, aiming to understand the selective influence of past context on subsequent token prediction. The core hypothesis explored is that for the generation process to be effective, the mechanism must decide how much influence each preceding token exerts; visualizing this mechanism allows users to examine which prior tokens affected specific generated tokens. This visualization achieves this by calculating attention weight, scaling it by the magnitude of the value vector aggregated across all attention heads and layers, and then using these aggregated values to control the opacity of the antecedent tokens, where the largest values are rendered with full opacity while others are interpolated. This mechanism reveals crucial insights into how LLMs operate. For instance, in prompts like the "Office Move Summary," hovering over verbatim content, such as addresses or dates, demonstrates that the generated token utilized a significant amount of information directly from the source data. This observation challenges the notion that probabilistic prediction alone accounts for this level of fidelity, suggesting that the selective retrieval mechanism minimizes the probability of random errors by allowing the model to deliberately draw specific context when copying. Furthermore, the visualization exposes complex contextual blending; for example, hovering over a word like "remain" can reveal that it drew contextual information from distinct phrases within the input prompt, such as combining concepts from clauses related to access cards and phone numbers. The implementation of this visualization presented several technical challenges. While the interface itself was constructed using React and Transformers.js, the standard generation loop was insufficient for extracting the necessary internal data. To address this, the author implemented a workaround by pre-generating prompts, allowing for instant loading and viewing of results. A significant hurdle involved accessing the model's internal computations, as the use of onnx files and wasm implementation in Transformers.js made accessing internal values difficult. Consequently, the author was required to modify the onnx file to expose these specific internal values, which necessitated uploading a separately instrumented model to a repository to ensure that the visualization could function correctly within a browser environment. This process underscores the complexity of extracting deep operational details from opaque transformer architectures. |