var_type.h 2.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
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
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14 15

#pragma once
Y
Yi Wang 已提交
16 17 18 19 20 21
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/lod_rank_table.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/lod_tensor_array.h"
#include "paddle/fluid/framework/reader.h"
#include "paddle/fluid/framework/selected_rows.h"
S
sneaxiy 已提交
22
#include "paddle/fluid/framework/var_type_traits.h"
Y
Yi Wang 已提交
23
#include "paddle/fluid/framework/variable.h"
24 25 26

namespace paddle {
namespace framework {
S
sneaxiy 已提交
27 28

template <typename T>
S
sneaxiy 已提交
29 30
inline bool IsType(const std::type_index& type) {
  return type == typeid(T);
S
sneaxiy 已提交
31 32
}

S
sneaxiy 已提交
33 34 35 36 37 38 39 40 41 42
inline proto::VarType::Type ToVarType(int type) {
  switch (type) {
    case proto::VarType::LOD_TENSOR:
    case proto::VarType::SELECTED_ROWS:
    case proto::VarType::LOD_RANK_TABLE:
    case proto::VarType::LOD_TENSOR_ARRAY:
    case proto::VarType::READER:
      return static_cast<proto::VarType::Type>(type);
    default:
      PADDLE_THROW("ToVarType:Unsupported type %d", type);
43 44 45
  }
}

Y
Yu Yang 已提交
46
template <typename Visitor>
Y
Yancey 已提交
47
inline void VisitVarType(const framework::Variable& var, Visitor visitor) {
S
sneaxiy 已提交
48
  switch (var.Type()) {
49
    case proto::VarType_Type_LOD_TENSOR:
F
fengjiayi 已提交
50
      visitor(var.Get<LoDTensor>());
Y
Yu Yang 已提交
51
      return;
52
    case proto::VarType_Type_LOD_RANK_TABLE:
Y
Yu Yang 已提交
53 54
      visitor(var.Get<LoDRankTable>());
      return;
55
    case proto::VarType_Type_LOD_TENSOR_ARRAY:
Y
Yu Yang 已提交
56 57
      visitor(var.Get<LoDTensorArray>());
      return;
58
    case proto::VarType_Type_SELECTED_ROWS:
Y
Yu Yang 已提交
59 60
      visitor(var.Get<SelectedRows>());
      return;
61
    case proto::VarType_Type_READER:
F
fengjiayi 已提交
62 63
      visitor(var.Get<ReaderHolder>());
      return;
Y
Yu Yang 已提交
64
    default:
S
sneaxiy 已提交
65
      PADDLE_THROW("Not supported visit type, %s", ToTypeName(var.Type()));
Y
Yu Yang 已提交
66 67 68
  }
}

69 70
}  // namespace framework
}  // namespace paddle