junrushao commented on code in PR #663:
URL: https://github.com/apache/tvm-ffi/pull/663#discussion_r3576403344


##########
python/tvm_ffi/cython/function.pxi:
##########
@@ -398,8 +398,10 @@ cdef int TVMFFIPyArgSetterDType_(
 ) except -1:
     """Setter for dtype"""
     cdef object arg = <object>py_arg
-    # dtype is a subclass of str, so this check occur before str
-    arg = arg._tvm_ffi_dtype
+    # tvm_ffi.dtype is a subclass of str, so this check occurs before str.
+    # The internal DataType helper can also be produced by TypeSchema 
converters.
+    if not isinstance(arg, DataType):

Review Comment:
   Done. I split the cached paths: internal `core.DataType` now uses a 
dedicated `TVMFFIPyArgSetterCDataType_` that directly copies the `DLDataType` 
struct, while public `tvm_ffi.dtype` uses the unwrapping setter. The one-time 
factory selects between them, so the cached hot path no longer pays an 
`isinstance` cost. I added a direct `core.DataType` FFI round-trip regression; 
the generated Cython code contains a direct field access with no type check.



##########
python/tvm_ffi/utils/descriptors.py:
##########
@@ -0,0 +1,80 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Descriptor utilities for the tvm_ffi Python package."""
+
+from __future__ import annotations
+
+import importlib
+import sys
+from typing import Any, Callable, Generic, TypeVar, overload
+
+_T = TypeVar("_T")
+
+
+def _get_forward_annotations(obj: Any) -> dict[str, Any]:
+    """Return Python 3.14+ annotations without forcing unresolved names."""
+    annotationlib: Any = importlib.import_module("annotationlib")
+    return annotationlib.get_annotations(obj, 
format=annotationlib.Format.FORWARDREF)
+
+
+class init_property(Generic[_T]):
+    """Auto-registered C++ field with eager computation at ``__init__`` time.

Review Comment:
   Done. I moved `init_property` into `dataclasses/field.py`, export it as 
`tvm_ffi.dataclasses.init_property`, and updated `py_class` plus the tests to 
use the dataclasses API. The generic `utils.descriptors` module and its utils 
re-export are removed; no compatibility shim is retained because this API is 
new in this unmerged PR.



##########
include/tvm/ffi/device.h:
##########
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file tvm/ffi/device.h
+ * \brief Device handling.
+ */
+#ifndef TVM_FFI_DEVICE_H_
+#define TVM_FFI_DEVICE_H_
+
+#include <dlpack/dlpack.h>
+#include <tvm/ffi/string.h>
+#include <tvm/ffi/type_traits.h>
+
+#include <cstdint>
+#include <limits>
+#include <optional>
+#include <string>
+#include <string_view>
+
+namespace tvm {
+namespace ffi {
+namespace details {
+
+TVM_FFI_INLINE static std::optional<DLDeviceType> 
TryParseDLDeviceType(std::string_view name) {
+  if (name == "llvm" || name == "cpu" || name == "c" || name == "test") return 
kDLCPU;

Review Comment:
   Agreed. The core String/Any-to-DLDevice fallback now recognizes only the 
canonical `cpu`, `cuda`, and `opencl` names. I left the existing broader Python 
`Device` name handling unchanged, since this request is specifically about the 
core conversion boundary. The C++ test now rejects every removed target/backend 
alias while keeping native `DLDevice` passthrough unchanged.



##########
tests/cpp/test_device.cc:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <gtest/gtest.h>
+#include <tvm/ffi/any.h>
+#include <tvm/ffi/device.h>
+
+namespace {
+
+using namespace tvm::ffi;
+
+TEST(Device, AnyConversion) {
+  DLDevice device{kDLCUDA, 1};
+  AnyView view = device;
+
+  DLDevice converted = view.cast<DLDevice>();
+  EXPECT_EQ(converted.device_type, kDLCUDA);
+  EXPECT_EQ(converted.device_id, 1);
+}
+
+TEST(Device, AnyConversionWithString) {
+  AnyView cpu_view = "cpu";
+  DLDevice cpu = cpu_view.cast<DLDevice>();
+  EXPECT_EQ(cpu.device_type, kDLCPU);
+  EXPECT_EQ(cpu.device_id, 0);
+
+  Any cuda_any = String("cuda:3");
+  DLDevice cuda = cuda_any.cast<DLDevice>();
+  EXPECT_EQ(cuda.device_type, kDLCUDA);
+  EXPECT_EQ(cuda.device_id, 3);
+
+  AnyView opencl_view = "opencl:-2";

Review Comment:
   Agreed. Textual device indices are now digits-only, so negative or 
explicitly signed spellings are rejected and the sign/minimum-range branches 
are gone. The tests cover negative rejection, acceptance of `INT32_MAX`, and 
rejection of `INT32_MAX + 1`.



-- 
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]

Reply via email to