TensorTonic
Study Plans
Study Plan
Patchify a Batch of Images
Language ModelsComputer VisionTransformers
Easy

Split a channel-first image batch of shape (batch, channels, height, width) into non-overlapping rectangular patches. Patch-grid order is row first, then column. Within each patch, flatten by channel, local row, then local column.

Coordinates contain zero-based patch-grid positions, not pixel positions: each row is [grid row, grid column] in the same order as the tokens. The patch count is the number of patch rows multiplied by the number of patch columns; these coordinates are shared across the batch.

Return a Python dictionary of PyTorch tensors: tokens has shape (batch, patch_count, channels * patch_height * patch_width) and preserves the image dtype; coordinates has shape (patch_count, 2) and int64 dtype. Both outputs stay on the image device.

Loading visualization...

Examples

Input: images = [[[[0,1,2,3],[4,5,6,7]]]], patch_height = 1, patch_width = 2

Output: {"tokens":[[[0,1],[2,3],[4,5],[6,7]]],"coordinates":[[0,0],[0,1],[1,0],[1,1]]}

Explanation: The 2 by 4 image contains four row-major patches of shape 1 by 2.

Input: images = [[[[1,2],[3,4]],[[5,6],[7,8]]]], patch_height = 2, patch_width = 1

Output: {"tokens":[[[1,3,5,7],[2,4,6,8]]],"coordinates":[[0,0],[0,1]]}

Input: images = [[[[0,1,2],[3,4,5]]]], patch_height = 2, patch_width = 3

Output: {"tokens":[[[0,1,2,3,4,5]]],"coordinates":[[0,0]]}

Hint 1

Tensor.reshape can separate grid dimensions from within-patch dimensions.

Hint 2

Tensor.permute reorders tensor axes.

Hint 3

torch.meshgrid supports matrix-style row and column indexing.

Requirements

  • Use PyTorch tensor operations without pixel-by-pixel loops.
  • Preserve the specified patch and within-patch ordering.
  • Support noncontiguous images without modifying their values.

Constraints

  • images is a rank-four PyTorch tensor with positive channel, height, and width dimensions; editor inputs use float32.
  • patch_height and patch_width are positive Python integers dividing image height and width.
  • Inputs contain finite values.
  • Pixel loops are not permitted.
Try Similar Problems
Assign Multimodal RoPE CoordinatesMediumImplement Symmetric CLIP Contrastive LossMediumImplement the SigLIP Pairwise LossMediumNamed-Dimension Batched Attention ScoresEasyTransformer Training FLOP EstimatorEasyRoPE: Rotary Query and Key EmbeddingsMediumDecode Attention with a KV CacheHardCausal Grouped-Query AttentionHardViT PatchifyEasy2D Sinusoidal Positional EmbeddingMediumWindow Partition and ReverseMediumVision Transformer Patch EmbeddingHard

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Patchify a Batch of Images

Language ModelsComputer VisionTransformers
Easy

Split a channel-first image batch of shape (batch, channels, height, width) into non-overlapping rectangular patches. Patch-grid order is row first, then column. Within each patch, flatten by channel, local row, then local column.

Coordinates contain zero-based patch-grid positions, not pixel positions: each row is [grid row, grid column] in the same order as the tokens. The patch count is the number of patch rows multiplied by the number of patch columns; these coordinates are shared across the batch.

Return a Python dictionary of PyTorch tensors: tokens has shape (batch, patch_count, channels * patch_height * patch_width) and preserves the image dtype; coordinates has shape (patch_count, 2) and int64 dtype. Both outputs stay on the image device.

Loading visualization...

Examples

Input: images = [[[[0,1,2,3],[4,5,6,7]]]], patch_height = 1, patch_width = 2

Output: {"tokens":[[[0,1],[2,3],[4,5],[6,7]]],"coordinates":[[0,0],[0,1],[1,0],[1,1]]}

Explanation: The 2 by 4 image contains four row-major patches of shape 1 by 2.

Input: images = [[[[1,2],[3,4]],[[5,6],[7,8]]]], patch_height = 2, patch_width = 1

Output: {"tokens":[[[1,3,5,7],[2,4,6,8]]],"coordinates":[[0,0],[0,1]]}

Input: images = [[[[0,1,2],[3,4,5]]]], patch_height = 2, patch_width = 3

Output: {"tokens":[[[0,1,2,3,4,5]]],"coordinates":[[0,0]]}

Hint 1

Tensor.reshape can separate grid dimensions from within-patch dimensions.

Hint 2

Tensor.permute reorders tensor axes.

Hint 3

torch.meshgrid supports matrix-style row and column indexing.

Requirements

  • Use PyTorch tensor operations without pixel-by-pixel loops.
  • Preserve the specified patch and within-patch ordering.
  • Support noncontiguous images without modifying their values.

Constraints

  • images is a rank-four PyTorch tensor with positive channel, height, and width dimensions; editor inputs use float32.
  • patch_height and patch_width are positive Python integers dividing image height and width.
  • Inputs contain finite values.
  • Pixel loops are not permitted.
Try Similar Problems
Assign Multimodal RoPE CoordinatesMediumImplement Symmetric CLIP Contrastive LossMediumImplement the SigLIP Pairwise LossMediumNamed-Dimension Batched Attention ScoresEasyTransformer Training FLOP EstimatorEasyRoPE: Rotary Query and Key EmbeddingsMediumDecode Attention with a KV CacheHardCausal Grouped-Query AttentionHardViT PatchifyEasy2D Sinusoidal Positional EmbeddingMediumWindow Partition and ReverseMediumVision Transformer Patch EmbeddingHard

Accepts: array

Accepts: number

Accepts: number

You must run your code first.