We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Orientation and the Grade step 2 of 3
The right answer, the wrong cost
Read the starter. It returns the transpose. Every number is correct. Press Submit and it fails anyway.
That is not a bug in the grader. It is the reason this course exists.
What is actually being graded
Most problem sets ask one question: is the returned value right? For machine learning written in PyTorch that is the right question, and the rest of this site asks it.
This course asks a second one: what did your code do to get there?
x.t() # a new view of the same buffer. 0 bytes copied.
x.t().contiguous() # a new buffer with the same numbers. N bytes copied.
Those two expressions are equal under ==, print identically, and have the
same shape and dtype. No test that only looks at the output can tell them
apart. On a 4x3 tensor the difference is nothing. On the activations of a
transformer layer it is the difference between a model that fits in memory
and one that does not.
How the grader can tell
A tensor is three things: a buffer of numbers, a shape, and a stride saying how far to step to move along each axis. Most of what looks like rearranging a tensor is arithmetic on the shape and stride, leaving the buffer completely alone. Transposing a matrix swaps two strides. That is the whole operation.
So the grader asks the tensors directly:
y.untyped_storage().data_ptr() == x.untyped_storage().data_ptr()
Same buffer, or a different one. You cannot fake it from inside your function, because you do not write the code that asks.
untyped_storage().data_ptr(), notdata_ptr(). The second one points at the first element, which moves when a view starts partway into the buffer, so it reportsFalseforx[1:3]andx[:, 1]even though both are genuine views. This trips up nearly everyone who writes this check by hand, including the author of this problem.
Your task
def transpose(x: torch.Tensor) -> torch.Tensor
Return the transpose of a 2-D tensor without copying the buffer.
The starter already returns the right numbers. Delete what makes it copy.
What the harness does
You never build the input. The harness constructs x, calls your function,
and reports two things: the values you returned, and whether the tensor you
returned is backed by the same buffer it handed you. Both must be right.
Stuck?
PyTorch reference solution
Sign in to attempt this problem and reveal the reference solution.