Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / gna / matchers / precision_matcher.hpp
1 /*
2  * INTEL CONFIDENTIAL
3  * Copyright (C) 2018-2019 Intel Corporation.
4  *
5  * The source code contained or described herein and all documents
6  * related to the source code ("Material") are owned by Intel Corporation
7  * or its suppliers or licensors. Title to the Material remains with
8  * Intel Corporation or its suppliers and licensors. The Material may
9  * contain trade secrets and proprietary and confidential information
10  * of Intel Corporation and its suppliers and licensors, and is protected
11  * by worldwide copyright and trade secret laws and treaty provisions.
12  * No part of the Material may be used, copied, reproduced, modified,
13  * published, uploaded, posted, transmitted, distributed, or disclosed
14  * in any way without Intel's prior express written permission.
15  *
16  * No license under any patent, copyright, trade secret or other
17  * intellectual property right is granted to or conferred upon you by
18  * disclosure or delivery of the Materials, either expressly, by implication,
19  * inducement, estoppel or otherwise. Any license under such intellectual
20  * property rights must be express and approved by Intel in writing.
21  *
22  * Include any supplier copyright notices as supplier requires Intel to use.
23  *
24  * Include supplier trademarks or logos as supplier requires Intel to use,
25  * preceded by an asterisk. An asterisked footnote can be added as follows:
26  * *Third Party trademarks are the property of their respective owners.
27  *
28  * Unless otherwise agreed by Intel in writing, you may not remove or alter
29  * this notice or any other notice embedded in Materials by Intel or Intel's
30  * suppliers or licensors in any way.
31  */
32
33 #pragma once
34 #include "nnet_base_matcher.hpp"
35
36 class NNetPrecisionMatcher : public ::testing::MatcherInterface<const intel_nnet_type_t*> {
37     GnaPluginTestEnvironment::NnetPrecision nnetPrecision;
38     intel_layer_kind_t layerKind = (intel_layer_kind_t)-1;
39  public:
40     explicit  NNetPrecisionMatcher(GnaPluginTestEnvironment::NnetPrecision nnetPrecision,
41                                    intel_layer_kind_t layerKind = (intel_layer_kind_t)-1) : nnetPrecision(nnetPrecision), layerKind(layerKind) {}
42     bool MatchAndExplain(const intel_nnet_type_t* foo, ::testing::MatchResultListener* listener) const override {
43
44         auto ioPrecision = (foo->pLayers->nBytesPerInput == nnetPrecision.input_precision.size()) &&
45             (foo->pLayers->nBytesPerOutput== nnetPrecision.output_precision.size());
46         if (!ioPrecision) {
47             return false;
48         }
49         if (layerKind != (intel_layer_kind_t)-1) {
50             if (foo->pLayers->nLayerKind != layerKind) {
51                 return false;
52             }
53             switch (layerKind) {
54                 case INTEL_AFFINE : {
55                     auto affine = (intel_affine_layer_t *) (foo->pLayers->pLayerStruct);
56
57                     return affine->affine.nBytesPerBias == nnetPrecision.biases_precision.size() &&
58                         affine->affine.nBytesPerWeight == nnetPrecision.weights_precision.size();
59                 }
60                 default :
61                     return false;
62             }
63
64         }
65         return  true;
66     }
67
68     void DescribeTo(::std::ostream* os) const override {
69         *os << "intel_nnet_layer_t nBytesPerInput equals " << nnetPrecision.input_precision.size() << std::endl;
70         *os << "intel_nnet_layer_t nBytesPerOutput equals " << nnetPrecision.output_precision.size() << std::endl;
71         *os << "intel_nnet_layer_t nBytesPerWeights equals " << nnetPrecision.weights_precision.size() << std::endl;
72         *os << "intel_nnet_layer_t nBytesPerBises equals " << nnetPrecision.biases_precision.size() << std::endl;
73         *os << "foo->pLayers->nLayerKind INTEL_AFFINE" ;
74     }
75 };
76
77 inline ::testing::Matcher<const intel_nnet_type_t*> BitnessOfNNetEq(GnaPluginTestEnvironment::NnetPrecision nnetPrecision,
78                                                          intel_layer_kind_t component) {
79     std::unique_ptr<NNetComponentMatcher> c (new NNetComponentMatcher());
80     c->add(new NNetPrecisionMatcher(nnetPrecision, component));
81     return ::testing::MakeMatcher(c.release());
82 }