提交 a2b6978b 编写于 作者: Z zp7 提交者: Yanzhan Yang

fix infershape when cmakelist option CPU&GPU_CL is ON, test=develop (#1880)

上级 05d3b19b
...@@ -27,9 +27,9 @@ void IncrementOp<Dtype, T>::InferShape() const { ...@@ -27,9 +27,9 @@ void IncrementOp<Dtype, T>::InferShape() const {
auto out = this->param_.Out(); auto out = this->param_.Out();
PADDLE_MOBILE_ENFORCE(input->numel() == 1, "input's numel should be 1"); PADDLE_MOBILE_ENFORCE(input->numel() == 1, "input's numel should be 1");
out->Resize(input->dims()); out->Resize(input->dims());
#ifdef PADDLE_MOBILE_CPU if (std::is_same<DeviceType<kCPU>, Dtype>::value) {
out->set_lod(input->lod()); out->set_lod(input->lod());
#endif }
} }
} // namespace operators } // namespace operators
......
...@@ -23,13 +23,12 @@ template <> ...@@ -23,13 +23,12 @@ template <>
bool ElementwiseAddKernel<GPU_CL, float>::Init( bool ElementwiseAddKernel<GPU_CL, float>::Init(
ElementwiseAddParam<GPU_CL> *param) { ElementwiseAddParam<GPU_CL> *param) {
DLOG << "-----init add-----"; DLOG << "-----init add-----";
CLImage *bias = reinterpret_cast<CLImage *>(const_cast<CLImage *> CLImage *bias =
(param->InputY())); reinterpret_cast<CLImage *>(const_cast<CLImage *>(param->InputY()));
if (!bias->isInit()) { if (!bias->isInit()) {
bias->InitNormalCLImage(cl_helper_.CLContext(), bias->InitNormalCLImage(cl_helper_.CLContext(),
this->cl_helper_.CLCommandQueue()); this->cl_helper_.CLCommandQueue());
} }
DLOG << " bias: " << *bias; DLOG << " bias: " << *bias;
if (bias->dims().size() == 4) { if (bias->dims().size() == 4) {
this->cl_helper_.AddKernel("elementwise_add", "elementwise_add_kernel.cl"); this->cl_helper_.AddKernel("elementwise_add", "elementwise_add_kernel.cl");
......
...@@ -23,11 +23,11 @@ template <typename Dtype, typename T> ...@@ -23,11 +23,11 @@ template <typename Dtype, typename T>
void LodResetOp<Dtype, T>::InferShape() const { void LodResetOp<Dtype, T>::InferShape() const {
const auto &input_dims = this->param_.input_x_->dims(); const auto &input_dims = this->param_.input_x_->dims();
this->param_.output_->Resize(input_dims); this->param_.output_->Resize(input_dims);
#ifdef PADDLE_MOBILE_CPU if (std::is_same<DeviceType<kCPU>, Dtype>::value) {
if (this->param_.append) { if (this->param_.append) {
this->param_.output_->set_lod(this->param_.input_x_->lod()); this->param_.output_->set_lod(this->param_.input_x_->lod());
}
} }
#endif
} }
} // namespace operators } // namespace operators
......
...@@ -26,9 +26,9 @@ void OnehotOp<Dtype, T>::InferShape() const { ...@@ -26,9 +26,9 @@ void OnehotOp<Dtype, T>::InferShape() const {
framework::DDim out_dims(x_dims); framework::DDim out_dims(x_dims);
out_dims[out_dims.size() - 1] = depth; out_dims[out_dims.size() - 1] = depth;
this->param_.output_->Resize(out_dims); this->param_.output_->Resize(out_dims);
#ifdef PADDLE_MOBILE_CPU if (std::is_same<DeviceType<kCPU>, Dtype>::value) {
this->param_.output_->set_lod(this->param_.input_->lod()); this->param_.output_->set_lod(this->param_.input_->lod());
#endif }
} }
} // namespace operators } // namespace operators
......
...@@ -14,9 +14,9 @@ limitations under the License. */ ...@@ -14,9 +14,9 @@ limitations under the License. */
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include "common/log.h" #include "common/log.h"
#include "common/type_define.h" #include "common/type_define.h"
#include "common/types.h" #include "common/types.h"
......
...@@ -16,6 +16,7 @@ limitations under the License. */ ...@@ -16,6 +16,7 @@ limitations under the License. */
#include "operators/reduce_prod_op.h" #include "operators/reduce_prod_op.h"
#include <algorithm> #include <algorithm>
#include <vector>
namespace paddle_mobile { namespace paddle_mobile {
namespace operators { namespace operators {
...@@ -64,12 +65,12 @@ void ReduceProdOp<Dtype, T>::InferShape() const { ...@@ -64,12 +65,12 @@ void ReduceProdOp<Dtype, T>::InferShape() const {
} }
auto out_dims = framework::make_ddim(dims_vector); auto out_dims = framework::make_ddim(dims_vector);
this->param_.Output()->Resize(out_dims); this->param_.Output()->Resize(out_dims);
#ifdef PADDLE_MOBILE_CPU if (std::is_same<DeviceType<kCPU>, Dtype>::value) {
if (dims[0] != 0) { if (dims[0] != 0) {
// Only pass LoD when not reducing on the first dim. // Only pass LoD when not reducing on the first dim.
this->param_.Output()->set_lod(this->param_.Input()->lod()); this->param_.Output()->set_lod(this->param_.Input()->lod());
}
} }
#endif
} }
} }
......
...@@ -27,60 +27,60 @@ void Reshape2Op<Dtype, T>::InferShape() const { ...@@ -27,60 +27,60 @@ void Reshape2Op<Dtype, T>::InferShape() const {
} }
auto &shape = this->param_.Shape(); auto &shape = this->param_.Shape();
auto input_x_dims = this->param_.InputX()->dims(); auto input_x_dims = this->param_.InputX()->dims();
#ifdef PADDLE_MOBILE_CL
auto input_dim_size = input_x_dims.size();
bool shouldResize = true; bool shouldResize = true;
if (input_dim_size > 4) { if (std::is_same<DeviceType<kGPU_CL>, Dtype>::value) {
for (int i = 0; i < input_dim_size - 4; ++i) { auto input_dim_size = input_x_dims.size();
if (input_x_dims[i] != 0 && input_x_dims[i] != 1) { if (input_dim_size > 4) {
shouldResize = false; for (int i = 0; i < input_dim_size - 4; ++i) {
break; if (input_x_dims[i] != 0 && input_x_dims[i] != 1) {
shouldResize = false;
break;
}
} }
} if (shouldResize) {
if (shouldResize) { std::vector<int64_t> temp_intput_dims;
std::vector<int64_t> temp_intput_dims; temp_intput_dims.reserve(static_cast<size_t>(4));
temp_intput_dims.reserve(static_cast<size_t>(4)); for (int i = input_dim_size - 4; i < input_dim_size; ++i) {
for (int i = input_dim_size - 4; i < input_dim_size; ++i) { temp_intput_dims.push_back(input_x_dims[i]);
temp_intput_dims.push_back(input_x_dims[i]); }
framework::DDim temp_ddim = framework::make_ddim(temp_intput_dims);
this->param_.InputX()->Resize(temp_ddim);
input_x_dims = this->param_.InputX()->dims();
} }
framework::DDim temp_ddim = framework::make_ddim(temp_intput_dims);
this->param_.InputX()->Resize(temp_ddim);
input_x_dims = this->param_.InputX()->dims();
} }
} }
#endif
auto out_dims = ValidateShape(shape, input_x_dims); auto out_dims = ValidateShape(shape, input_x_dims);
this->param_.Out()->Resize(out_dims); this->param_.Out()->Resize(out_dims);
#ifdef PADDLE_MOBILE_CL if (std::is_same<DeviceType<kGPU_CL>, Dtype>::value) {
input_x_dims = this->param_.InputX()->dims(); input_x_dims = this->param_.InputX()->dims();
shouldResize = true; shouldResize = true;
if (out_dims.size() > 4) { if (out_dims.size() > 4) {
for (int i = 0; i < out_dims.size() - 4; ++i) { for (int i = 0; i < out_dims.size() - 4; ++i) {
if (out_dims[i] != 0 && out_dims[i] != 1) { if (out_dims[i] != 0 && out_dims[i] != 1) {
shouldResize = false; shouldResize = false;
break; break;
}
} }
} if (shouldResize) {
if (shouldResize) { std::vector<int64_t> temp_output_dims;
std::vector<int64_t> temp_output_dims; temp_output_dims.reserve(static_cast<size_t>(4));
temp_output_dims.reserve(static_cast<size_t>(4)); for (int i = out_dims.size() - 4; i < out_dims.size(); ++i) {
for (int i = out_dims.size() - 4; i < out_dims.size(); ++i) { temp_output_dims.push_back(out_dims[i]);
temp_output_dims.push_back(out_dims[i]); }
framework::DDim temp_ddim = framework::make_ddim(temp_output_dims);
this->param_.Out()->Resize(temp_ddim);
} }
framework::DDim temp_ddim = framework::make_ddim(temp_output_dims);
this->param_.Out()->Resize(temp_ddim);
} }
} }
#endif
std::vector<int64_t> xshape_dims(input_x_dims.size() + 1, 0); std::vector<int64_t> xshape_dims(input_x_dims.size() + 1, 0);
for (int i = 0; i < input_x_dims.size(); ++i) { for (int i = 0; i < input_x_dims.size(); ++i) {
xshape_dims[i + 1] = input_x_dims[i]; xshape_dims[i + 1] = input_x_dims[i];
} }
this->param_.OutputXShape()->Resize(framework::make_ddim(xshape_dims)); this->param_.OutputXShape()->Resize(framework::make_ddim(xshape_dims));
#ifdef PADDLE_MOBILE_CL if (std::is_same<DeviceType<kGPU_CL>, Dtype>::value) {
this->param_.OutputXShape()->Resize(input_x_dims); this->param_.OutputXShape()->Resize(input_x_dims);
#endif }
} }
} // namespace operators } // namespace operators
......
...@@ -26,37 +26,39 @@ void SliceOp<Dtype, T>::InferShape() const { ...@@ -26,37 +26,39 @@ void SliceOp<Dtype, T>::InferShape() const {
auto axes = this->param_.axes_; auto axes = this->param_.axes_;
auto input = this->param_.input_; auto input = this->param_.input_;
auto output = this->param_.output_; auto output = this->param_.output_;
#ifdef PADDLE_MOBILE_CL if (std::is_same<DeviceType<kGPU_CL>, Dtype>::value) {
auto output_dims = output->dims(); auto output_dims = output->dims();
auto output_dims_size = output_dims.size(); auto output_dims_size = output_dims.size();
bool should_resize = true; bool should_resize = true;
if (output_dims_size > 4) { if (output_dims_size > 4) {
for (int i = 0; i < output_dims_size - 4; ++i) { for (int i = 0; i < output_dims_size - 4; ++i) {
if (output_dims[i] != 0 && output_dims[i] != 1) { if (output_dims[i] != 0 && output_dims[i] != 1) {
should_resize = false; should_resize = false;
break; break;
}
} }
} if (should_resize) {
if (should_resize) { std::vector<int64_t> temp_output_dims;
std::vector<int64_t> temp_output_dims; temp_output_dims.reserve(static_cast<size_t>(4));
temp_output_dims.reserve(static_cast<size_t>(4)); for (int i = output_dims_size - 4; i < output_dims_size; ++i) {
for (int i = output_dims_size - 4; i < output_dims_size; ++i) { temp_output_dims.push_back(output_dims[i]);
temp_output_dims.push_back(output_dims[i]); }
framework::DDim temp_ddim = framework::make_ddim(temp_output_dims);
this->param_.output_->Resize(temp_ddim);
} }
framework::DDim temp_ddim = framework::make_ddim(temp_output_dims);
this->param_.output_->Resize(temp_ddim);
} }
} }
#endif
PADDLE_MOBILE_ENFORCE(axes.size() == 1, "axes size should equals 1"); PADDLE_MOBILE_ENFORCE(axes.size() == 1, "axes size should equals 1");
PADDLE_MOBILE_ENFORCE(input->dims().size() == output->dims().size(), PADDLE_MOBILE_ENFORCE(input->dims().size() == output->dims().size(),
"input dim size should equals output dim size"); "input dim size should equals output dim size");
PADDLE_MOBILE_ENFORCE( if (std::is_same<DeviceType<kGPU_CL>, Dtype>::value) {
output->dims().size() - PADDLE_MOBILE_ENFORCE(
(axes[0] - (this->param_.original_output_dims_size_ - output->dims().size() -
this->param_.output_->dims().size())) == (axes[0] - (this->param_.original_output_dims_size_ -
3, this->param_.output_->dims().size())) ==
"op only support slice channel now"); 3,
"op only support slice channel now");
}
auto starts = this->param_.starts_; auto starts = this->param_.starts_;
auto ends = this->param_.ends_; auto ends = this->param_.ends_;
framework::DDim out_dims(input->dims()); framework::DDim out_dims(input->dims());
...@@ -82,11 +84,13 @@ void SliceOp<Dtype, T>::InferShape() const { ...@@ -82,11 +84,13 @@ void SliceOp<Dtype, T>::InferShape() const {
} }
} }
output->Resize(out_dims); output->Resize(out_dims);
#if !defined(PADDLE_MOBILE_CL) && defined(PADDLE_MOBILE_CPU) if (std::is_same<DeviceType<kCPU>, Dtype>::value) {
if (axes[0] != 0) { LoDTensor *output_lod = reinterpret_cast<LoDTensor *>(output);
output->set_lod(input->lod()); LoDTensor *input_lod = reinterpret_cast<LoDTensor *>(input);
if (axes[0] != 0) {
output_lod->set_lod(input_lod->lod());
}
} }
#endif
} }
} // namespace operators } // namespace operators
......
...@@ -19,18 +19,18 @@ limitations under the License. */ ...@@ -19,18 +19,18 @@ limitations under the License. */
namespace paddle_mobile { namespace paddle_mobile {
namespace operators { namespace operators {
template <typename DeviceType, typename T> template <typename Dtype, typename T>
void TopKOp<DeviceType, T>::InferShape() const { void TopKOp<Dtype, T>::InferShape() const {
const int k = this->param_.k_; const int k = this->param_.k_;
auto dims = this->param_.input_->dims(); auto dims = this->param_.input_->dims();
// should check k <= dims[-1] && k >= 1 // should check k <= dims[-1] && k >= 1
dims[dims.size() - 1] = k; dims[dims.size() - 1] = k;
this->param_.output_->Resize(dims); this->param_.output_->Resize(dims);
this->param_.indices_->Resize(dims); this->param_.indices_->Resize(dims);
#ifdef PADDLE_MOBILE_CPU if (std::is_same<DeviceType<kCPU>, Dtype>::value) {
this->param_.output_->set_lod(this->param_.input_->lod()); this->param_.output_->set_lod(this->param_.input_->lod());
this->param_.indices_->set_lod(this->param_.input_->lod()); this->param_.indices_->set_lod(this->param_.input_->lod());
#endif }
} }
} // namespace operators } // namespace operators
......
...@@ -29,54 +29,54 @@ void Transpose2Op<Dtype, T>::InferShape() const { ...@@ -29,54 +29,54 @@ void Transpose2Op<Dtype, T>::InferShape() const {
size_t x_dims_size = input_x_dims.size(); size_t x_dims_size = input_x_dims.size();
size_t axis_size = axis.size(); size_t axis_size = axis.size();
#ifdef PADDLE_MOBILE_CL if (std::is_same<DeviceType<kGPU_CL>, Dtype>::value) {
bool shouldResize = true; bool shouldResize = true;
int diff_dim = 0; int diff_dim = 0;
if (axis_size > 4) { if (axis_size > 4) {
for (int i = 0; i < axis_size - 4; ++i) { for (int i = 0; i < axis_size - 4; ++i) {
if (axis[i] != i) { if (axis[i] != i) {
shouldResize = false; shouldResize = false;
break; break;
} else { } else {
diff_dim++; diff_dim++;
}
} }
} if (shouldResize) {
if (shouldResize) { std::vector<int> temp_axis_dims;
std::vector<int> temp_axis_dims; temp_axis_dims.reserve(static_cast<size_t>(4));
temp_axis_dims.reserve(static_cast<size_t>(4)); for (int i = axis_size - 4; i < axis_size; ++i) {
for (int i = axis_size - 4; i < axis_size; ++i) { temp_axis_dims.push_back(axis[i] - diff_dim);
temp_axis_dims.push_back(axis[i] - diff_dim); }
axis.resize(4);
axis.clear();
axis.insert(axis.begin(), temp_axis_dims.begin(), temp_axis_dims.end());
} }
axis.resize(4);
axis.clear();
axis.insert(axis.begin(), temp_axis_dims.begin(), temp_axis_dims.end());
} }
}
auto input_dim_size = input_x_dims.size(); auto input_dim_size = input_x_dims.size();
shouldResize = true; shouldResize = true;
if (input_dim_size > 4) { if (input_dim_size > 4) {
for (int i = 0; i < input_dim_size - 4; ++i) { for (int i = 0; i < input_dim_size - 4; ++i) {
if (input_x_dims[i] != 0 && input_x_dims[i] != 1) { if (input_x_dims[i] != 0 && input_x_dims[i] != 1) {
shouldResize = false; shouldResize = false;
break; break;
}
} }
} if (shouldResize) {
if (shouldResize) { std::vector<int64_t> temp_intput_dims;
std::vector<int64_t> temp_intput_dims; temp_intput_dims.reserve(static_cast<size_t>(4));
temp_intput_dims.reserve(static_cast<size_t>(4)); for (int i = input_dim_size - 4; i < input_dim_size; ++i) {
for (int i = input_dim_size - 4; i < input_dim_size; ++i) { temp_intput_dims.push_back(input_x_dims[i]);
temp_intput_dims.push_back(input_x_dims[i]); }
framework::DDim temp_ddim = framework::make_ddim(temp_intput_dims);
this->param_.InputX()->Resize(temp_ddim);
} }
framework::DDim temp_ddim = framework::make_ddim(temp_intput_dims);
this->param_.InputX()->Resize(temp_ddim);
} }
}
axis_size = axis.size(); axis_size = axis.size();
input_x_dims = this->param_.InputX()->dims(); input_x_dims = this->param_.InputX()->dims();
x_dims_size = input_x_dims.size(); x_dims_size = input_x_dims.size();
#endif }
PADDLE_MOBILE_ENFORCE((x_dims_size == axis_size), PADDLE_MOBILE_ENFORCE((x_dims_size == axis_size),
"input_dims must " "input_dims must "
...@@ -100,9 +100,9 @@ void Transpose2Op<Dtype, T>::InferShape() const { ...@@ -100,9 +100,9 @@ void Transpose2Op<Dtype, T>::InferShape() const {
xshape_dims[i + 1] = input_x_dims[i]; xshape_dims[i + 1] = input_x_dims[i];
} }
this->param_.OutputXShape()->Resize(framework::make_ddim(xshape_dims)); this->param_.OutputXShape()->Resize(framework::make_ddim(xshape_dims));
#ifdef PADDLE_MOBILE_CL if (std::is_same<DeviceType<kGPU_CL>, Dtype>::value) {
this->param_.OutputXShape()->Resize(input_x_dims); this->param_.OutputXShape()->Resize(input_x_dims);
#endif }
} }
} // namespace operators } // namespace operators
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册