Dragon - C++ API
A Computation Graph Virtual Machine Based Deep Learning Framework
Classes | Namespaces | Macros | Functions
operator.h File Reference
#include "core/registry.h"
#include "core/context.h"
#include "core/tensor.h"
#include "core/operator_gradient.h"
#include "core/operator_schema.h"
#include "utils/cast.h"
#include <mpi.h>

Go to the source code of this file.

Classes

class  dragon::OperatorBase
 
class  dragon::Operator< Context >
 
struct  dragon::TensorTypes< Types >
 
struct  dragon::DispatchHelper< Sizes, Args >
 

Namespaces

 dragon
 

Macros

#define OpArg   OperatorBase::Arg
 
#define OpArgs   OperatorBase::Args
 
#define SIMPLE_CTOR_DTOR(name)
 
#define USE_OPERATOR_BASE_FUNCTIONS
 
#define USE_OPERATOR_FUNCTIONS
 
#define XIsType(x, dtype)   x.template IsType<dtype>()
 
#define DEFINE_TENSOR_TYPES_DISPATCHER(TensorTypes, Impl)
 
#define TENSOR_FILL_WITH_TYPE(tensor, shape, type)
 
#define TENSOR_FILL(tensor, shape)
 
#define DECLARE_MULTIPLIER(name, size)
 
#define DECLARE_ARG_WITH_DESC(type, arg)
 
#define DECLARE_ARGS_WITH_DESC(type, arg)
 
#define GET_ARG_WITH_DESC(type, arg, default_value)
 
#define GET_ARGS_WITH_DESC(type, arg)
 
#define DEFINE_ARG_WITH_DESC(type, classname, arg)
 
#define DEFINE_ARGS_WITH_DESC(type, classname, arg)
 
#define GET_ARGS_SIZE(arg)   (int)std::max(arg##_.size(), arg##_desc_.size())
 
#define INSTANTIATE_OPERATOR(name, context)   template class name##Op<context>;
 
#define INSTANTIATE_CUDNN_OPERATOR(name)   template class CuDNN##name##Op<CUDAContext>;
 
#define INSTANTIATE_CNML_OPERATOR(name)   template class CnML##name##Op<CNMLContext>;
 
#define REGISTER_CPU_OPERATOR(name, ...)   REGISTER_CLASS(CPUOperatorRegistry, name, __VA_ARGS__)
 
#define REGISTER_CUDA_OPERATOR(name, ...)   REGISTER_CLASS(CUDAOperatorRegistry, name, __VA_ARGS__)
 
#define REGISTER_CUDNN_OPERATOR(name, ...)   REGISTER_CLASS(CUDNNOperatorRegistry, name, __VA_ARGS__)
 
#define REGISTER_CNML_OPERATOR(name, ...)   REGISTER_CLASS(CNMLOperatorRegistry, name, __VA_ARGS__)
 
#define DEPLOY_CPU(name)
 
#define DEPLOY_CUDA(name)
 
#define DEPLOY_CPU_CUDA(name)
 
#define DEPLOY_CUDNN(name)
 
#define DEPLOY_CNML(name)
 

Functions

OperatorBase * dragon::NewOperator (const OperatorDef &def, Workspace *ws)
 New a operator from the raw def. More...
 
 dragon::DECLARE_REGISTRY (CPUOperatorRegistry, OperatorBase, const OperatorDef &, Workspace *)
 
 dragon::DECLARE_REGISTRY (CUDAOperatorRegistry, OperatorBase, const OperatorDef &, Workspace *)
 
 dragon::DECLARE_REGISTRY (CUDNNOperatorRegistry, OperatorBase, const OperatorDef &, Workspace *)
 
 dragon::DECLARE_REGISTRY (CNMLOperatorRegistry, OperatorBase, const OperatorDef &, Workspace *)
 
 dragon::DEFINE_TENSOR_TYPES_DISPATCHER (TensorTypes, RunImpl)
 

Macro Definition Documentation

◆ DECLARE_ARG_WITH_DESC

#define DECLARE_ARG_WITH_DESC (   type,
  arg 
)
Value:
type arg##_; \
string arg##_desc_; \
type arg()

◆ DECLARE_ARGS_WITH_DESC

#define DECLARE_ARGS_WITH_DESC (   type,
  arg 
)
Value:
vector<type> arg##_; \
vector<string> arg##_desc_; \
type arg(int i)

◆ DECLARE_MULTIPLIER

#define DECLARE_MULTIPLIER (   name,
  size 
)
Value:
const T* name; \
{ \
auto* mp = ws()->CreateTensor("/share/multiplier/" \
+ TypeMetaToString(TypeMeta::Make<T>())); \
if (size > mp->count()) { \
mp->Reshape({ size }); \
math::Set<T, Context>(size, cast::to<T>(1.f), \
mp->template mutable_data<T, Context>(), ctx()); \
} \
name = mp->template data<T, Context>(); \
}
const std::string TypeMetaToString(const TypeMeta &meta)
Definition: types.h:59

◆ DEFINE_ARG_WITH_DESC

#define DEFINE_ARG_WITH_DESC (   type,
  classname,
  arg 
)
Value:
template <class Context> \
type classname<Context>::arg() { \
if (arg##_desc_.empty()) return arg##_; \
auto* arg##T = ws()->GetTensor(arg##_desc_); \
CHECK(arg##T->template IsType<type>()) \
<< "\nThe type of " << #arg << " should be " << #type << "."; \
CHECK_EQ(arg##T->count(), 1) \
<< "\nThe argument of " << #arg << " should be a scalar."; \
return arg##T->template data<type, CPUContext>()[0]; \
}

◆ DEFINE_ARGS_WITH_DESC

#define DEFINE_ARGS_WITH_DESC (   type,
  classname,
  arg 
)
Value:
template <class Context> \
type classname<Context>::arg(int i) { \
if (arg##_desc_.empty()) { \
CHECK_LT(i, arg##_.size()) \
<< "\nExcepted the size of " << #arg \
<< " > " << i << ". (Got " \
<< arg##_.size() << ")."; \
return arg##_[i]; \
} \
CHECK_LT(i, arg##_desc_.size()) \
<< "\nExcepted the size of " << #arg \
<< " > " << i << ". (Got " \
<< arg##_desc_.size() << ")."; \
auto* arg##T = ws()->GetTensor( \
str::replace_first(arg##_desc_[i], \
"${HANDLE}", handle())); \
CHECK(arg##T->template IsType<type>()) \
<< "\nThe type of " << #arg << " should be " << #type << "."; \
CHECK_EQ(arg##T->count(), 1) \
<< "\nThe argument of " << #arg << " at pos(" \
<< i << ") should be a scalar."; \
return arg##T->template data<type, CPUContext>()[0]; \
}
std::string replace_first(const std::string &str, const std::string &pattern, const std::string &excepted)
Definition: string.h:51

◆ DEFINE_TENSOR_TYPES_DISPATCHER

#define DEFINE_TENSOR_TYPES_DISPATCHER (   TensorTypes,
  Impl 
)
Value:
template <typename T, typename... Types, typename... Args> \
struct DispatchHelper<TensorTypes<T, Types...>, Args...> { \
template <typename Op> \
static void Call(Op* op, const TypeMeta& meta, string& types) { \
if (meta.Match<T>()) return op->template Impl<T, Args...>(); \
types += " * " + TypeToString<T>() + ",\n"; \
return DispatchHelper<TensorTypes<Types...>, Args...> \
::Call(op, meta, types); \
} \
template <typename Op> \
static void Call(Op* op, const Tensor& tensor) { \
string types; return Call(op, tensor.meta(), types); \
} \
}; \
template <typename... Args> \
struct DispatchHelper<TensorTypes<>, Args...> { \
template <typename Op> \
static void Call(Op* op, const TypeMeta& meta, string& types) { \
LOG(FATAL) << "Unsupported DType: " \
<< TypeMetaToString(meta) << "\n" \
<< "<" << op->type() << "Op>" \
<< " supports the following dtypes: {\n" \
<< types << "}"; \
} \
template <typename Op> \
static void Call(Op* op, const Tensor& tensor) { \
return Call(op, tensor.meta(), ""); \
} \
};
Definition: logging.h:21
const std::string TypeMetaToString(const TypeMeta &meta)
Definition: types.h:59
const std::string TypeToString()
Definition: types.h:77

◆ DEPLOY_CNML

#define DEPLOY_CNML (   name)
Value:
REGISTER_CNML_OPERATOR(name, CnML##name##Op<CNMLContext>); \
INSTANTIATE_CNML_OPERATOR(name);
#define REGISTER_CNML_OPERATOR(name,...)
Definition: operator.h:476

◆ DEPLOY_CPU

#define DEPLOY_CPU (   name)
Value:
REGISTER_CPU_OPERATOR(name, name##Op<CPUContext>); \
INSTANTIATE_OPERATOR(name, CPUContext);
#define REGISTER_CPU_OPERATOR(name,...)
Definition: operator.h:467

◆ DEPLOY_CPU_CUDA

#define DEPLOY_CPU_CUDA (   name)
Value:
REGISTER_CPU_OPERATOR(name, name##Op<CPUContext>); \
REGISTER_CUDA_OPERATOR(name, name##Op<CPUContext>); \
INSTANTIATE_OPERATOR(name, CPUContext); \
#define REGISTER_CPU_OPERATOR(name,...)
Definition: operator.h:467

◆ DEPLOY_CUDA

#define DEPLOY_CUDA (   name)
Value:
REGISTER_CUDA_OPERATOR(name, name##Op<CUDAContext>); \
INSTANTIATE_OPERATOR(name, CUDAContext); \
#define REGISTER_CUDA_OPERATOR(name,...)
Definition: operator.h:470

◆ DEPLOY_CUDNN

#define DEPLOY_CUDNN (   name)
Value:
REGISTER_CUDNN_OPERATOR(name, CuDNN##name##Op<CUDAContext>); \
INSTANTIATE_CUDNN_OPERATOR(name);
#define REGISTER_CUDNN_OPERATOR(name,...)
Definition: operator.h:473

◆ GET_ARG_WITH_DESC

#define GET_ARG_WITH_DESC (   type,
  arg,
  default_value 
)
Value:
arg##_ = OpArg<type>(#arg, default_value); \
arg##_desc_ = OpArg<string>(string(#arg) + "_desc", "")

◆ GET_ARGS_SIZE

#define GET_ARGS_SIZE (   arg)    (int)std::max(arg##_.size(), arg##_desc_.size())

◆ GET_ARGS_WITH_DESC

#define GET_ARGS_WITH_DESC (   type,
  arg 
)
Value:
arg##_ = OpArgs<type>(#arg); \
arg##_desc_ = OpArgs<string>(string(#arg) + "_desc")

◆ INSTANTIATE_CNML_OPERATOR

#define INSTANTIATE_CNML_OPERATOR (   name)    template class CnML##name##Op<CNMLContext>;

◆ INSTANTIATE_CUDNN_OPERATOR

#define INSTANTIATE_CUDNN_OPERATOR (   name)    template class CuDNN##name##Op<CUDAContext>;

◆ INSTANTIATE_OPERATOR

#define INSTANTIATE_OPERATOR (   name,
  context 
)    template class name##Op<context>;

◆ OpArg

#define OpArg   OperatorBase::Arg

◆ OpArgs

#define OpArgs   OperatorBase::Args

◆ REGISTER_CNML_OPERATOR

#define REGISTER_CNML_OPERATOR (   name,
  ... 
)    REGISTER_CLASS(CNMLOperatorRegistry, name, __VA_ARGS__)

◆ REGISTER_CPU_OPERATOR

#define REGISTER_CPU_OPERATOR (   name,
  ... 
)    REGISTER_CLASS(CPUOperatorRegistry, name, __VA_ARGS__)

◆ REGISTER_CUDA_OPERATOR

#define REGISTER_CUDA_OPERATOR (   name,
  ... 
)    REGISTER_CLASS(CUDAOperatorRegistry, name, __VA_ARGS__)

◆ REGISTER_CUDNN_OPERATOR

#define REGISTER_CUDNN_OPERATOR (   name,
  ... 
)    REGISTER_CLASS(CUDNNOperatorRegistry, name, __VA_ARGS__)

◆ SIMPLE_CTOR_DTOR

#define SIMPLE_CTOR_DTOR (   name)
Value:
name(const OperatorDef& def, Workspace* ws) \
: Operator<Context>(def, ws) {} \
virtual ~name() {}

◆ TENSOR_FILL

#define TENSOR_FILL (   tensor,
  shape 
)
Value:
if (tensor.count() == 0) { \
CHECK(ws()->GetFiller(tensor.name())) \
<< "\nTensor(" << tensor.name() << ") is empty. \n" \
<< "may be specify a filler for it ?"; \
tensor.Reshape(shape); \
unique_ptr<Filler<T, Context>> filler( \
CreateFiller<T, Context>(*ws()->GetFiller(tensor.name()))); \
filler->Fill(&tensor, ctx()); \
} else { \
int64_t count = 1; \
for(int i = 0; i < shape.size(); i++) count *= shape[i]; \
CHECK_EQ(count, tensor.count()) \
<< "\nModel request " << "Tensor(" << tensor.name() << ")'s " \
<< "size is " << count << ", \n" \
<< "but now is " << tensor.count() << ", " \
<< "did you feed the incorrect Tensor before ?"; \
tensor.Reshape(shape); \
}

◆ TENSOR_FILL_WITH_TYPE

#define TENSOR_FILL_WITH_TYPE (   tensor,
  shape,
  type 
)
Value:
if (tensor.count() == 0) { \
CHECK(ws()->GetFiller(tensor.name())) \
<< "\nTensor(" << tensor.name() << ") is empty. \n" \
<< "may be specify a filler for it ?"; \
tensor.Reshape(shape); \
unique_ptr<Filler<type, Context>> filler( \
CreateFiller<type, Context>(*ws()->GetFiller(tensor.name()))); \
filler->Fill(&tensor, ctx()); \
} else { \
int64_t count = 1; \
for(int i = 0; i < shape.size(); i++) count *= shape[i]; \
CHECK_EQ(count, tensor.count()) \
<< "\nModel request " << "Tensor(" << tensor.name() << ")'s " \
<< "size is " << count << ", \n" \
<< "but now is " << tensor.count() << ", " \
<< "did you feed the incorrect Tensor before ?"; \
tensor.Reshape(shape); \
}

◆ USE_OPERATOR_BASE_FUNCTIONS

#define USE_OPERATOR_BASE_FUNCTIONS
Value:
using OperatorBase::ws; \
using OperatorBase::name; \
using OperatorBase::type; \
using OperatorBase::phase; \
using OperatorBase::handle; \
using OperatorBase::dtype; \
using OperatorBase::data_format; \
using OperatorBase::unique_name; \
using OperatorBase::def; \
using OperatorBase::X; \
using OperatorBase::Y; \
using OperatorBase::XSize; \
using OperatorBase::YSize; \
using OperatorBase::DebugString; \
using OperatorBase::DTypeString; \
using OperatorBase::SwitchToPhase

◆ USE_OPERATOR_FUNCTIONS

#define USE_OPERATOR_FUNCTIONS
Value:
using Operator<Context>::ctx; \
using Operator<Context>::AllowRun
#define USE_OPERATOR_BASE_FUNCTIONS
Definition: operator.h:243

◆ XIsType

#define XIsType (   x,
  dtype 
)    x.template IsType<dtype>()