blob: 5984b4d3b994b0257210ddf3957d6163d160b68f [file] [log] [blame]
Philip Quinn8f953ab2022-12-06 15:37:07 -08001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "TfLiteMotionPredictor"
18#include <input/TfLiteMotionPredictor.h>
19
Philip Quinncb3229a2023-02-08 22:50:59 -080020#include <fcntl.h>
21#include <sys/mman.h>
22#include <unistd.h>
23
Philip Quinn8f953ab2022-12-06 15:37:07 -080024#include <algorithm>
25#include <cmath>
26#include <cstddef>
27#include <cstdint>
Philip Quinn8f953ab2022-12-06 15:37:07 -080028#include <memory>
29#include <span>
Philip Quinn8f953ab2022-12-06 15:37:07 -080030#include <type_traits>
31#include <utility>
32
Siarhei Vishniakoufd0a68e2023-02-28 13:25:36 -080033#include <android-base/file.h>
Philip Quinncb3229a2023-02-08 22:50:59 -080034#include <android-base/logging.h>
35#include <android-base/mapped_file.h>
Philip Quinn8f953ab2022-12-06 15:37:07 -080036#define ATRACE_TAG ATRACE_TAG_INPUT
37#include <cutils/trace.h>
38#include <log/log.h>
Philip Quinnf84fa492023-06-26 14:15:15 -070039#include <utils/Timers.h>
Philip Quinn8f953ab2022-12-06 15:37:07 -080040
41#include "tensorflow/lite/core/api/error_reporter.h"
Philip Quinnda6a4482023-02-07 10:09:57 -080042#include "tensorflow/lite/core/api/op_resolver.h"
Philip Quinn8f953ab2022-12-06 15:37:07 -080043#include "tensorflow/lite/interpreter.h"
Philip Quinnda6a4482023-02-07 10:09:57 -080044#include "tensorflow/lite/kernels/builtin_op_kernels.h"
Philip Quinn8f953ab2022-12-06 15:37:07 -080045#include "tensorflow/lite/model.h"
Philip Quinnda6a4482023-02-07 10:09:57 -080046#include "tensorflow/lite/mutable_op_resolver.h"
Philip Quinn8f953ab2022-12-06 15:37:07 -080047
Philip Quinnf84fa492023-06-26 14:15:15 -070048#include "tinyxml2.h"
49
Philip Quinn8f953ab2022-12-06 15:37:07 -080050namespace android {
51namespace {
52
53constexpr char SIGNATURE_KEY[] = "serving_default";
54
55// Input tensor names.
56constexpr char INPUT_R[] = "r";
57constexpr char INPUT_PHI[] = "phi";
58constexpr char INPUT_PRESSURE[] = "pressure";
59constexpr char INPUT_TILT[] = "tilt";
60constexpr char INPUT_ORIENTATION[] = "orientation";
61
62// Output tensor names.
63constexpr char OUTPUT_R[] = "r";
64constexpr char OUTPUT_PHI[] = "phi";
65constexpr char OUTPUT_PRESSURE[] = "pressure";
66
Siarhei Vishniakouc065d7b2023-03-02 14:06:29 -080067// Ideally, we would just use std::filesystem::exists here, but it requires libc++fs, which causes
68// build issues in other parts of the system.
69#if defined(__ANDROID__)
70bool fileExists(const char* filename) {
71 struct stat buffer;
72 return stat(filename, &buffer) == 0;
73}
74#endif
75
Siarhei Vishniakoufd0a68e2023-02-28 13:25:36 -080076std::string getModelPath() {
77#if defined(__ANDROID__)
Philip Quinnf84fa492023-06-26 14:15:15 -070078 static const char* oemModel = "/vendor/etc/motion_predictor_model.tflite";
Siarhei Vishniakouc065d7b2023-03-02 14:06:29 -080079 if (fileExists(oemModel)) {
80 return oemModel;
81 }
Philip Quinnf84fa492023-06-26 14:15:15 -070082 return "/system/etc/motion_predictor_model.tflite";
Siarhei Vishniakoufd0a68e2023-02-28 13:25:36 -080083#else
Philip Quinnf84fa492023-06-26 14:15:15 -070084 return base::GetExecutableDirectory() + "/motion_predictor_model.tflite";
Siarhei Vishniakoufd0a68e2023-02-28 13:25:36 -080085#endif
86}
87
Philip Quinnf84fa492023-06-26 14:15:15 -070088std::string getConfigPath() {
89 // The config file should be alongside the model file.
90 return base::Dirname(getModelPath()) + "/motion_predictor_config.xml";
91}
92
93int64_t parseXMLInt64(const tinyxml2::XMLElement& configRoot, const char* elementName) {
94 const tinyxml2::XMLElement* element = configRoot.FirstChildElement(elementName);
95 LOG_ALWAYS_FATAL_IF(!element, "Could not find '%s' element", elementName);
96
97 int64_t value = 0;
98 LOG_ALWAYS_FATAL_IF(element->QueryInt64Text(&value) != tinyxml2::XML_SUCCESS,
99 "Failed to parse %s: %s", elementName, element->GetText());
100 return value;
101}
102
Philip Quinn107ce702023-07-14 13:07:13 -0700103float parseXMLFloat(const tinyxml2::XMLElement& configRoot, const char* elementName) {
104 const tinyxml2::XMLElement* element = configRoot.FirstChildElement(elementName);
105 LOG_ALWAYS_FATAL_IF(!element, "Could not find '%s' element", elementName);
106
107 float value = 0;
108 LOG_ALWAYS_FATAL_IF(element->QueryFloatText(&value) != tinyxml2::XML_SUCCESS,
109 "Failed to parse %s: %s", elementName, element->GetText());
110 return value;
111}
112
Philip Quinn8f953ab2022-12-06 15:37:07 -0800113// A TFLite ErrorReporter that logs to logcat.
114class LoggingErrorReporter : public tflite::ErrorReporter {
115public:
116 int Report(const char* format, va_list args) override {
117 return LOG_PRI_VA(ANDROID_LOG_ERROR, LOG_TAG, format, args);
118 }
119};
120
121// Searches a runner for an input tensor.
122TfLiteTensor* findInputTensor(const char* name, tflite::SignatureRunner* runner) {
123 TfLiteTensor* tensor = runner->input_tensor(name);
124 LOG_ALWAYS_FATAL_IF(!tensor, "Failed to find input tensor '%s'", name);
125 return tensor;
126}
127
128// Searches a runner for an output tensor.
129const TfLiteTensor* findOutputTensor(const char* name, tflite::SignatureRunner* runner) {
130 const TfLiteTensor* tensor = runner->output_tensor(name);
131 LOG_ALWAYS_FATAL_IF(!tensor, "Failed to find output tensor '%s'", name);
132 return tensor;
133}
134
135// Returns the buffer for a tensor of type T.
136template <typename T>
137std::span<T> getTensorBuffer(typename std::conditional<std::is_const<T>::value, const TfLiteTensor*,
138 TfLiteTensor*>::type tensor) {
139 LOG_ALWAYS_FATAL_IF(!tensor);
140
141 const TfLiteType type = tflite::typeToTfLiteType<typename std::remove_cv<T>::type>();
142 LOG_ALWAYS_FATAL_IF(tensor->type != type, "Unexpected type for '%s' tensor: %s (expected %s)",
143 tensor->name, TfLiteTypeGetName(tensor->type), TfLiteTypeGetName(type));
144
145 LOG_ALWAYS_FATAL_IF(!tensor->data.data);
146 return {reinterpret_cast<T*>(tensor->data.data),
147 static_cast<typename std::span<T>::index_type>(tensor->bytes / sizeof(T))};
148}
149
150// Verifies that a tensor exists and has an underlying buffer of type T.
151template <typename T>
152void checkTensor(const TfLiteTensor* tensor) {
153 LOG_ALWAYS_FATAL_IF(!tensor);
154
155 const auto buffer = getTensorBuffer<const T>(tensor);
156 LOG_ALWAYS_FATAL_IF(buffer.empty(), "No buffer for tensor '%s'", tensor->name);
157}
158
Philip Quinnda6a4482023-02-07 10:09:57 -0800159std::unique_ptr<tflite::OpResolver> createOpResolver() {
160 auto resolver = std::make_unique<tflite::MutableOpResolver>();
161 resolver->AddBuiltin(::tflite::BuiltinOperator_CONCATENATION,
162 ::tflite::ops::builtin::Register_CONCATENATION());
163 resolver->AddBuiltin(::tflite::BuiltinOperator_FULLY_CONNECTED,
164 ::tflite::ops::builtin::Register_FULLY_CONNECTED());
Philip Quinn107ce702023-07-14 13:07:13 -0700165 resolver->AddBuiltin(::tflite::BuiltinOperator_GELU, ::tflite::ops::builtin::Register_GELU());
Philip Quinnda6a4482023-02-07 10:09:57 -0800166 return resolver;
167}
168
Philip Quinn8f953ab2022-12-06 15:37:07 -0800169} // namespace
170
Philip Quinn9b8926e2023-01-31 14:50:02 -0800171TfLiteMotionPredictorBuffers::TfLiteMotionPredictorBuffers(size_t inputLength)
172 : mInputR(inputLength, 0),
173 mInputPhi(inputLength, 0),
174 mInputPressure(inputLength, 0),
175 mInputTilt(inputLength, 0),
176 mInputOrientation(inputLength, 0) {
Philip Quinn8f953ab2022-12-06 15:37:07 -0800177 LOG_ALWAYS_FATAL_IF(inputLength == 0, "Buffer input size must be greater than 0");
Philip Quinn8f953ab2022-12-06 15:37:07 -0800178}
179
180void TfLiteMotionPredictorBuffers::reset() {
181 std::fill(mInputR.begin(), mInputR.end(), 0);
182 std::fill(mInputPhi.begin(), mInputPhi.end(), 0);
183 std::fill(mInputPressure.begin(), mInputPressure.end(), 0);
184 std::fill(mInputTilt.begin(), mInputTilt.end(), 0);
185 std::fill(mInputOrientation.begin(), mInputOrientation.end(), 0);
186 mAxisFrom.reset();
187 mAxisTo.reset();
188}
189
190void TfLiteMotionPredictorBuffers::copyTo(TfLiteMotionPredictorModel& model) const {
191 LOG_ALWAYS_FATAL_IF(mInputR.size() != model.inputLength(),
192 "Buffer length %zu doesn't match model input length %zu", mInputR.size(),
193 model.inputLength());
194 LOG_ALWAYS_FATAL_IF(!isReady(), "Buffers are incomplete");
195
196 std::copy(mInputR.begin(), mInputR.end(), model.inputR().begin());
197 std::copy(mInputPhi.begin(), mInputPhi.end(), model.inputPhi().begin());
198 std::copy(mInputPressure.begin(), mInputPressure.end(), model.inputPressure().begin());
199 std::copy(mInputTilt.begin(), mInputTilt.end(), model.inputTilt().begin());
200 std::copy(mInputOrientation.begin(), mInputOrientation.end(), model.inputOrientation().begin());
201}
202
203void TfLiteMotionPredictorBuffers::pushSample(int64_t timestamp,
204 const TfLiteMotionPredictorSample sample) {
205 // Convert the sample (x, y) into polar (r, φ) based on a reference axis
206 // from the preceding two points (mAxisFrom/mAxisTo).
207
208 mTimestamp = timestamp;
209
210 if (!mAxisTo) { // First point.
211 mAxisTo = sample;
212 return;
213 }
214
215 // Vector from the last point to the current sample point.
216 const TfLiteMotionPredictorSample::Point v = sample.position - mAxisTo->position;
217
218 const float r = std::hypot(v.x, v.y);
219 float phi = 0;
220 float orientation = 0;
221
Philip Quinn107ce702023-07-14 13:07:13 -0700222 if (!mAxisFrom && r > 0) { // Second point.
Philip Quinn8f953ab2022-12-06 15:37:07 -0800223 // We can only determine the distance from the first point, and not any
224 // angle. However, if the second point forms an axis, the orientation can
225 // be transformed relative to that axis.
226 const float axisPhi = std::atan2(v.y, v.x);
227 // A MotionEvent's orientation is measured clockwise from the vertical
228 // axis, but axisPhi is measured counter-clockwise from the horizontal
229 // axis.
230 orientation = M_PI_2 - sample.orientation - axisPhi;
231 } else {
232 const TfLiteMotionPredictorSample::Point axis = mAxisTo->position - mAxisFrom->position;
233 const float axisPhi = std::atan2(axis.y, axis.x);
234 phi = std::atan2(v.y, v.x) - axisPhi;
235
236 if (std::hypot(axis.x, axis.y) > 0) {
237 // See note above.
238 orientation = M_PI_2 - sample.orientation - axisPhi;
239 }
240 }
241
242 // Update the axis for the next point.
Philip Quinn107ce702023-07-14 13:07:13 -0700243 if (r > 0) {
244 mAxisFrom = mAxisTo;
245 mAxisTo = sample;
246 }
Philip Quinn8f953ab2022-12-06 15:37:07 -0800247
248 // Push the current sample onto the end of the input buffers.
Philip Quinn9b8926e2023-01-31 14:50:02 -0800249 mInputR.pushBack(r);
250 mInputPhi.pushBack(phi);
251 mInputPressure.pushBack(sample.pressure);
252 mInputTilt.pushBack(sample.tilt);
253 mInputOrientation.pushBack(orientation);
Philip Quinn8f953ab2022-12-06 15:37:07 -0800254}
255
Siarhei Vishniakoufd0a68e2023-02-28 13:25:36 -0800256std::unique_ptr<TfLiteMotionPredictorModel> TfLiteMotionPredictorModel::create() {
257 const std::string modelPath = getModelPath();
Siarhei Vishniakouc065d7b2023-03-02 14:06:29 -0800258 android::base::unique_fd fd(open(modelPath.c_str(), O_RDONLY));
Philip Quinncb3229a2023-02-08 22:50:59 -0800259 if (fd == -1) {
260 PLOG(FATAL) << "Could not read model from " << modelPath;
261 }
Philip Quinn8f953ab2022-12-06 15:37:07 -0800262
Philip Quinncb3229a2023-02-08 22:50:59 -0800263 const off_t fdSize = lseek(fd, 0, SEEK_END);
264 if (fdSize == -1) {
265 PLOG(FATAL) << "Failed to determine file size";
266 }
267
268 std::unique_ptr<android::base::MappedFile> modelBuffer =
269 android::base::MappedFile::FromFd(fd, /*offset=*/0, fdSize, PROT_READ);
270 if (!modelBuffer) {
271 PLOG(FATAL) << "Failed to mmap model";
272 }
Philip Quinn8f953ab2022-12-06 15:37:07 -0800273
Philip Quinnf84fa492023-06-26 14:15:15 -0700274 const std::string configPath = getConfigPath();
275 tinyxml2::XMLDocument configDocument;
276 LOG_ALWAYS_FATAL_IF(configDocument.LoadFile(configPath.c_str()) != tinyxml2::XML_SUCCESS,
277 "Failed to load config file from %s", configPath.c_str());
278
279 // Parse configuration file.
280 const tinyxml2::XMLElement* configRoot = configDocument.FirstChildElement("motion-predictor");
281 LOG_ALWAYS_FATAL_IF(!configRoot);
Philip Quinn107ce702023-07-14 13:07:13 -0700282 Config config{
283 .predictionInterval = parseXMLInt64(*configRoot, "prediction-interval"),
284 .distanceNoiseFloor = parseXMLFloat(*configRoot, "distance-noise-floor"),
285 };
Philip Quinnf84fa492023-06-26 14:15:15 -0700286
Philip Quinn8f953ab2022-12-06 15:37:07 -0800287 return std::unique_ptr<TfLiteMotionPredictorModel>(
Philip Quinn107ce702023-07-14 13:07:13 -0700288 new TfLiteMotionPredictorModel(std::move(modelBuffer), std::move(config)));
Philip Quinn8f953ab2022-12-06 15:37:07 -0800289}
290
Philip Quinncb3229a2023-02-08 22:50:59 -0800291TfLiteMotionPredictorModel::TfLiteMotionPredictorModel(
Philip Quinn107ce702023-07-14 13:07:13 -0700292 std::unique_ptr<android::base::MappedFile> model, Config config)
293 : mFlatBuffer(std::move(model)), mConfig(std::move(config)) {
Philip Quinncb3229a2023-02-08 22:50:59 -0800294 CHECK(mFlatBuffer);
Philip Quinn8f953ab2022-12-06 15:37:07 -0800295 mErrorReporter = std::make_unique<LoggingErrorReporter>();
Philip Quinncb3229a2023-02-08 22:50:59 -0800296 mModel = tflite::FlatBufferModel::VerifyAndBuildFromBuffer(mFlatBuffer->data(),
297 mFlatBuffer->size(),
Philip Quinn8f953ab2022-12-06 15:37:07 -0800298 /*extra_verifier=*/nullptr,
299 mErrorReporter.get());
300 LOG_ALWAYS_FATAL_IF(!mModel);
301
Philip Quinnda6a4482023-02-07 10:09:57 -0800302 auto resolver = createOpResolver();
303 tflite::InterpreterBuilder builder(*mModel, *resolver);
Philip Quinn8f953ab2022-12-06 15:37:07 -0800304
305 if (builder(&mInterpreter) != kTfLiteOk || !mInterpreter) {
306 LOG_ALWAYS_FATAL("Failed to build interpreter");
307 }
308
309 mRunner = mInterpreter->GetSignatureRunner(SIGNATURE_KEY);
310 LOG_ALWAYS_FATAL_IF(!mRunner, "Failed to find runner for signature '%s'", SIGNATURE_KEY);
311
312 allocateTensors();
313}
314
Philip Quinnda6a4482023-02-07 10:09:57 -0800315TfLiteMotionPredictorModel::~TfLiteMotionPredictorModel() {}
316
Philip Quinn8f953ab2022-12-06 15:37:07 -0800317void TfLiteMotionPredictorModel::allocateTensors() {
318 if (mRunner->AllocateTensors() != kTfLiteOk) {
319 LOG_ALWAYS_FATAL("Failed to allocate tensors");
320 }
321
322 attachInputTensors();
323 attachOutputTensors();
324
325 checkTensor<float>(mInputR);
326 checkTensor<float>(mInputPhi);
327 checkTensor<float>(mInputPressure);
328 checkTensor<float>(mInputTilt);
329 checkTensor<float>(mInputOrientation);
330 checkTensor<float>(mOutputR);
331 checkTensor<float>(mOutputPhi);
332 checkTensor<float>(mOutputPressure);
333
334 const auto checkInputTensorSize = [this](const TfLiteTensor* tensor) {
335 const size_t size = getTensorBuffer<const float>(tensor).size();
336 LOG_ALWAYS_FATAL_IF(size != inputLength(),
337 "Tensor '%s' length %zu does not match input length %zu", tensor->name,
338 size, inputLength());
339 };
340
341 checkInputTensorSize(mInputR);
342 checkInputTensorSize(mInputPhi);
343 checkInputTensorSize(mInputPressure);
344 checkInputTensorSize(mInputTilt);
345 checkInputTensorSize(mInputOrientation);
346}
347
348void TfLiteMotionPredictorModel::attachInputTensors() {
349 mInputR = findInputTensor(INPUT_R, mRunner);
350 mInputPhi = findInputTensor(INPUT_PHI, mRunner);
351 mInputPressure = findInputTensor(INPUT_PRESSURE, mRunner);
352 mInputTilt = findInputTensor(INPUT_TILT, mRunner);
353 mInputOrientation = findInputTensor(INPUT_ORIENTATION, mRunner);
354}
355
356void TfLiteMotionPredictorModel::attachOutputTensors() {
357 mOutputR = findOutputTensor(OUTPUT_R, mRunner);
358 mOutputPhi = findOutputTensor(OUTPUT_PHI, mRunner);
359 mOutputPressure = findOutputTensor(OUTPUT_PRESSURE, mRunner);
360}
361
362bool TfLiteMotionPredictorModel::invoke() {
363 ATRACE_BEGIN("TfLiteMotionPredictorModel::invoke");
364 TfLiteStatus result = mRunner->Invoke();
365 ATRACE_END();
366
367 if (result != kTfLiteOk) {
368 return false;
369 }
370
371 // Invoke() might reallocate tensors, so they need to be reattached.
372 attachInputTensors();
373 attachOutputTensors();
374
375 if (outputR().size() != outputPhi().size() || outputR().size() != outputPressure().size()) {
376 LOG_ALWAYS_FATAL("Output size mismatch: (r: %zu, phi: %zu, pressure: %zu)",
377 outputR().size(), outputPhi().size(), outputPressure().size());
378 }
379
380 return true;
381}
382
383size_t TfLiteMotionPredictorModel::inputLength() const {
384 return getTensorBuffer<const float>(mInputR).size();
385}
386
Cody Heinerdbd14eb2023-03-30 18:41:45 -0700387size_t TfLiteMotionPredictorModel::outputLength() const {
388 return getTensorBuffer<const float>(mOutputR).size();
389}
390
Philip Quinn8f953ab2022-12-06 15:37:07 -0800391std::span<float> TfLiteMotionPredictorModel::inputR() {
392 return getTensorBuffer<float>(mInputR);
393}
394
395std::span<float> TfLiteMotionPredictorModel::inputPhi() {
396 return getTensorBuffer<float>(mInputPhi);
397}
398
399std::span<float> TfLiteMotionPredictorModel::inputPressure() {
400 return getTensorBuffer<float>(mInputPressure);
401}
402
403std::span<float> TfLiteMotionPredictorModel::inputTilt() {
404 return getTensorBuffer<float>(mInputTilt);
405}
406
407std::span<float> TfLiteMotionPredictorModel::inputOrientation() {
408 return getTensorBuffer<float>(mInputOrientation);
409}
410
411std::span<const float> TfLiteMotionPredictorModel::outputR() const {
412 return getTensorBuffer<const float>(mOutputR);
413}
414
415std::span<const float> TfLiteMotionPredictorModel::outputPhi() const {
416 return getTensorBuffer<const float>(mOutputPhi);
417}
418
419std::span<const float> TfLiteMotionPredictorModel::outputPressure() const {
420 return getTensorBuffer<const float>(mOutputPressure);
421}
422
423} // namespace android