tqchen commented on code in PR #49:
URL: https://github.com/apache/tvm-ffi/pull/49#discussion_r2376736408
##########
python/tvm_ffi/cython/object.pxi:
##########
@@ -261,29 +263,53 @@ cdef inline object make_ret_opaque_object(TVMFFIAny
result):
(<Object>obj).chandle = result.v_obj
return obj.pyobject()
+cdef inline object make_fallback_cls_for_type_index(int32_t type_index):
+ cdef str type_key = _type_index_to_key(type_index)
+ cdef object type_info = _lookup_type_info_from_type_key(type_key)
+ cdef object parent_type_info = type_info.parent_type_info
+ cdef object cls
+ cdef dict ann
+
+ assert type_info.type_cls is None
+ assert parent_type_info is not None
+
+ warnings.warn(
+ f"Returning type `{type_key}` which is not registered via
`@register_object`, "
+ f"falling back to auto-generated
prototype:\n{type_info.prototype_py()}",
+ )
+
+ from tvm_ffi.dataclasses import c_class
+
+ class cls(parent_type_info.type_cls):
+ pass
+
+ ann = dict(getattr(cls, "__annotations__", {}))
+ for field in type_info.fields:
+ ann[field.name] = Any
+ cls.__annotations__ = ann
+ cls = c_class(type_info.type_key)(cls)
+ assert type_info.type_cls is cls
+ return cls
+
cdef inline object make_ret_object(TVMFFIAny result):
- global TYPE_INDEX_TO_INFO
- cdef int32_t tindex
- cdef object cls
- tindex = result.type_index
-
- if tindex < len(TYPE_INDEX_TO_CLS):
- cls = TYPE_INDEX_TO_CLS[tindex]
- if cls is not None:
- if issubclass(cls, PyNativeObject):
- obj = Object.__new__(Object)
- (<Object>obj).chandle = result.v_obj
- return cls.__from_tvm_ffi_object__(cls, obj)
- obj = cls.__new__(cls)
- (<Object>obj).chandle = result.v_obj
- return obj
+ cdef int32_t type_index
+ cdef object cls, obj
+ type_index = result.type_index
- # object is not found in registered entry
- # in this case we need to report an warning
- type_key = _type_index_to_key(tindex)
- warnings.warn(f"Returning type `{type_key}` which is not registered via
register_object, fallback to Object")
- obj = _CLASS_OBJECT.__new__(_CLASS_OBJECT)
+ if type_index < len(TYPE_INDEX_TO_CLS) and (cls :=
TYPE_INDEX_TO_CLS[type_index]) is not None:
+ if issubclass(cls, PyNativeObject):
+ obj = Object.__new__(Object)
+ (<Object>obj).chandle = result.v_obj
+ return cls.__from_tvm_ffi_object__(cls, obj)
+ else:
+ pass
+ else:
+ # Slow path: object is not found in registered entry
+ # In this case we need to report a warning, create a dummy stub class
+ # and register the class for future usage.
+ cls = make_fallback_cls_for_type_index(type_index)
Review Comment:
did we register for future? we should update TYPE_INDEX_TO_CLS so that next
time it is not so slow
Maybe the function already did that, in which case the comment should
explicitly say so, maybe rename to
`make_and_register_fallback_cls_for_type_index`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]