1 /* 2 * Copyright (C) 2020 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 #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_RESILIENT_BURST_H 18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_RESILIENT_BURST_H 19 20 #include <android-base/thread_annotations.h> 21 #include <nnapi/IBurst.h> 22 #include <nnapi/Result.h> 23 #include <nnapi/Types.h> 24 25 #include <functional> 26 #include <memory> 27 #include <mutex> 28 #include <optional> 29 #include <utility> 30 31 namespace android::hardware::neuralnetworks::utils { 32 33 class ResilientBurst final : public nn::IBurst, 34 public std::enable_shared_from_this<ResilientBurst> { 35 struct PrivateConstructorTag {}; 36 37 public: 38 using Factory = std::function<nn::GeneralResult<nn::SharedBurst>()>; 39 40 static nn::GeneralResult<std::shared_ptr<const ResilientBurst>> create(Factory makeBurst); 41 42 ResilientBurst(PrivateConstructorTag tag, Factory makeBurst, nn::SharedBurst burst); 43 44 nn::SharedBurst getBurst() const; 45 nn::GeneralResult<nn::SharedBurst> recover(const nn::IBurst* failingBurst) const; 46 47 OptionalCacheHold cacheMemory(const nn::SharedMemory& memory) const override; 48 49 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> execute( 50 const nn::Request& request, nn::MeasureTiming measure, 51 const nn::OptionalTimePoint& deadline, const nn::OptionalDuration& loopTimeoutDuration, 52 const std::vector<nn::TokenValuePair>& hints, 53 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const override; 54 55 nn::GeneralResult<nn::SharedExecution> createReusableExecution( 56 const nn::Request& request, nn::MeasureTiming measure, 57 const nn::OptionalDuration& loopTimeoutDuration, 58 const std::vector<nn::TokenValuePair>& hints, 59 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const override; 60 61 private: 62 bool isValidInternal() const EXCLUDES(mMutex); 63 nn::GeneralResult<nn::SharedExecution> createReusableExecutionInternal( 64 const nn::Request& request, nn::MeasureTiming measure, 65 const nn::OptionalDuration& loopTimeoutDuration, 66 const std::vector<nn::TokenValuePair>& hints, 67 const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const; 68 69 const Factory kMakeBurst; 70 mutable std::mutex mMutex; 71 mutable nn::SharedBurst mBurst GUARDED_BY(mMutex); 72 }; 73 74 } // namespace android::hardware::neuralnetworks::utils 75 76 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_RESILIENT_BURST_H 77