print_op.cc 9.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yan Chunwei 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

   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 <algorithm>
Y
Yi Wang 已提交
16
#include "paddle/fluid/framework/op_registry.h"
S
sneaxiy 已提交
17
#include "paddle/fluid/framework/var_type.h"
Y
Yan Chunwei 已提交
18 19 20

namespace paddle {
namespace operators {
Y
Yu Yang 已提交
21
using framework::GradVarName;
Y
Yan Chunwei 已提交
22 23 24

#define CLOG std::cout

25 26 27
const char kForward[] = "FORWARD";
const char kBackward[] = "BACKWARD";
const char kBoth[] = "BOTH";
Y
yangyaming 已提交
28

C
chengduo 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
class LogGuard {
 public:
  inline LogGuard() { LogMutex().lock(); }

  inline ~LogGuard() { LogMutex().unlock(); }

 private:
  static std::mutex &LogMutex() {
    static std::mutex mtx;
    return mtx;
  }
};

Y
Yan Chunwei 已提交
42 43 44 45
struct Formater {
  std::string message;
  std::string name;
  std::vector<int> dims;
46
  std::type_index dtype{typeid(const char)};
Y
Yan Chunwei 已提交
47 48
  framework::LoD lod;
  int summarize;
Y
Yu Yang 已提交
49
  void *data{nullptr};
C
chengduo 已提交
50 51
  platform::Place place;
  std::stringstream logs;
Y
Yan Chunwei 已提交
52 53 54

  void operator()(size_t size) {
    PrintMessage();
C
chengduo 已提交
55
    PrintPlaceInfo();
Y
Yan Chunwei 已提交
56 57 58 59 60
    PrintName();
    PrintDims();
    PrintDtype();
    PrintLod();
    PrintData(size);
C
chengduo 已提交
61 62
    LogGuard guard;
    CLOG << logs.str();
Y
Yan Chunwei 已提交
63 64 65
  }

 private:
C
chengduo 已提交
66 67
  void PrintPlaceInfo() { logs << "The place is:" << place << std::endl; }
  void PrintMessage() { logs << std::time(nullptr) << "\t" << message << "\t"; }
Y
Yan Chunwei 已提交
68 69
  void PrintName() {
    if (!name.empty()) {
C
chengduo 已提交
70
      logs << "Tensor[" << name << "]" << std::endl;
Y
Yan Chunwei 已提交
71 72 73 74
    }
  }
  void PrintDims() {
    if (!dims.empty()) {
C
chengduo 已提交
75
      logs << "\tshape: [";
Y
Yan Chunwei 已提交
76
      for (auto i : dims) {
C
chengduo 已提交
77
        logs << i << ",";
Y
Yan Chunwei 已提交
78
      }
C
chengduo 已提交
79
      logs << "]" << std::endl;
Y
Yan Chunwei 已提交
80 81 82
    }
  }
  void PrintDtype() {
S
sneaxiy 已提交
83
    if (!framework::IsType<const char>(dtype)) {
C
chengduo 已提交
84
      logs << "\tdtype: " << dtype.name() << std::endl;
Y
Yan Chunwei 已提交
85 86 87 88
    }
  }
  void PrintLod() {
    if (!lod.empty()) {
C
chengduo 已提交
89
      logs << "\tLoD: [";
Y
Yan Chunwei 已提交
90
      for (auto level : lod) {
C
chengduo 已提交
91
        logs << "[ ";
Y
Yan Chunwei 已提交
92
        for (auto i : level) {
C
chengduo 已提交
93
          logs << i << ",";
Y
Yan Chunwei 已提交
94
        }
C
chengduo 已提交
95
        logs << " ]";
Y
Yan Chunwei 已提交
96
      }
C
chengduo 已提交
97
      logs << "]" << std::endl;
Y
Yan Chunwei 已提交
98 99 100 101 102 103
    }
  }

  void PrintData(size_t size) {
    PADDLE_ENFORCE_NOT_NULL(data);
    // print float
S
sneaxiy 已提交
104
    if (framework::IsType<const float>(dtype)) {
Y
Yan Chunwei 已提交
105
      Display<float>(size);
S
sneaxiy 已提交
106
    } else if (framework::IsType<const double>(dtype)) {
Y
Yan Chunwei 已提交
107
      Display<double>(size);
S
sneaxiy 已提交
108
    } else if (framework::IsType<const int>(dtype)) {
Y
Yan Chunwei 已提交
109
      Display<int>(size);
S
sneaxiy 已提交
110
    } else if (framework::IsType<const int64_t>(dtype)) {
Y
Yan Chunwei 已提交
111
      Display<int64_t>(size);
S
sneaxiy 已提交
112
    } else if (framework::IsType<const bool>(dtype)) {
113 114
      Display<bool>(size);
    } else {
C
chengduo 已提交
115
      logs << "\tdata: unprintable type: " << dtype.name() << std::endl;
Y
Yan Chunwei 已提交
116 117 118 119 120
    }
  }

  template <typename T>
  void Display(size_t size) {
Y
Yu Yang 已提交
121
    auto *d = reinterpret_cast<T *>(data);
C
chengduo 已提交
122
    logs << "\tdata: ";
Y
Yan Chunwei 已提交
123 124 125
    if (summarize != -1) {
      summarize = std::min(size, (size_t)summarize);
      for (int i = 0; i < summarize; i++) {
C
chengduo 已提交
126
        logs << d[i] << ",";
Y
Yan Chunwei 已提交
127 128 129
      }
    } else {
      for (size_t i = 0; i < size; i++) {
C
chengduo 已提交
130
        logs << d[i] << ",";
Y
Yan Chunwei 已提交
131 132
      }
    }
C
chengduo 已提交
133
    logs << std::endl;
Y
Yan Chunwei 已提交
134 135 136 137
  }
};

// TODO(ChunweiYan) there should be some other printers for TensorArray
138
class PrintOp : public framework::OperatorBase {
Y
Yan Chunwei 已提交
139
 public:
140 141 142
  PrintOp(const std::string &type, const framework::VariableNameMap &inputs,
          const framework::VariableNameMap &outputs,
          const framework::AttributeMap &attrs)
Y
Yan Chunwei 已提交
143 144
      : OperatorBase(type, inputs, outputs, attrs) {}

145
 private:
Y
Yu Yang 已提交
146 147
  void RunImpl(const framework::Scope &scope,
               const platform::Place &place) const override {
148 149 150 151 152 153 154 155 156 157 158 159 160 161
    const auto in_var = scope.FindVar(Input("In"));
    auto out_var = scope.FindVar(Output("Out"));
    PADDLE_ENFORCE_NOT_NULL(in_var, "The input should not be found in scope",
                            Input("In"));
    PADDLE_ENFORCE_NOT_NULL(out_var, "The output should not be found in scope",
                            Output("Out"));
    auto &in_tensor = in_var->Get<framework::LoDTensor>();
    framework::LoDTensor *out_tensor =
        out_var->GetMutable<framework::LoDTensor>();

    PrintValue(place, Inputs("In").front(), in_tensor);
    framework::TensorCopy(in_tensor, place, out_tensor);
    out_tensor->set_lod(in_tensor.lod());
  }
Y
yangyaming 已提交
162

163 164 165
  void PrintValue(const platform::Place &place,
                  const std::string &printed_var_name,
                  const framework::LoDTensor &in_tensor) const {
Y
yangyaming 已提交
166
    std::string print_phase = Attr<std::string>("print_phase");
Y
Yu Yang 已提交
167 168 169 170
    bool is_forward = Attr<bool>("is_forward");

    if ((is_forward && print_phase == kBackward) ||
        (!is_forward && print_phase == kForward)) {
Y
yangyaming 已提交
171 172 173
      return;
    }

Y
Yan Chunwei 已提交
174 175 176
    int first_n = Attr<int>("first_n");
    if (first_n > 0 && ++times_ > first_n) return;

Y
yangyaming 已提交
177 178 179
    framework::LoDTensor printed_tensor;
    printed_tensor.set_lod(in_tensor.lod());
    printed_tensor.Resize(in_tensor.dims());
Y
Yan Chunwei 已提交
180

181
    if (is_cpu_place(in_tensor.place())) {
Y
yangyaming 已提交
182 183 184 185
      printed_tensor.ShareDataWith(in_tensor);
    } else {
      // copy data to cpu to print
      platform::CPUPlace place;
186
      TensorCopy(in_tensor, place, &printed_tensor);
Y
yangyaming 已提交
187
    }
Y
Yan Chunwei 已提交
188 189

    Formater formater;
C
chengduo 已提交
190
    formater.place = place;
191
    formater.message = Attr<std::string>("message");
Y
Yan Chunwei 已提交
192
    if (Attr<bool>("print_tensor_name")) {
Y
yangyaming 已提交
193
      formater.name = printed_var_name;
Y
Yan Chunwei 已提交
194 195
    }
    if (Attr<bool>("print_tensor_type")) {
Y
Yu Yang 已提交
196
      formater.dtype = framework::ToTypeIndex(printed_tensor.type());
Y
Yan Chunwei 已提交
197 198
    }
    if (Attr<bool>("print_tensor_shape")) {
Y
Yu Yang 已提交
199
      auto &dims = printed_tensor.dims();
Y
yangyaming 已提交
200 201
      formater.dims.resize(dims.size());
      for (int i = 0; i < dims.size(); ++i) formater.dims[i] = dims[i];
Y
Yan Chunwei 已提交
202 203
    }
    if (Attr<bool>("print_tensor_lod")) {
Y
yangyaming 已提交
204
      formater.lod = printed_tensor.lod();
Y
Yan Chunwei 已提交
205 206
    }
    formater.summarize = Attr<int>("summarize");
Y
Yu Yang 已提交
207
    formater.data = reinterpret_cast<void *>(printed_tensor.data<void>());
Y
yangyaming 已提交
208
    formater(printed_tensor.numel());
Y
Yan Chunwei 已提交
209 210 211 212 213 214 215 216
  }

 private:
  mutable int times_{0};
};

class PrintOpProtoAndCheckMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
217
  void Make() override {
Y
yangyaming 已提交
218
    AddInput("In", "Input tensor to be displayed.");
219
    AddOutput("Out", "The output tensor.");
Y
Yan Chunwei 已提交
220 221
    AddAttr<int>("first_n", "Only log `first_n` number of times.");
    AddAttr<std::string>("message", "A string message to print as a prefix.");
Y
yangyaming 已提交
222
    AddAttr<int>("summarize", "Number of elements printed.");
Y
Yan Chunwei 已提交
223 224 225 226
    AddAttr<bool>("print_tensor_name", "Whether to print the tensor name.");
    AddAttr<bool>("print_tensor_type", "Whether to print the tensor's dtype.");
    AddAttr<bool>("print_tensor_shape", "Whether to print the tensor's shape.");
    AddAttr<bool>("print_tensor_lod", "Whether to print the tensor's lod.");
Y
Yu Yang 已提交
227 228 229 230
    AddAttr<std::string>("print_phase",
                         "(string, default 'FORWARD') Which phase to display "
                         "including 'FORWARD' "
                         "'BACKWARD' and 'BOTH'.")
231 232 233
        .SetDefault(std::string(kBoth))
        .InEnum({std::string(kForward), std::string(kBackward),
                 std::string(kBoth)});
Y
Yu Yang 已提交
234
    AddAttr<bool>("is_forward", "Whether is forward or not").SetDefault(true);
Y
Yan Chunwei 已提交
235
    AddComment(R"DOC(
Y
yangyaming 已提交
236
Creates a print op that will print when a tensor is accessed.
Y
Yan Chunwei 已提交
237

Y
yangyaming 已提交
238 239 240
Wraps the tensor passed in so that whenever that a tensor is accessed,
the message `message` is printed, along with the current value of the
tensor `t`.)DOC");
Y
Yan Chunwei 已提交
241 242 243
  }
};

244 245 246 247 248 249 250 251 252 253 254 255
class PrintOpInferShape : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext *ctx) const override {
    VLOG(10) << "PrintOpInferShape";
    PADDLE_ENFORCE(ctx->HasInput("In"), "Input(In) should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) should not be null.");
    ctx->ShareDim("In", /*->*/ "Out");
    ctx->ShareLoD("In", /*->*/ "Out");
  }
};

class PrintOpVarTypeInference : public framework::VarTypeInference {
Y
Yan Chunwei 已提交
256
 public:
257 258 259 260
  void operator()(framework::InferVarTypeContext *ctx) const override {
    auto input_type = ctx->GetType(ctx->Input("In")[0]);
    auto out_name = ctx->Output("Out").front();
    ctx->SetType(out_name, input_type);
Y
Yan Chunwei 已提交
261 262 263
  }
};

H
hong 已提交
264 265
template <typename T>
class PrintOpGradientMaker : public framework::SingleGradOpMaker<T> {
Y
yangyaming 已提交
266
 public:
H
hong 已提交
267
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
Y
yangyaming 已提交
268

H
hong 已提交
269 270
  std::unique_ptr<T> Apply() const override {
    auto *op_desc_ptr = new T();
Y
Yu Yang 已提交
271
    op_desc_ptr->SetType("print");
H
hong 已提交
272 273 274
    op_desc_ptr->SetInput("In", this->OutputGrad("Out"));
    op_desc_ptr->SetOutput("Out", this->InputGrad("In"));
    op_desc_ptr->SetAttrMap(this->Attrs());
Y
Yu Yang 已提交
275
    op_desc_ptr->SetAttr("is_forward", false);
H
hong 已提交
276
    return std::unique_ptr<T>(op_desc_ptr);
Y
yangyaming 已提交
277 278 279
  }
};

Y
Yan Chunwei 已提交
280 281 282
}  // namespace operators
}  // namespace paddle

Y
yangyaming 已提交
283 284
namespace ops = paddle::operators;

285
REGISTER_OPERATOR(print, ops::PrintOp, ops::PrintOpProtoAndCheckMaker,
H
hong 已提交
286 287 288
                  ops::PrintOpGradientMaker<paddle::framework::OpDesc>,
                  ops::PrintOpGradientMaker<paddle::imperative::OpBase>,
                  ops::PrintOpInferShape, ops::PrintOpVarTypeInference);