summaryrefslogtreecommitdiff
path: root/inference-engine/src/cldnn_engine/debug_options.cpp
blob: 5a6de153688be8623b6783603513e8e904d111d1 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (C) 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <iostream>
#include <iomanip>
#ifndef NDEBUG
    #include <algorithm>
    #include <cmath>
#endif

#include "debug_options.h"

namespace CLDNNPlugin {

DebugOptions::DebugOptions() {
    m_bDebugLayerContent =
#ifdef _DEBUG_LAYER_CONTENT
        true;
#else
        false;
#endif

    m_bDebugLayerContentIndexed =
#ifdef _DEBUG_LAYER_CONTENT_INDEXED
        true;
#else
        false;
#endif

    m_bDebugLayerFormat =
#ifdef _DEBUG_LAYER_FORMAT
        true;
#else
        false;
#endif

    m_bPluginPerfPrints =
#ifdef _PLUGIN_PERF_PRINTS
        true;
#else
        false;
#endif

    m_maxPrintSize =
#ifdef _DEBUG_LAYER_CONTENT_FULL
        1000000000;
#else
        3;
#endif
}

void DebugOptions::PrintOptions() const {
#ifndef NDEBUG
    std::cout << "Debug Options:" << std::endl;
    std::cout << "\tDebug Layer Content: " << m_bDebugLayerContent << std::endl;
    std::cout << "\tDebug Layer Content Indexed: " << m_bDebugLayerContentIndexed << std::endl;
    std::cout << "\tDebug Layers Format: " << m_bDebugLayerFormat << std::endl;
    std::cout << "\tPlugin Performance Prints: " << m_bPluginPerfPrints << std::endl;
    std::cout << "\tPrint Size: " << m_maxPrintSize << std::endl;
#endif  // NDEBUG
}

std::string DebugOptions::GetFormatName(cldnn::format::type format) {
    switch (format) {
    case cldnn::format::yxfb:
        return "yxfb";
    case cldnn::format::byxf:
        return "byxf";
    case cldnn::format::bfyx:
        return "bfyx";
    case cldnn::format::fyxb:
        return "fyxb";
    default:
        return "Unknown Format";
    }
}

std::string DebugOptions::GetDataTypeName(cldnn::data_types dataType) {
    switch (dataType) {
    case cldnn::data_types::f16:
        return "f16";
    case cldnn::data_types::f32:
        return "f32";
    default:
        return "Unknown Data Type";
    }
}

void DebugOptions::PrintInput(const InferenceEngine::TBlob<float>& input) const {
#ifndef NDEBUG
    const float* inputBlobPtr = input.readOnly();

    if (m_bDebugLayerContent) {
        std::cout << "Input (" << input.size() << ") = ";
        for (size_t i = 0; i < std::min<size_t>(m_maxPrintSize, input.size()); i++) {
            std::cout << inputBlobPtr[i] << ", ";
        }
        std::cout << std::endl;
    }
#endif  // NDEBUG
}

float DebugOptions::SimpleConvertFP16toFP32(uint16_t u16val) {
#ifndef NDEBUG
    // convert to fp32 (1,5,10)->(1,8,23)
    // trivial conversion not handling inf/denorm
    uint32_t sign = (u16val & 0x8000U) << 16;
    uint32_t mantissa = (u16val & 0x3FFU) << 13;
    uint32_t exp_val_f16 = (u16val & 0x7C00U) >> 10;
    uint32_t exp = (exp_val_f16 == 0x1FU ? 0xFFU : exp_val_f16 + 127 - 15) << 23;;
    uint32_t val = sign | exp | mantissa;
    float fval = *(reinterpret_cast<float*>(&val));
    return (fabs(fval) < 1e-4f) ? 0.0f : fval;  // clamp epsilon fp16 to 0
#endif  // NDEBUG
    return 0;
}
void DebugOptions::PrintIndexedValue(const cldnn::memory& mem, const cldnn::tensor index) const {
#ifndef NDEBUG
    auto layout = mem.get_layout();
    float fval;
    switch (layout.data_type) {
    case cldnn::data_types::f32: {
        auto p32 = mem.pointer<float>();
        auto resPtrF32 = p32.data();
        fval = resPtrF32[CalcLinearIndex(layout, index)];
    }
    break;
    case cldnn::data_types::f16:
    {
        auto p16 = mem.pointer<uint16_t>();
        auto resPtrU16 = p16.data();
        fval = SimpleConvertFP16toFP32(resPtrU16[CalcLinearIndex(layout, index)]);
    }
    break;
    default:
        assert(0);  // unhandled data type
        fval = 0.0f;
    }

    if (m_bDebugLayerContentIndexed) {
        std::cout << "\t[";
        for (size_t i = 0; i < index.raw.size(); i++) {
            std::cout << index.raw[i] << ",";
        }
        std::cout << "] = " << fval << "\n";
    } else {
        std::cout << fval << ", ";
    }
#endif  // NDEBUG
}

uint32_t DebugOptions::CalcLinearIndex(const cldnn::layout& memLayout, const cldnn::tensor index) {
#ifndef NDEBUG
    uint32_t bPitch, fPitch, xPitch, yPitch;
    switch (memLayout.format) {
    case cldnn::format::yxfb:
        bPitch = 1;
        fPitch = memLayout.size.batch[0] * bPitch;
        xPitch = memLayout.size.feature[0] * fPitch;
        yPitch = memLayout.size.spatial[1] * xPitch;
        return (index.batch[0] * bPitch)
            + (index.feature[0] * fPitch)
            + (index.spatial[1] * xPitch)
            + (index.spatial[0] * yPitch);
        break;
    case cldnn::format::bfyx:
        xPitch = 1;
        yPitch = memLayout.size.spatial[1] * xPitch;
        fPitch = memLayout.size.spatial[0] * yPitch;
        bPitch = memLayout.size.feature[0] * fPitch;
        return (index.batch[0] * bPitch)
            + (index.feature[0] * fPitch)
            + (index.spatial[1] * xPitch)
            + (index.spatial[0] * yPitch);
        break;
    default:
        assert(0);
        return 0;
    }
#endif  // NDEBUG
    return 0;
}

void DebugOptions::PrintNetworkOutputs(std::map<cldnn::primitive_id, cldnn::network_output>& outputsMap) const {
#ifndef NDEBUG
    if (!m_bDebugLayerContent && !m_bDebugLayerFormat) {
        return;
    }

    std::chrono::nanoseconds total(0);
    for (auto& layer : outputsMap) {
        std::cout << layer.first << ":\n";
        auto mem = layer.second.get_memory();
        auto layout = mem.get_layout();
        if (m_bDebugLayerFormat) {
            std::string formatName = GetFormatName(layout.format);
            std::string datatypeName = GetDataTypeName(layout.data_type);
            std::cout << "  Layout: ( " <<
                GetDataTypeName(layout.data_type) << ", " <<
                GetFormatName(layout.format) << ", [";
            for (auto s : layout.size.sizes()) {
                std::cout << s << ",";
            }
            std::cout << "] )\n";
        }
        if (m_bDebugLayerContent) {
            DumpSingleOutput(layer.first, outputsMap);
            std::cout << "\n";
        }
    }
#endif  // NDEBUG
}

void DebugOptions::DumpSingleOutput(cldnn::primitive_id name, std::map<cldnn::primitive_id, cldnn::network_output>& outputs, bool bSingleFeatureMap) const {
#ifndef NDEBUG
    if (outputs.find(name) == outputs.end()) {
        std::cout << "Couldn't find output: " << name << std::endl;
        return;
    }

    auto output = outputs.at(name);
    std::cout << name << ":\n";
    auto mem = output.get_memory();
    auto layout = mem.get_layout();
    cldnn::tensor lowerPad = layout.data_padding.lower_size();
    cldnn::tensor upperPad = layout.data_padding.upper_size();
    {   // format
        std::string formatName = GetFormatName(layout.format);
        std::string datatypeName = GetDataTypeName(layout.data_type);
        std::cout << "  Layout: ( " <<
            GetDataTypeName(layout.data_type) << ", " <<
            GetFormatName(layout.format) << ", [";
        for (auto s : layout.size.sizes()) {
            std::cout << s << ",";
        }
        std::cout << "] [";
        for (auto p : layout.data_padding.lower_size().sizes()) {
            std::cout << p << ",";
        }
        std::cout << "] [";
        for (auto p : layout.data_padding.upper_size().sizes()) {
            std::cout << p << ",";
        }
        std::cout << "] )\n";
    }
    {   // content
        switch (layout.format) {
        case cldnn::format::bfyx:
        {
            std::vector<size_t> pitches;
            size_t elements = 1;
            if (bSingleFeatureMap) {
                elements = layout.size.spatial[1] * layout.size.spatial[0];
            } else {
                for (int i = 0; i < 4; i++) {
                    elements *= layout.size.sizes()[i] + lowerPad.sizes()[i] + upperPad.sizes()[i];
                }
            }
            pitches.push_back(layout.size.spatial[0] + lowerPad.spatial[0] + upperPad.spatial[0]);  // x or width - rowpitch
            pitches.push_back(pitches[0] * (layout.size.spatial[1] + lowerPad.spatial[1] + upperPad.spatial[1]));  // slice pitch
            pitches.push_back(pitches[0] * pitches[1] * layout.size.feature[0]);  // depth/feature pitch
            if (layout.data_type == cldnn::data_types::f32)
                DumpElementsRaw<float>(mem, pitches, elements);
            else
                DumpElementsRaw<uint16_t>(mem, pitches, elements);
            break;
        }
        default:
            assert(0);  // unhandled format
            return;
        }
        std::cout << "\n";
    }
#endif  // NDEBUG
}

void DebugOptions::AddTimedEvent(std::string eventName, std::string startingAt) {
#ifdef _PLUGIN_PERF_PRINTS
    m_TimedEventTimestamp[eventName] = std::chrono::steady_clock::now();
    if (startingAt.compare(std::string()) == 0) {
        startingAt = eventName;
    }
    m_TimedEventStart[eventName] = startingAt;
#endif  // _PLUGIN_PERF_PRINTS
}

void DebugOptions::PrintTimedEvents() {
#ifdef _PLUGIN_PERF_PRINTS
    for (auto& e : m_TimedEventStart) {
        if (e.first.compare(e.second)) {
            std::cout << "[Plugin Internal Metric]: \t" << e.first << " took: " <<
                std::chrono::duration_cast<std::chrono::duration<double, std::chrono::milliseconds::period>>
                (m_TimedEventTimestamp[e.first] - m_TimedEventTimestamp[e.second]).count() << " ms\n";
        }
    }
#endif  // _PLUGIN_PERF_PRINTS
}

void DebugOptions::ClearTimedEvents() {
#ifdef _PLUGIN_PERF_PRINTS
    m_TimedEventStart.clear();
    m_TimedEventTimestamp.clear();
#endif  // _PLUGIN_PERF_PRINTS
}

void DebugOptions::EnableWA(std::string name) {
#ifndef NDEBUG
    m_workaroundNames.insert(name);
#endif  // NDEBUG
}

void DebugOptions::DisableWA(std::string name) {
#ifndef NDEBUG
    m_workaroundNames.erase(name);
#endif  // NDEBUG
}

bool DebugOptions::IsWAActive(std::string name) {
#ifndef NDEBUG
    return (m_workaroundNames.find(name) != m_workaroundNames.end());
#else
    return false;
#endif  // NDEBUG
}

std::string DebugOptions::IELayoutToString(InferenceEngine::Layout layout) {
    switch (layout) {
    case InferenceEngine::ANY: return "ANY";
    case InferenceEngine::NCHW: return "NCHW";
    case InferenceEngine::NHWC: return "NHWC";
    case InferenceEngine::OIHW: return "OIHW";
    case InferenceEngine::C: return "C";
    case InferenceEngine::CHW: return "CHW";
    case InferenceEngine::HW: return "HW";
    case InferenceEngine::NC: return "NC";
    default: return "Unknown";
    }
}

};  // namespace CLDNNPlugin