Skip to content

Commit

Permalink
[NeoMLTest] Fix CFloatHandleStackVar (#1117)
Browse files Browse the repository at this point in the history
* [NeoMLTest] Fix gpu SpaceToDepthTest

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>

* [NeoMLTest] micro speed-up (CFloatHandleVar --> CFloatHandleStackVar)

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>

* [NeoMLTest] LossLayer Tests (CFloatHandleVar  --> CFloatHandleStackVar)

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>

---------

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>
  • Loading branch information
favorart authored Sep 12, 2024
1 parent 9d6e6f8 commit 8ecfe4f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 78 deletions.
141 changes: 74 additions & 67 deletions NeoML/src/Dnn/Layers/LossLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,39 +194,40 @@ template<class T>
float CLossLayer::testImpl(int batchSize, CConstFloatHandle data, int vectorSize, CTypedMemoryHandle<const T> label,
int labelSize, CConstFloatHandle dataDelta)
{
int totalSize = batchSize * vectorSize;
const int totalSize = batchSize * vectorSize;
CFloatHandleStackVar temp( MathEngine(), ( 2 * totalSize ) + ( 3 * batchSize ) + 1 );

CFloatHandleVar lossValue(MathEngine(), batchSize); // the function value in data point
CFloatHandleVar lossGradient(MathEngine(), totalSize); // the function gradient in data point
CFloatHandleVar dataShift(MathEngine(), totalSize); // the data + dataDelta point
CFloatHandleVar lossValueShift(MathEngine(), batchSize); // the function value in data + dataDelta point
CFloatHandleVar lossValueShiftApp(MathEngine(), batchSize); // the function approximation in data + dataDelta point
CFloatHandleStackVar l2( MathEngine() ); // L2-measure (lossValueShiftApp - lossValueShift)
CFloatHandle lossValue = temp; // the function value in data point
CFloatHandle lossGradient = lossValue + batchSize; // the function gradient in data point
CFloatHandle dataShift = lossGradient + totalSize; // the data + dataDelta point
CFloatHandle lossValueShift = dataShift + totalSize; // the function value in data + dataDelta point
CFloatHandle lossValueShiftApp = lossValueShift + batchSize; // the function approximation in data + dataDelta point
CFloatHandle l2 = lossValueShiftApp + batchSize; // L2-measure (lossValueShiftApp - lossValueShift)

CPtr<CDnnBlob> oldWeights = weights;
weights = CDnnBlob::CreateVector(MathEngine(), CT_Float, batchSize);
weights->Fill(1);

// Estimate
BatchCalculateLossAndGradient(batchSize, data, vectorSize,
label, labelSize, lossValue.GetHandle(), lossGradient.GetHandle());
label, labelSize, lossValue, lossGradient);

MathEngine().VectorAdd(data, dataDelta, dataShift.GetHandle(), totalSize);
BatchCalculateLossAndGradient(batchSize, dataShift.GetHandle(), vectorSize,
label, labelSize, lossValueShift.GetHandle(), CFloatHandle());
MathEngine().VectorAdd(data, dataDelta, dataShift, totalSize);
BatchCalculateLossAndGradient(batchSize, dataShift, vectorSize,
label, labelSize, lossValueShift, CFloatHandle{});

for(int i = 0; i < batchSize; ++i) {
MathEngine().VectorDotProduct(lossGradient.GetHandle() + i * vectorSize,
dataDelta + i * vectorSize, vectorSize, lossValueShiftApp.GetHandle() + i);
MathEngine().VectorDotProduct(lossGradient + i * vectorSize,
dataDelta + i * vectorSize, vectorSize, lossValueShiftApp + i);
}
MathEngine().VectorAdd(lossValueShiftApp.GetHandle(), lossValue.GetHandle(),
lossValueShiftApp.GetHandle(), batchSize);
MathEngine().VectorSub(lossValueShiftApp.GetHandle(), lossValueShift.GetHandle(),
lossValueShiftApp.GetHandle(), batchSize);
MathEngine().VectorDotProduct(lossValueShiftApp.GetHandle(), lossValueShiftApp.GetHandle(),
batchSize, l2.GetHandle());
MathEngine().VectorAdd(lossValueShiftApp, lossValue,
lossValueShiftApp, batchSize);
MathEngine().VectorSub(lossValueShiftApp, lossValueShift,
lossValueShiftApp, batchSize);
MathEngine().VectorDotProduct(lossValueShiftApp, lossValueShiftApp,
batchSize, l2);

float res = l2.GetHandle().GetValue() / batchSize;
float res = l2.GetValue() / batchSize;

weights = oldWeights; // restore the old weight values

Expand All @@ -248,67 +249,73 @@ float CLossLayer::Test(int batchSize, CConstFloatHandle data, int vectorSize, CC
float CLossLayer::TestRandom(CRandom& random, int batchSize, float dataLabelMin, float dataLabelMax, float deltaAbsMax,
int vectorSize)
{
int totalSize = batchSize * vectorSize;
NeoAssert( batchSize > 0 && vectorSize > 0 );
NeoAssert( dataLabelMin < dataLabelMax && deltaAbsMax > 0 );

CArray<float> temp;
const int totalSize = batchSize * vectorSize;
CFloatHandleStackVar temp( MathEngine(), totalSize * 3 );

CFloatHandleVar data( MathEngine(), totalSize );
temp.SetSize(totalSize);
for(int i = 0; i < totalSize; ++i) {
temp[i] = (float)random.Uniform(dataLabelMin, dataLabelMax);
}
MathEngine().DataExchangeTyped(data.GetHandle(), temp.GetPtr(), totalSize);
CFloatHandle data = temp;
CFloatHandle label = data + totalSize;
CFloatHandle delta = label + totalSize;
{
CArray<float> buf;
buf.SetSize( totalSize );

CFloatHandleVar label( MathEngine(), totalSize );
temp.SetSize(totalSize);
for(int i = 0; i < totalSize; ++i) {
temp[i] = (float)random.Uniform(dataLabelMin, dataLabelMax);
}
MathEngine().DataExchangeTyped(label.GetHandle(), temp.GetPtr(), totalSize);
for( int i = 0; i < totalSize; ++i ) {
buf[i] = static_cast<float>( random.Uniform(dataLabelMin, dataLabelMax) );
}
MathEngine().DataExchangeTyped(data, buf.GetPtr(), totalSize);

NeoAssert(deltaAbsMax > 0);
CFloatHandleVar delta( MathEngine(), totalSize );
temp.SetSize(totalSize);
for(int i = 0; i < totalSize; ++i) {
temp[i] = (float)random.Uniform(-deltaAbsMax, deltaAbsMax);
}
MathEngine().DataExchangeTyped(delta.GetHandle(), temp.GetPtr(), totalSize);
for( int i = 0; i < totalSize; ++i ) {
buf[i] = static_cast<float>( random.Uniform(dataLabelMin, dataLabelMax) );
}
MathEngine().DataExchangeTyped(label, buf.GetPtr(), totalSize);

return Test(batchSize, data.GetHandle(), vectorSize, label.GetHandle(), vectorSize, delta.GetHandle());
for( int i = 0; i < totalSize; ++i ) {
buf[i] = static_cast<float>( random.Uniform(-deltaAbsMax, deltaAbsMax) );
}
MathEngine().DataExchangeTyped(delta, buf.GetPtr(), totalSize);
}
return Test(batchSize, data, vectorSize, label, vectorSize, delta);
}

float CLossLayer::TestRandom(CRandom& random, int batchSize, float dataMin, float dataMax, int labelMax, float deltaAbsMax,
int vectorSize)
float CLossLayer::TestRandom( CRandom& random, int batchSize, float dataMin, float dataMax, int labelMax, float deltaAbsMax,
int vectorSize )
{
int totalSize = batchSize * vectorSize;
NeoAssert( batchSize > 0 && vectorSize > 0 );
NeoAssert( dataMin < dataMax && labelMax > 0 && deltaAbsMax > 0 );

CArray<float> temp;
const int totalSize = batchSize * vectorSize;
CFloatHandleStackVar temp( MathEngine(), totalSize * 2 );

CFloatHandleVar data( MathEngine(), totalSize );
temp.SetSize(totalSize);
for(int i = 0; i < totalSize; ++i) {
temp[i] = (float)random.Uniform(dataMin, dataMax);
}
MathEngine().DataExchangeTyped(data.GetHandle(), temp.GetPtr(), totalSize);
CFloatHandle data = temp;
CFloatHandle delta = data + totalSize;
{
CArray<float> buf;
buf.SetSize( totalSize );

NeoAssert(labelMax > 0);
CPtr<CDnnBlob> label = CDnnBlob::CreateVector(MathEngine(), CT_Int, batchSize);
CArray<int> tempInt;
tempInt.SetSize(batchSize);
for(int i = 0; i < batchSize; ++i) {
tempInt[i] = random.UniformInt(0, labelMax - 1);
}
MathEngine().DataExchangeTyped(label->GetData<int>(), tempInt.GetPtr(), batchSize);
for( int i = 0; i < totalSize; ++i ) {
buf[i] = static_cast<float>( random.Uniform(dataMin, dataMax) );
}
MathEngine().DataExchangeTyped(data, buf.GetPtr(), totalSize);

NeoAssert(deltaAbsMax > 0);
CFloatHandleVar delta( MathEngine(), totalSize );
temp.SetSize(totalSize);
for(int i = 0; i < totalSize; ++i) {
temp[i] = (float)random.Uniform(-deltaAbsMax, deltaAbsMax);
for( int i = 0; i < totalSize; ++i ) {
buf[i] = static_cast<float>( random.Uniform(-deltaAbsMax, deltaAbsMax) );
}
MathEngine().DataExchangeTyped(delta, buf.GetPtr(), totalSize);
}
MathEngine().DataExchangeTyped(delta.GetHandle(), temp.GetPtr(), totalSize);

return Test(batchSize, data.GetHandle(), vectorSize, label->GetData<int>(), 1, delta.GetHandle());
CIntHandleStackVar label(MathEngine(), batchSize);
{
CArray<int> bufInt;
bufInt.SetSize( batchSize );
for( int i = 0; i < batchSize; ++i ) {
bufInt[i] = random.UniformInt(0, labelMax - 1);
}
MathEngine().DataExchangeTyped<int>(label, bufInt.GetPtr(), batchSize);
}
return Test(batchSize, data, vectorSize, label, 1, delta);
}

} // namespace NeoML
4 changes: 2 additions & 2 deletions NeoML/test/src/SpaceToDepthTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static void spaceToDepthTestInt( const CTestParams& params, int seed )
s2dnn.RunOnce();

CPtr<CDnnBlob> result = sink->GetBlob();
int* buffer = result->GetBuffer<int>( 0, dataSize, /*exchange*/false );
int* buffer = result->GetBuffer<int>( 0, dataSize, /*exchange*/true );
for( int i = 0; i < dataSize; ++i ) {
EXPECT_EQ( convertedData[i], buffer[i] ) << i;
}
Expand All @@ -175,7 +175,7 @@ static void spaceToDepthTestInt( const CTestParams& params, int seed )
d2snn.RunOnce();

CPtr<CDnnBlob> result = sink->GetBlob();
int* buffer = result->GetBuffer<int>( 0, dataSize, /*exchange*/false );
int* buffer = result->GetBuffer<int>( 0, dataSize, /*exchange*/true );
for( int i = 0; i < dataSize; ++i ) {
EXPECT_EQ( originalData[i], buffer[i] ) << i;
}
Expand Down
2 changes: 1 addition & 1 deletion NeoMathEngine/src/GPU/Vulkan/VulkanMathEngineDnnConvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void CVulkanMathEngine::BlobRleConvolution( const CRleConvolutionDesc& desc, con
const CVulkanRleConvolutionDesc& rleDesc = static_cast<const CVulkanRleConvolutionDesc&>( desc );
const CCommonConvolutionDesc* convDesc = static_cast<const CCommonConvolutionDesc*>( rleDesc.ConvDesc );

CFloatHandleVar inputConverted( mathEngine(), convDesc->Source.BlobSize() );
CFloatHandleStackVar inputConverted( mathEngine(), convDesc->Source.BlobSize() );
blobConvertFromRleCommon( rleDesc, sourceData, inputConverted );
BlobConvolution( *(rleDesc.ConvDesc), inputConverted, filterData, freeTermData, resultData );
}
Expand Down
4 changes: 2 additions & 2 deletions NeoMathEngine/test/src/inference/BlobMergeByDimTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright © 2017-2020 ABBYY Production LLC
/* Copyright © 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -93,7 +93,7 @@ static void blobMergeByDimTestImpl( const CTestParams& params, int seed )
expected.resize( resultDesc.BlobSize() );
blobMergeByDimNaive( mergeDim, sourceDescs.data(), sourceData, fromCount, resultDesc, expected );

CFloatHandleVar resultHandle( MathEngine(), resultDesc.BlobSize() );
CFloatHandleStackVar resultHandle( MathEngine(), resultDesc.BlobSize() );
MathEngine().BlobMergeByDim( static_cast<TBlobDim>( mergeDim ), sourceDescs.data(), fromHandles.data(), fromCount, resultDesc, resultHandle.GetHandle() );

for(size_t i = 0; i < fromHandleVars.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright © 2017-2020 ABBYY Production LLC
/* Copyright © 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -96,7 +96,7 @@ static void multichannelLookupAndCopyImpl( const CTestParams& params, int seed )
}
}

CMemoryHandleVar<TIndex> inputHandle( MathEngine(), inputData.size() );
CMemoryHandleStackVar<TIndex> inputHandle( MathEngine(), inputData.size() );
MathEngine().DataExchangeTyped( inputHandle.GetHandle(), inputData.data(), inputData.size() );

std::vector<TLookup> result;
Expand Down
6 changes: 3 additions & 3 deletions NeoMathEngine/test/src/learn/TransposeMatrixTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright © 2017-2020 ABBYY Production LLC
/* Copyright © 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,9 +55,9 @@ static void transposeMatrixTestImpl( const CTestParams& params, int seed )
}
}

CMemoryHandleVar<T> from( MathEngine(), matrixSize );
CMemoryHandleStackVar<T> from( MathEngine(), matrixSize );
MathEngine().DataExchangeTyped<T>( from, matrix.data(), matrixSize );
CMemoryHandleVar<T> result( MathEngine(), matrixSize );
CMemoryHandleStackVar<T> result( MathEngine(), matrixSize );
MathEngine().TransposeMatrix( batchSize, from, height, medium, width, channels, result, matrixSize );
MathEngine().DataExchangeTyped<T>( matrix.data(), result, matrixSize );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static void multichannelLookupAndAddToTableImpl( const CTestParams& params, int
resultChannelCount += lookupDimensions[i].VectorSize;
}

CMemoryHandleVar<T> inputHandle( MathEngine(), inputData.size() );
CMemoryHandleStackVar<T> inputHandle( MathEngine(), inputData.size() );
MathEngine().DataExchangeTyped( inputHandle.GetHandle(), inputData.data(), inputData.size() );
CREATE_FILL_FLOAT_ARRAY( matrix, valuesInterval.Begin, valuesInterval.End, batchSize * resultChannelCount, random )

Expand Down

0 comments on commit 8ecfe4f

Please sign in to comment.