tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/lite/tools/evaluation/evaluation_stage.h"
 
#include <memory>
 
#include <gtest/gtest.h>
#include "absl/container/flat_hash_map.h"
#include "tensorflow/lite/tools/evaluation/identity_stage.h"
#include "tensorflow/lite/tools/evaluation/proto/evaluation_config.pb.h"
#include "tensorflow/lite/tools/evaluation/proto/evaluation_stages.pb.h"
 
namespace tflite {
namespace evaluation {
namespace {
 
constexpr char kIdentityStageName[] = "identity_stage";
constexpr char kDefaultValueName[] = "default";
constexpr char kInputValueName[] = "in";
constexpr char kOutputValueName[] = "out";
constexpr char kInitializerMapping[] = "DEFAULT_VALUE:default";
constexpr char kInputMapping[] = "INPUT_VALUE:in";
constexpr char kOutputMapping[] = "OUTPUT_VALUE:out";
 
EvaluationStageConfig GetIdentityStageConfig() {
  IdentityStage_ENABLE();
  EvaluationStageConfig config;
  config.set_name(kIdentityStageName);
  config.mutable_specification()->set_process_class(IDENTITY);
  config.add_initializers(kInitializerMapping);
  config.add_inputs(kInputMapping);
  config.add_outputs(kOutputMapping);
  return config;
}
 
TEST(EvaluationStage, CreateFailsForMissingSpecification) {
  // Construct
  EvaluationStageConfig config;
  config.set_name(kIdentityStageName);
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  EXPECT_EQ(stage_ptr, nullptr);
}
 
TEST(EvaluationStage, StageEnableRequired) {
  // Construct
  EvaluationStageConfig config;
  config.set_name(kIdentityStageName);
  config.mutable_specification()->set_process_class(IDENTITY);
  config.add_initializers(kInitializerMapping);
  config.add_inputs(kInputMapping);
  config.add_outputs(kOutputMapping);
  config.clear_inputs();
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  EXPECT_EQ(stage_ptr, nullptr);
  IdentityStage_ENABLE();
  stage_ptr = EvaluationStage::Create(config);
  EXPECT_NE(stage_ptr, nullptr);
}
 
TEST(EvaluationStage, IncompleteConfig) {
  // Construct
  EvaluationStageConfig config = GetIdentityStageConfig();
  config.clear_inputs();
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  // Initialize
  absl::flat_hash_map<std::string, void*> object_map;
  float default_value = 1.0;
  object_map[kDefaultValueName] = &default_value;
  EXPECT_FALSE(stage_ptr->Init(object_map));
}
 
TEST(EvaluationStage, IncorrectlyFormattedConfig) {
  // Construct
  EvaluationStageConfig config = GetIdentityStageConfig();
  config.clear_initializers();
  config.add_initializers("DEFAULT_VALUE-default");
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  // Initialize
  absl::flat_hash_map<std::string, void*> object_map;
  float default_value = 1.0;
  object_map[kDefaultValueName] = &default_value;
  EXPECT_FALSE(stage_ptr->Init(object_map));
}
 
TEST(EvaluationStage, ConstructFromConfig_UnknownProcess) {
  // Construct
  EvaluationStageConfig config = GetIdentityStageConfig();
  config.mutable_specification()->clear_process_class();
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  EXPECT_EQ(stage_ptr.get(), nullptr);
}
 
TEST(EvaluationStage, NoInitializer) {
  // Construct
  EvaluationStageConfig config = GetIdentityStageConfig();
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  // Initialize
  absl::flat_hash_map<std::string, void*> object_map;
  EXPECT_FALSE(stage_ptr->Init(object_map));
}
 
TEST(EvaluationStage, InvalidInitializer) {
  // Construct
  EvaluationStageConfig config = GetIdentityStageConfig();
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  // Initialize
  absl::flat_hash_map<std::string, void*> object_map;
  object_map[kDefaultValueName] = nullptr;
  EXPECT_FALSE(stage_ptr->Init(object_map));
}
 
TEST(EvaluationStage, NoInputs) {
  // Construct
  EvaluationStageConfig config = GetIdentityStageConfig();
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  // Initialize
  absl::flat_hash_map<std::string, void*> object_map;
  float default_value = 1.0;
  object_map[kDefaultValueName] = &default_value;
  EXPECT_TRUE(stage_ptr->Init(object_map));
 
  // Run
  EXPECT_FALSE(stage_ptr->Run(object_map));
}
 
TEST(EvaluationStage, ExpectedIdentityOutput) {
  // Construct
  EvaluationStageConfig config = GetIdentityStageConfig();
  std::unique_ptr<EvaluationStage> stage_ptr = EvaluationStage::Create(config);
  // Initialize
  absl::flat_hash_map<std::string, void*> object_map;
  float default_value = 1.0;
  object_map[kDefaultValueName] = &default_value;
  EXPECT_TRUE(stage_ptr->Init(object_map));
 
  // Input Data
  float input_value = 2.0f;
  // Run
  object_map[kInputValueName] = &input_value;
  EXPECT_TRUE(stage_ptr->Run(object_map));
  EvaluationStageMetrics metrics = stage_ptr->LatestMetrics();
  // Check output
  float* output_value_ptr = static_cast<float*>(object_map[kOutputValueName]);
  EXPECT_NE(output_value_ptr, nullptr);
  EXPECT_FLOAT_EQ(*output_value_ptr, input_value);
  EXPECT_EQ(metrics.num_runs(), 1);
 
  // Input Data
  input_value = 0.0f;
  // Run
  object_map[kInputValueName] = &input_value;
  EXPECT_TRUE(stage_ptr->Run(object_map));
  metrics = stage_ptr->LatestMetrics();
  // Check output
  output_value_ptr = static_cast<float*>(object_map[kOutputValueName]);
  EXPECT_NE(output_value_ptr, nullptr);
  EXPECT_FLOAT_EQ(*output_value_ptr, default_value);
  EXPECT_EQ(metrics.num_runs(), 2);
}
 
}  // namespace
}  // namespace evaluation
}  // namespace tflite