{"record":{"id":"ceac98a1aa9c84c9","repo":"huggingface/candle","slug":"image-height-h-is-not-a-multiple-of-patch-height","errorCode":null,"errorMessage":"image height {h} is not a multiple of patch height {patch_h}","messagePattern":"image height (.+?) is not a multiple of patch height (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/beit.rs","lineNumber":269,"sourceCode":"    fn new(vb: VarBuilder, patch_size: usize, in_chans: usize, embed_dim: usize) -> Result<Self> {\n        let config = candle_nn::Conv2dConfig {\n            stride: patch_size,\n            ..Default::default()\n        };\n        let proj = candle_nn::conv2d(in_chans, embed_dim, patch_size, config, vb.pp(\"proj\"))?;\n        Ok(Self {\n            proj,\n            patch_size: (patch_size, patch_size),\n        })\n    }\n}\n\nimpl Module for PatchEmbed {\n    fn forward(&self, xs: &Tensor) -> Result<Tensor> {\n        let (_b, _c, h, w) = xs.dims4()?;\n        let (patch_h, patch_w) = self.patch_size;\n        if (h % patch_h) != 0 {\n            candle::bail!(\"image height {h} is not a multiple of patch height {patch_h}\")\n        }\n        if (w % patch_w) != 0 {\n            candle::bail!(\"image width {w} is not a multiple of patch width {patch_w}\")\n        }\n        let xs = self.proj.forward(xs)?;\n        let (b, c, h, w) = xs.dims4()?;\n        // flatten embeddings.\n        xs.reshape((b, c, h * w))?.transpose(1, 2)\n    }\n}\n\n#[derive(Debug)]\npub struct BeitVisionTransformer {\n    patch_embed: PatchEmbed,\n    cls_token: Tensor,\n    blocks: Vec<Block>,\n    norm: LayerNorm,\n    head: Linear,","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/beit.rs#L251-L287","documentation":"In the BeiT vision model, PatchEmbed::forward splits the input image into fixed-size patches via a convolution. This error is thrown when the input image height is not an exact multiple of the patch height, since leftover pixels cannot form complete patches. It is a hard shape constraint of the patch embedding layer.","triggerScenarios":"Forwarding an image tensor whose H dimension is not divisible by the model's patch_size.0, e.g. a 224x225 image with patch height 16.","commonSituations":"Resizing/preprocessing pipelines producing odd image sizes, mixing models with different patch sizes (14 vs 16), or cropped images not resized to a multiple of the patch size.","solutions":["Resize the image so height % patch_h == 0 before forwarding","Center-crop the image to the nearest multiple of patch_h","Verify the model's patch_size config matches your preprocessing (e.g. 16 vs 14)","Check for unwanted padding that changed the height"],"exampleFix":"// before\nlet xs = Tensor::from_shape((1, 3, 225, 224), ...)?; // 225 % 16 != 0\n// after\nlet xs = Tensor::from_shape((1, 3, 224, 224), ...)?; // 224 % 16 == 0","handlingStrategy":"validation","validationCode":"let (patch_h, _) = model.patch_size;\nlet (_, _, h, _) = xs.dims4()?;\nassert!(h % patch_h == 0, \"height {} not multiple of {}\", h, patch_h);","typeGuard":"fn is_valid_image_size(dims: (usize, usize), patch: (usize, usize)) -> bool {\n    dims.0 % patch.0 == 0 && dims.1 % patch.1 == 0\n}","tryCatchPattern":"match patch_embed.forward(&xs) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"not a multiple\") => {\n        let xs = resize_to_multiple(&xs, model.patch_size)?;\n        patch_embed.forward(&xs)\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Resize/center-crop images to a multiple of the patch size in preprocessing","Match patch_size config to the checkpoint you load","Validate input dims (dims4) before model forward","Use fixed canonical input sizes (e.g. 224x224)"],"tags":["rust","candle","vision","beit","shape-constraint"],"backgroundTag":"image-size-not-divisible-by-patch-size","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}