-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Target][Device] Auto detect target and create device from str in torch style #15714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
junrushao
merged 1 commit into
apache:main
from
LeshengJin:NN/feature/auto_device_target
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # 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(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": dev.warp_size, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| 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, | ||
| "arch": "sm_" + dev.compute_version.replace(".", ""), | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def _detect_rocm(dev: Device) -> Target: | ||
| return Target( | ||
| { | ||
| "kind": "rocm", | ||
| "mtriple": "amdgcn-and-amdhsa-hcc", | ||
| "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: | ||
| f_get_target_property = get_global_func("device_api.vulkan.get_target_property") | ||
| 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": f_get_target_property(dev, "supports_float16"), | ||
| "supports_int16": f_get_target_property(dev, "supports_int16"), | ||
| "supports_int8": f_get_target_property(dev, "supports_int8"), | ||
| "supports_16bit_buffer": f_get_target_property(dev, "supports_16bit_buffer"), | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| 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"] | ||
|
|
||
| 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"Auto detection for device `{device_type}` is not supported. " | ||
| f"Currently only supports: {SUPPORT_DEVICE.keys()}" | ||
| ) | ||
| 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" | ||
| ) | ||
| target = SUPPORT_DEVICE[device_type](dev) | ||
| return target | ||
|
|
||
|
|
||
| SUPPORT_DEVICE = { | ||
| "cuda": _detect_cuda, | ||
| "metal": _detect_metal, | ||
| "vulkan": _detect_vulkan, | ||
| "rocm": _detect_rocm, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # 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. | ||
| import pytest | ||
|
|
||
| import tvm | ||
| import tvm.testing | ||
| from tvm._ffi.runtime_ctypes import Device | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "dev_str, expected_device_type, expect_device_id", | ||
| [ | ||
| ("cpu", Device.kDLCPU, 0), | ||
| ("cuda", Device.kDLCUDA, 0), | ||
| ("cuda:0", Device.kDLCUDA, 0), | ||
| ("cuda:3", Device.kDLCUDA, 3), | ||
| ("metal:2", Device.kDLMetal, 2), | ||
| ], | ||
| ) | ||
| def test_device(dev_str, expected_device_type, expect_device_id): | ||
| dev = tvm.device(dev_str) | ||
| assert dev.device_type == expected_device_type | ||
| assert dev.device_id == expect_device_id | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "dev_type, dev_id, expected_device_type, expect_device_id", | ||
| [ | ||
| ("cpu", 0, Device.kDLCPU, 0), | ||
| ("cuda", 0, Device.kDLCUDA, 0), | ||
| (Device.kDLCUDA, 0, Device.kDLCUDA, 0), | ||
| ("cuda", 3, Device.kDLCUDA, 3), | ||
| (Device.kDLMetal, 2, Device.kDLMetal, 2), | ||
| ], | ||
| ) | ||
| def test_device_with_dev_id(dev_type, dev_id, expected_device_type, expect_device_id): | ||
| dev = tvm.device(dev_type=dev_type, dev_id=dev_id) | ||
| assert dev.device_type == expected_device_type | ||
| assert dev.device_id == expect_device_id | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "dev_type, dev_id", | ||
| [ | ||
| ("cpu:0:0", None), | ||
| ("cpu:?", None), | ||
| ("cpu:", None), | ||
| (Device.kDLCUDA, "?"), | ||
| ], | ||
| ) | ||
| def test_deive_error(dev_type, dev_id): | ||
| with pytest.raises(ValueError): | ||
| dev = tvm.device(dev_type=dev_type, dev_id=dev_id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tvm.testing.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.