Skip to content

Commit f60e4fa

Browse files
authored
Automated sync from github.com/tensorflow/tensorflow (#3124)
BUG=automated sync from upstream NO_CHECK_TFLITE_FILES=automated sync from upstream
1 parent 3200ccd commit f60e4fa

File tree

3 files changed

+83
-37
lines changed

3 files changed

+83
-37
lines changed

tensorflow/lite/core/c/common.cc

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,41 @@ void TfLiteVarArrayFree(T* a) {
104104

105105
#ifndef TF_LITE_STATIC_MEMORY
106106

107+
TfLiteSparsity TfLiteSparsityClone(const TfLiteSparsity& src) {
108+
TfLiteSparsity dst = src;
109+
dst.traversal_order = TfLiteIntArrayCopy(src.traversal_order);
110+
dst.block_map = TfLiteIntArrayCopy(src.block_map);
111+
if (src.dim_metadata) {
112+
dst.dim_metadata = reinterpret_cast<TfLiteDimensionMetadata*>(
113+
calloc(1, sizeof(TfLiteDimensionMetadata) * src.dim_metadata_size));
114+
for (int i = 0; i < src.dim_metadata_size; ++i) {
115+
dst.dim_metadata[i] = src.dim_metadata[i];
116+
dst.dim_metadata[i].array_segments =
117+
TfLiteIntArrayCopy(src.dim_metadata[i].array_segments);
118+
dst.dim_metadata[i].array_indices =
119+
TfLiteIntArrayCopy(src.dim_metadata[i].array_indices);
120+
}
121+
}
122+
return dst;
123+
}
124+
125+
// Clones the source sparsity to a newly allocated object.
126+
TfLiteSparsity* TfLiteSparsityClone(const TfLiteSparsity* const src) {
127+
if (!src) {
128+
return nullptr;
129+
}
130+
TfLiteSparsity* dst =
131+
reinterpret_cast<TfLiteSparsity*>(calloc(1, sizeof(TfLiteSparsity)));
132+
*dst = TfLiteSparsityClone(*src);
133+
return dst;
134+
}
135+
136+
#endif // TF_LITE_STATIC_MEMORY
137+
138+
} // namespace
139+
140+
#ifndef TF_LITE_STATIC_MEMORY
141+
107142
TfLiteQuantization TfLiteQuantizationClone(const TfLiteQuantization& src) {
108143
TfLiteQuantization dst;
109144
dst.type = src.type;
@@ -136,39 +171,8 @@ TfLiteQuantization TfLiteQuantizationClone(const TfLiteQuantization& src) {
136171
return dst;
137172
}
138173

139-
TfLiteSparsity TfLiteSparsityClone(const TfLiteSparsity& src) {
140-
TfLiteSparsity dst = src;
141-
dst.traversal_order = TfLiteIntArrayCopy(src.traversal_order);
142-
dst.block_map = TfLiteIntArrayCopy(src.block_map);
143-
if (src.dim_metadata) {
144-
dst.dim_metadata = reinterpret_cast<TfLiteDimensionMetadata*>(
145-
calloc(1, sizeof(TfLiteDimensionMetadata) * src.dim_metadata_size));
146-
for (int i = 0; i < src.dim_metadata_size; ++i) {
147-
dst.dim_metadata[i] = src.dim_metadata[i];
148-
dst.dim_metadata[i].array_segments =
149-
TfLiteIntArrayCopy(src.dim_metadata[i].array_segments);
150-
dst.dim_metadata[i].array_indices =
151-
TfLiteIntArrayCopy(src.dim_metadata[i].array_indices);
152-
}
153-
}
154-
return dst;
155-
}
156-
157-
// Clones the source sparsity to a newly allocated object.
158-
TfLiteSparsity* TfLiteSparsityClone(const TfLiteSparsity* const src) {
159-
if (!src) {
160-
return nullptr;
161-
}
162-
TfLiteSparsity* dst =
163-
reinterpret_cast<TfLiteSparsity*>(calloc(1, sizeof(TfLiteSparsity)));
164-
*dst = TfLiteSparsityClone(*src);
165-
return dst;
166-
}
167-
168174
#endif // TF_LITE_STATIC_MEMORY
169175

170-
} // namespace
171-
172176
extern "C" {
173177

174178
size_t TfLiteIntArrayGetSizeInBytes(int size) {
@@ -247,6 +251,11 @@ void TfLiteQuantizationFree(TfLiteQuantization* quantization) {
247251
}
248252
free(q_params);
249253
}
254+
if (quantization->type == kTfLiteBlockwiseQuantization) {
255+
TfLiteBlockwiseQuantization* q_params =
256+
reinterpret_cast<TfLiteBlockwiseQuantization*>(quantization->params);
257+
free(q_params);
258+
}
250259
quantization->params = nullptr;
251260
quantization->type = kTfLiteNoQuantization;
252261
}

tensorflow/lite/core/c/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ TfLiteStatus TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor);
788788
/// If all dimensions are known, this is the same as `t->dims`.
789789
/// (`dims_signature` is NULL or empty if all dimensions are known.)
790790
const TfLiteIntArray* TfLiteTensorGetDimsSignature(const TfLiteTensor* t);
791+
791792
#endif // TF_LITE_STATIC_MEMORY
792793

793794
/// WARNING: This is an experimental interface that is subject to change.
@@ -1633,5 +1634,8 @@ TfLiteStatus TfLiteTensorVariantRealloc(TfLiteTensor* t,
16331634
return kTfLiteOk;
16341635
}
16351636

1637+
// Returns a copy of the quantization parameters of the tensor.
1638+
TfLiteQuantization TfLiteQuantizationClone(const TfLiteQuantization& src);
1639+
16361640
#endif // __cplusplus
16371641
#endif // TENSORFLOW_LITE_CORE_C_COMMON_H_

tensorflow/lite/tools/visualize.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343
body {font-family: sans-serif; background-color: #fa0;}
4444
table {background-color: #eca;}
4545
th {background-color: black; color: white;}
46+
/* Constrain table cells to a max size and make them scrollable. */
47+
.data-table td {
48+
max-width: 900px;
49+
}
50+
.data-table .cell-content {
51+
max-height: 200px;
52+
overflow: auto;
53+
white-space: pre-wrap;
54+
word-break: break-all;
55+
}
4656
h1 {
4757
background-color: ffaa00;
4858
padding:5px;
@@ -284,6 +294,27 @@ def __call__(self, x):
284294
return html
285295

286296

297+
def QuantizationMapper(q):
298+
"""Pretty-print the quantization dictionary, truncating large arrays."""
299+
if not q:
300+
return ""
301+
302+
items_str = []
303+
for key, value in q.items():
304+
key_str = repr(key)
305+
# In TFLite, quantization arrays can be large.
306+
if isinstance(value, list) and len(value) > 20:
307+
head = value[:10]
308+
tail = value[-10:]
309+
value_str = (f"[{', '.join(map(repr, head))}, ..., "
310+
f"{', '.join(map(repr, tail))}]")
311+
else:
312+
value_str = repr(value)
313+
items_str.append(f"{key_str}: {value_str}")
314+
315+
return f"{{{', '.join(items_str)}}}"
316+
317+
287318
def GenerateGraph(subgraph_idx, g, opcode_mapper):
288319
"""Produces the HTML required to have a d3 visualization of the dag."""
289320

@@ -359,8 +390,8 @@ def GenerateTableHtml(items, keys_to_print, display_index=True):
359390
An html table.
360391
"""
361392
html = ""
362-
# Print the list of items
363-
html += "<table><tr>\n"
393+
# Print the list of items
394+
html += "<table class='data-table'>\n"
364395
html += "<tr>\n"
365396
if display_index:
366397
html += "<th>index</th>"
@@ -375,7 +406,7 @@ def GenerateTableHtml(items, keys_to_print, display_index=True):
375406
for h, mapper in keys_to_print:
376407
val = tensor[h] if h in tensor else None
377408
val = val if mapper is None else mapper(val)
378-
html += "<td>%s</td>\n" % val
409+
html += "<td><div class='cell-content'>%s</div></td>\n" % val
379410

380411
html += "</tr>\n"
381412
html += "</table>\n"
@@ -465,11 +496,13 @@ def create_html(tflite_input, input_is_filepath=True): # pylint: disable=invali
465496
toplevel_stuff = [("filename", None), ("version", None),
466497
("description", None)]
467498

468-
html += "<table>\n"
499+
html += "<table class='data-table'>\n"
469500
for key, mapping in toplevel_stuff:
470501
if not mapping:
471502
mapping = lambda x: x
472-
html += "<tr><th>%s</th><td>%s</td></tr>\n" % (key, mapping(data.get(key)))
503+
val = mapping(data.get(key))
504+
html += ("<tr><th>%s</th><td><div class='cell-content'>%s</div></td></tr>\n"
505+
% (key, val))
473506
html += "</table>\n"
474507

475508
# Spec on what keys to display
@@ -493,7 +526,7 @@ def create_html(tflite_input, input_is_filepath=True): # pylint: disable=invali
493526
tensor_keys_to_display = [("name", NameListToString),
494527
("type", TensorTypeToName), ("shape", None),
495528
("shape_signature", None), ("buffer", None),
496-
("quantization", None)]
529+
("quantization", QuantizationMapper)]
497530

498531
html += "<h2>Subgraph %d</h2>\n" % subgraph_idx
499532

0 commit comments

Comments
 (0)