Skip to content

Basal viz - #2

Merged
rkinas merged 2 commits into
rkinas:mainfrom
lukaspoloki:basal-viz
Oct 19, 2026
Merged

rkinas merged 2 commits into
rkinas:mainfrom
lukaspoloki:basal-viz

Conversation

@lukaspoloki

@lukaspoloki lukaspoloki commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

merge after #1 is merged

a simple local page that shows every step of a decision with many examples

Summary by Sourcery

Add Apple Silicon support and a local visualizer for inspecting basal decision-making end to end.

New Features:

  • Add the basal-viz local web interface for exploring model decisions step by step, including prompts, option-order packing, layer-wise probabilities, calibration, and confidence thresholds.
  • Add Apple Silicon MPS support with automatic device and serving-mode selection, plus a shared-prefix backend for MPS and CPU execution.

Enhancements:

  • Extend benchmarking to synchronize, identify devices, and report memory usage across CUDA, MPS, and CPU backends.

Build:

  • Expose the basal-viz command and package its web assets.

Documentation:

  • Document Apple Silicon installation, runtime limitations, performance, and the decision visualizer.

Tests:

  • Add coverage validating shared-prefix inference equivalence on CPU and MPS and validating visualizer prompt segmentation.

@sourcery-ai

sourcery-ai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Adds the basal-viz local web visualizer for inspecting every stage of calibrated decisions, backed by a new shared-prefix inference path that runs on Apple Silicon MPS or CPU, with platform-aware serving/benchmarking, documentation, and focused correctness tests.

Sequence diagram for the basal decision visualizer request

sequenceDiagram
    participant User
    participant Viz as basal-viz
    participant Explainer
    participant SharedBackend
    participant Model

    User->>Viz: POST /api/explain
    Viz->>Explainer: explain(body)
    Explainer->>Explainer: explain_question(q)
    Explainer->>Model: lens(ids, letter_ids)
    Model-->>Explainer: per-layer letter probabilities
    Explainer->>SharedBackend: run_shared(groups)
    SharedBackend->>Model: _forward_masked(packed inputs)
    Model-->>SharedBackend: packed probabilities
    SharedBackend-->>Explainer: option-order probabilities
    Explainer-->>Viz: calibrated answer and explanation data
    Viz-->>User: Render prompts, packing, charts, readout, thresholds
Loading

Flow diagram for the visualized calibrated decision

flowchart TD
    Input["State and typed questions"] --> Prompt["Render both option orders"]
    Prompt --> Pack["Pack shared prefix and option tails"]
    Pack --> Lens["Run model and record per-layer logit lens"]
    Lens --> Readout["Read option-letter probabilities"]
    Readout --> Average["Map letters to options and average orders"]
    Average --> Calibrate["Apply temperature calibration"]
    Calibrate --> Threshold["Compare confidence with thresholds"]
    Threshold --> Decision["Answer, auto-accept, or route to a person"]
Loading

File-Level Changes

Change Details Files
Adds a local decision-exploration web application that exposes the model’s full inference path and bundled examples.
  • Adds a CLI entry point and packages the standalone HTML frontend.
  • Implements endpoints for model metadata, examples, and serialized per-question explanations.
  • Records prompts, packed attention layout, per-layer logit-lens probabilities, order-specific readouts, calibration, thresholds, timings, and final decisions.
  • Builds an interactive UI with editable state/questions, example selection, history, responsive charts, attention-mask visualization, tooltips, theme switching, and expected-answer indicators.
basal/viz.py
basal/viz.html
pyproject.toml
README.md
Introduces a non-CUDA shared-prefix backend for MPS and CPU execution and makes device/mode selection platform-aware.
  • Detects CUDA, Apple MPS, or CPU and synchronizes the selected asynchronous device for timing.
  • Adds SharedBackend with packed option orders, attention masks, token-budget batching, shape bucketing, and optional MPS warmup.
  • Makes eager and shared backends accept explicit devices and defaults serving to fast on CUDA or mps otherwise.
  • Extends benchmark modes, memory reporting, device naming, and cleanup for MPS/CPU.
basal/engine.py
basal/server.py
basal/bench.py
README.md
Adds correctness coverage for shared execution and visualization prompt segmentation.
  • Compares packed SharedBackend probabilities with separate forward passes on CPU and available MPS.
  • Verifies visualization segments reconstruct the rendered prompt and preserve semantic segment ordering.
tests/test_engine.py
tests/test_server.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="basal/viz.html" line_range="341" />
<code_context>
+// ---- decide -----------------------------------------------------------------------------------------------------
+let currentGold = null;
+async function decide(gold) {
+  currentGold = gold === undefined ? currentGold : gold;
+  const req = readRequest();
+  if (!Object.keys(req.questions).length) return;
</code_context>
<issue_to_address>
**issue (bug_risk):** After loading a bundled example, editing its state, question text, or options leaves `currentGold` pointing to the example's original answer. The next decision therefore displays a stale “matches the expected answer” or “expected” badge for the modified request.

**Triggers:** When a user loads an example and changes any input before pressing Decide.

**Suggested fix:** Clear `currentGold` whenever the state or question editors change, or only retain it when the request still matches the loaded example.

```suggestion
  currentGold = gold === undefined ? null : gold;
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread basal/viz.html
// ---- decide -----------------------------------------------------------------------------------------------------
let currentGold = null;
async function decide(gold) {
currentGold = gold === undefined ? currentGold : gold;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): After loading a bundled example, editing its state, question text, or options leaves currentGold pointing to the example's original answer. The next decision therefore displays a stale “matches the expected answer” or “expected” badge for the modified request.

Triggers: When a user loads an example and changes any input before pressing Decide.

Suggested fix: Clear currentGold whenever the state or question editors change, or only retain it when the request still matches the loaded example.

Suggested change
currentGold = gold === undefined ? currentGold : gold;
currentGold = gold === undefined ? null : gold;

@lukaspoloki

Copy link
Copy Markdown
Contributor Author
Screen.Recording.2026-09-29.at.16.17.14.mov

rkinas pushed a commit that referenced this pull request Sep 30, 2026
feat: GGUF backend (llama.cpp) + Apple Silicon engine comparison
@rkinas

rkinas commented Oct 14, 2026

Copy link
Copy Markdown
Owner

basal-viz is a great idea and we'd like to merge it. Two things first: (1) MPS is already in main via #4, so please rebase onto v2.0.0 without the MPS commit and use the existing backends; (2) basal-2.0 uses prompt format 2 (up to 255 options; render and letter_ids take fmt from the model's basal.json) and one option order by default. Could you port viz to that and check it with basal-2.0-mini? Then we'll merge it as the optional basal-viz command.

@rkinas

rkinas commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Thanks again for basal-viz. main has moved on (2.1, and 2.2 on Mon 19 Oct). When you have time, please rebase onto v2.2.0 without the MPS commit and port to prompt format 2; once it runs with basal-2.0-mini we'll merge. If it doesn't fit your schedule, no problem: we'll close this in two weeks and you can reopen anytime.

@rkinas
rkinas merged commit 51908d6 into rkinas:main Oct 19, 2026
1 check passed
rkinas pushed a commit that referenced this pull request Oct 19, 2026
docs(strix): --mode strix benchmarks, HARDWARE.md and SERVING.md (stacked on #2)
rkinas added a commit that referenced this pull request Oct 19, 2026
… option orders, calibration and answer

Port of #2 to the current engine without its MPS backend (main has MPS since #4): the page runs a basal-serve Server in process (--mode mps on Apple Silicon, eager elsewhere) and reads its PyTorch model for the per-layer readout. Prompts, option codes (up to 255, format 2 from basal.json) and option orders (one by default for format-2 models; --orders 1|2) come from Server.jobs_for; temperatures per type and option-count bucket and the per-type certified thresholds from the calibration file basal-serve loads; multi questions are explained per label, act and tool by their base type. The code logits are read in fp32 as the engines read them. Each request is also answered by Server.decide and the page shows the difference. The page handles one or two orders and types without a certified threshold; question types the form does not edit (act, tool, tasks) are sent as JSON. Llama-architecture models only (refused otherwise). Docs: SERVING.md, "See how a decision is made". Checked with basal-2.0-mini on MPS and CPU, bundled examples and a multi / act / 30-option request: equal to the served answer for single questions (0 on MPS), within 0.006 for requests with several questions (one packed row over the state, bf16).
rkinas added a commit that referenced this pull request Oct 19, 2026
@rkinas

rkinas commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Thank you for basal-viz! It is merged and ships in basal v2.2.0
(https://github.com/rkinas/basal/releases/tag/v2.2.0) as the basal-viz command, with your commits in the history.

Since main had moved a long way, we did the port ourselves, as discussed in the review:

  • the MPS commit of the branch is not taken (main has MPS since feat: Apple Silicon MLX/MPS, GGUF and Ollama backends #4); the page now runs a basal-serve Server in the
    same process (--mode mps on Apple Silicon, eager elsewhere) and reads its PyTorch model for your per-layer view;
  • prompt format 2 (option codes up to 255, from the model's basal.json), the server's own option orders (one by
    default for basal-2.0, --orders 2 for both), and the temperatures and certified thresholds of the calibration file
    basal-serve loads; multi questions are explained per label;
  • the page also answers each request through the server and shows the difference, so what it explains is what
    basal-serve returns. Your page, the logit-lens chart and the step-by-step layout are as you made them; the texts
    adapt to one or two option orders;
  • checked with basal-2.0-mini on Apple Silicon (MPS) and on the CPU, on all bundled examples. Docs:
    docs/SERVING.md, "See how a decision is made". You are in the README's new "Contributors" section.

If you'd like, we'd be glad to list you in CODEOWNERS for basal/viz.py and basal/viz.html, so that changes there
come to you for review. Entirely optional: just say so here.

@lukaspoloki
lukaspoloki deleted the basal-viz branch October 23, 2026 14:34
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.

2 participants