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
9
#include <
popnn/NonLinearityDef.hpp
>
10
#include <
poputil/VertexTemplates.hpp
>
11
#include <
poputil/exceptions.hpp
>
12
13
namespace
popnn
{
14
15
inline
const
char
*asString(
const
popnn::NonLinearityType
&type) {
16
switch
(type) {
17
case
popnn::NonLinearityType::RELU
:
18
return
"relu"
;
19
case
popnn::NonLinearityType::SIGMOID
:
20
return
"sigmoid"
;
21
case
popnn::NonLinearityType::HARD_SIGMOID
:
22
return
"hard sigmoid"
;
23
case
popnn::NonLinearityType::TANH
:
24
return
"tanh"
;
25
case
popnn::NonLinearityType::GELU
:
26
return
"gelu"
;
27
case
popnn::NonLinearityType::SWISH:
28
return
"swish"
;
29
case
popnn::NonLinearityType::SOFTMAX
:
30
return
"softmax"
;
31
case
popnn::NonLinearityType::SOFTMAX_STABLE
:
32
return
"softmax (stable)"
;
33
case
popnn::NonLinearityType::SOFTMAX_SCALED
:
34
return
"softmax (scaled, stable)"
;
35
default
:
36
throw
poputil::poplibs_error
(
"Unsupported non-linearity type"
);
37
}
38
}
39
40
inline
std::ostream &
operator<<
(std::ostream &os,
41
const
popnn::NonLinearityType
&type) {
42
return
os << asString(type);
43
}
44
45
inline
std::istream &
operator>>
(std::istream &in,
46
popnn::NonLinearityType
&type) {
47
std::string token;
48
in >> token;
49
if
(token ==
"relu"
)
50
type =
popnn::NonLinearityType::RELU
;
51
else
if
(token ==
"sigmoid"
)
52
type =
popnn::NonLinearityType::SIGMOID
;
53
else
if
(token ==
"hard_sigmoid"
)
54
type =
popnn::NonLinearityType::HARD_SIGMOID
;
55
else
if
(token ==
"tanh"
)
56
type =
popnn::NonLinearityType::TANH
;
57
else
if
(token ==
"gelu"
)
58
type =
popnn::NonLinearityType::GELU
;
59
else
if
(token ==
"swish"
)
60
type = popnn::NonLinearityType::SWISH;
61
else
if
(token ==
"softmax"
)
62
type =
popnn::NonLinearityType::SOFTMAX
;
63
else
if
(token ==
"softmax_stable"
)
64
type =
popnn::NonLinearityType::SOFTMAX_STABLE
;
65
else
if
(token ==
"softmax_scaled"
)
66
type =
popnn::NonLinearityType::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
75
namespace
poputil
{
76
78
template
<>
struct
VertexTemplateToString<
popnn
::
NonLinearityType
> {
79
static
std::string to_string(
const
popnn::NonLinearityType
&nlType) {
80
switch
(nlType) {
81
case
popnn::NonLinearityType::SIGMOID
:
82
return
"popnn::NonLinearityType::SIGMOID"
;
83
case
popnn::NonLinearityType::RELU
:
84
return
"popnn::NonLinearityType::RELU"
;
85
case
popnn::NonLinearityType::TANH
:
86
return
"popnn::NonLinearityType::TANH"
;
87
case
popnn::NonLinearityType::GELU
:
88
return
"popnn::NonLinearityType::GELU"
;
89
case
popnn::NonLinearityType::SWISH:
90
return
"popnn::NonLinearityType::SWISH"
;
91
case
popnn::NonLinearityType::HARD_SIGMOID
:
92
return
"popnn::NonLinearityType::HARD_SIGMOID"
;
93
case
popnn::NonLinearityType::SOFTMAX
:
94
case
popnn::NonLinearityType::SOFTMAX_STABLE
:
95
case
popnn::NonLinearityType::SOFTMAX_SCALED
:
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_
NonLinearityDef.hpp
Definitions for non-linearity operations.
VertexTemplates.hpp
Generate a string describing a vertex type.
gcl::operator>>
std::istream & operator>>(std::istream &is, CollectiveOperator &op)
Parse token from input stream is to op.
gcl::operator<<
std::ostream & operator<<(std::ostream &os, const CollectiveOperator &op)
Write op to output stream os.
popnn
Functions used in neural networks.
Definition:
BatchNorm.hpp:14
popnn::NonLinearityType
NonLinearityType
Definition:
NonLinearityDef.hpp:11
popnn::NonLinearityType::TANH
@ TANH
Hyperbolic tangent:
popnn::NonLinearityType::SOFTMAX
@ SOFTMAX
Softmax:
popnn::NonLinearityType::SOFTMAX_STABLE
@ SOFTMAX_STABLE
Same as SOFTMAX, but slower more numerically stable algorithm used.
popnn::NonLinearityType::HARD_SIGMOID
@ HARD_SIGMOID
Hard Sigmoid:
popnn::NonLinearityType::GELU
@ GELU
Gaussian Error Linear Unit:
popnn::NonLinearityType::SOFTMAX_SCALED
@ SOFTMAX_SCALED
Same as SOFTMAX, but slower more numerically stable algorithm used.
popnn::NonLinearityType::SIGMOID
@ SIGMOID
Sigmoid:
popnn::NonLinearityType::RELU
@ RELU
Rectified Linear Unit:
poputil
General utility functions for building graphs.
Definition:
GfloatExprUtil.hpp:23
exceptions.hpp
Define a PopLibs exception.
poputil::poplibs_error
Class for PopLibs exceptions.
Definition:
exceptions.hpp:17
include
popnn
NonLinearityDefUtil.hpp
Generated by
1.9.3