Skip to content

Commit 28f271f

Browse files
committed
add codex fix
1 parent 24c5908 commit 28f271f

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎lighthouse/utils/torch.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ def to_memref(input: torch.Tensor) -> ctypes.Structure:
8181
input.dim(), torch_dtype_to_ctype(input.dtype)
8282
)
8383
data_ptr = input.data_ptr()
84+
# The allocated field is signed; wrap high-bit XPU addresses as ctypes does.
8485
# PyTorch strides are in element units (unlike NumPy which uses bytes)
8586
return descriptor.from_buffer_copy(
86-
layout.pack(data_ptr, data_ptr, 0, *input.shape, *input.stride())
87+
layout.pack(
88+
ctypes.c_long(data_ptr).value,
89+
data_ptr,
90+
0,
91+
*input.shape,
92+
*input.stride(),
93+
)
8794
)
8895

8996

‎test/execution/torch_to_memref.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""Tests for converting PyTorch tensors to memref descriptors."""
55

66
import ctypes
7+
from types import SimpleNamespace
78

89
import torch
910
from mlir.runtime.np_to_memref import (
@@ -53,6 +54,21 @@ def reference_memref(tensor: torch.Tensor) -> ctypes.Structure:
5354
desc, ref = to_memref(tensor), reference_memref(tensor)
5455
print(f"{name}: same bytes", bytes(desc) == bytes(ref))
5556

57+
# XPU addresses can set the high bit, while the descriptor's allocated field
58+
# is a signed c_long. This synthetic tensor needs no GPU or valid allocation.
59+
high_bit_tensor = SimpleNamespace(
60+
dtype=torch.bfloat16,
61+
shape=(2, 3),
62+
dim=lambda: 2,
63+
data_ptr=lambda: 0xFF00000000200000,
64+
stride=lambda: (3, 1),
65+
)
66+
# CHECK: high-bit pointer: same bytes True
67+
print(
68+
"high-bit pointer: same bytes",
69+
bytes(to_memref(high_bit_tensor)) == bytes(reference_memref(high_bit_tensor)),
70+
)
71+
5672
sliced = to_memref(tensors["sliced"])
5773
# CHECK: sliced shape: [2, 3] strides: [6, 1] data: [8.0, 9.0, 10.0]
5874
print(

0 commit comments

Comments
 (0)