1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/optional_debug_tools.h"
16
17 namespace tflite {
18
PrintIntVector(const std::vector<int> & v)19 void PrintIntVector(const std::vector<int>& v) {
20 for (const auto& it : v) {
21 printf(" %d", it);
22 }
23 printf("\n");
24 }
25
PrintTfLiteIntVector(const TfLiteIntArray * v)26 void PrintTfLiteIntVector(const TfLiteIntArray* v) {
27 if (!v) {
28 printf(" (null)\n");
29 return;
30 }
31 for (int k = 0; k < v->size; k++) {
32 printf(" %d", v->data[k]);
33 }
34 printf("\n");
35 }
36
TensorTypeName(TfLiteType type)37 const char* TensorTypeName(TfLiteType type) {
38 switch (type) {
39 case kTfLiteNoType:
40 return "kTfLiteNoType";
41 case kTfLiteFloat32:
42 return "kTfLiteFloat32";
43 case kTfLiteInt32:
44 return "kTfLiteInt32";
45 case kTfLiteUInt8:
46 return "kTfLiteUInt8";
47 case kTfLiteInt8:
48 return "kTfLiteInt8";
49 case kTfLiteInt64:
50 return "kTfLiteInt64";
51 case kTfLiteString:
52 return "kTfLiteString";
53 case kTfLiteBool:
54 return "kTfLiteBool";
55 case kTfLiteInt16:
56 return "kTfLiteInt16";
57 case kTfLiteComplex64:
58 return "kTfLiteComplex64";
59 }
60 return "(invalid)";
61 }
62
AllocTypeName(TfLiteAllocationType type)63 const char* AllocTypeName(TfLiteAllocationType type) {
64 switch (type) {
65 case kTfLiteMemNone:
66 return "kTfLiteMemNone";
67 case kTfLiteMmapRo:
68 return "kTfLiteMmapRo";
69 case kTfLiteDynamic:
70 return "kTfLiteDynamic";
71 case kTfLiteArenaRw:
72 return "kTfLiteArenaRw";
73 case kTfLiteArenaRwPersistent:
74 return "kTfLiteArenaRwPersistent";
75 }
76 return "(invalid)";
77 }
78
79 // Prints a dump of what tensors and what nodes are in the interpreter.
PrintInterpreterState(Interpreter * interpreter)80 void PrintInterpreterState(Interpreter* interpreter) {
81 printf("Interpreter has %zu tensors and %zu nodes\n",
82 interpreter->tensors_size(), interpreter->nodes_size());
83 printf("Inputs:");
84 PrintIntVector(interpreter->inputs());
85 printf("Outputs:");
86 PrintIntVector(interpreter->outputs());
87 printf("\n");
88 for (size_t tensor_index = 0; tensor_index < interpreter->tensors_size();
89 tensor_index++) {
90 TfLiteTensor* tensor = interpreter->tensor(static_cast<int>(tensor_index));
91 printf("Tensor %3zu %-20s %10s %15s %10zu bytes (%4.1f MB) ", tensor_index,
92 tensor->name, TensorTypeName(tensor->type),
93 AllocTypeName(tensor->allocation_type), tensor->bytes,
94 (static_cast<float>(tensor->bytes) / (1 << 20)));
95 PrintTfLiteIntVector(tensor->dims);
96 }
97 printf("\n");
98 for (size_t node_index = 0; node_index < interpreter->nodes_size();
99 node_index++) {
100 const std::pair<TfLiteNode, TfLiteRegistration>* node_and_reg =
101 interpreter->node_and_registration(static_cast<int>(node_index));
102 const TfLiteNode& node = node_and_reg->first;
103 const TfLiteRegistration& reg = node_and_reg->second;
104 if (reg.custom_name != nullptr) {
105 printf("Node %3zu Operator Custom Name %s\n", node_index,
106 reg.custom_name);
107 } else {
108 printf("Node %3zu Operator Builtin Code %3d\n", node_index,
109 reg.builtin_code);
110 }
111 printf(" Inputs:");
112 PrintTfLiteIntVector(node.inputs);
113 printf(" Outputs:");
114 PrintTfLiteIntVector(node.outputs);
115 }
116 }
117
118 } // namespace tflite
119