Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onnx operator layers #2101

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions include/lbann/layers/operator_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class OperatorLayer final : public data_type_layer<InputT, OutputT>
data_layout get_data_layout() const final;
El::Device get_device_allocation() const final;

#ifdef LBANN_HAS_ONNX
void fill_onnx_node(onnx::GraphProto& graph) const override;
#endif // LBANN_HAS_ONNX

void fp_compute() final;
void bp_compute() final;

Expand Down
29 changes: 28 additions & 1 deletion include/lbann/operators/declare_stateless_op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@

#include "lbann/proto/operators.pb.h"

#ifdef LBANN_HAS_ONNX
#define ADD_GET_ONNX_NODES_API() \
std::vector<onnx::NodeProto> get_onnx_nodes() const final \
{ \
return get_onnx_nodes_impl(*this); \
}
#else
#define ADD_GET_ONNX_NODES_API()
#endif // LBANN_HAS_ONNX

// These are all single-type operators.

#define LBANN_DECLARE_STATELESS_OPERATOR(OP_NAME, OP_STRING) \
Expand Down Expand Up @@ -64,6 +74,7 @@
ar(::cereal::make_nvp("Operator", \
::cereal::base_class<OperatorType>(this))); \
} \
ADD_GET_ONNX_NODES_API() \
void fp_compute(std::vector<ConstInputTensorType> const& inputs, \
std::vector<OutputTensorType> const& outputs) const final; \
void bp_compute( \
Expand Down Expand Up @@ -113,6 +124,7 @@
ar(::cereal::make_nvp("ElementwiseOperator", \
::cereal::base_class<OperatorType>(this))); \
} \
ADD_GET_ONNX_NODES_API() \
\
private: \
void \
Expand All @@ -128,6 +140,21 @@
} \
void do_fill_description(description&) const final \
{} \
}
};

namespace lbann {

#ifdef LBANN_HAS_ONNX
// Overloads of this function are used to implement the functions in
// the macro template above.
template <typename OperatorT>
std::vector<onnx::NodeProto> get_onnx_nodes_impl(OperatorT const& op)
{
// The default assumption is that we don't know how to represent
// this operator in ONNX terms yet.
return {};
}
#endif // LBANN_HAS_ONNX

} // namespace lbann
#endif // LBANN_INCLUDE_LBANN_OPERATORS_DECLARE_STATELESS_OP_HPP_INCLUDED
57 changes: 40 additions & 17 deletions include/lbann/operators/math/binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,57 @@

#include "lbann/operators/declare_stateless_op.hpp"

#ifdef LBANN_HAS_ONNX
#define LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(OP_NAME, \
OP_STRING, \
OP_ONNX_NAME) \
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(OP_NAME, OP_STRING); \
template <typename T, El::Device D> \
std::vector<onnx::NodeProto> get_onnx_nodes_impl( \
OP_NAME##Operator<T, D> const& op) \
{ \
std::vector<onnx::NodeProto> nodes(1UL); \
nodes.front().set_op_type(OP_ONNX_NAME); \
return nodes; \
}
#else
#define LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(OP_NAME, \
OP_STRING, \
OP_ONNX_NAME) \
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(OP_NAME, OP_STRING)
#endif // LBANN_HAS_ONNX

namespace lbann {

// Arithmetic operations
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Add, "add");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Subtract, "subtract");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Multiply, "multiply");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Divide, "divide");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Mod, "modulo");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Pow, "power");
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Add, "add", "Add")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Subtract, "subtract", "Sub")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Multiply, "multiply", "Mul")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Divide, "divide", "Div")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Mod, "modulo", "Mod")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Pow, "power", "Pow")
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(SafeDivide, "safe divide");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(SquaredDifference,
"squared difference");

// Comparison operations
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Max, "maximum");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Min, "minimum");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Equal, "equal");
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Max, "maximum", "Max")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Min, "minimum", "Min")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Equal, "equal", "Equal")
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(NotEqual, "not equal");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Less, "less than");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(LessEqual, "less than or equal");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(Greater, "greater than");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(GreaterEqual,
"greater than or equal");
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Less, "less than", "Less")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(LessEqual,
"less than or equal",
"LessOrEqual")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(Greater, "greater than", "Greater")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(GreaterEqual,
"greater than or equal",
"GreaterOrEqual")

// Logical operations
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(LogicalAnd, "logical and");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(LogicalOr, "logical or");
LBANN_DECLARE_STATELESS_ELEMENTWISE_OPERATOR(LogicalXor, "logical xor");
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(LogicalAnd, "logical and", "And")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(LogicalOr, "logical or", "Or")
LBANN_DECLARE_STATELESS_EWISE_ONNX_OP(LogicalXor, "logical xor", "Xor")

} // namespace lbann

Expand Down
165 changes: 163 additions & 2 deletions include/lbann/operators/math/binary_with_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
#include "lbann/operators/elementwise_operator.hpp"
#include "lbann/utils/cloneable.hpp"

#include "lbann/proto/operators.pb.h"
#ifdef LBANN_HAS_ONNX
#include <onnx/onnx_pb.h>
#endif // LBANN_HAS_ONNX


/** @file
*
Expand All @@ -50,6 +53,16 @@

#include "lbann/proto/operators.pb.h"

#ifdef LBANN_HAS_ONNX
#define ADD_GET_ONNX_NODES_API() \
std::vector<onnx::NodeProto> get_onnx_nodes() const final \
{ \
return get_onnx_nodes_impl(*this); \
}
#else
#define ADD_GET_ONNX_NODES_API()
#endif // LBANN_HAS_ONNX

// These are all single-type operators.

#define LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(OP_NAME, OP_STRING) \
Expand Down Expand Up @@ -88,6 +101,7 @@
::cereal::base_class<OperatorType>(this)), \
CEREAL_NVP(m_constant)); \
} \
ADD_GET_ONNX_NODES_API() \
DataT get_constant() const noexcept \
{ \
return m_constant; \
Expand Down Expand Up @@ -123,7 +137,7 @@ namespace lbann {
// x + c -- treated as commutative.
LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(AddConstant, "add constant");

// x + c -- treated as commutative.
// x * c -- treated as commutative.
LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(Scale, "scale");

// x - C -- yes, could be "plus -C", but so could 7-4 be 7+-4, but
Expand All @@ -149,5 +163,152 @@ LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(GreaterEqualConstant,
LBANN_DECLARE_BINARY_WITH_CONSTANT_OPERATOR(GreaterConstant,
"greater than constant");

#ifdef LBANN_HAS_ONNX
inline onnx::NodeProto get_constant_node(float val)
{
onnx::NodeProto const_node;
const_node.add_output("const_val");
const_node.set_domain("");
const_node.set_doc_string("Const value for binary with constant operations");
auto* const_val = const_node.add_attribute();
const_val->set_name("value_float");
const_val->set_type(onnx::AttributeProto::FLOAT);
const_val->set_f(val);
return const_node;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(AddConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Add");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PostConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto> get_onnx_nodes_impl(ScaleOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Mul");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PostConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(SubtractConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Sub");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PostConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(ConstantSubtractOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Sub");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PreConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(MaxConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Max");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PreConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(MinConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Min");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PreConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(EqualConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Equal");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PreConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(NotEqualConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(3UL);
nodes.front().set_op_type("Equal");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PreConstant");
nodes.at(1).set_op_type("Not");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(LessConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Less");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PostConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(LessEqualConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("LessOrEqual");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PostConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(GreaterConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("Greater");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PreConstant");
return nodes;
}

template <typename T, El::Device D>
std::vector<onnx::NodeProto>
get_onnx_nodes_impl(GreaterEqualConstantOperator<T, D> const op)
{
std::vector<onnx::NodeProto> nodes(2UL);
nodes.front().set_op_type("GreaterOrEqual");
nodes.back() = get_constant_node(El::To<float>(op.get_constant()));
nodes.back().set_op_type("PreConstant");
return nodes;
}
#endif // LBANN_HAS_ONNX

} // namespace lbann
#endif // LBANN_INCLUDE_LBANN_OPERATORS_BINARY_WITH_CONSTANT_HPP_INCLUDED
21 changes: 20 additions & 1 deletion include/lbann/operators/operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@

#include <google/protobuf/message.h>

#ifdef LBANN_HAS_ONNX
#include <onnx/onnx_pb.h>
#endif

#include <string>
#include <vector>

Expand Down Expand Up @@ -130,6 +134,10 @@ class Operator : public AbstractCloneableBase<Operator<InputT, OutputT, D>>,
template <typename ArchiveT>
void serialize(ArchiveT& ar);

#ifdef LBANN_HAS_ONNX
virtual std::vector<onnx::NodeProto> get_onnx_nodes() const;
#endif

///@}
/** @name Computational interface */
///@{
Expand Down Expand Up @@ -164,7 +172,7 @@ class Operator : public AbstractCloneableBase<Operator<InputT, OutputT, D>>,
virtual void set_proto_params(lbann_data::Operator&) const = 0;
/** @brief Concrete operator description. */
virtual void do_fill_description(Description&) const = 0;
};
}; // class Operator

template <typename InputT, typename OutputT, El::Device D>
void Operator<InputT, OutputT, D>::write_proto(
Expand Down Expand Up @@ -208,5 +216,16 @@ template <typename ArchiveT>
void Operator<InputT, OutputT, D>::serialize(ArchiveT& ar)
{}

#ifdef LBANN_HAS_ONNX
template <typename InputT, typename OutputT, El::Device D>
std::vector<onnx::NodeProto>
Operator<InputT, OutputT, D>::get_onnx_nodes() const
{
// The default assumption is that we don't know how to represent
// this operator in ONNX terms yet.
return {};
}
#endif

} // namespace lbann
#endif // LBANN_OPERATORS_OPERATOR_HPP_INCLUDED
Loading