Skip to content
← Learn PyTorch

Memory, Copies and In-Place

PyTorch

Compare clone, detach, copy_, output buffers and in-place updates. Track shared storage and learn when mutation conflicts with the required behavior.

6 exercises

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Not solved yet. Clone, detach, or both Practice · Medium
  2. 2 Not solved yet. Write into the buffer you were given Practice · Easy
  3. 3 Not solved yet. Fill the caller's buffer Practice · Easy
  4. 4 Not solved yet. Save independent tensor snapshots Practice · Medium
  5. 5 Not solved yet. Updating a parameter by hand Practice · Medium
  6. 6 Not solved yet. Accumulate into an existing tensor Practice · Medium

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

With gradient recording enabled, how do clone() and detach() differ for this tensor?

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

import torch

with torch.enable_grad():
    z = torch.ones(2, requires_grad=True)
    detached = z.detach()
    cloned = z.clone()
    print(detached.requires_grad, detached.data_ptr() == z.data_ptr())
    print(cloned.requires_grad, cloned.data_ptr() == z.data_ptr())
Question 1 of 4