lod_tensor.cc 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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/framework/lod_tensor.h"
武毅 已提交
16 17
#include "paddle/framework/data_type.h"
#include "paddle/framework/framework.pb.h"
18 19 20 21 22 23 24 25

#include "paddle/memory/memcpy.h"
#include "paddle/memory/memory.h"

#include <stdint.h>
#include <string.h>
#include <algorithm>
#include <iterator>
26 27 28 29 30 31

#include <glog/logging.h>

namespace paddle {
namespace framework {

武毅 已提交
32
std::ostream &operator<<(std::ostream &os, const LoD &lod) {
33
  os << "{";
武毅 已提交
34
  for (auto &v : lod) {
35
    os << "{";
武毅 已提交
36
    for (auto &i : v) {
37 38 39 40 41 42 43 44 45
      os << i << ",";
    }
    os << "}";
  }
  os << "}";

  return os;
}

Y
Yang Yang 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
std::ostream &operator<<(std::ostream &os, const LoDTensor &t) {
  PADDLE_ENFORCE(platform::is_cpu_place(t.place()));
  PADDLE_ENFORCE(t.type().hash_code() == typeid(float).hash_code());

  os << "dim: " << t.dims() << "\n";
  os << "lod: " << t.lod() << "\n";

  // only print first ten elements
  int64_t size = t.numel() < 10 ? t.numel() : 10;
  for (int64_t i = 0; i < size; ++i) {
    os << t.data<float>()[i] << " ";
  }

  return os;
}

武毅 已提交
62
LoD SliceLevels(const LoD &in, size_t level_begin, size_t level_end) {
63
  LoD new_lod;
64 65
  new_lod.reserve(level_end - level_begin);
  for (size_t i = level_begin; i < level_end; i++) {
Q
qijun 已提交
66
    new_lod.emplace_back(in.at(i));
67
  }
68 69 70
  // transform the lowest level to absolute offset.
  LoD abs_offset_lod = ToAbsOffset(in);
  new_lod.back() = abs_offset_lod[level_end - 1];
71
  return new_lod;
72 73
}

武毅 已提交
74
LoD SliceInLevel(const LoD &in, size_t level, size_t elem_begin,
Q
qijun 已提交
75
                 size_t elem_end) {
76 77 78 79 80 81 82 83 84
  PADDLE_ENFORCE_LT(level, in.size());
  PADDLE_ENFORCE_LT(elem_end, in[level].size());

  LoD res;
  res.resize(in.size() - level);
  // copy the first level
  res[0].assign(in[level].begin() + elem_begin,
                in[level].begin() + elem_end + 1);
  for (size_t lvl = 1; lvl < res.size(); lvl++) {
武毅 已提交
85 86 87
    const auto &in_level = in[level + lvl];
    const auto &above_level = res[lvl - 1];
    auto &out_level = res[lvl];
88 89
    out_level.assign(in_level.begin() + above_level.front(),
                     in_level.begin() + above_level.back() + 1);
90
  }
91 92 93 94
  for (size_t lvl = 0; lvl < res.size(); lvl++) {
    // to make the first offset equals 0, all the elements minus the first
    // element
    size_t front = res[lvl].front();
武毅 已提交
95
    for (auto &ele : res[lvl]) {
96 97 98 99 100 101
      ele -= front;
    }
  }
  return res;
}

武毅 已提交
102
LoD ToAbsOffset(const LoD &in) {
103 104 105 106
  // the lowest level stores relative offsets
  if (in.empty() || in.size() == 1) return in;
  LoD result = in;
  for (int level = result.size() - 2; level >= 0; level--) {
武毅 已提交
107
    for (auto &ele : result[level]) {
108 109 110 111
      ele = result[level + 1][ele];
    }
  }
  return result;
112 113
}

武毅 已提交
114
bool operator==(const LoD &a, const LoD &b) {
115 116 117 118 119
  if (a.size() != b.size()) {
    return false;
  }

  for (size_t i = 0; i < a.size(); i++) {
武毅 已提交
120 121
    const auto &a_level = a[i];
    const auto &b_level = b[i];
122 123 124 125 126 127 128 129 130 131
    if (a_level.size() != b_level.size()) {
      return false;
    }
    for (size_t j = 0; j < a_level.size(); j++) {
      if (a_level[j] != b_level[j]) {
        return false;
      }
    }
  }
  return true;
132 133
}

134 135 136
size_t LoDTensor::NumElements(size_t level, size_t idx) const {
  PADDLE_ENFORCE_LT(level, NumLevels());
  PADDLE_ENFORCE_LT(idx, NumElements(level));
137
  return lod_[level][idx + 1] - lod_[level][idx];
138 139
}

140 141 142 143 144 145 146 147 148
size_t LoDTensor::NumInstancesInElement(size_t level, size_t idx) const {
  PADDLE_ENFORCE_LT(level, NumLevels());
  PADDLE_ENFORCE_LT(idx, NumElements(level));
  auto abs_lod = ToAbsOffset(lod());
  size_t begin = abs_lod[level][idx];
  size_t end = abs_lod[level][idx + 1];
  return end - begin;
}

149
void LoDTensor::ShrinkLevels(size_t level_begin, size_t level_end) {
Q
qijun 已提交
150 151 152 153
  auto new_lod = framework::SliceLevels(lod_, level_begin, level_end);
  lod_ = new_lod;
}

154 155 156 157 158
void LoDTensor::ShrinkInLevel(size_t level, size_t elem_begin,
                              size_t elem_end) {
  PADDLE_ENFORCE_LT(level, NumLevels());
  PADDLE_ENFORCE_LT(elem_begin, NumElements(level));
  PADDLE_ENFORCE_LT(elem_end, NumElements(level) + 1);
Q
qijun 已提交
159

160
  auto abs_lod = framework::ToAbsOffset(lod());
Q
qijun 已提交
161 162
  auto new_lod = framework::SliceInLevel(lod_, level, elem_begin, elem_end);
  lod_ = new_lod;
163 164 165 166 167 168

  // slice the underlying tensor
  size_t begin = abs_lod[level][elem_begin];
  size_t end = abs_lod[level][elem_end];
  PADDLE_ENFORCE_LT(begin, end, "Cannot shrink, the result tensor is empty.");
  ShareDataWith(Slice(begin, end));
Q
qijun 已提交
169
}
170

171
using LoDAndOffset = std::pair<LoD, std::pair<size_t, size_t>>;
武毅 已提交
172
LoDAndOffset GetSubLoDAndAbsoluteOffset(const LoD &lod, size_t start_idx,
173 174 175 176 177 178
                                        size_t end_idx, size_t start_level) {
  LoD sub_lod;

  for (size_t level_idx = start_level; level_idx < lod.size(); ++level_idx) {
    PADDLE_ENFORCE_LE(start_idx, end_idx);
    PADDLE_ENFORCE_LT(end_idx, lod[level_idx].size());
179 180 181 182
    std::vector<size_t> level_lens;
    for (size_t i = start_idx; i < end_idx; ++i) {
      level_lens.push_back(lod[level_idx][i + 1] - lod[level_idx][i]);
    }
183
    sub_lod.emplace_back(level_lens);
184 185 186
    start_idx = lod[level_idx][start_idx];
    end_idx = lod[level_idx][end_idx];
  }
187 188

  return LoDAndOffset{sub_lod, {start_idx, end_idx}};
189 190
}

武毅 已提交
191
void AppendLoD(LoD *lod, const LoD &lod_length) {
192 193
  PADDLE_ENFORCE(
      lod->empty() || lod->size() == lod_length.size(),
194
      "The lod_length should has the same size with the appended lod.");
195 196 197
  if (lod->empty()) {
    *lod = LoD(lod_length.size(), std::vector<size_t>({0}));
  }
198
  for (size_t i = 0; i < lod->size(); ++i) {
武毅 已提交
199
    auto &level = (*lod)[i];
200 201 202 203 204 205
    for (size_t len : lod_length[i]) {
      level.push_back(level.back() + len);
    }
  }
}

武毅 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
void SerializeToStream(std::ostream &os, const LoDTensor &tensor,
                       const platform::DeviceContext &dev_ctx) {
  // TODO(typhoonzero): serialize to ostream
  {  // the 1st field, uint32_t version
    constexpr uint32_t version = 0;
    os.write(reinterpret_cast<const char *>(&version), sizeof(version));
  }
  {  // the 2nd field, tensor description
     // int32_t  size
     // void*    protobuf message
    framework::TensorDesc desc;
    desc.set_data_type(framework::ToDataType(tensor.type()));
    auto dims = framework::vectorize(tensor.dims());
    auto *pb_dims = desc.mutable_dims();
    pb_dims->Resize(static_cast<int>(dims.size()), 0);
    std::copy(dims.begin(), dims.end(), pb_dims->begin());
    int32_t size = desc.ByteSize();
    os.write(reinterpret_cast<const char *>(&size), sizeof(size));
    auto out = desc.SerializeAsString();
    os.write(out.data(), size);
  }
  {  // the 3rd field, tensor data
    uint64_t size = tensor.memory_size();
    auto *data_ptr = tensor.data<void>();
    PADDLE_ENFORCE(size < std::numeric_limits<std::streamsize>::max(),
                   "Index overflow when writing tensor");
    if (platform::is_gpu_place(tensor.place())) {
#ifdef PADDLE_WITH_CUDA
      constexpr size_t kBufSize = 1024 * 1024 * 64;  // 64MB
      std::unique_ptr<char[]> buf(new char[kBufSize]);
      auto &gpu_dev_ctx =
          static_cast<const platform::CUDADeviceContext &>(dev_ctx);
      platform::CPUPlace cpu;
      uintptr_t data = reinterpret_cast<uintptr_t>(data_ptr);
      while (size != 0) {
        size_t size_to_write = std::min(kBufSize, static_cast<size_t>(size));
        memory::Copy(cpu, buf.get(),
                     boost::get<platform::GPUPlace>(tensor.place()),
                     reinterpret_cast<const void *>(data), size_to_write,
                     gpu_dev_ctx.stream());
        gpu_dev_ctx.Wait();
        os.write(buf.get(), size_to_write);
        data += size_to_write;
        size -= size_to_write;
      }
#else
      PADDLE_THROW("Unexpected branch");
#endif
    } else {
      os.write(static_cast<const char *>(data_ptr),
               static_cast<std::streamsize>(size));
    }
  }
  {  // the 4th field, lod information
     // uint64_t lod_level
     // uint64_t lod_level_1 size in byte.
     // int*     lod_level_1 data
     // ...
    auto lod = tensor.lod();
    uint64_t size = lod.size();
    os.write(reinterpret_cast<const char *>(&size), sizeof(size));

    for (auto &each : lod) {
      size = each.size() * sizeof(framework::LoD::value_type::value_type);
      os.write(reinterpret_cast<const char *>(&size), sizeof(size));
      os.write(reinterpret_cast<const char *>(each.data()),
               static_cast<std::streamsize>(size));
    }
  }
}

void DeserializeFromStream(std::istream &is, LoDTensor *tensor) {
  uint32_t version;
  is.read(reinterpret_cast<char *>(&version), sizeof(version));
  PADDLE_ENFORCE_EQ(version, 0U, "Only version 0 is supported");
  framework::TensorDesc desc;
  {  // int32_t size
     // proto buffer
    int32_t size;
    is.read(reinterpret_cast<char *>(&size), sizeof(size));
    std::unique_ptr<char[]> buf(new char[size]);
    is.read(reinterpret_cast<char *>(buf.get()), size);
    PADDLE_ENFORCE(desc.ParseFromArray(buf.get(), size),
                   "Cannot parse tensor desc");
  }
  {  // read tensor
    std::vector<int64_t> dims;
    dims.reserve(static_cast<size_t>(desc.dims().size()));
    std::copy(desc.dims().begin(), desc.dims().end(), std::back_inserter(dims));
    tensor->Resize(framework::make_ddim(dims));

    void *buf;
    platform::Place cpu = platform::CPUPlace();
    switch (desc.data_type()) {
      case framework::FP32:
        buf = tensor->mutable_data<float>(cpu);
        break;
      case framework::FP64:
        buf = tensor->mutable_data<double>(cpu);
        break;
      case framework::INT32:
        buf = tensor->mutable_data<int>(cpu);
        break;
      case framework::INT64:
        buf = tensor->mutable_data<int64_t>(cpu);
        break;
      default:
        PADDLE_THROW("DataType %d not supported", desc.data_type());
    }
    is.read(static_cast<char *>(buf), tensor->memory_size());
  }
  {  // read lod
    uint64_t lod_level;
    is.read(reinterpret_cast<char *>(&lod_level), sizeof(lod_level));
    auto &lod = *tensor->mutable_lod();
    lod.resize(lod_level);
    for (uint64_t i = 0; i < lod_level; ++i) {
      uint64_t size;
      is.read(reinterpret_cast<char *>(&size), sizeof(size));
      std::vector<size_t> tmp(size / sizeof(size_t));
      is.read(reinterpret_cast<char *>(tmp.data()),
              static_cast<std::streamsize>(size));
      lod[i] = tmp;
    }
  }
}

Y
Yang Yang 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
std::vector<LoDTensor> LoDTensor::SplitLoDTensor(
    const std::vector<platform::Place> places) const {
  check_memory_size();
  //  PADDLE_ENFORCE(lod().empty() || (lod().size() == 1 && lod()[0].empty())
  //                 , "Disable parallel lod for now");
  PADDLE_ENFORCE(lod().empty(), "Disable parallel lod for now");
  PADDLE_ENFORCE(dims()[0] % places.size() == 0,
                 "Batch size should be divided by places size");

  std::vector<LoDTensor> lods;
  for (int place_idx = 0; place_idx < places.size(); ++place_idx) {
    int begin = place_idx * dims()[0] / places.size();
    int end = (place_idx + 1) * dims()[0] / places.size();
    auto src = Slice(begin, end);

    LoDTensor dst;
    dst.Resize(src.dims());
    auto &dst_place = places[place_idx];
    auto dst_ptr = dst.mutable_data(dst_place, src.type());

    // TODO(tonyyang-svail):
    //   change the following to framework::CopyFrom
    auto src_place = src.place();
    auto src_ptr = src.data<void>();
    auto size = src.numel() * SizeOfType(src.type());
    if (platform::is_cpu_place(src_place) &&
        platform::is_cpu_place(dst_place)) {
      memory::Copy(boost::get<platform::CPUPlace>(dst_place), dst_ptr,
                   boost::get<platform::CPUPlace>(src_place), src_ptr, size);
    } else {
      PADDLE_THROW("Not Implemented");
    }

    lods.emplace_back(dst);
  }

  return lods;
}

Y
Yang Yang 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
void LoDTensor::MergeLoDTensor(
    const std::vector<const LoDTensor *> &lod_tensors, platform::Place place) {
  PADDLE_ENFORCE(platform::is_cpu_place(place));
  PADDLE_ENFORCE(!lod_tensors.empty());

  framework::DDim new_dim = lod_tensors[0]->dims();
  std::type_index new_type = lod_tensors[0]->type();
  for (auto *lod : lod_tensors) {
    PADDLE_ENFORCE(new_dim == lod->dims());
    PADDLE_ENFORCE(new_type == lod->type());
    PADDLE_ENFORCE(platform::is_cpu_place(lod->place()));
  }
  new_dim[0] *= lod_tensors.size();
  Resize(new_dim);

  auto *dst_ptr = reinterpret_cast<uint8_t *>(mutable_data(place, new_type));
  for (auto *src : lod_tensors) {
    auto size = src->numel() * SizeOfType(src->type());
    memory::Copy(boost::get<platform::CPUPlace>(place), dst_ptr,
                 boost::get<platform::CPUPlace>(src->place()),
                 src->data<void>(), size);
    dst_ptr += size;
  }
}

397 398
}  // namespace framework
}  // namespace paddle