variable_visitor.cc 5.9 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

namespace paddle {
namespace framework {
21
class Tensor;
W
wanghuancoder 已提交
22 23 24 25
class Variable;
}  // namespace framework
}  // namespace paddle

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

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

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()() {
65 66
    PADDLE_THROW(platform::errors::Unimplemented(
        "Getting tensor from type %s is not supported.", typeid(T).name()));
Y
Yu Yang 已提交
67 68 69 70 71
  }
};

Tensor& VariableVisitor::GetMutableTensor(Variable* var) {
  TensorVisitor vistor;
C
chengduoZH 已提交
72
  VisitVariable(var, &vistor);
Y
Yu Yang 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  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&) {
94 95
    PADDLE_THROW(platform::errors::Unimplemented(
        "ShareDimsAndLoD is not supported for type %s.", typeid(T).name()));
Y
Yu Yang 已提交
96 97 98 99 100
  }
};

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

C
chengduoZH 已提交
104
struct EnforceShapeAndDTypeEQVisitor {
105
  const Variable* dst_;
C
chengduoZH 已提交
106 107

  void operator()(const LoDTensor& src) {
108
    auto& tensor = dst_->Get<LoDTensor>();
109 110 111 112 113 114
    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 已提交
115
    PADDLE_ENFORCE_EQ(src.type(), tensor.type(),
116 117 118 119 120 121
                      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 已提交
122
    PADDLE_ENFORCE_EQ(src.lod(), tensor.lod(),
123 124 125 126 127 128
                      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 已提交
129 130 131
  }

  void operator()(const SelectedRows& src) {
132
    auto& selected_rows = dst_->Get<SelectedRows>();
133 134 135 136 137 138
    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 已提交
139
    PADDLE_ENFORCE_EQ(src.value().type(), selected_rows.value().type(),
140 141 142 143 144 145
                      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 已提交
146
    PADDLE_ENFORCE_EQ(src.height(), selected_rows.height(),
147 148
                      platform::errors::PreconditionNotMet(
                          "The height of the two variables is not equal."));
C
chengduoZH 已提交
149
    PADDLE_ENFORCE_EQ(src.GetCompleteDims(), selected_rows.GetCompleteDims(),
150 151
                      platform::errors::PreconditionNotMet(
                          "The dims of the two variables is not equal."));
C
chengduoZH 已提交
152 153 154 155
  }

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

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

Y
Yu Yang 已提交
168 169 170
}  // namespace details
}  // namespace framework
}  // namespace paddle