Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
| 2 | // Copyright (c) 2020, ARM Limited. |
| 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" |
| 17 | |
| 18 | using namespace TosaReference; |
| 19 | using namespace Eigen; |
| 20 | using namespace tosa; |
| 21 | |
| 22 | SubgraphTraverser::SubgraphTraverser(TosaSerializationBasicBlock* _block, TosaSerializationHandler* _tsh) |
| 23 | { |
| 24 | block = _block; |
| 25 | tsh = _tsh; |
| 26 | |
| 27 | tensors.clear(); |
| 28 | nodes.clear(); |
| 29 | nextNodeList.clear(); |
| 30 | } |
| 31 | |
| 32 | SubgraphTraverser::~SubgraphTraverser() |
| 33 | { |
| 34 | nextNodeList.clear(); |
| 35 | |
| 36 | for (GraphNode* n : nodes) |
| 37 | { |
| 38 | delete n; |
| 39 | } |
| 40 | nodes.clear(); |
| 41 | |
| 42 | for (TosaReference::Tensor* t : tensors) |
| 43 | { |
| 44 | if (t->is_allocated()) |
| 45 | { |
| 46 | t->deallocate(); |
| 47 | } |
| 48 | delete t; |
| 49 | } |
| 50 | tensors.clear(); |
| 51 | } |
| 52 | |
| 53 | int SubgraphTraverser::getNumInputTensors() const |
| 54 | { |
| 55 | return inputTensors.size(); |
| 56 | } |
| 57 | |
| 58 | TosaReference::Tensor* SubgraphTraverser::getInputTensor(const unsigned int idx) const |
| 59 | { |
| 60 | return inputTensors[idx]; |
| 61 | } |
| 62 | |
| 63 | TosaReference::Tensor* SubgraphTraverser::getInputTensorByName(const std::string name) const |
| 64 | { |
| 65 | for (auto t : inputTensors) |
| 66 | { |
| 67 | if (t->getName() == name) |
| 68 | { |
| 69 | return t; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | int SubgraphTraverser::getNumOutputTensors() const |
| 77 | { |
| 78 | return outputTensors.size(); |
| 79 | } |
| 80 | |
| 81 | TosaReference::Tensor* SubgraphTraverser::getOutputTensor(const unsigned int idx) const |
| 82 | { |
| 83 | return outputTensors[idx]; |
| 84 | } |
| 85 | |
| 86 | TosaReference::Tensor* SubgraphTraverser::getOutputTensorByName(const std::string name) const |
| 87 | { |
| 88 | for (auto t : outputTensors) |
| 89 | { |
| 90 | if (t->getName() == name) |
| 91 | { |
| 92 | return t; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return nullptr; |
| 97 | } |
| 98 | |
| 99 | int SubgraphTraverser::initializeGraph() |
| 100 | { |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 101 | int idx = 0; |
| 102 | for (auto op : block->GetOperators()) |
| 103 | { |
| 104 | // translated TosaSerializationOperator to GraphNode |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 105 | DType input_dtype = DType_UNKNOWN; |
| 106 | DType output_dtype = DType_UNKNOWN; |
| 107 | DType weight_dtype = DType_UNKNOWN; |
| 108 | uint32_t input_rank = 0; |
| 109 | uint32_t output_rank = 0; |
| 110 | uint32_t weight_rank = 0; |
| 111 | int32_t input_index = -1; |
| 112 | int32_t weight_index = -1; |
| 113 | |
| 114 | switch (op->GetOp()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 115 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 116 | case Op_CONV2D: |
| 117 | case Op_DEPTHWISE_CONV2D: |
| 118 | case Op_TRANSPOSE_CONV2D: |
| 119 | case Op_FULLY_CONNECTED: |
| 120 | input_index = 0; |
| 121 | weight_index = 1; |
| 122 | break; |
| 123 | case Op_SELECT: |
| 124 | input_index = 1; |
| 125 | break; |
| 126 | default: |
| 127 | if (!op->GetInputTensorNames().empty()) |
| 128 | input_index = 0; |
| 129 | break; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 132 | if (input_index != -1) |
Kevin Cheng | df86269 | 2021-02-22 15:22:22 -0800 | [diff] [blame] | 133 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 134 | ASSERT_MSG((size_t)input_index < op->GetInputTensorNames().size(), |
| 135 | "Op=%s, input_index %d must be within [0, num_input - 1]", EnumNamesOp()[op->GetOp()], |
| 136 | input_index); |
Kevin Cheng | df86269 | 2021-02-22 15:22:22 -0800 | [diff] [blame] | 137 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 138 | std::string input_name = op->GetInputTensorNames()[input_index]; |
| 139 | TosaSerializationTensor* input_tensor = block->GetTensorByName(input_name); |
| 140 | ASSERT_MSG(input_tensor, "SubgraphTraverser: fail to get input tensor %s from TosaSerializationHandler", |
| 141 | input_name.c_str()); |
| 142 | input_dtype = input_tensor->GetDtype(); |
| 143 | input_rank = input_tensor->GetShape().size(); |
Kevin Cheng | df86269 | 2021-02-22 15:22:22 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 146 | if (weight_index != -1) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 147 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 148 | ASSERT_MSG((size_t)weight_index < op->GetInputTensorNames().size(), |
| 149 | "Op=%s, weight_index %d must be within [0, num_input - 1]", EnumNamesOp()[op->GetOp()], |
| 150 | weight_index); |
| 151 | std::string weight_name = op->GetInputTensorNames()[weight_index]; |
| 152 | TosaSerializationTensor* weight_tensor = block->GetTensorByName(weight_name); |
| 153 | ASSERT_MSG(weight_tensor, "SubgraphTraverser: fail to get weight tensor %s from TosaSerializationHandler", |
| 154 | weight_name.c_str()); |
| 155 | weight_dtype = weight_tensor->GetDtype(); |
| 156 | weight_rank = weight_tensor->GetShape().size(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 159 | std::string output_name = op->GetOutputTensorNames()[0]; |
| 160 | TosaSerializationTensor* output_tensor = block->GetTensorByName(output_name); |
| 161 | ASSERT_MSG(output_tensor, "SubgraphTraverser: fail to get output tensor %s from TosaSerializationHandler", |
| 162 | output_name.c_str()); |
| 163 | output_dtype = output_tensor->GetDtype(); |
| 164 | output_rank = output_tensor->GetShape().size(); |
| 165 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 166 | DEBUG_INFO(GT, "Creating operator id_%03u, %8s, %lu input tensors, %lu output tensors", idx, |
| 167 | EnumNamesOp()[op->GetOp()], op->GetInputTensorNames().size(), op->GetOutputTensorNames().size()); |
| 168 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 169 | GraphNode* node = OpFactory::newOp(tsh, op->GetOp(), op->GetAttribute(), op->GetQInfo(), idx, input_dtype, |
| 170 | input_rank, output_dtype, output_rank, weight_dtype, weight_rank); |
| 171 | if (!node) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 172 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 173 | if (weight_index == -1) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 174 | { |
| 175 | fprintf(g_func_debug.func_debug_file, |
| 176 | "OpFactory could not allocate op %8s input=(%s rank %d) -> (%s rank %d)", |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 177 | EnumNamesOp()[op->GetOp()], EnumNamesDType()[input_dtype], input_rank, |
| 178 | EnumNamesDType()[output_dtype], output_rank); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 179 | } |
| 180 | else |
| 181 | { |
| 182 | fprintf(g_func_debug.func_debug_file, |
| 183 | "OpFactory could not allocate op %8s input=(%s rank %d), weight=(%s rank %d) -> (%s rank %d)", |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 184 | EnumNamesOp()[op->GetOp()], EnumNamesDType()[input_dtype], input_rank, |
| 185 | EnumNamesDType()[weight_dtype], weight_rank, EnumNamesDType()[output_dtype], output_rank); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 188 | for (auto& ts : op->GetInputTensorNames()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 189 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 190 | fprintf(g_func_debug.func_debug_file, "Input: %s\n", ts.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 193 | for (auto& ts : op->GetOutputTensorNames()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 194 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 195 | fprintf(g_func_debug.func_debug_file, "Output: %s\n", ts.c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 196 | } |
| 197 | FATAL_ERROR("Unsupported operation type or rank."); |
| 198 | } |
| 199 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 200 | for (auto& name : op->GetInputTensorNames()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 201 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 202 | node->addInputName(name); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | for (auto name : op->GetOutputTensorNames()) |
| 206 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 207 | node->addOutputName(name); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 210 | addNode(node); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 211 | |
| 212 | // if node doesn't have any inputs (i.e. CONST) |
| 213 | // it should be ready for evaluation |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 214 | if (op->GetInputTensorNames().empty() && !node->getOnNextNodeList()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 215 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 216 | addToNextNodeList(node); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | idx++; |
| 220 | } |
| 221 | |
| 222 | for (auto ts : block->GetTensors()) |
| 223 | { |
| 224 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 225 | DEBUG_INFO(GT, "Creating tensor %s", ts->GetName().c_str()); |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 226 | TosaReference::Tensor* tensor = |
| 227 | TensorFactory::newTensor(ts->GetName(), ts->GetDtype(), ts->GetShape(), ts->GetShape().size()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 228 | |
Kevin Cheng | 82507d7 | 2021-06-17 16:01:59 -0700 | [diff] [blame] | 229 | if (!ts->GetData().empty()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 230 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 231 | if (tensor->allocate()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 232 | { |
Kevin Cheng | 82507d7 | 2021-06-17 16:01:59 -0700 | [diff] [blame] | 233 | WARNING("Fail to allocate tensor %s", tensor->getName().c_str()); |
| 234 | return 1; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Kevin Cheng | 82507d7 | 2021-06-17 16:01:59 -0700 | [diff] [blame] | 237 | switch (ts->GetDtype()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 238 | { |
Kevin Cheng | 82507d7 | 2021-06-17 16:01:59 -0700 | [diff] [blame] | 239 | case DType_INT8: |
| 240 | { |
| 241 | std::vector<int8_t> i8_data; |
| 242 | TosaSerializationHandler::ConvertU8toI8(ts->GetData(), tensor->getElementCount(), i8_data); |
| 243 | std::vector<int32_t> i32_data(i8_data.begin(), i8_data.end()); |
| 244 | tensor->setTensorValueInt32(i32_data.size(), i32_data.data()); |
| 245 | } |
| 246 | break; |
| 247 | case DType_INT16: |
| 248 | { |
| 249 | std::vector<int16_t> i16_data; |
| 250 | TosaSerializationHandler::ConvertU8toI16(ts->GetData(), tensor->getElementCount(), i16_data); |
| 251 | std::vector<int32_t> i32_data(i16_data.begin(), i16_data.end()); |
| 252 | tensor->setTensorValueInt32(i32_data.size(), i32_data.data()); |
| 253 | } |
| 254 | break; |
| 255 | case DType_INT32: |
| 256 | { |
| 257 | std::vector<int32_t> i32_data; |
| 258 | TosaSerializationHandler::ConvertU8toI32(ts->GetData(), tensor->getElementCount(), i32_data); |
| 259 | tensor->setTensorValueInt32(i32_data.size(), i32_data.data()); |
| 260 | } |
| 261 | break; |
| 262 | case DType_INT48: |
| 263 | { |
| 264 | std::vector<int64_t> i64_data; |
| 265 | TosaSerializationHandler::ConvertU8toI48(ts->GetData(), tensor->getElementCount(), i64_data); |
| 266 | tensor->setTensorValueInt64(i64_data.size(), i64_data.data()); |
| 267 | } |
| 268 | break; |
| 269 | case DType_FLOAT: |
| 270 | { |
| 271 | std::vector<float> fp32_data; |
| 272 | TosaSerializationHandler::ConvertU8toF32(ts->GetData(), tensor->getElementCount(), fp32_data); |
| 273 | tensor->setTensorValueFloat(fp32_data.size(), fp32_data.data()); |
| 274 | } |
| 275 | break; |
| 276 | case DType_BOOL: |
| 277 | { |
| 278 | std::vector<bool> bool_data; |
| 279 | TosaSerializationHandler::ConvertU8toBool(ts->GetData(), tensor->getElementCount(), bool_data); |
| 280 | |
| 281 | // std::vector<bool>::data() will return bit mask instead of array of bool array. |
| 282 | // Need to translate manually. |
| 283 | bool* bool_array = (bool*)calloc(bool_data.size(), sizeof(bool)); |
| 284 | for (size_t i = 0; i < bool_data.size(); i++) |
| 285 | { |
| 286 | bool_array[i] = bool_data[i]; |
| 287 | } |
| 288 | tensor->setTensorValueBool(bool_data.size(), bool_array); |
| 289 | } |
| 290 | break; |
| 291 | default: |
| 292 | FATAL_ERROR("Unsupported tensor type %s.", EnumNamesDType()[ts->GetDtype()]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | // update this->tensors |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 297 | addTensor(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | DEBUG_INFO(GT, "Enumerating block %s graph inputs", block->GetName().c_str()); |
| 301 | for (auto& input_name : block->GetInputs()) |
| 302 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 303 | TosaReference::Tensor* tensor = findTensorByName(input_name); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 304 | DEBUG_INFO(GT, "input tensor name=%s", input_name.c_str()); |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 305 | if (tensor) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 306 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 307 | tensor->setIsSubgraphInput(); |
| 308 | inputTensors.push_back(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 309 | } |
| 310 | else |
| 311 | { |
| 312 | FATAL_ERROR("loadGraphJson: Fail to find input tensor by name %s", input_name.c_str()); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | DEBUG_INFO(GT, "Enumerating block %s graph outputs", block->GetName().c_str()); |
| 317 | for (auto& output_name : block->GetOutputs()) |
| 318 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 319 | TosaReference::Tensor* tensor = findTensorByName(output_name); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 320 | DEBUG_INFO(GT, "output tensor name=%s\n", output_name.c_str()); |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 321 | if (tensor) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 322 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 323 | tensor->setIsSubgraphOutput(); |
| 324 | outputTensors.push_back(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | { |
| 328 | FATAL_ERROR("loadGraphJson: Fail to find output tensor by name %s", output_name.c_str()); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (DEBUG_ENABLED(DEBUG_VERB_HIGH, GT)) |
| 333 | { |
| 334 | dumpNextNodeList(g_func_debug.func_debug_file); |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | int SubgraphTraverser::isFullyEvaluated() const |
| 341 | { |
| 342 | return nextNodeList.empty(); |
| 343 | } |
| 344 | |
| 345 | GraphNode* SubgraphTraverser::getNextNode() |
| 346 | { |
| 347 | GraphNode* nextNode = nextNodeList.front(); |
| 348 | ASSERT_MSG(nextNode, "SubgraphTraverser::getNextNode(): called with empty next node list"); |
| 349 | ASSERT_MSG(nextNode->getOnNextNodeList(), |
| 350 | "SubgraphTraverser::getNextNode(): internal state error: node is not listed as being on next node list"); |
| 351 | |
| 352 | nextNodeList.pop_front(); |
| 353 | |
| 354 | nextNode->clearOnNextNodeList(); |
| 355 | return nextNode; |
| 356 | } |
| 357 | |
| 358 | int SubgraphTraverser::addToNextNodeList(GraphNode* nextNode) |
| 359 | { |
| 360 | ASSERT_MSG(nextNode, "SubgraphTraverser::addToNextNodeList(): called with no node"); |
| 361 | ASSERT_MSG(!nextNode->getOnNextNodeList(), |
| 362 | "SubgraphTraverser::addToNextNodeList(): internal state error: node is already on next node list"); |
| 363 | |
| 364 | nextNode->setOnNextNodeList(); |
| 365 | nextNodeList.push_back(nextNode); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int SubgraphTraverser::evaluateNextNode() |
| 371 | { |
| 372 | if (isFullyEvaluated()) |
| 373 | return 0; |
| 374 | |
| 375 | GraphNode* currNode = getNextNode(); |
| 376 | |
| 377 | DEBUG_INFO(GT, "Evaluating node_%03lu, %8s, output tensor=%s", currNode->getID(), EnumNamesOp()[currNode->getOp()], |
| 378 | currNode->getOutputNames()[0].c_str()); |
| 379 | |
| 380 | // Sanity check for never-ending loops |
| 381 | if (currNode->getEvalCount() >= MAX_EVAL_COUNT && (currNode->getEvalCount() % MAX_EVAL_COUNT) == 0) |
| 382 | { |
| 383 | WARNING("Node %lu has been evaluated %d times. Loop suspected.", currNode->getID(), currNode->getEvalCount()); |
| 384 | } |
| 385 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 386 | for (auto tensor : currNode->getOutputs()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 387 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 388 | if (!tensor->is_allocated()) |
| 389 | if (tensor->allocate()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 390 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 391 | FATAL_ERROR("Fail to allocate Eigen tensor %s", tensor->getName().c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | if (currNode->eval()) |
| 396 | { |
| 397 | FATAL_ERROR("Error evaluating node: %lu\n", currNode->getID()); |
| 398 | } |
| 399 | |
| 400 | // free input tensor if all of its consumers have all of their outputs ready and it's not block's output |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 401 | for (auto tensor : currNode->getInputs()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 402 | { |
| 403 | bool in_use = false; |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 404 | for (auto node : tensor->getConsumers()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 405 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 406 | if (!node->hasAllOutputsReady()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 407 | { |
| 408 | in_use = true; |
| 409 | } |
| 410 | } |
| 411 | for (auto name : block->GetOutputs()) |
| 412 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 413 | if (name == tensor->getName()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 414 | { |
| 415 | in_use = true; |
| 416 | } |
| 417 | } |
| 418 | if (!in_use) |
| 419 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 420 | tensor->deallocate(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | // Search the output tensors of this node to see if |
| 425 | // there are now new ready nodes available from completing this node |
| 426 | for (TosaReference::Tensor* tensor : currNode->getOutputs()) |
| 427 | { |
| 428 | for (GraphNode* node : tensor->getConsumers()) |
| 429 | { |
| 430 | if (!node->getOnNextNodeList() && node->hasAllInputsReady()) |
| 431 | { |
| 432 | addToNextNodeList(node); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (DEBUG_ENABLED(DEBUG_VERB_HIGH, GT)) |
| 438 | { |
| 439 | dumpNextNodeList(g_func_debug.func_debug_file); |
| 440 | } |
| 441 | |
| 442 | if (g_func_config.dump_intermediates) |
| 443 | { |
| 444 | currNode->dumpNode(g_func_debug.func_debug_file); |
| 445 | for (auto outs : currNode->getOutputs()) |
| 446 | { |
| 447 | outs->dumpTensorParams(g_func_debug.func_debug_file); |
| 448 | outs->dumpTensor(g_func_debug.func_debug_file); |
| 449 | fprintf(g_func_debug.func_debug_file, "\n"); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | int SubgraphTraverser::dumpNextNodeList(FILE* out) const |
| 457 | { |
| 458 | |
| 459 | // Dump next node list |
| 460 | fprintf(out, "Next node list\n"); |
| 461 | |
| 462 | if (nextNodeList.empty()) |
| 463 | { |
| 464 | fprintf(out, "<empty>\n"); |
| 465 | } |
| 466 | |
| 467 | for (auto gn : nextNodeList) |
| 468 | { |
| 469 | gn->dumpNode(out); |
| 470 | } |
| 471 | |
| 472 | fprintf(out, "Done.\n"); |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | int SubgraphTraverser::clearAllNodeMarkings() |
| 477 | { |
| 478 | for (GraphNode* currNode : nodes) |
| 479 | { |
| 480 | currNode->clearNodeMarked(); |
| 481 | } |
| 482 | |
| 483 | return false; |
| 484 | } |
| 485 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 486 | int SubgraphTraverser::addTensor(TosaReference::Tensor* tensor) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 487 | { |
| 488 | // Enforce no duplicate tensors/tensor names |
| 489 | // O(N), but the number of tensors is small |
| 490 | for (TosaReference::Tensor* currTensor : tensors) |
| 491 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 492 | if (tensor == currTensor || currTensor->getName() == tensor->getName()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 493 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 494 | FATAL_ERROR("Error: Duplicate tensor or tensor name being added to graph: %s\n", tensor->getName().c_str()); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 495 | return 1; |
| 496 | } |
| 497 | } |
| 498 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 499 | tensors.push_back(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 500 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 501 | if (tensor->getIsSubgraphInput()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 502 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 503 | inputTensors.push_back(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 506 | if (tensor->getIsSubgraphOutput()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 507 | { |
Kevin Cheng | 550ccc5 | 2021-03-03 11:21:43 -0800 | [diff] [blame] | 508 | outputTensors.push_back(tensor); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | int SubgraphTraverser::addNode(GraphNode* newNode) |
| 514 | { |
| 515 | // Enforce no duplicate nodes |
| 516 | for (GraphNode* currNode : nodes) |
| 517 | { |
| 518 | if (currNode == newNode) |
| 519 | { |
| 520 | FATAL_ERROR("Error: duplicate node being added to graph"); |
| 521 | return 1; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | nodes.push_back(newNode); |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | TosaReference::Tensor* SubgraphTraverser::findTensorByName(const std::string& name) const |
| 531 | { |
| 532 | for (TosaReference::Tensor* currTensor : tensors) |
| 533 | { |
| 534 | if (currTensor->getName() == name) |
| 535 | { |
| 536 | return currTensor; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | WARNING("Unable to find tensor with name: %s\n", name.c_str()); |
| 541 | |
| 542 | return nullptr; |
| 543 | } |
| 544 | |
| 545 | int SubgraphTraverser::linkTensorsAndNodes() |
| 546 | { |
| 547 | // Nodes have a list of input/output tensor names |
| 548 | // For each node, read this list, link up the tensors with their inputs/outputs |
| 549 | for (GraphNode* currNode : nodes) |
| 550 | { |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 551 | // Link inputs/consuming nodes |
| 552 | for (std::string& name : currNode->getInputNames()) |
| 553 | { |
| 554 | TosaReference::Tensor* t = findTensorByName(name); |
| 555 | if (!t) |
| 556 | { |
| 557 | FATAL_ERROR("linkTensorsAndNodes: Cannot find tensor %s in node %lu\n", name.c_str(), |
| 558 | currNode->getID()); |
| 559 | return 1; |
| 560 | } |
| 561 | |
| 562 | if (currNode->addInputTensor(t)) |
| 563 | { |
| 564 | FATAL_ERROR("linkTensorsAndNodes: cannot link tensor %s to node %lu\n", name.c_str(), |
| 565 | currNode->getID()); |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | if (t->addConsumer(currNode)) |
| 570 | { |
| 571 | FATAL_ERROR("linkTensorsAndNodes: cannot link consumer node %lu to tensor %s\n", currNode->getID(), |
| 572 | name.c_str()); |
| 573 | return 1; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | // Link outputs/producing nodes |
| 578 | for (std::string& name : currNode->getOutputNames()) |
| 579 | { |
| 580 | TosaReference::Tensor* t = findTensorByName(name); |
| 581 | if (!t) |
| 582 | { |
| 583 | FATAL_ERROR("linkTensorsAndNodes: Cannot find tensor %s in node %lu\n", name.c_str(), |
| 584 | currNode->getID()); |
| 585 | return 1; |
| 586 | } |
| 587 | |
| 588 | if (currNode->addOutputTensor(t)) |
| 589 | { |
| 590 | FATAL_ERROR("linkTensorsAndNodes: cannot link tensor %s to node %lu\n", name.c_str(), |
| 591 | currNode->getID()); |
| 592 | return 1; |
| 593 | } |
| 594 | |
| 595 | if (t->setProducer(currNode)) |
| 596 | { |
| 597 | FATAL_ERROR("linkTensorsAndNodes: cannot link producer node %lu to tensor tensor %s\n", |
| 598 | currNode->getID(), name.c_str()); |
| 599 | return 1; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | int SubgraphTraverser::validateGraph() |
| 608 | { |
| 609 | // Need to make sure that: |
| 610 | // - each tensor is actually used |
| 611 | // - input and output tesnsors truly are just input and just output |
| 612 | // Graph building already determined that each node has found its input/output tensors |
| 613 | |
| 614 | for (TosaReference::Tensor* currTensor : tensors) |
| 615 | { |
| 616 | |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 617 | // It's okay for block input tensor not being consumed by operators. |
| 618 | // This is common in control flow op execution. |
| 619 | if (!currTensor->getIsSubgraphInput()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 620 | { |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 621 | if (!currTensor->getProducer() && currTensor->getConsumers().empty()) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 622 | { |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 623 | WARNING("Graph inconsistency: TosaReference::Tensor %s has no producers or consumers\n", |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 624 | currTensor->getName().c_str()); |
| 625 | return 1; |
| 626 | } |
| 627 | } |
| 628 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 629 | if (g_func_config.tosa_profile == 0) |
| 630 | { |
| 631 | DType dtype = currTensor->getDtype(); |
| 632 | |
| 633 | // Float-point disallowed |
| 634 | if (dtype == DType_FLOAT) |
| 635 | { |
| 636 | WARNING("TOSA Base Inference profile selected: All floating point disabled, but %s tensor %s found\n", |
| 637 | EnumNamesDType()[dtype], currTensor->getName().c_str()); |
| 638 | return 1; |
| 639 | } |
| 640 | } |
| 641 | else if (g_func_config.tosa_profile == 1 || g_func_config.tosa_profile == 2) |
| 642 | { |
| 643 | // Do nothing. All FP types allowed |
| 644 | // Currently no implementation difference between Main Inference and Main Training modes |
| 645 | } |
| 646 | else |
| 647 | { |
| 648 | FATAL_ERROR("TOSA profile not recognized: %d", g_func_config.tosa_profile); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | for (GraphNode* currNode : nodes) |
| 653 | { |
| 654 | if (currNode->checkTensorAttributes()) |
| 655 | { |
| 656 | WARNING("TosaReference::Tensor attribute check failed"); |
| 657 | return 1; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | if (outputTensors.size() <= 0) |
| 662 | { |
| 663 | DEBUG_MED(GT, "Graph output tensor empty"); |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | int SubgraphTraverser::dumpGraph(FILE* out) const |
| 671 | { |
| 672 | int i = 0; |
| 673 | |
| 674 | fprintf(out, "Full graph dump:\n"); |
| 675 | for (GraphNode* currNode : nodes) |
| 676 | { |
| 677 | fprintf(out, "Node [%d]: ", i++); |
| 678 | currNode->dumpNode(out); |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | int SubgraphTraverser::evaluateAll() |
| 685 | { |
| 686 | // evaluation loop |
| 687 | while (!isFullyEvaluated()) |
| 688 | { |
| 689 | if (evaluateNextNode()) |
| 690 | { |
| 691 | return 1; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | return 0; |
| 696 | } |