Skip to main content

Analyze PyTorch models to determine limiting factors in each layer.

Project description

Tensorscope

Tensorscope is an extension of the fvcore library created by Facebook that provides additional model analysis. Tensorscope adds the ability to count the number of bytes that are read and written by each operation. Additionally, a function is provided that outputs the limiting factor for each operation in your model, whether that is Arithmetic Intensity or GPU ops:bytes. This can be useful when attempting to optimize your model and identify bottlenecks.

Features

  • Analysis of the number of elements read or written in each operation of a model
  • Analysis of the number of bytes read or written in each operation of a model
  • Calculation of the arithmetic intensity of each operation
  • Calculation of a GPU's ops:bytes ratio
  • Support for various precisions and GPUs
  • Support for various model types and architectures
  • Estimation of the forward pass time for the given model, data, precision, and GPU

Installation

pip install tensorscope

Element Counting

Tensorscope contains a class called ElemCountAnalysis that counts the number of elements read and written in each operation of a model. Combining this information with knowing what precision the operation is executed in provides the number of bytes read and written for each operation. See the fvcore documentation for more information on how it works and ways to extend it or add support for more operations.

import torch
from torchvision.models import resnet18
from tensorscope import ElemCountAnalysis

model = resnet18()
inputs = (torch.randn(32, 3, 224, 224))

elems = ElemCountAnalysis(model, inputs)
print(elems.total)
print(elems.by_module())
print(elems.by_module_and_operator())

Analyzing Operation Limitations (Byte + FLOP counting)

As described in NVIDIA's GPU Performance Background documentation, comparing an algorithm's arithmetic intensity with a GPU's ops:bytes ratio can identify whether an operation is limited by algorithm efficiency or GPU bandwidths. Using the analysis_model function will output a table containing this information.

from torchvision.models import resnet18
from tensorscope import analyze_model

batch_size = 512
model = resnet18()
input_shape = [(batch_size, 3, 224, 224)]
gpu = 'NVIDIA GeForce GTX 1650 Ti'
precision = 'fp32'

analyze_model(model, input_shape, gpu, precision)

Forward Pass Time Estimation

The analysis_model function provides an estimate on the time required for the forward pass. This is based on the time needed for the limiatior in each operation to completed, summed across all operations. Currently this estimate is not very accurate and I would like to improve this in the future.

GPU Specifications

When passing the GPU name to the analysis_model function, there must be the corresponding information located in the gpu_specs.json file. If it does not already exist in there, it can easily be added by following the template provided in the file. The name of the GPU corresponds to the returned value from torch.cuda.get_device_name().

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

tensorscope-1.0.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

tensorscope-1.0.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file tensorscope-1.0.0.tar.gz.

File metadata

  • Download URL: tensorscope-1.0.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for tensorscope-1.0.0.tar.gz
Algorithm Hash digest
SHA256 49f75721e600f6a02ce4f417285d81fbb9bd42bfdc1e273961b6a5d20b5d372c
MD5 568ccf6e8c51bd902093a450d1d78da9
BLAKE2b-256 b55da25a9cf0b29410db4200a19dd04ef8c8674806384eeefd83233c14ac8ca7

See more details on using hashes here.

File details

Details for the file tensorscope-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tensorscope-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for tensorscope-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 351d0e55410c24b99510ea9fa6168ff42de82a9ff3aa23988133f06d45c0aa07
MD5 3b05185ca26b77ff24d18db93e788757
BLAKE2b-256 7100917606032c64884dc1605405a9002458b09c8180afc4a2d22359293cd853

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