Skip to content

← Memory, Copies and In-Place step 2 of 6

Easy Primitives

Write into the buffer you were given

Jump to the task

Some PyTorch operations accept an out argument: a tensor where they should write the result. This lets the caller reuse an existing destination:

import torch

x = torch.tensor([1.0, 2.0, 3.0])
dest = torch.empty_like(x)
returned = torch.mul(x, 2.0, out=dest)
print(dest.tolist())      # [2.0, 4.0, 6.0]
print(returned is dest)   # True
print(x.tolist())         # [1.0, 2.0, 3.0]

The destination already has the required storage. Reusing it avoids creating a separate result buffer for this multiplication. It does not promise that an operation uses no internal workspace or that the program will run faster.

Choose which tensor may change

x.mul_(2) changes x itself. torch.mul(x, 2, out=dest) writes to dest. The second preserves x when the destination does not overlap its storage, as in the example. If dest aliases an input, writing the result may change that input too; some overlap patterns are rejected.

Buffer reuse can be useful in repeated inference or preprocessing. Ensure the previous result is no longer needed before overwriting the buffer. It is not a snapshot of every iteration.

Shape, dtype and gradients

Supply a destination with the intended shape, compatible dtype and device. Do not rely on automatic resizing to manage it: resizing behavior depends on the operation and can warn or fail, and expanding storage defeats reuse. An incompatible dtype or device can also raise rather than being corrected.

The out variants used here do not support ordinary autograd recording when an argument requires gradients. Use differentiable out-of-place operations when you need that graph. Buffer reuse is not a general replacement for a training forward pass.

Measure performance on the actual workload. Allocator caching, tensor size, operation cost and device all affect whether reuse saves meaningful time.

Your task

def scale_into(dest: torch.Tensor, x: torch.Tensor, factor: float) -> torch.Tensor

Write x * factor into dest and return that same Tensor object. Preserve its original storage and leave x unchanged. The harness supplies independent float32 vectors of matching shape, with no gradients required.

For x=[1.0, 2.0] and factor=3.0, dest and the returned tensor should contain [3.0, 6.0]. Use torch.mul with out=dest to avoid a separate product tensor. The checks verify values, object identity, storage reuse and input preservation; they do not benchmark speed or count internal allocations.