Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XPU] matmul bridge and unit test #2666

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lite/kernels/xpu/bridges/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lite_cc_library(subgraph_bridge_transpose_op_xpu SRCS transpose_op.cc DEPS ${xpu
lite_cc_library(subgraph_bridge_reshape_op_xpu SRCS reshape_op.cc DEPS ${xpu_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_layer_norm_op_xpu SRCS layer_norm_op.cc DEPS ${xpu_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_dropout_op_xpu SRCS dropout_op.cc DEPS ${xpu_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_matmul_op_xpu SRCS matmul_op.cc DEPS ${xpu_subgraph_bridge_deps})

set(xpu_subgraph_bridges
subgraph_bridge_registry
Expand All @@ -44,6 +45,7 @@ set(xpu_subgraph_bridges
subgraph_bridge_reshape_op_xpu
subgraph_bridge_layer_norm_op_xpu
subgraph_bridge_dropout_op_xpu
subgraph_bridge_matmul_op_xpu
CACHE INTERNAL "xpu_subgraph_bridges")

message(STATUS "+++++ xpu_subgraph_bridges: ${xpu_subgraph_bridges}")
88 changes: 88 additions & 0 deletions lite/kernels/xpu/bridges/matmul_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "lite/kernels/npu/bridges/registry.h"
#include "lite/kernels/xpu/bridges/graph.h"
#include "lite/kernels/xpu/bridges/utility.h"

namespace paddle {
namespace lite {
namespace subgraph {
namespace xpu {

int MatmulConverter(void* ctx, OpLite* op, KernelBase* kernel) {
CHECK(ctx != nullptr);
CHECK(op != nullptr);
auto graph = static_cast<Graph*>(ctx);
auto op_info = op->op_info();
auto op_type = op_info->Type();
auto scope = op->scope();
VLOG(3) << "[XPU] Converting " + op_type + "...";

// Get input and output vars and op attributes
auto x_name = op_info->Input("X").front();
auto x_type = kernel->GetInputDeclType("X");
CHECK(x_type->precision() == PRECISION(kFloat));
CHECK(x_type->layout() == DATALAYOUT(kNCHW));
auto x = scope->FindMutableTensor(x_name);
auto x_dims = x->dims();

auto y_name = op_info->Input("Y").front();
auto y_type = kernel->GetInputDeclType("Y");
CHECK(y_type->precision() == PRECISION(kFloat));
CHECK(y_type->layout() == DATALAYOUT(kNCHW));
auto y = scope->FindMutableTensor(y_name);
auto y_dims = y->dims();

auto out_name = op_info->Output("Out").front();
auto out_type = kernel->GetOutputDeclType("Out");
CHECK(out_type->precision() == PRECISION(kFloat));
CHECK(out_type->layout() == DATALAYOUT(kNCHW));

auto transpose_x = op_info->GetAttr<bool>("transpose_X");
CHECK(!transpose_x) << "XPU only support transpose_x == true now";
auto transpose_y = op_info->GetAttr<bool>("transpose_Y");
auto alpha = op_info->GetAttr<float>("alpha");

// X node
std::shared_ptr<xtcl::xExpr> x_node = nullptr;
if (graph->HasNode(x_name)) {
x_node = graph->GetNode(x_name);
} else {
x_node = graph->AddNode(x_name, x_dims);
}

// Y node
std::shared_ptr<xtcl::xExpr> y_node = nullptr;
if (graph->HasNode(y_name)) {
y_node = graph->GetNode(y_name);
} else {
y_node = graph->AddNode(y_name, y_dims);
}

auto matmul_node =
graph->builder_.CreateMatmul2D(*x_node, *y_node, transpose_y);
graph->AddNode(out_name, graph->builder_.CreateScale(matmul_node, alpha));

return SUCCESS;
}

} // namespace xpu
} // namespace subgraph
} // namespace lite
} // namespace paddle

REGISTER_SUBGRAPH_BRIDGE(XPU,
matmul,
paddle::lite::subgraph::xpu::MatmulConverter);
1 change: 1 addition & 0 deletions lite/kernels/xpu/bridges/paddle_use_bridges.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ USE_SUBGRAPH_BRIDGE(XPU, reshape2);
USE_SUBGRAPH_BRIDGE(XPU, layer_norm);
USE_SUBGRAPH_BRIDGE(XPU, gelu);
USE_SUBGRAPH_BRIDGE(XPU, dropout);
USE_SUBGRAPH_BRIDGE(XPU, matmul);
27 changes: 17 additions & 10 deletions lite/tests/kernels/matmul_compute_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,16 @@ void test_matmulnxn_transpose(Place place) {
}

TEST(Matmul2x2, precision) {
#ifdef LITE_WITH_X86
Place place(TARGET(kX86));
Place place;
#if defined(LITE_WITH_ARM)
place = TARGET(kARM);
#elif defined(LITE_WITH_XPU)
place = TARGET(kXPU);
#else
return;
#endif
#ifdef LITE_WITH_ARM
Place place(TARGET(kARM));

test_matmul2x2_no_transform(place);
#endif
}

TEST(Matmul2x2_x_transpose, precision) {
Expand All @@ -520,14 +523,18 @@ TEST(Matmul2x2_x_transpose, precision) {
test_matmul2x2_x_transpose(place);
#endif
}

TEST(Matmul2x2_y_transpose, precision) {
#ifdef LITE_WITH_X86
Place place(TARGET(kX86));
Place place;
#if defined(LITE_WITH_ARM)
place = TARGET(kARM);
#elif defined(LITE_WITH_XPU)
place = TARGET(kXPU);
#else
return;
#endif
#ifdef LITE_WITH_ARM
Place place(TARGET(kARM));

test_matmul2x2_y_transpose(place);
#endif
}

TEST(Matmul2x2_transpose, precision) {
Expand Down