Skip to content

Adding Support for preserve_format when converting Evalues #2242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions exir/emit/_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,11 @@ def _constant_to_evalue( # noqa: C901
return EValue(Int(layout_enum(val)))

if isinstance(val, torch.memory_format):
if val != torch.contiguous_format:
if val != torch.contiguous_format and val != torch.preserve_format:
raise InternalError(
self._emit_node_specific_error(
self.node,
"Non contiguous tensors are not supported in ExecuTorch",
"Only Tensors that have a contiguous or preserve_format memory_format are supported in ExecuTorch",
)
)
return EValue(Int(memory_format_enum(val)))
Expand Down
42 changes: 42 additions & 0 deletions exir/emit/test/test_emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from executorch.exir.backend.backend_api import to_backend
from executorch.exir.backend.backend_details import BackendDetails, PreprocessResult
from executorch.exir.emit import emit_program # noqa
from executorch.exir.error import InternalError
from executorch.exir.passes.constant_prop_pass import constant_prop_pass
from executorch.exir.passes.sym_shape_eval_pass import ConstraintBasedSymShapeEvalPass
from executorch.exir.print_program import pretty_print, print_program # noqa
Expand Down Expand Up @@ -836,6 +837,47 @@ def _compare_execution_plans(
else:
self.assertEqual(single_val, merged_val)

def test_emit_memory_format(self) -> None:
class SimpleLinear(torch.nn.Module):
def __init__(self) -> None:
super().__init__()

def forward(self, x: torch.Tensor) -> torch.Tensor:
contiguous = x.to(
dtype=torch.float32, memory_format=torch.contiguous_format
)
preserve = x.to(
dtype=torch.float32, memory_format=torch.preserve_format
)
return contiguous + preserve

# Should succeed at exporting model with legal memory format (contiguous, preserve)
model = SimpleLinear()
inputs = (torch.ones(10, 5),)
try:
to_edge(
export(model, inputs),
compile_config=exir.EdgeCompileConfig(_check_ir_validity=False),
).to_executorch()
except:
self.fail("Failed to export model with legal memory format")

class SimpleLinear2(torch.nn.Module):
def __init__(self) -> None:
super().__init__()

def forward(self, x: torch.Tensor) -> torch.Tensor:
return x.to(dtype=torch.float32, memory_format=torch.channels_last)

# Failure Expected when exporting model with illegal memory format (channels_last)
model = SimpleLinear2()
inputs = (torch.ones(10, 5, 2, 1),)
with self.assertRaises(InternalError):
to_edge(
export(model, inputs),
compile_config=exir.EdgeCompileConfig(_check_ir_validity=False),
).to_executorch()

def test_emit_multiple_entry_points(self) -> None:
class SimpleLinear(torch.nn.Module):
def __init__(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions exir/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def memory_format_enum(memory_format: torch.memory_format) -> int:
)
table = {
torch.contiguous_format: 0,
torch.preserve_format: 1,
}
return table[memory_format]

Expand Down