|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +""" |
| 18 | +Functions for processing dynamic workspace pool TVMC args |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +import logging |
| 23 | +import re |
| 24 | + |
| 25 | +from tvm.driver.tvmc import TVMCException |
| 26 | +from tvm.target import Target |
| 27 | +from tvm.ir.memory_pools import PoolInfoProperties, WorkspaceMemoryPools, WorkspacePoolInfo |
| 28 | + |
| 29 | + |
| 30 | +# pylint: disable=invalid-name |
| 31 | +logger = logging.getLogger("TVMC") |
| 32 | + |
| 33 | + |
| 34 | +def generate_workspace_pools_args(parser): |
| 35 | + """Generates arguments for each Workspace Pools's options""" |
| 36 | + parser.add_argument( |
| 37 | + "--workspace-pools", |
| 38 | + help="""The name of the memory pool |
| 39 | + Example usage: --workspace-pools=flash""", |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + "--workspace-pools-targets", |
| 43 | + help="""The name of the targets specified for the memory pool |
| 44 | + Example usage: --workspace-pools-targets=flash:llvm""", |
| 45 | + action="append", |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + "--workspace-pools-size-hint-bytes", |
| 49 | + nargs="?", |
| 50 | + help="""The expected size hint to be used by the allocator. |
| 51 | + Example usage: --workspace-pools-size-hint-bytes=flash:8""", |
| 52 | + action="append", |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + "--workspace-pools-clock-frequency-hz", |
| 56 | + nargs="?", |
| 57 | + help="""The clock frequency that the memory pool runs at in Hz. |
| 58 | + Example usage: --workspace-pools-clock-frequency-hz=flash:70000000""", |
| 59 | + action="append", |
| 60 | + ) |
| 61 | + parser.add_argument( |
| 62 | + "--workspace-pools-read-bandwidth-bytes-per-cycle", |
| 63 | + nargs="?", |
| 64 | + help="""The read bandwidth of the memory pool in bytes/cycle. |
| 65 | + Example usage: --workspace-pools-read-bandwidth-bytes-per-cycle=flash:4""", |
| 66 | + action="append", |
| 67 | + ) |
| 68 | + parser.add_argument( |
| 69 | + "--workspace-pools-write-bandwidth-bytes-per-cycle", |
| 70 | + nargs="?", |
| 71 | + help="""The write bandwidth of the memory pool in bytes/cycle. |
| 72 | + Example usage: --workspace-pools-write-bandwidth-bytes-per-cycle=flash:8""", |
| 73 | + action="append", |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "--workspace-pools-read-latency-cycles", |
| 77 | + nargs="?", |
| 78 | + help="""The read latency of the memory pool in cycles. |
| 79 | + Example usage: --workspace-pools-read-latency-cycles=flash:4""", |
| 80 | + action="append", |
| 81 | + ) |
| 82 | + parser.add_argument( |
| 83 | + "--workspace-pools-write-latency-cycles", |
| 84 | + nargs="?", |
| 85 | + help="""The write latency of the memory pool in cycles. |
| 86 | + Example usage: --workspace-pools-write-latency-cycles=flash:8""", |
| 87 | + action="append", |
| 88 | + ) |
| 89 | + parser.add_argument( |
| 90 | + "--workspace-pools-target-burst-bytes", |
| 91 | + help="""The burst length of the memory pool in bytes per target. |
| 92 | + Example usage: --workspace-pools-target-burst-bytes=flash:accel:1""", |
| 93 | + action="append", |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def _parse_target_burst(attr_str, pool_name): |
| 98 | + if pool_name not in attr_str: |
| 99 | + return {} |
| 100 | + |
| 101 | + return {target: int(attr_str[pool_name][target]) for target in attr_str[pool_name]} |
| 102 | + |
| 103 | + |
| 104 | +def _parse_target_string(attr_str, targets, pool_name): |
| 105 | + if attr_str is None: |
| 106 | + raise TVMCException(f'No target specified for Workspace Pool "{pool_name}"') |
| 107 | + |
| 108 | + target_name = [re.split(",", attr_str)] |
| 109 | + matched_targets = [ |
| 110 | + target |
| 111 | + for target in targets |
| 112 | + if any(target.kind.name in target_string_match for target_string_match in target_name[0]) |
| 113 | + ] |
| 114 | + if not matched_targets: |
| 115 | + raise TVMCException(f'Workspace Pool "{pool_name}" using undefined Target "{target_name}"') |
| 116 | + return matched_targets |
| 117 | + |
| 118 | + |
| 119 | +def _split_pools_to_pool_names(attr_str): |
| 120 | + return re.split(",", attr_str) if attr_str else [] |
| 121 | + |
| 122 | + |
| 123 | +def _parse_target_attributes_of_pool_name(attr_str, targets): |
| 124 | + if not targets or attr_str is None: |
| 125 | + return {} |
| 126 | + |
| 127 | + target_attributes = {} |
| 128 | + for pool_values in attr_str: |
| 129 | + pool_name, target_name, target_value = re.split(":", pool_values) |
| 130 | + if pool_name not in target_attributes: |
| 131 | + target_attributes[pool_name] = {} |
| 132 | + |
| 133 | + matched_targets = [target for target in targets if target_name == target.kind.name] |
| 134 | + if matched_targets: |
| 135 | + target_attributes[pool_name][matched_targets[0]] = target_value |
| 136 | + else: |
| 137 | + raise TVMCException( |
| 138 | + "The workspace pool target specification " |
| 139 | + "needs to contain a subset of the same TVM " |
| 140 | + "targets as when specifying targets to use." |
| 141 | + ) |
| 142 | + return target_attributes |
| 143 | + |
| 144 | + |
| 145 | +def _parse_attribute_of_pool_name(attr_str): |
| 146 | + return dict(pool.split(":", maxsplit=1) for pool in attr_str) if attr_str else {} |
| 147 | + |
| 148 | + |
| 149 | +def workspace_pools_recombobulate(parsed, targets, extra_target): |
| 150 | + """Reconstructs the Workspace Pools args and returns a WorkspaceMemoryPool object""" |
| 151 | + WORKSPACE_POOL_PARAMS = [ |
| 152 | + "workspace_pools_size_hint_bytes", |
| 153 | + "workspace_pools_targets", |
| 154 | + "workspace_pools_clock_frequency_hz", |
| 155 | + "workspace_pools_read_bandwidth_bytes_per_cycle", |
| 156 | + "workspace_pools_write_bandwidth_bytes_per_cycle", |
| 157 | + "workspace_pools_read_latency_cycles", |
| 158 | + "workspace_pools_write_latency_cycles", |
| 159 | + ] |
| 160 | + WORKSPACE_POOL_TARGET_PARAMS = [ |
| 161 | + "workspace_pools_target_burst_bytes", |
| 162 | + ] |
| 163 | + |
| 164 | + # Load extra targets from CLI |
| 165 | + additional_targets = [] |
| 166 | + |
| 167 | + for t in extra_target: |
| 168 | + additional_targets.append(Target(t["raw"], host=targets[0].host or targets[0])) |
| 169 | + |
| 170 | + target = targets + additional_targets |
| 171 | + if targets[0].host: |
| 172 | + target.append(targets[0].host) |
| 173 | + |
| 174 | + workspace_pools = _split_pools_to_pool_names(parsed.workspace_pools) |
| 175 | + if not workspace_pools: |
| 176 | + return None |
| 177 | + |
| 178 | + parse_attribute_to_pool_name = { |
| 179 | + workspace_pool_param: _parse_attribute_of_pool_name(getattr(parsed, workspace_pool_param)) |
| 180 | + for workspace_pool_param in WORKSPACE_POOL_PARAMS |
| 181 | + } |
| 182 | + parse_target_burst_bytes_to_pool = { |
| 183 | + workspace_pool_param: _parse_target_attributes_of_pool_name( |
| 184 | + getattr(parsed, workspace_pool_param), targets |
| 185 | + ) |
| 186 | + for workspace_pool_param in WORKSPACE_POOL_TARGET_PARAMS |
| 187 | + } |
| 188 | + |
| 189 | + return WorkspaceMemoryPools( |
| 190 | + [ |
| 191 | + WorkspacePoolInfo( |
| 192 | + pool_name, |
| 193 | + targets=_parse_target_string( |
| 194 | + parse_attribute_to_pool_name["workspace_pools_targets"].get(pool_name), |
| 195 | + target, |
| 196 | + pool_name, |
| 197 | + ), |
| 198 | + pool_info_properties=PoolInfoProperties( |
| 199 | + size_hint_bytes=int( |
| 200 | + parse_attribute_to_pool_name["workspace_pools_size_hint_bytes"].get( |
| 201 | + pool_name, -1 |
| 202 | + ) |
| 203 | + ), |
| 204 | + clock_frequency_hz=int( |
| 205 | + parse_attribute_to_pool_name["workspace_pools_clock_frequency_hz"].get( |
| 206 | + pool_name, -1 |
| 207 | + ) |
| 208 | + ), |
| 209 | + read_bandwidth_bytes_per_cycle=int( |
| 210 | + parse_attribute_to_pool_name[ |
| 211 | + "workspace_pools_read_bandwidth_bytes_per_cycle" |
| 212 | + ].get(pool_name, -1) |
| 213 | + ), |
| 214 | + write_bandwidth_bytes_per_cycle=int( |
| 215 | + parse_attribute_to_pool_name[ |
| 216 | + "workspace_pools_write_bandwidth_bytes_per_cycle" |
| 217 | + ].get(pool_name, -1) |
| 218 | + ), |
| 219 | + read_latency_cycles=int( |
| 220 | + parse_attribute_to_pool_name["workspace_pools_read_latency_cycles"].get( |
| 221 | + pool_name, 0 |
| 222 | + ) |
| 223 | + ), |
| 224 | + write_latency_cycles=int( |
| 225 | + parse_attribute_to_pool_name["workspace_pools_write_latency_cycles"].get( |
| 226 | + pool_name, 0 |
| 227 | + ) |
| 228 | + ), |
| 229 | + target_burst_bytes=_parse_target_burst( |
| 230 | + parse_target_burst_bytes_to_pool["workspace_pools_target_burst_bytes"], |
| 231 | + pool_name, |
| 232 | + ), |
| 233 | + ), |
| 234 | + ) |
| 235 | + for pool_name in workspace_pools |
| 236 | + ] |
| 237 | + ) |
0 commit comments