Skip to content

Commit 95296eb

Browse files
committed
[NeoML] CrossEntropyLossLayer mem-optimize
Signed-off-by: Kirill Golikov <[email protected]>
1 parent a2194e2 commit 95296eb

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

NeoML/include/NeoML/Dnn/Layers/LossLayer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class NEOML_API CLossLayer : public CBaseLayer {
142142
class NEOML_API CCrossEntropyLossLayer : public CLossLayer {
143143
NEOML_DNN_LAYER( CCrossEntropyLossLayer )
144144
public:
145-
explicit CCrossEntropyLossLayer( IMathEngine& mathEngine );
145+
explicit CCrossEntropyLossLayer( IMathEngine& mathEngine ) : CLossLayer( mathEngine, "CCnnCrossEntropyLossLayer" ) {}
146146

147147
// Indicates if softmax function should be applied to input data. True by default.
148148
// If you turn off the flag, make sure each vector you pass to the input contains only positive numbers making 1 in total.
@@ -160,7 +160,7 @@ class NEOML_API CCrossEntropyLossLayer : public CLossLayer {
160160
int labelSize, CFloatHandle lossValue, CFloatHandle lossGradient) override;
161161

162162
private:
163-
bool isSoftmaxApplied;
163+
bool isSoftmaxApplied = true;
164164
};
165165

166166
NEOML_API CLayerWrapper<CCrossEntropyLossLayer> CrossEntropyLoss(

NeoML/src/Dnn/Layers/CrossEntropyLossLayer.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright © 2017-2020 ABBYY Production LLC
1+
/* Copyright © 2017-2024 ABBYY
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -22,31 +22,23 @@ limitations under the License.
2222

2323
namespace NeoML {
2424

25-
CCrossEntropyLossLayer::CCrossEntropyLossLayer( IMathEngine& mathEngine ) :
26-
CLossLayer( mathEngine, "CCnnCrossEntropyLossLayer" ),
27-
isSoftmaxApplied( true )
28-
{
29-
}
30-
3125
void CCrossEntropyLossLayer::BatchCalculateLossAndGradient( int batchSize, CConstFloatHandle data, int vectorSize,
3226
CConstFloatHandle label, int labelSize, CFloatHandle lossValue, CFloatHandle lossGradient )
3327
{
3428
BatchCalculateLossAndGradient( batchSize, data, vectorSize, label, labelSize,
35-
lossValue, lossGradient, CFloatHandle() );
29+
lossValue, lossGradient, CFloatHandle{} );
3630
}
3731

3832
void CCrossEntropyLossLayer::BatchCalculateLossAndGradient( int batchSize, CConstFloatHandle data, int vectorSize,
3933
CConstFloatHandle label, int labelSize, CFloatHandle lossValue, CFloatHandle lossGradient, CFloatHandle labelLossGradient )
4034
{
4135
CheckLayerArchitecture( labelSize == vectorSize,
4236
"for float labels the dimensions should be equal to the first input dimensions" );
43-
44-
int totalSize = batchSize * vectorSize;
45-
4637
CheckLayerArchitecture( vectorSize >= 2, "CrossEntropyLoss layer works only with multi-class classification" );
4738

39+
const int totalSize = batchSize * vectorSize;
4840
CFloatHandleStackVar activation( MathEngine(), totalSize );
49-
CFloatHandleStackVar activationEltwiseMul( MathEngine(), totalSize );
41+
CFloatHandle activationEltwiseMul = lossGradient.IsNull() ? lossValue : lossGradient;
5042

5143
if( isSoftmaxApplied ) {
5244
MathEngine().MatrixSoftmaxByRows( data, batchSize, vectorSize, activation );
@@ -83,7 +75,7 @@ void CCrossEntropyLossLayer::BatchCalculateLossAndGradient( int batchSize, CCons
8375
}
8476

8577
// Put 0 for those elements for which the label sum is 0
86-
CFloatHandleStackVar& labelSum = activation;
78+
CFloatHandle labelSum = activation;
8779
MathEngine().SumMatrixColumns( labelSum, label, batchSize, vectorSize );
8880
MathEngine().MultiplyDiagMatrixByMatrix( labelSum, batchSize, activationEltwiseMul, vectorSize,
8981
lossGradient, totalSize );
@@ -94,13 +86,11 @@ void CCrossEntropyLossLayer::BatchCalculateLossAndGradient( int batchSize, CCons
9486
{
9587
CheckLayerArchitecture( labelSize == 1,
9688
"for int labels each object in the blob should contain the number of the class" );
97-
98-
int totalSize = batchSize * vectorSize;
99-
10089
CheckLayerArchitecture( vectorSize >= 2, "CrossEntropyLoss layer works only with multi-class classification" );
10190

102-
CFloatHandleStackVar activationMul( MathEngine(), batchSize );
91+
const int totalSize = batchSize * vectorSize;
10392
CFloatHandleStackVar activation( MathEngine(), totalSize );
93+
CFloatHandle activationMul = lossGradient.IsNull() ? lossValue : lossGradient;
10494

10595
if( isSoftmaxApplied ) {
10696
MathEngine().MatrixSoftmaxByRows( data, batchSize, vectorSize, activation );
@@ -115,7 +105,6 @@ void CCrossEntropyLossLayer::BatchCalculateLossAndGradient( int batchSize, CCons
115105

116106
MathEngine().VectorFill( activationMul, 0, batchSize );
117107
MathEngine().AddMatrixElementsToVector( activation, batchSize, vectorSize, label, activationMul, batchSize );
118-
119108
MathEngine().VectorNegLog( activationMul, lossValue, batchSize );
120109

121110
if( lossGradient.IsNull() ) {
@@ -141,18 +130,17 @@ void CCrossEntropyLossLayer::BatchCalculateLossAndGradient( int batchSize, CCons
141130
MathEngine().MultiplyDiagMatrixByMatrix( activationMul, batchSize, activation, vectorSize, lossGradient, totalSize );
142131
}
143132

144-
static const int CrossEntropyLossLayerVersion = 2000;
133+
constexpr int crossEntropyLossLayerVersion = 2000;
145134

146135
void CCrossEntropyLossLayer::Serialize( CArchive& archive )
147136
{
148-
archive.SerializeVersion( CrossEntropyLossLayerVersion, CDnn::ArchiveMinSupportedVersion );
137+
archive.SerializeVersion( crossEntropyLossLayerVersion, CDnn::ArchiveMinSupportedVersion );
149138
CLossLayer::Serialize( archive );
150139

151140
archive.Serialize( isSoftmaxApplied );
152141
}
153142

154-
CLayerWrapper<CCrossEntropyLossLayer> CrossEntropyLoss(
155-
bool isSoftmaxApplied, float lossWeight )
143+
CLayerWrapper<CCrossEntropyLossLayer> CrossEntropyLoss( bool isSoftmaxApplied, float lossWeight )
156144
{
157145
return CLayerWrapper<CCrossEntropyLossLayer>( "CrossEntropyLoss", [=]( CCrossEntropyLossLayer* result ) {
158146
result->SetApplySoftmax( isSoftmaxApplied );

0 commit comments

Comments
 (0)