Strides, views and a CPU matmul roofline
CPU · your Macnotebook90 minrequiredGoal · Produce a table proving which ops are views, and a GFLOP/s measurement of your laptop CPU for the matmul shapes GPT-2 uses.
- 1Create x = torch.randn(4, 1024, 768); print x.stride(), x.transpose(1, 2).stride(), x.view(4, -1).stride().
- 2For reshape, view, transpose, permute, slice, contiguous and clone, check data_ptr() equality with the source and record view-or-copy in a table.
- 3Try x.transpose(1, 2).view(4, -1) and explain the error; fix it with contiguous() or reshape().
- 4Time torch.matmul on (T, 768) @ (768, 3072) for T in [1, 16, 128, 1024] with time.perf_counter, 10 warmup runs then 50 timed runs.
- 5Convert each timing to GFLOP/s using 2*T*768*3072; repeat with dtype bfloat16.
- 6Vary torch.set_num_threads from 1 to your core count for T=1024 and plot GFLOP/s vs threads.
- Deliverable
- A notebook with the view/copy table, a GFLOP/s vs T plot and a GFLOP/s vs threads plot.
- Check
- T=1 achieves at least 10x lower GFLOP/s than T=1024, and the view/copy table matches the official Tensor Views page for every op.