Skip to main content

Count the MACs / FLOPs of PyTorch models

Project description

TorchProfile

PyPI License PyTorch

TorchProfile counts the number of MACs (multiply-accumulate operations) in a PyTorch model. It works by tracing the computation graph with torch.jit.trace, making it more accurate than hook-based profilers and more general than ONNX-based ones.

Installation

pip install torchprofile

Quick Start

import torch
from transformers import AutoModel
from torchprofile import profile_macs

model = AutoModel.from_pretrained("meta-llama/Llama-3.2-1B").eval()
inputs = torch.randint(0, model.config.vocab_size, (1, 128))

macs = profile_macs(model, inputs)
print(f"{macs / 1e9:.2f} GMACs")

To get a per-operator breakdown, pass reduction=None:

results = profile_macs(model, inputs, reduction=None)
for node, macs in results.items():
    if macs > 0:
        print(f"{node.scope:40s} {node.operator:30s} {macs / 1e6:>8.2f} MMACs")

License

This repository is released under the MIT license. See LICENSE for additional details.

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

torchprofile-0.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

torchprofile-0.1.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: torchprofile-0.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for torchprofile-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aeb725e9446b3a6d8f82d0f0cf944a6ffb9cced4e5db1354ad3b9692ce820dac
MD5 b65e706e24800cdff91b9e5fb160fc06
BLAKE2b-256 0339564175b0147da5f712702209de3904cf97de492a115943c75af1b1b56d82

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchprofile-0.1.0.tar.gz:

Publisher: publish-to-pypi.yml on zhijian-liu/torchprofile

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

File details

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

File metadata

  • Download URL: torchprofile-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for torchprofile-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39aefcd194927befb0cfe7ed886be565dee384e3889d0b6890bace5ae1614578
MD5 114b5fb58987423c26fa8c789cb485e7
BLAKE2b-256 137682c336b80fcd19ad9eedd7c17e459d69a0024aed7229dbcf663c975c5ded

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchprofile-0.1.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on zhijian-liu/torchprofile

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