op_kernel_type.h 3.4 KB
Newer Older
Q
QI JUN 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

#include "paddle/framework/data_layout.h"
#include "paddle/framework/data_type.h"
#include "paddle/framework/library_type.h"
Q
qiaolongfei 已提交
20
#include "paddle/platform/device_context.h"
Q
QI JUN 已提交
21 22 23 24 25 26 27 28
#include "paddle/platform/place.h"

namespace paddle {
namespace framework {

struct OpKernelType {
  struct Hash {
    size_t operator()(const OpKernelType& key) const {
D
dzhwinter 已提交
29 30 31 32 33 34
      int place = key.place_.which();
      int data_type = static_cast<int>(key.data_type_) << LEFT_SHIFT;
      int data_layout = static_cast<int>(key.data_layout_) << (LEFT_SHIFT * 2);
      int library_type = static_cast<int>(key.library_type_)
                         << (LEFT_SHIFT * 3);

D
dzhwinter 已提交
35 36
      std::hash<int> hasher;
      return hasher(place + data_type + data_layout + library_type);
Q
QI JUN 已提交
37 38 39
    }
  };

D
dzhwinter 已提交
40 41
  // place, data_type, library_type kinds less than 2^8
  constexpr static int LEFT_SHIFT = 8;
D
dzhwinter 已提交
42

Q
QI JUN 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  proto::DataType data_type_;
  DataLayout data_layout_;
  platform::Place place_;
  LibraryType library_type_;

  OpKernelType(proto::DataType data_type, platform::Place place,
               DataLayout data_layout = DataLayout::kAnyLayout,
               LibraryType library_type = LibraryType::kPlain)
      : data_type_(data_type),
        data_layout_(data_layout),
        place_(place),
        library_type_(library_type) {}

  OpKernelType(proto::DataType data_type,
               const platform::DeviceContext& dev_ctx,
               DataLayout data_layout = DataLayout::kAnyLayout,
               LibraryType library_type = LibraryType::kPlain)
      : data_type_(data_type),
        data_layout_(data_layout),
        place_(dev_ctx.GetPlace()),
        library_type_(library_type) {}

  bool operator==(const OpKernelType& o) const {
    return platform::places_are_same_class(place_, o.place_) &&
           data_type_ == o.data_type_ && data_layout_ == o.data_layout_ &&
           library_type_ == o.library_type_;
  }
Q
QI JUN 已提交
70 71

  bool operator!=(const OpKernelType& o) const { return !(*this == o); }
Q
QI JUN 已提交
72 73
};

Q
qiaolongfei 已提交
74 75 76 77 78 79 80 81
inline std::ostream& operator<<(std::ostream& os,
                                const OpKernelType& kernel_key) {
  os << "data_type[" << kernel_key.data_type_ << "]:data_layout["
     << kernel_key.data_layout_ << "]:place[" << kernel_key.place_
     << "]:library_type[" << kernel_key.library_type_ << "]";
  return os;
}

Q
QI JUN 已提交
82 83 84 85 86 87
inline std::string KernelTypeToString(const OpKernelType& kernel_key) {
  std::ostringstream stream;
  stream << kernel_key;
  return stream.str();
}

88 89 90 91
inline bool NeedTransformLayout(const DataLayout& l, const DataLayout& r) {
  return l != DataLayout::kAnyLayout && r != DataLayout::kAnyLayout && l != r;
}

92 93
inline bool TransFromNeeded(const OpKernelType& l, const OpKernelType& r) {
  return (!platform::places_are_same_class(l.place_, r.place_)) ||
94 95
         (l.data_type_ != r.data_type_) ||
         NeedTransformLayout(l.data_layout_, r.data_layout_);
96 97
}

Q
QI JUN 已提交
98 99
}  // namespace framework
}  // namespace paddle