mock_idevice.cpp 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "const.h"
#include "mock_idevice.h"

namespace OHOS {
namespace HDI {
namespace Nnrt {
namespace V1_0 {

sptr<INnrtDevice> INnrtDevice::Get(bool isStub)
{
    return INnrtDevice::Get("mock_device_service", isStub);
}

sptr<INnrtDevice> INnrtDevice::Get(const std::string &serviceName, bool isStub)
{
    if (isStub) {
        return nullptr;
    }
    sptr<INnrtDevice> mockIDevice = sptr<MockIDevice>(MockIDevice::GetInstance());
    return mockIDevice;
}

MockIDevice::~MockIDevice()
{
    for (auto ash : m_ashmems) {
        ash.second->UnmapAshmem();
        ash.second->CloseAshmem();
    }
}

MockIDevice *MockIDevice::GetInstance()
{
    static MockIDevice iDevice;
    return &iDevice;
}

void MockIDevice::SetFP16Supported(bool isSupported)
{
    m_fp16 = isSupported;
}

void MockIDevice::SetPerformanceSupported(bool isSupported)
{
    m_performance = isSupported;
}

void MockIDevice::SetPrioritySupported(bool isSupported)
{
    m_priority = isSupported;
}

void MockIDevice::SetModelCacheSupported(bool isSupported)
{
    m_cache = isSupported;
}

void MockIDevice::SetOperationsSupported(std::vector<bool> isSupported)
{
    m_operations = isSupported;
}

void MockIDevice::SetDynamicInputSupported(bool isSupported)
{
    m_dynamic = isSupported;
}

int32_t MockIDevice::GetDeviceName(std::string& name)
{
    name = "RK3568-CPU";
    return HDF_SUCCESS;
}

int32_t MockIDevice::GetVendorName(std::string& name)
{
    name = "Rockchip";
    return HDF_SUCCESS;
}

int32_t MockIDevice::GetDeviceType(DeviceType& deviceType)
{
    deviceType = DeviceType::CPU;
    return HDF_SUCCESS;
}

int32_t MockIDevice::GetDeviceStatus(DeviceStatus& status)
{
    status = DeviceStatus::AVAILABLE;
    return HDF_SUCCESS;
}

int32_t MockIDevice::GetVersion(uint32_t &majorVersion, uint32_t &minorVersion)
{
    majorVersion = 1;
    minorVersion = 0;
    return HDF_SUCCESS;
}

T
tangshihua 已提交
112
int32_t MockIDevice::GetSupportedOperation(const Model& model, std::vector<bool>& ops)
113 114 115 116 117
{
    ops = m_operations;
    return HDF_SUCCESS;
}

T
tangshihua 已提交
118
int32_t MockIDevice::IsFloat16PrecisionSupported(bool& isSupported)
119 120 121 122 123
{
    isSupported = m_fp16;
    return HDF_SUCCESS;
}

T
tangshihua 已提交
124
int32_t MockIDevice::IsPerformanceModeSupported(bool& isSupported)
125 126 127 128 129
{
    isSupported = m_performance;
    return HDF_SUCCESS;
}

T
tangshihua 已提交
130
int32_t MockIDevice::IsPrioritySupported(bool& isSupported)
131 132 133 134 135
{
    isSupported = m_priority;
    return HDF_SUCCESS;
}

T
tangshihua 已提交
136
int32_t MockIDevice::IsDynamicInputSupported(bool& isSupported)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
{
    isSupported = m_dynamic;
    return HDF_SUCCESS;
}

int32_t MockIDevice::IsModelCacheSupported(bool& isSupported)
{
    isSupported = m_cache;
    return HDF_SUCCESS;
}

int32_t MockIDevice::AllocateBuffer(uint32_t length, SharedBuffer &buffer)
{
    sptr<Ashmem> ashptr = Ashmem::CreateAshmem("allocateBuffer", length);
    if (ashptr == nullptr) {
        LOGE("[NNRtTest] Create shared memory failed.");
        return HDF_FAILURE;
    }

    if (!ashptr->MapReadAndWriteAshmem()) {
        LOGE("[NNRtTest] Map allocate buffer failed.");
        return HDF_FAILURE;
    }

    buffer.fd = ashptr->GetAshmemFd();
    buffer.bufferSize = ashptr->GetAshmemSize();
    buffer.offset = 0;
    buffer.dataSize = length;

    m_ashmems[buffer.fd] = ashptr;
T
tangshihua 已提交
167
    m_bufferFd = buffer.fd;
168 169 170 171 172 173 174 175 176 177
    return HDF_SUCCESS;
}

int32_t MockIDevice::ReleaseBuffer(const SharedBuffer &buffer)
{
    auto ash = m_ashmems[buffer.fd];
    ash->UnmapAshmem();
    return HDF_SUCCESS;
}

T
tangshihua 已提交
178
int32_t MockIDevice::MemoryCopy(float *data, uint32_t length)
179 180
{
    auto memManager = NeuralNetworkRuntime::MemoryManager::GetInstance();
T
tangshihua 已提交
181
    auto memAddress = memManager->MapMemory(m_bufferFd, length);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    if (memAddress == nullptr) {
        LOGE("[NNRtTest] Map fd to address failed.");
        return HDF_FAILURE;
    }
    auto ret = memcpy_s(memAddress, length, data, length);
    if (ret != EOK) {
        LOGE("[NNRtTest] MockIDevice memory cop failed.");
        return HDF_FAILURE;
    }
    return HDF_SUCCESS;
}

int32_t MockIDevice::PrepareModel(const Model& model, const ModelConfig& config, sptr<IPreparedModel>& preparedModel)
{
    preparedModel = new (std::nothrow) V1_0::MockIPreparedModel();
    return HDF_SUCCESS;
}

int32_t MockIDevice::PrepareModelFromModelCache(const std::vector<SharedBuffer>& modelCache, const ModelConfig& config,
T
tangshihua 已提交
201
sptr<IPreparedModel>& preparedModel)
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
{
    preparedModel = new (std::nothrow) V1_0::MockIPreparedModel();
    return HDF_SUCCESS;
}

int32_t MockIPreparedModel::ExportModelCache(std::vector<SharedBuffer>& modelCache)
{
    if (!modelCache.empty()) {
        LOGE("[NNRtTest] The parameters of ExportModelCache should be an empty vector.");
        return HDF_ERR_INVALID_PARAM;
    }
    uint8_t buffer[4] = {0, 1, 2, 3};
    uint32_t size = sizeof(buffer);
    sptr<Ashmem> cache = Ashmem::CreateAshmem("cache", size);
    if (cache == nullptr) {
        LOGE("[NNRtTest] Create shared memory failed.");
        return HDF_ERR_MALLOC_FAIL;
    }
    bool ret = cache->MapReadAndWriteAshmem();
    if (!ret) {
        LOGE("[NNRtTest] Map fd to write cache failed.");
        return HDF_FAILURE;
    }
    
    ret = cache->WriteToAshmem(buffer, size, 0);
    cache->UnmapAshmem();
    if (!ret) {
        LOGE("[NNRtTest] Write cache failed.");
        return HDF_FAILURE;
    }
    // SharedBuffer: fd, bufferSize, offset, dataSize
    modelCache.emplace_back(SharedBuffer {cache->GetAshmemFd(), cache->GetAshmemSize(), 0, cache->GetAshmemSize()});
    return HDF_SUCCESS;
}

int32_t MockIPreparedModel::GetVersion(uint32_t &majorVersion, uint32_t &minorVersion)
{
    majorVersion = 1;
    minorVersion = 0;
    return HDF_SUCCESS;
}

int32_t MockIPreparedModel::Run(const std::vector<IOTensor>& inputs, const std::vector<IOTensor>& outputs,
    std::vector<std::vector<int32_t>>& outputsDims, std::vector<bool>& isOutputBufferEnough)
{
    outputsDims = {{1, 2, 2, 1}};
    isOutputBufferEnough = {true};
    return HDF_SUCCESS;
}

} // namespace V1_0
} // namespace Nnrt
} // namespace HDI
} // namespace OHOS