Poplar and PopLibs
NonLinearity.hpp
Go to the documentation of this file.
1// Copyright (c) 2016 Graphcore Ltd. All rights reserved.
6#ifndef popnn_NonLinearity_hpp
7#define popnn_NonLinearity_hpp
8
10
11#ifndef __POPC__
12#include <poplar/Graph.hpp>
13#include <poplar/Program.hpp>
14
15namespace popnn {
16
17#define DEF_NONLINEARITY_INPLACE(fn, nlType) \
18 inline void fn##InPlace(poplar::Graph &graph, poplar::Tensor t, \
19 poplar::program::Sequence &prog, \
20 const poplar::DebugContext &debugContext = {}) { \
21 nonLinearityInPlace(graph, nlType, t, prog, debugContext); \
22 } \
23 inline void fn##InPlace(poplar::Graph &graph, poplar::Tensor t, \
24 float &nonLinearityScaling, \
25 poplar::program::Sequence &prog, \
26 const poplar::DebugContext &debugContext = {}) { \
27 nonLinearityInPlace(graph, nlType, t, nonLinearityScaling, prog, \
28 debugContext); \
29 }
30
31#define DEF_NONLINEARITY_(fn, nlType) \
32 inline poplar::Tensor fn(poplar::Graph &graph, poplar::Tensor t, \
33 poplar::program::Sequence &prog, \
34 const poplar::DebugContext &debugContext = {}) { \
35 return nonLinearity(graph, nlType, t, prog, debugContext); \
36 } \
37 inline poplar::Tensor fn(poplar::Graph &graph, poplar::Tensor t, \
38 float &nonLinearityScaling, \
39 poplar::program::Sequence &prog, \
40 const poplar::DebugContext &debugContext = {}) { \
41 return nonLinearity(graph, nlType, t, nonLinearityScaling, prog, \
42 debugContext); \
43 }
44
45#define DEF_NONLINEARITY(fn, nlType) \
46 DEF_NONLINEARITY_INPLACE(fn, nlType) \
47 DEF_NONLINEARITY_(fn, nlType)
48
57void nonLinearityInPlace(poplar::Graph &graph,
58 NonLinearityType nonLinearityType, poplar::Tensor t,
60 const poplar::DebugContext &debugContext = {});
61
70void nonLinearityInPlace(poplar::Graph &graph,
71 NonLinearityType nonLinearityType, poplar::Tensor t,
73 const poplar::DebugContext &debugContext = {});
74
91void nonLinearityInPlace(poplar::Graph &graph,
92 NonLinearityType nonLinearityType, poplar::Tensor t,
93 float &nonLinearityScaling,
95 const poplar::DebugContext &debugContext = {});
96
113void nonLinearityInPlace(poplar::Graph &graph,
114 NonLinearityType nonLinearityType, poplar::Tensor t,
115 float &nonLinearityScaling, poplar::ComputeSet &cs,
116 const poplar::DebugContext &debugContext = {});
117
129poplar::Tensor nonLinearity(poplar::Graph &graph,
130 NonLinearityType nonLinearityType, poplar::Tensor t,
132 const poplar::DebugContext &debugContext = {});
133
153poplar::Tensor nonLinearity(poplar::Graph &graph,
154 NonLinearityType nonLinearityType, poplar::Tensor t,
155 float &nonLinearityScaling,
157 const poplar::DebugContext &debugContext = {});
158
159DEF_NONLINEARITY(sigmoid, NonLinearityType::SIGMOID)
160DEF_NONLINEARITY(relu, NonLinearityType::RELU)
161DEF_NONLINEARITY(tanh, NonLinearityType::TANH)
162DEF_NONLINEARITY(gelu, NonLinearityType::GELU)
163DEF_NONLINEARITY(swish, NonLinearityType::SWISH)
164DEF_NONLINEARITY(softmax, NonLinearityType::SOFTMAX)
165DEF_NONLINEARITY(softmaxStable, NonLinearityType::SOFTMAX_STABLE)
166DEF_NONLINEARITY(scaledSoftmaxStable, NonLinearityType::SOFTMAX_SCALED)
167
168
185nonLinearityInputGradient(poplar::Graph &graph,
186 NonLinearityType nonLinearityType, poplar::Tensor act,
187 poplar::Tensor outGradient, poplar::ComputeSet &cs,
188 const poplar::DebugContext &debugContext = {});
189
206poplar::Tensor nonLinearityInputGradient(
207 poplar::Graph &graph, NonLinearityType nonLinearityType, poplar::Tensor act,
208 poplar::Tensor outGradient, poplar::program::Sequence &prog,
209 const poplar::DebugContext &debugContext = {});
210
211} // end namespace popnn
212
213#endif // !__POPC__
214
215#endif // popnn_NonLinearity_hpp
Definitions for non-linearity operations.
A reference to a compute set within a graph.
Definition: GraphElements.hpp:131
DebugContext gathers the common external parameters of the context of an operation.
Definition: DebugContext.hpp:221
This class represents a graph program to be executed on the IPU.
Definition: Graph.hpp:52
A reference to a subset of tensor elements.
Definition: Tensor.hpp:38
Program that executes a sequence of programs.
Definition: Program.hpp:77
float sigmoid(float x)
The sigmoid function, ie 1/(1 + exp(- x )).
Definition: ipu_vector_math:1427
half2 tanh(half2 src)
Targets the f16v2tanh instruction.
Definition: ipu_intrinsics:546
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.
@ GELU
Gaussian Error Linear Unit:
@ SOFTMAX_SCALED
Same as SOFTMAX, but slower more numerically stable algorithm used.
@ RELU
Rectified Linear Unit: