Skip to content

Conversation

noobsiecoder
Copy link
Contributor

Description

Fixes #2583

This PR fixes the conversion of torch.nn.functional.pad operations with dynamic padding values (e.g., x.size(-1)) to Core ML format.

Problem

When padding values contain runtime-determined sizes, the _array_construct function in ops.py was returning a Python list instead of a proper MIL Var object, causing AttributeError: 'list' object has no attribute 'val'.

Solution

Modified _array_construct to create a proper Var object using mb.concat for dynamic list construction, maintaining the expected Var interface throughout the conversion pipeline.

Testing

  • Added comprehensive tests in test_ops.py covering dynamic, constant, and mixed padding cases
  • All tests pass with PyTorch 2.6.0 and coremltools 8.3.0
  • Verified no regression in existing padding functionality

Checklist for Reviewer(s)

  • Code follows project style guidelines (passed flake8)
  • Tests added for new functionality
  • All tests pass

Modified _array_construct to handle dynamic padding values: Creates proper Var objects using mb.concat instead of Python lists + Fixes AttributeError when converting models with x.size(-1) padding
@noobsiecoder
Copy link
Contributor Author

Hey @karimnosseir, can you review my PR please?

@junpeiz
Copy link
Collaborator

junpeiz commented Sep 9, 2025

@noobsiecoder Thank you for the contribution! Let me kick off a CI run for this PR.

@junpeiz
Copy link
Collaborator

junpeiz commented Sep 9, 2025

@junpeiz junpeiz self-requested a review September 9, 2025 16:46
@junpeiz
Copy link
Collaborator

junpeiz commented Sep 9, 2025

@noobsiecoder There are some failures in the CI link above. Could you take a look to fix it? Thanks!

@noobsiecoder
Copy link
Contributor Author

@junpeiz, yes I'm on it.
Thanks!

@noobsiecoder
Copy link
Contributor Author

Hi @junpeiz, I found the issue and fixed it. Could you please try running CI now?

@junpeiz
Copy link
Collaborator

junpeiz commented Sep 11, 2025

Sure! Started another CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2033126061

@junpeiz junpeiz merged commit 7e5b37c into apple:main Sep 11, 2025
@noobsiecoder noobsiecoder deleted the fix-dyn-shape-pad-ops-2583 branch September 11, 2025 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic shape for pad operator
3 participants