Scalene: A high-resolution, low-overhead CPU, GPU, and memory profiler for Python with AI-powered optimization suggestions
Project description
Scalene: a Python CPU+GPU+memory profiler with AI-powered optimization proposals
by Emery Berger, Sam Stern, and Juan Altmayer Pizzorno.
(tweet from Ian Ozsvald, author of High Performance Python)
Scalene web-based user interface: http://plasma-umass.org/scalene-gui/
About Scalene
Scalene is a high-performance CPU, GPU and memory profiler for Python that does a number of things that other Python profilers do not and cannot do. It runs orders of magnitude faster than many other profilers while delivering far more detailed information. It is also the first profiler ever to incorporate AI-powered proposed optimizations.
AI-powered optimization suggestions
Note
To enable AI-powered optimization suggestions, you need to enter an OpenAI key in the box under "Advanced options". Your account will need to have a positive balance for this to work (check your balance at https://platform.openai.com/account/usage).
Once you've entered your OpenAI key (see above), click on the lightning bolt (⚡) beside any line or the explosion (💥) for an entire region of code to generate a proposed optimization. Click on a proposed optimization to copy it to the clipboard.
You can click as many times as you like on the lightning bolt or explosion, and it will generate different suggested optimizations. Your mileage may vary, but in some cases, the suggestions are quite impressive (e.g., order-of-magnitude improvements).
Quick Start
Installing Scalene:
python3 -m pip install -U scalene
or
conda install -c conda-forge scalene
Using Scalene:
After installing Scalene, you can use Scalene at the command line, or as a Visual Studio Code extension.
Using the Scalene VS Code Extension:
First, install the Scalene extension from the VS Code Marketplace or by searching for it within VS Code by typing Command-Shift-X (Mac) or Ctrl-Shift-X (Windows). Once that's installed, click Command-Shift-P or Ctrl-Shift-P to open the Command Palette. Then select "Scalene: AI-powered profiling..." (you can start typing Scalene and it will pop up if it's installed). Run that and, assuming your code runs for at least a second, a Scalene profile will appear in a webview.
Commonly used command-line options:
scalene your_prog.py # full profile (outputs to web interface)
python3 -m scalene your_prog.py # equivalent alternative
scalene --cli your_prog.py # use the command-line only (no web interface)
scalene --cpu your_prog.py # only profile CPU
scalene --cpu --gpu your_prog.py # only profile CPU and GPU
scalene --cpu --gpu --memory your_prog.py # profile everything (same as no options)
scalene --reduced-profile your_prog.py # only profile lines with significant usage
scalene --profile-interval 5.0 your_prog.py # output a new profile every five seconds
scalene (Scalene options) --- your_prog.py (...) # use --- to tell Scalene to ignore options after that point
scalene --help # lists all options
Using Scalene programmatically in your code:
Invoke using scalene
as above and then:
from scalene import scalene_profiler
# Turn profiling on
scalene_profiler.start()
# your code
# Turn profiling off
scalene_profiler.stop()
from scalene.scalene_profiler import enable_profiling
with enable_profiling():
# do something
Using Scalene to profile only specific functions via @profile
:
Just preface any functions you want to profile with the @profile
decorator and run it with Scalene:
# do not import profile!
@profile
def slow_function():
import time
time.sleep(3)
Web-based GUI
Scalene has both a CLI and a web-based GUI (demo here).
By default, once Scalene has profiled your program, it will open a
tab in a web browser with an interactive user interface (all processing is done
locally). Hover over bars to see breakdowns of CPU and memory
consumption, and click on underlined column headers to sort the
columns. The generated file profile.html
is self-contained and can be saved for later use.
Scalene Overview
Scalene talk (PyCon US 2021)
This talk presented at PyCon 2021 walks through Scalene's advantages and how to use it to debug the performance of an application (and provides some technical details on its internals). We highly recommend watching this video!
Fast and Accurate
-
Scalene is fast. It uses sampling instead of instrumentation or relying on Python's tracing facilities. Its overhead is typically no more than 10-20% (and often less).
-
Scalene is accurate. We tested CPU profiler accuracy and found that Scalene is among the most accurate profilers, correctly measuring time taken.
- Scalene performs profiling at the line level and per function, pointing to the functions and the specific lines of code responsible for the execution time in your program.
CPU profiling
- Scalene separates out time spent in Python from time in native code (including libraries). Most Python programmers aren't going to optimize the performance of native code (which is usually either in the Python implementation or external libraries), so this helps developers focus their optimization efforts on the code they can actually improve.
- Scalene highlights hotspots (code accounting for significant percentages of CPU time or memory allocation) in red, making them even easier to spot.
- Scalene also separates out system time, making it easy to find I/O bottlenecks.
GPU profiling
- Scalene reports GPU time (currently limited to NVIDIA-based systems).
Memory profiling
- Scalene profiles memory usage. In addition to tracking CPU usage, Scalene also points to the specific lines of code responsible for memory growth. It accomplishes this via an included specialized memory allocator.
- Scalene separates out the percentage of memory consumed by Python code vs. native code.
- Scalene produces per-line memory profiles.
- Scalene identifies lines with likely memory leaks.
- Scalene profiles copying volume, making it easy to spot inadvertent copying, especially due to crossing Python/library boundaries (e.g., accidentally converting
numpy
arrays into Python arrays, and vice versa).
Other features
- Scalene can produce reduced profiles (via
--reduced-profile
) that only report lines that consume more than 1% of CPU or perform at least 100 allocations. - Scalene supports
@profile
decorators to profile only specific functions. - When Scalene is profiling a program launched in the background (via
&
), you can suspend and resume profiling.
Comparison to Other Profilers
Performance and Features
Below is a table comparing the performance and features of various profilers to Scalene.
- Slowdown: the slowdown when running a benchmark from the Pyperformance suite. Green means less than 2x overhead. Scalene's overhead is just a 35% slowdown.
Scalene has all of the following features, many of which only Scalene supports:
- Lines or functions: does the profiler report information only for entire functions, or for every line -- Scalene does both.
- Unmodified Code: works on unmodified code.
- Threads: supports Python threads.
- Multiprocessing: supports use of the
multiprocessing
library -- Scalene only - Python vs. C time: breaks out time spent in Python vs. native code (e.g., libraries) -- Scalene only
- System time: breaks out system time (e.g., sleeping or performing I/O) -- Scalene only
- Profiles memory: reports memory consumption per line / function
- GPU: reports time spent on an NVIDIA GPU (if present) -- Scalene only
- Memory trends: reports memory use over time per line / function -- Scalene only
- Copy volume: reports megabytes being copied per second -- Scalene only
- Detects leaks: automatically pinpoints lines responsible for likely memory leaks -- Scalene only
Output
If you include the --cli
option, Scalene prints annotated source code for the program being profiled
(as text, JSON (--json
), or HTML (--html
)) and any modules it
uses in the same directory or subdirectories (you can optionally have
it --profile-all
and only include files with at least a
--cpu-percent-threshold
of time). Here is a snippet from
pystone.py
.
- Memory usage at the top: Visualized by "sparklines", memory consumption over the runtime of the profiled code.
- "Time Python": How much time was spent in Python code.
- "native": How much time was spent in non-Python code (e.g., libraries written in C/C++).
- "system": How much time was spent in the system (e.g., I/O).
- "GPU": (not shown here) How much time spent on the GPU, if your system has an NVIDIA GPU installed.
- "Memory Python": How much of the memory allocation happened on the Python side of the code, as opposed to in non-Python code (e.g., libraries written in C/C++).
- "net": Positive net memory numbers indicate total memory allocation in megabytes; negative net memory numbers indicate memory reclamation.
- "timeline / %": Visualized by "sparklines", memory consumption generated by this line over the program runtime, and the percentages of total memory activity this line represents.
- "Copy (MB/s)": The amount of megabytes being copied per second (see "About Scalene").
Scalene
The following command runs Scalene on a provided example program.
scalene test/testme.py
Click to see all Scalene's options (available by running with --help
)
% scalene --help
usage: scalene [-h] [--outfile OUTFILE] [--html] [--reduced-profile]
[--profile-interval PROFILE_INTERVAL] [--cpu-only]
[--profile-all] [--profile-only PROFILE_ONLY]
[--use-virtual-time]
[--cpu-percent-threshold CPU_PERCENT_THRESHOLD]
[--cpu-sampling-rate CPU_SAMPLING_RATE]
[--malloc-threshold MALLOC_THRESHOLD]
Scalene: a high-precision CPU and memory profiler.
https://github.com/plasma-umass/scalene
command-line:
% scalene [options] yourprogram.py
or
% python3 -m scalene [options] yourprogram.py
in Jupyter, line mode:
%scrun [options] statement
in Jupyter, cell mode:
%%scalene [options]
code...
code...
optional arguments:
-h, --help show this help message and exit
--outfile OUTFILE file to hold profiler output (default: stdout)
--html output as HTML (default: text)
--reduced-profile generate a reduced profile, with non-zero lines only (default: False)
--profile-interval PROFILE_INTERVAL
output profiles every so many seconds (default: inf)
--cpu-only only profile CPU time (default: profile CPU, memory, and copying)
--profile-all profile all executed code, not just the target program (default: only the target program)
--profile-only PROFILE_ONLY
profile only code in filenames that contain the given strings, separated by commas (default: no restrictions)
--use-virtual-time measure only CPU time, not time spent in I/O or blocking (default: False)
--cpu-percent-threshold CPU_PERCENT_THRESHOLD
only report profiles with at least this percent of CPU time (default: 1%)
--cpu-sampling-rate CPU_SAMPLING_RATE
CPU sampling rate (default: every 0.01s)
--malloc-threshold MALLOC_THRESHOLD
only report profiles with at least this many allocations (default: 100)
When running Scalene in the background, you can suspend/resume profiling
for the process ID that Scalene reports. For example:
% python3 -m scalene [options] yourprogram.py &
Scalene now profiling process 12345
to suspend profiling: python3 -m scalene.profile --off --pid 12345
to resume profiling: python3 -m scalene.profile --on --pid 12345
Scalene with Jupyter
Instructions for installing and using Scalene with Jupyter notebooks
This notebook illustrates the use of Scalene in Jupyter.
Installation:
!pip install scalene
%load_ext scalene
Line mode:
%scrun [options] statement
Cell mode:
%%scalene [options]
code...
code...
Installation
Using pip
(Mac OS X, Linux, Windows, and WSL2)
Scalene is distributed as a pip
package and works on Mac OS X, Linux (including Ubuntu in Windows WSL2) and (with limitations) Windows platforms.
Note
The Windows version currently only supports CPU and GPU profiling, but not memory or copy profiling.
You can install it as follows:
% pip install -U scalene
or
% python3 -m pip install -U scalene
You may need to install some packages first.
See https://stackoverflow.com/a/19344978/4954434 for full instructions for all Linux flavors.
For Ubuntu/Debian:
% sudo apt install git python3-all-dev
Using conda
(Mac OS X, Linux, Windows, and WSL2)
% conda install -c conda-forge scalene
Scalene is distributed as a conda
package and works on Mac OS X, Linux (including Ubuntu in Windows WSL2) and (with limitations) Windows platforms.
Note
The Windows version currently only supports CPU and GPU profiling, but not memory or copy profiling.
On ArchLinux
You can install Scalene on Arch Linux via the AUR
package. Use your favorite AUR helper, or
manually download the PKGBUILD
and run makepkg -cirs
to build. Note that this will place
libscalene.so
in /usr/lib
; modify the below usage instructions accordingly.
Frequently Asked Questions
Can I use Scalene with PyTest?
A: Yes! You can run it as follows (for example):
python3 -m scalene --- -m pytest your_test.py
Is there any way to get shorter profiles or do more targeted profiling?
A: Yes! There are several options:
- Use
--reduced-profile
to include only lines and files with memory/CPU/GPU activity. - Use
--profile-only
to include only filenames containing specific strings (as in,--profile-only foo,bar,baz
). - Decorate functions of interest with
@profile
to have Scalene report only those functions. - Turn profiling on and off programmatically by importing Scalene profiler (
from scalene import scalene_profiler
) and then turning profiling on and off viascalene_profiler.start()
andscalene_profiler.stop()
. By default, Scalene runs with profiling on, so to delay profiling until desired, use the--off
command-line option (python3 -m scalene --off yourprogram.py
).
How do I run Scalene in PyCharm?
A: In PyCharm, you can run Scalene at the command line by opening the terminal at the bottom of the IDE and running a Scalene command (e.g., python -m scalene <your program>
). Use the options --cli
, --html
, and --outfile <your output.html>
to generate an HTML file that you can then view in the IDE.
How do I use Scalene with Django?
A: Pass in the --noreload
option (see https://github.com/plasma-umass/scalene/issues/178).
Does Scalene work with gevent/Greenlets?
A: Yes! Put the following code in the beginning of your program, or modify the call to monkey.patch_all
as below:
from gevent import monkey
monkey.patch_all(thread=False)
How do I use Scalene with PyTorch on the Mac?
A: Scalene works with PyTorch version 1.5.1 on Mac OS X. There's a bug in newer versions of PyTorch (https://github.com/pytorch/pytorch/issues/57185) that interferes with Scalene (discussion here: https://github.com/plasma-umass/scalene/issues/110), but only on Macs.
Technical Information
For details about how Scalene works, please see the following paper, which won the Jay Lepreau Best Paper Award at OSDI 2023: Triangulating Python Performance Issues with Scalene. (Note that this paper does not include information about the AI-driven proposed optimizations.)
To cite Scalene in an academic paper, please use the following:
@inproceedings{288540,
author = {Emery D. Berger and Sam Stern and Juan Altmayer Pizzorno},
title = {Triangulating Python Performance Issues with {S}calene},
booktitle = {{17th USENIX Symposium on Operating Systems Design and Implementation (OSDI 23)}},
year = {2023},
isbn = {978-1-939133-34-2},
address = {Boston, MA},
pages = {51--64},
url = {https://www.usenix.org/conference/osdi23/presentation/berger},
publisher = {USENIX Association},
month = jul
}
Success Stories
If you use Scalene to successfully debug a performance problem, please add a comment to this issue!
Acknowledgements
Logo created by Sophia Berger.
This material is based upon work supported by the National Science Foundation under Grant No. 1955610. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for scalene-1.5.45-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f63d0b62b74d3d37b5e92d41465f46821bee3d684f7c2a8dab864aac37ebacb |
|
MD5 | cb770b05ba0a0822341690b4fd54dbe4 |
|
BLAKE2b-256 | 3adb2c32219caceda2fd5b5261fd7ac2fae73589e14657277e529d5599e23c5f |
Hashes for scalene-1.5.45-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cd9b97410633cf6c58cfb951003cfbca7e05dd13cc8b8030f94580272db7cc8 |
|
MD5 | 26c12b1e3def7078fdc6e74b95d29a3a |
|
BLAKE2b-256 | 5be80e28e0616e5bc334283d4fadc1330638973901371ff451fb89f7c2db6b62 |
Hashes for scalene-1.5.45-cp312-cp312-macosx_14_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f3f36487cf852477abe9844eb189bcdab5ef532b4bdf3e0969ce171ce8c3cd6 |
|
MD5 | 4219e3aca690b01d89cc42c8f637198d |
|
BLAKE2b-256 | 1b3fd1daa0874e7125322dd182eac42f554449460c37709a2d60745177ebb158 |
Hashes for scalene-1.5.45-cp312-cp312-macosx_13_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 047f60f3b3c78e2f105d7da40a22d724ce7a15755b390211c4c69c803bb19ce3 |
|
MD5 | 37ea0a14a2df774177a9b4777e7cbf11 |
|
BLAKE2b-256 | f1761216b6a5263e388b28df1e046d12271bc0d6cc5e7c55a51054cb2bb2c2a4 |
Hashes for scalene-1.5.45-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca53428d7fb8bf1b892203ffc13520341f47865fd7753374b449b2efd3c553ff |
|
MD5 | 7a611f433e4319073e78df18d820d6b6 |
|
BLAKE2b-256 | 4de2f4017981002304f3237bbaaa42bbc5bf4af67b5ed204135a02b0f8ad08af |
Hashes for scalene-1.5.45-cp311-cp311-macosx_14_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d92b109c3526da3c3242e75b50f0a30f5d5a5ed0d599e363a05f0dbb18cb2559 |
|
MD5 | 14579c716d72968f289ca718c95addf3 |
|
BLAKE2b-256 | 0d3d3c559b1bd5dda35dcd45eb9b66e8f3f5c59ddb58ace593a88f3fb2ff00cf |
Hashes for scalene-1.5.45-cp311-cp311-macosx_13_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f0f68d6895e4d12215307b0593e1c38437ce2c032044e5b3a2a7cc99fd5de5d |
|
MD5 | eac0696173f036a9423737baed72efba |
|
BLAKE2b-256 | 45796d43cc343b286660e9d8a63fb99d70862a2c8fd20f1f3873b561d4d2acb9 |
Hashes for scalene-1.5.45-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04529738d14e1c47e0874dca745dadf05f3d5da329c32779bc15e446b92d0c7d |
|
MD5 | d68677a071cf196cb35b6d62f2a348a9 |
|
BLAKE2b-256 | 81f408c9c8685b898cf80ac343d00c2290b7d62ee14b6590a7acca887aa7f31f |
Hashes for scalene-1.5.45-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f91951140d533b35f91e83b65ac69c23bbcb0f048cea586c3669700be628795 |
|
MD5 | 68ca8ae303f85d31d4e66268ea0a1033 |
|
BLAKE2b-256 | e12d0940179cefc7aca4dadc4699e462c77e6fcb74b0b567593c7a2ffc521494 |
Hashes for scalene-1.5.45-cp310-cp310-macosx_14_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28cf3f3a1fe1c82b3f599833314bb79b2ef9cdaff472f8be6972a3c871323a14 |
|
MD5 | 2eee448c3ed371d75ce6a947bb60ed00 |
|
BLAKE2b-256 | eeefe2f71be772759b9e687a11441cb0a301b8742fd912488b4732f03e4ccbce |
Hashes for scalene-1.5.45-cp310-cp310-macosx_13_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18d76bca68cd6be7f6c0efc734085b63ca006e2a9f2249a69a55ffa0059cc1b2 |
|
MD5 | b621c5b442b853f59674ab9e5aa6b7c0 |
|
BLAKE2b-256 | 418a4417df698ec0f3af1c51eebb6159c7d231b99ae6bc76d0994d1edec08c3e |
Hashes for scalene-1.5.45-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af82baa1564964b121a49066fb2383640deccbd37107cf7fd303f969976fadaa |
|
MD5 | aa521e932fae39be23e8d90d368033ad |
|
BLAKE2b-256 | e7d9e8e48a68353860008548cdfd2af74047e793a40dc436a705ba0bf7dbab35 |
Hashes for scalene-1.5.45-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c283cc5a84dc838fb582df38ac3655d74f58e0fca3714d0b6620169b0b92bf5d |
|
MD5 | ba73f3a654cd5da52ea83a8d51f386aa |
|
BLAKE2b-256 | dfd32c31705b43a4299c806b8318bf69e8f64045eacaec06a3aafdd6db95c038 |
Hashes for scalene-1.5.45-cp39-cp39-macosx_14_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4cf2f2d11b4caa64caabb1ac939edb75b309ad1163e6a8b5cc6472978930270 |
|
MD5 | e07808c1e04c86cd2bedcb1f905efbc7 |
|
BLAKE2b-256 | 7565d69d6409ad9ccdae16e84f10e23a1325edcd0e53711c8c3608f19526b12a |
Hashes for scalene-1.5.45-cp39-cp39-macosx_13_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9280554413f1e07daa2df39a40672035b7da30f6888b0ff16d3ecfdd7db55b6 |
|
MD5 | 5e47c30e788dfe67616ae47898d6a0a1 |
|
BLAKE2b-256 | 20a00c12ea41d67b3e4e15027bef54e0ed61540f38cf929de3b8c4bd1d1dc733 |
Hashes for scalene-1.5.45-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 024058c10e7bc2175d7d797b5af651bac5f33404376863a2b28b7efeb2bcef99 |
|
MD5 | da8fe48bd43b1ada0ef634a6f0d6316b |
|
BLAKE2b-256 | 60f913e9e28642d9a18c1230ca010ea30f8cca85406562afe687033d81f5f533 |
Hashes for scalene-1.5.45-cp38-cp38-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d6baf3b812a96049ca156eb4bccbdf3ae78f8960a76644b82796e8ebcd0bed7 |
|
MD5 | 769b2857eb0eac3a1db8799de991f388 |
|
BLAKE2b-256 | 7bd9f370a6c6b39404cd151a2861cd719663f412cf39dba94920c4c1a56b52a8 |
Hashes for scalene-1.5.45-cp38-cp38-macosx_14_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e6c726c79a5e22d2d2794326df4c2be58000dd6ee1a85524b7820a823cab94c |
|
MD5 | 719211ebee61531b6be1889bd6215bcc |
|
BLAKE2b-256 | 09ba82a72ad41becd455fb1fa12c1802c19e9d1ceb3935cbbc1cd14cde52b2ef |
Hashes for scalene-1.5.45-cp38-cp38-macosx_13_0_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 326acc139520654bcc932af22ca3e178ee19721c477947a86eb1d80ace844ca1 |
|
MD5 | 972424b82ef42db7d2be23f9d85ccea5 |
|
BLAKE2b-256 | cf1ec7c645a0e6061f6aec993e8e6d644ac3913a6a563fdcdda2bd6f7f547ab5 |