junrushao commented on code in PR #15714: URL: https://github.com/apache/tvm/pull/15714#discussion_r1323593544
########## python/tvm/target/detect_target.py: ########## @@ -0,0 +1,169 @@ +# 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. +"""Detect target.""" +from typing import Union + +from . import Target +from .._ffi import get_global_func +from .._ffi.runtime_ctypes import Device +from ..runtime.ndarray import device + + +def _detect_metal_host() -> "Target": + target_triple = get_global_func("tvm.codegen.llvm.GetDefaultTargetTriple")() + process_triple = get_global_func("tvm.codegen.llvm.GetProcessTriple")() + host_cpu = get_global_func("tvm.codegen.llvm.GetHostCPUName")() + print( + f"Host CPU dection:\n Target triple: {target_triple}\n " + f"Process triple: {process_triple}\n Host CPU: {host_cpu}" + ) + if target_triple.startswith("x86_64-"): + return Target( + { + "kind": "llvm", + "mtriple": "x86_64-apple-macos", + "mcpu": host_cpu, + } + ) + # should start with "arm64-" + return Target( + { + "kind": "llvm", + "mtriple": "arm64-apple-macos", + "mcpu": host_cpu, + } + ) + + +def _detect_metal(dev: Device) -> Target: + return Target( + { + "kind": "metal", + "max_shared_memory_per_block": 32768, + "max_threads_per_block": dev.max_threads_per_block, + "thread_warp_size": 32, + }, + host=_detect_metal_host(), + ) + + +def _detect_cuda(dev: Device) -> Target: + return Target( + { + "kind": "cuda", + "max_shared_memory_per_block": dev.max_shared_memory_per_block, + "max_threads_per_block": dev.max_threads_per_block, + "thread_warp_size": dev.warp_size, + "registers_per_block": 65536, + "arch": "sm_" + dev.compute_version.replace(".", ""), + } + ) + + +def _detect_rocm(dev: Device) -> Target: + return Target( + { + "kind": "rocm", + "max_shared_memory_per_block": dev.max_shared_memory_per_block, + "max_threads_per_block": dev.max_threads_per_block, + "thread_warp_size": dev.warp_size, + } + ) + + +def _detect_vulkan(dev: Device) -> Target: + return Target( + { + "kind": "vulkan", + "max_threads_per_block": dev.max_threads_per_block, + "max_shared_memory_per_block": dev.max_shared_memory_per_block, + "thread_warp_size": dev.warp_size, + "supports_float16": 1, + "supports_int16": 1, + "supports_int8": 1, + "supports_16bit_buffer": 1, + } + ) + + +def _detect_opencl(dev: Device) -> Target: # pylint: disable=unused-argument + return Target("opencl") + + +def _detect_cpu(dev: Device) -> Target: # pylint: disable=unused-argument + return Target("llvm") + + +def auto_detect_local_target() -> Target: + """Automatically detects the target representing a local GPU or falls + back to CPU as a target if no compatible GPU is found. Supported + local GPU types: ["cuda", "metal", "rocm", "vulkan", "opencl"]. + + Returns + ------- + target : Target + The detected target. + """ + for device_type in ["metal", "rocm", "cuda", "vulkan", "opencl"]: + dev = device(device_type) + if not dev.exist: + continue + print(f"Detect local GPU type: {device_type}") + target = SUPPORT_DEVICE[device_type](dev) + return target + + print("Failed to detect local GPU, falling back to CPU as a target") + return _detect_cpu(device("cpu")) + + +def detect_target_from_device(dev: Union[str, Device]) -> Target: + """Detects Target associated with the given device. If the device does not exist, + there will be an Error. + + Parameters + ---------- + dev : Union[str, Device] + The device to detect the target for. + Supported device types: ["cuda", "metal", "rocm", "vulkan", "opencl", "cpu"] + + Returns + ------- + target : Target + The detected target. + """ + if isinstance(dev, str): + dev = device(dev) + device_type = Device.MASK2STR[dev.device_type] + if device_type not in SUPPORT_DEVICE: + raise ValueError( + f"Unsupported device type: {device_type}, " + f"currently auto detection only supports {SUPPORT_DEVICE.keys()}" + ) + if not dev.exist: + raise ValueError(f"Device {dev} does not exist in the local environment.") Review Comment: ```suggestion if not dev.exist: raise ValueError(f"Cannot detect device `{dev}`. Please make sure the device and its driver is installed properly, and TVM is compiled with the driver") ``` -- 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]
