11import ctypes
2+ import struct
3+ from functools import cache
24
35import torch
46from mlir import ir
1113
1214from . import memref as memref_utils
1315
16+ _DTYPE_TO_CTYPE = {
17+ torch .float16 : F16 ,
18+ torch .bfloat16 : BF16 ,
19+ torch .float32 : ctypes .c_float ,
20+ torch .float64 : ctypes .c_double ,
21+ torch .int8 : ctypes .c_int8 ,
22+ torch .int16 : ctypes .c_int16 ,
23+ torch .int32 : ctypes .c_int32 ,
24+ torch .int64 : ctypes .c_int64 ,
25+ torch .uint8 : ctypes .c_uint8 ,
26+ torch .bool : ctypes .c_bool ,
27+ }
28+
1429
1530def torch_dtype_to_ctype (dtype : torch .dtype ):
1631 """
@@ -21,49 +36,55 @@ def torch_dtype_to_ctype(dtype: torch.dtype):
2136 Returns:
2237 Corresponding ctype
2338 """
24- _dtype_map = {
25- torch .float16 : F16 ,
26- torch .bfloat16 : BF16 ,
27- torch .float32 : ctypes .c_float ,
28- torch .float64 : ctypes .c_double ,
29- torch .int8 : ctypes .c_int8 ,
30- torch .int16 : ctypes .c_int16 ,
31- torch .int32 : ctypes .c_int32 ,
32- torch .int64 : ctypes .c_int64 ,
33- torch .uint8 : ctypes .c_uint8 ,
34- torch .bool : ctypes .c_bool ,
35- }
36- if dtype not in _dtype_map :
39+ if dtype not in _DTYPE_TO_CTYPE :
3740 raise ValueError (f"Unsupported dtype: { dtype } " )
38- return _dtype_map [dtype ]
41+ return _DTYPE_TO_CTYPE [dtype ]
42+
43+
44+ def _struct_format (field_type : type ) -> str :
45+ """The `struct` format of a ctypes field type."""
46+ if issubclass (field_type , ctypes .Array ):
47+ return f"{ field_type ._length_ } { _struct_format (field_type ._type_ )} "
48+ if issubclass (field_type , ctypes ._Pointer ):
49+ return "P"
50+ return field_type ._type_
51+
52+
53+ @cache
54+ def _memref_descriptor (ndim : int , ctype : type ) -> tuple [type , struct .Struct ]:
55+ """The memref descriptor class of a rank and element type, and its layout.
56+
57+ A new class per call would also make ctypes create new pointer types.
58+ """
59+ if ndim == 0 :
60+ descriptor = make_zero_d_memref_descriptor (ctype )
61+ else :
62+ descriptor = make_nd_memref_descriptor (ndim , ctype )
63+ layout = struct .Struct (
64+ "" .join (_struct_format (field_type ) for _ , field_type in descriptor ._fields_ )
65+ )
66+ if layout .size != ctypes .sizeof (descriptor ):
67+ raise RuntimeError (f"Unexpected memref descriptor layout: { layout .format } " )
68+ return descriptor , layout
3969
4070
4171def to_memref (input : torch .Tensor ) -> ctypes .Structure :
4272 """
4373 Convert a PyTorch tensor into a memref descriptor.
4474
75+ The descriptor does not keep the tensor alive.
76+
4577 Args:
4678 input: PyTorch tensor.
4779 """
48- ctp = torch_dtype_to_ctype (input .dtype )
49- ndim = input .dim ()
80+ descriptor , layout = _memref_descriptor (
81+ input .dim (), torch_dtype_to_ctype (input .dtype )
82+ )
5083 data_ptr = input .data_ptr ()
51-
52- if ndim == 0 :
53- x = make_zero_d_memref_descriptor (ctp )()
54- x .allocated = data_ptr
55- x .aligned = ctypes .cast (data_ptr , ctypes .POINTER (ctp ))
56- x .offset = ctypes .c_longlong (0 )
57- return x
58-
59- x = make_nd_memref_descriptor (ndim , ctp )()
60- x .allocated = data_ptr
61- x .aligned = ctypes .cast (data_ptr , ctypes .POINTER (ctp ))
62- x .offset = ctypes .c_longlong (0 )
63- x .shape = (ctypes .c_longlong * ndim )(* input .shape )
6484 # PyTorch strides are in element units (unlike NumPy which uses bytes)
65- x .strides = (ctypes .c_longlong * ndim )(* input .stride ())
66- return x
85+ return descriptor .from_buffer_copy (
86+ layout .pack (data_ptr , data_ptr , 0 , * input .shape , * input .stride ())
87+ )
6788
6889
6990def to_packed_args (inputs : list [torch .Tensor ]) -> ctypes .Array [ctypes .c_void_p ]:
0 commit comments