Skip to content

Commit 95bf8ea

Browse files
author
Alejandro Gaston Alvarez Franceschi
committed
Fixes
1 parent b607396 commit 95bf8ea

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

coremltools/converters/mil/frontend/torch/ops.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ def get_bindings(alist) -> List[Any]:
217217

218218
for i in alist:
219219
if isinstance(i, str):
220-
results.append(context[i])
220+
try:
221+
results.append(context[i])
222+
except ValueError:
223+
results.append(None)
221224
elif isinstance(i, (list, tuple)) and all(isinstance(j, int) for j in i):
222225
results.append(mb.const(val=i))
223226
elif isinstance(i, (list, tuple)):
@@ -980,11 +983,25 @@ def _convolution(context, node):
980983
# we require a (2 * n)-tuple, where n is the number of spatial dimensions, start and end for each spatial dimension
981984
pad = inputs[4].val
982985

983-
if len(weight.shape) in (3, 4):
984-
# 1D and 2D: Need to explicitly state L-R, T-B pad
986+
if type(pad) == str:
987+
if pad == "same":
988+
pad = 1
989+
elif pad == "valid":
990+
pad = 0
991+
else:
992+
raise ValueError(f"Unkown padding string value: '{pad}'")
993+
994+
if len(weight.shape) == 3:
995+
# 1D padding: needs explicitly state L-R for x dim
985996
pad = _np.repeat(pad, 2)
997+
elif len(weight.shape) == 4:
998+
# 2D padding: needs explicitly state L-R for x,y dims
999+
if type(pad) == int:
1000+
pad = _np.repeat(pad, 4)
1001+
elif len(pad) == 2:
1002+
pad = _np.repeat(pad, 2)
9861003
elif len(weight.shape) == 5:
987-
# 3D: Need to explicitly state F-Bk, L-R, T-B pad
1004+
# 3D padding: needs explicitly state L-R for x,y,z dims
9881005
if type(pad) == int:
9891006
pad = _np.repeat(pad, 6)
9901007
elif len(pad) == 3:

coremltools/converters/mil/frontend/torch/test/test_torch_ops.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,7 @@ class TestConv(TorchBaseTest):
14331433
[
14341434
"compute_unit",
14351435
"backend",
1436+
"scripting",
14361437
"padding",
14371438
"stride",
14381439
"length",
@@ -1444,10 +1445,11 @@ class TestConv(TorchBaseTest):
14441445
]
14451446
),
14461447
[
1447-
(compute_unit, backend, padding, stride, *param)
1448-
for compute_unit, backend, padding, stride, param in itertools.product(
1448+
(compute_unit, backend, scripting, padding, stride, *param)
1449+
for compute_unit, backend, scripting, padding, stride, param in itertools.product(
14491450
[ct.ComputeUnit.CPU_ONLY],
14501451
backends,
1452+
[True, False],
14511453
["same", "valid", 0, 1],
14521454
[1, 2, 3],
14531455
[
@@ -1467,6 +1469,7 @@ def test_convolution1d(
14671469
self,
14681470
compute_unit,
14691471
backend,
1472+
scripting,
14701473
padding,
14711474
stride,
14721475
length,
@@ -1495,13 +1498,15 @@ def test_convolution1d(
14951498
model,
14961499
backend=backend,
14971500
compute_unit=compute_unit,
1501+
use_scripting=scripting,
14981502
)
14991503

15001504
@pytest.mark.parametrize(
15011505
",".join(
15021506
[
15031507
"compute_unit",
15041508
"backend",
1509+
"scripting",
15051510
"padding",
15061511
"stride",
15071512
"height",
@@ -1514,10 +1519,11 @@ def test_convolution1d(
15141519
]
15151520
),
15161521
[
1517-
(compute_unit, backend, padding, stride, *param)
1518-
for compute_unit, backend, padding, stride, param in itertools.product(
1522+
(compute_unit, backend, scripting, padding, stride, *param)
1523+
for compute_unit, backend, scripting, padding, stride, param in itertools.product(
15191524
[ct.ComputeUnit.CPU_ONLY],
15201525
backends,
1526+
[True, False],
15211527
["same", "valid", 1, 0],
15221528
[1, 2, 3],
15231529
[
@@ -1537,6 +1543,7 @@ def test_convolution2d(
15371543
self,
15381544
compute_unit,
15391545
backend,
1546+
scripting,
15401547
padding,
15411548
stride,
15421549
height,
@@ -1550,6 +1557,7 @@ def test_convolution2d(
15501557
):
15511558
if padding == "same" and stride != 1:
15521559
return
1560+
# pytest.skip("iOS16 target not available on macOS 13")
15531561
model = nn.Conv2d(
15541562
in_channels=in_channels,
15551563
out_channels=out_channels,
@@ -1565,13 +1573,15 @@ def test_convolution2d(
15651573
model,
15661574
backend=backend,
15671575
compute_unit=compute_unit,
1576+
use_scripting=scripting,
15681577
)
15691578

15701579
@pytest.mark.parametrize(
15711580
",".join(
15721581
[
15731582
"compute_unit",
15741583
"backend",
1584+
"scripting",
15751585
"padding",
15761586
"stride",
15771587
"depth",
@@ -1585,10 +1595,11 @@ def test_convolution2d(
15851595
]
15861596
),
15871597
[
1588-
(compute_unit, backend, padding, stride, *param)
1589-
for compute_unit, backend, padding, stride, param in itertools.product(
1598+
(compute_unit, backend, scripting, padding, stride, *param)
1599+
for compute_unit, backend, scripting, padding, stride, param in itertools.product(
15901600
[ct.ComputeUnit.CPU_ONLY],
15911601
backends,
1602+
[True, False],
15921603
["same", "valid", 1, 0],
15931604
[1, 2, 3],
15941605
[
@@ -1608,6 +1619,7 @@ def test_convolution3d(
16081619
self,
16091620
compute_unit,
16101621
backend,
1622+
scripting,
16111623
padding,
16121624
stride,
16131625
depth,
@@ -1637,6 +1649,7 @@ def test_convolution3d(
16371649
model,
16381650
backend=backend,
16391651
compute_unit=compute_unit,
1652+
use_scripting=scripting,
16401653
)
16411654

16421655

0 commit comments

Comments
 (0)