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


##########
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;
+  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);
+}

Review Comment:
   it's too defensive



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