Fix meshgrid converter to handle non-1D inputs#2665
Merged
TobyRoseman merged 1 commit intoapple:mainfrom Mar 30, 2026
Merged
Conversation
The meshgrid op converter raises ValueError when inputs have rank > 1. This happens when converting models that use torch.linspace(...) / scalar before torch.meshgrid(), because the MIL real_div op can broadcast the 1D linspace result with a 0D scalar divisor to produce a higher-rank tensor. This pattern is common in deformable attention modules (DINO, Deformable-DETR, RF-DETR) where coordinate grids are created via: grid_y = torch.linspace(0.5, h - 0.5, steps=h) / h grid_x = torch.linspace(0.5, w - 0.5, steps=w) / w grid_y, grid_x = torch.meshgrid(grid_y, grid_x, indexing='ij') Instead of rejecting non-1D inputs, flatten them to 1D with mb.reshape(shape=[-1]) before meshgrid processing.
Collaborator
|
Code changes look good. CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2418650256 |
TobyRoseman
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I am trying to use https://github.com/roboflow/rf-detr and convert the model to CoreML for use on iPad computer vision app. I'm using some of the fixes from https://github.com/landchenxuan/rf-detr-to-coreml and after upgrading to latest PyTorch 2.11 am getting good performance! :-)
There's one issue with
torch.meshgridthat needs a small fix. For now I have a helper function to monkey-patchcoremltoolswith this fix as workaround.See description and code/test in this PR - done by coding agent.
Would be great if you could include it.
Summary
The
meshgridop converter raisesValueError("meshgrid received non-1d tensor.")wheninputs have rank > 1. This happens in practice when converting models that use
torch.linspace(...) / scalarbeforetorch.meshgrid(), because the MILreal_divopcan broadcast the 1D linspace result with a 0D scalar divisor to produce a higher-rank
tensor.
This pattern is common in deformable attention modules (DINO, Deformable-DETR, RF-DETR)
where coordinate grids are created:
The PyTorch JIT trace shows 1D tensors flowing into
meshgrid, but during MIL conversionthe division by a 0D shape-derived scalar produces a result with rank > 1 in the MIL IR,
causing the meshgrid converter to reject it.
Changes
coremltools/converters/mil/frontend/torch/ops.pyraise ValueError("meshgrid received non-1d tensor."))_flatten_inputs()helper that reshapes any non-1D inputs to 1D viamb.reshape(shape=[-1])before meshgrid processingmeshgridsemantically requires 1D-like inputs, and the flatten simply recovers the intended shapecoremltools/converters/mil/frontend/torch/test/test_torch_ops.pytest_meshgrid_non_1d_inputstoTestMeshgridclasslinspace / scalar → meshgrid) with bothijandxyindexing modesTesting
Red/green verified:
ValueError: meshgrid received non-1d tensor.Related Issues