Matrix-matrix multiplication combines X with shape n×m and Y with shape m×p to produce C with shape n×p. In PyTorch, the @ operator performs the multiplication. Matching the inner dimensions and tracking tensor shapes prevents common implementation bugs. section 1.2.1
import timeit
x = torch.rand(2**11, 2**11)
time_cpu = timeit.timeit("x@x", globals=globals(), number=100)
Benchmark repeated matrix multiplication on a large PyTorch tensor with timeit. section 1.2.1
GPU comparisons depend on both workload size and device placement:
- Small calculations may run slower on a GPU because data transfer and coordination costs can exceed the computation time. section 1.2.1
- Move the tensors involved in the operation to the same device; mixing CUDA and CPU tensors raises a device-mismatch error. section 1.2.1
- Benchmark repeated operations rather than a single small multiplication to make performance differences measurable. section 1.2.1







