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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/assist_ranker/classifier_predictor.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/field_trial_params.h"
#include "base/test/scoped_feature_list.h"
#include "components/assist_ranker/example_preprocessing.h"
#include "components/assist_ranker/fake_ranker_model_loader.h"
#include "components/assist_ranker/nn_classifier_test_util.h"
#include "components/assist_ranker/proto/ranker_model.pb.h"
#include "components/assist_ranker/ranker_model.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace assist_ranker {
using ::assist_ranker::testing::FakeRankerModelLoader;
using ::testing::FloatEq;
// Preprocessor feature names.
const char kFeatureName0[] = "feature_0";
const char kFeatureName1[] = "feature_1";
const char kFeatureExtra[] = "feature_extra";
class ClassifierPredictorTest : public ::testing::Test {
public:
void SetUp() override;
std::unique_ptr<ClassifierPredictor> InitPredictor(
std::unique_ptr<RankerModel> ranker_model,
const PredictorConfig& config);
PredictorConfig GetConfig();
protected:
base::test::ScopedFeatureList scoped_feature_list_;
};
void ClassifierPredictorTest::SetUp() {
::testing::Test::SetUp();
scoped_feature_list_.Init();
}
std::unique_ptr<ClassifierPredictor> ClassifierPredictorTest::InitPredictor(
std::unique_ptr<RankerModel> ranker_model,
const PredictorConfig& config) {
std::unique_ptr<ClassifierPredictor> predictor(
new ClassifierPredictor(config));
auto fake_model_loader = std::make_unique<FakeRankerModelLoader>(
base::BindRepeating(&ClassifierPredictor::ValidateModel),
base::BindRepeating(&ClassifierPredictor::OnModelAvailable,
base::Unretained(predictor.get())),
std::move(ranker_model));
predictor->LoadModel(std::move(fake_model_loader));
return predictor;
}
BASE_FEATURE(kTestRankerQuery,
"TestRankerQuery",
base::FEATURE_ENABLED_BY_DEFAULT);
const base::FeatureParam<std::string> kTestRankerUrl{
&kTestRankerQuery, "url-param-name", "https://default.model.url"};
PredictorConfig ClassifierPredictorTest::GetConfig() {
return PredictorConfig("model_name", "logging_name", "uma_prefix", LOG_NONE,
GetEmptyAllowlist(), &kTestRankerQuery,
&kTestRankerUrl, 0);
}
TEST_F(ClassifierPredictorTest, EmptyRankerModel) {
auto ranker_model = std::make_unique<RankerModel>();
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_FALSE(predictor->IsReady());
RankerExample ranker_example;
auto& features = *ranker_example.mutable_features();
features[kFeatureName0].set_bool_value(true);
std::vector<float> response;
EXPECT_FALSE(predictor->Predict(ranker_example, &response));
}
TEST_F(ClassifierPredictorTest, NoInferenceModuleForModel) {
auto ranker_model = std::make_unique<RankerModel>();
// TranslateRankerModel does not have an inference module. Validation will
// fail.
ranker_model->mutable_proto()
->mutable_translate()
->mutable_translate_logistic_regression_model()
->set_bias(1);
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_FALSE(predictor->IsReady());
RankerExample ranker_example;
auto& features = *ranker_example.mutable_features();
features[kFeatureName0].set_bool_value(true);
std::vector<float> response;
EXPECT_FALSE(predictor->Predict(ranker_example, &response));
EXPECT_FALSE(predictor->Predict({{0, 0}}, &response));
}
TEST_F(ClassifierPredictorTest, PredictFeatureVector) {
auto ranker_model = std::make_unique<RankerModel>();
*ranker_model->mutable_proto()->mutable_nn_classifier() =
nn_classifier::CreateXorClassifierModel();
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_TRUE(predictor->IsReady());
std::vector<float> response;
// True responses.
EXPECT_TRUE(predictor->Predict({{0.0, 1}}, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(2.8271765));
EXPECT_TRUE(predictor->Predict({{1, 0}}, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(2.6790769));
// False responses.
EXPECT_TRUE(predictor->Predict({{0, 0}}, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(-2.7154054));
EXPECT_TRUE(predictor->Predict({{1, 1}}, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(-3.1652793));
}
TEST_F(ClassifierPredictorTest, PredictRankerExampleNoPreprocessor) {
auto ranker_model = std::make_unique<RankerModel>();
*ranker_model->mutable_proto()->mutable_nn_classifier() =
nn_classifier::CreateXorClassifierModel();
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_TRUE(predictor->IsReady());
// Prediction of RankerExample without preprocessor config should fail.
std::vector<float> response;
RankerExample example;
EXPECT_FALSE(predictor->Predict(RankerExample(), &response));
}
TEST_F(ClassifierPredictorTest, PredictRankerExampleWithPreprocessor) {
auto ranker_model = std::make_unique<RankerModel>();
auto& model = *ranker_model->mutable_proto()->mutable_nn_classifier();
model = nn_classifier::CreateXorClassifierModel();
// Set up the preprocessor config with two features at feature vector
// indices 0 and 1.
auto& indices =
*model.mutable_preprocessor_config()->mutable_feature_indices();
indices[kFeatureName0] = 0;
indices[kFeatureName1] = 1;
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_TRUE(predictor->IsReady());
// Prediction of RankerExample with preprocessor config should work.
RankerExample example;
auto& feature_map = *example.mutable_features();
std::vector<float> response;
// True responses.
feature_map[kFeatureName0].set_float_value(0);
feature_map[kFeatureName1].set_float_value(1);
EXPECT_TRUE(predictor->Predict(example, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(2.8271765));
feature_map[kFeatureName0].set_float_value(1);
feature_map[kFeatureName1].set_float_value(0);
EXPECT_TRUE(predictor->Predict(example, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(2.6790769));
// False responses.
feature_map[kFeatureName0].set_float_value(0);
feature_map[kFeatureName1].set_float_value(0);
EXPECT_TRUE(predictor->Predict(example, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(-2.7154054));
feature_map[kFeatureName0].set_float_value(1);
feature_map[kFeatureName1].set_float_value(1);
EXPECT_TRUE(predictor->Predict(example, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(-3.1652793));
// Check that extra features do not cause an error.
feature_map[kFeatureName0].set_float_value(0);
feature_map[kFeatureName1].set_float_value(1);
feature_map[kFeatureExtra].set_float_value(1);
EXPECT_TRUE(predictor->Predict(example, &response));
EXPECT_EQ(response.size(), 1u);
EXPECT_THAT(response[0], FloatEq(2.8271765));
}
TEST_F(ClassifierPredictorTest, PredictRankerExamplePreprocessorError) {
auto ranker_model = std::make_unique<RankerModel>();
auto& model = *ranker_model->mutable_proto()->mutable_nn_classifier();
model = nn_classifier::CreateXorClassifierModel();
// Set up the preprocessor config with two features at feature vector
// indices 0 and 1.
auto& config = *model.mutable_preprocessor_config();
auto& indices = *config.mutable_feature_indices();
indices[kFeatureName0] = 0;
indices[kFeatureName1] = 1;
// Zero normalizer will generate a preprocessing error.
(*config.mutable_normalizers())[kFeatureName0] = 0;
auto predictor = InitPredictor(std::move(ranker_model), GetConfig());
EXPECT_TRUE(predictor->IsReady());
// Prediction of RankerExample should fail due to preprocessing error.
RankerExample example;
auto& feature_map = *example.mutable_features();
std::vector<float> response;
feature_map[kFeatureName0].set_float_value(0);
feature_map[kFeatureName1].set_float_value(1);
EXPECT_FALSE(predictor->Predict(example, &response));
}
} // namespace assist_ranker
|