We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Devices and Data Movement step 1 of 4
How many copies did that cost
An explicit dtype conversion can allocate a converted tensor before an
operation even starts. Some operations accept a dtype argument, so you
can request the calculation's dtype directly.
import torch
x = torch.tensor([16777216.0, 1.0, -16777216.0], dtype=torch.float32)
print(x.sum().item()) # 0.0
print(x.sum(dtype=torch.float64).item()) # 1.0
In float32, this reduction loses the contribution of 1 through rounding. Requesting float64 for the reduction preserves it in this example. Converting the already-rounded result to float64 cannot recover that lost contribution.
Request precision where it is needed
These two expressions request a float64 sum:
explicit = x.to(torch.float64).sum()
direct = x.sum(dtype=torch.float64)
print(explicit.item(), direct.item()) # 1.0 1.0
The first explicitly creates a float64 copy of x. The second requests
float64 from the reduction itself. It still performs computation and creates
a result; this is not a claim of zero allocation or zero conversion work
inside the kernel.
A float64 accumulator alone is not enough. In total + x.sum(), the sum
of x happens first in its default dtype. The later addition cannot repair
rounding that already happened.
What the tests count
The harness counts calls to PyTorch's aten._to_copy operator during your
function. Explicit casts to a different dtype use this operator. A call to
sum(dtype=torch.float64) does not dispatch a separate _to_copy for these
CPU tensors. The counter does not measure every allocation, internal cast,
or memory transfer, and it is not a timing benchmark.
Your task
def summarise(chunks: list) -> torch.Tensor
Sum all elements of a list of one-dimensional CPU float32 tensors. Reduce each chunk in float64 and add its scalar result to a float64 accumulator. Return a scalar float64 tensor, or float64 zero for an empty list.
Use zero separate _to_copy calls. Request the reduction dtype directly
instead of explicitly converting each chunk. The starter also adds whole
vectors; fix that so chunks with different lengths can be summed.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
An explicit dtype conversion can allocate a converted tensor before an
operation even starts. Some operations accept a dtype argument, so you
can request the calculation's dtype directly.
import torch
x = torch.tensor([16777216.0, 1.0, -16777216.0], dtype=torch.float32)
print(x.sum().item()) # 0.0
print(x.sum(dtype=torch.float64).item()) # 1.0
In float32, this reduction loses the contribution of 1 through rounding. Requesting float64 for the reduction preserves it in this example. Converting the already-rounded result to float64 cannot recover that lost contribution.
Request precision where it is needed
These two expressions request a float64 sum:
explicit = x.to(torch.float64).sum()
direct = x.sum(dtype=torch.float64)
print(explicit.item(), direct.item()) # 1.0 1.0
The first explicitly creates a float64 copy of x. The second requests
float64 from the reduction itself. It still performs computation and creates
a result; this is not a claim of zero allocation or zero conversion work
inside the kernel.
A float64 accumulator alone is not enough. In total + x.sum(), the sum
of x happens first in its default dtype. The later addition cannot repair
rounding that already happened.
What the tests count
The harness counts calls to PyTorch's aten._to_copy operator during your
function. Explicit casts to a different dtype use this operator. A call to
sum(dtype=torch.float64) does not dispatch a separate _to_copy for these
CPU tensors. The counter does not measure every allocation, internal cast,
or memory transfer, and it is not a timing benchmark.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.