Skip to main content

Measure execution times of every layer in your pytorch model.

Project description

Pytorch profiler

Pytorchures is a simple model profiler intended for any pytorch model. It measures execution time of model layers individually. Every layer of a model is wrapped with timing class which measures latency when called.

TLDR;

Install

pip install pytorchures

Run

from pytorchures import TimedModule

model = TimedModule(model)

_output_ = model(inputs)

profiling_data = model.get_timings()

with open(profiling_filename, "w") as f:
    json.dump(profiling_data, f, indent=4)

One layer extract of sample output .json

    {
        "module_name": "InvertedResidual",
        "device_type": "cuda",
        "execution_times_ms": [
            5.021333694458008,
            2.427816390991211,
            2.4025440216064453
        ],
        "mean_time_ms": 3.283898035685221,
        "median_time_ms": 2.427816390991211,
        "sub_modules": [
            {
                "module_name": "Sequential",
                "device_type": "cuda",
                "execution_times_ms": [
                    4.198789596557617,
                    1.9135475158691406,
                    1.9412040710449219
                ],
                "mean_time_ms": 2.684513727823893,
                "median_time_ms": 1.9412040710449219,
                "sub_modules": [
                    {
                        "module_name": "Conv2dNormActivation",
                        "device_type": "cuda",
                        "execution_times_ms": [
                            2.0263195037841797,
                            0.7545948028564453,
                            0.9317398071289062
                        ],
                        "mean_time_ms": 1.2375513712565105,
                        "median_time_ms": 0.9317398071289062,
                        "sub_modules": [
                            ...
                                                    

Setup

This repo was developed under WSL 2 running Ubuntu 20.04 LTS, and Ubuntu 22.04 LTS. The editor of choice is VS Code.

Install python

The code was tested for Python 3.11, if you want to run other release please subsitute the python version in commands below which install python and virtual environment.

sudo apt-get update

sudo apt-get install python3.11

sudo apt-get install python3.11-venv

Install for PIL image.show() to work on WSL sudo apt install imagemagick

Install relevant VS Code extentions.

If you choose to use the recommended VS Code as editor please install the extensions from extensions.json.

Create virtual environment

Create venv

python3.11 -m venv .venv

To activate venv type - VS Code should automatically detect your new venv, so select it as your default interpreter.

source venv/bin/activate

Install package in editable mode

In order to be able to develop and run the code install this repo in editable mode.

pip install -e .

To install in editable mode with additional dependencies for development use the command below.

pip install -e .[dev]

Running

The entry point to profiling the sample object detection models is run_profiling.py file.

Examples

Running on CPU python pytorchures/run_profiling.py --device 'cpu' --nr_images 3

Running on GPU python pytorchures/run_profiling.py --device 'cuda' --nr_images 3

The script will print CPU wall time of every layer encountered in the model. Values are printed in a nested manner.

TimedModule wrapper

from pytorchures import TimedModule

model = TimedModule(model)

_output = model(inputs)

profiling_data = model.get_timings()

with open(profiling_filename, "w") as f:
    json.dump(profiling_data, f, indent=4)

In the code above the model and all it's sublayers are recursively wrapped with TimedModule class which measures execution times when a layers are called and stores them for every time the model is called. Execution times of every wrapped layer are retrieved as hierarchical dictionary using model.get_timings(). This dictionary can be saved to json file.

If for some reason there is a need to clear recorded timings call model.clear_timings(). This may useful in only some of the measurements should be included in the final results. It is often the case that first inference run takes much more time due to resource initialization, so clearing the measurements is a way to exclude this first run.

Testing

All tests are located in 'tests' folder. Please follow Arange-Act-Assert pattern for all tests. The tests should load in the test explorer.

Formatting

This repo uses 'Black' code formatter.

Publishing to Pypi

Build the package. This command will create a dist folder with pytorchures package as .whl and tar.gz.

python -m build

Check if the build pacakge were build correctly.

twine check dist/*

Optionally upload the new package to testpypi server.

twine upload -r testpypi dist/*

To test the package from use testpypi the command:

pip install --index-url https://test.pypi.org/simple/ pytorchures

Upload the new package to production pypi server.

twine upload dist/*

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

pytorchures-0.1.2.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

pytorchures-0.1.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file pytorchures-0.1.2.tar.gz.

File metadata

  • Download URL: pytorchures-0.1.2.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for pytorchures-0.1.2.tar.gz
Algorithm Hash digest
SHA256 228038b2674895f6330df71f984d5df5ba56b360a1b18ca52c996cb226968817
MD5 81842e02e295a026b1ee493dfd064a9b
BLAKE2b-256 295633866d157a8dfe6cdd35d01aa1552228b3dcb779f00a2d1cefe3e341351d

See more details on using hashes here.

File details

Details for the file pytorchures-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pytorchures-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for pytorchures-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3b90295629f3c4b76bb6678193b4f71dbfa41050194bddf8675f99665afcea98
MD5 9b48972a49b002db4c3e6a2446c3b564
BLAKE2b-256 073e662de177e113f28e5dbc183193dc0f905e4c1928496e6086504747fc0ef1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page