{"record":{"id":"6a0bb29ff860a861","repo":"rohitg00/ai-engineering-from-scratch","slug":"patch-size-self-patch-size-must-divide-image-siz","errorCode":null,"errorMessage":"patch_size {self.patch_size} must divide image_size {self.image_size}","messagePattern":"patch_size (.+?) must divide image_size (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/58-vision-encoder-patches/code/main.py","lineNumber":32,"sourceCode":"import math\nfrom dataclasses import dataclass\n\nimport numpy as np\nimport torch\nimport torch.nn as nn\n\n\n@dataclass(frozen=True)\nclass FrontEndConfig:\n    image_size: int = 224\n    patch_size: int = 16\n    in_channels: int = 3\n    hidden: int = 768\n\n    @property\n    def grid_size(self) -> int:\n        if self.image_size % self.patch_size != 0:\n            raise ValueError(\n                f\"patch_size {self.patch_size} must divide image_size {self.image_size}\"\n            )\n        return self.image_size // self.patch_size\n\n    @property\n    def num_patches(self) -> int:\n        return self.grid_size * self.grid_size\n\n\ndef sinusoidal_2d(grid_h: int, grid_w: int, dim: int) -> torch.Tensor:\n    \"\"\"Build a deterministic 2D sinusoidal position table of shape (grid_h * grid_w, dim).\n\n    Half of dim encodes row position, half encodes column position. Within each\n    half, frequencies span the standard Transformer sin/cos band. Identical\n    inputs always produce identical outputs, with no learned state.\n    \"\"\"\n    if dim % 4 != 0:\n        raise ValueError(f\"sinusoidal_2d dim must be divisible by 4, got {dim}\")","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/58-vision-encoder-patches/code/main.py#L14-L50","documentation":"FrontEndConfig.grid_size guard: image_size is not an exact multiple of patch_size, so the patch grid would have fractional dimensions and the Conv2d patchifier could not tile the image.","triggerScenarios":"Thrown at phases/19-capstone-projects/58-vision-encoder-patches/code/main.py:32 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use standard ViT pairs like 224/16 or 224/14","Resize or crop the image to a multiple of patch_size","Choose a patch_size that divides the configured image_size"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}