summaryrefslogtreecommitdiff
path: root/inference-engine/tests/unit/engines/mkldnn/dump_test.cpp
blob: 042f7ac9f62eb2546c3afa7a40e51346a60f9841 (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
// Copyright (C) 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <gtest/gtest.h>

#include "ie_blob.h"
#include "blob_factory.hpp"
#include "utils/blob_dump.h"

using namespace InferenceEngine;
using namespace MKLDNNPlugin;

TEST(MKLDNNDumpTests, UnallocatedBlob_NoDump) {
    SizeVector dims {2,3,4,5};
    Blob::Ptr blob = make_blob_with_precision({Precision::U8, dims, NHWC});

    std::stringstream buff;

    EXPECT_THROW({
        BlobDumper(blob).dump(buff);
    }, details::InferenceEngineException);
}

TEST(MKLDNNDumpTests, EmptyBlob_NoDump) {
    SizeVector dims {2,3,4,5};
    Blob::Ptr blob;

    std::stringstream buff;

    EXPECT_THROW({
        BlobDumper(blob).dump(buff);
    }, details::InferenceEngineException);
}

TEST(MKLDNNDumpTests, Ser) {
    SizeVector dims {2,3,4,5};
    Blob::Ptr blob = make_blob_with_precision({Precision::U8, dims, NHWC});
    blob->allocate();

    std::stringstream buff;
    BlobDumper(blob).dump(buff);

    ASSERT_GT(buff.str().size(), blob->byteSize());
}

TEST(MKLDNNDumpTests, SerDeser) {
    SizeVector dims {2,3,4,5};
    Blob::Ptr blob = make_blob_with_precision({Precision::U8, dims, NCHW});
    blob->allocate();

    std::stringstream buff;

    BlobDumper(blob).dump(buff);
    Blob::Ptr deser_blob = BlobDumper::read(buff).get();

    ASSERT_EQ(deser_blob->dims(), blob->dims());
    ASSERT_EQ(deser_blob->precision(), blob->precision());

    std::vector<uint8_t> data(blob->buffer().as<uint8_t*>(), blob->buffer().as<uint8_t*>() + blob->size());
    std::vector<uint8_t> deser_data(deser_blob->buffer().as<uint8_t*>(), deser_blob->buffer().as<uint8_t*>()
                                    + deser_blob->size());
    ASSERT_EQ(deser_data, data);
}

TEST(MKLDNNDumpTests, SerDeserWithScales) {
    SizeVector dims {2,3,4,5};
    auto blob = make_blob_with_precision({Precision::U8, dims, NCHW});
    blob->allocate();

    auto scls = make_blob_with_precision({Precision::FP32, {3}, C});
    scls->allocate();

    std::stringstream buff;

    BlobDumper(blob).withScales(scls).dump(buff);
    auto deser = BlobDumper::read(buff);
    auto deser_blob = deser.get();
    auto deser_scls = deser.getScales();

    ASSERT_EQ(deser_blob->dims(), blob->dims());
    ASSERT_EQ(deser_blob->precision(), blob->precision());

    std::vector<uint8_t> data(blob->buffer().as<uint8_t*>(), blob->buffer().as<uint8_t*>() + blob->size());
    std::vector<uint8_t> deser_data(deser_blob->buffer().as<uint8_t*>(), deser_blob->buffer().as<uint8_t*>()
                                                                         + deser_blob->size());
    ASSERT_EQ(deser_data, data);

    std::vector<uint8_t> scls_data(scls->buffer().as<uint8_t*>(), scls->buffer().as<uint8_t*>() + scls->size());
    std::vector<uint8_t> deser_scls_data(deser_scls->buffer().as<uint8_t*>(), deser_scls->buffer().as<uint8_t*>()
                                                                         + deser_scls->size());
    ASSERT_EQ(deser_scls_data, scls_data);
}


TEST(MKLDNNDumpTests, SerU8AsTxt) {
    SizeVector dims {2,3,4,5};

    Blob::Ptr blob = make_blob_with_precision({Precision::U8, dims, NCHW});
    blob->allocate();

    Blob::Ptr scls = make_blob_with_precision({Precision::FP32, {dims[1]}, C});
    scls->allocate();

    std::stringstream buff;
    BlobDumper(blob).withScales(scls).dumpAsTxt(buff);

    std::string deser_header, ref_header = "U8 4D shape: 2 3 4 5 (120)";
    std::getline(buff, deser_header);
    ASSERT_EQ(deser_header, ref_header);

    auto num_line = std::count(std::istreambuf_iterator<char>(buff),
            std::istreambuf_iterator<char>(), '\n');
    ASSERT_EQ(num_line, blob->size());
}

TEST(MKLDNNDumpTests, SerAsTxt) {
    SizeVector dims {2,3};

    Blob::Ptr blob = make_blob_with_precision({Precision::FP32, dims, NC});
    blob->allocate();

    Blob::Ptr scls = make_blob_with_precision({Precision::FP32, {dims[1]}, C});
    scls->allocate();

    std::stringstream buff;
    BlobDumper(blob).withScales(scls).dumpAsTxt(buff);

    std::string deser_header, ref_header = "FP32 2D shape: 2 3 (6)";
    std::getline(buff, deser_header);
    ASSERT_EQ(deser_header, ref_header);

    auto num_line = std::count(std::istreambuf_iterator<char>(buff),
                               std::istreambuf_iterator<char>(), '\n');
    ASSERT_EQ(num_line, blob->size());
}