Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / gna / matchers / diag_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"gna-api.h"
35 #include "nnet_base_matcher.hpp"
36 #include "quantization/quantization.h"
37
38 class DiagLayerMatcher : public ::testing::MatcherInterface<const intel_nnet_type_t*> {
39     bool matchInserted;
40     int matchQuantity;
41  public:
42     DiagLayerMatcher(bool matchInserted, int matchQuantity) : matchInserted(matchInserted), matchQuantity(matchQuantity) {}
43     bool MatchAndExplain(const intel_nnet_type_t *foo, ::testing::MatchResultListener *listener) const override {
44         if (foo == nullptr)
45             return false;
46         for(int i = 0; i < foo->nLayers; i++) {
47             if (foo->pLayers[i].nLayerKind != INTEL_AFFINE_DIAGONAL) continue;
48             // diagonal layer has to have 1 for weights and 0 for biases
49
50             auto diag = (intel_affine_func_t*)foo->pLayers[i].pLayerStruct;
51
52             bool bWeightsOK = true;
53             for (int j =0; j < foo->pLayers[i].nOutputRows; j++) {
54                 auto weights = (int16_t*)diag->pWeights;    
55                 auto biases = (int32_t*)diag->pBiases;
56                 // identity matrix tansformed to 16384 values
57                 if (weights[j] != MAX_VAL_2B_WEIGHT || biases[j] != 0) {
58                     bWeightsOK = false;
59                     break;
60                 }
61             }
62             if (!bWeightsOK) continue;
63
64             return matchInserted;
65         }
66         return !matchInserted;
67     };
68     void DescribeTo(::std::ostream *os) const override {
69         *os << "should "<< (matchInserted ? "" : "not ") << "have Identity Diagonal Primitive primitive as part of nnet structure";
70     }
71 };
72
73 inline ::testing::Matcher<const intel_nnet_type_t*> HasDiagonalLayer(bool matchInserted = false, int matchQuantity = -1) {
74     std::unique_ptr<NNetComponentMatcher> c (new NNetComponentMatcher());
75     c->add(new DiagLayerMatcher(matchInserted, matchQuantity));
76     return ::testing::MakeMatcher(c.release());
77 }
78
79