Skip to content

Dynamic shape for pad operator #2583

@hdnhan

Description

@hdnhan

🐞Describing the bug

When using torch.nn.functional.pad(x, (1, x.size(-1))) with dynamic pad size, coremltools throws an error at line

Stack Trace

Image

To Reproduce

import torch
import numpy as np
import coremltools as ct

class SimpleModel(torch.nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return torch.nn.functional.pad(x, (1, x.size(-1)))

example_input = torch.rand(3)
traced_model = torch.jit.trace(SimpleModel(), example_input)

for i in range(2, 10):
    x = torch.rand(i)
    output = traced_model(x)
    print(f"{x.shape}: {output.shape}")

input_shape = ct.EnumeratedShapes(shapes=[[1], [2], [3]], default=[2])
model = ct.convert(
    traced_model,
    convert_to="mlprogram",
    inputs=[ct.TensorType(shape=input_shape, name="input", dtype=np.float32)],
    outputs=[ct.TensorType(name="output", dtype=np.float32)],
)

System environment (please complete the following information):

  • coremltools version: 8.3.0
  • OS (e.g. MacOS version or Linux type): macOS 15.6
  • Any other relevant version information (e.g. PyTorch or TensorFlow version): PyTorch 2.6.0

Metadata

Metadata

Assignees

Labels

bugUnexpected behaviour that should be corrected (type)triagedReviewed and examined, release as been assigned if applicable (status)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions