|
|
|
@ -131,29 +131,22 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> {
|
|
|
|
|
if (axis != -1 && axis != rank - 1) {
|
|
|
|
|
X_trans.mutable_data<T>(framework::make_ddim(shape), ctx.GetPlace());
|
|
|
|
|
Out_trans.mutable_data<T>(framework::make_ddim(shape), ctx.GetPlace());
|
|
|
|
|
TransCompute<MKLDNNDeviceContext, T>(rank, dev_ctx, *X, &X_trans, perm);
|
|
|
|
|
TransCompute<MKLDNNDeviceContext, T>(rank, dev_ctx, *Out, &Out_trans, perm);
|
|
|
|
|
X_2d = framework::ReshapeToMatrix(X_trans, rank - 1);
|
|
|
|
|
Out_2d = framework::ReshapeToMatrix(Out_trans, rank - 1);
|
|
|
|
|
TransCompute<platform::CPUDeviceContext, T>(rank, dev_ctx, *X, &X_trans, perm);
|
|
|
|
|
TransCompute<platform::CPUDeviceContext, T>(rank, dev_ctx, *Out, &Out_trans, perm);
|
|
|
|
|
auto dims = X_trans.dims();
|
|
|
|
|
auto flattened_dims = framework::flatten_to_2d(dims, dims.size() - 1);
|
|
|
|
|
X_2d.ShareDataWith(X_trans).Resize(flattened_dims);
|
|
|
|
|
Out_2d.ShareDataWith(Out_trans).Resize(flattened_dims);
|
|
|
|
|
} else {
|
|
|
|
|
X_2d = framework::ReshapeToMatrix(*X, rank - 1);
|
|
|
|
|
Out_2d = framework::ReshapeToMatrix(*Out, rank - 1);
|
|
|
|
|
auto dims = X->dims();
|
|
|
|
|
auto flattened_dims = framework::flatten_to_2d(dims, dims.size() - 1);
|
|
|
|
|
X_2d.ShareDataWith(*X).Resize(flattened_dims);
|
|
|
|
|
Out_2d.ShareDataWith(*Out).Resize(flattened_dims);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// flatten input and output to 2-D matrixs
|
|
|
|
|
// auto dims = input->dims(); // input and output share the same shape
|
|
|
|
|
// auto flattened_dims = framework::flatten_to_2d(dims, dims.size() - 1);
|
|
|
|
|
// framework::Tensor flattened_input;
|
|
|
|
|
// framework::Tensor flattened_output;
|
|
|
|
|
// flattened_input.ShareDataWith(*input).Resize(flattened_dims);
|
|
|
|
|
// flattened_output.ShareDataWith(*output).Resize(flattened_dims);
|
|
|
|
|
|
|
|
|
|
// const T* input_data = flattened_input.data<T>();
|
|
|
|
|
// T* output_data = flattened_output.mutable_data<T>(ctx.GetPlace());
|
|
|
|
|
const T* input_data = X_2d.data<T>();
|
|
|
|
|
T* output_data = Out_2d.mutable_data<T>(ctx.GetPlace());
|
|
|
|
|
|
|
|
|
|
// std::vector<int> src_tz = paddle::framework::vectorize2int(flattened_dims);
|
|
|
|
|
std::vector<int> src_tz = paddle::framework::vectorize2int(X_2d.dims());
|
|
|
|
|
std::vector<int> dst_tz = src_tz;
|
|
|
|
|
// Same memory descriptor to be used for input and output
|
|
|
|
@ -184,10 +177,16 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> {
|
|
|
|
|
// We cannot use softmax_dst_memory_p to get prim desc as
|
|
|
|
|
// it contains flattened dims (2D) while output tensor can
|
|
|
|
|
// have 2,3,4+ dims
|
|
|
|
|
auto output_mem_pd = paddle::platform::create_prim_desc_from_dims(
|
|
|
|
|
paddle::framework::vectorize2int(output->dims()),
|
|
|
|
|
mkldnn::memory::format::blocked);
|
|
|
|
|
output->set_mkldnn_prim_desc(output_mem_pd);
|
|
|
|
|
if (axis != -1 && axis != rank - 1) {
|
|
|
|
|
auto output_mem_pd = paddle::platform::create_prim_desc_from_dims(
|
|
|
|
|
shape, mkldnn::memory::format::blocked);
|
|
|
|
|
Out_trans.set_mkldnn_prim_desc(output_mem_pd);
|
|
|
|
|
} else {
|
|
|
|
|
auto output_mem_pd = paddle::platform::create_prim_desc_from_dims(
|
|
|
|
|
paddle::framework::vectorize2int(Out->dims()),
|
|
|
|
|
mkldnn::memory::format::blocked);
|
|
|
|
|
Out->set_mkldnn_prim_desc(output_mem_pd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<primitive> pipeline{
|
|
|
|
|
*(static_cast<softmax_forward::primitive*>(softmax_p.get()))};
|
|
|
|
@ -203,7 +202,7 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel<T> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (axis != -1 && axis != rank - 1) {
|
|
|
|
|
TransCompute<MKLDNNDeviceContext, T>(rank, dev_ctx, Out_trans, Out, perm);
|
|
|
|
|
TransCompute<platform::CPUDeviceContext, T>(rank, dev_ctx, Out_trans, Out, perm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -242,30 +241,22 @@ class SoftmaxMKLDNNGradKernel : public paddle::framework::OpKernel<T> {
|
|
|
|
|
dX_trans.mutable_data<T>(framework::make_ddim(shape), ctx.GetPlace());
|
|
|
|
|
Out_trans.mutable_data<T>(framework::make_ddim(shape), ctx.GetPlace());
|
|
|
|
|
dOut_trans.mutable_data<T>(framework::make_ddim(shape), ctx.GetPlace());
|
|
|
|
|
TransCompute<MKLDNNDeviceContext, T>(rank, dev_ctx, *dX, &dX_trans, perm);
|
|
|
|
|
TransCompute<MKLDNNDeviceContext, T>(rank, dev_ctx, *Out, &Out_trans, perm);
|
|
|
|
|
TransCompute<MKLDNNDeviceContext, T>(rank, dev_ctx, *dOut, &dOut_trans, perm);
|
|
|
|
|
dX_2d = framework::ReshapeToMatrix(dX_trans, rank - 1);
|
|
|
|
|
Out_2d = framework::ReshapeToMatrix(Out_trans, rank - 1);
|
|
|
|
|
dOut_2d = framework::ReshapeToMatrix(dOut_trans, rank - 1);
|
|
|
|
|
TransCompute<platform::CPUDeviceContext, T>(rank, dev_ctx, *dX, &dX_trans, perm);
|
|
|
|
|
TransCompute<platform::CPUDeviceContext, T>(rank, dev_ctx, *Out, &Out_trans, perm);
|
|
|
|
|
TransCompute<platform::CPUDeviceContext, T>(rank, dev_ctx, *dOut, &dOut_trans, perm);
|
|
|
|
|
auto dims = dX_trans.dims();
|
|
|
|
|
auto flattened_dims = framework::flatten_to_2d(dims, dims.size() - 1);
|
|
|
|
|
dX_2d.ShareDataWith(dX_trans).Resize(flattened_dims);
|
|
|
|
|
Out_2d.ShareDataWith(Out_trans).Resize(flattened_dims);
|
|
|
|
|
dOut_2d.ShareDataWith(dOut_trans).Resize(flattened_dims);
|
|
|
|
|
} else {
|
|
|
|
|
dX_2d = framework::ReshapeToMatrix(*dX, rank - 1);
|
|
|
|
|
Out_2d = framework::ReshapeToMatrix(*Out, rank - 1);
|
|
|
|
|
dOut_2d = framework::ReshapeToMatrix(*dOut, rank - 1);
|
|
|
|
|
auto dims = dX->dims();
|
|
|
|
|
auto flattened_dims = framework::flatten_to_2d(dims, dims.size() - 1);
|
|
|
|
|
dX_2d.ShareDataWith(*dX).Resize(flattened_dims);
|
|
|
|
|
Out_2d.ShareDataWith(*Out).Resize(flattened_dims);
|
|
|
|
|
dOut_2d.ShareDataWith(*dOut).Resize(flattened_dims);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// auto dims = dout->dims(); // input and output share the same shape
|
|
|
|
|
// auto flattened_dims = framework::flatten_to_2d(dims, dims.size() - 1);
|
|
|
|
|
// framework::Tensor flattened_output;
|
|
|
|
|
// framework::Tensor flattened_dout;
|
|
|
|
|
// framework::Tensor flattened_dx;
|
|
|
|
|
// flattened_output.ShareDataWith(*output).Resize(flattened_dims);
|
|
|
|
|
// flattened_dout.ShareDataWith(*dout).Resize(flattened_dims);
|
|
|
|
|
// flattened_dx.ShareDataWith(*dx).Resize(flattened_dims);
|
|
|
|
|
|
|
|
|
|
// const T* dst_data = flattened_output.data<T>();
|
|
|
|
|
// const T* diff_dst_ptr = flattened_dout.template data<T>();
|
|
|
|
|
// T* diff_src_ptr = flattened_dx.template mutable_data<T>(ctx.GetPlace());
|
|
|
|
|
const T* dst_data = Out_2d.data<T>();
|
|
|
|
|
const T* diff_dst_ptr = dOut_2d.template data<T>();
|
|
|
|
|
T* diff_src_ptr = dX_2d.template mutable_data<T>(ctx.GetPlace());
|
|
|
|
@ -317,7 +308,7 @@ class SoftmaxMKLDNNGradKernel : public paddle::framework::OpKernel<T> {
|
|
|
|
|
stream(stream::kind::eager).submit(pipeline).wait();
|
|
|
|
|
|
|
|
|
|
if (axis != -1 && axis != rank - 1) {
|
|
|
|
|
TransCompute<MKLDNNDeviceContext, T>(rank, dev_ctx, dX_trans, dX, perm);
|
|
|
|
|
TransCompute<platform::CPUDeviceContext, T>(rank, dev_ctx, dX_trans, dX, perm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|