Merge branch 'develop' of https://github.com/PaddlePaddle/paddle into enhance-ReshapeOp
commit
4bfbc59122
@ -0,0 +1,43 @@
|
||||
if(NOT WITH_AMD_GPU)
|
||||
return()
|
||||
endif()
|
||||
|
||||
include_directories("/opt/rocm/include")
|
||||
include_directories("/opt/rocm/hipblas/include")
|
||||
include_directories("/opt/rocm/hiprand/include")
|
||||
include_directories("/opt/rocm/rocrand/include")
|
||||
include_directories("/opt/rocm/rccl/include")
|
||||
include_directories("/opt/rocm/thrust")
|
||||
|
||||
list(APPEND EXTERNAL_LIBS "-L/opt/rocm/lib/ -lhip_hcc")
|
||||
|
||||
set(HIP_HCC_FLAGS "${HIP_HCC_FLAGS} -fPIC -DPADDLE_WITH_HIP -std=c++14" )
|
||||
|
||||
if(WITH_DSO)
|
||||
set(HIP_HCC_FLAGS "${HIP_HCC_FLAGS} -DPADDLE_USE_DSO")
|
||||
endif(WITH_DSO)
|
||||
|
||||
if(WITH_DOUBLE)
|
||||
set(HIP_HCC_FLAGS "${HIP_HCC_FLAGS} -DPADDLE_TYPE_DOUBLE")
|
||||
endif(WITH_DOUBLE)
|
||||
|
||||
if(WITH_TESTING)
|
||||
set(HIP_HCC_FLAGS "${HIP_HCC_FLAGS} -DPADDLE_WITH_TESTING")
|
||||
endif(WITH_TESTING)
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
list(APPEND HIP_HCC_FLAGS ${CMAKE_CXX_FLAGS_DEBUG})
|
||||
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
|
||||
list(APPEND HIP_HCC_FLAGS ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
|
||||
elseif(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
|
||||
list(APPEND HIP_HCC_FLAGS ${CMAKE_CXX_FLAGS_MINSIZEREL})
|
||||
endif()
|
||||
|
||||
if("x${HCC_HOME}" STREQUAL "x")
|
||||
set(HCC_HOME "/opt/rocm/hcc")
|
||||
endif()
|
||||
|
||||
set(CMAKE_HIP_LINK_EXECUTABLE "${HIP_HIPCC_CMAKE_LINKER_HELPER} ${HCC_HOME} <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
|
||||
set(CMAKE_HIP_CREATE_SHARED_LIBRARY "${HIP_HIPCC_CMAKE_LINKER_HELPER} ${HCC_HOME} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> -shared")
|
||||
set(CMAKE_HIP_CREATE_SHARED_MODULE "${HIP_HIPCC_CMAKE_LINKER_HELPER} ${HCC_HOME} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> -shared")
|
||||
|
@ -0,0 +1,231 @@
|
||||
# go_op Design
|
||||
|
||||
## Introduction
|
||||
|
||||
The **go_op** allows user's of PaddlePaddle to run program blocks on a detached
|
||||
thread. It works in conjuction with CSP operators (channel_send,
|
||||
channel_receive, channel_open, channel_close, and select) to allow users to
|
||||
concurrently process data and communicate easily between different threads.
|
||||
|
||||
## How to use it
|
||||
|
||||
```
|
||||
channel = fluid.make_channel(dtype=core.VarDesc.VarType.LOD_TENSOR)
|
||||
|
||||
with fluid.Go():
|
||||
# Send a tensor of value 99 to "channel" on a detached thread
|
||||
tensor = fill_constant(shape=[1], dtype='int', value=99)
|
||||
tensor.stop_gradient = True
|
||||
fluid.channel_send(channel, tensor)
|
||||
|
||||
# Receive sent tensor from "channel" on the main thread
|
||||
result = fill_constant(shape=[1], dtype='int', value=-1)
|
||||
fluid.channel_recv(ch, result)
|
||||
```
|
||||
|
||||
The go operator can be accessed by using the fluid.Go() control flow. This
|
||||
will create a new sub block, where the user can add additional operators
|
||||
to be ran on the thread.
|
||||
|
||||
**Note:** Since back propegation is currently not support in the go_op, users
|
||||
should ensure that operators in the go block does not require gradient
|
||||
calculations.
|
||||
|
||||
## How it Works
|
||||
|
||||
Similar to other control blocks, go_op will create a sub block and add it
|
||||
as a child to the current block. Operators and variables defined in this
|
||||
block will be added to the go sub_block.
|
||||
|
||||
In addition, the go operator will create a new child scope whose parent is
|
||||
the global scope. Please refer to [block captures](#block-captures) for more
|
||||
information.
|
||||
|
||||
When Paddle executor runs go_op, go_op will take the sub_block and pass it to
|
||||
the executor.run method (along with a newly created local scope) on a detached
|
||||
thread.
|
||||
|
||||
An example of the generated program description is shown below. Take note of
|
||||
the **go_op** in particular. It is added as an operator in the current
|
||||
block (in this example, block0). The **go_op** contains a `sub_block`
|
||||
attribute, which points to the id of the block that will be executed in a
|
||||
detached thread.
|
||||
|
||||
```
|
||||
blocks {
|
||||
idx: 0
|
||||
parent_idx: -1
|
||||
vars {
|
||||
name: "return_value"
|
||||
type {
|
||||
type: LOD_TENSOR
|
||||
lod_tensor {
|
||||
tensor {
|
||||
data_type: INT64
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
vars {
|
||||
name: "status_recv"
|
||||
type {
|
||||
type: LOD_TENSOR
|
||||
lod_tensor {
|
||||
tensor {
|
||||
data_type: BOOL
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
...
|
||||
ops {
|
||||
outputs {
|
||||
parameter: "Out"
|
||||
arguments: "channel"
|
||||
}
|
||||
type: "channel_create"
|
||||
attrs {
|
||||
name: "data_type"
|
||||
type: INT
|
||||
i: 7
|
||||
}
|
||||
attrs {
|
||||
name: "capacity"
|
||||
type: INT
|
||||
i: 0
|
||||
}
|
||||
}
|
||||
ops {
|
||||
inputs {
|
||||
parameter: "X"
|
||||
arguments: "channel"
|
||||
}
|
||||
type: "go"
|
||||
attrs {
|
||||
name: "sub_block"
|
||||
type: BLOCK
|
||||
block_idx: 1
|
||||
}
|
||||
}
|
||||
ops {
|
||||
inputs {
|
||||
parameter: "Channel"
|
||||
arguments: "channel"
|
||||
}
|
||||
outputs {
|
||||
parameter: "Out"
|
||||
arguments: "return_value"
|
||||
}
|
||||
outputs {
|
||||
parameter: "Status"
|
||||
arguments: "status_recv"
|
||||
}
|
||||
type: "channel_recv"
|
||||
}
|
||||
...
|
||||
}
|
||||
|
||||
blocks {
|
||||
idx: 1
|
||||
parent_idx: 0
|
||||
vars {
|
||||
name: "status"
|
||||
type {
|
||||
type: LOD_TENSOR
|
||||
lod_tensor {
|
||||
tensor {
|
||||
data_type: BOOL
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
...
|
||||
|
||||
ops {
|
||||
outputs {
|
||||
parameter: "Out"
|
||||
arguments: "fill_constant_1.tmp_0"
|
||||
}
|
||||
type: "fill_constant"
|
||||
attrs {
|
||||
name: "force_cpu"
|
||||
type: BOOLEAN
|
||||
b: false
|
||||
}
|
||||
attrs {
|
||||
name: "value"
|
||||
type: FLOAT
|
||||
f: 99.0
|
||||
}
|
||||
attrs {
|
||||
name: "shape"
|
||||
type: INTS
|
||||
ints: 1
|
||||
}
|
||||
attrs {
|
||||
name: "dtype"
|
||||
type: INT
|
||||
i: 3
|
||||
}
|
||||
}
|
||||
ops {
|
||||
inputs {
|
||||
parameter: "Channel"
|
||||
arguments: "channel"
|
||||
}
|
||||
inputs {
|
||||
parameter: "X"
|
||||
arguments: "fill_constant_1.tmp_0"
|
||||
}
|
||||
outputs {
|
||||
parameter: "Status"
|
||||
arguments: "status"
|
||||
}
|
||||
type: "channel_send"
|
||||
attrs {
|
||||
name: "copy"
|
||||
type: BOOLEAN
|
||||
b: false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Current Limitations
|
||||
|
||||
#### <a name="block-captures"></a>Scopes and block captures:
|
||||
|
||||
Paddle utilizes [scopes](./../concepts/scope.md) to store variables used in a
|
||||
block. When a block is executed, a new local scope is created from the parent
|
||||
scope (ie: scope derived from the parent block) and associated with the new
|
||||
child block. After the block finishes executing, then the local scope and
|
||||
all associated variables in the scope is deleted.
|
||||
|
||||
This works well in a single threaded scenario, however with introduction of
|
||||
go_op, a child block may continue to execute even after the parent block has
|
||||
exited. If the go_op tries to access variables located in the parent block's
|
||||
scope, it may receive a segmentation fault because the parent scope may have
|
||||
been deleted.
|
||||
|
||||
We need to implement block closures in order to prevent access to parent
|
||||
scope variables from causing a segmentation fault. As a temporary workaround,
|
||||
please ensure that all variables accessed in the go block is not destructed
|
||||
before it is being accessed. Currently, the go_op will explicitly enforce
|
||||
this requirement and raise an exception if a variable could not be found in
|
||||
the scope.
|
||||
|
||||
Please refer to [Closure issue](https://github.com/PaddlePaddle/Paddle/issues/8502)
|
||||
for more details.
|
||||
|
||||
#### Green Threads
|
||||
|
||||
Golang utilizes `green threads`, which is a mechnism for the runtime library to
|
||||
manage multiple threads (instead of natively by the OS). Green threads usually
|
||||
allows for faster thread creation and switching, as there is less overhead
|
||||
when spawning these threads. For the first version of CSP, we only support
|
||||
OS threads.
|
||||
|
||||
|
||||
#### Backward Propegation:
|
||||
|
||||
go_op currently does not support backwards propagation. Please use go_op with
|
||||
non training operators.
|
@ -0,0 +1,193 @@
|
||||
/* Copyright (c) 2018 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 "mkldnn.hpp"
|
||||
#include "mkldnn_activation_op.h"
|
||||
#include "paddle/fluid/operators/activation_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
using paddle::framework::Tensor;
|
||||
using paddle::platform::MKLDNNDeviceContext;
|
||||
|
||||
namespace {
|
||||
template <typename T, typename ExecContext>
|
||||
void eltwise_forward(const ExecContext &ctx, mkldnn::algorithm algorithm,
|
||||
const T alpha = 0, const T beta = 0) {
|
||||
PADDLE_ENFORCE(paddle::platform::is_cpu_place(ctx.GetPlace()),
|
||||
"It must use CPUPlace.");
|
||||
|
||||
auto &dev_ctx = ctx.template device_context<MKLDNNDeviceContext>();
|
||||
const auto &mkldnn_engine = dev_ctx.GetEngine();
|
||||
|
||||
// get buffers
|
||||
const auto *src = ctx.template Input<Tensor>("X");
|
||||
const auto *src_data = src->template data<T>();
|
||||
|
||||
auto *dst = ctx.template Output<Tensor>("Out");
|
||||
const T *dst_data = dst->template mutable_data<T>(ctx.GetPlace());
|
||||
|
||||
// get memory dim
|
||||
PADDLE_ENFORCE(src->dims().size() == 4,
|
||||
"Input dim must be with 4, i.e. NCHW");
|
||||
std::vector<int> src_tz = framework::vectorize2int(src->dims());
|
||||
|
||||
// create memory description
|
||||
// TODO(kbinias-intel): support more formats
|
||||
auto data_md = platform::MKLDNNMemDesc(src_tz, mkldnn::memory::f32,
|
||||
mkldnn::memory::format::nchw);
|
||||
|
||||
// create memory primitives
|
||||
auto src_memory = mkldnn::memory({data_md, mkldnn_engine}, (void *)src_data);
|
||||
auto dst_memory = mkldnn::memory({data_md, mkldnn_engine}, (void *)dst_data);
|
||||
|
||||
auto forward_desc = mkldnn::eltwise_forward::desc(
|
||||
mkldnn::prop_kind::forward_training, algorithm, data_md, alpha, beta);
|
||||
|
||||
// save prim desc into global device context to be referred in backward path
|
||||
const std::string key = ctx.op().Output("Out");
|
||||
const std::string key_eltwise_pd = key + "@eltwise_pd";
|
||||
auto forward_pd = std::make_shared<mkldnn::eltwise_forward::primitive_desc>(
|
||||
forward_desc, mkldnn_engine);
|
||||
dev_ctx.SetBlob(key_eltwise_pd, forward_pd);
|
||||
|
||||
auto eltwise = mkldnn::eltwise_forward(*forward_pd, src_memory, dst_memory);
|
||||
|
||||
// push primitive to stream and wait until it's executed
|
||||
std::vector<mkldnn::primitive> pipeline = {eltwise};
|
||||
mkldnn::stream(mkldnn::stream::kind::eager).submit(pipeline).wait();
|
||||
}
|
||||
|
||||
template <typename T, typename ExecContext>
|
||||
void eltwise_grad(const ExecContext &ctx, mkldnn::algorithm algorithm,
|
||||
const T alpha = 0, const T beta = 0) {
|
||||
auto &dev_ctx = ctx.template device_context<MKLDNNDeviceContext>();
|
||||
const auto &mkldnn_engine = dev_ctx.GetEngine();
|
||||
|
||||
// get buffers
|
||||
const auto *x = ctx.template Input<Tensor>("X");
|
||||
const auto *src = x->template data<T>();
|
||||
|
||||
auto *dout = ctx.template Input<Tensor>(framework::GradVarName("Out"));
|
||||
const auto *diff_dst = dout->template data<T>();
|
||||
|
||||
auto *dx =
|
||||
ctx.template Output<framework::Tensor>(framework::GradVarName("X"));
|
||||
const T *diff_src = dx->template mutable_data<T>(ctx.GetPlace());
|
||||
|
||||
// get memory dim
|
||||
std::vector<int> src_tz = framework::vectorize2int(x->dims());
|
||||
|
||||
// create memory description
|
||||
auto data_md = platform::MKLDNNMemDesc(src_tz, mkldnn::memory::f32,
|
||||
mkldnn::memory::format::nchw);
|
||||
|
||||
// create memory primitives
|
||||
auto src_memory = mkldnn::memory({data_md, mkldnn_engine}, (void *)src);
|
||||
auto diff_src_memory =
|
||||
mkldnn::memory({data_md, mkldnn_engine}, (void *)diff_src);
|
||||
auto diff_dst_memory =
|
||||
mkldnn::memory({data_md, mkldnn_engine}, (void *)diff_dst);
|
||||
|
||||
auto backward_desc =
|
||||
mkldnn::eltwise_backward::desc(algorithm, data_md, data_md, alpha, beta);
|
||||
|
||||
// retrieve eltwise primitive desc from device context
|
||||
const std::string key = ctx.op().Input("Out");
|
||||
const std::string key_eltwise_pd = key + "@eltwise_pd";
|
||||
const std::shared_ptr<void> forward_pd = dev_ctx.GetBlob(key_eltwise_pd);
|
||||
PADDLE_ENFORCE(forward_pd != nullptr,
|
||||
"Fail to find eltwise_pd in device context");
|
||||
auto *p_forward_pd =
|
||||
static_cast<mkldnn::eltwise_forward::primitive_desc *>(forward_pd.get());
|
||||
|
||||
auto eltwise_bwd_prim_desc = mkldnn::eltwise_backward::primitive_desc(
|
||||
backward_desc, mkldnn_engine, *p_forward_pd);
|
||||
|
||||
auto eltwise_bwd = mkldnn::eltwise_backward(eltwise_bwd_prim_desc, src_memory,
|
||||
diff_dst_memory, diff_src_memory);
|
||||
|
||||
// push primitive to stream and wait until it's executed
|
||||
std::vector<mkldnn::primitive> pipeline = {eltwise_bwd};
|
||||
mkldnn::stream(mkldnn::stream::kind::eager).submit(pipeline).wait();
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
template <typename T, mkldnn::algorithm algorithm>
|
||||
struct MKLDNNActivationFunc : public BaseActivationFunctor<T> {
|
||||
template <typename ExecContext>
|
||||
void operator()(const ExecContext &ctx) const {
|
||||
eltwise_forward<T>(ctx, algorithm);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, mkldnn::algorithm algorithm>
|
||||
struct MKLDNNActivationGradFunc : public BaseActivationFunctor<T> {
|
||||
template <typename ExecContext>
|
||||
void operator()(const ExecContext &ctx) const {
|
||||
eltwise_grad<T>(ctx, algorithm);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
using ReluMkldnnFunctor =
|
||||
MKLDNNActivationFunc<T, mkldnn::algorithm::eltwise_relu>;
|
||||
|
||||
template <typename T>
|
||||
using TanhMkldnnFunctor =
|
||||
MKLDNNActivationFunc<T, mkldnn::algorithm::eltwise_tanh>;
|
||||
|
||||
template <typename T>
|
||||
using SqrtMkldnnFunctor =
|
||||
MKLDNNActivationFunc<T, mkldnn::algorithm::eltwise_sqrt>;
|
||||
|
||||
template <typename T>
|
||||
using AbsMkldnnFunctor =
|
||||
MKLDNNActivationFunc<T, mkldnn::algorithm::eltwise_abs>;
|
||||
|
||||
template <typename T>
|
||||
using ReluMkldnnGradFunctor =
|
||||
MKLDNNActivationGradFunc<T, mkldnn::algorithm::eltwise_relu>;
|
||||
|
||||
template <typename T>
|
||||
using TanhMkldnnGradFunctor =
|
||||
MKLDNNActivationGradFunc<T, mkldnn::algorithm::eltwise_tanh>;
|
||||
|
||||
template <typename T>
|
||||
using SqrtMkldnnGradFunctor =
|
||||
MKLDNNActivationGradFunc<T, mkldnn::algorithm::eltwise_sqrt>;
|
||||
|
||||
template <typename T>
|
||||
using AbsMkldnnGradFunctor =
|
||||
MKLDNNActivationGradFunc<T, mkldnn::algorithm::eltwise_abs>;
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
namespace ops = paddle::operators;
|
||||
|
||||
#define REGISTER_ACTIVATION_MKLDNN_KERNEL(act_type, functor, grad_functor) \
|
||||
REGISTER_OP_KERNEL(act_type, MKLDNN, ::paddle::platform::CPUPlace, \
|
||||
ops::MKLDNNActivationKernel<ops::functor<float>>); \
|
||||
REGISTER_OP_KERNEL( \
|
||||
act_type##_grad, MKLDNN, ::paddle::platform::CPUPlace, \
|
||||
ops::MKLDNNActivationGradKernel<ops::grad_functor<float>>);
|
||||
|
||||
#define FOR_EACH_MKLDNN_KERNEL_FUNCTOR(__macro) \
|
||||
__macro(relu, ReluMkldnnFunctor, ReluMkldnnGradFunctor); \
|
||||
__macro(tanh, TanhMkldnnFunctor, TanhMkldnnGradFunctor); \
|
||||
__macro(sqrt, SqrtMkldnnFunctor, SqrtMkldnnGradFunctor); \
|
||||
__macro(abs, AbsMkldnnFunctor, AbsMkldnnGradFunctor);
|
||||
|
||||
FOR_EACH_MKLDNN_KERNEL_FUNCTOR(REGISTER_ACTIVATION_MKLDNN_KERNEL);
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue