Skip to content

Commit 4fe4d86

Browse files
authored
Keep YOLO26Model in contiguous layout after predict() (pytorch#23505)
### Summary Part of pytorch#23453 (case 1: ultralytics 8.4.133 to 8.4.139). Starting with 8.4.133, ultralytics converts the model to `channels_last` during `predict()` on x86 Linux/Windows CPUs with oneDNN. `YOLO26Model` calls `predict()` once during setup, so the exported program ends up with channels-last conv weights and a contiguous example input. The portable convolution kernel then rejects this with `tensors_have_same_dim_order(in, out)` (`0x12` at instruction 0:0). This change converts the model back to the default layout after `predict()`, both in `YOLO26Model` (`model.py`) and in `export_and_validate.py`, which follows the same pattern. With the pinned `ultralytics==8.4.14`, this is a no-op. Passing `channels_last=False` to `predict()`, as `examples/riscv` does, isn't an option while we're pinned to 8.4.14, since that version rejects the argument. This does not address case 2 (8.4.140+, `PendingUnbackedSymbolNotFound` during strict export). ### Test plan Tested on main @ [238506a](pytorch@238506a), Ubuntu 24.04 (WSL2), CPU-only torch 2.14.0: ```bash python -m examples.portable.scripts.export --model_name=yolo26 --strict ./cmake-out/executor_runner --model_path yolo26.pte ``` | ultralytics | before | after | | ----------- | ------------------------- | ------------------------- | | 8.4.14 | runs, outputs match eager | runs, outputs match eager | | 8.4.133 | fails at instruction 0:0 | runs, outputs match eager | | 8.4.137 | fails at instruction 0:0 | runs, outputs match eager | Outputs were compared against eager through the portable pybindings (`atol=1e-3`). The `(1, 300, 6)` top-k output can reorder near-tied scores with random-noise inputs, including on unmodified main, so for that output I compared sorted scores instead (8/8 seeds match). No automated test was added. CI currently runs yolo26 with the pinned 8.4.14, where this change is a no-op, and a unit test would require installing ultralytics and downloading the weights. Happy to add one if there is a preferred place for it. Authored with assistance from Claude Code. cc @nil-is-all
1 parent d9649f7 commit 4fe4d86

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

‎examples/models/yolo26/export_and_validate.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ def main(
236236
np_dummy_tensor = np.ones((input_dims[0], input_dims[1], 3))
237237
model.predict(np_dummy_tensor, imgsz=((input_dims[0], input_dims[1])), device="cpu")
238238

239-
pt_model = model.model.to(torch.device("cpu"))
239+
# ultralytics 8.4.133-8.4.139 switch to channels_last in predict(); undo it.
240+
pt_model = model.model.to(
241+
torch.device("cpu"), memory_format=torch.contiguous_format
242+
)
240243

241244
def transform_fn(frame):
242245
input_tensor = model.predictor.preprocess([frame])

‎examples/models/yolo26/model.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def __init__(self, model_name: str = "yolo26n.pt"):
1818
self.yolo = YOLO(model_name)
1919
self.dummy_frame = torch.randn((320, 320, 3)).to(torch.uint8).numpy()
2020
self.yolo.predict(self.dummy_frame, imgsz=(320, 320), verbose=False)
21-
self.model = self.yolo.model.eval()
21+
# ultralytics 8.4.133-8.4.139 switch to channels_last in predict(); undo it.
22+
self.model = self.yolo.model.eval().to(memory_format=torch.contiguous_format)
2223

2324
def get_eager_model(self) -> torch.nn.Module:
2425
logging.info("Loading " + self.model_name + " model")

0 commit comments

Comments
 (0)