A Vision Transformer treats an image as a sequence of patches. Getting the reshape right is most of the work.
For images x: (N, C, H, W) and patch size p (dividing H and W):
patchify(x, p): return (N, (H/p) * (W/p), C * p * p). Patches are ordered row by row across the image, and each patch is flattened in (C, p, p) order.unpatchify(patches, p, C, H, W): the exact inverseembed(patches, W_proj, cls, pos): project each patch with W_proj: (C*p*p, D), prepend the class token cls: (D,) to every sequence, and add the position embeddings pos: (1 + num_patches, D). Return (N, 1 + num_patches, D).