Skip to main content

pt-snap-cli

中文文档 | English

A command-line tool for analyzing PyTorch memory snapshots. Set a snapshot database, run built-in queries, and inspect memory usage, leaks, and timelines.

Installation

pip install -e .

Quick Start

To start from a raw PyTorch memory snapshot, import the pickle into a SnapshotDB with the built-in backend, then list the available query templates:

pt-snap import snapshot.pkl
pt-snap query --list
# Set the snapshot database and device
pt-snap focus examples/snapshot_expandable.pkl.db --device 0

# List available queries
pt-snap query --list

# Run a query (automatically uses the focused device)
pt-snap query --template-use memory_peak

# Detect potential memory leaks
pt-snap query --template-use leak_detection --params '{"min_size": 1024}'

See the full quick start guide for a walkthrough.

Commands

Command Description
pt-snap focus Set and manage analysis focus (database + device)
pt-snap import <snapshot.pkl> Import a PyTorch memory snapshot pickle into a SnapshotDB
pt-snap query Run memory analysis queries
pt-snap config Manage global configuration
pt-snap-mcp Start the MCP server for agent integration

MCP Server

pt-snap-cli provides an MCP (Model Context Protocol) server so AI agents can interact with PyTorch memory snapshots programmatically.

# Start the MCP server
pt-snap-mcp

The server exposes the following tools:

Tool Description
get_focus Get the current analysis focus
set_focus Set focus to a database and optional device
list_templates List available query templates
get_template_info Get template details and parameters
execute_query Run a query template against the focused database

See the MCP guide for setup and usage details.

Documentation

Topic Guide
Getting started Quick Start
Managing focus Focus Management
Running queries Querying
MCP server MCP Guide
Database format SnapshotDB Schema
Python API ResultMapper API

Query Templates

6 built-in templates across 3 categories:

  • Basic: block, event, allocation
  • Statistical: callstack_analysis, memory_peak
  • Business: leak_detection

See Querying for details.

Project Structure

pt-snap-cli/
├── src/
│   └── pt_snap_cli/
│       ├── cli.py              # CLI entry point
│       ├── context.py          # Database context manager
│       ├── config.py           # Focus management
│       ├── api.py              # Python API layer
│       ├── query/
│       │   ├── builder.py      # Query builder
│       │   ├── executor.py     # Query executor
│       │   ├── mapper.py       # Result mapper
│       │   ├── registry.py     # Query registry
│       │   ├── condition.py    # Query conditions
│       │   ├── config.py       # Query configuration
│       │   └── templates/      # Query templates
│       ├── models/             # Data models
│       └── mcp/                # MCP server for agent integration
├── tests/                  # Test files
├── examples/               # Example data
└── docs/                   # Documentation

Development

pytest                           # Run all tests
./tests/run_tests.sh             # Full test run with coverage
black . && ruff check .          # Format and lint
python -m build                  # Build sdist and wheel

CLI/MCP semantic contract

The CLI and MCP server must keep using the shared API/core services as the single semantic source of truth. The CLI may render results as terminal text and the MCP server may return JSON-like tool payloads, but normalized meanings must stay in sync for focus handling, query template listing, template metadata, query execution, and shared error cases.

tests/test_contract_cli_mcp.py compares normalized CLI output with MCP tool results on the same inputs and fixtures. When adding a new shared CLI/MCP capability, update or add a contract case in that file so CI fails if either adapter drifts from the shared behavior.

Metadata

Release files for pt-snap-cli 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pt-snap-cli 0.1.1
File Size Uploaded
pt_snap_cli-0.1.1.tar.gz 250.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pt-snap-cli 0.1.1
File Interpreter ABI Platform
pt_snap_cli-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 330.4 kB

Release files / pt_snap_cli-0.1.1.tar.gz

Download URL pt_snap_cli-0.1.1.tar.gz
Size 250.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7574960731372418249d3fa75dca38136ccbc9061343f21e109c873e03c9bdb2
BLAKE2b-256 checksum
How to use checksums
e32e63ab327184f73cf5bbaeb14629b8716d46382f5215e2dfbfed9453abf69b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 22, 2026.

Transparency log

Release files / pt_snap_cli-0.1.1-py3-none-any.whl

Download URL pt_snap_cli-0.1.1-py3-none-any.whl
Size 80.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
382eec9e2d7aab08336d5053ac6207ffa08562d814c450fc0d570b5d988b18e1
BLAKE2b-256 checksum
How to use checksums
3b6bdbaa3593c7cb92573ca9b90b44657a7d9db2a6196c6246f2aaeaf7ae780d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 22, 2026.

Transparency log

Release history Release notifications | RSS feed

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

This release

0.1.1 This release

2 release 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