Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions coremltools/converters/mil/frontend/torch/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6591,8 +6591,8 @@ def _parse_keyword_args(context, node, dim, start, end, step) -> Tuple[Var]:

if start == 0 and end is None and step == 1:
# Handling x[:], just pass through the tensor.
x_identity = mb.identity(x=x, name=node.name)
context.add(x_identity)
result = x if types.is_complex(x.dtype) else mb.identity(x=x, name=node.name)
context.add(result, torch_name=node.name)
return

begin_array = [0] * len(x.shape)
Expand Down Expand Up @@ -6629,8 +6629,8 @@ def _parse_keyword_args(context, node, dim, start, end, step) -> Tuple[Var]:
stride_array[dim] = step
kwargs["stride"] = stride_array

res = mb.slice_by_index(**kwargs)
context.add(res)
slice_op = mb.complex_slice_by_index if types.is_complex(x.dtype) else mb.slice_by_index
context.add(slice_op(**kwargs))


@register_torch_op(torch_alias=["split_with_sizes", "split_with_sizes_copy"])
Expand Down
15 changes: 15 additions & 0 deletions coremltools/converters/mil/frontend/torch/test/test_torch_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8531,6 +8531,21 @@ def forward(self, x):
(9,), model, frontend=frontend, backend=backend, compute_unit=compute_unit
)

@pytest.mark.parametrize("backend", backends)
def test_complex_slice(self, backend):
class ComplexSliceModel(torch.nn.Module):
def forward(self, x):
z = torch.complex(x, -x)
sliced = z[:, 1:-1:2]
return torch.stack((sliced.real, sliced.imag))

self.run_compare_torch(
(2, 8),
ComplexSliceModel(),
backend=backend,
converter_input_type=[ct.TensorType(shape=(2, RangeDim(4, 12)))],
)

@pytest.mark.skipif(_python_version() < (3, 6), reason="requires python 3.6")
@pytest.mark.parametrize(
"compute_unit, backend, frontend",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def fft_fft(context, nodes):

from coremltools.converters.mil.mil import operation, types
from coremltools.converters.mil.mil.input_type import DefaultInputs, InputSpec, TensorInputType
from coremltools.converters.mil.mil.ops.defs.iOS15 import slice_by_index
from coremltools.converters.mil.mil.ops.registry import SSAOpRegistry
from coremltools.converters.mil.mil.types.symbolic import any_symbolic, is_symbolic
from coremltools.converters.mil.mil.types.type_mapping import (
Expand Down Expand Up @@ -698,6 +699,14 @@ def type_inference(self):
return types.tensor(output_type, tuple(output_shape))


@register_op(namespace="complex")
class complex_slice_by_index(slice_by_index):
"""Slice a complex tensor with the same indexing rules as slice_by_index."""

input_spec = slice_by_index.input_spec + InputSpec(x=TensorInputType(type_domain="T"))
type_domains = {"T": (types.complex64,)}


@register_op(namespace="complex")
class complex_shape(operation.Operation):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,14 @@ def _lower_complex_stft(op: Operation):
return _wrap_complex_output(op.outputs[0], real, imag)


@LowerComplex.register_lower_func(op_type="complex_slice_by_index")
def _lower_complex_slice_by_index(op: Operation):
indices = {key: value for key, value in op.inputs.items() if key != "x"}
real = mb.slice_by_index(x=op.x.real, **indices)
imag = mb.slice_by_index(x=op.x.imag, **indices)
return _wrap_complex_output(op.outputs[0], real, imag)


@LowerComplex.register_lower_func(op_type="complex_shape")
def _lower_complex_shape(op: Operation):
return mb.shape(x=op.data.real)
Expand Down