summaryrefslogtreecommitdiff
path: root/adservices/service-core/java/com/android/adservices/service/topics/classifier/OnDeviceClassifier.java
blob: 70c2a135a9ea3127a2103ee681db2a4dcf2112ef (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * 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.
 */

package com.android.adservices.service.topics.classifier;

import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_EPOCH_COMPUTATION_CLASSIFIER_REPORTED__CLASSIFIER_TYPE__ON_DEVICE_CLASSIFIER;
import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_EPOCH_COMPUTATION_CLASSIFIER_REPORTED__ON_DEVICE_CLASSIFIER_STATUS__ON_DEVICE_CLASSIFIER_STATUS_FAILURE;
import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_EPOCH_COMPUTATION_CLASSIFIER_REPORTED__ON_DEVICE_CLASSIFIER_STATUS__ON_DEVICE_CLASSIFIER_STATUS_SUCCESS;
import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_EPOCH_COMPUTATION_CLASSIFIER_REPORTED__PRECOMPUTED_CLASSIFIER_STATUS__PRECOMPUTED_CLASSIFIER_STATUS_NOT_INVOKED;
import static com.android.adservices.service.topics.classifier.Preprocessor.limitDescriptionSize;

import android.annotation.NonNull;

import com.android.adservices.LogUtil;
import com.android.adservices.data.topics.Topic;
import com.android.adservices.service.FlagsFactory;
import com.android.adservices.service.stats.AdServicesLogger;
import com.android.adservices.service.stats.EpochComputationClassifierStats;
import com.android.adservices.service.topics.AppInfo;
import com.android.adservices.service.topics.PackageManagerUtil;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;

import org.tensorflow.lite.support.label.Category;
import org.tensorflow.lite.task.text.nlclassifier.BertNLClassifier;

import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * This Classifier classifies app into list of Topics using the on-device classification ML Model.
 */
public class OnDeviceClassifier implements Classifier {

    private static OnDeviceClassifier sSingleton;

    private static final String EMPTY = "";
    private static final AppInfo EMPTY_APP_INFO = new AppInfo(EMPTY, EMPTY);

    private static final String MODEL_ASSET_FIELD = "tflite_model";
    private static final String LABELS_ASSET_FIELD = "labels_topics";
    private static final String ASSET_VERSION_FIELD = "asset_version";
    private static final String VERSION_INFO_FIELD = "version_info";
    private static final String BUILD_ID_FIELD = "build_id";

    private static final String NO_VERSION_INFO = "NO_VERSION_INFO";

    private final Preprocessor mPreprocessor;
    private final PackageManagerUtil mPackageManagerUtil;
    private final Random mRandom;
    private final ModelManager mModelManager;

    private BertNLClassifier mBertNLClassifier;
    private ImmutableList<Integer> mLabels;
    private long mModelVersion;
    private long mLabelsVersion;
    private int mBuildId;
    private boolean mLoaded;
    private ImmutableMap<String, AppInfo> mAppInfoMap;
    private final AdServicesLogger mLogger;

    OnDeviceClassifier(
            @NonNull Preprocessor preprocessor,
            @NonNull PackageManagerUtil packageManagerUtil1,
            @NonNull Random random,
            @NonNull ModelManager modelManager,
            @NonNull AdServicesLogger logger) {
        mPreprocessor = preprocessor;
        mPackageManagerUtil = packageManagerUtil1;
        mRandom = random;
        mLoaded = false;
        mAppInfoMap = ImmutableMap.of();
        mModelManager = modelManager;
        mLogger = logger;
    }

    @Override
    @NonNull
    public ImmutableMap<String, List<Topic>> classify(@NonNull Set<String> appPackageNames) {
        if (appPackageNames.isEmpty()) {
            return ImmutableMap.of();
        }

        if (!mModelManager.isModelAvailable()) {
            // Return empty map since no model is available.
            LogUtil.d("[ML] No ML model available for classification. Return empty Map.");
            return ImmutableMap.of();
        }

        // Load the assets if not loaded already.
        if (!isLoaded()) {
            mLoaded = load();
        }

        // Load test app info for every call.
        mAppInfoMap = mPackageManagerUtil.getAppInformation(appPackageNames);
        if (mAppInfoMap.isEmpty()) {
            LogUtil.w("Loaded app description map is empty.");
        }

        ImmutableMap.Builder<String, List<Topic>> packageNameToTopics = ImmutableMap.builder();
        for (String appPackageName : appPackageNames) {
            String appDescription = getProcessedAppDescription(appPackageName);
            List<Topic> appClassificationTopics = getAppClassificationTopics(appDescription);
            logEpochComputationClassifierStats(appClassificationTopics);
            LogUtil.v(
                    "[ML] Top classification for app description \""
                            + appDescription
                            + "\" is "
                            + Iterables.getFirst(appClassificationTopics, /*default value*/ -1));
            packageNameToTopics.put(appPackageName, appClassificationTopics);
        }

        return packageNameToTopics.build();
    }

    private void logEpochComputationClassifierStats(List<Topic> topics) {
        // Log atom for getTopTopics call.
        ImmutableList.Builder<Integer> topicIds = ImmutableList.builder();
        for (Topic topic : topics) {
            topicIds.add(topic.getTopic());
        }
        mLogger.logEpochComputationClassifierStats(
                EpochComputationClassifierStats.builder()
                        .setTopicIds(topicIds.build())
                        .setBuildId(mBuildId)
                        .setAssetVersion(Long.toString(mModelVersion))
                        .setClassifierType(
                                AD_SERVICES_EPOCH_COMPUTATION_CLASSIFIER_REPORTED__CLASSIFIER_TYPE__ON_DEVICE_CLASSIFIER)
                        .setOnDeviceClassifierStatus(
                                topics.isEmpty()
                                        ? AD_SERVICES_EPOCH_COMPUTATION_CLASSIFIER_REPORTED__ON_DEVICE_CLASSIFIER_STATUS__ON_DEVICE_CLASSIFIER_STATUS_FAILURE
                                        : AD_SERVICES_EPOCH_COMPUTATION_CLASSIFIER_REPORTED__ON_DEVICE_CLASSIFIER_STATUS__ON_DEVICE_CLASSIFIER_STATUS_SUCCESS)
                        .setPrecomputedClassifierStatus(
                                AD_SERVICES_EPOCH_COMPUTATION_CLASSIFIER_REPORTED__PRECOMPUTED_CLASSIFIER_STATUS__PRECOMPUTED_CLASSIFIER_STATUS_NOT_INVOKED)
                        .build());
    }

    @Override
    @NonNull
    public List<Topic> getTopTopics(
            Map<String, List<Topic>> appTopics, int numberOfTopTopics, int numberOfRandomTopics) {
        // Load assets if necessary.
        if (!isLoaded()) {
            load();
        }

        return CommonClassifierHelper.getTopTopics(
                appTopics, mLabels, mRandom, numberOfTopTopics, numberOfRandomTopics, mLogger);
    }

    // Uses the BertNLClassifier to fetch the most relevant topic id based on the input app
    // description.
    private List<Topic> getAppClassificationTopics(@NonNull String appDescription) {
        // Returns list of labelIds with their corresponding score in Category for the app
        // description.
        List<Category> classifications = ImmutableList.of();
        try {
            classifications = mBertNLClassifier.classify(appDescription);
        } catch (Exception e) {
            // (TODO:b/242926783): Update to more granular Exception after resolving JNI error
            // propagation.
            LogUtil.e("[ML] classify call failed for mBertNLClassifier.");
            return ImmutableList.of();
        }
        // Get the highest score first. Sort in decreasing order.
        classifications.sort(Comparator.comparing(Category::getScore).reversed());

        // Limit the number of entries to first MAX_LABELS_PER_APP.
        // TODO(b/235435229): Evaluate the strategy to use first x elements.
        int numberOfTopLabels = FlagsFactory.getFlags().getClassifierNumberOfTopLabels();
        float classifierThresholdValue = FlagsFactory.getFlags().getClassifierThreshold();
        LogUtil.i(
                "numberOfTopLabels = %s\n classifierThresholdValue = %s",
                numberOfTopLabels, classifierThresholdValue);
        return classifications.stream()
                .sorted((c1, c2) -> Float.compare(c2.getScore(), c1.getScore())) // Reverse sorted.
                .filter(category -> isAboveThreshold(category, classifierThresholdValue))
                .map(OnDeviceClassifier::convertCategoryLabelToTopicId)
                .map(this::createTopic)
                .limit(numberOfTopLabels)
                .collect(Collectors.toList());
    }

    // Filter category above the required threshold.
    private static boolean isAboveThreshold(Category category, float classifierThresholdValue) {
        return category.getScore() >= classifierThresholdValue;
    }

    // Converts Category Label to TopicId. Expects label to be labelId of the classified category.
    // Returns -1 if conversion to int fails for the label.
    private static int convertCategoryLabelToTopicId(Category category) {
        try {
            // Category label is expected to be the topicId of the predicted topic.
            return Integer.parseInt(category.getLabel());
        } catch (NumberFormatException numberFormatException) {
            LogUtil.e(
                    numberFormatException,
                    "ML model did not return a topic id. Label returned is %s",
                    category.getLabel());
            return -1;
        }
    }

    // Fetch app description for the package and preprocess it for the ML model.
    private String getProcessedAppDescription(@NonNull String appPackageName) {
        // Fetch app description from the loaded map.
        AppInfo appInfo = mAppInfoMap.getOrDefault(appPackageName, EMPTY_APP_INFO);
        String appDescription = appInfo.getAppDescription();

        // Preprocess the app description for the model.
        appDescription = mPreprocessor.preprocessAppDescription(appDescription);
        appDescription = mPreprocessor.removeStopWords(appDescription);
        // Limit description size.
        int maxNumberOfWords = FlagsFactory.getFlags().getClassifierDescriptionMaxWords();
        int maxNumberOfCharacters = FlagsFactory.getFlags().getClassifierDescriptionMaxLength();
        appDescription =
                limitDescriptionSize(appDescription, maxNumberOfWords, maxNumberOfCharacters);

        return appDescription;
    }

    long getModelVersion() {
        // Load assets if not loaded already.
        if (!isLoaded()) {
            load();
        }

        return mModelVersion;
    }

    long getBertModelVersion() {
        // Load assets if necessary.
        if (!isLoaded()) {
            load();
        }

        String modelVersion = mBertNLClassifier.getModelVersion();
        if (modelVersion.equals(NO_VERSION_INFO)) {
            return 0;
        }

        return Long.parseLong(modelVersion);
    }

    long getLabelsVersion() {
        // Load assets if not loaded already.
        if (!isLoaded()) {
            load();
        }

        return mLabelsVersion;
    }

    long getBertLabelsVersion() {
        // Load assets if necessary.
        if (!isLoaded()) {
            load();
        }

        String labelsVersion = mBertNLClassifier.getLabelsVersion();
        if (labelsVersion.equals(NO_VERSION_INFO)) {
            return 0;
        }

        return Long.parseLong(labelsVersion);
    }

    private Topic createTopic(int topicId) {
        return Topic.create(topicId, mLabelsVersion, mModelVersion);
    }

    // Indicates whether assets are loaded.
    private boolean isLoaded() {
        return mLoaded;
    }

    // Loads classifier model evaluation assets needed for classification.
    // Return true on successful loading of all assets. Return false if any loading fails.
    private boolean load() {

        // Load Bert model.
        try {
            mBertNLClassifier = loadModel();
        } catch (Exception e) {
            LogUtil.e(e, "Loading ML model failed.");
            return false;
        }

        // Load labels.
        mLabels = mModelManager.retrieveLabels();

        // Load classifier assets metadata.
        ImmutableMap<String, ImmutableMap<String, String>> classifierAssetsMetadata =
                mModelManager.retrieveClassifierAssetsMetadata();
        mModelVersion =
                Long.parseLong(
                        classifierAssetsMetadata.get(MODEL_ASSET_FIELD).get(ASSET_VERSION_FIELD));
        mLabelsVersion =
                Long.parseLong(
                        classifierAssetsMetadata.get(LABELS_ASSET_FIELD).get(ASSET_VERSION_FIELD));
        try {
            mBuildId =
                    Integer.parseInt(
                            classifierAssetsMetadata.get(VERSION_INFO_FIELD).get(BUILD_ID_FIELD));
        } catch (NumberFormatException e) {
            // No build id is available.
            LogUtil.d(e, "Build id is not available");
            mBuildId = -1;
        }
        return true;
    }

    // Load BertNLClassifier Model from the corresponding modelFilePath.
    private BertNLClassifier loadModel() throws IOException {
        return BertNLClassifier.createFromBuffer(mModelManager.retrieveModel());
    }
}