Skip to content
Edit this page

Contributing to muscari

Project settings

Setting Source of truth
Purpose and deliverable A Python library and the muscari command: parts for learned sparse retrieval with Sentence Transformers, and SAE training for text encoders. See How it works
Target environments One process on one device: CPU, CUDA or Apple MPS. See What is verified for what has run where
Python Development version in .python-version; supported minimum in pyproject.toml
Tools mise pins uv and Betterleaks in mise.toml
Dependencies Runtime ranges and the saelens, retrieval and llm extras in pyproject.toml. Developer tools use the dev group
Reproducibility uv.lock pins the main development and CI environment
Quality Ruff formatting and lint, mypy, pytest, package build, a strict build of the documentation site, public-content and license audit, Betterleaks
Documentation A short README for users; guides, reference, design notes and measurements in docs/, built with Zensical and published on GitHub Pages; the API reference comes from the docstrings. In English
Distribution Source repository only. No package publishing job is configured
License Apache-2.0; LICENSE and NOTICE are included in the wheel and the sdist

The package version is defined by project.version; the exported __version__ must agree. Changes to the export format, the token selection rule, the loss or a default require matching documentation and behavioural tests.

Setup and validation

Install mise, inspect the checkout configuration, then run:

git clone https://github.com/tkhshtsh0917/muscari.git
cd muscari

mise trust
mise install
mise run setup
mise run check

setup installs the locked dependencies, the saelens, retrieval and llm extras and the dev group. check runs formatting, lint, mypy, the tests, the package build and the audits with uv run --frozen --no-sync, so it never resolves or updates dependencies. For an intentional dependency change, edit pyproject.toml, run mise exec -- uv lock, review the diff, then run setup and check. mise run format applies formatting.

The main CI job runs the same locked setup and check tasks. Separate compatibility jobs resolve the lowest supported versions on Python 3.11 without the SAELens extra, and the newest versions on Python 3.13, record what was installed and run the tests. Actions are pinned to commit SHAs; update the SHA and the version comment together.

The mise environment sets HF_HUB_OFFLINE=1. No check downloads a model or a dataset. For a run that needs the Hugging Face Hub, set the variable inside the mise environment:

mise exec -- env HF_HUB_OFFLINE=0 uv run --frozen --no-sync muscari train-sae ...

Writing HF_HUB_OFFLINE=0 in front of mise exec has no effect, because mise applies mise.toml afterwards.

Tests

Tests at the root of tests/ cover the library with a randomly initialised two-layer BERT and synthetic activations. They check numerics and interfaces: the sparse backward pass against dense autograd and numerical differentiation, exact checkpoint resumption, the export against SAELens' own inference model, and the command line end to end. tests/tooling/ checks the development audits. The SAELens tests and the retrieval-head tests are skipped when their extra is not installed.

On a machine where PyTorch can use CUDA or Apple MPS, run the hardware tests as well:

mise exec -- uv run --frozen --no-sync pytest -q -m accelerator -rs

They compare losses, gradients, activations and the evaluation statistics with the CPU, train a small SAE on the device and, on CUDA, check bfloat16 autocast. They are skipped where neither device exists, which includes hosted CI. Report which groups ran and which were skipped.

Documentation

mise run docs builds the site into site/ with Zensical from zensical.toml. The build fails on a link, anchor or cross-reference that does not resolve, and on a docstring that does not match its signature; check runs it. mise run docs-serve serves it locally with live reload. A push to main publishes the site through the docs workflow.

A page says how to do one thing. Usage goes to docs/guides, facts about commands and files to docs/reference, the reasons for a choice to docs/design, and what was run, with its conditions and limits, to docs/measurements.

Offline example and benchmark

mise run smoke trains and evaluates a tiny SAE with both trainers. It needs no download and is optional: the standard check already runs the example through the tests.

benchmarks/sae_step.py times one optimisation step with the sparse backward pass or with dense autograd:

mise exec -- uv run --frozen --no-sync python benchmarks/sae_step.py --mode sparse
mise exec -- uv run --frozen --no-sync python benchmarks/sae_step.py --mode dense --batch-tokens 2048

It uses random activations, so only time and memory are meaningful. Keep the device, thread count, arguments and library versions with any number you publish, and state what was not run.

Reviews and readable code

Keep responsibilities separate across src/muscari, examples, benchmarks, tests and scripts. Inside the package, code lives in the subpackage of its purpose: data, encoding, sae, analysis, heads, training, evaluation and cli.

Keep files and functions small enough to read in one go: about 100 lines for a file and 20 to 30 for a function, not counting docstrings. A class whose docstring carries its sources may run longer; split by responsibility, not by line count. Use blank lines to form blocks: around if and for statements and between input validation, computation and state changes.

Use one name for one thing everywhere, in the Python API, the command line and the saved files, and take the name from Sentence Transformers where it has one: prompt, query, document, max_seq_length, include_prompt, model_name_or_path. In multiline calls, put one argument per line with trailing commas; keep simple short calls compact. Explain responsibilities, shapes, failure conditions and numerical constraints in docstrings.

Fixes need tests that would fail before the fix, preferably against an independent oracle such as a dense reference or a hand-built SAE with a known answer. Cover relevant boundaries without repeating the same property across every parameter combination. Tests on a random encoder do not establish SAE quality on a real one. Do not suppress a check globally to hide a failure.

Public documents are written in English. Public files must be sufficient without local notes and must not contain personal paths, private links, tokens or keys. Agent-specific rules are in AGENTS.md.

Builds, licensing and secret scanning

mise run build empties dist and produces one wheel and one sdist in it, so that artifacts of an earlier version cannot fail the audit. The secrets task first performs the public-content and license audit, then runs Betterleaks; check invokes this combined task once. mise run audit runs the audit alone.

The audit checks the license metadata and the byte-for-byte LICENSE and NOTICE contents, the typing marker, the public documents and their relative links, and that every sdist member belongs to the public source inventory or to named build-generated metadata. Setuptools 77.0.3 or later is required for PEP 639 metadata. Remove stale artifacts from dist when the package version changes.

Betterleaks uses the pinned built-in rules and scans the public files, the extracted artifacts and all locally available Git refs. Provider validation is disabled; findings and scanner errors fail the task. Redacted reports stay in the ignored build/security/. Shallow clones fail the history check: fetch the missing history before retrying. Pattern detection does not guarantee that no secret exists.

Dependencies retain their own licenses. Preserve required third-party notices if you incorporate their material. Before any package release, confirm hosted CI, inspect the artifacts and their licensing, and follow the maintainer's release process.