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