test_group.cc 4.9 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 33 34 35
// 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"

#if defined(PADDLE_WITH_NCCL)
#include "paddle/fluid/imperative/reducer.h"
#endif

namespace paddle {
namespace imperative {

#if defined(PADDLE_WITH_NCCL)
TEST(TestGroup, TestPrintGroupMessage) {
  Group group;
  std::stringstream stream1, stream2;
  stream1 << group;
  ASSERT_STREQ(stream1.str().c_str(),
36
               "numel: 0 ;is_sparse: 0 ;var number: 0\n[]\n");
37 38 39 40 41 42 43 44 45 46

  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;

47
  std::string head = "numel: 102 ;is_sparse: 0 ;var number: 102\n";
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  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());
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 136 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
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) {
      paddle::memory::Copy(place, data, cpu_place, value.data(),
                           sizeof(T) * value.size(), 0);
    } 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));
      }
    }
  }
}

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 168 169
#endif

}  // namespace imperative
}  // namespace paddle