Metadata-Version: 2.5
Name: tensor-spectra
Version: 0.3.0
Summary: Reproducible spectral analysis of tensors from Transformer models
Project-URL: Repository, https://github.com/petitquentin/tensor-spectra
Project-URL: Issues, https://github.com/petitquentin/tensor-spectra/issues
Keywords: kv-cache,pytorch,spectral-analysis,transformers
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Requires-Dist: accelerate>=1.0
Requires-Dist: dash>=2.18
Requires-Dist: datasets>=3.0
Requires-Dist: huggingface-hub>=0.26
Requires-Dist: jsonschema>=4.23
Requires-Dist: matplotlib>=3.9
Requires-Dist: numpy>=1.26
Requires-Dist: plotly>=5.24
Requires-Dist: pydantic>=2.9
Requires-Dist: pyyaml>=6.0
Requires-Dist: torch>=2.2
Requires-Dist: transformers>=4.45
Description-Content-Type: text/markdown

# TensorSpectra

TensorSpectra is a reproducible Python framework for extracting user-selected tensor
populations from Hugging Face Transformer models and computing numerical and spectral
descriptors. The KV cache is the flagship target, accompanied by queries, hidden states, MLP
activations, attention maps, and decoder projection weights.

Every run pins the model revision, records the resolved configuration, its hash, and the
environment, and writes a schema-versioned JSON result (plus an optional NPZ sidecar) that the
figure renderer and the interactive explorer read without loading the model.

## Installation

Install the PyPI package (Python 3.10 or newer):

```bash
python -m pip install tensor-spectra==0.2.0
python -m tensor_spectra --help
```

The distribution is named `tensor-spectra`; the Python import is `tensor_spectra`.

## Quick start

Analyze a model (latest `main` revision, resolved to a commit SHA) with schema defaults —
KV-cache K/V, one synthetic sample of 1024 tokens, `bfloat16`, automatic device placement:

```bash
python -m tensor_spectra run --model HuggingFaceTB/SmolLM2-135M
```

A small CPU run with dotted overrides, a YAML config, or a dry run that only prints the plan:

```bash
python -m tensor_spectra run --model HuggingFaceTB/SmolLM2-135M \
  --model.dtype float32 --model.device_map cpu --data.sequence_lengths '[32]'
python -m tensor_spectra run --config tests/configs/tiny.yaml
python -m tensor_spectra run --config tests/configs/tiny.yaml --dry-run
```

Render figures or explore a saved result:

```bash
python -m tensor_spectra figures --input runs/<run_name>/result.json --out figures
python -m tensor_spectra explore --input runs/<run_name>/result.json --port 8050
```

The same pipeline is available from Python:

```python
from tensor_spectra.config import load_config
from tensor_spectra.pipeline import run_pipeline

outcome = run_pipeline(load_config("tests/configs/tiny.yaml"))
print(outcome.output_path)
```

## Documentation

| Document | Contents |
|---|---|
| [docs/architecture.md](docs/architecture.md) | Pipeline, module map, data model, invariants, how to add targets/metrics/figures |
| [docs/configuration.md](docs/configuration.md) | Reproducibility, YAML configuration and overrides, model loading, `run` CLI options |
| [docs/targets.md](docs/targets.md) | Extraction targets (KV cache, queries, hidden states, MLP, attention, weights) and benchmark data |
| [docs/analysis.md](docs/analysis.md) | Spectral engine, orchestration, cross-layer similarity, approximation, result logging |
| [docs/model-analysis-output.md](docs/model-analysis-output.md) | Field-by-field reference of the result document |
| [docs/visualization.md](docs/visualization.md) | Static figure catalogue and the web explorer |
| [docs/figures-and-analysis.md](docs/figures-and-analysis.md) | How to read each figure (local file, not versioned) |
| [docs/examples.md](docs/examples.md) | Real-model example scripts and presets |
| [docs/history/](docs/history/) | Original specification and past audits |

## Development

Install [uv](https://docs.astral.sh/uv/), then create the project environment and install the
locked dependencies:

```bash
uv sync --dev
```

This creates `.venv` and uses the Python version in `.python-version`. Run commands inside the
environment without activating it:

```bash
uv run pytest
uv run ruff check .
uv run ruff format --check .
```

After changing dependencies, run `uv lock` and commit the updated `uv.lock`.

Numerical regressions are guarded by golden-output tests (`tests/test_golden.py`, see
[`tests/golden/README.md`](tests/golden/README.md)); analysis performance is measured with
[`benchmarks/bench_analysis.py`](benchmarks/README.md) on synthetic, model-shaped inputs.

Contributor and AI-agent orientation (commands, layout, conventions) is in [AGENTS.md](AGENTS.md).

### Branches and releases

- `dev` is the integration branch: push work and open feature pull requests against `dev`.
- `main` only receives pull requests from `dev`, merged with a merge commit. Each merge into
  `main` is a release: the `Release` workflow runs the CI, publishes the package to PyPI, then
  tags `vX.Y.Z` and creates the GitHub release.
- Before opening the `dev` → `main` pull request, bump `project.version` in `pyproject.toml`
  above the latest version on PyPI. The `Release gate` checks reject pull requests into `main`
  that do not come from `dev` or that reuse a published version; branch rules on `main` require
  these checks and the CI to pass.
