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 #include <stddef.h>
18 #include <stdio.h>
19
20 #include <utility>
21 #include <vector>
22
23 #include "tensorflow/lite/interpreter.h"
24 #include "tensorflow/lite/schema/schema_generated.h"
25
26 namespace tflite {
27
PrintIntVector(const std::vector<int> & v)28 void PrintIntVector(const std::vector<int>& v) {
29 for (const auto& it : v) {
30 printf(" %d", it);
31 }
32 printf("\n");
33 }
34
PrintTfLiteIntVector(const TfLiteIntArray * v)35 void PrintTfLiteIntVector(const TfLiteIntArray* v) {
36 if (!v) {
37 printf(" (null)\n");
38 return;
39 }
40 for (int k = 0; k < v->size; k++) {
41 printf(" %d", v->data[k]);
42 }
43 printf("\n");
44 }
45
TensorTypeName(TfLiteType type)46 const char* TensorTypeName(TfLiteType type) {
47 switch (type) {
48 case kTfLiteNoType:
49 return "kTfLiteNoType";
50 case kTfLiteFloat32:
51 return "kTfLiteFloat32";
52 case kTfLiteInt32:
53 return "kTfLiteInt32";
54 case kTfLiteUInt32:
55 return "kTfLiteUInt32";
56 case kTfLiteUInt8:
57 return "kTfLiteUInt8";
58 case kTfLiteInt8:
59 return "kTfLiteInt8";
60 case kTfLiteInt64:
61 return "kTfLiteInt64";
62 case kTfLiteUInt64:
63 return "kTfLiteUInt64";
64 case kTfLiteString:
65 return "kTfLiteString";
66 case kTfLiteBool:
67 return "kTfLiteBool";
68 case kTfLiteInt16:
69 return "kTfLiteInt16";
70 case kTfLiteComplex64:
71 return "kTfLiteComplex64";
72 case kTfLiteComplex128:
73 return "kTfLiteComplex128";
74 case kTfLiteFloat16:
75 return "kTfLiteFloat16";
76 case kTfLiteFloat64:
77 return "kTfLiteFloat64";
78 case kTfLiteResource:
79 return "kTfLiteResource";
80 case kTfLiteVariant:
81 return "kTfLiteVariant";
82 }
83 return "(invalid)";
84 }
85
AllocTypeName(TfLiteAllocationType type)86 const char* AllocTypeName(TfLiteAllocationType type) {
87 switch (type) {
88 case kTfLiteMemNone:
89 return "kTfLiteMemNone";
90 case kTfLiteMmapRo:
91 return "kTfLiteMmapRo";
92 case kTfLiteDynamic:
93 return "kTfLiteDynamic";
94 case kTfLiteArenaRw:
95 return "kTfLiteArenaRw";
96 case kTfLiteArenaRwPersistent:
97 return "kTfLiteArenaRwPersistent";
98 case kTfLitePersistentRo:
99 return "kTfLitePersistentRo";
100 case kTfLiteCustom:
101 return "kTfLiteCustom";
102 }
103 return "(invalid)";
104 }
105
106 // Prints a dump of what tensors and what nodes are in the interpreter.
PrintInterpreterState(Interpreter * interpreter)107 void PrintInterpreterState(Interpreter* interpreter) {
108 printf("Interpreter has %zu tensors and %zu nodes\n",
109 interpreter->tensors_size(), interpreter->nodes_size());
110 printf("Inputs:");
111 PrintIntVector(interpreter->inputs());
112 printf("Outputs:");
113 PrintIntVector(interpreter->outputs());
114 printf("\n");
115 for (size_t tensor_index = 0; tensor_index < interpreter->tensors_size();
116 tensor_index++) {
117 TfLiteTensor* tensor = interpreter->tensor(static_cast<int>(tensor_index));
118 printf("Tensor %3zu %-20s %10s %15s %10zu bytes (%4.1f MB) ", tensor_index,
119 tensor->name, TensorTypeName(tensor->type),
120 AllocTypeName(tensor->allocation_type), tensor->bytes,
121 (static_cast<float>(tensor->bytes) / (1 << 20)));
122 PrintTfLiteIntVector(tensor->dims);
123 }
124 printf("\n");
125 for (size_t node_index = 0; node_index < interpreter->nodes_size();
126 node_index++) {
127 const std::pair<TfLiteNode, TfLiteRegistration>* node_and_reg =
128 interpreter->node_and_registration(static_cast<int>(node_index));
129 const TfLiteNode& node = node_and_reg->first;
130 const TfLiteRegistration& reg = node_and_reg->second;
131 if (reg.custom_name != nullptr) {
132 printf("Node %3zu Operator Custom Name %s\n", node_index,
133 reg.custom_name);
134 } else {
135 printf("Node %3zu Operator Builtin Code %3d %s\n", node_index,
136 reg.builtin_code, EnumNamesBuiltinOperator()[reg.builtin_code]);
137 }
138 printf(" Inputs:");
139 PrintTfLiteIntVector(node.inputs);
140 printf(" Outputs:");
141 PrintTfLiteIntVector(node.outputs);
142 if (node.intermediates && node.intermediates->size) {
143 printf(" Intermediates:");
144 PrintTfLiteIntVector(node.intermediates);
145 }
146 if (node.temporaries && node.temporaries->size) {
147 printf(" Temporaries:");
148 PrintTfLiteIntVector(node.temporaries);
149 }
150 }
151 }
152
153 } // namespace tflite
154