Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / kernel_selector / core / actual_kernels / lstm / lstm_gemm_kernel_base.h
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #pragma once
18
19 #include "common_kernel_base.h"
20 #include "kernel_selector_params.h"
21
22 namespace kernel_selector
23 {
24     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25     // lstm_gemm_params
26     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27     struct lstm_gemm_params : public base_params
28     {
29         lstm_gemm_params() : base_params(KernelType::LSTM_GEMM) {}
30
31         DataTensor weights;
32         DataTensor recurrent;
33         DataTensor bias;
34         DataTensor hidden;
35         bool hasBias = false;
36         bool hasHidden = false;
37         uint32_t direction = 0;
38         uint32_t input_direction = 0; // for bidirectional node fusion in stacked LSTMs
39         uint32_t hidden_direction = 0;
40
41         void SetBias(const DataTensor& v) {
42             bias = v;
43             hasBias = true;
44         }
45
46         void SetHidden(const DataTensor& v) {
47             hidden = v;
48             hasHidden = true;
49         }
50
51         virtual ParamsKey GetParamsKey() const override
52         {
53             ParamsKey k = base_params::GetParamsKey();
54
55             if (hasBias)
56             {
57                 k.EnableLSTMGEMMBias();
58             }
59
60             if (hasHidden)
61             {
62                 k.EnableLSTMGEMMHidden();
63             }
64
65             return k;
66         }
67     };
68
69     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70     // lstm_gemm_optional_params
71     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
72     struct lstm_gemm_optional_params : optional_params
73     {
74         lstm_gemm_optional_params() : optional_params(KernelType::LSTM_GEMM) {}
75     };
76
77     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78     // LSTMGemmKernelBase
79     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80     class LSTMGemmKernelBase : public common_kernel_base
81     {
82     public:
83                 using common_kernel_base::common_kernel_base;
84         virtual ~LSTMGemmKernelBase() {}
85
86         struct DispatchData : public CommonDispatchData
87         {};
88
89     protected:
90         virtual JitConstants GetJitConstants(const lstm_gemm_params& params) const;
91         KernelsData GetCommonKernelsData(const Params& params, const optional_params& optParams) const;
92
93         bool Validate(const Params& p, const optional_params&) const override
94         {
95             if (p.GetType() != KernelType::LSTM_GEMM)
96             {
97                 return false;
98             }
99
100             return true;
101         }
102     };
103 }