blob: d64cb389128163c3b966a37c46bdbed960302d3b [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
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
18using namespace TosaReference;
19using namespace Eigen;
20using namespace tosa;
21
22SubgraphTraverser::SubgraphTraverser(TosaSerializationBasicBlock* _block, TosaSerializationHandler* _tsh)
23{
24 block = _block;
25 tsh = _tsh;
26
27 tensors.clear();
28 nodes.clear();
29 nextNodeList.clear();
30}
31
32SubgraphTraverser::~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
53int SubgraphTraverser::getNumInputTensors() const
54{
55 return inputTensors.size();
56}
57
58TosaReference::Tensor* SubgraphTraverser::getInputTensor(const unsigned int idx) const
59{
60 return inputTensors[idx];
61}
62
63TosaReference::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
76int SubgraphTraverser::getNumOutputTensors() const
77{
78 return outputTensors.size();
79}
80
81TosaReference::Tensor* SubgraphTraverser::getOutputTensor(const unsigned int idx) const
82{
83 return outputTensors[idx];
84}
85
86TosaReference::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
99int SubgraphTraverser::initializeGraph()
100{
Eric Kunzee5e26762020-10-13 16:11:07 -0700101 int idx = 0;
102 for (auto op : block->GetOperators())
103 {
104 // translated TosaSerializationOperator to GraphNode
Kevin Cheng550ccc52021-03-03 11:21:43 -0800105 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 Kunzee5e26762020-10-13 16:11:07 -0700115 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800116 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 Kunzee5e26762020-10-13 16:11:07 -0700130 }
131
Kevin Cheng550ccc52021-03-03 11:21:43 -0800132 if (input_index != -1)
Kevin Chengdf862692021-02-22 15:22:22 -0800133 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800134 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 Chengdf862692021-02-22 15:22:22 -0800137
Kevin Cheng550ccc52021-03-03 11:21:43 -0800138 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 Chengdf862692021-02-22 15:22:22 -0800144 }
145
Kevin Cheng550ccc52021-03-03 11:21:43 -0800146 if (weight_index != -1)
Eric Kunzee5e26762020-10-13 16:11:07 -0700147 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800148 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 Kunzee5e26762020-10-13 16:11:07 -0700157 }
158
Kevin Cheng550ccc52021-03-03 11:21:43 -0800159 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 Kunzee5e26762020-10-13 16:11:07 -0700166 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 Cheng550ccc52021-03-03 11:21:43 -0800169 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 Kunzee5e26762020-10-13 16:11:07 -0700172 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800173 if (weight_index == -1)
Eric Kunzee5e26762020-10-13 16:11:07 -0700174 {
175 fprintf(g_func_debug.func_debug_file,
176 "OpFactory could not allocate op %8s input=(%s rank %d) -> (%s rank %d)",
Kevin Cheng550ccc52021-03-03 11:21:43 -0800177 EnumNamesOp()[op->GetOp()], EnumNamesDType()[input_dtype], input_rank,
178 EnumNamesDType()[output_dtype], output_rank);
Eric Kunzee5e26762020-10-13 16:11:07 -0700179 }
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 Cheng550ccc52021-03-03 11:21:43 -0800184 EnumNamesOp()[op->GetOp()], EnumNamesDType()[input_dtype], input_rank,
185 EnumNamesDType()[weight_dtype], weight_rank, EnumNamesDType()[output_dtype], output_rank);
Eric Kunzee5e26762020-10-13 16:11:07 -0700186 }
187
Kevin Cheng550ccc52021-03-03 11:21:43 -0800188 for (auto& ts : op->GetInputTensorNames())
Eric Kunzee5e26762020-10-13 16:11:07 -0700189 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800190 fprintf(g_func_debug.func_debug_file, "Input: %s\n", ts.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -0700191 }
192
Kevin Cheng550ccc52021-03-03 11:21:43 -0800193 for (auto& ts : op->GetOutputTensorNames())
Eric Kunzee5e26762020-10-13 16:11:07 -0700194 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800195 fprintf(g_func_debug.func_debug_file, "Output: %s\n", ts.c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -0700196 }
197 FATAL_ERROR("Unsupported operation type or rank.");
198 }
199
Kevin Cheng550ccc52021-03-03 11:21:43 -0800200 for (auto& name : op->GetInputTensorNames())
Eric Kunzee5e26762020-10-13 16:11:07 -0700201 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800202 node->addInputName(name);
Eric Kunzee5e26762020-10-13 16:11:07 -0700203 }
204
205 for (auto name : op->GetOutputTensorNames())
206 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800207 node->addOutputName(name);
Eric Kunzee5e26762020-10-13 16:11:07 -0700208 }
209
Kevin Cheng550ccc52021-03-03 11:21:43 -0800210 addNode(node);
Eric Kunzee5e26762020-10-13 16:11:07 -0700211
212 // if node doesn't have any inputs (i.e. CONST)
213 // it should be ready for evaluation
Kevin Cheng550ccc52021-03-03 11:21:43 -0800214 if (op->GetInputTensorNames().empty() && !node->getOnNextNodeList())
Eric Kunzee5e26762020-10-13 16:11:07 -0700215 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800216 addToNextNodeList(node);
Eric Kunzee5e26762020-10-13 16:11:07 -0700217 }
218
219 idx++;
220 }
221
222 for (auto ts : block->GetTensors())
223 {
224
Eric Kunzee5e26762020-10-13 16:11:07 -0700225 DEBUG_INFO(GT, "Creating tensor %s", ts->GetName().c_str());
Kevin Cheng550ccc52021-03-03 11:21:43 -0800226 TosaReference::Tensor* tensor =
227 TensorFactory::newTensor(ts->GetName(), ts->GetDtype(), ts->GetShape(), ts->GetShape().size());
Eric Kunzee5e26762020-10-13 16:11:07 -0700228
Kevin Cheng82507d72021-06-17 16:01:59 -0700229 if (!ts->GetData().empty())
Eric Kunzee5e26762020-10-13 16:11:07 -0700230 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800231 if (tensor->allocate())
Eric Kunzee5e26762020-10-13 16:11:07 -0700232 {
Kevin Cheng82507d72021-06-17 16:01:59 -0700233 WARNING("Fail to allocate tensor %s", tensor->getName().c_str());
234 return 1;
Eric Kunzee5e26762020-10-13 16:11:07 -0700235 }
236
Kevin Cheng82507d72021-06-17 16:01:59 -0700237 switch (ts->GetDtype())
Eric Kunzee5e26762020-10-13 16:11:07 -0700238 {
Kevin Cheng82507d72021-06-17 16:01:59 -0700239 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 Kunzee5e26762020-10-13 16:11:07 -0700293 }
294 }
295
296 // update this->tensors
Kevin Cheng550ccc52021-03-03 11:21:43 -0800297 addTensor(tensor);
Eric Kunzee5e26762020-10-13 16:11:07 -0700298 }
299
300 DEBUG_INFO(GT, "Enumerating block %s graph inputs", block->GetName().c_str());
301 for (auto& input_name : block->GetInputs())
302 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800303 TosaReference::Tensor* tensor = findTensorByName(input_name);
Eric Kunzee5e26762020-10-13 16:11:07 -0700304 DEBUG_INFO(GT, "input tensor name=%s", input_name.c_str());
Kevin Cheng550ccc52021-03-03 11:21:43 -0800305 if (tensor)
Eric Kunzee5e26762020-10-13 16:11:07 -0700306 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800307 tensor->setIsSubgraphInput();
308 inputTensors.push_back(tensor);
Eric Kunzee5e26762020-10-13 16:11:07 -0700309 }
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 Cheng550ccc52021-03-03 11:21:43 -0800319 TosaReference::Tensor* tensor = findTensorByName(output_name);
Eric Kunzee5e26762020-10-13 16:11:07 -0700320 DEBUG_INFO(GT, "output tensor name=%s\n", output_name.c_str());
Kevin Cheng550ccc52021-03-03 11:21:43 -0800321 if (tensor)
Eric Kunzee5e26762020-10-13 16:11:07 -0700322 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800323 tensor->setIsSubgraphOutput();
324 outputTensors.push_back(tensor);
Eric Kunzee5e26762020-10-13 16:11:07 -0700325 }
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
340int SubgraphTraverser::isFullyEvaluated() const
341{
342 return nextNodeList.empty();
343}
344
345GraphNode* 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
358int 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
370int 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 Cheng550ccc52021-03-03 11:21:43 -0800386 for (auto tensor : currNode->getOutputs())
Eric Kunzee5e26762020-10-13 16:11:07 -0700387 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800388 if (!tensor->is_allocated())
389 if (tensor->allocate())
Eric Kunzee5e26762020-10-13 16:11:07 -0700390 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800391 FATAL_ERROR("Fail to allocate Eigen tensor %s", tensor->getName().c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -0700392 }
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 Cheng550ccc52021-03-03 11:21:43 -0800401 for (auto tensor : currNode->getInputs())
Eric Kunzee5e26762020-10-13 16:11:07 -0700402 {
403 bool in_use = false;
Kevin Cheng550ccc52021-03-03 11:21:43 -0800404 for (auto node : tensor->getConsumers())
Eric Kunzee5e26762020-10-13 16:11:07 -0700405 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800406 if (!node->hasAllOutputsReady())
Eric Kunzee5e26762020-10-13 16:11:07 -0700407 {
408 in_use = true;
409 }
410 }
411 for (auto name : block->GetOutputs())
412 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800413 if (name == tensor->getName())
Eric Kunzee5e26762020-10-13 16:11:07 -0700414 {
415 in_use = true;
416 }
417 }
418 if (!in_use)
419 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800420 tensor->deallocate();
Eric Kunzee5e26762020-10-13 16:11:07 -0700421 }
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
456int 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
476int SubgraphTraverser::clearAllNodeMarkings()
477{
478 for (GraphNode* currNode : nodes)
479 {
480 currNode->clearNodeMarked();
481 }
482
483 return false;
484}
485
Kevin Cheng550ccc52021-03-03 11:21:43 -0800486int SubgraphTraverser::addTensor(TosaReference::Tensor* tensor)
Eric Kunzee5e26762020-10-13 16:11:07 -0700487{
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 Cheng550ccc52021-03-03 11:21:43 -0800492 if (tensor == currTensor || currTensor->getName() == tensor->getName())
Eric Kunzee5e26762020-10-13 16:11:07 -0700493 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800494 FATAL_ERROR("Error: Duplicate tensor or tensor name being added to graph: %s\n", tensor->getName().c_str());
Eric Kunzee5e26762020-10-13 16:11:07 -0700495 return 1;
496 }
497 }
498
Kevin Cheng550ccc52021-03-03 11:21:43 -0800499 tensors.push_back(tensor);
Eric Kunzee5e26762020-10-13 16:11:07 -0700500
Kevin Cheng550ccc52021-03-03 11:21:43 -0800501 if (tensor->getIsSubgraphInput())
Eric Kunzee5e26762020-10-13 16:11:07 -0700502 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800503 inputTensors.push_back(tensor);
Eric Kunzee5e26762020-10-13 16:11:07 -0700504 }
505
Kevin Cheng550ccc52021-03-03 11:21:43 -0800506 if (tensor->getIsSubgraphOutput())
Eric Kunzee5e26762020-10-13 16:11:07 -0700507 {
Kevin Cheng550ccc52021-03-03 11:21:43 -0800508 outputTensors.push_back(tensor);
Eric Kunzee5e26762020-10-13 16:11:07 -0700509 }
510
511 return 0;
512}
513int 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
530TosaReference::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
545int 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 Kunzee5e26762020-10-13 16:11:07 -0700551 // 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
607int 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 Cheng14d7f7a2021-05-12 10:44:49 -0700617 // 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 Kunzee5e26762020-10-13 16:11:07 -0700620 {
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700621 if (!currTensor->getProducer() && currTensor->getConsumers().empty())
Eric Kunzee5e26762020-10-13 16:11:07 -0700622 {
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700623 WARNING("Graph inconsistency: TosaReference::Tensor %s has no producers or consumers\n",
Eric Kunzee5e26762020-10-13 16:11:07 -0700624 currTensor->getName().c_str());
625 return 1;
626 }
627 }
628
Eric Kunzee5e26762020-10-13 16:11:07 -0700629 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
670int 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
684int SubgraphTraverser::evaluateAll()
685{
686 // evaluation loop
687 while (!isFullyEvaluated())
688 {
689 if (evaluateNextNode())
690 {
691 return 1;
692 }
693 }
694
695 return 0;
696}