Skip to content

Commit a6b975a

Browse files
Implement IP and Softmax using OneDNN Graph API (#227)
* add submodel onednngraph * add innerproduct and softmax fp32 * reformat * fix onednn graph version
1 parent 9c6a8fa commit a6b975a

File tree

9 files changed

+446
-2
lines changed

9 files changed

+446
-2
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@
7676
[submodule "nlp_toolkit/backends/neural_engine/executor/third_party/boost/libs/mp11"]
7777
path = nlp_toolkit/backends/neural_engine/executor/third_party/boost/libs/mp11
7878
url = https://github.com/boostorg/mp11.git
79+
[submodule "nlp_toolkit/backends/neural_engine/executor/third_party/oneDNNGraph"]
80+
path = nlp_toolkit/backends/neural_engine/executor/third_party/oneDNNGraph
81+
url = https://github.com/oneapi-src/oneDNN.git
82+
branch = dev-graph

nlp_toolkit/backends/neural_engine/compile/ops/empty_ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def __init__(self):
101101
# Fused_op MatMul + BiasAdd
102102
# The inputs are two-dimensional matrices and 1-D const bias
103103
@operator_registry(operator_type='InnerProduct')
104+
@operator_registry(operator_type='InnerProductGraph')
104105
class InnerProduct(Operator):
105106
def __init__(self):
106107
super().__init__()
@@ -400,4 +401,4 @@ def __init__(self):
400401
@operator_registry(operator_type='Convolution')
401402
class Convolution(Operator):
402403
def __init__(self):
403-
super().__init__()
404+
super().__init__()

nlp_toolkit/backends/neural_engine/compile/ops/softmax.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# Computes softmax activations.
2424
# tf.nn.softmax(logits, axis=None, name=None)
2525
@operator_registry(operator_type='Softmax')
26+
@operator_registry(operator_type='SoftmaxGraph')
2627
class Softmax(Operator):
2728
def __init__(self):
2829
super().__init__()

nlp_toolkit/backends/neural_engine/executor/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ endif()
5050
set(GFLAGS_USE_TARGET_NAMESPACE TRUE)
5151
add_subdirectory(${THIRD_PARTY_DIR}/gflags)
5252
set(WITH_GFLAGS OFF CACHE BOOL "disable gflags for glog")
53-
add_subdirectory(${THIRD_PARTY_DIR}/oneDNN)
53+
add_subdirectory(${THIRD_PARTY_DIR}/oneDNNGraph)
5454
add_subdirectory(${THIRD_PARTY_DIR}/pybind11)
5555
add_subdirectory(${THIRD_PARTY_DIR}/yaml-cpp)
5656
add_subdirectory(../SparseLib/ ./hostlibs)
@@ -63,12 +63,14 @@ add_library(neural_engine SHARED
6363
src/operators/output.cpp
6464
src/operators/binary_add.cpp
6565
src/operators/inner_product.cpp
66+
src/onednn_graph_operators/inner_product_graph.cpp
6667
src/operators/layer_norm.cpp
6768
src/operators/matmul.cpp
6869
src/operators/one_hot.cpp
6970
src/operators/padding_sequence.cpp
7071
src/operators/reorder.cpp
7172
src/operators/softmax.cpp
73+
src/onednn_graph_operators/softmax_graph.cpp
7274
src/operators/reshape.cpp
7375
src/operators/gather.cpp
7476
src/operators/strided_slice.cpp
@@ -93,6 +95,7 @@ target_include_directories(neural_engine
9395
PUBLIC
9496
../
9597
./include/sparse_operators
98+
./include/onednn_graph_operators
9699
./include/operators
97100
./include
98101
./third_party/boost/libs/assert/include
@@ -111,13 +114,15 @@ target_include_directories(neural_engine
111114
./third_party/boost/libs/tuple/include
112115
./third_party/boost/libs/predef/include
113116
./third_party/boost/libs/mp11/include
117+
./third_party/oneDNNGraph/include
114118
)
115119

116120
# link against the third party libraries
117121
target_link_libraries(neural_engine
118122
PUBLIC
119123
${CMAKE_THREAD_LIBS_INIT}
120124
dnnl
125+
dnnl_graph
121126
yaml-cpp
122127
gflags
123128
glog
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) 2021 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef ENGINE_EXECUTOR_INCLUDE_ONEDNN_GRAPH_OPERATORS_INNER_PRODUCT_GRAPH_HPP_
16+
#define ENGINE_EXECUTOR_INCLUDE_ONEDNN_GRAPH_OPERATORS_INNER_PRODUCT_GRAPH_HPP_
17+
#include <string>
18+
#include <unordered_map>
19+
#include <vector>
20+
21+
#include "../operator.hpp"
22+
#include "oneapi/dnnl/dnnl_graph.hpp"
23+
24+
namespace executor {
25+
26+
using logical_tensor = dnnl::graph::logical_tensor;
27+
using data_type = dnnl::graph::logical_tensor::data_type;
28+
using layout_type = dnnl::graph::logical_tensor::layout_type;
29+
using property_type = dnnl::graph::logical_tensor::property_type;
30+
31+
/**
32+
* @brief A InnerProduct operator.
33+
*
34+
*/
35+
36+
class InnerProductGraphOperator : public Operator {
37+
public:
38+
explicit InnerProductGraphOperator(const OperatorConfig& conf);
39+
virtual ~InnerProductGraphOperator() {}
40+
41+
void Reshape(const vector<Tensor*>& input, const vector<Tensor*>& output) override;
42+
void Forward(const vector<Tensor*>& input, const vector<Tensor*>& output) override;
43+
void Prepare(const vector<Tensor*>& input, const vector<Tensor*>& output) override;
44+
45+
private:
46+
dnnl::graph::graph g_;
47+
dnnl::graph::engine eng_ {dnnl::graph::engine::kind::cpu, 0};
48+
dnnl::graph::stream strm_ {eng_};
49+
vector<logical_tensor> logical_inputs_;
50+
vector<logical_tensor> logical_outputs_;
51+
dnnl::graph::partition partition_;
52+
dnnl::graph::compiled_partition cp_;
53+
54+
string output_dtype_ = "fp32";
55+
Tensor* dst_min_ = nullptr;
56+
Tensor* dst_max_ = nullptr;
57+
Tensor* src_ = nullptr;
58+
Tensor* dst_ = nullptr;
59+
vector<int64_t> src0_perm_;
60+
vector<int64_t> src1_perm_;
61+
Tensor* src0_ = nullptr;
62+
Tensor* src1_ = nullptr;
63+
Tensor* bias_ = nullptr;
64+
bool has_bias_ = false;
65+
bool transpose_a_ = false;
66+
bool transpose_b_ = true;
67+
bool append_sum_ = false;
68+
bool binary_add_ = false;
69+
bool tanh_ = false;
70+
bool gelu_tanh_ = false;
71+
bool gelu_erf_ = false;
72+
bool gelu_split_ = false;
73+
bool sigmoid_ = false;
74+
bool relu_ = false;
75+
bool append_eltwise_ = false;
76+
string append_op_;
77+
78+
void MapTensors(const vector<Tensor*>& input, const vector<Tensor*>& output);
79+
};
80+
} // namespace executor
81+
#endif // ENGINE_EXECUTOR_INCLUDE_ONEDNN_GRAPH_OPERATORS_INNER_PRODUCT_GRAPH_HPP_
82+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2021 Intel Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef ENGINE_EXECUTOR_INCLUDE_ONEDNN_GRAPH_OPERATORS_SOFTMAX_GRAPH_HPP_
16+
#define ENGINE_EXECUTOR_INCLUDE_ONEDNN_GRAPH_OPERATORS_SOFTMAX_GRAPH_HPP_
17+
#include <string>
18+
#include <unordered_map>
19+
#include <vector>
20+
21+
#include "../operator.hpp"
22+
#include "oneapi/dnnl/dnnl_graph.hpp"
23+
24+
namespace executor {
25+
26+
using logical_tensor = dnnl::graph::logical_tensor;
27+
using data_type = dnnl::graph::logical_tensor::data_type;
28+
using layout_type = dnnl::graph::logical_tensor::layout_type;
29+
using property_type = dnnl::graph::logical_tensor::property_type;
30+
31+
/**
32+
* @brief A Softmax operator.
33+
*
34+
*/
35+
36+
class SoftmaxGraphOperator : public Operator {
37+
public:
38+
explicit SoftmaxGraphOperator(const OperatorConfig& conf);
39+
virtual ~SoftmaxGraphOperator() {}
40+
41+
void Reshape(const vector<Tensor*>& input, const vector<Tensor*>& output) override;
42+
void Forward(const vector<Tensor*>& input, const vector<Tensor*>& output) override;
43+
void Prepare(const vector<Tensor*>& input, const vector<Tensor*>& output) override;
44+
45+
private:
46+
dnnl::graph::graph g_;
47+
dnnl::graph::engine eng_ {dnnl::graph::engine::kind::cpu, 0};
48+
dnnl::graph::stream strm_ {eng_};
49+
vector<logical_tensor> logical_inputs_;
50+
vector<logical_tensor> logical_outputs_;
51+
dnnl::graph::partition partition_;
52+
dnnl::graph::compiled_partition cp_;
53+
54+
int axis_;
55+
string output_dtype_ = "fp32";
56+
Tensor* dst_min_ = nullptr;
57+
Tensor* dst_max_ = nullptr;
58+
Tensor* src_ = nullptr;
59+
Tensor* dst_ = nullptr;
60+
61+
void MapTensors(const vector<Tensor*>& input, const vector<Tensor*>& output);
62+
};
63+
} // namespace executor
64+
#endif // ENGINE_EXECUTOR_INCLUDE_ONEDNN_GRAPH_OPERATORS_SOFTMAX_GRAPH_HPP_
65+

0 commit comments

Comments
 (0)