提交 7f2b4391 编写于 作者: C chenjiaoAngel

fiix build error, test=develop

上级 1391e255
......@@ -32,10 +32,12 @@ void seq_pool_sum<float>(const float* din,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
LOG(INFO) << "size: " << lod.size() - 1;
for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
const float* din_ptr = din + lod[i] * width;
float* dout_ptr = dout + i * width;
int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
if (height > 0) {
if (width == 1) {
float sum = 0.f;
for (int h = 0; h < height; ++h) {
......@@ -46,6 +48,16 @@ void seq_pool_sum<float>(const float* din,
memcpy(dout_ptr, din_ptr, width * sizeof(float));
din_ptr += width;
height = height - 1;
/* for (int h = 0; h < height; h++) {
for (int w = 0; w < width; ++w) {
dout_ptr[w] += din_ptr[w];
}
din_ptr += width;
}
*/
// continue;
if (height == 0) return;
int cnt_w = width >> 2;
int remain_w = width & 3;
int cnt_h = height >> 2;
......@@ -103,6 +115,8 @@ void seq_pool_sum<float>(const float* din,
}
}
}
}
printf("end--\n");
}
template <>
......
......@@ -33,11 +33,12 @@ void seq_pool_sum_grad<float>(const float* din,
float* dout,
const std::vector<uint64_t> lod,
int64_t width) {
for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
for (int i = 0; i < static_cast<int>(lod.size()) - 1; i++) {
int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
const float* din_ptr = din + lod[i] * width;
const float* din_grad_ptr = din + i * width;
const float* din_grad_ptr = din_grad + i * width;
float* dout_ptr = dout + lod[i] * width;
if (height > 0) {
if (width == 1) {
for (int h = 0; h < height; ++h) {
dout_ptr[h] = din_grad_ptr[h];
......@@ -52,6 +53,7 @@ void seq_pool_sum_grad<float>(const float* din,
}
}
}
}
}
template <>
......@@ -63,7 +65,7 @@ void seq_pool_average_grad<float>(const float* din,
for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
const float* din_ptr = din + lod[i] * width;
const float* din_grad_ptr = din + i * width;
const float* din_grad_ptr = din_grad + i * width;
float* dout_ptr = dout + lod[i] * width;
float alpha = 1.0 / height;
if (height > 0) {
......@@ -93,7 +95,7 @@ void seq_pool_sqrt_grad<float>(const float* din,
for (int i = 0; i < static_cast<int>(lod.size()) - 1; ++i) {
int64_t height = static_cast<int64_t>(lod[i + 1] - lod[i]);
const float* din_ptr = din + lod[i] * width;
const float* din_grad_ptr = din + i * width;
const float* din_grad_ptr = din_grad + i * width;
float* dout_ptr = dout + lod[i] * width;
float alpha = 1.0 / sqrtf(height);
if (height > 0) {
......
......@@ -33,14 +33,12 @@ void SequencePoolGradCompute::Run() {
auto& x_grad = param.X_Grad;
const auto* din_ptr = param.X->data<float>();
const auto* dout_grad_ptr = output_grad->data<float>();
float* x_grad_ptr = x_grad->mutable_dataa<float>();
float* x_grad_ptr = x_grad->mutable_data<float>();
const auto pool_type = param.pool_type;
const auto lod = param.X->lod()[0];
int64_t width = param.X->numel() / param.X->dims()[0];
if (pool_type == "SUM" || pool_type == "MAX" || pool_type == "MIN") {
lite::arm::math::seq_pool_grad(din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
lite::arm::math::seq_pool_sum_grad(din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
} else if (pool_type == "AVERAGE") {
lite::arm::math::seq_pool_average_grad(din_ptr, dout_grad_ptr, x_grad_ptr, lod, width);
} else if (pool_type == "SQRT") {
......
......@@ -20,14 +20,21 @@
namespace paddle {
namespace lite {
namespace kernels {
namespace arm {
using param_t = operators::SequencePoolParam;
using grad_param_t = operators::SequencePoolGradParam;
using kernel_t = SequencePoolCompute;
using grad_kernel_t = SequencePoolGradCompute;
template <class kernel_t, class grad_kernel_t>
class SequencePoolGradTester {
public:
explicit SequencePoolGradTester(DDim dims) : dims_(dims) {}
explicit SequencePoolGradTester(DDim dims,
std::vector<std::vector<uint64_t>> lod,
std::string pool_type)
: dims_(dims),
lod_(lod),
pool_type_(pool_type) {}
void prepare_kernel() {
std::unique_ptr<KernelContext> ctx1(new KernelContext);
......@@ -43,18 +50,6 @@ class SequencePoolGradTester {
grad_kernel_.SetContext(std::move(ctx3));
}
void generate_lod(int seq_num,
int max_len,
std::vector<uint64_t>& seq_offset) { // NOLINT
seq_offset.clear();
int sum = 0;
seq_offset.push_back(sum);
for (int i = 0; i < seq_num; i++) {
sum += std::rand() % max_len + 1;
seq_offset.push_back(uint64_t(sum));
}
}
void run_forward(param_t* param,
kernel_t* kernel,
const std::vector<float>& in_vec,
......@@ -67,13 +62,12 @@ class SequencePoolGradTester {
for (int i = 0; i < dims_.production(); i++) {
x_data[i] = in_vec[i];
}
x->set_lod(lod_);
x.set_lod(lod_);
param->X = &x;
param->pool_type = pool_type_;
param->Out = &output;
kernel->SetParam(*param);
kernel->Launch();
auto* output_data = output.mutable_data<float>();
for (int i = 0; i < output.numel(); i++) {
out_vec[i] = output_data[i];
......@@ -83,38 +77,32 @@ class SequencePoolGradTester {
void run_backward(grad_param_t* param,
grad_kernel_t* kernel,
const std::vector<float>& in_vec,
const std::vector<float>& out_vec,
const std::vector<float>& out_grad_vec,
float* in_grad_vec) {
Tensor x;
Tensor out;
Tensor x_grad;
Tensor out_grad;
x.Resize(dims_);
x_grad.Resize(dims_);
x.set_lod(lod_);
// backword
out_grad.Resize(out_dims_);
out.Resize(out_dims_);
auto* x_data = x.mutable_data<float>();
auto* out_data = out.mutable_data<float>();
auto* out_grad_data = out_grad.mutable_data<float>();
for (int i = 0; i < dims_.production(); i++) {
x_data[i] = in_vec[i];
}
for (int i = 0; i < out_dims_.production(); i++) {
out_data[i] = out_vec[i];
out_grad_data[i] = out_grad_vec[i];
}
param->X = &x;
param->Out = &out;
param->X_grad = &x_grad;
param->Out_grad = &out_grad;
param->X_Grad = &x_grad;
param->Out_Grad = &out_grad;
param->pool_type = pool_type_;
kernel->SetParam(*param);
kernel->Launch();
auto* x_grad_data = x_grad.mutable_data<float>();
auto* x_grad_data = x_grad.data<float>();
for (int i = 0; i < dims_.production(); i++) {
in_grad_vec[i] = x_grad_data[i];
}
......@@ -131,38 +119,21 @@ class SequencePoolGradTester {
static_cast<float>(i % 19 - 10.0) / 10.0 * 0.333 +
static_cast<float>(i % 39 - 20.0) / 20.0 * 0.333 + 0.001213;
}
LOG(INFO) << "run_forward:";
this->run_forward(&param_, &kernel_, x, out.data());
std::vector<float> out_grad(out_dims_.production());
std::vector<float> x_grad(dims_.production());
std::vector<float> x_delta(dims_.production());
std::vector<float> out_delta(out_dims_.production());
for (int i = 0; i < out_dims_.production(); i++) {
out_grad[i] = 1.0;
x_grad[i] = 1.0;
}
LOG(INFO) << "run_backward:";
this->run_backward(
&grad_param_, &grad_kernel_, x, out, out_grad, x_grad.data());
for (int i = 0; i < dims_.production(); i++) {
for (int j = 0; j < dims_.production(); j++) {
if (i == j) {
x_delta[j] = x[j] + delta;
} else {
x_delta[j] = x[j];
}
}
this->run_forward(
&delta_param_, &delta_kernel_, x_delta, out_delta.data());
float sum = 0;
for (int j = 0; j < out_dims_.production(); j++) {
sum += (out_delta[j] - out[j]);
}
EXPECT_NEAR(x_grad[i], sum / delta, max_grad_delta);
}
}
&grad_param_, &grad_kernel_, x, out_grad, x_grad.data());
}
private:
DDim dims_;
......@@ -177,6 +148,18 @@ class SequencePoolGradTester {
grad_param_t grad_param_;
};
void generate_lod(int seq_num,
int max_len,
std::vector<uint64_t>& seq_offset) { // NOLINT
seq_offset.clear();
int sum = 0;
seq_offset.push_back(sum);
for (int i = 0; i < seq_num; i++) {
sum += std::rand() % max_len + 1;
seq_offset.push_back(uint64_t(sum));
}
}
void TestSequencePoolGrad(DDim dims, std::vector<std::vector<uint64_t>> lod, std::string pool_type) {
LOG(INFO) << "Test SequencePool grad";
std::unique_ptr<SequencePoolGradTester> tester(new SequencePoolGradTester(
......@@ -190,24 +173,39 @@ void TestSequencePoolGrad(DDim dims, std::vector<std::vector<uint64_t>> lod, std
TEST(sequence_pool_grad_host, compute) {
int max_len = 2;
DeviceInfo::Init();
for (auto seq_num : {1, 3, 5}) {
for (auto c : {2, 9}) {
for (auto h : {2, 1}) {
for (auto w : {2, 10}) {
for (auto c : {2, 4}) {
for (auto h : {1, 3, 4}) {
for (auto w : {1, 3, 4}) {
for (auto pool_type :
{"SUM", "AVERAGE", "SQRT", "MAX", "MIN", "FIRST", "LAST"}) {
for (auto seq_num : {1, 3, 5}) {
std::vector<std::vector<uint64_t>> lod;
lod.resize(1);
generate_lod(seq_num, max_len, lod[0]);
x.set_lod(lod);
int64_t n = int64_t(lod[0].back());
LOG(INFO) << "sequence_pool_grad parameter: "
<< ", n = "
<< n
<< ", c = "
<< c
<< ", h = "
<< h
<< ", w = "
<< w
<< ", seq_num = "
<< seq_num
<< ", pool_type = "
<< pool_type;
TestSequencePoolGrad(DDim(std::vector<int64_t>({n, c, h, w})), lod, pool_type);
}
}
}
}
}
}
} // namespace arm
} // namespace kernels
} // namespace lite
} // namespace paddle
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册