pybind_boost_headers.h 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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. */
#pragma once

#include <Python.h>

#include <vector>

#include "glog/logging.h"
#include "paddle/fluid/platform/variant.h"
R
Ruibiao Chen 已提交
22
#include "paddle/utils/variant.h"
23 24 25
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
R
Ruibiao Chen 已提交
26
// Cast paddle::variant for PyBind.
27
// Copy from
R
Ruibiao Chen 已提交
28

29 30 31 32 33 34 35 36 37 38 39 40 41
// https://github.com/pybind/pybind11/issues/576#issuecomment-269563199
namespace pybind11 {
namespace detail {

#if !defined(PYBIND11_HIDDEN)
#ifdef _WIN32
#define PYBIND11_HIDDEN __declspec(dllexport)
#else
#define PYBIND11_HIDDEN __attribute__((visibility("hidden")))
#endif
#endif

// Can be replaced by a generic lambda in C++14
R
Ruibiao Chen 已提交
42
struct PYBIND11_HIDDEN paddle_variant_caster_visitor {
43 44 45 46 47 48
  return_value_policy policy;
  handle parent;

  paddle_variant_caster_visitor(return_value_policy policy, handle parent)
      : policy(policy), parent(parent) {}

49 50 51 52
  template <class T,
            typename std::enable_if<!std::is_same<T, std::string>::value,
                                    bool>::type* = nullptr>
  handle operator()(T const& src) const {
53 54
    return make_caster<T>::cast(src, policy, parent);
  }
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

  template <class T,
            typename std::enable_if<std::is_same<T, std::string>::value,
                                    bool>::type* = nullptr>
  handle operator()(T const& src) const {
    try {
      return make_caster<T>::cast(src, policy, parent);
    } catch (std::exception& ex) {
      VLOG(4) << ex.what();
      VLOG(4) << src;
      // UnicodeDecodeError, src is not utf-8 encoded
      // see details:
      // https://github.com/pybind/pybind11/blob/master/docs/advanced/cast/strings.rst
      return PYBIND11_BYTES_FROM_STRING_AND_SIZE(src.data(), src.size());
    }
  }
71 72 73 74 75 76 77 78 79 80 81
};

template <class Variant>
struct paddle_variant_caster;

template <template <class...> class V, class... Ts>
struct paddle_variant_caster<V<Ts...>> {
  using Type = V<Ts...>;

  template <typename T>
  typename std::enable_if<
82 83
      !std::is_same<T, boost::detail::variant::void_>::value,
      bool>::type
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  try_load(handle src, bool convert) {
    auto caster = make_caster<T>();
    if (!load_success_ && caster.load(src, convert)) {
      load_success_ = true;

      if (std::is_same<T, std::vector<float>>::value) {
        auto caster_ints = make_caster<std::vector<int64_t>>();
        if (caster_ints.load(src, convert)) {
          VLOG(4) << "This value are floats and int64_ts satisfy "
                     "simultaneously, will set it's type to "
                     "std::vector<int64_t>";
          value = cast_op<std::vector<int64_t>>(caster_ints);
          return true;
        }
      }

100 101 102 103 104 105 106 107 108
      if (std::is_same<T, float>::value) {
        auto caster_int64 = make_caster<int64_t>();
        if (caster_int64.load(src, convert)) {
          VLOG(4) << "this value are float and int64 satisfy simula.";
          value = cast_op<int64_t>(caster_int64);
          return true;
        }
      }

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      value = cast_op<T>(caster);
      return true;
    }
    return false;
  }

  template <typename T>
  typename std::enable_if<std::is_same<T, boost::detail::variant::void_>::value,
                          bool>::type
  try_load(handle src, bool convert) {
    return false;
  }

  bool load(handle src, bool convert) {
    auto unused = {false, try_load<Ts>(src, convert)...};
    (void)(unused);
    return load_success_;
  }

128 129
  static handle cast(Type const& src,
                     return_value_policy policy,
130
                     handle parent) {
R
Ruibiao Chen 已提交
131 132 133 134 135
    /*
    auto paddle_variant_caster_visitor = [&](Type const& src)->handle {
      return make_caster<Type>::cast(src, policy, parent);
    }
    */
136
    paddle_variant_caster_visitor visitor(policy, parent);
R
Ruibiao Chen 已提交
137
    return paddle::visit(visitor, src);
138 139 140 141 142 143 144 145
  }

  PYBIND11_TYPE_CASTER(Type, _("Variant"));
  bool load_success_{false};
};

// Add specialization for concrete variant type
template <class... Args>
R
Ruibiao Chen 已提交
146 147
struct type_caster<paddle::variant<Args...>>
    : paddle_variant_caster<paddle::variant<Args...>> {};
148 149 150

}  // namespace detail
}  // namespace pybind11