Xusong Wang | 52d2df8 | 2019-07-02 13:53:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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_FRAMEWORKS_ML_NN_RUNTIME_MODEL_ARGUMENT_INFO_H |
| 18 | #define ANDROID_FRAMEWORKS_ML_NN_RUNTIME_MODEL_ARGUMENT_INFO_H |
| 19 | |
David Gross | 936c802 | 2020-03-12 17:27:48 -0700 | [diff] [blame] | 20 | #include <utility> |
Xusong Wang | 52d2df8 | 2019-07-02 13:53:25 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
| 23 | #include "HalInterfaces.h" |
| 24 | #include "NeuralNetworks.h" |
David Gross | 936c802 | 2020-03-12 17:27:48 -0700 | [diff] [blame] | 25 | #include "Utils.h" |
Xusong Wang | 52d2df8 | 2019-07-02 13:53:25 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace nn { |
| 29 | |
| 30 | // TODO move length out of DataLocation |
David Gross | 936c802 | 2020-03-12 17:27:48 -0700 | [diff] [blame] | 31 | // |
| 32 | // NOTE: The primary usage pattern is that a ModelArgumentInfo instance |
| 33 | // is not modified once it is created (unless it is reassigned to). |
| 34 | // There are a small number of use cases where it NEEDS to be modified, |
| 35 | // and we have a limited number of methods that support this. |
| 36 | class ModelArgumentInfo { |
| 37 | public: |
| 38 | ModelArgumentInfo() {} |
| 39 | |
| 40 | static std::pair<int, ModelArgumentInfo> createFromPointer( |
| 41 | const hal::Operand& operand, const ANeuralNetworksOperandType* type, |
| 42 | void* data /* nullptr means HAS_NO_VALUE */, uint32_t length); |
| 43 | static std::pair<int, ModelArgumentInfo> createFromMemory( |
| 44 | const hal::Operand& operand, const ANeuralNetworksOperandType* type, uint32_t poolIndex, |
| 45 | uint32_t offset, uint32_t length); |
| 46 | |
| 47 | enum State { POINTER, MEMORY, HAS_NO_VALUE, UNSPECIFIED }; |
| 48 | |
| 49 | State state() const { return mState; } |
| 50 | |
| 51 | bool unspecified() const { return mState == UNSPECIFIED; } |
| 52 | |
| 53 | void* buffer() const { |
| 54 | CHECK_EQ(mState, POINTER); |
| 55 | return mBuffer; |
| 56 | } |
| 57 | |
| 58 | const std::vector<uint32_t>& dimensions() const { |
| 59 | CHECK(mState == POINTER || mState == MEMORY); |
| 60 | return mDimensions; |
| 61 | } |
| 62 | std::vector<uint32_t>& dimensions() { |
| 63 | CHECK(mState == POINTER || mState == MEMORY); |
| 64 | return mDimensions; |
| 65 | } |
| 66 | |
| 67 | bool isSufficient() const { |
| 68 | CHECK(mState == POINTER || mState == MEMORY); |
| 69 | return mIsSufficient; |
| 70 | } |
| 71 | bool& isSufficient() { |
| 72 | CHECK(mState == POINTER || mState == MEMORY); |
| 73 | return mIsSufficient; |
| 74 | } |
| 75 | |
| 76 | uint32_t length() const { |
| 77 | CHECK(mState == POINTER || mState == MEMORY); |
| 78 | return mLocationAndLength.length; |
| 79 | } |
| 80 | |
| 81 | const hal::DataLocation& locationAndLength() const { |
| 82 | CHECK_EQ(mState, MEMORY); |
| 83 | return mLocationAndLength; |
| 84 | } |
| 85 | hal::DataLocation& locationAndLength() { |
| 86 | CHECK_EQ(mState, MEMORY); |
| 87 | return mLocationAndLength; |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | int updateDimensionInfo(const hal::Operand& operand, const ANeuralNetworksOperandType* newType); |
| 92 | |
Xusong Wang | 52d2df8 | 2019-07-02 13:53:25 -0700 | [diff] [blame] | 93 | // Whether the argument was specified as being in a Memory, as a pointer, |
| 94 | // has no value, or has not been specified. |
| 95 | // If POINTER then: |
David Gross | 936c802 | 2020-03-12 17:27:48 -0700 | [diff] [blame] | 96 | // mLocationAndLength.length is valid. |
| 97 | // mDimensions is valid. |
| 98 | // mBuffer is valid. |
Xusong Wang | 52d2df8 | 2019-07-02 13:53:25 -0700 | [diff] [blame] | 99 | // If MEMORY then: |
David Gross | 936c802 | 2020-03-12 17:27:48 -0700 | [diff] [blame] | 100 | // mLocationAndLength.{poolIndex, offset, length} is valid. |
| 101 | // mDimensions is valid. |
| 102 | State mState = UNSPECIFIED; // fixed at creation |
| 103 | void* mBuffer = nullptr; // fixed at creation |
| 104 | hal::DataLocation mLocationAndLength; // can be updated after creation |
| 105 | std::vector<uint32_t> mDimensions; // can be updated after creation |
| 106 | bool mIsSufficient = true; // can be updated after creation |
Xusong Wang | 52d2df8 | 2019-07-02 13:53:25 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Michael Butler | 4753567 | 2019-11-08 17:06:54 -0800 | [diff] [blame] | 109 | // Convert ModelArgumentInfo to HIDL RequestArgument. For pointer arguments, use the location |
| 110 | // information in ptrArgsLocations. |
| 111 | hal::hidl_vec<hal::RequestArgument> createRequestArguments( |
| 112 | const std::vector<ModelArgumentInfo>& argumentInfos, |
| 113 | const std::vector<hal::DataLocation>& ptrArgsLocations); |
| 114 | |
Xusong Wang | 52d2df8 | 2019-07-02 13:53:25 -0700 | [diff] [blame] | 115 | } // namespace nn |
| 116 | } // namespace android |
| 117 | |
Slava Shklyaev | 4aad5a3 | 2019-10-29 13:27:13 +0000 | [diff] [blame] | 118 | #endif // ANDROID_FRAMEWORKS_ML_NN_RUNTIME_MODEL_ARGUMENT_INFO_H |