assign_op_test.cc 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.

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 "paddle/fluid/operators/assign_op.h"

#include <gtest/gtest.h>

#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/platform/place.h"
21
#include "paddle/phi/core/ddim.h"
22 23 24

TEST(AssignOp, AssignLoDTensor) {
  paddle::platform::CPUPlace cpu_place;
L
Leo Chen 已提交
25
  phi::CPUContext ctx(cpu_place);
26 27 28 29 30

  paddle::framework::Variable output;
  paddle::operators::AssignFunctor assign_functor(&output, ctx);

  paddle::framework::LoDTensor input;
31
  paddle::framework::DDim in_dims = phi::make_ddim({3, 4});
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  int* in_data = input.mutable_data<int>(in_dims, cpu_place);
  for (int i = 0; i < 12; ++i) {
    in_data[i] = i;
  }

  assign_functor(input);

  auto& out_tensor = output.Get<paddle::framework::LoDTensor>();
  paddle::framework::DDim out_dims = out_tensor.dims();
  EXPECT_EQ(in_dims, out_dims);
  auto* out_data = out_tensor.data<int>();
  for (int i = 0; i < 12; ++i) {
    EXPECT_EQ(i, out_data[i]);
  }
}

TEST(AssignOp, AssignLoDTensorArray) {
  paddle::platform::CPUPlace cpu_place;
L
Leo Chen 已提交
50
  phi::CPUContext ctx(cpu_place);
51 52 53 54 55 56

  paddle::framework::Variable output;
  paddle::operators::AssignFunctor assign_functor(&output, ctx);

  paddle::framework::LoDTensorArray input;
  for (int i = 0; i < 5; ++i) {
57
    paddle::framework::DDim in_dims = phi::make_ddim({i + 1, i + 2});
58 59 60 61 62 63 64 65 66 67 68 69 70
    paddle::framework::LoDTensor lod_tensor;
    float* in_data = lod_tensor.mutable_data<float>(in_dims, cpu_place);
    for (int j = 0; j < (i + 1) * (i + 2); ++j) {
      in_data[j] = static_cast<float>(j);
    }
    input.push_back(lod_tensor);
  }

  assign_functor(input);

  auto& out_array = output.Get<paddle::framework::LoDTensorArray>();
  for (int i = 0; i < 5; ++i) {
    paddle::framework::DDim out_dims = out_array[i].dims();
71
    EXPECT_EQ(phi::make_ddim({i + 1, i + 2}), out_dims);
72 73 74 75 76 77 78 79 80
    const float* out_data = out_array[i].data<float>();
    for (int j = 0; j < (i + 1) * (i + 2); ++j) {
      EXPECT_EQ(static_cast<float>(j), out_data[j]);
    }
  }
}

TEST(AssignOp, AssignSelectedRows) {
  paddle::platform::CPUPlace cpu_place;
L
Leo Chen 已提交
81
  phi::CPUContext ctx(cpu_place);
82 83 84 85 86 87 88

  paddle::framework::Variable output;
  paddle::operators::AssignFunctor assign_functor(&output, ctx);

  std::vector<int64_t> rows{0, 4, 7};
  int64_t height = 10;

89
  phi::SelectedRows input(rows, height);
90 91
  paddle::framework::Tensor* input_tensor = input.mutable_value();

92
  paddle::framework::DDim in_dims = phi::make_ddim({3, 4});
93 94 95 96 97 98 99
  int* in_data = input_tensor->mutable_data<int>(in_dims, cpu_place);
  for (int i = 0; i < 12; ++i) {
    in_data[i] = i;
  }

  assign_functor(input);

100
  auto& out_selected_row = output.Get<phi::SelectedRows>();
101 102 103 104 105 106 107 108 109 110 111 112 113 114
  const paddle::framework::Vector<int64_t>& out_rows = out_selected_row.rows();
  EXPECT_EQ(rows.size(), out_rows.size());
  for (size_t i = 0; i < rows.size(); ++i) {
    EXPECT_EQ(rows[i], out_rows[i]);
  }
  EXPECT_EQ(height, out_selected_row.height());
  const paddle::framework::Tensor& out_tensor = out_selected_row.value();
  paddle::framework::DDim out_dims = out_tensor.dims();
  EXPECT_EQ(in_dims, out_dims);
  auto* out_data = out_tensor.data<int>();
  for (int i = 0; i < 12; ++i) {
    EXPECT_EQ(i, out_data[i]);
  }
}