Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 2 | // Copyright (c) 2020-2023, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include "subgraph_traverser.h" |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 17 | #include "arith_util.h" |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 18 | #include "tosa_model_types.h" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 19 | |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 20 | #ifndef SUBGRAPH_ERROR_IF |
| 21 | #define SUBGRAPH_ERROR_IF(COND, fmt, ...) \ |
| 22 | if ((COND)) \ |
| 23 | { \ |
| 24 | if (this->getGraphStatus() != GraphStatus::TOSA_UNPREDICTABLE) \ |
| 25 | { \ |
| 26 | this->setGraphStatus(GraphStatus::TOSA_ERROR); \ |
| 27 | } \ |
| 28 | fprintf(g_func_debug.func_debug_file, COL_FATAL("SUBGRAPH_ERROR_IF() fails AT %s:%d %s(): (%s)\n"), __FILE__, \ |
| 29 | __LINE__, __func__, #COND); \ |
| 30 | fprintf(g_func_debug.func_debug_file, COL_FATAL(fmt) "\n", ##__VA_ARGS__); \ |
| 31 | func_print_backtrace(g_func_debug.func_debug_file); \ |
| 32 | return 1; \ |
| 33 | } |
| 34 | #endif |
| 35 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 36 | using namespace TosaReference; |
| 37 | using namespace Eigen; |
| 38 | using namespace tosa; |
| 39 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 40 | SubgraphTraverser::SubgraphTraverser(TosaSerializationBasicBlock* _block, |
| 41 | TosaSerializationHandler* _tsh, |
| 42 | SubgraphTraverser* _parent_sgt) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 43 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 44 | |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 45 | graph_status = GraphStatus::TOSA_VALID; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 46 | block = _block; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 47 | |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 48 | tsh = _tsh; |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 49 | parent_sgt = _parent_sgt; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 50 | tensors.clear(); |
| 51 | nodes.clear(); |
| 52 | nextNodeList.clear(); |
| 53 | } |
| 54 | |
| 55 | SubgraphTraverser::~SubgraphTraverser() |
| 56 | { |
| 57 | nextNodeList.clear(); |
| 58 | |
| 59 | for (GraphNode* n : nodes) |
| 60 | { |
| 61 | delete n; |
| 62 | } |
| 63 | nodes.clear(); |
| 64 | |
| 65 | for (TosaReference::Tensor* t : tensors) |
| 66 | { |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 67 | if (t->getIsVariable() && parent_sgt) |
| 68 | { |
| 69 | // variable tensors are owned by top level sgt |
| 70 | continue; |
| 71 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 72 | if (t->is_allocated()) |
| 73 | { |
| 74 | t->deallocate(); |
| 75 | } |
| 76 | delete t; |
| 77 | } |
| 78 | tensors.clear(); |
| 79 | } |
| 80 | |
| 81 | int SubgraphTraverser::getNumInputTensors() const |
| 82 | { |
| 83 | return inputTensors.size(); |
| 84 | } |
| 85 | |
| 86 | TosaReference::Tensor* SubgraphTraverser::getInputTensor(const unsigned int idx) const |
| 87 | { |
| 88 | return inputTensors[idx]; |
| 89 | } |
| 90 | |
| 91 | TosaReference::Tensor* SubgraphTraverser::getInputTensorByName(const std::string name) const |
| 92 | { |
| 93 | for (auto t : inputTensors) |
| 94 | { |
| 95 | if (t->getName() == name) |
| 96 | { |
| 97 | return t; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return nullptr; |
| 102 | } |
| 103 | |
| 104 | int SubgraphTraverser::getNumOutputTensors() const |
| 105 | { |
| 106 | return outputTensors.size(); |
| 107 | } |
| 108 | |
| 109 | TosaReference::Tensor* SubgraphTraverser::getOutputTensor(const unsigned int idx) const |
| 110 | { |
| 111 | return outputTensors[idx]; |
| 112 | } |
| 113 | |
| 114 | TosaReference::Tensor* SubgraphTraverser::getOutputTensorByName(const std::string name) const |
| 115 | { |
| 116 | for (auto t : outputTensors) |
| 117 | { |
| 118 | if (t->getName() == name) |
| 119 | { |
| 120 | return t; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 127 | int SubgraphTraverser::getNumVariableTensors() const |
| 128 | { |
| 129 | return variableTensors.size(); |
| 130 | } |
| 131 | |
| 132 | TosaReference::Tensor* SubgraphTraverser::getVariableTensor(const unsigned int idx) const |
| 133 | { |
| 134 | return variableTensors[idx]; |
| 135 | } |
| 136 | |
| 137 | // find variable tensor by name in top level sgt's @a variableTensors |
| 138 | TosaReference::Tensor* SubgraphTraverser::getVariableTensorByName(const std::string name) const |
| 139 | { |
| 140 | // variable tensors are owned by top level sgt |
| 141 | if (parent_sgt) |
| 142 | { |
| 143 | return parent_sgt->getVariableTensorByName(name); |
| 144 | } |
| 145 | |
| 146 | for (auto t : variableTensors) |
| 147 | { |
| 148 | if (t->getName() == name) |
| 149 | { |
| 150 | return t; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
| 157 | // add variable tensor to top level sgt's @a variableTensors |
| 158 | int SubgraphTraverser::registerVariableTensor(Tensor* tensor) |
| 159 | { |
| 160 | SUBGRAPH_ERROR_IF(!tensor->getIsVariable(), |
| 161 | "SubgraphTraverser::registerVariableTensor(): tensor %s is not a variable", |
| 162 | tensor->getName().c_str()); |
| 163 | // variable tensors are owned by top level sgt |
| 164 | if (parent_sgt) |
| 165 | { |
| 166 | return parent_sgt->registerVariableTensor(tensor); |
| 167 | } |
| 168 | variableTensors.push_back(tensor); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 172 | int SubgraphTraverser::initializeGraph() |
| 173 | { |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 174 | int idx = 0; |
Kevin Cheng | c72b59c | 2021-09-29 16:57:55 -0700 | [diff] [blame] | 175 | |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 176 | std::vector<TosaSerializationTensor*> ser_tensor_vec; |
| 177 | // Get all the serialized tensors from TosaSerializationHandler. |
Jiacheng Liang | eb52cc1 | 2023-05-17 16:49:44 +0100 | [diff] [blame] | 178 | if (tsh) |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 179 | { |
Jiacheng Liang | eb52cc1 | 2023-05-17 16:49:44 +0100 | [diff] [blame] | 180 | for (auto region : tsh->GetRegions()) |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 181 | { |
Jiacheng Liang | eb52cc1 | 2023-05-17 16:49:44 +0100 | [diff] [blame] | 182 | for (auto block : region->GetBlocks()) |
Tai Ly | 4e9a977 | 2023-03-16 22:24:05 +0000 | [diff] [blame] | 183 | { |
Jiacheng Liang | eb52cc1 | 2023-05-17 16:49:44 +0100 | [diff] [blame] | 184 | for (auto ser_tensor : block->GetTensors()) |
| 185 | { |
| 186 | ser_tensor_vec.push_back(ser_tensor); |
| 187 | } |
Tai Ly | 4e9a977 | 2023-03-16 22:24:05 +0000 | [diff] [blame] | 188 | } |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
Jiacheng Liang | eb52cc1 | 2023-05-17 16:49:44 +0100 | [diff] [blame] | 191 | else |
| 192 | { |
| 193 | for (auto ser_tensor : block->GetTensors()) |
| 194 | { |
| 195 | ser_tensor_vec.push_back(ser_tensor); |
| 196 | } |
| 197 | } |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 198 | |
| 199 | std::vector<GraphNode*> non_const_node_vec; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 200 | for (auto op : block->GetOperators()) |
| 201 | { |
| 202 | // translated TosaSerializationOperator to GraphNode |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 203 | TOSA_REF_TYPE input_dtype = TOSA_REF_TYPE_UNKNOWN; |
| 204 | TOSA_REF_TYPE output_dtype = TOSA_REF_TYPE_UNKNOWN; |
| 205 | TOSA_REF_TYPE weight_dtype = TOSA_REF_TYPE_UNKNOWN; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 206 | uint32_t input_rank = 0; |
| 207 | uint32_t output_rank = 0; |
| 208 | uint32_t weight_rank = 0; |
| 209 | int32_t input_index = -1; |
| 210 | int32_t weight_index = -1; |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 211 | |
| 212 | switch (op->GetOp()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 213 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 214 | case Op_CONV2D: |
Kevin Cheng | 1533b85 | 2021-09-01 12:51:58 -0700 | [diff] [blame] | 215 | case Op_CONV3D: |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 216 | case Op_DEPTHWISE_CONV2D: |
| 217 | case Op_TRANSPOSE_CONV2D: |
| 218 | case Op_FULLY_CONNECTED: |
| 219 | input_index = 0; |
| 220 | weight_index = 1; |
| 221 | break; |
| 222 | case Op_SELECT: |
| 223 | input_index = 1; |
| 224 | break; |
| 225 | default: |
| 226 | if (!op->GetInputTensorNames().empty()) |
| 227 | input_index = 0; |
| 228 | break; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 231 | if (input_index != -1) |
Kevin Cheng | df86269 | 2021-02-22 15:22:22 -0800 | [diff] [blame] | 232 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 233 | SUBGRAPH_ERROR_IF( |
| 234 | (size_t)input_index >= op->GetInputTensorNames().size(), |
| 235 | "SubgraphTraverser::initializeGraph(): Op=%s, input_index %d must be within [0, num_input - 1]", |
| 236 | EnumNamesOp()[op->GetOp()], input_index); |
Kevin Cheng | df86269 | 2021-02-22 15:22:22 -0800 | [diff] [blame] | 237 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 238 | std::string input_name = op->GetInputTensorNames()[input_index]; |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 239 | TosaSerializationTensor* input_tensor = nullptr; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 240 | for (auto ser_tensor : ser_tensor_vec) |
| 241 | { |
| 242 | if (ser_tensor->GetName() == input_name) |
| 243 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 244 | input_tensor = ser_tensor; |
| 245 | } |
| 246 | } |
| 247 | |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 248 | SUBGRAPH_ERROR_IF( |
| 249 | !input_tensor, |
| 250 | "SubgraphTraverser::initializeGraph(): fail to get input tensor %s from TosaSerializationHandler", |
| 251 | input_name.c_str()); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 252 | input_dtype = ConvertDType(input_tensor->GetDtype()); |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 253 | input_rank = input_tensor->GetShape().size(); |
Kevin Cheng | df86269 | 2021-02-22 15:22:22 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 256 | if (weight_index != -1) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 257 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 258 | SUBGRAPH_ERROR_IF( |
| 259 | (size_t)weight_index >= op->GetInputTensorNames().size(), |
| 260 | "SubgraphTraverser::initializeGraph(): Op=%s, weight_index %d must be within [0, num_input - 1]", |
| 261 | EnumNamesOp()[op->GetOp()], weight_index); |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 262 | std::string weight_name = op->GetInputTensorNames()[weight_index]; |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 263 | TosaSerializationTensor* weight_tensor = nullptr; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 264 | for (auto ser_tensor : ser_tensor_vec) |
| 265 | { |
| 266 | if (ser_tensor->GetName() == weight_name) |
| 267 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 268 | weight_tensor = ser_tensor; |
| 269 | } |
| 270 | } |
| 271 | |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 272 | SUBGRAPH_ERROR_IF( |
| 273 | !weight_tensor, |
| 274 | "SubgraphTraverser::initializeGraph(): fail to get weight tensor %s from TosaSerializationHandler", |
| 275 | weight_name.c_str()); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 276 | weight_dtype = ConvertDType(weight_tensor->GetDtype()); |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 277 | weight_rank = weight_tensor->GetShape().size(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Kevin Cheng | 478101b | 2021-10-04 10:43:14 -0700 | [diff] [blame] | 280 | SUBGRAPH_ERROR_IF(op->GetOutputTensorNames().size() == 0, |
| 281 | "SubgraphTraverser::initializeGraph(): Op=%s must have at least one output tensor.", |
| 282 | EnumNamesOp()[op->GetOp()]); |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 283 | std::string output_name = op->GetOutputTensorNames()[0]; |
| 284 | TosaSerializationTensor* output_tensor = block->GetTensorByName(output_name); |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 285 | SUBGRAPH_ERROR_IF( |
| 286 | !output_tensor, |
| 287 | "SubgraphTraverser::initializeGraph(): fail to get output tensor %s from TosaSerializationHandler", |
| 288 | output_name.c_str()); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 289 | output_dtype = ConvertDType(output_tensor->GetDtype()); |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 290 | output_rank = output_tensor->GetShape().size(); |
| 291 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 292 | DEBUG_INFO(GT, "Creating operator id_%03u, %8s, %lu input tensors, %lu output tensors", idx, |
| 293 | EnumNamesOp()[op->GetOp()], op->GetInputTensorNames().size(), op->GetOutputTensorNames().size()); |
| 294 | |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 295 | GraphNode* node = nullptr; |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 296 | if (this->parent_sgt) |
| 297 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 298 | node = OpFactory::newOp(this->parent_sgt, tsh, op->GetOp(), op->GetAttribute(), idx, input_dtype, |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 299 | input_rank, output_dtype, output_rank, weight_dtype, weight_rank); |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 300 | node->setInMainBlock(false); |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 301 | } |
| 302 | else |
| 303 | { |
| 304 | node = OpFactory::newOp(this, tsh, op->GetOp(), op->GetAttribute(), idx, input_dtype, input_rank, |
| 305 | output_dtype, output_rank, weight_dtype, weight_rank); |
| 306 | if (node) |
| 307 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 308 | node->setInMainBlock(true); |
| 309 | } |
| 310 | } |
| 311 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 312 | if (!node) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 313 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 314 | if (weight_index == -1) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 315 | { |
| 316 | fprintf(g_func_debug.func_debug_file, |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 317 | "SubgraphTraverser::initializeGraph(): OpFactory could not allocate op %8s input=(%s rank %d) " |
| 318 | "-> (%s rank %d)", |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 319 | EnumNamesOp()[op->GetOp()], EnumNameTOSAREFTYPE(input_dtype), input_rank, |
| 320 | EnumNameTOSAREFTYPE(output_dtype), output_rank); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 321 | } |
| 322 | else |
| 323 | { |
| 324 | fprintf(g_func_debug.func_debug_file, |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 325 | "SubgraphTraverser::initializeGraph(): OpFactory could not allocate op %8s input=(%s rank %d), " |
| 326 | "weight=(%s rank %d) -> (%s rank %d)", |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 327 | EnumNamesOp()[op->GetOp()], EnumNameTOSAREFTYPE(input_dtype), input_rank, |
| 328 | EnumNameTOSAREFTYPE(weight_dtype), weight_rank, EnumNameTOSAREFTYPE(output_dtype), output_rank); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 331 | for (auto& ts : op->GetInputTensorNames()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 332 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 333 | fprintf(g_func_debug.func_debug_file, "SubgraphTraverser::initializeGraph(): Input: %s\n", ts.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 336 | for (auto& ts : op->GetOutputTensorNames()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 337 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 338 | fprintf(g_func_debug.func_debug_file, "SubgraphTraverser::initializeGraph(): Output: %s\n", ts.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 339 | } |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 340 | SUBGRAPH_ERROR_IF(true, "SubgraphTraverser::initializeGraph(): Unsupported operation type or rank."); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Kevin Cheng | c72b59c | 2021-09-29 16:57:55 -0700 | [diff] [blame] | 343 | // Elementwise operator might set TOSA_ERROR when registering lambda function when creating the op. |
| 344 | // Check graph status after the op being constructed. |
| 345 | SUBGRAPH_ERROR_IF(getGraphStatus() == GraphStatus::TOSA_ERROR, |
| 346 | "SubgraphTraverser::initializeGraph(): Op %8s triggered ERROR_IF() when constructing the op.", |
| 347 | EnumNamesOp()[op->GetOp()]); |
| 348 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 349 | for (auto& name : op->GetInputTensorNames()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 350 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 351 | node->addInputName(name); |
Kevin Cheng | c72b59c | 2021-09-29 16:57:55 -0700 | [diff] [blame] | 352 | used_tensor_name_set.insert(name); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | for (auto name : op->GetOutputTensorNames()) |
| 356 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 357 | node->addOutputName(name); |
Kevin Cheng | c72b59c | 2021-09-29 16:57:55 -0700 | [diff] [blame] | 358 | used_tensor_name_set.insert(name); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 361 | addNode(node); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 362 | |
| 363 | // if node doesn't have any inputs (i.e. CONST) |
| 364 | // it should be ready for evaluation |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 365 | if (op->GetInputTensorNames().empty() && !node->getOnNextNodeList()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 366 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 367 | addToNextNodeList(node); |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 368 | } |
| 369 | else if (!node->getInMainBlock()) |
| 370 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 371 | non_const_node_vec.push_back(node); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 374 | // Bug fix: add the ready node in main block for evaluation |
| 375 | if (node->hasAllInputsReady() && !node->getOnNextNodeList() && !node->getEvaluated()) |
| 376 | { |
| 377 | addToNextNodeList(node); |
| 378 | } |
| 379 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 380 | idx++; |
| 381 | } |
| 382 | |
| 383 | for (auto ts : block->GetTensors()) |
| 384 | { |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 385 | addTensor(ts); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | DEBUG_INFO(GT, "Enumerating block %s graph inputs", block->GetName().c_str()); |
| 389 | for (auto& input_name : block->GetInputs()) |
| 390 | { |
| 391 | TosaReference::Tensor* tensor = findTensorByName(input_name); |
| 392 | DEBUG_INFO(GT, "input tensor name=%s", input_name.c_str()); |
| 393 | if (tensor) |
| 394 | { |
| 395 | tensor->setIsSubgraphInput(); |
| 396 | inputTensors.push_back(tensor); |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | SUBGRAPH_ERROR_IF(true, "SubgraphTraverser::initializeGraph(): Failed to find input tensor by name %s", |
| 401 | input_name.c_str()); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | DEBUG_INFO(GT, "Enumerating block %s graph outputs", block->GetName().c_str()); |
| 406 | for (auto& output_name : block->GetOutputs()) |
| 407 | { |
| 408 | TosaReference::Tensor* tensor = findTensorByName(output_name); |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 409 | DEBUG_INFO(GT, "output tensor name=%s", output_name.c_str()); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 410 | if (tensor) |
| 411 | { |
| 412 | tensor->setIsSubgraphOutput(); |
| 413 | outputTensors.push_back(tensor); |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | SUBGRAPH_ERROR_IF(true, "SubgraphTraverser::initializeGraph(): Failed to find output tensor by name %s", |
| 418 | output_name.c_str()); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (DEBUG_ENABLED(DEBUG_VERB_HIGH, GT)) |
| 423 | { |
| 424 | dumpNextNodeList(g_func_debug.func_debug_file); |
| 425 | } |
| 426 | |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 427 | // If the node is not in mainblock and not const |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 428 | for (auto node : non_const_node_vec) |
| 429 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 430 | bool all_inputs_from_parent = true; |
| 431 | for (std::string& name : node->getInputNames()) |
| 432 | { |
| 433 | TosaReference::Tensor* t = findTensorByName(name); |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 434 | if (!t->getIsParentGraphOutput()) |
| 435 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 436 | all_inputs_from_parent = false; |
| 437 | } |
| 438 | } |
| 439 | // In the children block, when a node has all its inputs from parent |
| 440 | // block, we have to manually add this node to the evaluation list |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 441 | if (all_inputs_from_parent && !node->getOnNextNodeList()) |
| 442 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 443 | addToNextNodeList(node); |
| 444 | } |
| 445 | } |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 446 | return 0; |
| 447 | } |
| 448 | |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 449 | int SubgraphTraverser::allocateInputTensors() |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 450 | { |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 451 | auto input_tensor_names_vec = block->GetInputs(); |
| 452 | |
| 453 | for (auto input_tensor_name : input_tensor_names_vec) |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 454 | { |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 455 | this->allocateTensor(input_tensor_name); |
| 456 | } |
| 457 | |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 458 | // allocate variable tensors if not already allocated |
| 459 | for (auto ts : block->GetTensors()) |
| 460 | { |
| 461 | if (ts->GetVariable()) |
| 462 | { |
| 463 | TosaReference::Tensor* tensor = findTensorByName(ts->GetName()); |
| 464 | SUBGRAPH_ERROR_IF(!tensor, "SubgraphTraverser::allocateInputTensors(): can't find tensor %s.", |
| 465 | ts->GetName().c_str()); |
| 466 | if (!tensor->is_allocated()) |
| 467 | { |
| 468 | DEBUG_INFO(GT, "Is a VariableTensor %s", ts->GetName().c_str()); |
| 469 | this->allocateTensor(ts->GetName()); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | int SubgraphTraverser::allocateTensor(std::string name) |
| 478 | { |
| 479 | auto ts = block->GetTensorByName(name); |
| 480 | |
| 481 | // Bail out if tensor is used and any of its dimension is invalid. |
| 482 | auto got = used_tensor_name_set.find(ts->GetName()); |
| 483 | if (got != used_tensor_name_set.end()) |
| 484 | { |
| 485 | uint32_t elements = 1; |
| 486 | for (auto& dim : ts->GetShape()) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 487 | { |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 488 | if (dim <= 0) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 489 | { |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 490 | DEBUG_INFO(GT, "Failed to allocate tensor %s with invalid dimension of %d", ts->GetName().c_str(), dim); |
| 491 | this->setGraphStatus(GraphStatus::TOSA_UNPREDICTABLE); |
| 492 | return 1; |
| 493 | } |
| 494 | if (dim > static_cast<int32_t>(TOSA_MAX_TENSOR_SIZE / elements)) |
| 495 | { |
| 496 | // Size greather than maximum defined in spec |
| 497 | DEBUG_INFO(GT, "Tensor %s size is greater than allowed maximum", ts->GetName().c_str()); |
| 498 | this->setGraphStatus(GraphStatus::TOSA_UNPREDICTABLE); |
| 499 | return 1; |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 502 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 503 | |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 504 | TosaReference::Tensor* tensor = findTensorByName(ts->GetName()); |
| 505 | SUBGRAPH_ERROR_IF(!tensor, "SubgraphTraverser::allocateTensor(): can't find tensor %s.", ts->GetName().c_str()); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 506 | |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 507 | DEBUG_INFO(GT, "Allocating tensor %s", tensor->getName().c_str()); |
| 508 | if (tensor->allocate()) |
| 509 | { |
| 510 | FATAL_ERROR("Failed to allocate tensor %s", tensor->getName().c_str()); |
| 511 | } |
| 512 | |
| 513 | if (!ts->GetData().empty()) |
| 514 | { |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 515 | if (ts->GetVariable() && g_func_config.initialize_variable_tensor_from_numpy) |
| 516 | return 0; |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 517 | DEBUG_INFO(GT, "Setting data for tensor %s", tensor->getName().c_str()); |
| 518 | auto serialization_dtype = ts->GetDtype(); |
| 519 | switch (serialization_dtype) |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 520 | { |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 521 | case DType_INT4: { |
| 522 | std::vector<int8_t> i4_data; |
| 523 | TosaSerializationHandler::ConvertU8toI4(ts->GetData(), tensor->getElementCount(), i4_data); |
| 524 | std::vector<int32_t> i32_data(i4_data.begin(), i4_data.end()); |
| 525 | tensor->setTensorValueInt32(i32_data.size(), i32_data.data()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 526 | } |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 527 | break; |
| 528 | case DType_INT8: { |
| 529 | std::vector<int8_t> i8_data; |
| 530 | TosaSerializationHandler::ConvertU8toI8(ts->GetData(), tensor->getElementCount(), i8_data); |
| 531 | std::vector<int32_t> i32_data(i8_data.begin(), i8_data.end()); |
| 532 | tensor->setTensorValueInt32(i32_data.size(), i32_data.data()); |
| 533 | } |
| 534 | break; |
| 535 | case DType_INT16: { |
| 536 | std::vector<int16_t> i16_data; |
| 537 | TosaSerializationHandler::ConvertU8toI16(ts->GetData(), tensor->getElementCount(), i16_data); |
| 538 | std::vector<int32_t> i32_data(i16_data.begin(), i16_data.end()); |
| 539 | tensor->setTensorValueInt32(i32_data.size(), i32_data.data()); |
| 540 | } |
| 541 | break; |
| 542 | case DType_INT32: { |
| 543 | std::vector<int32_t> i32_data; |
| 544 | TosaSerializationHandler::ConvertU8toI32(ts->GetData(), tensor->getElementCount(), i32_data); |
| 545 | tensor->setTensorValueInt32(i32_data.size(), i32_data.data()); |
| 546 | } |
| 547 | break; |
Won Jeon | a21b2e8 | 2023-08-10 10:33:01 +0000 | [diff] [blame] | 548 | case DType_INT48: |
| 549 | case DType_SHAPE: { |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 550 | std::vector<int64_t> i64_data; |
| 551 | TosaSerializationHandler::ConvertU8toI48(ts->GetData(), tensor->getElementCount(), i64_data); |
| 552 | tensor->setTensorValueInt64(i64_data.size(), i64_data.data()); |
| 553 | } |
| 554 | break; |
| 555 | case DType_FP16: { |
| 556 | // Interpret f16 data as float |
| 557 | std::vector<float> f16_data; |
| 558 | TosaSerializationHandler::ConvertU8toF16(ts->GetData(), tensor->getElementCount(), f16_data); |
| 559 | if (tensor->getDtype() == TOSA_REF_TYPE_FP64) |
| 560 | { |
| 561 | std::vector<double> f64_data(f16_data.begin(), f16_data.end()); |
| 562 | tensor->setTensorValueDouble(f64_data.size(), f64_data.data()); |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | tensor->setTensorValueFloat(f16_data.size(), f16_data.data()); |
| 567 | } |
| 568 | } |
| 569 | break; |
| 570 | case DType_BF16: { |
| 571 | std::vector<float> fp32_data; |
| 572 | TosaSerializationHandler::ConvertU8toF32(ts->GetData(), tensor->getElementCount(), fp32_data); |
| 573 | // Ensure valid bfloat16 stored in each float |
| 574 | for (auto f : fp32_data) |
| 575 | ASSERT_MSG(checkValidBFloat(f), "Float value %f not valid bfloat16", f); |
| 576 | if (tensor->getDtype() == TOSA_REF_TYPE_FP64) |
| 577 | { |
| 578 | std::vector<double> f64_data(fp32_data.begin(), fp32_data.end()); |
| 579 | tensor->setTensorValueDouble(f64_data.size(), f64_data.data()); |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | tensor->setTensorValueFloat(fp32_data.size(), fp32_data.data()); |
| 584 | } |
| 585 | } |
| 586 | break; |
| 587 | case DType_FP32: { |
| 588 | std::vector<float> fp32_data; |
| 589 | TosaSerializationHandler::ConvertU8toF32(ts->GetData(), tensor->getElementCount(), fp32_data); |
| 590 | if (tensor->getDtype() == TOSA_REF_TYPE_FP64) |
| 591 | { |
| 592 | std::vector<double> f64_data(fp32_data.begin(), fp32_data.end()); |
| 593 | tensor->setTensorValueDouble(f64_data.size(), f64_data.data()); |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | tensor->setTensorValueFloat(fp32_data.size(), fp32_data.data()); |
| 598 | } |
| 599 | } |
| 600 | break; |
| 601 | case DType_BOOL: { |
| 602 | std::vector<bool> bool_data; |
| 603 | TosaSerializationHandler::ConvertU8toBool(ts->GetData(), tensor->getElementCount(), bool_data); |
| 604 | |
| 605 | // std::vector<bool>::data() will return bit mask instead of array of bool array. |
| 606 | // Need to translate manually. |
| 607 | bool* bool_array = (bool*)calloc(bool_data.size(), sizeof(bool)); |
| 608 | for (size_t i = 0; i < bool_data.size(); i++) |
| 609 | { |
| 610 | bool_array[i] = bool_data[i]; |
| 611 | } |
| 612 | tensor->setTensorValueBool(bool_data.size(), bool_array); |
| 613 | } |
| 614 | break; |
| 615 | default: |
| 616 | SUBGRAPH_ERROR_IF(true, "SubgraphTraverser::initializeGraph(): Unsupported tensor type %s.", |
| 617 | EnumNameDType(ts->GetDtype())); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 618 | } |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 619 | tensor->setIsValid(); |
| 620 | // Push ready consumers to the next node list |
| 621 | for (auto gn : tensor->getConsumers()) |
| 622 | { |
| 623 | if (gn->hasAllInputsReady() && !gn->getOnNextNodeList() && !gn->getEvaluated()) |
| 624 | { |
| 625 | addToNextNodeList(gn); |
| 626 | } |
| 627 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 628 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | int SubgraphTraverser::isFullyEvaluated() const |
| 633 | { |
| 634 | return nextNodeList.empty(); |
| 635 | } |
| 636 | |
| 637 | GraphNode* SubgraphTraverser::getNextNode() |
| 638 | { |
| 639 | GraphNode* nextNode = nextNodeList.front(); |
| 640 | ASSERT_MSG(nextNode, "SubgraphTraverser::getNextNode(): called with empty next node list"); |
| 641 | ASSERT_MSG(nextNode->getOnNextNodeList(), |
| 642 | "SubgraphTraverser::getNextNode(): internal state error: node is not listed as being on next node list"); |
| 643 | |
| 644 | nextNodeList.pop_front(); |
| 645 | |
| 646 | nextNode->clearOnNextNodeList(); |
| 647 | return nextNode; |
| 648 | } |
| 649 | |
| 650 | int SubgraphTraverser::addToNextNodeList(GraphNode* nextNode) |
| 651 | { |
| 652 | ASSERT_MSG(nextNode, "SubgraphTraverser::addToNextNodeList(): called with no node"); |
| 653 | ASSERT_MSG(!nextNode->getOnNextNodeList(), |
| 654 | "SubgraphTraverser::addToNextNodeList(): internal state error: node is already on next node list"); |
| 655 | |
| 656 | nextNode->setOnNextNodeList(); |
| 657 | nextNodeList.push_back(nextNode); |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | int SubgraphTraverser::evaluateNextNode() |
| 663 | { |
| 664 | if (isFullyEvaluated()) |
| 665 | return 0; |
| 666 | |
| 667 | GraphNode* currNode = getNextNode(); |
| 668 | |
| 669 | DEBUG_INFO(GT, "Evaluating node_%03lu, %8s, output tensor=%s", currNode->getID(), EnumNamesOp()[currNode->getOp()], |
| 670 | currNode->getOutputNames()[0].c_str()); |
| 671 | |
| 672 | // Sanity check for never-ending loops |
| 673 | if (currNode->getEvalCount() >= MAX_EVAL_COUNT && (currNode->getEvalCount() % MAX_EVAL_COUNT) == 0) |
| 674 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 675 | WARNING("SubgraphTraverser::evaluateNextNode(): Node %lu has been evaluated %d times. Loop suspected.", |
| 676 | currNode->getID(), currNode->getEvalCount()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 679 | for (auto tensor : currNode->getOutputs()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 680 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 681 | if (!tensor->is_allocated()) |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 682 | { |
| 683 | if (this->allocateTensor(tensor->getName())) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 684 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 685 | FATAL_ERROR("SubgraphTraverser::evaluateNextNode(): Failed to allocate Eigen tensor %s", |
| 686 | tensor->getName().c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 687 | } |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 688 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | if (currNode->eval()) |
| 692 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 693 | WARNING("SubgraphTraverser::evaluateNextNode(): Failed to evaluate node: %lu", currNode->getID()); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 694 | return 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 697 | currNode->setEvaluated(); |
| 698 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 699 | // free input tensor if all of its consumers have all of their outputs ready and it's not block's output |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 700 | for (auto tensor : currNode->getInputs()) |
| 701 | { |
| 702 | bool in_use = false; |
| 703 | |
| 704 | auto tensor_check = findTensorByName(tensor->getName()); |
| 705 | if (tensor_check->getIsParentGraphOutput()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 706 | { |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 707 | // if it's parent's block output tensor, we can't free it |
| 708 | continue; |
| 709 | } |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 710 | |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 711 | if (tensor->getIsVariable()) |
| 712 | { |
| 713 | // if tensor is a Variable, we cannot free it |
| 714 | continue; |
| 715 | } |
| 716 | |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 717 | for (auto node : tensor->getConsumers()) |
| 718 | { |
| 719 | // If the node is inside a loop, the input tensor is still needed |
| 720 | if (!node->hasAllOutputsReady()) |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 721 | { |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 722 | in_use = true; |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 723 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 724 | } |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 725 | |
| 726 | for (auto name : block->GetOutputs()) |
| 727 | { |
| 728 | if (name == tensor->getName()) |
| 729 | { |
| 730 | in_use = true; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | if (!in_use) |
| 735 | { |
| 736 | tensor->deallocate(); |
| 737 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 738 | } |
Jerry Ge | e5cabbf | 2023-07-17 21:33:17 +0000 | [diff] [blame] | 739 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 740 | // Search the output tensors of this node to see if |
| 741 | // there are now new ready nodes available from completing this node |
| 742 | for (TosaReference::Tensor* tensor : currNode->getOutputs()) |
| 743 | { |
| 744 | for (GraphNode* node : tensor->getConsumers()) |
| 745 | { |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 746 | if (!node->getOnNextNodeList() && node->hasAllInputsReady() && !node->getEvaluated()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 747 | { |
| 748 | addToNextNodeList(node); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (DEBUG_ENABLED(DEBUG_VERB_HIGH, GT)) |
| 754 | { |
| 755 | dumpNextNodeList(g_func_debug.func_debug_file); |
| 756 | } |
| 757 | |
| 758 | if (g_func_config.dump_intermediates) |
| 759 | { |
| 760 | currNode->dumpNode(g_func_debug.func_debug_file); |
| 761 | for (auto outs : currNode->getOutputs()) |
| 762 | { |
| 763 | outs->dumpTensorParams(g_func_debug.func_debug_file); |
| 764 | outs->dumpTensor(g_func_debug.func_debug_file); |
| 765 | fprintf(g_func_debug.func_debug_file, "\n"); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | int SubgraphTraverser::dumpNextNodeList(FILE* out) const |
| 773 | { |
| 774 | |
| 775 | // Dump next node list |
| 776 | fprintf(out, "Next node list\n"); |
| 777 | |
| 778 | if (nextNodeList.empty()) |
| 779 | { |
| 780 | fprintf(out, "<empty>\n"); |
| 781 | } |
| 782 | |
| 783 | for (auto gn : nextNodeList) |
| 784 | { |
| 785 | gn->dumpNode(out); |
| 786 | } |
| 787 | |
| 788 | fprintf(out, "Done.\n"); |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | int SubgraphTraverser::clearAllNodeMarkings() |
| 793 | { |
| 794 | for (GraphNode* currNode : nodes) |
| 795 | { |
| 796 | currNode->clearNodeMarked(); |
| 797 | } |
| 798 | |
| 799 | return false; |
| 800 | } |
| 801 | |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 802 | int SubgraphTraverser::addTensor(const TosaSerializationTensor* ts) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 803 | { |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 804 | TosaReference::Tensor* tensor = nullptr; |
| 805 | |
| 806 | // variable tensors are shared: make new tensor only if not found |
| 807 | if (ts->GetVariable()) |
| 808 | { |
| 809 | tensor = getVariableTensorByName(ts->GetName()); |
| 810 | } |
| 811 | |
| 812 | if (!tensor) |
| 813 | { |
| 814 | DEBUG_INFO(GT, "Creating tensor %s", ts->GetName().c_str()); |
| 815 | tensor = TensorFactory::newTensor(ts->GetName(), ts->GetDtype(), ts->GetShape(), ts->GetShape().size()); |
| 816 | |
| 817 | SUBGRAPH_ERROR_IF(!tensor, "SubgraphTraverser::initializeGraph(): Unsupported tensor name=%s, type=%s, rank=%d", |
| 818 | ts->GetName().c_str(), EnumNameDType(ts->GetDtype()), (int)ts->GetShape().size()); |
| 819 | |
| 820 | if (ts->GetVariable()) |
| 821 | { |
| 822 | tensor->setIsVariable(); |
| 823 | registerVariableTensor(tensor); |
| 824 | } |
| 825 | } |
| 826 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 827 | // Enforce no duplicate tensors/tensor names |
| 828 | // O(N), but the number of tensors is small |
| 829 | for (TosaReference::Tensor* currTensor : tensors) |
| 830 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 831 | if (tensor == currTensor || currTensor->getName() == tensor->getName()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 832 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 833 | FATAL_ERROR("SubgraphTraverser::addTensor(): Duplicate tensor or tensor name being added to graph: %s\n", |
| 834 | tensor->getName().c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 835 | return 1; |
| 836 | } |
| 837 | } |
| 838 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 839 | tensors.push_back(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 840 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 841 | if (tensor->getIsSubgraphInput()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 842 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 843 | inputTensors.push_back(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 846 | if (tensor->getIsSubgraphOutput()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 847 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 848 | outputTensors.push_back(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | int SubgraphTraverser::addNode(GraphNode* newNode) |
| 854 | { |
| 855 | // Enforce no duplicate nodes |
| 856 | for (GraphNode* currNode : nodes) |
| 857 | { |
| 858 | if (currNode == newNode) |
| 859 | { |
Tai Ly | 4762564 | 2023-09-07 20:49:09 +0000 | [diff] [blame] | 860 | FATAL_ERROR("SubgraphTraverser::addNode(): duplicate node being added to graph"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 861 | return 1; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | nodes.push_back(newNode); |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | TosaReference::Tensor* SubgraphTraverser::findTensorByName(const std::string& name) const |
| 871 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 872 | TosaReference::Tensor* res_tensor = nullptr; |
| 873 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 874 | for (TosaReference::Tensor* currTensor : tensors) |
| 875 | { |
| 876 | if (currTensor->getName() == name) |
| 877 | { |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 878 | res_tensor = currTensor; |
| 879 | return res_tensor; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 883 | if (parent_sgt) |
| 884 | { |
| 885 | for (TosaReference::Tensor* currTensor : parent_sgt->tensors) |
| 886 | { |
| 887 | if (currTensor->getName() == name) |
| 888 | { |
| 889 | res_tensor = currTensor; |
| 890 | res_tensor->setIsParentGraphOutput(); |
| 891 | } |
| 892 | } |
| 893 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 894 | |
Jerry Ge | 9e94af8 | 2022-10-27 09:57:00 -0700 | [diff] [blame] | 895 | if (!res_tensor) |
| 896 | { |
| 897 | WARNING("SubgraphTraverser::findTensorByName(): Unable to find tensor with name: %s\n", name.c_str()); |
| 898 | return nullptr; |
| 899 | } |
| 900 | return res_tensor; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | int SubgraphTraverser::linkTensorsAndNodes() |
| 904 | { |
| 905 | // Nodes have a list of input/output tensor names |
| 906 | // For each node, read this list, link up the tensors with their inputs/outputs |
| 907 | for (GraphNode* currNode : nodes) |
| 908 | { |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 909 | // Link inputs/consuming nodes |
| 910 | for (std::string& name : currNode->getInputNames()) |
| 911 | { |
| 912 | TosaReference::Tensor* t = findTensorByName(name); |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 913 | SUBGRAPH_ERROR_IF(!t, "SubgraphTraverser::linkTensorsAndNodes(): Cannot find tensor %s in node %lu\n", |
| 914 | name.c_str(), currNode->getID()); |
| 915 | SUBGRAPH_ERROR_IF(currNode->addInputTensor(t), |
| 916 | "SubgraphTraverser::linkTensorsAndNodes(): cannot link tensor %s to node %lu\n", |
| 917 | name.c_str(), currNode->getID()); |
| 918 | SUBGRAPH_ERROR_IF(t->addConsumer(currNode), |
| 919 | "SubgraphTraverser::linkTensorsAndNodes(): cannot link consumer node %lu to tensor %s\n", |
| 920 | currNode->getID(), name.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | // Link outputs/producing nodes |
| 924 | for (std::string& name : currNode->getOutputNames()) |
| 925 | { |
| 926 | TosaReference::Tensor* t = findTensorByName(name); |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 927 | SUBGRAPH_ERROR_IF(!t, "SubgraphTraverser::linkTensorsAndNodes(): Cannot find tensor %s in node %lu\n", |
| 928 | name.c_str(), currNode->getID()); |
| 929 | SUBGRAPH_ERROR_IF(currNode->addOutputTensor(t), |
| 930 | "SubgraphTraverser::linkTensorsAndNodes(): cannot link tensor %s to node %lu\n", |
| 931 | name.c_str(), currNode->getID()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 932 | |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 933 | SUBGRAPH_ERROR_IF( |
| 934 | t->setProducer(currNode), |
| 935 | "SubgraphTraverser::linkTensorsAndNodes(): cannot link producer node %lu to tensor tensor %s\n", |
| 936 | currNode->getID(), name.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | int SubgraphTraverser::validateGraph() |
| 944 | { |
| 945 | // Need to make sure that: |
| 946 | // - each tensor is actually used |
| 947 | // - input and output tesnsors truly are just input and just output |
| 948 | // Graph building already determined that each node has found its input/output tensors |
| 949 | |
| 950 | for (TosaReference::Tensor* currTensor : tensors) |
| 951 | { |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 952 | // It's okay for block input tensor not being consumed by operators. |
| 953 | // This is common in control flow op execution. |
| 954 | if (!currTensor->getIsSubgraphInput()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 955 | { |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 956 | if (!currTensor->getProducer() && currTensor->getConsumers().empty()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 957 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 958 | WARNING("SubgraphTraverser::validateGraph(): TosaReference::Tensor %s has no producers or consumers\n", |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 959 | currTensor->getName().c_str()); |
| 960 | return 1; |
| 961 | } |
| 962 | } |
| 963 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 964 | if (g_func_config.tosa_profile == 0) |
| 965 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 966 | TOSA_REF_TYPE dtype = currTensor->getDtype(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 967 | |
| 968 | // Float-point disallowed |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 969 | if (dtype == TOSA_REF_TYPE_FP32 || dtype == TOSA_REF_TYPE_FP16) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 970 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 971 | WARNING("SubgraphTraverser::validateGraph(): TOSA Base Inference profile selected: All floating point " |
| 972 | "disabled, but %s tensor %s found\n", |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 973 | EnumNameTOSAREFTYPE(dtype), currTensor->getName().c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 974 | return 1; |
| 975 | } |
| 976 | } |
| 977 | else if (g_func_config.tosa_profile == 1 || g_func_config.tosa_profile == 2) |
| 978 | { |
| 979 | // Do nothing. All FP types allowed |
| 980 | // Currently no implementation difference between Main Inference and Main Training modes |
| 981 | } |
| 982 | else |
| 983 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 984 | FATAL_ERROR("SubgraphTraverser::validateGraph(): TOSA profile not recognized: %d", |
| 985 | g_func_config.tosa_profile); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | for (GraphNode* currNode : nodes) |
| 990 | { |
Kevin Cheng | 903763c | 2021-09-28 16:14:52 -0700 | [diff] [blame] | 991 | SUBGRAPH_ERROR_IF(currNode->checkTensorAttributes(), |
| 992 | "SubgraphTraverser::validateGraph(): TosaReference::Tensor attribute check failed"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | if (outputTensors.size() <= 0) |
| 996 | { |
| 997 | DEBUG_MED(GT, "Graph output tensor empty"); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | int SubgraphTraverser::dumpGraph(FILE* out) const |
| 1005 | { |
| 1006 | int i = 0; |
| 1007 | |
| 1008 | fprintf(out, "Full graph dump:\n"); |
| 1009 | for (GraphNode* currNode : nodes) |
| 1010 | { |
| 1011 | fprintf(out, "Node [%d]: ", i++); |
| 1012 | currNode->dumpNode(out); |
| 1013 | } |
| 1014 | |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | int SubgraphTraverser::evaluateAll() |
| 1019 | { |
| 1020 | // evaluation loop |
| 1021 | while (!isFullyEvaluated()) |
| 1022 | { |
| 1023 | if (evaluateNextNode()) |
| 1024 | { |
| 1025 | return 1; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |