Skip to content
← Learn PyTorch

Performance

PyTorch

Measure tensor-code performance and investigate unnecessary loops, allocations and synchronization. Compare changes using the benchmarks available for each exercise.

7 exercises

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. Replace an elementwise loop Practice · Medium
  2. 2 Not solved yet. Reuse one output buffer Practice · Medium
  3. 3 Not solved yet. Build a reusable mask outside the loop Practice · Easy
  4. 4 Not solved yet. Batch the head projections Practice · Medium
  5. 5 Not solved yet. Sort once Practice · Medium
  6. 6 Not solved yet. Measure runtime after warmup Practice · Medium
  7. 7 Not solved yet. Vectorize a row-scoring pipeline Practice · Hard

Check yourself

4 questions · not saved while you're signed out

Use these questions to check your understanding and compare explanations. Your score does not affect track completion.

0 / 4 correct

What does no_grad change for this linear layer forward pass?

Choose one answer to reveal the explanations. Answers are kept only while this page is open.

import torch
from torch import nn

layer = nn.Linear(2, 1)
x = torch.ones(1, 2)
print(layer(x).requires_grad)
with torch.no_grad():
    output = layer(x)
print(output.requires_grad)
print(layer.weight.requires_grad)
Question 1 of 4