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
6 changes: 6 additions & 0 deletions runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ def execute(self, inputs: Sequence[Any]) -> Sequence[Any]:
Returns:
A list of output values, typically torch.Tensor objects.
"""
import torch
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Importing torch inside execute() adds overhead on every call and also changes when ImportError would surface (now at runtime call time). Prefer a module-level import, or at least import lazily only when a torch.Tensor is actually present (e.g., scan inputs first), optionally with a clear error if torch isn't available.

Copilot uses AI. Check for mistakes.

inputs = [
x.contiguous() if isinstance(x, torch.Tensor) and not x.is_contiguous() else x
for x in inputs
]
return self._method(inputs)
Comment on lines +149 to 153
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This always allocates a new Python list and iterates all inputs even when all tensors are already contiguous (or when there are no tensors). For a hot path like execute(), consider only creating a copied container if at least one replacement is needed; otherwise, pass through the original inputs unchanged to avoid unnecessary allocations.

Suggested change
inputs = [
x.contiguous() if isinstance(x, torch.Tensor) and not x.is_contiguous() else x
for x in inputs
]
return self._method(inputs)
converted_inputs = None
for i, x in enumerate(inputs):
if isinstance(x, torch.Tensor) and not x.is_contiguous():
if converted_inputs is None:
converted_inputs = list(inputs)
converted_inputs[i] = x.contiguous()
return self._method(converted_inputs if converted_inputs is not None else inputs)

Copilot uses AI. Check for mistakes.

@property
Expand Down
18 changes: 18 additions & 0 deletions runtime/test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ def test_add(program):
program = runtime.load_program(f.read())
test_add(program)

def test_execute_non_contiguous_inputs(self):
"""Non-contiguous tensors (e.g. after permute) must produce the same
result as their contiguous equivalents."""
ep, inputs = create_program(ModuleAdd())
runtime = Runtime.get()
program = runtime.load_program(ep.buffer, verification=Verification.Minimal)

# Make a non-contiguous version of the first input via transpose.
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says the non-contiguous tensor is created 'via transpose', but the code uses unsqueeze/expand/permute and slicing. Update the comment to match the actual approach to avoid confusion during future maintenance.

Suggested change
# Make a non-contiguous version of the first input via transpose.
# Make a non-contiguous version of the first input via
# unsqueeze/expand/permute followed by slicing.

Copilot uses AI. Check for mistakes.
x = inputs[0] # shape (2, 2)
non_contig = x.unsqueeze(0).expand(3, -1, -1).permute(1, 2, 0)[:, :, 0]
self.assertFalse(non_contig.is_contiguous())
self.assertTrue(torch.equal(non_contig, x))

method = program.load_method("forward")
out_non_contig = method.execute([non_contig, inputs[1]])[0]
out_contig = method.execute([x, inputs[1]])[0]
self.assertTrue(torch.allclose(out_non_contig, out_contig))

def test_load_program_with_file_like_objects(self):
"""Regression test: Ensure file-like objects (BytesIO, etc.) work correctly.

Expand Down
Loading