Skip to content

Commit 819668b

Browse files
HollowPrincessSergey TsimferSergeyTsimfer
authored
Small fixes, refactorings and updates (#15)
* fix pylint_notebook: markdown cells processing * Fix bug, change colors / resource names, change separator * Cache, vertical scroll, add help * Update doc, README, cli help * Update pylintrc * Fix width in readme * Update test-install * add changes in run_notebook and rewrite some docs * Speed up formatting * small fixes for exec_notebook * Change separators to thin * Update NBstat tutorial * Self-review; more separator controls * More F-keys; change table elements * Make pylint happy * update pylint_notebook * text prettifyings * Fix empty table handling * Update version --------- Co-authored-by: Sergey Tsimfer <[email protected]> Co-authored-by: Sergey Tsimfer <[email protected]>
1 parent 2f6adf9 commit 819668b

File tree

16 files changed

+513
-255
lines changed

16 files changed

+513
-255
lines changed

.github/workflows/test-install.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ jobs:
1616
fail-fast: false
1717
matrix:
1818
os: [ubuntu-latest, macos-latest, windows-latest]
19-
python-version: [3.8, 3.9]
19+
python-version: [3.8, 3.9, '3.10', 3.11]
2020

2121
runs-on: ${{ matrix.os }}
2222

2323
steps:
24-
- uses: actions/checkout@v2
24+
- uses: actions/checkout@v4
2525

2626
- name: Set up Python ${{ matrix.python-version }} on ${{ matrix.os }}
2727
uses: actions/setup-python@v2
@@ -47,7 +47,7 @@ jobs:
4747
fail-fast: false
4848
matrix:
4949
os: [ubuntu-latest, macos-latest, windows-latest]
50-
python-version: [3.8, 3.9]
50+
python-version: [3.8, 3.9, '3.10', 3.11]
5151

5252
runs-on: ${{ matrix.os }}
5353

README.md

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,12 @@ The main tool of this package is **nbstat** / **nbwatch** command line utility.
1313

1414
<img src="images/nbwatch.gif" width="90%"/>
1515

16-
While in the `watch` mode, you can hit buttons to modify the displayed view:
17-
18-
* `tab` — swaps views, from `nbwatch` to `devicewatch` and back.
19-
* `b` — toggles bar representation for some of the resources: in addition to its value, show colored bar.
20-
* `m` — toggles moving average column for some of the resources: values are averaged across the latest iterations.
21-
* `s` — toggles table separators.
22-
23-
We also add the **devicestat** and **devicewatch** commands that show transposed view with the same information and parameters.
24-
2516
For more information, check out the full [user documentation:](nbtools/nbstat/README.md) explanation of different table views, command line options and ready-to-use snippets.
2617

2718

2819
### Troubleshooting: PID namespaces, user permissions and zombie processes
2920
A [known problem](https://github.com/NVIDIA/nvidia-docker/issues/179) of NVIDIA drivers is that **nvidia-smi** reports PIDs of processes on devices in the global namespace, not in the container namespace, which does not allow to match PIDs of container processes to their device PIDs. There are a few workarounds:
30-
* pass `--pid=host` flag to `docker run`.
21+
* [recommended] pass `--pid=host` flag to `docker run`.
3122
* patch NVIDIA driver to handle PID namespaces correctly.
3223
* [Linux only] fallback on manually inspecting */proc/PID/* files to identify the host PID for each process inside of the container.
3324

@@ -42,8 +33,11 @@ In order to inspect certain properties of processes, we rely on having all neces
4233
### Contribute
4334
If you are interested to contribute, check out the [developer/contributor page.](nbtools/nbstat/DEV.md) It contains detailed description about inner workings of the library, my design choices and motivation behind them, as well as discussion of complexities along the way.
4435

36+
## Library
37+
Other than `nbstat / nbwatch` monitoring utilities, this library provides a few useful tools for working with notebooks and GPUs.
38+
4539

46-
## **pylint_notebook**
40+
### **pylint_notebook**
4741
Shamelessly taken from [pylint page:](https://pylint.pycqa.org/en/latest/)
4842

4943
Function that checks for errors in Jupyter Notebooks with Python code, tries to enforce a coding standard and looks for code smells. It can also look for certain type errors, it can recommend suggestions about how particular blocks can be refactored and can offer you details about the code's complexity.
@@ -58,37 +52,38 @@ pylint_notebook(path_to_ipynb, # If not provided, use path to the cu
5852

5953
Under the hood, it converts `.ipynb` notebook to `.py` script, creates a custom `.pylintrc` configuration, runs the `pylint` and removes all temporary files. Learn more about its usage in the [tutorial.](tutorials/NBstat.ipynb)
6054

61-
## **run_notebook**
55+
### **exec_notebook**
6256
Provides a `eval`-like interface for running Jupyter Notebooks programmatically. We use it for running interactive tests, that are easy to work with: in case of any failures, one can jump right into fixing it with an already set-up environment.
6357

6458
```python
65-
from nbtools import run_notebook
66-
run_notebook(path_to_ipynb, # Which notebook to run
67-
out_path_ipynb, # Where to save result
68-
inputs={'learning_rate': 0.05,}, # Pass variables to notebook
69-
outputs=['accuracy']) # Extract variables from notebook
59+
from nbtools import exec_notebook
60+
exec_notebook(path_to_ipynb, # Which notebook to run
61+
out_path_ipynb, # Where to save result
62+
inputs={'learning_rate': 0.05,}, # Pass variables to notebook
63+
outputs=['accuracy']) # Extract variables from notebook
7064
```
7165

7266

73-
## **set_gpus**
67+
### **set_gpus, free_gpus**
7468
Select free device(s) and set `CUDA_VISIBLE_DEVICES` environment variable so that the current process sees only them.
7569

7670
Eliminates an enormous amount of bugs and unexpected behaviors related to GPU usage.
7771

7872
```python
79-
from nbtools import set_gpus
80-
set_gpus(n=2, # Number of devices to set.
81-
min_free_memory=0.7, # Minimum amount of free memory on device to consider it free.
82-
max_processes=3) # Maximum amount of processes on device to consider it free.
73+
from nbtools import set_gpus, free_gpus
74+
used_gpus = set_gpus(n=2, # Number of devices to set.
75+
min_free_memory=0.7,# Minimum amount of free memory on device to consider free.
76+
max_processes=3) # Maximum amount of processes on device to consider free.
77+
free_gpus(used_gpus) # Kill all processes on selected GPUs. Useful at teardown.
8378
```
8479

85-
## Other functions
80+
### Other functions
8681
```python
87-
from nbtools import (in_notebook, # Return True if executed inside of Jupyter Notebook
88-
get_notebook_path, # If executed in Jupyter Notebook, return its absolute path
89-
get_notebook_name, # If executed in Jupyter Notebook, return its name
90-
notebook_to_script) # Convert Jupyter Notebook to an executable Python script.
91-
# Works well with magics and command line executions.
82+
from nbtools import (in_notebook, # Return True if executed inside of Jupyter Notebook
83+
get_notebook_path, # If executed in Jupyter Notebook, return its absolute path
84+
get_notebook_name, # If executed in Jupyter Notebook, return its name
85+
notebook_to_script) # Convert Jupyter Notebook to an executable Python script.
86+
# Works well with magics and command line executions.
9287
```
9388

9489

nbtools/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" Init file. """
22
#pylint: disable=wildcard-import
33
from .core import *
4-
from .run_notebook import run_notebook
4+
from .exec_notebook import exec_notebook, run_notebook
55
from .pylint_notebook import pylint_notebook
66

7-
__version__ = '0.9.12'
7+
__version__ = '0.9.13'

nbtools/core.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,23 @@ def notebook_to_script(path_script, path_notebook=None, ignore_markdown=True, re
9090
continue
9191

9292
cell_lines = cell['source'].split('\n')
93-
cell_lines.insert(0, f'\n### [{cell_number}] cell')
93+
94+
if cell['cell_type'] == 'code':
95+
cell_lines.insert(0, f'\n### [{cell_number}] cell')
9496

9597
# Comment cell/line magics
9698
for j, line in enumerate(cell_lines):
97-
if line.startswith('%') or line.startswith('!'):
99+
if line.startswith('%') or line.startswith('!') or cell['cell_type'] != 'code':
98100
cell_lines[j] = '### ' + line
99101

100102
code_line_number = len(code_lines) + 1
101103
cell_line_numbers[cell_number] = range(code_line_number, code_line_number + len(cell_lines))
102104

103105
code_lines.extend([line.strip('\n') for line in cell_lines])
104106
code_lines.append('')
105-
cell_number += 1
107+
108+
if cell['cell_type'] == 'code':
109+
cell_number += 1
106110

107111
code = '\n'.join(code_lines).strip()
108112

@@ -135,7 +139,8 @@ def get_available_gpus(n=1, min_free_memory=0.9, max_processes=2, verbose=False,
135139
136140
Returns
137141
-------
138-
List with indices of available GPUs
142+
available_devices : list
143+
Indices of available GPUs.
139144
"""
140145
try:
141146
import nvidia_smi
@@ -211,6 +216,11 @@ def set_gpus(n=1, min_free_memory=0.9, max_processes=2, verbose=False, raise_err
211216
If 2, then display memory and process information for each device.
212217
raise_error : bool
213218
Whether to raise an exception if not enough devices are available.
219+
220+
Returns
221+
-------
222+
devices : list
223+
Indices of selected and reserved GPUs.
214224
"""
215225
#pylint: disable=consider-iterating-dictionary
216226
if 'CUDA_VISIBLE_DEVICES' in os.environ.keys():

0 commit comments

Comments
 (0)