Poplar and PopLibs
NonLinearityDefUtil.hpp
Go to the documentation of this file.
1// Copyright (c) 2018 Graphcore Ltd. All rights reserved.
6#ifndef poplibs_NonLinearityDefUtil_hpp_
7#define poplibs_NonLinearityDefUtil_hpp_
8
12
13namespace popnn {
14
15inline const char *asString(const popnn::NonLinearityType &type) {
16 switch (type) {
18 return "relu";
20 return "sigmoid";
22 return "hard sigmoid";
24 return "tanh";
26 return "gelu";
27 case popnn::NonLinearityType::SWISH:
28 return "swish";
30 return "softmax";
32 return "softmax (stable)";
34 return "softmax (scaled, stable)";
35 default:
36 throw poputil::poplibs_error("Unsupported non-linearity type");
37 }
38}
39
40inline std::ostream &operator<<(std::ostream &os,
41 const popnn::NonLinearityType &type) {
42 return os << asString(type);
43}
44
45inline std::istream &operator>>(std::istream &in,
47 std::string token;
48 in >> token;
49 if (token == "relu")
51 else if (token == "sigmoid")
53 else if (token == "hard_sigmoid")
55 else if (token == "tanh")
57 else if (token == "gelu")
59 else if (token == "swish")
60 type = popnn::NonLinearityType::SWISH;
61 else if (token == "softmax")
63 else if (token == "softmax_stable")
65 else if (token == "softmax_scaled")
67 else
68 throw poputil::poplibs_error("Unsupported non-linearity type \'" + token +
69 "\'");
70 return in;
71}
72
73} // end namespace popnn
74
75namespace poputil {
76
78template <> struct VertexTemplateToString<popnn::NonLinearityType> {
79 static std::string to_string(const popnn::NonLinearityType &nlType) {
80 switch (nlType) {
82 return "popnn::NonLinearityType::SIGMOID";
84 return "popnn::NonLinearityType::RELU";
86 return "popnn::NonLinearityType::TANH";
88 return "popnn::NonLinearityType::GELU";
89 case popnn::NonLinearityType::SWISH:
90 return "popnn::NonLinearityType::SWISH";
92 return "popnn::NonLinearityType::HARD_SIGMOID";
96 default:
97 throw poputil::poplibs_error("Unsupported non-linearity type");
98 }
99 }
100};
101
102} // end namespace poputil
103
104#endif // poplibs_NonLinearityDefUtil_hpp_
Definitions for non-linearity operations.
Generate a string describing a vertex type.
std::istream & operator>>(std::istream &is, CollectiveOperator &op)
Parse token from input stream is to op.
std::ostream & operator<<(std::ostream &os, const CollectiveOperator &op)
Write op to output stream os.
Functions used in neural networks.
Definition: BatchNorm.hpp:14
NonLinearityType
Definition: NonLinearityDef.hpp:11
@ TANH
Hyperbolic tangent:
@ SOFTMAX_STABLE
Same as SOFTMAX, but slower more numerically stable algorithm used.
@ HARD_SIGMOID
Hard Sigmoid:
@ GELU
Gaussian Error Linear Unit:
@ SOFTMAX_SCALED
Same as SOFTMAX, but slower more numerically stable algorithm used.
@ RELU
Rectified Linear Unit:
General utility functions for building graphs.
Definition: GfloatExprUtil.hpp:23
Define a PopLibs exception.
Class for PopLibs exceptions.
Definition: exceptions.hpp:17