Skip to content

Commit b3c8f92

Browse files
committed
Add pyproject.toml
1 parent 237ed53 commit b3c8f92

1 file changed

Lines changed: 62 additions & 58 deletions

File tree

docs/userguides/profiling/using_tensorboard_and_pytorch_profiler.md

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ from torch.profiler
112112
# (if the folder does not exist, it is created)
113113
scratch_path = os.environ.get("SCRATCH")
114114
job_id = os.environ.get("SLURM_JOB_ID")
115-
folder_name = f"{scratch_path}$/runs/{job_id}_profiling"
115+
folder_name = f"{scratch_path}/runs/{job_id}_profiling"
116116

117117
# Initialize the profiler
118118
# - schedule:
@@ -145,53 +145,55 @@ profiler.stop()
145145

146146
### Ready-for-use code
147147
Putting all of this together, here is an example you can run directly:
148-
```python
149-
import os
150-
import torch
151-
# Import Pytorch profiler
152-
import torch.profiler
153148

149+
=== "experiment.py"
150+
```python
151+
import os
152+
import torch
153+
# Import Pytorch profiler
154+
import torch.profiler
154155

155-
# Linear regression training example
156-
x = torch.arange(-5, 5, 0.1).view(-1, 1)
157-
y = -5 * x + 0.1 * torch.randn(x.size())
158156

159-
model = torch.nn.Linear(1, 1)
160-
criterion = torch.nn.MSELoss()
161-
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)
157+
# Linear regression training example
158+
x = torch.arange(-5, 5, 0.1).view(-1, 1)
159+
y = -5 * x + 0.1 * torch.randn(x.size())
162160

163-
# Define in which folder we want the results to be stored
164-
# (if the folder does not exist, it is created)
165-
scratch_path = os.environ.get("SCRATCH")
166-
job_id = os.environ.get("SLURM_JOB_ID")
167-
folder_name = f"{scratch_path}$/runs/{job_id}_profiling"
161+
model = torch.nn.Linear(1, 1)
162+
criterion = torch.nn.MSELoss()
163+
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)
168164

169-
profiler = torch.profiler.profile(
170-
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2),
171-
on_trace_ready=torch.profiler.tensorboard_trace_handler(folder_name),
172-
record_shapes=True,
173-
with_stack=True)
165+
# Define in which folder we want the results to be stored
166+
# (if the folder does not exist, it is created)
167+
scratch_path = os.environ.get("SCRATCH")
168+
job_id = os.environ.get("SLURM_JOB_ID")
169+
folder_name = f"{scratch_path}/runs/{job_id}_profiling"
174170

175-
# Start the profiler
176-
profiler.start()
171+
profiler = torch.profiler.profile(
172+
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2),
173+
on_trace_ready=torch.profiler.tensorboard_trace_handler(folder_name),
174+
record_shapes=True,
175+
with_stack=True)
177176

178-
# While the model is training
179-
def train_model(iter):
180-
for epoch in range(iter):
181-
y1 = model(x)
182-
loss = criterion(y1, y)
183-
optimizer.zero_grad()
184-
loss.backward()
185-
optimizer.step()
186-
# Write the metrics while training the model
187-
profiler.step()
177+
# Start the profiler
178+
profiler.start()
188179

189-
# Train the model
190-
train_model(10)
180+
# While the model is training
181+
def train_model(iter):
182+
for epoch in range(iter):
183+
y1 = model(x)
184+
loss = criterion(y1, y)
185+
optimizer.zero_grad()
186+
loss.backward()
187+
optimizer.step()
188+
# Write the metrics while training the model
189+
profiler.step()
191190

192-
# Stop the profiler when you do not need it anymore
193-
profiler.stop()
194-
```
191+
# Train the model
192+
train_model(10)
193+
194+
# Stop the profiler when you do not need it anymore
195+
profiler.stop()
196+
```
195197

196198

197199
## Try this example locally
@@ -208,31 +210,33 @@ Launching the example locally is done through the following steps:
208210
We use the code explained in [the previous section](#ready-for-use-code).
209211

210212
### Set up the environment
211-
To easily set the environment for this example, we use `uv`. If it has already be done,
212-
you can skip this section and go to [Launch the experiment](#launch-the-experiment).
213-
214-
1. [Optional if already done] The first step is to install `uv` : this is explained in [this section](../../../userguides/python_uv/#install-uv).
213+
The environment is described in the following file. Copying it as `pyproject.toml` would make available all the prerequisites
214+
while running the `uv` command.
215215

216-
2. We then initialize the project. This create a `pyproject.toml` file.
217-
```
218-
uv init
219-
```
220-
221-
3. In this example, we use `torch`, `torch-tb-profiler` and `tensorboard`, so we add them to the environment configuration:
222-
```
223-
uv add torch
224-
uv add torchvision
225-
uv add torch-tb-profiler
226-
uv add tensorboard
227-
```
216+
=== "pyproject.toml"
217+
```
218+
[project]
219+
name = "tmp1"
220+
version = "0.1.0"
221+
description = "Add your description here"
222+
readme = "README.md"
223+
requires-python = ">=3.14"
224+
dependencies = [
225+
"tensorboard>=2.21.0",
226+
"torch>=2.12.1",
227+
"torch-tb-profiler>=0.4.3",
228+
"torchvision>=0.27.1",
229+
]
230+
```
228231

229232
### Launch the experiment
230-
Launching the experiment is done through the command:
233+
Once the two files (`experiment.py` and `pyproject.toml`) have been written in your environment, you can
234+
launch the experiment through the following command:
231235
```
232236
uv run python experiment.py
233237
```
234238

235-
The folder `runs/experiment1` has been created.
239+
The folder `{scratch_path}/runs/{job_id}_profiling` has been created.
236240

237241

238242
### Launch Tensorboard
@@ -308,7 +312,7 @@ Hence, we copy (or write) the following files on the login node:
308312
# (if the folder does not exist, it is created)
309313
scratch_path = os.environ.get("SCRATCH")
310314
job_id = os.environ.get("SLURM_JOB_ID")
311-
folder_name = f"{scratch_path}$/runs/{job_id}_profiling"
315+
folder_name = f"{scratch_path}/runs/{job_id}_profiling"
312316

313317
profiler = torch.profiler.profile(
314318
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2),

0 commit comments

Comments
 (0)