summaryrefslogtreecommitdiff
path: root/inference-engine/src/hetero_plugin/hetero_infer_request.cpp
blob: b5e4cb4ecfb22fa0010a2e22172ba3cd524d7c3b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (C) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

#include "hetero_infer_request.h"
#include <ie_blob.h>
#include <ie_plugin.hpp>
#include <ie_util_internal.hpp>
#include <description_buffer.hpp>
#include <debug.h>
#include <ie_layouts.h>
#include <assert.h>
#include "ie_profiling.hpp"

using namespace HeteroPlugin;
using namespace InferenceEngine;

HeteroInferRequest::HeteroInferRequest(InferenceEngine::InputsDataMap networkInputs,
                                       InferenceEngine::OutputsDataMap networkOutputs,
                                       const SubRequestsList &inferRequests) :
        InferRequestInternal(networkInputs, networkOutputs),
        _inferRequests(inferRequests) {
    if (_networkOutputs.empty() || _networkInputs.empty()) {
        THROW_IE_EXCEPTION << "Internal error: no information about network's output/input";
    }

    auto requestBlob([&](const std::string &e, InferenceEngine::InferRequest::Ptr r) {
        if (networkInputs.find(e) != networkInputs.end()) {
            if (_blobs.find(e) != _blobs.end()) {
                r->SetBlob(e.c_str(), _blobs[e]);
            } else {
                _blobs[e] = r->GetBlob(e.c_str());
                _inputs[e] = _blobs[e];
            }
        } else if (networkOutputs.find(e) != networkOutputs.end()) {
            if (_blobs.find(e) != _blobs.end()) {
                r->SetBlob(e.c_str(), _blobs[e]);
            } else {
                _blobs[e] = r->GetBlob(e.c_str());
                _outputs[e] = _blobs[e];
            }
        } else {
            if (_blobs.find(e) != _blobs.end()) {
                r->SetBlob(e.c_str(), _blobs[e]);
            } else {
                _blobs[e] = r->GetBlob(e.c_str());
            }
        }
    });

    // go over all subnet and create requests
    for (auto &&ireq : _inferRequests) {
        ireq._request = ireq._network->CreateInferRequestPtr();
        // go over all inputs and get blobs from subnet infer requests
        for (auto e : ireq._oNames) {
            requestBlob(e, ireq._request);
        }
    }

    // go over all outputs and get blobs from subnet infer requests
    for (auto r : _inferRequests) {
        for (auto e : r._iNames) {
            requestBlob(e, r._request);
        }
    }
}

void HeteroInferRequest::InferImpl() {
    updateInOutIfNeeded();
    size_t i = 0;
    for (auto &&desc : _inferRequests) {
        IE_PROFILING_AUTO_SCOPE_TASK(desc._profilingTask);
        auto &r = desc._request;
        assert(nullptr != r);
        r->Infer();
    }
}

void HeteroInferRequest::GetPerformanceCounts(std::map<std::string, InferenceEngineProfileInfo> &perfMap) const {
    perfMap.clear();
    for (size_t i = 0; i < _inferRequests.size(); i++) {
        auto perfMapRequest = _inferRequests[i]._request->GetPerformanceCounts();
        for (auto &&r : perfMapRequest) {
            perfMap[std::string("subgraph") + std::to_string(i) + ": " + r.first] = r.second;
        }
    }
}

void HeteroInferRequest::updateInOutIfNeeded() {
    IE_PROFILING_AUTO_SCOPE(updateInOutIfNeeded);
    assert(!_inferRequests.empty());
    for (auto &&desc : _inferRequests) {
        auto &r = desc._request;
        assert(nullptr != r);
        for (auto &&ioname : desc._iNames) {
            auto iti = _inputs.find(ioname);
            if (iti != _inputs.end()) {
                auto it = _preProcData.find(ioname);
                if (it != _preProcData.end()) {
                    if (it->second.getRoiBlob() != _blobs[ioname]) {
                        r->SetBlob(ioname.c_str(), it->second.getRoiBlob());
                        _blobs[ioname] = iti->second;
                    }
                } else {
                    if (iti->second != _blobs[ioname]) {
                        r->SetBlob(ioname.c_str(), iti->second);
                        _blobs[ioname] = iti->second;
                    }
                }
            }
        }
        for (auto &&ioname : desc._oNames) {
            auto ito = _outputs.find(ioname);
            if (ito != _outputs.end()) {
                if (ito->second != _blobs[ioname]) {
                    r->SetBlob(ioname.c_str(), ito->second);
                    _blobs[ioname] = ito->second;
                }
            }
        }
    }
}

void HeteroInferRequest::startFirstAsyncRequest() {
    auto firstAsyncRequest = _inferRequests.begin()->_request;
    firstAsyncRequest->StartAsync();
}

void HeteroInferRequest::setCallbackForLastRequest(std::function<void(InferenceEngine::InferRequest, InferenceEngine::StatusCode)>& callback) {
    auto lastRequest = _inferRequests.back()._request;
    if (lastRequest) lastRequest->SetCompletionCallback(callback);
}

void HeteroInferRequest::setCallbackSequence() {
    for (auto desc = _inferRequests.begin(); desc != _inferRequests.end(); desc++) {
        auto &currentAsyncRequest = desc->_request;
        auto nextRequestDesc = std::next(desc);
        if (nextRequestDesc != _inferRequests.end()) {
            currentAsyncRequest->SetCompletionCallback<std::function<void(InferRequest, StatusCode)>>(
                    [=](InferRequest request, StatusCode sts) {
                        IE_PROFILING_AUTO_SCOPE(Callback)
                        if (sts == OK) {
                            nextRequestDesc->_request->StartAsync();
                        }
                    });
        }
    }
}

StatusCode HeteroInferRequest::waitAllRequests(int64_t millis_timeout) {
    StatusCode status = INFER_NOT_STARTED;
    bool shareMsMode = true;
    std::chrono::high_resolution_clock::time_point startTime;
    int64_t msLeft;
    if (millis_timeout == IInferRequest::WaitMode::STATUS_ONLY ||
        millis_timeout == IInferRequest::WaitMode::RESULT_READY) {
        shareMsMode = false;
    }
    for (auto it = _inferRequests.begin(); it != _inferRequests.end(); ++it) {
        startTime = std::chrono::high_resolution_clock::now();
        status = it->_request->Wait(millis_timeout);
        msLeft = std::chrono::duration_cast<std::chrono::milliseconds>(
                std::chrono::high_resolution_clock::now() - startTime).count();
        if (OK != status) {
            return status;
        }
        if (shareMsMode) {
            if (millis_timeout - msLeft > 0) {
                millis_timeout -= msLeft;
            } else if (it != _inferRequests.end()) {
                return RESULT_NOT_READY;
            }
        }
    }
    return status;
}