utils.h 16.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2021 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.

#pragma once

17
#include "paddle/fluid/eager/api/utils/tensor_utils.h"
18 19 20
#include "paddle/fluid/eager/autograd_meta.h"
#include "paddle/fluid/eager/eager_tensor.h"
#include "paddle/fluid/eager/grad_node_info.h"
21
#include "paddle/phi/api/all.h"
22 23 24

namespace egr {

25 26
class TensorWrapper;

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/**
 * EagerUtils is utils used to do some static conversion or autograd
 * members access, this class is desinged to be a full static functional
 * utils class
 * **/

template <typename ElementType>
class IterHelper {
  virtual void visit(ElementType element) = 0;

  void visit(std::vector<ElementType>* elements) {
    for (auto element : *elements) visit(element);
  }

  template <typename... Args>
  void apply() {}

 public:
  template <typename T, typename... Args>
  void apply(T&& arg, Args&&... args) {
    visit(std::forward<T>(arg));
    return apply(std::forward<Args>(args)...);
  }
  virtual ~IterHelper() = default;
};

class ComputeRequireGradIter : public IterHelper<AutogradMeta*> {
 public:
  bool RequireGrad() { return require_grad_; }

 private:
  void visit(AutogradMeta* element) override {
59 60 61
    // Dispensable Tensors feeds in nullptr autograd_meta
    if (!element) return;

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    bool stop_gradient = element->StopGradient();
    if (!stop_gradient) require_grad_ = true;
  }

  bool require_grad_ = false;
};

class PassStopGradientIter : public IterHelper<AutogradMeta*> {
 public:
  void SetStopGradient(bool stop_gradient) { stop_gradient_ = stop_gradient; }

 private:
  void visit(AutogradMeta* element) override {
    if (!element) {
      // TODO(jiabin): Add Tensor name here when we supported.
      VLOG(2) << "Tensor is NULL";
      return;
    }
80
    element->SetStopGradient(stop_gradient_);
81 82 83 84 85 86 87 88 89 90
  }

  bool stop_gradient_ = true;
};

class EagerUtils {
 public:
  /**
   * We have to use autograd_meta and multi_autograd_meta to initialize
   * autograd_meta for tensor, since we can't init it in
91
   * egr::EagerVariable's
92 93 94
   * constructor (it's abstract class there)
   *
   * **/
95
  static AutogradMeta* autograd_meta(paddle::experimental::Tensor* target);
96

97
  static std::vector<AutogradMeta*> autograd_meta(
98
      std::vector<paddle::experimental::Tensor>* targets);
99

W
wanghuancoder 已提交
100 101 102
  static std::vector<AutogradMeta*> autograd_meta(
      std::vector<paddle::experimental::Tensor*>* targets);

103 104
  static std::pair<size_t, size_t> OutRankInfo(
      const paddle::experimental::Tensor& target);
105 106

  static std::shared_ptr<GradNodeBase> grad_node(
107
      const paddle::experimental::Tensor& target);
108 109
  static paddle::experimental::Tensor* mutable_grad(
      const paddle::experimental::Tensor& target);
110 111 112 113 114 115 116 117 118 119 120 121 122 123

  // Set history is used to set backward info during forward process, it will
  // set forward var's autograd meta's grad node as current backward node.
  static void SetHistory(std::vector<AutogradMeta*>* autograd_metas,
                         const std::shared_ptr<GradNodeBase>& grad_node);
  static void SetHistory(AutogradMeta* autograd_meta,
                         const std::shared_ptr<GradNodeBase>& grad_node);

  // This is used for Set vector of tensors' rank
  static void SetOutRankWithSlot(std::vector<AutogradMeta*>* targets,
                                 size_t slot_id);
  static void SetOutRankWithSlot(AutogradMeta* target, size_t slot_id);

  // This method will return an AutogradMeta pointer unsafely.
124 125
  static AutogradMeta* nullable_autograd_meta(
      const paddle::experimental::Tensor& target);
H
hong 已提交
126
  static AutogradMeta* nullable_autograd_meta(
127
      const paddle::optional<paddle::experimental::Tensor>& target);
128
  static std::vector<AutogradMeta*> nullable_autograd_meta(
129
      const std::vector<paddle::experimental::Tensor>& targets);
W
wanghuancoder 已提交
130 131
  static std::vector<AutogradMeta*> nullable_autograd_meta(
      const std::vector<paddle::experimental::Tensor*>& targets);
132 133
  static AutogradMeta* unsafe_autograd_meta(
      const paddle::experimental::Tensor& target);
134
  static std::vector<AutogradMeta*> unsafe_autograd_meta(
135
      const std::vector<paddle::experimental::Tensor>& targets);
136

137 138
  template <typename T, typename... Args>
  static bool ComputeRequireGrad(T trace_backward, Args&&... args) {
W
wanghuancoder 已提交
139 140 141 142
    if (!trace_backward) {
      VLOG(6) << "Do not require grad because trace_backward = false";
      return false;
    }
143 144 145 146 147 148 149 150 151 152 153 154 155 156

    auto iter = ComputeRequireGradIter();
    iter.apply(std::forward<Args>(args)...);

    return iter.RequireGrad();
  }

  template <typename T, typename... Args>
  static void PassStopGradient(T stop_gradient, Args&&... args) {
    auto iter = PassStopGradientIter();
    iter.SetStopGradient(stop_gradient);
    iter.apply(std::forward<Args>(args)...);
  }

157 158 159 160 161 162
  static void CheckInplace(const paddle::experimental::Tensor& target,
                           const AutogradMeta* autograd_meta,
                           bool require_any_grad) {
    if (require_any_grad && autograd_meta) {
      PADDLE_ENFORCE_EQ(!autograd_meta->StopGradient() &&
                            egr::egr_utils_api::IsLeafTensor(target),
163 164 165 166 167
                        false,
                        paddle::platform::errors::InvalidArgument(
                            "Leaf Var (%s) that doesn't stop gradient "
                            "can't use inplace strategy.",
                            target.name()));
168 169 170
    }
  }

171 172 173 174
  // View Strategy
  static void HandleViewBetweenInputAndOutput(
      const std::shared_ptr<EagerVariable>& input_var,
      const std::shared_ptr<EagerVariable>& view_output_var);
175 176 177
  static void HandleViewBetweenInputAndOutput(
      const paddle::experimental::Tensor& input_tensor,
      paddle::experimental::Tensor* view_output_tensor);
178

179
  // TensorWrapper Utils
180
  static paddle::experimental::Tensor RecoverTensorWrapper(TensorWrapper* tw);
181
  static std::vector<paddle::experimental::Tensor> RecoverTensorWrapper(
182
      std::vector<TensorWrapper>* tw);
183

184
  // Intermidate needed remove this once we don't need legacy
185
  // Inner Method
186
  static std::shared_ptr<egr::EagerVariable> TrySyncToVar(
187 188
      const paddle::experimental::Tensor& tensor);
  // Basic Input
189
  static std::vector<std::shared_ptr<egr::EagerVariable>> TrySyncToVars(
190 191
      const paddle::experimental::Tensor& tensor);
  // Basic Output
192
  static std::vector<std::shared_ptr<egr::EagerVariable>> TrySyncToVars(
193 194
      paddle::experimental::Tensor* tensor);
  // Multi Output
195
  static std::vector<std::shared_ptr<egr::EagerVariable>> TrySyncToVars(
196 197
      const std::vector<paddle::experimental::Tensor*>& tensors);
  // Multi Input
198
  static std::vector<std::shared_ptr<egr::EagerVariable>> TrySyncToVars(
199 200
      const std::vector<paddle::experimental::Tensor>& tensors);
  // Construct empty output
201 202
  static std::vector<std::shared_ptr<EagerVariable>> CreateVars(
      const size_t num);
203 204
  // Construct Tensor From var
  static std::vector<paddle::experimental::Tensor> GetOutputs(
205
      const std::vector<std::shared_ptr<EagerVariable>>& outs);
206
  static paddle::experimental::Tensor GetOutput(
207
      const std::shared_ptr<EagerVariable>& out);
208 209 210
  static void GetOutput(const std::shared_ptr<EagerVariable>& out,
                        paddle::experimental::Tensor* out_var);
  static void GetOutputs(
211
      const std::vector<std::shared_ptr<EagerVariable>>& outs,
212 213 214 215 216 217 218 219 220 221 222 223 224 225
      std::vector<paddle::experimental::Tensor>* result);
  static void GetOutputs(
      const std::vector<std::shared_ptr<EagerVariable>>& outs,
      const std::vector<paddle::experimental::Tensor*>& out_var);
  static void GetOutputs(const std::shared_ptr<EagerVariable>& out,
                         std::vector<paddle::experimental::Tensor>* result);
  static void GetOutputs(
      const std::shared_ptr<EagerVariable>& out,
      const std::vector<paddle::experimental::Tensor*>& out_var);

  static void Output2Result(
      const std::vector<paddle::experimental::Tensor*>& out_var,
      std::vector<paddle::experimental::Tensor>* result);

226 227 228 229
  // end Intermidate needed

  static void CheckAndRetainGrad(const paddle::experimental::Tensor& tensor);
  static void CheckAndRetainGrad(
230
      const std::vector<paddle::experimental::Tensor>& tensors);
W
wanghuancoder 已提交
231 232
  static void CheckAndRetainGrad(
      const std::vector<paddle::experimental::Tensor*>& tensors);
233

234 235
  static std::shared_ptr<egr::GradNodeBase> GetGradAccumulationNode(
      const paddle::experimental::Tensor& tensor);
236 237

  /**
238 239
   * Fill Zero
   * **/
W
wanghuancoder 已提交
240 241 242
  static void FillZeroForEmptyOptionalGradInput(
      std::vector<paddle::experimental::Tensor>* in_grads,
      const std::vector<GradSlotMeta>& grad_in_metas);
243 244 245 246 247 248 249
  static void FillZeroForEmptyGradInput(paddle::experimental::Tensor* in_grad,
                                        const GradSlotMeta& grad_in_meta);
  static void FillZeroForEmptyOptionalGradInput(
      paddle::experimental::Tensor* in_grad, const GradSlotMeta& grad_in_meta);
  static void FillZeroForEmptyGradInput(
      std::vector<paddle::experimental::Tensor>* in_grads,
      const std::vector<GradSlotMeta>& grad_in_metas);
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 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 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  /**
   * Print Input Output (level 0 means least info, level 2 means most info)
   * **/
  static const std::string TensorStr(const paddle::experimental::Tensor& t) {
    std::string tensor_name_str = "";
    if (t.name() == "") {
      tensor_name_str = "None";
    } else {
      tensor_name_str = t.name();
    }
    const char* TENSOR_INFO_TEMPLATE =
        "{ Type: [ \"%s\" ], Dtype:[ \"%s\" ], Place:[ \"%s\" ] }";
    std::string tensor_info_str = "";
    if (t.defined()) {
      if (t.initialized()) {
        tensor_info_str += paddle::string::Sprintf(TENSOR_INFO_TEMPLATE,
                                                   t.impl()->type_info().name(),
                                                   t.dtype(),
                                                   t.place().DebugString());
      } else {
        tensor_info_str += paddle::string::Sprintf(TENSOR_INFO_TEMPLATE,
                                                   t.impl()->type_info().name(),
                                                   "Unknown",
                                                   "Unknown");
      }
    } else {
      tensor_info_str += "Unknown";
    }
    if (VLOG_IS_ON(6)) {
      const char* TENSOR_PRINT_TEMPLATE =
          "{ Name:[ \"%s\" ], Initialized: [ \"%d\" ], Ptr: [ \"%d\" ] "
          "TensorInfo: [ \"%s\" ], ADInfo:[ \"%s\" ] }";
      auto* ad_meta = nullable_autograd_meta(t);
      if (!ad_meta && !(ad_meta->WeakGrad().lock().get())) {
        std::string ad_info_str = "";
        const char* AD_INFO_TEMPLATE =
            "{ Grad: [ \"%s\" ],  GradNode: [ %s ], StopGradient: [ %d ] }";
        ad_info_str += paddle::string::Sprintf(AD_INFO_TEMPLATE,
                                               TensorStr(ad_meta->Grad()),
                                               GradNodeStr(t),
                                               ad_meta->StopGradient());
        return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
                                       tensor_name_str,
                                       t.initialized(),
                                       t.impl(),
                                       tensor_info_str,
                                       ad_info_str);
      } else {
        return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
                                       tensor_name_str,
                                       t.initialized(),
                                       t.impl(),
                                       tensor_info_str,
                                       "None");
      }
    } else if (VLOG_IS_ON(5)) {
      const char* TENSOR_PRINT_TEMPLATE =
          "{ Name:[ \"%s\" ], Initialized: [ \"%d\" ], Ptr: [ \"%d\" ] "
          "TensorInfo: [ \"%s\" ] }";
      return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
                                     tensor_name_str,
                                     t.initialized(),
                                     t.impl(),
                                     tensor_info_str);
    } else if (VLOG_IS_ON(4)) {
      const char* TENSOR_PRINT_TEMPLATE =
          "{ Name:[ \"%s\" ], Initialized: [ \"%d\" ], Ptr: [ \"%d\" ] }";
      return paddle::string::Sprintf(
          TENSOR_PRINT_TEMPLATE, tensor_name_str, t.initialized(), t.impl());
    } else {
      return "[ Not specified tensor log level ]";
    }
  }

  static const std::string GradNodeStr(const egr::GradNodeBase& node) {
    if (VLOG_IS_ON(6)) {
      const char* GRAD_NODE_TEMPLATE =
          " { BackwardOutMeta: [ %s ], BackwardInMeta: [ %s ] }";
      const char* GRAD_SLOT_META_TEMPLATE = " {SlotSize: [%d]: %s} ";
      const char* SLOT_INFO_TEMPLATE =
          " {SlotID: [\"%s\"], StopGradients: [ %s ], Edges[ %s ] }";
      auto out_metas = node.OutputMeta();
      auto in_metas = node.InputMeta();
      std::string out_slot_str = "";
      std::string in_slot_str = "";
      const char* EDGE_INFO_TEMPLATE = " { [%d, %d]: [%s, %s] }, ";
      std::string slot_str = "";
      for (size_t i = 0; i < out_metas.size(); i++) {
        std::string edges_str = "";
        std::string sg_str = "";
        for (const GradSlotMeta& meta : out_metas[i]) {
          const egr::Edge& edge = meta.GetEdge();
          if (edge.IsInitialized()) {
            edges_str += paddle::string::Sprintf(EDGE_INFO_TEMPLATE,
                                                 edge.GetEdgeRankInfo().first,
                                                 edge.GetEdgeRankInfo().second,
                                                 edge.GetGradNode(),
                                                 edge.GetGradNode()->name());
          } else {
            edges_str += paddle::string::Sprintf("{ NULL Edge }");
          }
          sg_str += meta.IsStopGradient() ? "1, " : "0, ";
        }
        out_slot_str +=
            paddle::string::Sprintf(SLOT_INFO_TEMPLATE, i, sg_str, edges_str);
      }
      std::string out_meta_str = paddle::string::Sprintf(
          GRAD_SLOT_META_TEMPLATE, out_metas.size(), out_slot_str);

      for (size_t i = 0; i < in_metas.size(); i++) {
        std::string edges_str = "";
        std::string sg_str = "";
        for (const GradSlotMeta& meta : in_metas[i]) {
          edges_str += paddle::string::Sprintf("{ NULL Edge }");
          sg_str += meta.IsStopGradient() ? "1, " : "0, ";
        }
        in_slot_str +=
            paddle::string::Sprintf(SLOT_INFO_TEMPLATE, i, sg_str, edges_str);
      }
      std::string in_meta_str =
          paddle::string::Sprintf(GRAD_SLOT_META_TEMPLATE, in_slot_str);
      return paddle::string::Sprintf(
          GRAD_NODE_TEMPLATE, out_meta_str, in_meta_str);
    } else if (VLOG_IS_ON(5)) {
      const char* GRAD_NODE_TEMPLATE =
          " { BackwardOutMeta: [ %s ], BackwardInMeta: [ %s ] }";
      const char* GRAD_SLOT_META_TEMPLATE = "SlotSize: [\"%d\"]";
      std::string out_meta_str = paddle::string::Sprintf(
          GRAD_SLOT_META_TEMPLATE, node.OutputMeta().size());
      std::string in_meta_str = paddle::string::Sprintf(
          GRAD_SLOT_META_TEMPLATE, node.InputMeta().size());
      return paddle::string::Sprintf(
          GRAD_NODE_TEMPLATE, out_meta_str, in_meta_str);
    } else {
      return "[ Not specified grad node log level. ] ";
    }
  }

  static const std::string GradNodeStr(const paddle::experimental::Tensor& t) {
    auto* ad_meta = nullable_autograd_meta(t);
    if (ad_meta && !(ad_meta->GetMutableGradNode().get())) {
      return GradNodeStr((*ad_meta->GetMutableGradNode().get()));
    } else {
      return "None";
    }
  }

  static const std::string TensorStr(
      const std::vector<paddle::experimental::Tensor>& tensors) {
    std::string tensors_str = "";
    for (const auto& tensor : tensors) {
      tensors_str += TensorStr(tensor) + ", ";
    }
    return "[ " + tensors_str + " ]";
  }

  static const std::string TensorStr(
      const paddle::optional<paddle::experimental::Tensor>& t) {
    if (!t.is_initialized()) {
      return "{ UnDefinedTensor }";
    } else {
      return TensorStr((*t.get_ptr()));
    }
  }

  static const std::string TensorStr(
      const paddle::optional<std::vector<paddle::experimental::Tensor>>&
          tensors) {
    std::string tensors_str = "";
    if (!tensors.is_initialized()) {
      return "[ UnDefinedTensor List ]";
    } else {
      for (const auto& tensor : (*tensors.get_ptr())) {
        tensors_str += TensorStr(tensor) + ", ";
      }
      return "[ " + tensors_str + " ]";
    }
  }
428 429 430
};

}  // namespace egr