1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef OHOS_HDI_NNRT_V2_0_INNRTDEVICE_H
17#define OHOS_HDI_NNRT_V2_0_INNRTDEVICE_H
18
19#include <stdint.h>
20#include <string>
21#include <vector>
22#include <hdf_base.h>
23#include <hdi_base.h>
24#include "nnrt/v2_0/iprepared_model.h"
25#include "nnrt/v2_0/model_types.h"
26#include "nnrt/v2_0/nnrt_types.h"
27
28#ifndef HDI_BUFF_MAX_SIZE
29#define HDI_BUFF_MAX_SIZE (1024 * 200)
30#endif
31
32#ifndef HDI_CHECK_VALUE_RETURN
33#define HDI_CHECK_VALUE_RETURN(lv, compare, rv, ret) do { \
34    if ((lv) compare (rv)) { \
35        return ret; \
36    } \
37} while (false)
38#endif
39
40#ifndef HDI_CHECK_VALUE_RET_GOTO
41#define HDI_CHECK_VALUE_RET_GOTO(lv, compare, rv, ret, value, table) do { \
42    if ((lv) compare (rv)) { \
43        ret = value; \
44        goto table; \
45    } \
46} while (false)
47#endif
48
49namespace OHOS {
50namespace HDI {
51namespace Nnrt {
52namespace V2_0 {
53using namespace OHOS;
54using namespace OHOS::HDI;
55
56enum {
57    CMD_NNRT_DEVICE_GET_VERSION = 0,
58    CMD_NNRT_DEVICE_GET_DEVICE_NAME = 1,
59    CMD_NNRT_DEVICE_GET_VENDOR_NAME = 2,
60    CMD_NNRT_DEVICE_GET_DEVICE_TYPE = 3,
61    CMD_NNRT_DEVICE_GET_DEVICE_STATUS = 4,
62    CMD_NNRT_DEVICE_GET_SUPPORTED_OPERATION = 5,
63    CMD_NNRT_DEVICE_IS_FLOAT16_PRECISION_SUPPORTED = 6,
64    CMD_NNRT_DEVICE_IS_PERFORMANCE_MODE_SUPPORTED = 7,
65    CMD_NNRT_DEVICE_IS_PRIORITY_SUPPORTED = 8,
66    CMD_NNRT_DEVICE_IS_DYNAMIC_INPUT_SUPPORTED = 9,
67    CMD_NNRT_DEVICE_PREPARE_MODEL = 10,
68    CMD_NNRT_DEVICE_IS_MODEL_CACHE_SUPPORTED = 11,
69    CMD_NNRT_DEVICE_PREPARE_MODEL_FROM_MODEL_CACHE = 12,
70    CMD_NNRT_DEVICE_PREPARE_OFFLINE_MODEL = 13,
71    CMD_NNRT_DEVICE_ALLOCATE_BUFFER = 14,
72    CMD_NNRT_DEVICE_RELEASE_BUFFER = 15,
73};
74
75class INnrtDevice : public HdiBase {
76public:
77    DECLARE_HDI_DESCRIPTOR(u"ohos.hdi.nnrt.v2_0.INnrtDevice");
78
79    virtual ~INnrtDevice() = default;
80
81    static sptr<OHOS::HDI::Nnrt::V2_0::INnrtDevice> Get(bool isStub = false);
82    static sptr<OHOS::HDI::Nnrt::V2_0::INnrtDevice> Get(const std::string &serviceName, bool isStub = false);
83
84    virtual int32_t GetDeviceName(std::string& name) = 0;
85
86    virtual int32_t GetVendorName(std::string& name) = 0;
87
88    virtual int32_t GetDeviceType(OHOS::HDI::Nnrt::V2_0::DeviceType& deviceType) = 0;
89
90    virtual int32_t GetDeviceStatus(OHOS::HDI::Nnrt::V2_0::DeviceStatus& status) = 0;
91
92    virtual int32_t GetSupportedOperation(const OHOS::HDI::Nnrt::V2_0::Model& model, std::vector<bool>& ops) = 0;
93
94    virtual int32_t IsFloat16PrecisionSupported(bool& isSupported) = 0;
95
96    virtual int32_t IsPerformanceModeSupported(bool& isSupported) = 0;
97
98    virtual int32_t IsPrioritySupported(bool& isSupported) = 0;
99
100    virtual int32_t IsDynamicInputSupported(bool& isSupported) = 0;
101
102    virtual int32_t PrepareModel(const OHOS::HDI::Nnrt::V2_0::Model& model,
103         const OHOS::HDI::Nnrt::V2_0::ModelConfig& config, sptr<OHOS::HDI::Nnrt::V2_0::IPreparedModel>& preparedModel) = 0;
104
105    virtual int32_t IsModelCacheSupported(bool& isSupported) = 0;
106
107    virtual int32_t PrepareModelFromModelCache(const std::vector<OHOS::HDI::Nnrt::V2_0::SharedBuffer>& modelCache,
108         const OHOS::HDI::Nnrt::V2_0::ModelConfig& config, sptr<OHOS::HDI::Nnrt::V2_0::IPreparedModel>& preparedModel) = 0;
109
110    virtual int32_t PrepareOfflineModel(const std::vector<OHOS::HDI::Nnrt::V2_0::SharedBuffer>& offlineModels,
111         const OHOS::HDI::Nnrt::V2_0::ModelConfig& config, sptr<OHOS::HDI::Nnrt::V2_0::IPreparedModel>& preparedModel) = 0;
112
113    virtual int32_t AllocateBuffer(uint32_t length, OHOS::HDI::Nnrt::V2_0::SharedBuffer& buffer) = 0;
114
115    virtual int32_t ReleaseBuffer(const OHOS::HDI::Nnrt::V2_0::SharedBuffer& buffer) = 0;
116
117    virtual int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer)
118    {
119        majorVer = 2;
120        minorVer = 0;
121        return HDF_SUCCESS;
122    }
123
124    virtual bool IsProxy()
125    {
126        return false;
127    }
128
129    virtual const std::u16string GetDesc()
130    {
131        return metaDescriptor_;
132    }
133};
134} // V2_0
135} // Nnrt
136} // HDI
137} // OHOS
138
139#endif // OHOS_HDI_NNRT_V2_0_INNRTDEVICE_H
140
141