MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ResizeOutput

Function ResizeOutput

tensorflow/lite/kernels/topk_v2.cc:32–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30
31namespace {
32TfLiteStatus ResizeOutput(TfLiteContext* context, TfLiteNode* node) {
33 const TfLiteTensor* top_k = GetInput(context, node, kInputTopK);
34 // INT32 number of top results is supported.
35 TF_LITE_ENSURE_EQ(context, top_k->type, kTfLiteInt32);
36 // Check that the tensor contains only one value.
37 TF_LITE_ENSURE_EQ(context, NumElements(top_k), 1);
38 const int32 k = *GetTensorData<int32_t>(top_k);
39
40 const TfLiteTensor* input = GetInput(context, node, kInputTensor);
41 const int num_dimensions = NumDimensions(input);
42 // Check that input has one or more dimensions.
43 TF_LITE_ENSURE_MSG(context, input->dims->size >= 1,
44 "TopK k input must have 1 or more dimensions.");
45 // Check that k is less or equal the internal dimension.
46 TF_LITE_ENSURE_MSG(context, k <= input->dims->data[num_dimensions - 1],
47 "TopK k is higher than the internal dimension.");
48
49 TfLiteIntArray* output_indexes_shape = TfLiteIntArrayCreate(num_dimensions);
50 TfLiteIntArray* output_values_shape = TfLiteIntArrayCreate(num_dimensions);
51 for (int i = 0; i < num_dimensions - 1; ++i) {
52 output_indexes_shape->data[i] = input->dims->data[i];
53 output_values_shape->data[i] = input->dims->data[i];
54 }
55 output_indexes_shape->data[num_dimensions - 1] = k;
56 output_values_shape->data[num_dimensions - 1] = k;
57 TfLiteTensor* output_indexes = GetOutput(context, node, kOutputIndexes);
58 TfLiteTensor* output_values = GetOutput(context, node, kOutputValues);
59 // Force output types.
60 output_indexes->type = kTfLiteInt32;
61 output_values->type = input->type;
62 auto resize_tensor = [context](TfLiteTensor* tensor, TfLiteIntArray* new_size,
63 TfLiteIntArray* delete_on_error) {
64 TfLiteStatus status = context->ResizeTensor(context, tensor, new_size);
65 if (status != kTfLiteOk) {
66 if (delete_on_error != nullptr) {
67 TfLiteIntArrayFree(delete_on_error);
68 }
69 }
70 return status;
71 };
72 TF_LITE_ENSURE_OK(context, resize_tensor(output_indexes, output_indexes_shape,
73 output_values_shape));
74 TF_LITE_ENSURE_OK(context,
75 resize_tensor(output_values, output_values_shape, nullptr));
76 return kTfLiteOk;
77}
78
79// Class that collects indices of top k values. Based on template
80// tensorflow::gtl::TopN<> but, for optimization, it re-uses the same container.

Callers 2

PrepareFunction · 0.70
EvalFunction · 0.70

Calls 7

GetInputFunction · 0.85
NumDimensionsFunction · 0.85
TfLiteIntArrayCreateFunction · 0.85
GetOutputFunction · 0.85
TfLiteIntArrayFreeFunction · 0.85
ResizeTensorMethod · 0.80
NumElementsFunction · 0.70

Tested by

no test coverage detected