Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmake/inference_lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ copy(inference_lib_dist
copy(inference_lib_dist
SRCS ${PADDLE_SOURCE_DIR}/paddle/fluid/platform/float16.h
DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/)
copy(inference_lib_dist
SRCS ${PADDLE_SOURCE_DIR}/paddle/utils/any.h
DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/)

# CAPI inference library for only inference
set(PADDLE_INFERENCE_C_INSTALL_DIR "${CMAKE_BINARY_DIR}/paddle_inference_c_install_dir" CACHE STRING
Expand Down
137 changes: 68 additions & 69 deletions paddle/fluid/extension/include/ext_op_meta_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ limitations under the License. */
#include <unordered_map>
#include <vector>

#include <boost/any.hpp>

#include "any.h"
#include "ext_dll_decl.h" // NOLINT
#include "ext_exception.h" // NOLINT
#include "ext_tensor.h" // NOLINT
Expand Down Expand Up @@ -83,7 +82,7 @@ inline std::string Vec(const std::string& t_name) {
using KernelFunc =
std::vector<Tensor> (*)(const std::vector<Tensor>& inputs,
const std::vector<std::vector<Tensor>>& vec_inputs,
const std::vector<boost::any>& attrs);
const std::vector<paddle::any>& attrs);

#define PD_SPECIALIZE_ComputeCallHelper(attr_type) \
template <typename... Tail> \
Expand All @@ -92,14 +91,14 @@ using KernelFunc =
typename... PreviousArgs> \
static Return Compute(const std::vector<Tensor>& inputs, \
const std::vector<std::vector<Tensor>>& vec_inputs, \
const std::vector<boost::any>& attrs, \
const std::vector<paddle::any>& attrs, \
const PreviousArgs&... pargs) { \
try { \
attr_type arg = boost::any_cast<attr_type>(attrs[attr_idx]); \
attr_type arg = paddle::any_cast<attr_type>(attrs[attr_idx]); \
return ComputeCallHelper<Tail...>::template Compute< \
in_idx, vec_in_idx, attr_idx + 1>(inputs, vec_inputs, attrs, \
pargs..., arg); \
} catch (boost::bad_any_cast&) { \
} catch (paddle::bad_any_cast&) { \
PD_THROW( \
"Attribute cast error in custom operator. Expected " #attr_type \
" value."); \
Expand All @@ -117,7 +116,7 @@ template <typename Return, typename... Args, Return (*impl_fn)(Args...)>
struct KernelFuncImpl<Return (*)(Args...), impl_fn> {
static Return Compute(const std::vector<Tensor>& inputs,
const std::vector<std::vector<Tensor>>& vec_inputs,
const std::vector<boost::any>& attrs) {
const std::vector<paddle::any>& attrs) {
return ComputeCallHelper<Args..., TypeTag<int>>::template Compute<0, 0, 0>(
inputs, vec_inputs, attrs);
}
Expand All @@ -132,7 +131,7 @@ struct KernelFuncImpl<Return (*)(Args...), impl_fn> {
typename... PreviousArgs>
static Return Compute(const std::vector<Tensor>& inputs,
const std::vector<std::vector<Tensor>>& vec_inputs,
const std::vector<boost::any>& attrs,
const std::vector<paddle::any>& attrs,
const PreviousArgs&... pargs) {
const Tensor& arg = inputs[in_idx];
return ComputeCallHelper<Tail...>::template Compute<in_idx + 1,
Expand All @@ -147,7 +146,7 @@ struct KernelFuncImpl<Return (*)(Args...), impl_fn> {
typename... PreviousArgs>
static Return Compute(const std::vector<Tensor>& inputs,
const std::vector<std::vector<Tensor>>& vec_inputs,
const std::vector<boost::any>& attrs,
const std::vector<paddle::any>& attrs,
const PreviousArgs&... pargs) {
const std::vector<Tensor>& arg = vec_inputs[vec_in_idx];
return ComputeCallHelper<Tail...>::template Compute<
Expand Down Expand Up @@ -189,7 +188,7 @@ struct KernelFuncImpl<Return (*)(Args...), impl_fn> {
template <int in_idx, int vec_in_idx, int attr_idx>
static Return Compute(const std::vector<Tensor>& inputs,
const std::vector<std::vector<Tensor>>& vec_inputs,
const std::vector<boost::any>& attrs,
const std::vector<paddle::any>& attrs,
const Args&... args) {
return impl_fn(args...);
}
Expand All @@ -205,67 +204,67 @@ struct KernelFuncImpl<Return (*)(Args...), impl_fn> {
using InferShapeFunc = std::vector<std::vector<int64_t>> (*)(
const std::vector<std::vector<int64_t>>& input_shapes,
const std::vector<std::vector<std::vector<int64_t>>>& vec_input_shapes,
const std::vector<boost::any>& attrs);

#define PD_SPECIALIZE_InferShapeCallHelper_FOR_SHAPE(input_type) \
template <typename... Tail> \
struct InferShapeCallHelper<input_type, Tail...> { \
template <int in_idx, int vec_in_idx, int attr_idx, \
typename... PreviousArgs> \
static Return InferShape( \
const std::vector<std::vector<int64_t>>& input_shapes, \
const std::vector<std::vector<std::vector<int64_t>>>& \
vec_input_shapes, \
const std::vector<boost::any>& attrs, const PreviousArgs&... pargs) { \
input_type arg = input_shapes[in_idx]; \
return InferShapeCallHelper<Tail...>::template InferShape< \
in_idx + 1, vec_in_idx, attr_idx>(input_shapes, vec_input_shapes, \
attrs, pargs..., arg); \
} \
const std::vector<paddle::any>& attrs);

#define PD_SPECIALIZE_InferShapeCallHelper_FOR_SHAPE(input_type) \
template <typename... Tail> \
struct InferShapeCallHelper<input_type, Tail...> { \
template <int in_idx, int vec_in_idx, int attr_idx, \
typename... PreviousArgs> \
static Return InferShape( \
const std::vector<std::vector<int64_t>>& input_shapes, \
const std::vector<std::vector<std::vector<int64_t>>>& \
vec_input_shapes, \
const std::vector<paddle::any>& attrs, const PreviousArgs&... pargs) { \
input_type arg = input_shapes[in_idx]; \
return InferShapeCallHelper<Tail...>::template InferShape< \
in_idx + 1, vec_in_idx, attr_idx>(input_shapes, vec_input_shapes, \
attrs, pargs..., arg); \
} \
}

#define PD_SPECIALIZE_InferShapeCallHelper_FOR_SHAPES(input_type) \
template <typename... Tail> \
struct InferShapeCallHelper<input_type, Tail...> { \
template <int in_idx, int vec_in_idx, int attr_idx, \
typename... PreviousArgs> \
static Return InferShape( \
const std::vector<std::vector<int64_t>>& input_shapes, \
const std::vector<std::vector<std::vector<int64_t>>>& \
vec_input_shapes, \
const std::vector<boost::any>& attrs, const PreviousArgs&... pargs) { \
input_type arg = vec_input_shapes[vec_in_idx]; \
return InferShapeCallHelper<Tail...>::template InferShape< \
in_idx, vec_in_idx + 1, attr_idx>(input_shapes, vec_input_shapes, \
attrs, pargs..., arg); \
} \
#define PD_SPECIALIZE_InferShapeCallHelper_FOR_SHAPES(input_type) \
template <typename... Tail> \
struct InferShapeCallHelper<input_type, Tail...> { \
template <int in_idx, int vec_in_idx, int attr_idx, \
typename... PreviousArgs> \
static Return InferShape( \
const std::vector<std::vector<int64_t>>& input_shapes, \
const std::vector<std::vector<std::vector<int64_t>>>& \
vec_input_shapes, \
const std::vector<paddle::any>& attrs, const PreviousArgs&... pargs) { \
input_type arg = vec_input_shapes[vec_in_idx]; \
return InferShapeCallHelper<Tail...>::template InferShape< \
in_idx, vec_in_idx + 1, attr_idx>(input_shapes, vec_input_shapes, \
attrs, pargs..., arg); \
} \
}

#define PD_SPECIALIZE_InferShapeCallHelper_FOR_ATTR(attr_type) \
template <typename... Tail> \
struct InferShapeCallHelper<attr_type, Tail...> { \
template <int in_idx, int vec_in_idx, int attr_idx, \
typename... PreviousArgs> \
static Return InferShape( \
const std::vector<std::vector<int64_t>>& input_shapes, \
const std::vector<std::vector<std::vector<int64_t>>>& \
vec_input_shapes, \
const std::vector<boost::any>& attrs, const PreviousArgs&... pargs) { \
try { \
attr_type arg = boost::any_cast<attr_type>(attrs[attr_idx]); \
return InferShapeCallHelper<Tail...>::template InferShape< \
in_idx, vec_in_idx, attr_idx + 1>(input_shapes, vec_input_shapes, \
attrs, pargs..., arg); \
} catch (boost::bad_any_cast&) { \
PD_THROW( \
"Attribute cast error in custom operator InferShapeFn. " \
"Expected " #attr_type \
" value. InferShapeFn's attribute list must be exactly same as " \
"Forward " \
"KernelFn's attribute list except std::vector<int64_t> " \
"attribute."); \
} \
} \
#define PD_SPECIALIZE_InferShapeCallHelper_FOR_ATTR(attr_type) \
template <typename... Tail> \
struct InferShapeCallHelper<attr_type, Tail...> { \
template <int in_idx, int vec_in_idx, int attr_idx, \
typename... PreviousArgs> \
static Return InferShape( \
const std::vector<std::vector<int64_t>>& input_shapes, \
const std::vector<std::vector<std::vector<int64_t>>>& \
vec_input_shapes, \
const std::vector<paddle::any>& attrs, const PreviousArgs&... pargs) { \
try { \
attr_type arg = paddle::any_cast<attr_type>(attrs[attr_idx]); \
return InferShapeCallHelper<Tail...>::template InferShape< \
in_idx, vec_in_idx, attr_idx + 1>(input_shapes, vec_input_shapes, \
attrs, pargs..., arg); \
} catch (paddle::bad_any_cast&) { \
PD_THROW( \
"Attribute cast error in custom operator InferShapeFn. " \
"Expected " #attr_type \
" value. InferShapeFn's attribute list must be exactly same as " \
"Forward " \
"KernelFn's attribute list except std::vector<int64_t> " \
"attribute."); \
} \
} \
}

template <typename F, F f>
Expand All @@ -276,7 +275,7 @@ struct InferShapeFuncImpl<Return (*)(Args...), impl_fn> {
static Return InferShape(
const std::vector<std::vector<int64_t>>& input_shapes,
const std::vector<std::vector<std::vector<int64_t>>>& vec_input_shapes,
const std::vector<boost::any>& attrs) {
const std::vector<paddle::any>& attrs) {
return InferShapeCallHelper<Args..., TypeTag<int>>::template InferShape<
0, 0, 0>(input_shapes, vec_input_shapes, attrs);
}
Expand Down Expand Up @@ -314,7 +313,7 @@ struct InferShapeFuncImpl<Return (*)(Args...), impl_fn> {
static Return InferShape(
const std::vector<std::vector<int64_t>>& input_shapes,
const std::vector<std::vector<std::vector<int64_t>>>& vec_input_shapes,
const std::vector<boost::any>& attrs, const Args&... args) {
const std::vector<paddle::any>& attrs, const Args&... args) {
return impl_fn(args...);
}
};
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ configure_file(commit.h.in commit.h)
# to avoid exposing the path of the underlying file
include_directories(${PADDLE_SOURCE_DIR}/paddle/fluid/platform)
include_directories(${PADDLE_SOURCE_DIR}/paddle/fluid/extension/include)
include_directories(${PADDLE_SOURCE_DIR}/paddle/utils)

if(WITH_ROCM)
hip_library(custom_tensor SRCS ../extension/src/ext_tensor.cc DEPS lod_tensor memory enforce)
Expand Down
5 changes: 3 additions & 2 deletions paddle/fluid/framework/custom_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ limitations under the License. */
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
#include "paddle/fluid/string/string_helper.h"
#include "paddle/utils/any.h"

namespace paddle {
namespace framework {
Expand Down Expand Up @@ -149,7 +150,7 @@ static void RunKernelFunc(const framework::ExecutionContext& ctx,
}
}

std::vector<boost::any> custom_attrs;
std::vector<paddle::any> custom_attrs;
for (auto& attr_str : attrs) {
auto attr_name_and_type = detail::ParseAttrStr(attr_str);
auto attr_name = attr_name_and_type[0];
Expand Down Expand Up @@ -605,7 +606,7 @@ void RegisterOperatorWithMetaInfo(
}
}

std::vector<boost::any> custom_attrs;
std::vector<paddle::any> custom_attrs;
for (auto& attr_str : op_attrs) {
auto attr_name_and_type = detail::ParseAttrStr(attr_str);
auto attr_name = attr_name_and_type[0];
Expand Down
7 changes: 4 additions & 3 deletions paddle/fluid/framework/ir/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License. */
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/variant.h"
#include "paddle/utils/any.h"

DECLARE_bool(convert_all_blocks);

Expand Down Expand Up @@ -147,8 +148,8 @@ class Graph {
platform::errors::PreconditionNotMet(
"%s attribute not registered for current graph.", attr_name));
try {
return *boost::any_cast<AttrType *>(attrs_.at(attr_name));
} catch (boost::bad_any_cast &) {
return *paddle::any_cast<AttrType *>(attrs_.at(attr_name));
} catch (paddle::bad_any_cast &) {
PADDLE_THROW(platform::errors::InvalidArgument(
"Invalid attribute type of %s, expected: %s, received: %s.",
attr_name, platform::demangle(typeid(AttrType *).name()), // NOLINT
Expand Down Expand Up @@ -426,7 +427,7 @@ class Graph {
const Graph *main_graph_; // not owned.
std::vector<std::unique_ptr<Graph>> sub_graphs_;

std::map<std::string, boost::any> attrs_;
std::map<std::string, paddle::any> attrs_;
std::map<std::string, std::function<void(void)>> attr_dels_;
std::map<ir::Node *, std::unique_ptr<ir::Node>> nodes_;
std::unordered_set<ir::Node *> node_set_;
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/framework/ir/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ limitations under the License. */
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/var_desc.h"
#include "paddle/fluid/platform/macros.h"

#include "paddle/utils/any.h"
namespace paddle {
namespace framework {
class OpDesc;
Expand Down Expand Up @@ -104,8 +104,8 @@ class Node {
template <typename T>
T& Wrapper() {
try {
return *boost::any_cast<T*>(wrapper_);
} catch (boost::bad_any_cast&) {
return *paddle::any_cast<T*>(wrapper_);
} catch (paddle::bad_any_cast&) {
PADDLE_THROW(platform::errors::InvalidArgument(
"Invalid wrapper type error, expected %s, actual %s.",
typeid(T).name(), wrapper_type_.name()));
Expand Down Expand Up @@ -277,7 +277,7 @@ class Node {

Node() = delete;

boost::any wrapper_;
paddle::any wrapper_;
std::function<void(void)> wrapper_deleter_;
std::type_index wrapper_type_ = std::type_index(typeid(void));

Expand Down
Loading