variable_visitor.cc 5.6 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 18 19 20 21
#include "paddle/fluid/framework/selected_rows.h"
namespace paddle {
namespace framework {
namespace details {
template <typename Func>
C
chengduoZH 已提交
22
static void VisitVariable(Variable* var, Func* func) {
Y
Yu Yang 已提交
23
  if (var->IsType<LoDTensor>()) {
C
chengduoZH 已提交
24
    (*func)(var->GetMutable<LoDTensor>());
Y
Yu Yang 已提交
25
  } else if (var->IsType<SelectedRows>()) {
C
chengduoZH 已提交
26
    (*func)(var->GetMutable<SelectedRows>());
Y
Yu Yang 已提交
27
  } else {
28 29 30
    PADDLE_THROW(platform::errors::Unimplemented(
        "VisitVariable is not supported for type %s.",
        ToTypeName(var->Type())));
Y
Yu Yang 已提交
31 32 33 34
  }
}

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

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()() {
57 58
    PADDLE_THROW(platform::errors::Unimplemented(
        "Getting tensor from type %s is not supported.", typeid(T).name()));
Y
Yu Yang 已提交
59 60 61 62 63
  }
};

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

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

C
chengduoZH 已提交
96
struct EnforceShapeAndDTypeEQVisitor {
97
  const Variable* dst_;
C
chengduoZH 已提交
98 99

  void operator()(const LoDTensor& src) {
100 101 102 103
    auto& tensor = dst_->Get<LoDTensor>();
    PADDLE_ENFORCE_EQ(src.place().which(), tensor.place().which(),
                      platform::errors::PreconditionNotMet(
                          "The place type of the two variables is not equal."));
C
chengduoZH 已提交
104
    PADDLE_ENFORCE_EQ(src.type(), tensor.type(),
105 106 107 108 109 110
                      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 已提交
111
    PADDLE_ENFORCE_EQ(src.lod(), tensor.lod(),
112 113 114 115 116 117
                      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 已提交
118 119 120
  }

  void operator()(const SelectedRows& src) {
121 122 123 124
    auto& selected_rows = dst_->Get<SelectedRows>();
    PADDLE_ENFORCE_EQ(src.place().which(), selected_rows.place().which(),
                      platform::errors::PreconditionNotMet(
                          "The place type of the two variables is not equal."));
C
chengduoZH 已提交
125
    PADDLE_ENFORCE_EQ(src.value().type(), selected_rows.value().type(),
126 127 128 129 130 131
                      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 已提交
132
    PADDLE_ENFORCE_EQ(src.height(), selected_rows.height(),
133 134
                      platform::errors::PreconditionNotMet(
                          "The height of the two variables is not equal."));
C
chengduoZH 已提交
135
    PADDLE_ENFORCE_EQ(src.GetCompleteDims(), selected_rows.GetCompleteDims(),
136 137
                      platform::errors::PreconditionNotMet(
                          "The dims of the two variables is not equal."));
C
chengduoZH 已提交
138 139 140 141
  }

  template <typename T>
  void operator()(const T&) {
142 143 144
    PADDLE_THROW(platform::errors::Unimplemented(
        "EnforceShapeAndDTypeEQ is not supported for type %s.",
        typeid(T).name()));
C
chengduoZH 已提交
145 146 147 148 149
  }
};

void VariableVisitor::EnforceShapeAndDTypeEQ(const Variable& var1,
                                             const Variable& var2) {
C
chengduoZH 已提交
150
  EnforceShapeAndDTypeEQVisitor visitor{&var1};
C
chengduoZH 已提交
151 152 153
  VisitVariable(var2, &visitor);
}

Y
Yu Yang 已提交
154 155 156
}  // namespace details
}  // namespace framework
}  // namespace paddle