This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[Retiarii] Bugfix: wrong device placement and invalid CUDA ordinal when using CGO engine #4086
Merged
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ac7f94c
update interface of CGO's BypassAccelerator to latest
hzhua afbb014
set pytorch-lightning version >= 1.4.2 in recommanded.txt
hzhua 822329d
nit (lint)
hzhua 3fdfdc8
Add device placement in CGO Engine with new device interface
hzhua cc5a48e
fix bug: single host placement of multiple GPUs is wrongly identified…
hzhua fa943a4
fix lint
hzhua 98d3c57
fix typo
hzhua bcdd06e
fix lint
hzhua bdf0160
fix lint
hzhua 6a48572
don't generate cuda mapping when placement is None
hzhua d0d52ea
Merge remote-tracking branch 'upstream/master' into fix-placement-cgo
hzhua 0f96890
Merge remote-tracking branch 'upstream/master' into fix-placement-cgo
hzhua 0f5b458
add explain for overridden_device_repr
hzhua 5b47e79
format
hzhua 005fd4e
move CPUDevice to nni.common.device
hzhua 031bfc4
docstring and comments for assemble and logical nodes
hzhua b196726
fix lint
hzhua 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 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 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 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 |
---|---|---|
|
@@ -2,7 +2,10 @@ | |
# Licensed under the MIT license. | ||
|
||
import logging | ||
from typing import List, Tuple, Any | ||
from typing import Dict, List, Tuple, Any | ||
|
||
from nni.retiarii.operation_def.torch_op_def import ToDevice | ||
from nni.common.device import Device, GPUDevice | ||
|
||
from ..graph import IllegalGraphError, Edge, Graph, Node, Model | ||
|
||
|
@@ -70,7 +73,7 @@ def _format_inputs(node: Node) -> Tuple[List[str], List[Any]]: | |
# when the input comes from a single-output operator | ||
inputs.append('{}'.format(edge.head.name)) | ||
if edge.head.operation.type in ('prim::Constant', 'prim::GetAttr') and \ | ||
'value' in edge.head.operation.parameters: | ||
'value' in edge.head.operation.parameters: | ||
inputs_value.append(edge.head.operation.parameters['value']) | ||
else: | ||
inputs_value.append(None) | ||
|
@@ -98,15 +101,39 @@ def _remove_prefix(names, graph_name): | |
return names[len(graph_name):] if names.startswith(graph_name) else names | ||
|
||
|
||
def generate_cuda_mapping(placement: Dict[Node, Device]) -> Dict[Device, int]: | ||
''' | ||
Since CUDA_VISIBLE_DEVICES will be set to the list of real GPU ID, | ||
we need to remap the GPU ID when generating code to match them correctly. | ||
For example, when CUDA_VISIBLE_DEVICES="0,3", we need to use "cuda:0", "cuda:1" in the generated code. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't get the point, why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nni_manager sets When Thus, when generating code that explicitly place operations (e.g., |
||
''' | ||
unique_devices = sorted(list(set([e for e in placement.values() if isinstance(e, GPUDevice)]))) | ||
node_gpu_cnt = {} | ||
cuda_remapped_id = {} | ||
for d in unique_devices: | ||
if d.node_id not in node_gpu_cnt: | ||
node_gpu_cnt[d.node_id] = 0 | ||
node_gpu_cnt[d.node_id] += 1 | ||
cuda_remapped_id[d] = node_gpu_cnt[d.node_id] - 1 | ||
|
||
return cuda_remapped_id | ||
|
||
|
||
def graph_to_pytorch_model(graph_name: str, graph: Graph, placement=None) -> str: | ||
nodes = graph.topo_sort() | ||
|
||
# handle module node and function node differently | ||
# only need to generate code for module here | ||
import_pkgs = set() | ||
node_codes = [] | ||
cuda_remapped_id = None | ||
if placement: | ||
cuda_remapped_id = generate_cuda_mapping(placement) | ||
for node in nodes: | ||
if node.operation: | ||
if placement and isinstance(node.operation, ToDevice): | ||
node.operation.override_device_repr("cuda:%d" % cuda_remapped_id[node.operation.device]) | ||
|
||
if node.operation.type == 'shared': | ||
continue | ||
pkg_name = node.operation.get_import_pkg() | ||
|
@@ -115,7 +142,11 @@ def graph_to_pytorch_model(graph_name: str, graph: Graph, placement=None) -> str | |
node_code = node.operation.to_init_code(_remove_prefix(node.name, graph_name)) | ||
if node_code is not None: | ||
if placement and node in placement and len(node_code) > 0: | ||
node_codes.append(f"{node_code}.to('{placement[node].device_repr()}')") | ||
if isinstance(placement[node], GPUDevice): | ||
device_repr = "cuda:%d" % cuda_remapped_id[placement[node]] | ||
else: | ||
device_repr = placement[node].device_repr() | ||
node_codes.append(f"{node_code}.to('{device_repr}')") | ||
else: | ||
node_codes.append(node_code) | ||
|
||
|
This file contains 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this typical usage of
@dataclass
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it is typical. Since the dataclass
GPUDevice
inherits the dataclassDevice
, the order in__init__
by default should benode_id, status, gpu_id
, which looks not nature to human. So I explicitly declare the order here with the expected order.