variable_visitor.cc 6.0 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//   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/details/variable_visitor.h"
16

Y
Yu Yang 已提交
17
#include "paddle/fluid/framework/selected_rows.h"
W
wanghuancoder 已提交
18

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

W
wanghuancoder 已提交
23 24 25 26 27 28
namespace paddle {
namespace framework {
class Variable;
}  // namespace framework
}  // namespace paddle

Y
Yu Yang 已提交
29 30 31 32
namespace paddle {
namespace framework {
namespace details {
template <typename Func>
C
chengduoZH 已提交
33
static void VisitVariable(Variable* var, Func* func) {
Y
Yu Yang 已提交
34
  if (var->IsType<LoDTensor>()) {
C
chengduoZH 已提交
35
    (*func)(var->GetMutable<LoDTensor>());
Y
Yu Yang 已提交
36
  } else if (var->IsType<SelectedRows>()) {
C
chengduoZH 已提交
37
    (*func)(var->GetMutable<SelectedRows>());
Y
Yu Yang 已提交
38
  } else {
39 40 41
    PADDLE_THROW(platform::errors::Unimplemented(
        "VisitVariable is not supported for type %s.",
        ToTypeName(var->Type())));
Y
Yu Yang 已提交
42 43 44 45
  }
}

template <typename Func>
C
chengduoZH 已提交
46
static void VisitVariable(const Variable& var, Func* func) {
Y
Yu Yang 已提交
47
  if (var.IsType<LoDTensor>()) {
C
chengduoZH 已提交
48
    (*func)(var.Get<LoDTensor>());
Y
Yu Yang 已提交
49
  } else if (var.IsType<SelectedRows>()) {
C
chengduoZH 已提交
50
    (*func)(var.Get<SelectedRows>());
Y
Yu Yang 已提交
51
  } else {
52 53
    PADDLE_THROW(platform::errors::Unimplemented(
        "VisitVariable is not supported for type %s.", ToTypeName(var.Type())));
Y
Yu Yang 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67
  }
}

struct TensorVisitor {
  Tensor* result_{nullptr};

  void operator()(LoDTensor* tensor) { result_ = tensor; }

  void operator()(SelectedRows* selected_rows) {
    result_ = selected_rows->mutable_value();
  }

  template <typename T>
  void operator()() {
68 69
    PADDLE_THROW(platform::errors::Unimplemented(
        "Getting tensor from type %s is not supported.", typeid(T).name()));
Y
Yu Yang 已提交
70 71 72 73 74
  }
};

Tensor& VariableVisitor::GetMutableTensor(Variable* var) {
  TensorVisitor vistor;
C
chengduoZH 已提交
75
  VisitVariable(var, &vistor);
Y
Yu Yang 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  return *vistor.result_;
}

struct ShareDimsAndLoDVisitor {
  Variable* trg_;
  void operator()(const LoDTensor& val) {
    auto* tensor = trg_->GetMutable<LoDTensor>();
    tensor->set_layout(val.layout());
    tensor->set_lod(val.lod());
    tensor->Resize(val.dims());
  }

  void operator()(const SelectedRows& val) {
    auto* selected_rows = trg_->GetMutable<SelectedRows>();
    selected_rows->set_rows(val.rows());
    selected_rows->set_height(val.height());
    selected_rows->mutable_value()->Resize(val.value().dims());
  }

  template <typename T>
  void operator()(const T&) {
97 98
    PADDLE_THROW(platform::errors::Unimplemented(
        "ShareDimsAndLoD is not supported for type %s.", typeid(T).name()));
Y
Yu Yang 已提交
99 100 101 102 103
  }
};

void VariableVisitor::ShareDimsAndLoD(const Variable& src, Variable* trg) {
  ShareDimsAndLoDVisitor visitor{trg};
C
chengduoZH 已提交
104
  VisitVariable(src, &visitor);
Y
Yu Yang 已提交
105 106
}

C
chengduoZH 已提交
107
struct EnforceShapeAndDTypeEQVisitor {
108
  const Variable* dst_;
C
chengduoZH 已提交
109 110

  void operator()(const LoDTensor& src) {
111
    auto& tensor = dst_->Get<LoDTensor>();
112 113 114 115 116 117
    PADDLE_ENFORCE_EQ(
        src.place().GetType(), tensor.place().GetType(),
        platform::errors::PreconditionNotMet(
            "The place type of the two variables is not equal. The src place "
            "is %s, but the dst place is %s",
            src.place().DebugString(), tensor.place().DebugString()));
C
chengduoZH 已提交
118
    PADDLE_ENFORCE_EQ(src.type(), tensor.type(),
119 120 121 122 123 124
                      platform::errors::PreconditionNotMet(
                          "The dtype of the two variables is not equal."));
    PADDLE_ENFORCE_EQ(
        src.dims(), tensor.dims(),
        platform::errors::PreconditionNotMet(
            "The layout of the two variables' tensors is not equal."));
C
chengduoZH 已提交
125
    PADDLE_ENFORCE_EQ(src.lod(), tensor.lod(),
126 127 128 129 130 131
                      platform::errors::PreconditionNotMet(
                          "The lod of the two variable is not equal."));
    PADDLE_ENFORCE_EQ(
        src.layout(), tensor.layout(),
        platform::errors::PreconditionNotMet(
            "The layout of the two variables' tensors tensor is not equal."));
C
chengduoZH 已提交
132 133 134
  }

  void operator()(const SelectedRows& src) {
135
    auto& selected_rows = dst_->Get<SelectedRows>();
136 137 138 139 140 141
    PADDLE_ENFORCE_EQ(
        src.place().GetType(), selected_rows.place().GetType(),
        platform::errors::PreconditionNotMet(
            "The place type of the two variables is not equal. The src place "
            "is %s, but the dst place is %s",
            src.place().DebugString(), selected_rows.place().DebugString()));
C
chengduoZH 已提交
142
    PADDLE_ENFORCE_EQ(src.value().type(), selected_rows.value().type(),
143 144 145 146 147 148
                      platform::errors::PreconditionNotMet(
                          "The dtype of the two variables is not equal."));
    PADDLE_ENFORCE_EQ(
        src.value().layout(), selected_rows.value().layout(),
        platform::errors::PreconditionNotMet(
            "The layout of the two variables' tensors is not equal."));
C
chengduoZH 已提交
149
    PADDLE_ENFORCE_EQ(src.height(), selected_rows.height(),
150 151
                      platform::errors::PreconditionNotMet(
                          "The height of the two variables is not equal."));
C
chengduoZH 已提交
152
    PADDLE_ENFORCE_EQ(src.GetCompleteDims(), selected_rows.GetCompleteDims(),
153 154
                      platform::errors::PreconditionNotMet(
                          "The dims of the two variables is not equal."));
C
chengduoZH 已提交
155 156 157 158
  }

  template <typename T>
  void operator()(const T&) {
159 160 161
    PADDLE_THROW(platform::errors::Unimplemented(
        "EnforceShapeAndDTypeEQ is not supported for type %s.",
        typeid(T).name()));
C
chengduoZH 已提交
162 163 164 165 166
  }
};

void VariableVisitor::EnforceShapeAndDTypeEQ(const Variable& var1,
                                             const Variable& var2) {
C
chengduoZH 已提交
167
  EnforceShapeAndDTypeEQVisitor visitor{&var1};
C
chengduoZH 已提交
168 169 170
  VisitVariable(var2, &visitor);
}

Y
Yu Yang 已提交
171 172 173
}  // namespace details
}  // namespace framework
}  // namespace paddle