test_group.cc 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Copyright (c) 2020 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 <memory>
#include <ostream>
#include <sstream>
#include <string>

#include "glog/logging.h"
#include "gtest/gtest.h"

#include "paddle/fluid/imperative/reducer.h"

namespace paddle {
namespace imperative {

TEST(TestGroup, TestPrintGroupMessage) {
  Group group;
  std::stringstream stream1, stream2;
  stream1 << group;
  ASSERT_STREQ(stream1.str().c_str(),
33
               "numel: 0 ;is_sparse: 0 ;var number: 0\n[]\n");
34 35 36 37 38 39 40 41 42 43

  std::vector<size_t> vars;
  size_t vars_num = 102;
  for (size_t i = 0; i < vars_num; ++i) {
    vars.push_back(i);
  }
  group.variable_indices_ = vars;
  group.all_length_ = 102;
  group.is_sparse_ = false;

44
  std::string head = "numel: 102 ;is_sparse: 0 ;var number: 102\n";
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  head = head + "[";
  auto begin = vars.begin();
  auto end = vars.end();
  for (int i = 0; begin != end && i < 100; ++i, ++begin) {
    if (i > 0) head += ' ';
    head += std::to_string(*begin);
  }
  if (begin != end) {
    head += " ...";
  }
  head += "]\n";
  stream2 << group;
  ASSERT_STREQ(stream2.str().c_str(), head.c_str());
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
template <typename T, typename Place>
void GroupConcatSplit(Place place, size_t size) {
  platform::CPUPlace cpu_place;
  Group group;

  // [[0.0], [0.0, 1.0], [0.0, 1.0, 2.0] .. ]
  std::vector<framework::Variable> vars;
  vars.resize(size);
  for (size_t i = 0; i < size; ++i) {
    auto len = i + 1;
    auto* tensor = vars[i].GetMutable<framework::LoDTensor>();
    tensor->Resize({static_cast<int64_t>(len)});
    auto* data = tensor->mutable_data<T>(place);

    std::vector<T> value;
    for (size_t j = 0; j < len; ++j) {
      value.push_back(static_cast<T>(1.0 * j));
    }

    if (std::is_same<Place, platform::CUDAPlace>::value) {
80
#if defined(PADDLE_WITH_NCCL)
81 82
      paddle::memory::Copy(place, data, cpu_place, value.data(),
                           sizeof(T) * value.size(), 0);
83
#endif
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    } else {
      paddle::memory::Copy(place, data, cpu_place, value.data(),
                           sizeof(T) * value.size());
    }

    framework::Tensor tmp;
    tmp.ShareDataWith(*tensor).Resize({static_cast<int64_t>(len)});
    group.dense_tensors_.push_back(std::move(tmp));
    group.all_length_ += len;
    group.dtype_ = tensor->type();
  }

  paddle::platform::DeviceContextPool& pool =
      paddle::platform::DeviceContextPool::Instance();
  auto* dev_ctx = pool.Get(place);

  {  // concat
    group.ConcatTensors(*dev_ctx);

    auto* tensor = group.dense_contents_.GetMutable<framework::LoDTensor>();
    framework::Tensor tmp;
    framework::TensorCopySync(*tensor, cpu_place, &tmp);
    auto* data = tmp.data<T>();
    size_t offset = 0;
    for (size_t i = 0; i < size; ++i) {
      auto len = i + 1;
      for (size_t j = 0; j < len; ++j) {
        EXPECT_EQ(data[offset + j], static_cast<T>(1.0 * j));
        // [[-0.0], [-0.0, -1.0], [-0.0, -1.0, -2.0] .. ]
        data[offset + j] = -data[offset + j];
      }
      offset += len;
    }
    framework::TensorCopySync(tmp, place, tensor);
  }

  {  // split
    group.SplitTensors(*dev_ctx);
    for (size_t i = 0; i < size; ++i) {
      auto len = i + 1;
      auto& tensor = group.dense_tensors_[i];
      framework::Tensor tmp;
      framework::TensorCopySync(tensor, cpu_place, &tmp);
      auto* data = tmp.data<T>();

      for (size_t j = 0; j < len; ++j) {
        EXPECT_EQ(data[j], static_cast<T>(-1.0 * j));
      }
    }
  }
}

136
#if defined(PADDLE_WITH_NCCL)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
TEST(TestGroup, TestConcatSplit) {
  platform::CUDAPlace cuda_place(0);
  platform::CPUPlace cpu_place;

  int size = 3;
  GroupConcatSplit<float>(cpu_place, size);
  GroupConcatSplit<double>(cpu_place, size);
  GroupConcatSplit<platform::float16>(cpu_place, size);

  GroupConcatSplit<float>(cuda_place, size);
  GroupConcatSplit<double>(cuda_place, size);
  GroupConcatSplit<platform::float16>(cuda_place, size);

  size = 15;
  GroupConcatSplit<float>(cpu_place, size);
  GroupConcatSplit<double>(cpu_place, size);
  GroupConcatSplit<platform::float16>(cpu_place, size);

  GroupConcatSplit<float>(cuda_place, size);
  GroupConcatSplit<double>(cuda_place, size);
  GroupConcatSplit<platform::float16>(cuda_place, size);
}

TEST(TestGroup, TestConcatSplitException) {
  platform::CUDAPinnedPlace place;

  int size = 3;
  ASSERT_ANY_THROW(GroupConcatSplit<float>(place, size));
}
166 167
#endif

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#if defined(PADDLE_WITH_XPU_BKCL)
TEST(TestGroup, TestXPUConcatSplit) {
  platform::XPUPlace xpu_place(0);
  platform::CPUPlace cpu_place;

  int size = 3;
  GroupConcatSplit<float>(cpu_place, size);
  GroupConcatSplit<float>(xpu_place, size);

  size = 15;
  GroupConcatSplit<float>(cpu_place, size);
  GroupConcatSplit<float>(xpu_place, size);
}
#endif

183 184
}  // namespace imperative
}  // namespace paddle