| 1 |
.. figure:: |
| 2 |
https://github.com/plasma-umass/scalene/raw/master/docs/scalene-icon-white.png |
| 3 |
:alt: scalene |
| 4 |
|
| 5 |
scalene |
| 6 |
|
| 7 |
Scalene: a Python CPU+GPU+memory profiler with AI-powered optimization proposals |
| 8 |
================================================================================ |
| 9 |
|
| 10 |
by `Emery Berger <https://emeryberger.com>`__, `Sam |
| 11 |
Stern <https://samstern.me/>`__, and `Juan Altmayer |
| 12 |
Pizzorno <https://github.com/jaltmayerpizzorno>`__. |
| 13 |
|
| 14 |
|Scalene community Slack|\ `Scalene community |
| 15 |
Slack <https://join.slack.com/t/scaleneprofil-jge3234/shared_invite/zt-110vzrdck-xJh5d4gHnp5vKXIjYD3Uwg>`__ |
| 16 |
|
| 17 |
|PyPI Latest Release|\ |Anaconda-Server Badge| |Downloads|\ |Anaconda |
| 18 |
downloads| |image1| |Python versions|\ |Visual Studio Code Extension |
| 19 |
version| |License| |
| 20 |
|
| 21 |
.. figure:: |
| 22 |
https://github.com/plasma-umass/scalene/raw/master/docs/Ozsvald-tweet.png |
| 23 |
:alt: Ozsvald tweet |
| 24 |
|
| 25 |
Ozsvald tweet |
| 26 |
|
| 27 |
(tweet from Ian Ozsvald, author of `High Performance |
| 28 |
Python <https://smile.amazon.com/High-Performance-Python-Performant-Programming/dp/1492055026/ref=sr_1_1?crid=texbooks>`__) |
| 29 |
|
| 30 |
.. figure:: |
| 31 |
https://github.com/plasma-umass/scalene/raw/master/docs/semantic-scholar-success.png |
| 32 |
:alt: Semantic Scholar success story |
| 33 |
|
| 34 |
Semantic Scholar success story |
| 35 |
|
| 36 |
**Scalene web-based user interface:** |
| 37 |
http://plasma-umass.org/scalene-gui/ |
| 38 |
|
| 39 |
About Scalene |
| 40 |
------------- |
| 41 |
|
| 42 |
Scalene is a high-performance CPU, GPU *and* memory profiler for Python |
| 43 |
that does a number of things that other Python profilers do not and |
| 44 |
cannot do. It runs orders of magnitude faster than many other profilers |
| 45 |
while delivering far more detailed information. It is also the first |
| 46 |
profiler ever to incorporate AI-powered proposed optimizations. |
| 47 |
|
| 48 |
AI-powered optimization suggestions |
| 49 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 50 |
|
| 51 |
**Note** |
| 52 |
|
| 53 |
To enable AI-powered optimization suggestions, you need to enter an |
| 54 |
`OpenAI key <https://openai.com/api/>`__ in the box under “Advanced |
| 55 |
options”. *Your account will need to have a positive balance for this |
| 56 |
to work* (check your balance at |
| 57 |
https://platform.openai.com/account/usage). |
| 58 |
|
| 59 |
Once you’ve entered your OpenAI key (see above), click on the lightning |
| 60 |
bolt (⚡) beside any line or the explosion (💥) for an entire region of |
| 61 |
code to generate a proposed optimization. Click on a proposed |
| 62 |
optimization to copy it to the clipboard. |
| 63 |
|
| 64 |
You can click as many times as you like on the lightning bolt or |
| 65 |
explosion, and it will generate different suggested optimizations. Your |
| 66 |
mileage may vary, but in some cases, the suggestions are quite |
| 67 |
impressive (e.g., order-of-magnitude improvements). |
| 68 |
|
| 69 |
Quick Start |
| 70 |
~~~~~~~~~~~ |
| 71 |
|
| 72 |
Installing Scalene: |
| 73 |
^^^^^^^^^^^^^^^^^^^ |
| 74 |
|
| 75 |
.. code:: console |
| 76 |
|
| 77 |
python3 -m pip install -U scalene |
| 78 |
|
| 79 |
or |
| 80 |
|
| 81 |
.. code:: console |
| 82 |
|
| 83 |
conda install -c conda-forge scalene |
| 84 |
|
| 85 |
Using Scalene: |
| 86 |
^^^^^^^^^^^^^^ |
| 87 |
|
| 88 |
After installing Scalene, you can use Scalene at the command line, or as |
| 89 |
a Visual Studio Code extension. |
| 90 |
|
| 91 |
.. raw:: html |
| 92 |
|
| 93 |
<details> |
| 94 |
|
| 95 |
.. raw:: html |
| 96 |
|
| 97 |
<summary> |
| 98 |
|
| 99 |
Using the Scalene VS Code Extension: |
| 100 |
|
| 101 |
.. raw:: html |
| 102 |
|
| 103 |
</summary> |
| 104 |
|
| 105 |
First, install the Scalene extension from the VS Code Marketplace or by |
| 106 |
searching for it within VS Code by typing Command-Shift-X (Mac) or |
| 107 |
Ctrl-Shift-X (Windows). Once that’s installed, click Command-Shift-P or |
| 108 |
Ctrl-Shift-P to open the Command Palette. Then select “Scalene: |
| 109 |
AI-powered profiling…” (you can start typing Scalene and it will pop up |
| 110 |
if it’s installed). Run that and, assuming your code runs for at least a |
| 111 |
second, a Scalene profile will appear in a webview. |
| 112 |
|
| 113 |
.. raw:: html |
| 114 |
|
| 115 |
</details> |
| 116 |
|
| 117 |
.. raw:: html |
| 118 |
|
| 119 |
<details> |
| 120 |
|
| 121 |
.. raw:: html |
| 122 |
|
| 123 |
<summary> |
| 124 |
|
| 125 |
Commonly used command-line options: |
| 126 |
|
| 127 |
.. raw:: html |
| 128 |
|
| 129 |
</summary> |
| 130 |
|
| 131 |
.. code:: console |
| 132 |
|
| 133 |
scalene your_prog.py # full profile (outputs to web interface) |
| 134 |
python3 -m scalene your_prog.py # equivalent alternative |
| 135 |
|
| 136 |
scalene --cli your_prog.py # use the command-line only (no web interface) |
| 137 |
|
| 138 |
scalene --cpu your_prog.py # only profile CPU |
| 139 |
scalene --cpu --gpu your_prog.py # only profile CPU and GPU |
| 140 |
scalene --cpu --gpu --memory your_prog.py # profile everything (same as no options) |
| 141 |
|
| 142 |
scalene --reduced-profile your_prog.py # only profile lines with significant usage |
| 143 |
scalene --profile-interval 5.0 your_prog.py # output a new profile every five seconds |
| 144 |
|
| 145 |
scalene (Scalene options) --- your_prog.py (...) # use --- to tell Scalene to ignore options after that point |
| 146 |
scalene --help # lists all options |
| 147 |
|
| 148 |
.. raw:: html |
| 149 |
|
| 150 |
</details> |
| 151 |
|
| 152 |
.. raw:: html |
| 153 |
|
| 154 |
<details> |
| 155 |
|
| 156 |
.. raw:: html |
| 157 |
|
| 158 |
<summary> |
| 159 |
|
| 160 |
Using Scalene programmatically in your code: |
| 161 |
|
| 162 |
.. raw:: html |
| 163 |
|
| 164 |
</summary> |
| 165 |
|
| 166 |
Invoke using ``scalene`` as above and then: |
| 167 |
|
| 168 |
.. code:: python |
| 169 |
|
| 170 |
from scalene import scalene_profiler |
| 171 |
|
| 172 |
# Turn profiling on |
| 173 |
scalene_profiler.start() |
| 174 |
|
| 175 |
# your code |
| 176 |
|
| 177 |
# Turn profiling off |
| 178 |
scalene_profiler.stop() |
| 179 |
|
| 180 |
.. code:: python |
| 181 |
|
| 182 |
from scalene.scalene_profiler import enable_profiling |
| 183 |
|
| 184 |
with enable_profiling(): |
| 185 |
# do something |
| 186 |
|
| 187 |
.. raw:: html |
| 188 |
|
| 189 |
</details> |
| 190 |
|
| 191 |
.. raw:: html |
| 192 |
|
| 193 |
<details> |
| 194 |
|
| 195 |
.. raw:: html |
| 196 |
|
| 197 |
<summary> |
| 198 |
|
| 199 |
Using Scalene to profile only specific functions via @profile: |
| 200 |
|
| 201 |
.. raw:: html |
| 202 |
|
| 203 |
</summary> |
| 204 |
|
| 205 |
Just preface any functions you want to profile with the ``@profile`` |
| 206 |
decorator and run it with Scalene: |
| 207 |
|
| 208 |
.. code:: python |
| 209 |
|
| 210 |
# do not import profile! |
| 211 |
|
| 212 |
@profile |
| 213 |
def slow_function(): |
| 214 |
import time |
| 215 |
time.sleep(3) |
| 216 |
|
| 217 |
.. raw:: html |
| 218 |
|
| 219 |
</details> |
| 220 |
|
| 221 |
Web-based GUI |
| 222 |
^^^^^^^^^^^^^ |
| 223 |
|
| 224 |
Scalene has both a CLI and a web-based GUI `(demo |
| 225 |
here) <https://scalene-gui.github.io/scalene-gui/>`__. |
| 226 |
|
| 227 |
By default, once Scalene has profiled your program, it will open a tab |
| 228 |
in a web browser with an interactive user interface (all processing is |
| 229 |
done locally). Hover over bars to see breakdowns of CPU and memory |
| 230 |
consumption, and click on underlined column headers to sort the columns. |
| 231 |
The generated file ``profile.html`` is self-contained and can be saved |
| 232 |
for later use. |
| 233 |
|
| 234 |
|Scalene web GUI| |
| 235 |
|
| 236 |
Scalene Overview |
| 237 |
---------------- |
| 238 |
|
| 239 |
Scalene talk (PyCon US 2021) |
| 240 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 241 |
|
| 242 |
`This talk <https://youtu.be/5iEf-_7mM1k>`__ presented at PyCon 2021 |
| 243 |
walks through Scalene’s advantages and how to use it to debug the |
| 244 |
performance of an application (and provides some technical details on |
| 245 |
its internals). We highly recommend watching this video! |
| 246 |
|
| 247 |
|Scalene presentation at PyCon 2021| |
| 248 |
|
| 249 |
Fast and Accurate |
| 250 |
~~~~~~~~~~~~~~~~~ |
| 251 |
|
| 252 |
- Scalene is **fast**. It uses sampling instead of instrumentation or |
| 253 |
relying on Python’s tracing facilities. Its overhead is typically no |
| 254 |
more than 10-20% (and often less). |
| 255 |
|
| 256 |
- Scalene is **accurate**. We tested CPU profiler accuracy and found |
| 257 |
that Scalene is among the most accurate profilers, correctly |
| 258 |
measuring time taken. |
| 259 |
|
| 260 |
.. figure:: |
| 261 |
https://github.com/plasma-umass/scalene/raw/master/docs/cpu-accuracy-comparison.png |
| 262 |
:alt: Profiler accuracy |
| 263 |
|
| 264 |
Profiler accuracy |
| 265 |
|
| 266 |
- Scalene performs profiling **at the line level** *and* **per |
| 267 |
function**, pointing to the functions and the specific lines of code |
| 268 |
responsible for the execution time in your program. |
| 269 |
|
| 270 |
CPU profiling |
| 271 |
~~~~~~~~~~~~~ |
| 272 |
|
| 273 |
- Scalene **separates out time spent in Python from time in native |
| 274 |
code** (including libraries). Most Python programmers aren’t going to |
| 275 |
optimize the performance of native code (which is usually either in |
| 276 |
the Python implementation or external libraries), so this helps |
| 277 |
developers focus their optimization efforts on the code they can |
| 278 |
actually improve. |
| 279 |
- Scalene **highlights hotspots** (code accounting for significant |
| 280 |
percentages of CPU time or memory allocation) in red, making them |
| 281 |
even easier to spot. |
| 282 |
- Scalene also separates out **system time**, making it easy to find |
| 283 |
I/O bottlenecks. |
| 284 |
|
| 285 |
GPU profiling |
| 286 |
~~~~~~~~~~~~~ |
| 287 |
|
| 288 |
- Scalene reports **GPU time** (currently limited to NVIDIA-based |
| 289 |
systems). |
| 290 |
|
| 291 |
Memory profiling |
| 292 |
~~~~~~~~~~~~~~~~ |
| 293 |
|
| 294 |
- Scalene **profiles memory usage**. In addition to tracking CPU usage, |
| 295 |
Scalene also points to the specific lines of code responsible for |
| 296 |
memory growth. It accomplishes this via an included specialized |
| 297 |
memory allocator. |
| 298 |
- Scalene separates out the percentage of **memory consumed by Python |
| 299 |
code vs. native code**. |
| 300 |
- Scalene produces **per-line memory profiles**. |
| 301 |
- Scalene **identifies lines with likely memory leaks**. |
| 302 |
- Scalene **profiles copying volume**, making it easy to spot |
| 303 |
inadvertent copying, especially due to crossing Python/library |
| 304 |
boundaries (e.g., accidentally converting ``numpy`` arrays into |
| 305 |
Python arrays, and vice versa). |
| 306 |
|
| 307 |
Other features |
| 308 |
~~~~~~~~~~~~~~ |
| 309 |
|
| 310 |
- Scalene can produce **reduced profiles** (via ``--reduced-profile``) |
| 311 |
that only report lines that consume more than 1% of CPU or perform at |
| 312 |
least 100 allocations. |
| 313 |
- Scalene supports ``@profile`` decorators to profile only specific |
| 314 |
functions. |
| 315 |
- When Scalene is profiling a program launched in the background (via |
| 316 |
``&``), you can **suspend and resume profiling**. |
| 317 |
|
| 318 |
Comparison to Other Profilers |
| 319 |
============================= |
| 320 |
|
| 321 |
Performance and Features |
| 322 |
------------------------ |
| 323 |
|
| 324 |
Below is a table comparing the **performance and features** of various |
| 325 |
profilers to Scalene. |
| 326 |
|
| 327 |
.. figure:: |
| 328 |
https://raw.githubusercontent.com/plasma-umass/scalene/master/docs/images/profiler-comparison.png |
| 329 |
:alt: Performance and feature comparison |
| 330 |
|
| 331 |
Performance and feature comparison |
| 332 |
|
| 333 |
- **Slowdown**: the slowdown when running a benchmark from the |
| 334 |
Pyperformance suite. Green means less than 2x overhead. Scalene’s |
| 335 |
overhead is just a 35% slowdown. |
| 336 |
|
| 337 |
Scalene has all of the following features, many of which only Scalene |
| 338 |
supports: |
| 339 |
|
| 340 |
- **Lines or functions**: does the profiler report information only for |
| 341 |
entire functions, or for every line – Scalene does both. |
| 342 |
- **Unmodified Code**: works on unmodified code. |
| 343 |
- **Threads**: supports Python threads. |
| 344 |
- **Multiprocessing**: supports use of the ``multiprocessing`` library |
| 345 |
– *Scalene only* |
| 346 |
- **Python vs. C time**: breaks out time spent in Python vs. native |
| 347 |
code (e.g., libraries) – *Scalene only* |
| 348 |
- **System time**: breaks out system time (e.g., sleeping or performing |
| 349 |
I/O) – *Scalene only* |
| 350 |
- **Profiles memory**: reports memory consumption per line / function |
| 351 |
- **GPU**: reports time spent on an NVIDIA GPU (if present) – *Scalene |
| 352 |
only* |
| 353 |
- **Memory trends**: reports memory use over time per line / function – |
| 354 |
*Scalene only* |
| 355 |
- **Copy volume**: reports megabytes being copied per second – *Scalene |
| 356 |
only* |
| 357 |
- **Detects leaks**: automatically pinpoints lines responsible for |
| 358 |
likely memory leaks – *Scalene only* |
| 359 |
|
| 360 |
Output |
| 361 |
------ |
| 362 |
|
| 363 |
If you include the ``--cli`` option, Scalene prints annotated source |
| 364 |
code for the program being profiled (as text, JSON (``--json``), or HTML |
| 365 |
(``--html``)) and any modules it uses in the same directory or |
| 366 |
subdirectories (you can optionally have it ``--profile-all`` and only |
| 367 |
include files with at least a ``--cpu-percent-threshold`` of time). Here |
| 368 |
is a snippet from ``pystone.py``. |
| 369 |
|
| 370 |
.. figure:: |
| 371 |
https://raw.githubusercontent.com/plasma-umass/scalene/master/docs/images/sample-profile-pystone.png |
| 372 |
:alt: Example profile |
| 373 |
|
| 374 |
Example profile |
| 375 |
|
| 376 |
- **Memory usage at the top**: Visualized by “sparklines”, memory |
| 377 |
consumption over the runtime of the profiled code. |
| 378 |
- **“Time Python”**: How much time was spent in Python code. |
| 379 |
- **“native”**: How much time was spent in non-Python code (e.g., |
| 380 |
libraries written in C/C++). |
| 381 |
- **“system”**: How much time was spent in the system (e.g., I/O). |
| 382 |
- **“GPU”**: (not shown here) How much time spent on the GPU, if your |
| 383 |
system has an NVIDIA GPU installed. |
| 384 |
- **“Memory Python”**: How much of the memory allocation happened on |
| 385 |
the Python side of the code, as opposed to in non-Python code (e.g., |
| 386 |
libraries written in C/C++). |
| 387 |
- **“net”**: Positive net memory numbers indicate total memory |
| 388 |
allocation in megabytes; negative net memory numbers indicate memory |
| 389 |
reclamation. |
| 390 |
- **“timeline / %”**: Visualized by “sparklines”, memory consumption |
| 391 |
generated by this line over the program runtime, and the percentages |
| 392 |
of total memory activity this line represents. |
| 393 |
- **“Copy (MB/s)”**: The amount of megabytes being copied per second |
| 394 |
(see “About Scalene”). |
| 395 |
|
| 396 |
Scalene |
| 397 |
------- |
| 398 |
|
| 399 |
The following command runs Scalene on a provided example program. |
| 400 |
|
| 401 |
.. code:: console |
| 402 |
|
| 403 |
scalene test/testme.py |
| 404 |
|
| 405 |
.. raw:: html |
| 406 |
|
| 407 |
<details> |
| 408 |
|
| 409 |
.. raw:: html |
| 410 |
|
| 411 |
<summary> |
| 412 |
|
| 413 |
Click to see all Scalene’s options (available by running with –help) |
| 414 |
|
| 415 |
.. raw:: html |
| 416 |
|
| 417 |
</summary> |
| 418 |
|
| 419 |
.. code:: console |
| 420 |
|
| 421 |
% scalene --help |
| 422 |
usage: scalene [-h] [--outfile OUTFILE] [--html] [--reduced-profile] |
| 423 |
[--profile-interval PROFILE_INTERVAL] [--cpu-only] |
| 424 |
[--profile-all] [--profile-only PROFILE_ONLY] |
| 425 |
[--use-virtual-time] |
| 426 |
[--cpu-percent-threshold CPU_PERCENT_THRESHOLD] |
| 427 |
[--cpu-sampling-rate CPU_SAMPLING_RATE] |
| 428 |
[--malloc-threshold MALLOC_THRESHOLD] |
| 429 |
|
| 430 |
Scalene: a high-precision CPU and memory profiler. |
| 431 |
https://github.com/plasma-umass/scalene |
| 432 |
|
| 433 |
command-line: |
| 434 |
% scalene [options] yourprogram.py |
| 435 |
or |
| 436 |
% python3 -m scalene [options] yourprogram.py |
| 437 |
|
| 438 |
in Jupyter, line mode: |
| 439 |
%scrun [options] statement |
| 440 |
|
| 441 |
in Jupyter, cell mode: |
| 442 |
%%scalene [options] |
| 443 |
code... |
| 444 |
code... |
| 445 |
|
| 446 |
optional arguments: |
| 447 |
-h, --help show this help message and exit |
| 448 |
--outfile OUTFILE file to hold profiler output (default: stdout) |
| 449 |
--html output as HTML (default: text) |
| 450 |
--reduced-profile generate a reduced profile, with non-zero lines only (default: False) |
| 451 |
--profile-interval PROFILE_INTERVAL |
| 452 |
output profiles every so many seconds (default: inf) |
| 453 |
--cpu-only only profile CPU time (default: profile CPU, memory, and copying) |
| 454 |
--profile-all profile all executed code, not just the target program (default: only the target program) |
| 455 |
--profile-only PROFILE_ONLY |
| 456 |
profile only code in filenames that contain the given strings, separated by commas (default: no restrictions) |
| 457 |
--use-virtual-time measure only CPU time, not time spent in I/O or blocking (default: False) |
| 458 |
--cpu-percent-threshold CPU_PERCENT_THRESHOLD |
| 459 |
only report profiles with at least this percent of CPU time (default: 1%) |
| 460 |
--cpu-sampling-rate CPU_SAMPLING_RATE |
| 461 |
CPU sampling rate (default: every 0.01s) |
| 462 |
--malloc-threshold MALLOC_THRESHOLD |
| 463 |
only report profiles with at least this many allocations (default: 100) |
| 464 |
|
| 465 |
When running Scalene in the background, you can suspend/resume profiling |
| 466 |
for the process ID that Scalene reports. For example: |
| 467 |
|
| 468 |
% python3 -m scalene [options] yourprogram.py & |
| 469 |
Scalene now profiling process 12345 |
| 470 |
to suspend profiling: python3 -m scalene.profile --off --pid 12345 |
| 471 |
to resume profiling: python3 -m scalene.profile --on --pid 12345 |
| 472 |
|
| 473 |
.. raw:: html |
| 474 |
|
| 475 |
</details> |
| 476 |
|
| 477 |
Scalene with Jupyter |
| 478 |
~~~~~~~~~~~~~~~~~~~~ |
| 479 |
|
| 480 |
.. raw:: html |
| 481 |
|
| 482 |
<details> |
| 483 |
|
| 484 |
.. raw:: html |
| 485 |
|
| 486 |
<summary> |
| 487 |
|
| 488 |
Instructions for installing and using Scalene with Jupyter notebooks |
| 489 |
|
| 490 |
.. raw:: html |
| 491 |
|
| 492 |
</summary> |
| 493 |
|
| 494 |
`This |
| 495 |
notebook <https://nbviewer.jupyter.org/github/plasma-umass/scalene/blob/master/docs/scalene-demo.ipynb>`__ |
| 496 |
illustrates the use of Scalene in Jupyter. |
| 497 |
|
| 498 |
Installation: |
| 499 |
|
| 500 |
.. code:: console |
| 501 |
|
| 502 |
!pip install scalene |
| 503 |
%load_ext scalene |
| 504 |
|
| 505 |
Line mode: |
| 506 |
|
| 507 |
.. code:: console |
| 508 |
|
| 509 |
%scrun [options] statement |
| 510 |
|
| 511 |
Cell mode: |
| 512 |
|
| 513 |
.. code:: console |
| 514 |
|
| 515 |
%%scalene [options] |
| 516 |
code... |
| 517 |
code... |
| 518 |
|
| 519 |
.. raw:: html |
| 520 |
|
| 521 |
</details> |
| 522 |
|
| 523 |
Installation |
| 524 |
------------ |
| 525 |
|
| 526 |
.. raw:: html |
| 527 |
|
| 528 |
<details open> |
| 529 |
|
| 530 |
.. raw:: html |
| 531 |
|
| 532 |
<summary> |
| 533 |
|
| 534 |
Using pip (Mac OS X, Linux, Windows, and WSL2) |
| 535 |
|
| 536 |
.. raw:: html |
| 537 |
|
| 538 |
</summary> |
| 539 |
|
| 540 |
Scalene is distributed as a ``pip`` package and works on Mac OS X, Linux |
| 541 |
(including Ubuntu in `Windows |
| 542 |
WSL2 <https://docs.microsoft.com/en-us/windows/wsl/wsl2-index>`__) and |
| 543 |
(with limitations) Windows platforms. |
| 544 |
|
| 545 |
**Note** |
| 546 |
|
| 547 |
The Windows version currently only supports CPU and GPU profiling, |
| 548 |
but not memory or copy profiling. |
| 549 |
|
| 550 |
You can install it as follows: |
| 551 |
|
| 552 |
.. code:: console |
| 553 |
|
| 554 |
% pip install -U scalene |
| 555 |
|
| 556 |
or |
| 557 |
|
| 558 |
.. code:: console |
| 559 |
|
| 560 |
% python3 -m pip install -U scalene |
| 561 |
|
| 562 |
You may need to install some packages first. |
| 563 |
|
| 564 |
See https://stackoverflow.com/a/19344978/4954434 for full instructions |
| 565 |
for all Linux flavors. |
| 566 |
|
| 567 |
For Ubuntu/Debian: |
| 568 |
|
| 569 |
.. code:: console |
| 570 |
|
| 571 |
% sudo apt install git python3-all-dev |
| 572 |
|
| 573 |
.. raw:: html |
| 574 |
|
| 575 |
</details> |
| 576 |
|
| 577 |
.. raw:: html |
| 578 |
|
| 579 |
<details> |
| 580 |
|
| 581 |
.. raw:: html |
| 582 |
|
| 583 |
<summary> |
| 584 |
|
| 585 |
Using conda (Mac OS X, Linux, Windows, and WSL2) |
| 586 |
|
| 587 |
.. raw:: html |
| 588 |
|
| 589 |
</summary> |
| 590 |
|
| 591 |
.. code:: console |
| 592 |
|
| 593 |
% conda install -c conda-forge scalene |
| 594 |
|
| 595 |
Scalene is distributed as a ``conda`` package and works on Mac OS X, |
| 596 |
Linux (including Ubuntu in `Windows |
| 597 |
WSL2 <https://docs.microsoft.com/en-us/windows/wsl/wsl2-index>`__) and |
| 598 |
(with limitations) Windows platforms. |
| 599 |
|
| 600 |
**Note** |
| 601 |
|
| 602 |
The Windows version currently only supports CPU and GPU profiling, |
| 603 |
but not memory or copy profiling. |
| 604 |
|
| 605 |
.. raw:: html |
| 606 |
|
| 607 |
</details> |
| 608 |
|
| 609 |
.. raw:: html |
| 610 |
|
| 611 |
<details> |
| 612 |
|
| 613 |
.. raw:: html |
| 614 |
|
| 615 |
<summary> |
| 616 |
|
| 617 |
On ArchLinux |
| 618 |
|
| 619 |
.. raw:: html |
| 620 |
|
| 621 |
</summary> |
| 622 |
|
| 623 |
You can install Scalene on Arch Linux via the `AUR |
| 624 |
package <https://aur.archlinux.org/packages/python-scalene-git/>`__. Use |
| 625 |
your favorite AUR helper, or manually download the ``PKGBUILD`` and run |
| 626 |
``makepkg -cirs`` to build. Note that this will place ``libscalene.so`` |
| 627 |
in ``/usr/lib``; modify the below usage instructions accordingly. |
| 628 |
|
| 629 |
.. raw:: html |
| 630 |
|
| 631 |
</details> |
| 632 |
|
| 633 |
Frequently Asked Questions |
| 634 |
========================== |
| 635 |
|
| 636 |
.. raw:: html |
| 637 |
|
| 638 |
<details> |
| 639 |
|
| 640 |
.. raw:: html |
| 641 |
|
| 642 |
<summary> |
| 643 |
|
| 644 |
Can I use Scalene with PyTest? |
| 645 |
|
| 646 |
.. raw:: html |
| 647 |
|
| 648 |
</summary> |
| 649 |
|
| 650 |
**A:** Yes! You can run it as follows (for example): |
| 651 |
|
| 652 |
``python3 -m scalene --- -m pytest your_test.py`` |
| 653 |
|
| 654 |
.. raw:: html |
| 655 |
|
| 656 |
</details> |
| 657 |
|
| 658 |
.. raw:: html |
| 659 |
|
| 660 |
<details> |
| 661 |
|
| 662 |
.. raw:: html |
| 663 |
|
| 664 |
<summary> |
| 665 |
|
| 666 |
Is there any way to get shorter profiles or do more targeted profiling? |
| 667 |
|
| 668 |
.. raw:: html |
| 669 |
|
| 670 |
</summary> |
| 671 |
|
| 672 |
**A:** Yes! There are several options: |
| 673 |
|
| 674 |
1. Use ``--reduced-profile`` to include only lines and files with |
| 675 |
memory/CPU/GPU activity. |
| 676 |
2. Use ``--profile-only`` to include only filenames containing specific |
| 677 |
strings (as in, ``--profile-only foo,bar,baz``). |
| 678 |
3. Decorate functions of interest with ``@profile`` to have Scalene |
| 679 |
report *only* those functions. |
| 680 |
4. Turn profiling on and off programmatically by importing Scalene |
| 681 |
profiler (``from scalene import scalene_profiler``) and then turning |
| 682 |
profiling on and off via ``scalene_profiler.start()`` and |
| 683 |
``scalene_profiler.stop()``. By default, Scalene runs with profiling |
| 684 |
on, so to delay profiling until desired, use the ``--off`` |
| 685 |
command-line option (``python3 -m scalene --off yourprogram.py``). |
| 686 |
|
| 687 |
.. raw:: html |
| 688 |
|
| 689 |
</details> |
| 690 |
|
| 691 |
.. raw:: html |
| 692 |
|
| 693 |
<details> |
| 694 |
|
| 695 |
.. raw:: html |
| 696 |
|
| 697 |
<summary> |
| 698 |
|
| 699 |
How do I run Scalene in PyCharm? |
| 700 |
|
| 701 |
.. raw:: html |
| 702 |
|
| 703 |
</summary> |
| 704 |
|
| 705 |
**A:** In PyCharm, you can run Scalene at the command line by opening |
| 706 |
the terminal at the bottom of the IDE and running a Scalene command |
| 707 |
(e.g., ``python -m scalene <your program>``). Use the options ``--cli``, |
| 708 |
``--html``, and ``--outfile <your output.html>`` to generate an HTML |
| 709 |
file that you can then view in the IDE. |
| 710 |
|
| 711 |
.. raw:: html |
| 712 |
|
| 713 |
</details> |
| 714 |
|
| 715 |
.. raw:: html |
| 716 |
|
| 717 |
<details> |
| 718 |
|
| 719 |
.. raw:: html |
| 720 |
|
| 721 |
<summary> |
| 722 |
|
| 723 |
How do I use Scalene with Django? |
| 724 |
|
| 725 |
.. raw:: html |
| 726 |
|
| 727 |
</summary> |
| 728 |
|
| 729 |
**A:** Pass in the ``--noreload`` option (see |
| 730 |
https://github.com/plasma-umass/scalene/issues/178). |
| 731 |
|
| 732 |
.. raw:: html |
| 733 |
|
| 734 |
</details> |
| 735 |
|
| 736 |
.. raw:: html |
| 737 |
|
| 738 |
<details> |
| 739 |
|
| 740 |
.. raw:: html |
| 741 |
|
| 742 |
<summary> |
| 743 |
|
| 744 |
Does Scalene work with gevent/Greenlets? |
| 745 |
|
| 746 |
.. raw:: html |
| 747 |
|
| 748 |
</summary> |
| 749 |
|
| 750 |
**A:** Yes! Put the following code in the beginning of your program, or |
| 751 |
modify the call to ``monkey.patch_all`` as below: |
| 752 |
|
| 753 |
.. code:: python |
| 754 |
|
| 755 |
from gevent import monkey |
| 756 |
monkey.patch_all(thread=False) |
| 757 |
|
| 758 |
.. raw:: html |
| 759 |
|
| 760 |
</details> |
| 761 |
|
| 762 |
.. raw:: html |
| 763 |
|
| 764 |
<details> |
| 765 |
|
| 766 |
.. raw:: html |
| 767 |
|
| 768 |
<summary> |
| 769 |
|
| 770 |
How do I use Scalene with PyTorch on the Mac? |
| 771 |
|
| 772 |
.. raw:: html |
| 773 |
|
| 774 |
</summary> |
| 775 |
|
| 776 |
**A:** Scalene works with PyTorch version 1.5.1 on Mac OS X. There’s a |
| 777 |
bug in newer versions of PyTorch |
| 778 |
(https://github.com/pytorch/pytorch/issues/57185) that interferes with |
| 779 |
Scalene (discussion here: |
| 780 |
https://github.com/plasma-umass/scalene/issues/110), but only on Macs. |
| 781 |
|
| 782 |
.. raw:: html |
| 783 |
|
| 784 |
</details> |
| 785 |
|
| 786 |
Technical Information |
| 787 |
===================== |
| 788 |
|
| 789 |
For details about how Scalene works, please see the following paper, |
| 790 |
which won the Jay Lepreau Best Paper Award at `OSDI |
| 791 |
2023 <https://www.usenix.org/conference/osdi23/presentation/berger>`__: |
| 792 |
`Triangulating Python Performance Issues with |
| 793 |
Scalene <https://arxiv.org/pdf/2212.07597>`__. (Note that this paper |
| 794 |
does not include information about the AI-driven proposed |
| 795 |
optimizations.) |
| 796 |
|
| 797 |
.. raw:: html |
| 798 |
|
| 799 |
<details> |
| 800 |
|
| 801 |
.. raw:: html |
| 802 |
|
| 803 |
<summary> |
| 804 |
|
| 805 |
To cite Scalene in an academic paper, please use the following: |
| 806 |
|
| 807 |
.. raw:: html |
| 808 |
|
| 809 |
</summary> |
| 810 |
|
| 811 |
.. code:: latex |
| 812 |
|
| 813 |
@inproceedings{288540, |
| 814 |
author = {Emery D. Berger and Sam Stern and Juan Altmayer Pizzorno}, |
| 815 |
title = {Triangulating Python Performance Issues with {S}calene}, |
| 816 |
booktitle = {{17th USENIX Symposium on Operating Systems Design and Implementation (OSDI 23)}}, |
| 817 |
year = {2023}, |
| 818 |
isbn = {978-1-939133-34-2}, |
| 819 |
address = {Boston, MA}, |
| 820 |
pages = {51--64}, |
| 821 |
url = {https://www.usenix.org/conference/osdi23/presentation/berger}, |
| 822 |
publisher = {USENIX Association}, |
| 823 |
month = jul |
| 824 |
} |
| 825 |
|
| 826 |
.. raw:: html |
| 827 |
|
| 828 |
</details> |
| 829 |
|
| 830 |
Success Stories |
| 831 |
=============== |
| 832 |
|
| 833 |
If you use Scalene to successfully debug a performance problem, please |
| 834 |
`add a comment to this |
| 835 |
issue <https://github.com/plasma-umass/scalene/issues/58>`__! |
| 836 |
|
| 837 |
Acknowledgements |
| 838 |
================ |
| 839 |
|
| 840 |
Logo created by `Sophia |
| 841 |
Berger <https://www.linkedin.com/in/sophia-berger/>`__. |
| 842 |
|
| 843 |
This material is based upon work supported by the National Science |
| 844 |
Foundation under Grant No. 1955610. Any opinions, findings, and |
| 845 |
conclusions or recommendations expressed in this material are those of |
| 846 |
the author(s) and do not necessarily reflect the views of the National |
| 847 |
Science Foundation. |
| 848 |
|
| 849 |
.. |Scalene community Slack| image:: https://github.com/plasma-umass/scalene/raw/master/docs/images/slack-logo.png |
| 850 |
:target: https://join.slack.com/t/scaleneprofil-jge3234/shared_invite/zt-110vzrdck-xJh5d4gHnp5vKXIjYD3Uwg |
| 851 |
.. |PyPI Latest Release| image:: https://img.shields.io/pypi/v/scalene.svg |
| 852 |
:target: https://pypi.org/project/scalene/ |
| 853 |
.. |Anaconda-Server Badge| image:: https://img.shields.io/conda/v/conda-forge/scalene |
| 854 |
:target: https://anaconda.org/conda-forge/scalene |
| 855 |
.. |Downloads| image:: https://static.pepy.tech/badge/scalene |
| 856 |
:target: https://pepy.tech/project/scalene |
| 857 |
.. |Anaconda downloads| image:: https://img.shields.io/conda/d/conda-forge/scalene?logo=conda |
| 858 |
:target: https://anaconda.org/conda-forge/scalene |
| 859 |
.. |image1| image:: https://static.pepy.tech/badge/scalene/month |
| 860 |
:target: https://pepy.tech/project/scalene |
| 861 |
.. |Python versions| image:: https://img.shields.io/pypi/pyversions/scalene.svg?style=flat-square |
| 862 |
.. |Visual Studio Code Extension version| image:: https://img.shields.io/visual-studio-marketplace/v/emeryberger.scalene?logo=visualstudiocode |
| 863 |
:target: https://marketplace.visualstudio.com/items?itemName=EmeryBerger.scalene |
| 864 |
.. |License| image:: https://img.shields.io/github/license/plasma-umass/scalene |
| 865 |
.. |Scalene web GUI| image:: https://raw.githubusercontent.com/plasma-umass/scalene/master/docs/scalene-gui-example.png |
| 866 |
:target: https://raw.githubusercontent.com/plasma-umass/scalene/master/docs/scalene-gui-example-full.png |
| 867 |
.. |Scalene presentation at PyCon 2021| image:: https://raw.githubusercontent.com/plasma-umass/scalene/master/docs/images/scalene-video-img.png |
| 868 |
:target: https://youtu.be/5iEf-_7mM1k |