• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "LimitedSupportDevice.h"
18 
19 #include <android-base/logging.h>
20 #include <nnapi/IBuffer.h>
21 #include <nnapi/IDevice.h>
22 #include <nnapi/IPreparedModel.h>
23 #include <nnapi/OperandTypes.h>
24 #include <nnapi/Result.h>
25 #include <nnapi/Types.h>
26 #include <nnapi/Validation.h>
27 
28 #include <algorithm>
29 #include <any>
30 #include <chrono>
31 #include <functional>
32 #include <iterator>
33 #include <memory>
34 #include <optional>
35 #include <set>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 
40 namespace android::nn::sample {
41 
LimitedSupportDevice(SharedDevice device,Capabilities capabilities,SupportedOperationsFunction supportedOperationsFunction)42 LimitedSupportDevice::LimitedSupportDevice(SharedDevice device, Capabilities capabilities,
43                                            SupportedOperationsFunction supportedOperationsFunction)
44     : kDevice(std::move(device)),
45       kCapabilities(std::move(capabilities)),
46       kSupportedOperationsFunction(std::move(supportedOperationsFunction)) {
47     CHECK(kDevice != nullptr);
48     CHECK(kSupportedOperationsFunction != nullptr);
49     const auto result = validate(kCapabilities);
50     CHECK(result.has_value()) << result.error();
51 }
52 
getName() const53 const std::string& LimitedSupportDevice::getName() const {
54     return kDevice->getName();
55 }
56 
getVersionString() const57 const std::string& LimitedSupportDevice::getVersionString() const {
58     return kDevice->getVersionString();
59 }
60 
getFeatureLevel() const61 Version LimitedSupportDevice::getFeatureLevel() const {
62     return kDevice->getFeatureLevel();
63 }
64 
getType() const65 DeviceType LimitedSupportDevice::getType() const {
66     return kDevice->getType();
67 }
68 
getSupportedExtensions() const69 const std::vector<Extension>& LimitedSupportDevice::getSupportedExtensions() const {
70     return kDevice->getSupportedExtensions();
71 }
72 
getCapabilities() const73 const Capabilities& LimitedSupportDevice::getCapabilities() const {
74     return kCapabilities;
75 }
76 
getNumberOfCacheFilesNeeded() const77 std::pair<uint32_t, uint32_t> LimitedSupportDevice::getNumberOfCacheFilesNeeded() const {
78     return kDevice->getNumberOfCacheFilesNeeded();
79 }
80 
wait() const81 GeneralResult<void> LimitedSupportDevice::wait() const {
82     return kDevice->wait();
83 }
84 
getSupportedOperations(const Model & model) const85 GeneralResult<std::vector<bool>> LimitedSupportDevice::getSupportedOperations(
86         const Model& model) const {
87     return kSupportedOperationsFunction(model);
88 }
89 
prepareModel(const Model & model,ExecutionPreference preference,Priority priority,OptionalTimePoint deadline,const std::vector<SharedHandle> & modelCache,const std::vector<SharedHandle> & dataCache,const CacheToken & token) const90 GeneralResult<SharedPreparedModel> LimitedSupportDevice::prepareModel(
91         const Model& model, ExecutionPreference preference, Priority priority,
92         OptionalTimePoint deadline, const std::vector<SharedHandle>& modelCache,
93         const std::vector<SharedHandle>& dataCache, const CacheToken& token) const {
94     const auto supportedOperations = NN_TRY(kSupportedOperationsFunction(model));
95     constexpr auto id = [](auto v) { return v; };
96     if (!std::all_of(supportedOperations.begin(), supportedOperations.end(), id)) {
97         return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) << "Not all operations are supported";
98     }
99     return kDevice->prepareModel(model, preference, priority, deadline, modelCache, dataCache,
100                                  token);
101 }
102 
prepareModelFromCache(OptionalTimePoint deadline,const std::vector<SharedHandle> & modelCache,const std::vector<SharedHandle> & dataCache,const CacheToken & token) const103 GeneralResult<SharedPreparedModel> LimitedSupportDevice::prepareModelFromCache(
104         OptionalTimePoint deadline, const std::vector<SharedHandle>& modelCache,
105         const std::vector<SharedHandle>& dataCache, const CacheToken& token) const {
106     return kDevice->prepareModelFromCache(deadline, modelCache, dataCache, token);
107 }
108 
allocate(const BufferDesc & desc,const std::vector<SharedPreparedModel> & preparedModels,const std::vector<BufferRole> & inputRoles,const std::vector<BufferRole> & outputRoles) const109 GeneralResult<SharedBuffer> LimitedSupportDevice::allocate(
110         const BufferDesc& desc, const std::vector<SharedPreparedModel>& preparedModels,
111         const std::vector<BufferRole>& inputRoles,
112         const std::vector<BufferRole>& outputRoles) const {
113     return kDevice->allocate(desc, preparedModels, inputRoles, outputRoles);
114 }
115 
116 }  // namespace android::nn::sample
117