device_worker.cc 6.6 KB
Newer Older
D
dongdaxiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 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/framework/device_worker.h"

17 18
#include "paddle/fluid/framework/convert_utils.h"

19
namespace phi {
20
class DenseTensor;
21
}  // namespace phi
22

D
dongdaxiang 已提交
23 24 25
namespace paddle {
namespace framework {

W
wanghuancoder 已提交
26 27
class Scope;

D
dongdaxiang 已提交
28 29
void DeviceWorker::SetRootScope(Scope* root_scope) { root_scope_ = root_scope; }

J
jiaqi 已提交
30
void DeviceWorker::SetDataFeed(DataFeed* data_feed) {
D
dongdaxiang 已提交
31 32 33
  device_reader_ = data_feed;
}

34
template <typename T>
35
std::string PrintLodTensorType(Tensor* tensor, int64_t start, int64_t end) {
36 37 38 39 40 41 42 43 44 45 46 47
  auto count = tensor->numel();
  if (start < 0 || end > count) {
    VLOG(3) << "access violation";
    return "access violation";
  }
  std::ostringstream os;
  for (int64_t i = start; i < end; i++) {
    os << ":" << tensor->data<T>()[i];
  }
  return os.str();
}

48
std::string PrintLodTensorIntType(Tensor* tensor, int64_t start, int64_t end) {
49 50 51 52 53 54 55 56 57 58 59 60
  auto count = tensor->numel();
  if (start < 0 || end > count) {
    VLOG(3) << "access violation";
    return "access violation";
  }
  std::ostringstream os;
  for (int64_t i = start; i < end; i++) {
    os << ":" << static_cast<uint64_t>(tensor->data<int64_t>()[i]);
  }
  return os.str();
}

61
std::string PrintLodTensor(Tensor* tensor, int64_t start, int64_t end) {
62
  std::string out_val;
63
  if (framework::TransToProtoVarType(tensor->dtype()) == proto::VarType::FP32) {
64
    out_val = PrintLodTensorType<float>(tensor, start, end);
65 66
  } else if (framework::TransToProtoVarType(tensor->dtype()) ==
             proto::VarType::INT64) {
67
    out_val = PrintLodTensorIntType(tensor, start, end);
68 69
  } else if (framework::TransToProtoVarType(tensor->dtype()) ==
             proto::VarType::FP64) {
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
    out_val = PrintLodTensorType<double>(tensor, start, end);
  } else {
    out_val = "unsupported type";
  }
  return out_val;
}

std::pair<int64_t, int64_t> GetTensorBound(LoDTensor* tensor, int index) {
  auto& dims = tensor->dims();
  if (tensor->lod().size() != 0) {
    auto& lod = tensor->lod()[0];
    return {lod[index] * dims[1], lod[index + 1] * dims[1]};
  } else {
    return {index * dims[1], (index + 1) * dims[1]};
  }
}

bool CheckValidOutput(LoDTensor* tensor, size_t batch_size) {
  auto& dims = tensor->dims();
  if (dims.size() != 2) return false;
  if (tensor->lod().size() != 0) {
    auto& lod = tensor->lod()[0];
    if (lod.size() != batch_size + 1) {
      return false;
    }
  } else {
    if (dims[0] != static_cast<int>(batch_size)) {
      return false;
    }
  }
  return true;
}

H
hutuxian 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
void DeviceWorker::DumpParam(const Scope& scope, const int batch_id) {
  std::ostringstream os;
  for (auto& param : *dump_param_) {
    os.str("");
    Variable* var = scope.FindVar(param);
    if (var == nullptr) {
      continue;
    }
    LoDTensor* tensor = var->GetMutable<LoDTensor>();
    framework::LoDTensor cpu_tensor;
    if (platform::is_gpu_place(tensor->place())) {
      TensorCopySync(*tensor, platform::CPUPlace(), &cpu_tensor);
      tensor = &cpu_tensor;
    }
    int64_t len = tensor->numel();
    os << "(" << batch_id << "," << param << ")"
       << PrintLodTensor(tensor, 0, len);
    writer_ << os.str();
  }
}
X
xujiaqi01 已提交
123

H
hutuxian 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137
void DeviceWorker::InitRandomDumpConfig(const TrainerDesc& desc) {
  bool enable_random_dump = desc.enable_random_dump();
  if (!enable_random_dump) {
    dump_mode_ = 0;
  } else {
    if (desc.random_with_lineid()) {
      dump_mode_ = 1;
    } else {
      dump_mode_ = 2;
    }
  }
  dump_interval_ = desc.dump_interval();
}

138 139
void DeviceWorker::DumpField(const Scope& scope,
                             int dump_mode,
H
hutuxian 已提交
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 166 167 168 169 170 171 172 173 174 175 176
                             int dump_interval) {  // dump_mode: 0: no random,
                                                   // 1: random with insid hash,
                                                   // 2: random with random
                                                   // number
  size_t batch_size = device_reader_->GetCurBatchSize();
  auto& ins_id_vec = device_reader_->GetInsIdVec();
  auto& ins_content_vec = device_reader_->GetInsContentVec();
  if (ins_id_vec.size() > 0) {
    batch_size = ins_id_vec.size();
  }
  std::vector<std::string> ars(batch_size);
  std::vector<bool> hit(batch_size, false);

  std::default_random_engine engine(0);
  std::uniform_int_distribution<size_t> dist(0U, INT_MAX);
  for (size_t i = 0; i < batch_size; i++) {
    size_t r = 0;
    if (dump_mode == 1) {
      r = XXH64(ins_id_vec[i].data(), ins_id_vec[i].length(), 0);
    } else if (dump_mode == 2) {
      r = dist(engine);
    }
    if (r % dump_interval != 0) {
      continue;
    }
    hit[i] = true;
  }
  for (size_t i = 0; i < ins_id_vec.size(); i++) {
    if (!hit[i]) {
      continue;
    }
    ars[i] += ins_id_vec[i];
    ars[i] = ars[i] + "\t" + ins_content_vec[i];
  }
  for (auto& field : *dump_fields_) {
    Variable* var = scope.FindVar(field);
    if (var == nullptr) {
177 178
      VLOG(0) << "Note: field[" << field
              << "] cannot be find in scope, so it was skipped.";
H
hutuxian 已提交
179 180 181
      continue;
    }
    LoDTensor* tensor = var->GetMutable<LoDTensor>();
182 183 184 185 186
    if (!tensor->IsInitialized()) {
      VLOG(0) << "Note: field[" << field
              << "] is not initialized, so it was skipped.";
      continue;
    }
H
hutuxian 已提交
187 188 189
    framework::LoDTensor cpu_tensor;
    if (platform::is_gpu_place(tensor->place())) {
      TensorCopySync(*tensor, platform::CPUPlace(), &cpu_tensor);
190
      cpu_tensor.set_lod(tensor->lod());
H
hutuxian 已提交
191 192 193
      tensor = &cpu_tensor;
    }
    if (!CheckValidOutput(tensor, batch_size)) {
194 195 196 197
      VLOG(0) << "Note: field[" << field
              << "] cannot pass check, so it was "
                 "skipped. Maybe the dimension is "
                 "wrong ";
H
hutuxian 已提交
198 199 200 201 202 203 204
      continue;
    }
    for (size_t i = 0; i < batch_size; ++i) {
      if (!hit[i]) {
        continue;
      }
      auto bound = GetTensorBound(tensor, i);
205 206
      ars[i] = ars[i] + "\t" + field + ":" +
               std::to_string(bound.second - bound.first);
H
hutuxian 已提交
207 208 209 210 211 212 213 214 215 216 217 218
      ars[i] += PrintLodTensor(tensor, bound.first, bound.second);
    }
  }
  // #pragma omp parallel for
  for (size_t i = 0; i < ars.size(); i++) {
    if (ars[i].length() == 0) {
      continue;
    }
    writer_ << ars[i];
  }
}

D
dongdaxiang 已提交
219 220
}  // namespace framework
}  // namespace paddle