Skip to main content

Extract simplex arrays from PyTorch model outputs

Project description

fisher-torch

Extract simplex arrays from PyTorch model outputs.

Built on fisher-simplex for simplex geometry and invariants.

Install

pip install fisher-torch

With transformers support:

pip install fisher-torch[transformers]

Quick start

import torch
from fisher_torch import extract_predictions, capture_forward
from fisher_torch import SamplingPolicy

# From raw logits
logits = torch.randn(10, 50000)  # (seq_len, vocab_size)
simplex = extract_predictions(logits, top_k=50)
# simplex.shape == (10, 51)  — top-50 + remainder bin, valid simplex

# From a HuggingFace model
policy = SamplingPolicy(top_k=50, final_token_only=True)
result = capture_forward(model, input_ids, predictions=True, policy=policy)
# result.predictions is a float64 numpy simplex array
# result.projection_spec describes the projection geometry

Atlas extraction (convenience)

from fisher_torch import extract_for_atlas

attention, seq_lens = extract_for_atlas(model, tokenizer, texts, layers=[0, 15, 31])
# attention.shape == (n_prompts, n_positions, n_layers, n_heads, max_seq_len)
# attention.dtype == np.float64
# seq_lens gives each prompt's valid token count

Multi-prompt batch extraction

from fisher_torch import capture_batch, SamplingPolicy

prompts = [tokenizer.encode(p, return_tensors="pt") for p in texts]
policy = SamplingPolicy(position_preset="atlas")
batch = capture_batch(model, prompts, attention=True, policy=policy)
# batch.aligned_attention.shape ==
#   (n_prompts, n_layers, n_heads, n_positions, max_seq_len)
# batch.metadata["seq_lens"] gives each prompt's valid length

Gradient-enabled extraction

result = capture_forward(
    model, input_ids,
    predictions=True, attention=True,
    no_grad=False,
)
# result.prediction_tensors — torch.Tensor with grad graph intact
# result.attention_tensors  — dict[int, Tensor]
result.detach_to_numpy()  # convert tensor fields to numpy in-place

Raw hidden states

result = capture_forward(
    model, input_ids,
    predictions=False, hidden_states=True, raw_hidden_states=True,
)
# result.hidden_states     — logit-lens projected predictions per layer
# result.raw_hidden_states — raw vectors without lm_head projection

Modules

Module Purpose
extractors Stateless functions: logits, attention, hidden states, routing → simplex arrays
capture capture_forward (single pass), capture_batch (multi-prompt with alignment), extract_for_atlas (convenience)
convert Tensor ↔ numpy simplex conversion, stack_attention, truncate_and_renormalize
sampling SamplingPolicy — layers, heads, positions, presets ("atlas", "quartiles")
utils Numerically stable softmax, top-k simplex projection, device helpers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fisher_torch-0.3.1.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fisher_torch-0.3.1-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file fisher_torch-0.3.1.tar.gz.

File metadata

  • Download URL: fisher_torch-0.3.1.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fisher_torch-0.3.1.tar.gz
Algorithm Hash digest
SHA256 d5c20b95b547d6a1c4b66f353e47fa97d79c2981d7cc9ee7905c04a9d1dba125
MD5 40c63f37508c8e8e09527055770cbd77
BLAKE2b-256 1b9460dc0de7c721d37a1f77b6c8516d2f9a1cc3281e3b0269b1df67103a6c44

See more details on using hashes here.

Provenance

The following attestation bundles were made for fisher_torch-0.3.1.tar.gz:

Publisher: workflow.yml on syzygyhack/fisher-torch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fisher_torch-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: fisher_torch-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fisher_torch-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a61e31686110cec2a8cd5785875673b9b98b4abc664b5619430bcc2271ae3df7
MD5 df3ee9b2907e8faa34606f553f334720
BLAKE2b-256 e076090a859336c80db0316bf4b85f095cf2f1358aa2ca881e7a5eb19b095053

See more details on using hashes here.

Provenance

The following attestation bundles were made for fisher_torch-0.3.1-py3-none-any.whl:

Publisher: workflow.yml on syzygyhack/fisher-torch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page