Skip to content

Commit

Permalink
Move math performance tests to MathTests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivrodr-msft committed Jul 18, 2016
1 parent 555b17c commit af6ddf9
Show file tree
Hide file tree
Showing 7 changed files with 480 additions and 61 deletions.
11 changes: 8 additions & 3 deletions Source/Math/TensorView.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#pragma warning(push)
#pragma warning(disable : 4251) // needs to have dll-interface to be used by clients of... caused by TensorView::m_shape which is only private. We use the same compiler everywhere.

template<class ElemType> struct TensorTest;
namespace Microsoft { namespace MSR { namespace CNTK { namespace Test {
template <class ElemType> struct TensorTest;
}}}}

template <class ElemType> struct TensorTest;

// This class is exported from the Math.dll.
namespace Microsoft { namespace MSR { namespace CNTK {
Expand Down Expand Up @@ -150,8 +154,9 @@ class MATH_API TensorView
// -------------------------------------------------------------------

const Matrix<ElemType>& GetSOB() const { return *m_sob; }
Matrix<ElemType>& GetSOB() { return *m_sob; }
friend struct ::TensorTest<ElemType>;
Matrix<ElemType>& GetSOB() { return *m_sob; }
// friend struct ::TensorTest<ElemType>;
friend Microsoft::MSR::CNTK::Test::TensorTest<ElemType>;

// -------------------------------------------------------------------
// sob members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ void MandSTest(int count, int devId)

int wmain()
{
TensorTest<float>();
//TensorTest<float>();

ColumnSliceMultAndAddTest<float>(2048, 2048, 256, 0);

Expand Down
26 changes: 2 additions & 24 deletions Tests/UnitTests/MathPerformanceTests/MathPerformanceTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,21 @@
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Choose>
<When Condition="Exists('$(BOOST_INCLUDE_PATH)') And Exists('$(BOOST_LIB_PATH)')">
<PropertyGroup>
<HasBoost>true</HasBoost>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<HasBoost>false</HasBoost>
</PropertyGroup>
</Otherwise>
</Choose>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<LinkIncremental>$(DebugBuild)</LinkIncremental>
<OutDir>$(OutDir)\UnitTests\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(BOOST_INCLUDE_PATH);$(SolutionDir)Source\Math;$(SolutionDir)Source\Common\Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)\Source\Math;$(SolutionDir)Source\Common\Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4819</DisableSpecificWarnings>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(OutDir)..;$(BOOST_LIB_PATH)</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>$(OutDir)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="$(DebugBuild)">
Expand Down Expand Up @@ -95,7 +82,6 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<UseFullPaths>true</UseFullPaths>
<SDLCheck>true</SDLCheck>
<OpenMPSupport>true</OpenMPSupport>
<TreatWarningAsError>true</TreatWarningAsError>
Expand All @@ -117,9 +103,6 @@
<ClCompile>
<AdditionalIncludeDirectories>$(CudaToolkitIncludeDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories);$(CudaLibPath)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ImportGroup Condition="$(GpuBuild)" Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\CUDA $(CudaVersion).props" />
Expand All @@ -134,14 +117,9 @@
<ItemGroup>
<ClCompile Include="..\..\..\Source\Common\ExceptionWithCallStack.cpp" />
<ClCompile Include="MathPerformanceTests.cpp" />
<ClCompile Include="PerformanceTests.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<Target Name="Build" Condition="$(HasBoost)" Outputs="$(TargetPath)" DependsOnTargets="$(BuildDependsOn)" />
<Target Name="CheckDependencies">
<Warning Condition="!$(HasBoost)" Text="MathTests requires the Boost library to build. Please see https://github.com/Microsoft/CNTK/wiki/Setup-CNTK-on-Windows#boost for installation instructions." />
</Target>
</Project>
87 changes: 58 additions & 29 deletions Tests/UnitTests/MathPerformanceTests/PerformanceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,84 @@
#include "CPUMatrix.h"
#include "TensorView.h"
#include "Sequences.h"
#include <boost/test/parameterized_test.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <chrono>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace Microsoft::MSR::CNTK;
using namespace boost::unit_test;

namespace Microsoft { namespace MSR { namespace CNTK { namespace Test {

BOOST_AUTO_TEST_SUITE(MathPerformance)
BOOST_AUTO_TEST_SUITE(MathPerformanceTests)

template <typename FN>
struct TensorTestParameters{
const char * testString;
double tolerance;
const FN& fn;
};
template<class FN>
static void OneTensorTest(const char* what, double tolerance, const FN& fn)
{
cout << "===== Tensor test '" << what << "'\n ";

// run on GPU and CPU
let resultGPU = fn(0);
let resultCPU = fn(-1);

template<typename FN>
class TensorTest {
public:
void OneTensorTest(const char* what, double tolerance, const FN& fn)
{
cout << "===== Tensor test '" << what << "'\n ";
// dump top corner of the result to get a feel for the error
resultGPU.GetSOB().Print("GPU result", 0, 7, 0, 9);
resultGPU.GetSOB().TransferToDeviceIfNotThere(-1, true, false, true);
resultCPU.GetSOB().Print("CPU result", 0, 7, 0, 9);

// run on GPU and CPU
let resultGPU = fn(0);
let resultCPU = fn(-1);
// compare
let isSame = resultGPU.GetSOB().IsEqualTo(resultCPU.GetSOB(), (ElemType)tolerance);
BOOST_CHECK(isSame);
}

template<class ElemType>
static TensorView<ElemType> CreateTensor(TensorShape shape, int randomSeed, DEVICEID_TYPE deviceId, bool isResult = false)
{
let numElements = shape.GetNumElements();

// dump top corner of the result to get a feel for the error
resultGPU.GetSOB().Print("GPU result", 0, 7, 0, 9);
resultGPU.GetSOB().TransferToDeviceIfNotThere(-1, true, false, true);
resultCPU.GetSOB().Print("CPU result", 0, 7, 0, 9);
if (isResult)
cout << " ->";
cout << " [" << string(shape) << "]";
if (isResult)
cout << " \t// " << (deviceId < 0 ? "C" : "G") << "PU\n " << flush;

// compare
let isSame = resultGPU.GetSOB().IsEqualTo(resultCPU.GetSOB(), (ElemType)tolerance);
BOOST_CHECK(isSame);
}
};
// random init
mt19937 rng(randomSeed);
uniform_real_distribution<float> nd(-1, 1);
vector<ElemType> init(numElements);
generate(begin(init), end(init), [&] { return nd(rng); });

TensorTestParameters<TensorView<float>> parameters[];
// create storage object (one-column matrix)
let sob = make_shared<Matrix<ElemType>>(numElements/*rows*/, 1/*cols*/, init.data(), deviceId);

TensorTest<float> tester;
// create TensorView
return TensorView<ElemType>(sob, shape);
}

bool InitUnitTest()
template<class ElemType>
static TensorView<ElemType> BroadcastingTest(TensorShape layerShape, TensorShape biasShape, DEVICEID_TYPE deviceId)
{
return false;
int randomSeed = 1;
let input = CreateTensor<ElemType>(layerShape, randomSeed++, deviceId);
auto bias = CreateTensor<ElemType>(biasShape, randomSeed++, deviceId);

auto result = CreateTensor<ElemType>(layerShape, randomSeed++, deviceId, true);
result.AssignSumOf(input, bias);
return result;
}

BOOST_AUTO_TEST_CASE(test01) {
OneTensorTest("elementwise addition", 1e-8, [](DEVICEID_TYPE deviceId)->TensorView<float>
{
return BroadcastingTest<float>(TensorShape{ 512, 256 }, TensorShape({ 512, 256 }), deviceId);
});
}

BOOST_AUTO_TEST_SUITE_END()

}
} } }
1 change: 0 additions & 1 deletion Tests/UnitTests/MathPerformanceTests/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#define _CRT_SECURE_NO_WARNINGS // "secure" CRT not available on all platforms
#include "targetver.h"

#include <boost/test/unit_test.hpp>
#include <stdio.h>

// TODO: reference additional headers your program requires here
7 changes: 4 additions & 3 deletions Tests/UnitTests/MathTests/MathTests.vcxproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" InitialTargets="CheckDependencies" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
Expand Down Expand Up @@ -66,7 +66,7 @@
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<AdditionalIncludeDirectories>$(BOOST_INCLUDE_PATH);$(SolutionDir)Source\Common\Include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(BOOST_INCLUDE_PATH);$(SolutionDir)\Source\Math;$(SolutionDir)Source\Common\Include</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4819</DisableSpecificWarnings>
</ClCompile>
<Link>
Expand Down Expand Up @@ -156,6 +156,7 @@
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="CPUMatrixTests.cpp" />
<ClCompile Include="TensorTests.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<Target Name="Build" Condition="$(HasBoost)" Outputs="$(TargetPath)" DependsOnTargets="$(BuildDependsOn)" />
Expand All @@ -179,4 +180,4 @@
<Output TaskParameter="DestinationFiles" ItemName="NewFileWrites" />
</Copy>
</Target>
</Project>
</Project>
Loading

0 comments on commit af6ddf9

Please sign in to comment.