Skip to main content

Model inspection and summary tools for MLX neural networks

Project description

mlxsummary

Model inspection and summary tools for MLX neural networks on Apple Silicon.

Inspired from and similar to torchsummary for PyTorch, but designed specifically for MLX's module system.

Features

  • 📊 Multiple output formats: Table, Tree, JSON, Markdown, Minimal
  • 🔍 Detailed inspection: Layer paths, parameter counts, shapes
  • 🎯 Filtering: Find layers by type, name pattern, or parameter count
  • 📈 Statistics: Aggregate stats by layer type
  • 🖥️ CLI support: Use from command line or Python
  • 🧊 Freeze-aware: Track trainable vs frozen parameters

Installation

pip install mlxsummary

Or install from source:

git clone https://github.com/dhruvshr/mlxsummary.git
cd mlxsummary
pip install -e .

Quick Start

import mlx.nn as nn
from mlxsummary import summary

# Create a model
model = nn.Sequential(
    nn.Linear(784, 256),
    nn.ReLU(),
    nn.Dropout(0.1),
    nn.Linear(256, 128),
    nn.ReLU(),
    nn.Linear(128, 10),
)

# Print summary
summary(model)

Output:

=============================================================================================================================
 Model Summary: Sequential
=============================================================================================================================
Layer                                         Type                         Params Details                 Trainable
-----------------------------------------------------------------------------------------------------------------------------
layers.0                                      Linear                      200,960 (784 → 256)               200,960
layers.1                                      ReLU                              0                                 0
layers.2                                      Dropout                           0                                 0
layers.3                                      Linear                       32,896 (256 → 128)                32,896
layers.4                                      ReLU                              0                                 0
layers.5                                      Linear                        1,290 (128 → 10)                  1,290
-----------------------------------------------------------------------------------------------------------------------------
Total Parameters:                                                         235,146
Trainable Parameters:                                                     235,146
=============================================================================================================================

Output Formats

Table (default)

summary(model, format="table")

Tree

summary(model, format="tree")
📦 Sequential (235,146 params)
│   ├── 0: Linear (784 → 256) [200,960]
│   ├── 1: ReLU [0]
│   ├── 2: Dropout [0]
│   ├── 3: Linear (256 → 128) [32,896]
│   ├── 4: ReLU [0]
│   ├── 5: Linear (128 → 10) [1,290]

JSON

data = summary(model, format="json", print_output=False)

Markdown

summary(model, format="markdown")

Minimal (one-line)

summary(model, format="minimal")
# Output: Sequential: 235,146 params (6 layers)

Programmatic API

Inspector

For detailed programmatic access:

from mlxsummary import inspect

inspector = inspect(model)

# Get all layers
layers = inspector.get_layers()
for layer in layers:
    print(f"{layer.path}: {layer.total_params:,} params")

# Get statistics
stats = inspector.get_stats()
print(f"Total: {stats.total_params:,}")
print(f"Layer types: {stats.layer_type_counts}")

# Find specific layers
linear_layers = inspector.find_layers(layer_type=nn.Linear)
attention = inspector.find_layers(name_pattern="attention")
large_layers = inspector.find_layers(min_params=10000)

Convenience Functions

from mlxsummary import count_params, get_layers, get_stats, to_dict

# Count parameters
total = count_params(model)
trainable = count_params(model, trainable_only=True)

# Get layers
layers = get_layers(model)
linear_layers = get_layers(model, layer_type=nn.Linear)

# Get stats
stats = get_stats(model)

# Export to dict
data = to_dict(model)

Options

summary(
    model,
    format="table",           # Output format
    show_shapes=True,         # Show layer dimensions
    show_trainable=True,      # Show trainable params column
    show_frozen=False,        # Show frozen params column
    max_depth=None,           # Limit layer depth
    max_rows=None,            # Limit output rows
    include_zero_param=True,  # Include zero-param layers
    width=100,                # Output width
    print_output=True,        # Print vs return only
)

Command Line

# Summarize a model from a file
mlxsummary model.py

# Different formats
mlxsummary model.py --format tree
mlxsummary model.py --format json -o model.json
mlxsummary model.py --format markdown

# Options
mlxsummary model.py --max-depth 2
mlxsummary model.py --hide-zero
mlxsummary model.py --no-shapes

# Demo mode
mlxsummary --demo

Your model file should define a model variable or get_model() function:

# model.py
import mlx.nn as nn

model = nn.Sequential(
    nn.Linear(784, 256),
    nn.ReLU(),
    nn.Linear(256, 10),
)

API Reference

Classes

Class Description
MLXInspector Main inspector for detailed model analysis
LayerInfo Information about a single layer
ModelStats Aggregate statistics about a model
FormatterOptions Options for output formatting
OutputFormat Enum of available output formats

Functions

Function Description
summary(model, ...) Generate and print a model summary
inspect(model) Create an inspector instance
count_params(model) Count model parameters
get_layers(model) Get list of layer information
get_stats(model) Get aggregate statistics
to_dict(model) Export model info to dictionary
tree(model) Shortcut for tree format
table(model) Shortcut for table format

Requirements

  • macOS with Apple Silicon (M1/M2/M3/M4)
  • Python 3.9+
  • MLX 0.1.0+

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or pull request.

See Also

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

mlxsummary-0.1.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

mlxsummary-0.1.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file mlxsummary-0.1.0.tar.gz.

File metadata

  • Download URL: mlxsummary-0.1.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for mlxsummary-0.1.0.tar.gz
Algorithm Hash digest
SHA256 227e04ed060ac1c447a9bd2c7f27cdf19c7b3999027ae46be18ed3eb2e150a1c
MD5 7ed60155a193e6a624f8204856cf1e0f
BLAKE2b-256 d463570184f144eed4548e7868d2a53022c0196f09bd7961cc6c925901699c0f

See more details on using hashes here.

File details

Details for the file mlxsummary-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mlxsummary-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for mlxsummary-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7142cff80eba7427b529180a3e923c860624343aaabbe8aa2c3affecfbbb78f
MD5 d65386bc767b10499950044f8c4ea4ed
BLAKE2b-256 56bbf126ad4637c6210e0b66f8fae2d1edb5c58cc0c879a4dce4f232652a75b2

See more details on using hashes here.

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