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


##########
include/tvm/ffi/type_traits.h:
##########
@@ -536,6 +537,88 @@ struct TypeTraits<void*> : public TypeTraitsBase {
   }
 };
 
+namespace details {
+
+TVM_FFI_INLINE static std::optional<std::string_view> TryGetStringViewFromAny(
+    const TVMFFIAny* src) {
+  if (src->type_index == TypeIndex::kTVMFFIRawStr) {
+    return std::string_view(src->v_c_str);
+  }
+  if (src->type_index == TypeIndex::kTVMFFISmallStr) {
+    TVMFFIByteArray bytes = TVMFFISmallBytesGetContentByteArray(src);
+    return std::string_view(bytes.data, bytes.size);
+  }
+  if (src->type_index == TypeIndex::kTVMFFIStr) {
+    TVMFFIByteArray* bytes = TVMFFIBytesGetByteArrayPtr(src->v_obj);
+    return std::string_view(bytes->data, bytes->size);
+  }
+  return std::nullopt;
+}
+
+TVM_FFI_INLINE static std::optional<DLDeviceType> 
TryParseDLDeviceType(std::string_view name) {
+  if (name == "llvm" || name == "cpu" || name == "c" || name == "test") return 
kDLCPU;
+  if (name == "cuda" || name == "nvptx") return kDLCUDA;
+  if (name == "cl" || name == "opencl") return kDLOpenCL;
+  if (name == "vulkan") return kDLVulkan;
+  if (name == "metal") return kDLMetal;
+  if (name == "vpi") return kDLVPI;
+  if (name == "rocm") return kDLROCM;
+  if (name == "ext_dev") return kDLExtDev;
+  if (name == "hexagon") return kDLHexagon;
+  if (name == "webgpu") return kDLWebGPU;
+  if (name == "maia") return kDLMAIA;
+  if (name == "trn") return kDLTrn;
+  return std::nullopt;
+}
+
+TVM_FFI_INLINE static std::optional<int32_t> 
TryParseDLDeviceIndex(std::string_view index) {
+  if (index.empty()) return std::nullopt;
+  int64_t sign = 1;
+  size_t pos = 0;
+  if (index[0] == '-') {
+    sign = -1;
+    pos = 1;
+  }
+  if (pos >= index.size()) return std::nullopt;
+  int64_t value = 0;
+  for (; pos < index.size(); ++pos) {
+    char ch = index[pos];
+    if (ch < '0' || ch > '9') return std::nullopt;
+    value = value * 10 + (ch - '0');
+    if (value > std::numeric_limits<int32_t>::max()) return std::nullopt;
+  }
+  value *= sign;
+  if (value < std::numeric_limits<int32_t>::min() || value > 
std::numeric_limits<int32_t>::max()) {
+    return std::nullopt;
+  }
+  return static_cast<int32_t>(value);
+}
+
+TVM_FFI_INLINE static std::optional<DLDevice> 
TryStringViewToDLDevice(std::string_view str) {
+  size_t space_pos = str.find(' ');
+  if (space_pos != std::string_view::npos) {
+    str = str.substr(0, space_pos);
+  }
+  size_t colon_pos = str.find(':');
+  std::string_view name = colon_pos == std::string_view::npos ? str : 
str.substr(0, colon_pos);
+  if (name.empty()) return std::nullopt;
+  if (str.find(':', colon_pos == std::string_view::npos ? str.size() : 
colon_pos + 1) !=
+      std::string_view::npos) {
+    return std::nullopt;
+  }
+  std::optional<DLDeviceType> device_type = TryParseDLDeviceType(name);

Review Comment:
   now that we do string handling perhaps worthadding ffi/device.h like dtype.h



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