|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +import logging |
| 10 | +from typing import Optional |
| 11 | + |
| 12 | +import executorch.exir.schema as schema |
| 13 | + |
| 14 | +import torch |
| 15 | +from executorch.exir.delegate import executorch_call_delegate |
| 16 | +from executorch.exir.lowered_backend_module import LoweredBackendModule |
| 17 | +from executorch.exir.tensor import TensorSpec |
| 18 | +from torch.fx.passes.infra.pass_base import PassBase, PassResult |
| 19 | + |
| 20 | +logger: logging.Logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +# CompileSpec key convention for specifying the target device. |
| 23 | +# Partitioners that target a specific device should include a CompileSpec entry |
| 24 | +# with this key and a value encoding the device string (e.g., b"cuda:0"). |
| 25 | +TARGET_DEVICE_COMPILE_SPEC_KEY = "target_device" |
| 26 | + |
| 27 | +# Mapping from torch.device type strings to schema.DeviceType. |
| 28 | +_DEVICE_STR_TO_ET_DEVICE: dict[str, schema.DeviceType] = { |
| 29 | + "cpu": schema.DeviceType.CPU, |
| 30 | + "cuda": schema.DeviceType.CUDA, |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def _parse_device_spec_value(value: bytes) -> tuple[schema.DeviceType, int]: |
| 35 | + """ |
| 36 | + Parse a target_device CompileSpec value (e.g., b"cuda:0") into |
| 37 | + (DeviceType, device_index). |
| 38 | + """ |
| 39 | + device_str = value.decode("utf-8") |
| 40 | + torch_device = torch.device(device_str) |
| 41 | + device_type = _DEVICE_STR_TO_ET_DEVICE.get(torch_device.type, schema.DeviceType.CPU) |
| 42 | + device_index = torch_device.index if torch_device.index is not None else 0 |
| 43 | + return device_type, device_index |
| 44 | + |
| 45 | + |
| 46 | +def _get_lowered_module( |
| 47 | + graph_module: torch.fx.GraphModule, |
| 48 | + delegate_call_node: torch.fx.Node, |
| 49 | +) -> Optional[LoweredBackendModule]: |
| 50 | + """ |
| 51 | + Given an executorch_call_delegate node, retrieve the associated |
| 52 | + LoweredBackendModule from the graph module. |
| 53 | + The first argument to executorch_call_delegate is a get_attr node |
| 54 | + whose target names the LoweredBackendModule attribute. |
| 55 | + """ |
| 56 | + if len(delegate_call_node.args) < 1: |
| 57 | + return None |
| 58 | + lowered_node = delegate_call_node.args[0] |
| 59 | + if not isinstance(lowered_node, torch.fx.Node) or lowered_node.op != "get_attr": |
| 60 | + return None |
| 61 | + lowered_module = getattr(graph_module, lowered_node.target, None) |
| 62 | + if isinstance(lowered_module, LoweredBackendModule): |
| 63 | + return lowered_module |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +def _get_target_device_from_compile_specs( |
| 68 | + lowered_module: LoweredBackendModule, |
| 69 | +) -> Optional[tuple[schema.DeviceType, int]]: |
| 70 | + """ |
| 71 | + Look for a CompileSpec with key TARGET_DEVICE_COMPILE_SPEC_KEY and return |
| 72 | + the corresponding (DeviceType, device_index), or None if not found. |
| 73 | + """ |
| 74 | + for spec in lowered_module.compile_specs: |
| 75 | + if spec.key == TARGET_DEVICE_COMPILE_SPEC_KEY: |
| 76 | + return _parse_device_spec_value(spec.value) |
| 77 | + return None |
| 78 | + |
| 79 | + |
| 80 | +def _set_device_on_spec( |
| 81 | + spec: TensorSpec, |
| 82 | + device_type: schema.DeviceType, |
| 83 | +) -> None: |
| 84 | + """Set the device attribute on a TensorSpec.""" |
| 85 | + spec.device = device_type |
| 86 | + |
| 87 | + |
| 88 | +class PropagateDevicePass(PassBase): |
| 89 | + """ |
| 90 | + After to_backend, walk the graph and set device metadata on TensorSpecs |
| 91 | + based on partitioner-assigned delegation info. |
| 92 | +
|
| 93 | + Rules: |
| 94 | + 1. Delegated nodes: Output tensors of a delegate call are marked with the |
| 95 | + target device derived from the delegate's CompileSpec (key="target_device"). |
| 96 | + 2. Non-delegated nodes: Remain on CPU (default). |
| 97 | + 3. Getitem nodes that extract from a delegate call inherit the device from |
| 98 | + the delegate call's output spec at the corresponding index. |
| 99 | + """ |
| 100 | + |
| 101 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 102 | + changed = False |
| 103 | + for node in graph_module.graph.nodes: |
| 104 | + if node.op == "call_function" and node.target == executorch_call_delegate: |
| 105 | + lowered_module = _get_lowered_module(graph_module, node) |
| 106 | + if lowered_module is None: |
| 107 | + continue |
| 108 | + |
| 109 | + result = _get_target_device_from_compile_specs(lowered_module) |
| 110 | + if result is None: |
| 111 | + continue |
| 112 | + |
| 113 | + target_device_type, _device_index = result |
| 114 | + |
| 115 | + # Mark all output TensorSpecs of this delegate call node |
| 116 | + specs = node.meta.get("spec") |
| 117 | + if specs is None: |
| 118 | + continue |
| 119 | + |
| 120 | + if isinstance(specs, TensorSpec): |
| 121 | + _set_device_on_spec(specs, target_device_type) |
| 122 | + changed = True |
| 123 | + elif isinstance(specs, (tuple, list)): |
| 124 | + for s in specs: |
| 125 | + if isinstance(s, TensorSpec): |
| 126 | + _set_device_on_spec(s, target_device_type) |
| 127 | + changed = True |
| 128 | + |
| 129 | + logger.debug( |
| 130 | + "PropagateDevicePass: set device=%s on delegate node %s " |
| 131 | + "(backend=%s)", |
| 132 | + target_device_type, |
| 133 | + node.name, |
| 134 | + lowered_module.backend_id, |
| 135 | + ) |
| 136 | + |
| 137 | + # Second pass: propagate device through getitem nodes that extract |
| 138 | + # individual outputs from a delegate call. |
| 139 | + for node in graph_module.graph.nodes: |
| 140 | + if node.op == "call_function" and node.target.__name__ == "getitem": |
| 141 | + source_node = node.args[0] |
| 142 | + if ( |
| 143 | + isinstance(source_node, torch.fx.Node) |
| 144 | + and source_node.op == "call_function" |
| 145 | + and source_node.target == executorch_call_delegate |
| 146 | + ): |
| 147 | + spec = node.meta.get("spec") |
| 148 | + source_specs = source_node.meta.get("spec") |
| 149 | + idx = node.args[1] |
| 150 | + if ( |
| 151 | + spec is not None |
| 152 | + and isinstance(spec, TensorSpec) |
| 153 | + and source_specs is not None |
| 154 | + and isinstance(source_specs, (tuple, list)) |
| 155 | + and isinstance(idx, int) |
| 156 | + and idx < len(source_specs) |
| 157 | + ): |
| 158 | + source_spec = source_specs[idx] |
| 159 | + if isinstance(source_spec, TensorSpec): |
| 160 | + _set_device_on_spec(spec, source_spec.device) |
| 161 | + changed = True |
| 162 | + |
| 163 | + return PassResult(graph_module, changed) |
0 commit comments