Skip to main content

neurarch-trace

Run one forward pass over a PyTorch model and write a .neurarch.json graph with the real input and output shape of every layer. Point neurarch-mcp at that file and every tool works: parameter counts, FLOPs, shape contracts, lint_model, check_design.

neurarch-mcp can already read a .py file by parsing it statically. Static parsing cannot see tensor shapes, and it cannot follow a model that is built at runtime: AutoModel.from_pretrained(...), a timm factory, an architecture spread across a dozen files. neurarch-trace closes that gap by instantiating the model and watching the tensors go through it.

Install

pip install neurarch-trace          # torch >= 2.0
pip install 'neurarch-trace[hf]'    # adds transformers for hf: targets

Usage

neurarch-trace <target> --input 1,3,224,224 [--input 1,128:long ...] [-o out.neurarch.json]
python -m neurarch_trace <target> ...     # same thing

A file and a class or factory in it:

neurarch-trace models/resnet.py:ResNet18 --input 1,3,224,224 -o resnet18.neurarch.json

An importable module and a factory function (called with no arguments):

neurarch-trace my_pkg.model:build_model --input 1,3,224,224

A Hugging Face checkpoint (defaults to --input 1,16 --dtype long, token ids):

neurarch-trace hf:prajjwal1/bert-tiny -o bert-tiny.neurarch.json

Then hand the graph to your agent:

npx -y neurarch-mcp ./resnet18.neurarch.json

<target> may name an nn.Module instance, an nn.Module subclass (instantiated with no arguments), or a callable that returns one. --input is repeated for multi-input forwards; a :dtype suffix (1,128:long) overrides --dtype for that input. Random tensors are used, torch.randn for float dtypes and torch.randint(0, 1000, ...) for integer ones.

Other flags: --name sets the graph name (default: the attribute or repo name), -o - writes to stdout, --depth N stops descending at module depth N and records the modules there as single nodes, --verbose shows the traceback on a failure (otherwise a failure is one line on stderr and exit code 1).

The shape convention

Shapes in the graph are written without the batch dimension: [3, 224, 224] for an image, [128, 768] for a token sequence. The first dim of every --input is the batch and is stripped from every recorded shape. Neurarch reads a leading dimension as the channel axis, so a shape that still carries its batch of 1 would be read as a one-channel tensor and every downstream number would be wrong.

The trace runs in eval() mode on CPU with autograd enabled.

What ends up in the graph

  • One node per leaf module call, typed with the Neurarch vocabulary (conv2d, linear, layerNorm, multiHeadAttention, ...) and carrying the same parameter keys the static parser emits, so the MCP estimators read them. A module called twice is two nodes. Modules the mapping table does not know become customModule with className and paramCount in their params.
  • scope on every node is the dotted path of its parent module (encoder.layer.0.attention), which is what the MCP block tools group by.
  • Edges follow actual data flow. A tensor a layer returns is matched by identity; anything produced by functional code in between (x + residual, torch.cat, reshapes, RoPE math) is traced back through autograd to the layers it came from. A residual add becomes an add node with two inputs, a concatenation a concatenate node.
  • Exactly one input node per --input and one output node.

Limits

  • Functional ops other than add / cat / stack do not get nodes of their own; a torch.flatten or F.softmax between two modules is invisible, and the two modules are wired directly.
  • Attribution runs on the autograd graph, so a forward wrapped in torch.no_grad() or an integer-only path with no learnable parameter behind it falls back to matching by dtype against the model inputs.
  • The batch is assumed to be dim 0. Sequence-first layouts (nn.MultiheadAttention with batch_first=False) come out with the sequence length stripped instead.
  • The mapping table lives in neurarch_trace/mapping.py and mirrors codeParser.ts in the Neurarch app. If a torch module is missing there, it is recorded as customModule rather than guessed.

Download files

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

Source Distribution

neurarch_trace-0.1.1.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

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

neurarch_trace-0.1.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file neurarch_trace-0.1.1.tar.gz.

File metadata

  • Download URL: neurarch_trace-0.1.1.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for neurarch_trace-0.1.1.tar.gz
Algorithm Hash digest
SHA256 eaa985593393062e5362ef085f7c75aa01f4f6aed92e2fdc8b6be3f44c2f8b82
MD5 891928c95693f7c3833e81827d0b6a49
BLAKE2b-256 c266d754ac83acd784e9c482bf2d216e6e2e946489eeb18c3e8680781cde4b38

See more details on using hashes here.

Provenance

The following attestation bundles were made for neurarch_trace-0.1.1.tar.gz:

Publisher: publish-pypi.yml on neurarch-ai/neurarch-mcp

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

File details

Details for the file neurarch_trace-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: neurarch_trace-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for neurarch_trace-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82c00e5f8f7d94d04852cad64fb43687d1c135b6f07bef6dda60522c133a82ee
MD5 78228c24a1ec7f9a493df679a7ff01ed
BLAKE2b-256 2948759e7b79c1916557d87ef4af875851854a51dacfe13edff4ed869b27b6e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for neurarch_trace-0.1.1-py3-none-any.whl:

Publisher: publish-pypi.yml on neurarch-ai/neurarch-mcp

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

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page