commit
ddafe5ceeb
@ -0,0 +1,72 @@
|
||||
# Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.
|
||||
#
|
||||
# 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.
|
||||
|
||||
IF(NOT ${WITH_MKLDNN})
|
||||
return()
|
||||
ENDIF(NOT ${WITH_MKLDNN})
|
||||
|
||||
INCLUDE(ExternalProject)
|
||||
|
||||
SET(MKLDNN_PROJECT "extern_mkldnn")
|
||||
SET(MKLDNN_SOURCES_DIR ${THIRD_PARTY_PATH}/mkldnn)
|
||||
SET(MKLDNN_INSTALL_ROOT ${CMAKE_INSTALL_PREFIX})
|
||||
IF(NOT "$ENV{HOME}" STREQUAL "/root")
|
||||
SET(MKLDNN_INSTALL_ROOT "$ENV{HOME}")
|
||||
ENDIF()
|
||||
|
||||
SET(MKLDNN_INSTALL_DIR "${MKLDNN_INSTALL_ROOT}/opt/paddle/third_party/mkldnn")
|
||||
SET(MKLDNN_INCLUDE_DIR "${MKLDNN_INSTALL_DIR}/include" CACHE PATH "mkldnn include directory." FORCE)
|
||||
|
||||
IF(WIN32)
|
||||
MESSAGE(WARNING "It is not supported compiling with mkldnn in windows Paddle yet."
|
||||
"Force WITH_MKLDNN=OFF")
|
||||
SET(WITH_MKLDNN OFF)
|
||||
return()
|
||||
ELSE(WIN32)
|
||||
SET(MKLDNN_LIBRARY "${MKLDNN_INSTALL_DIR}/lib/libmkldnn.so" CACHE FILEPATH "mkldnn library." FORCE)
|
||||
MESSAGE(STATUS "Set ${MKLDNN_INSTALL_DIR}/lib to runtime path")
|
||||
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
|
||||
#SET(CMAKE_MACOSX_RPATH 1) # hold for MacOS
|
||||
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKLDNN_INSTALL_DIR}/lib")
|
||||
ENDIF(WIN32)
|
||||
|
||||
INCLUDE_DIRECTORIES(${MKLDNN_INCLUDE_DIR})
|
||||
|
||||
IF(${CBLAS_PROVIDER} STREQUAL "MKLML")
|
||||
SET(MKLDNN_DEPENDS ${MKLML_PROJECT})
|
||||
SET(MKLDNN_MKLROOT ${MKLML_ROOT})
|
||||
SET(MKLDNN_IOMP_LIB ${MKLML_IOMP_LIB})
|
||||
SET(MKLDNN_IOMP_DIR ${MKLML_LIB_DIR})
|
||||
ENDIF()
|
||||
|
||||
ExternalProject_Add(
|
||||
${MKLDNN_PROJECT}
|
||||
${EXTERNAL_PROJECT_LOG_ARGS}
|
||||
DEPENDS ${MKLDNN_DEPENDS}
|
||||
GIT_REPOSITORY "https://github.com/01org/mkl-dnn.git"
|
||||
GIT_TAG "v0.9"
|
||||
PREFIX ${MKLDNN_SOURCES_DIR}
|
||||
CONFIGURE_COMMAND mkdir -p <SOURCE_DIR>/build
|
||||
BUILD_COMMAND cd <SOURCE_DIR>/build
|
||||
&& cmake .. -DCMAKE_INSTALL_PREFIX=${MKLDNN_INSTALL_DIR} -DMKLROOT=${MKLDNN_MKLROOT}
|
||||
&& $(MAKE)
|
||||
INSTALL_COMMAND cd <SOURCE_DIR>/build && $(MAKE) install
|
||||
UPDATE_COMMAND ""
|
||||
)
|
||||
|
||||
ADD_LIBRARY(mkldnn SHARED IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET mkldnn PROPERTY IMPORTED_LOCATION ${MKLDNN_LIBRARY})
|
||||
ADD_DEPENDENCIES(mkldnn ${MKLDNN_PROJECT})
|
||||
MESSAGE(STATUS "Mkldnn library: ${MKLDNN_LIBRARY}")
|
||||
LIST(APPEND external_project_dependencies mkldnn)
|
@ -0,0 +1,64 @@
|
||||
# Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve.
|
||||
#
|
||||
# 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.
|
||||
|
||||
IF(NOT ${WITH_MKLML})
|
||||
return()
|
||||
ENDIF(NOT ${WITH_MKLML})
|
||||
|
||||
INCLUDE(ExternalProject)
|
||||
|
||||
SET(MKLML_PROJECT "extern_mklml")
|
||||
SET(MKLML_VER "mklml_lnx_2018.0.20170425")
|
||||
SET(MKLML_URL "https://github.com/01org/mkl-dnn/releases/download/v0.9/${MKLML_VER}.tgz")
|
||||
SET(MKLML_SOURCE_DIR "${THIRD_PARTY_PATH}/mklml")
|
||||
SET(MKLML_DOWNLOAD_DIR "${MKLML_SOURCE_DIR}/src/${MKLML_PROJECT}")
|
||||
SET(MKLML_DST_DIR "opt/paddle/third_party/mklml")
|
||||
SET(MKLML_INSTALL_ROOT "${CMAKE_INSTALL_PREFIX}")
|
||||
IF(NOT "$ENV{HOME}" STREQUAL "/root")
|
||||
SET(MKLML_INSTALL_ROOT "$ENV{HOME}")
|
||||
ENDIF()
|
||||
|
||||
SET(MKLML_INSTALL_DIR ${MKLML_INSTALL_ROOT}/${MKLML_DST_DIR})
|
||||
SET(MKLML_ROOT ${MKLML_INSTALL_DIR}/${MKLML_VER})
|
||||
SET(MKLML_INC_DIR ${MKLML_ROOT}/include)
|
||||
SET(MKLML_LIB_DIR ${MKLML_ROOT}/lib)
|
||||
SET(MKLML_LIB ${MKLML_LIB_DIR}/libmklml_intel.so)
|
||||
SET(MKLML_IOMP_LIB ${MKLML_LIB_DIR}/libiomp5.so)
|
||||
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKLML_ROOT}/lib")
|
||||
|
||||
INCLUDE_DIRECTORIES(${MKLML_INC_DIR})
|
||||
|
||||
SET(mklml_cmakefile ${MKLML_DOWNLOAD_DIR}/CMakeLists.txt)
|
||||
FILE(WRITE ${mklml_cmakefile} "PROJECT(MKLML)\n"
|
||||
"cmake_minimum_required(VERSION 3.0)\n"
|
||||
"install(DIRECTORY ${MKLML_VER}\n"
|
||||
" DESTINATION ${MKLML_DST_DIR})\n")
|
||||
|
||||
ExternalProject_Add(
|
||||
${MKLML_PROJECT}
|
||||
${EXTERNAL_PROJECT_LOG_ARGS}
|
||||
PREFIX ${MKLML_SOURCE_DIR}
|
||||
DOWNLOAD_DIR ${MKLML_DOWNLOAD_DIR}
|
||||
DOWNLOAD_COMMAND wget --no-check-certificate -O ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz ${MKLML_URL}
|
||||
&& tar -xzf ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz
|
||||
DOWNLOAD_NO_PROGRESS 1
|
||||
UPDATE_COMMAND ""
|
||||
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${MKLML_INSTALL_ROOT}
|
||||
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${MKLML_INSTALL_ROOT}
|
||||
)
|
||||
|
||||
ADD_LIBRARY(mklml SHARED IMPORTED GLOBAL)
|
||||
SET_PROPERTY(TARGET mklml PROPERTY IMPORTED_LOCATION ${MKLML_LIB})
|
||||
ADD_DEPENDENCIES(mklml ${MKLML_PROJECT})
|
||||
LIST(APPEND external_project_dependencies mklml)
|
@ -0,0 +1,116 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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 "paddle/framework/grad_op_builder.h"
|
||||
#include "paddle/framework/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
OperatorBase* GradOpBuilder::Build() {
|
||||
BuildOpInOutArgList();
|
||||
std::string grad_op_type = OpRegistry::grad_ops().at(op_->type_);
|
||||
OperatorBase* grad_op = OpRegistry::op_creators().at(grad_op_type)();
|
||||
grad_op->type_ = grad_op_type;
|
||||
CompleteGradOp(grad_op);
|
||||
return grad_op;
|
||||
}
|
||||
|
||||
OpInOutArg* GradOpBuilder::BuildArg(const VarProto& var,
|
||||
const VarIndexMap& var_map,
|
||||
const std::vector<int>& format,
|
||||
InOutType type) {
|
||||
int idx = var_map.at(var.name());
|
||||
int begin_idx = format.empty() ? idx : format.at(idx);
|
||||
int end_idx = format.empty() ? idx + 1 : format.at(idx + 1);
|
||||
return new OpInOutArg(var.name(), type, !var.ignore_gradient(), begin_idx,
|
||||
end_idx);
|
||||
}
|
||||
|
||||
void GradOpBuilder::BuildOpInOutArgList() {
|
||||
const OpProto& op_proto = OpRegistry::protos().at(op_->type_);
|
||||
const auto& var_map = *(OpRegistry::VarIndexMaps().at(op_->type_));
|
||||
const std::vector<int>& in_format =
|
||||
op_->attrs_.count("input_format")
|
||||
? op_->GetAttr<std::vector<int>>("input_format")
|
||||
: std::vector<int>();
|
||||
const std::vector<int>& out_format =
|
||||
op_->attrs_.count("output_format")
|
||||
? op_->GetAttr<std::vector<int>>("output_format")
|
||||
: std::vector<int>();
|
||||
for (const auto& var : op_proto.inputs()) {
|
||||
arg_list_.emplace_back(
|
||||
std::shared_ptr<OpInOutArg>(BuildArg(var, var_map, in_format, IN)));
|
||||
}
|
||||
for (const auto& var : op_proto.outputs()) {
|
||||
arg_list_.emplace_back(
|
||||
std::shared_ptr<OpInOutArg>(BuildArg(var, var_map, out_format, OUT)));
|
||||
}
|
||||
}
|
||||
|
||||
void GradOpBuilder::AddArgIntoGradOp(const OpInOutArg* arg,
|
||||
std::vector<std::string>& in_out,
|
||||
std::vector<int>& format,
|
||||
VarIndexMap* varmap, int& idx,
|
||||
bool is_grad) const {
|
||||
std::string var_name = arg->proto_name_;
|
||||
if (is_grad) {
|
||||
var_name += OperatorBase::GRAD_VAR_SUFFIX();
|
||||
}
|
||||
(*varmap)[var_name] = idx++;
|
||||
size_t pre_sz = in_out.size();
|
||||
auto base_it =
|
||||
arg->type_ == IN ? op_->inputs_.begin() : op_->outputs_.begin();
|
||||
std::copy(base_it + arg->begin_idx_, base_it + arg->end_idx_,
|
||||
std::back_inserter(in_out));
|
||||
if (is_grad) {
|
||||
for (size_t i = pre_sz; i < in_out.size(); ++i) {
|
||||
in_out[i] += OperatorBase::GRAD_VAR_SUFFIX();
|
||||
}
|
||||
}
|
||||
format.push_back(in_out.size());
|
||||
}
|
||||
|
||||
void GradOpBuilder::CompleteGradOp(OperatorBase* grad_op) const {
|
||||
grad_op->attrs_ = op_->attrs_;
|
||||
grad_op->attrs_.erase("input_format");
|
||||
grad_op->attrs_.erase("output_format");
|
||||
VarIndexMap* grad_varmap = new VarIndexMap();
|
||||
int in_idx = 0;
|
||||
int out_idx = 0;
|
||||
std::vector<int> in_format({0});
|
||||
std::vector<int> out_format({0});
|
||||
for (const auto& arg : arg_list_) {
|
||||
// op_'s inputs_ and outputs_
|
||||
if (arg->needed_in_grad_) {
|
||||
AddArgIntoGradOp(arg.get(), grad_op->inputs_, in_format, grad_varmap,
|
||||
in_idx, false);
|
||||
}
|
||||
if (arg->type_ == IN) {
|
||||
// gradients of op_'s inputs_
|
||||
AddArgIntoGradOp(arg.get(), grad_op->outputs_, out_format, grad_varmap,
|
||||
out_idx, true);
|
||||
} else {
|
||||
// gradients of op_'s outputs_
|
||||
AddArgIntoGradOp(arg.get(), grad_op->inputs_, in_format, grad_varmap,
|
||||
in_idx, true);
|
||||
}
|
||||
}
|
||||
grad_op->attrs_["input_format"] = in_format;
|
||||
grad_op->attrs_["output_format"] = out_format;
|
||||
grad_op->in_out_idxs_.reset(grad_varmap);
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "paddle/framework/op_proto.pb.h"
|
||||
#include "paddle/framework/operator.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
class OpRegistry;
|
||||
|
||||
enum InOutType { IN, OUT };
|
||||
|
||||
struct OpInOutArg {
|
||||
OpInOutArg(const std::string& proto_name, const InOutType& type,
|
||||
bool needed_in_grad, size_t begin_idx, size_t end_idx)
|
||||
: proto_name_(proto_name),
|
||||
type_(type),
|
||||
needed_in_grad_(needed_in_grad),
|
||||
begin_idx_(begin_idx),
|
||||
end_idx_(end_idx) {}
|
||||
|
||||
std::string proto_name_;
|
||||
InOutType type_;
|
||||
bool needed_in_grad_;
|
||||
size_t begin_idx_;
|
||||
size_t end_idx_;
|
||||
};
|
||||
|
||||
class GradOpBuilder {
|
||||
using VarIndexMap = std::unordered_map<std::string, int>;
|
||||
|
||||
public:
|
||||
GradOpBuilder(const OperatorBase* op) : op_(op) {}
|
||||
OperatorBase* Build();
|
||||
|
||||
private:
|
||||
OpInOutArg* BuildArg(const VarProto& var, const VarIndexMap& var_map,
|
||||
const std::vector<int>& format, InOutType type);
|
||||
void BuildOpInOutArgList();
|
||||
void AddArgIntoGradOp(const OpInOutArg* arg, std::vector<std::string>& in_out,
|
||||
std::vector<int>& format, VarIndexMap* varmap, int& idx,
|
||||
bool is_grad) const;
|
||||
void CompleteGradOp(OperatorBase* grad_op) const;
|
||||
const OperatorBase* op_;
|
||||
std::vector<std::shared_ptr<OpInOutArg>> arg_list_;
|
||||
};
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue