2using System.Collections.Generic;
31 long m_hLayerNorm = 0;
36 List<int> m_rgShape =
new List<int>(4);
52 m_blobWork =
new Blob<T>(cuda, log);
54 m_blobMu =
new Blob<T>(cuda, log);
56 m_blobXmu =
new Blob<T>(cuda, log);
58 m_blobXmuSq =
new Blob<T>(cuda, log);
60 m_blobVar =
new Blob<T>(cuda, log);
62 m_blobStdev =
new Blob<T>(cuda, log);
64 m_blobStdevFull =
new Blob<T>(cuda, log);
81 if (m_hLayerNorm != 0)
83 m_cuda.FreeLayerNorm(m_hLayerNorm);
101 col.
Add(m_blobStdev);
102 col.
Add(m_blobStdevFull);
129 m_log.
WriteLine(
"WARNING: LayerNormLayer '" +
m_param.
name +
"' is using passthrough mode which is only used when debugging.");
139 int nAxes = colBottom[0].num_axes;
140 m_nCount = colBottom[0].count();
141 m_nOuterNum = colBottom[0].num;
142 m_nChannels = (nAxes == 2) ? 1 : colBottom[0].channels;
143 m_nInnerNum = (nAxes == 2) ? colBottom[0].channels : colBottom[0].count(2);
147 if (m_hLayerNorm == 0 || colBottom[0].count() != m_nCount || colBottom[0].num != m_nOuterNum || colBottom[0].channels != m_nChannels || colBottom[0].count(2) != m_nInnerNum)
149 if (m_hLayerNorm != 0)
150 m_cuda.FreeLayerNorm(m_hLayerNorm);
152 int nGpuID =
m_cuda.GetDeviceID();
154 if (m_hLayerNorm == 0)
155 m_log.
FAIL(
"Failed to create CUDA version LayerNorm!");
161 m_blobWork.ReshapeLike(colBottom[0]);
176 m_rgShape.Add(m_nOuterNum);
177 m_rgShape.Add(m_nChannels);
182 m_blobStdev.
Reshape(m_rgShape);
204 m_cuda.LayerNormForward(m_hLayerNorm, colBottom[0].gpu_data, colTop[0].mutable_gpu_data);
206 forward_local(colBottom, colTop);
215 m_cuda.channel_mean(m_nCount, m_nOuterNum, m_nChannels, m_nInnerNum, colBottom[0].gpu_data, m_blobMu.
mutable_gpu_data);
269 if (rgbPropagateDown[0])
273 colBottom[0].
CopyFrom(colTop[0],
true);
278 m_cuda.LayerNormBackward(m_hLayerNorm, colTop[0].gpu_data, colTop[0].gpu_diff, colBottom[0].mutable_gpu_diff);
280 backward_local(colTop, rgbPropagateDown, colBottom);
288 m_blobWork.ReshapeLike(colTop[0]);
289 m_cuda.mul(m_nCount, colTop[0].gpu_data, colTop[0].gpu_diff, m_blobWork.mutable_gpu_diff);
292 m_cuda.channel_mean(m_nCount, m_nOuterNum, m_nChannels, m_nInnerNum, m_blobWork.gpu_diff, m_blobVar.
mutable_gpu_diff);
295 m_cuda.channel_mean(m_nCount, m_nOuterNum, m_nChannels, m_nInnerNum, colTop[0].gpu_diff, m_blobStdev.
mutable_gpu_diff);
299 m_cuda.mul(m_nCount, colTop[0].gpu_data, m_blobStdevFull.
gpu_diff, m_blobWork.mutable_gpu_diff);
303 m_cuda.add(m_nCount, m_blobWork.gpu_diff, m_blobStdevFull.
gpu_diff, m_blobWork.mutable_gpu_diff);
307 m_cuda.sub(m_nCount, colTop[0].gpu_diff, m_blobWork.gpu_diff, m_blobWork.mutable_gpu_diff);
311 m_cuda.div(m_nCount, m_blobWork.gpu_diff, m_blobStdevFull.
gpu_data, colBottom[0].mutable_gpu_diff);
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
int Count
Returns the number of items in the collection.
void ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
void CopyFrom(BlobCollection< T > bSrc, bool bCopyDiff=false)
Copy the data or diff from another BlobCollection into this one.
The Blob is the main holder of data that moves through the Layers of the Net.
int channels
DEPRECIATED; legacy shape accessor channels: use shape(1) instead.
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
void add_scalar(double dfVal)
Adds a scalar value to the Blob.
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
string Name
Get/set the name of the Blob.
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
int num
DEPRECIATED; legacy shape accessor num: use shape(0) instead.
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
An interface for the units of computation which can be composed into a Net.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
bool shareLayerBlob(Blob< T > b, List< int > rgMinShape)
Attempts to share a Layer Blob if another parameter Blob with the same name and acceptable size is fo...
BlobCollection< T > m_colInternalBlobs
Specifies internal blobs used by the layer.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
LayerParameter.LayerType m_type
Specifies the Layer type.
The LayerNormalizationLayer performs layer normalization similar to the PyTorch LayerNorm layer.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Computes the forward calculation.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t the inputs.
LayerNormLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The LayerNormalizationLayer constructor.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: norm
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void dispose()
Releases all GPU and host resources used by the Layer.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: data
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
LayerNormParameter layer_norm_param
Returns the parameter set when initialized with LayerType.LAYERNORM
LayerType
Specifies the layer type.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.common namespace contains common MyCaffe classes.
DIR
Defines the direction of data flow.
The MyCaffe.layers.gpt namespace contains all GPT related layers.
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...