aboutsummaryrefslogtreecommitdiff
path: root/1.1/ArmnnDriverImpl.cpp
blob: a5e32766b0dba9e03ee8d5a487fd9bb9fc92359b (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#include "ArmnnDriverImpl.hpp"
#include "../1.0/ArmnnDriverImpl.hpp"

#include <OperationsUtils.h>

#include <log/log.h>
#include <boost/assert.hpp>

#include <ValidateHal.h>

using namespace std;
using namespace android;
using namespace android::nn;
using namespace android::hardware;

namespace
{

void NotifyCallbackAndCheck(const sp<IPreparedModelCallback>& callback,
                            ErrorStatus errorStatus,
                            const sp<IPreparedModel>& preparedModelPtr)
{
    Return<void> returned = callback->notify(errorStatus, preparedModelPtr);
    // This check is required, if the callback fails and it isn't checked it will bring down the service
    if (!returned.isOk())
    {
        ALOGE("V1_1::ArmnnDriverImpl::prepareModel_1_1: hidl callback failed to return properly: %s ",
              returned.description().c_str());
    }
}

Return<ErrorStatus> FailPrepareModel(ErrorStatus error,
                                     const string& message,
                                     const sp<IPreparedModelCallback>& callback)
{
    ALOGW("V1_1::ArmnnDriverImpl::prepareModel_1_1: %s", message.c_str());
    NotifyCallbackAndCheck(callback, error, nullptr);
    return error;
}

} // namespace

namespace armnn_driver
{
namespace V1_1
{

Return<void> ArmnnDriverImpl::getCapabilities_1_1(
        const armnn::IRuntimePtr& runtime,
        neuralnetworks::V1_1::IDevice::getCapabilities_1_1_cb cb)
{
    ALOGV("V1_1::ArmnnDriverImpl::getCapabilities_1_1()");

    neuralnetworks::V1_0::IDevice::getCapabilities_cb cb_1_0 =
            [&](ErrorStatus status, const neuralnetworks::V1_0::Capabilities& capabilities)
    {
        BOOST_ASSERT_MSG(compliantWithV1_1(capabilities),
                         "V1_1::ArmnnDriverImpl: V1_0::Capabilities not compliant with V1_1::Capabilities");

        cb(status, convertToV1_1(capabilities));
    };

    V1_0::ArmnnDriverImpl::getCapabilities(runtime, cb_1_0);

    return Void();
}

Return<void> ArmnnDriverImpl::getSupportedOperations_1_1(
        const armnn::IRuntimePtr& runtime,
        const DriverOptions& options,
        const neuralnetworks::V1_1::Model& model,
        neuralnetworks::V1_1::IDevice::getSupportedOperations_1_1_cb cb)
{
    ALOGV("V1_1::ArmnnDriverImpl::getSupportedOperations_1_1()");

    if(compliantWithV1_0(model))
    {
        V1_0::ArmnnDriverImpl::getSupportedOperations(runtime, options, convertToV1_0(model), cb);
    }
    else
    {
        std::vector<bool> result;

        if (!runtime)
        {
            ALOGW("V1_1::ArmnnDriverImpl::getSupportedOperations_1_1: Device unavailable");
            cb(ErrorStatus::DEVICE_UNAVAILABLE, result);
            return Void();
        }

        if (!android::nn::validateModel(model))
        {
            ALOGW("V1_1::ArmnnDriverImpl::getSupportedOperations_1_1: Invalid model passed as input");
            cb(ErrorStatus::INVALID_ARGUMENT, result);
            return Void();
        }

        result.assign(model.operations.size(), false);
        cb(ErrorStatus::NONE, result);
    }

    return Void();
}

Return<ErrorStatus> ArmnnDriverImpl::prepareModel_1_1(
        const armnn::IRuntimePtr& runtime,
        const armnn::IGpuAccTunedParametersPtr& clTunedParameters,
        const DriverOptions& options,
        const neuralnetworks::V1_1::Model& model,
        const sp<IPreparedModelCallback>& cb)
{
    ALOGV("V1_1::ArmnnDriverImpl::prepareModel_1_1()");

    if(compliantWithV1_0(model))
    {
        return V1_0::ArmnnDriverImpl::prepareModel(runtime, clTunedParameters, options, convertToV1_0(model), cb,
                                                   model.relaxComputationFloat32toFloat16 && options.GetFp16Enabled());
    }
    else
    {
        if (cb.get() == nullptr)
        {
            ALOGW("V1_1::ArmnnDriverImpl::prepareModel_1_1: Invalid callback passed to prepareModel");
            return ErrorStatus::INVALID_ARGUMENT;
        }

        if (!runtime)
        {
            return FailPrepareModel(ErrorStatus::DEVICE_UNAVAILABLE,
                                    "V1_1::ArmnnDriverImpl::prepareModel_1_1: Device unavailable", cb);
        }

        if (!android::nn::validateModel(model))
        {
            return FailPrepareModel(ErrorStatus::INVALID_ARGUMENT,
                                    "V1_1::ArmnnDriverImpl::prepareModel_1_1: Invalid model passed as input", cb);
        }

        FailPrepareModel(ErrorStatus::GENERAL_FAILURE,
                         "V1_1::ArmnnDriverImpl::prepareModel_1_1: Unsupported model", cb);
        return ErrorStatus::NONE;
    }
}

} // armnn_driver::namespace V1_1
} // namespace armnn_driver