@@ -143,6 +143,7 @@ profiler.step()
143143profiler.stop()
144144```
145145
146+ ### Ready-for-use code
146147Putting all of this together, here is an example you can run directly:
147148``` python
148149import os
@@ -204,56 +205,7 @@ Launching the example locally is done through the following steps:
2042055 . Access Tensorboard visualization
205206
206207### Write the experiment code
207- We use the code explained in [ the previous section] ( #write-a-code-example ) .
208-
209- === "experiment.py"
210- ```python
211- import os
212- import torch
213- # Import Pytorch profiler
214- import torch.profiler
215-
216-
217- # Linear regression training example
218- x = torch.arange(-5, 5, 0.1).view(-1, 1)
219- y = -5 * x + 0.1 * torch.randn(x.size())
220-
221- model = torch.nn.Linear(1, 1)
222- criterion = torch.nn.MSELoss()
223- optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)
224-
225- # Define in which folder we want the results to be stored
226- # (if the folder does not exist, it is created)
227- scratch_path = os.environ.get("SCRATCH")
228- job_id = os.environ.get("SLURM_JOB_ID")
229- folder_name = f"{scratch_path}$/runs/{job_id}_profiling"
230-
231- profiler = torch.profiler.profile(
232- schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=2),
233- on_trace_ready=torch.profiler.tensorboard_trace_handler(folder_name),
234- record_shapes=True,
235- with_stack=True)
236-
237- # Start the profiler
238- profiler.start()
239-
240- # While the model is training
241- def train_model(iter):
242- for epoch in range(iter):
243- y1 = model(x)
244- loss = criterion(y1, y)
245- optimizer.zero_grad()
246- loss.backward()
247- optimizer.step()
248- # Write the metrics while training the model
249- profiler.step()
250-
251- # Train the model
252- train_model(10)
253-
254- # Stop the profiler when you do not need it anymore
255- profiler.stop()
256- ```
208+ We use the code explained in [ the previous section] ( #ready-for-use-code ) .
257209
258210### Set up the environment
259211To easily set the environment for this example, we use ` uv ` . If it has already be done,
0 commit comments