Skip to content

Commit

Permalink
additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Dermody committed Jul 26, 2018
1 parent 99167ee commit c586459
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions UnitTests/CudaTensorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ public void TensorIm2Col2()
}
}

[TestMethod]
public void TensorIm2Col3()
{
var normalDistribution = new Normal(0, 1);
using (var cpuTensor = _cpu.Create3DTensor(Enumerable.Range(0, 2).Select(i => _cpu.CreateMatrix(8, 8, (j, k) => Convert.ToSingle(normalDistribution.Sample()))).ToList()))
using (var gpuTensor = _cuda.Create3DTensor(cpuTensor.AsIndexable()))
using (var cpuMatrix = cpuTensor.Im2Col(2, 2, 1))
using (var gpuMatrix = gpuTensor.Im2Col(2, 2, 1)) {
var cpu = cpuMatrix.AsIndexable();
var gpu = gpuMatrix.AsIndexable();
FloatingPointHelper.AssertEqual(cpu, gpu);
}
}

//[TestMethod]
//public void TensorCalculateWeightUpdate()
//{
Expand Down Expand Up @@ -335,7 +349,7 @@ void _TensorReverseIm2Col(int filterWidth, int filterHeight, int stride, int dep
cpuFilterList.Add(cpuFilter.Column(i).Split(depth).Select(v => v.Rotate(v.Count / filterWidth)).ToList());

var cpuReverseIm2Col = outputTensor.ReverseIm2Col(cpuFilterList, inputHeight, inputWidth, depth, 0, filterHeight, filterWidth, stride);
var cpuUpdate = cpuReverseIm2Col.ConvertTo3DTensor(inputHeight, inputWidth);
//var cpuUpdate = cpuReverseIm2Col.ConvertTo3DTensor(inputHeight, inputWidth);

using (var gpuTensor = _cuda.Create3DTensor(outputTensor.AsIndexable())) {
var gpuFilterList = cpuFilterList.Select(fl => fl.Select(f => _cuda.CreateVector(f.AsIndexable())).ToList()).ToList();
Expand Down Expand Up @@ -369,6 +383,18 @@ public void TensorReverseIm2Col4()
_TensorReverseIm2Col(2, 2, 2, 2, 2, 4, 4);
}

[TestMethod]
public void TensorReverseIm2Col5()
{
_TensorReverseIm2Col(2, 2, 1, 2, 1, 4, 4);
}

[TestMethod]
public void TensorReverseIm2Col6()
{
_TensorReverseIm2Col(2, 2, 1, 2, 2, 4, 4);
}

FloatMatrix _CreateMatrix(int depth, int rows, int columns, Func<int, int, int, float> valueProvider)
{
return new FloatMatrix {
Expand Down Expand Up @@ -440,6 +466,18 @@ public void TensorCombineDepthSlices()
FloatingPointHelper.AssertEqual(cpuCombined.AsIndexable(), gpuCombined.AsIndexable());
}

[TestMethod]
public void TensorCombineDepthSlices2()
{
var tensor = _CreateTensor(12, 6, 3, (i, j, k) => (i + 1) * (j + 1) * (k + 1));
var cpuTensor = _cpu.Create3DTensor(tensor);
var cpuCombined = cpuTensor.CombineDepthSlices();

using (var gpuTensor = _cuda.Create3DTensor(tensor))
using (var gpuCombined = gpuTensor.CombineDepthSlices())
FloatingPointHelper.AssertEqual(cpuCombined.AsIndexable(), gpuCombined.AsIndexable());
}

[TestMethod]
public void TensorAddInPlace()
{
Expand Down Expand Up @@ -682,11 +720,26 @@ public void Tensor4DColumnSums()
}
}

[TestMethod]
public void Tensor4DGetTensorAt()
{
var data = Enumerable.Range(0, 5)
.Select(z => _CreateTensor(3, 4, 2, (i, j, k) => (i + 1) * (j + 1) * (k + 1))).ToList();
var cpu4dTensor = _cpu.Create4DTensor(data);

using (var gpu4dTensor = _cuda.Create4DTensor(data)) {
for (var i = 0; i < 5; i++) {
var cpuTensor = cpu4dTensor.GetTensorAt(i);
var gpuTensor = gpu4dTensor.GetTensorAt(i);
FloatingPointHelper.AssertEqual(cpuTensor.AsIndexable(), gpuTensor.AsIndexable());
}
}
}

[TestMethod]
public void Tensor4DReverseIm2Col()
{
const int rows = 4, columns = 4, depth = 2, count = 2, filterWidth = 2, filterHeight = 2, filterCount = 4, stride = 2;
const int rows = 4, columns = 4, depth = 1, count = 1, filterWidth = 2, filterHeight = 2, filterCount = 1, stride = 2;

var normalDistribution = new Normal(0, 1);
var data = Enumerable.Range(0, count)
Expand All @@ -698,8 +751,6 @@ public void Tensor4DReverseIm2Col()
for (var i = 0; i < cpuFilter.ColumnCount; i++)
cpuFilterList.Add(cpuFilter.Column(i).Split(depth).Select(v => v.Rotate(v.Count / filterWidth)).ToList());

var cpuReverseIm2Col = cpuTensor.ReverseIm2Col(cpuFilterList, rows, columns, depth, 0, filterHeight, filterWidth, stride);

using (var gpuTensor = _cuda.Create4DTensor(data))
using (var gpuFilter = _cuda.CreateMatrix(cpuFilter.AsIndexable())) {
var gpuFilterList = new List<IReadOnlyList<IVector>>();
Expand All @@ -720,9 +771,12 @@ public void Tensor4DReverseIm2Col()
}
FloatingPointHelper.AssertEqual(cpuTensor.AsIndexable(), gpuTensor.AsIndexable());

var cpuReverseIm2Col = cpuTensor.ReverseIm2Col(cpuFilterList, rows, columns, depth, 0, filterHeight, filterWidth, stride);
using (var gpuReverseIm2Col = gpuTensor.ReverseIm2Col(gpuFilterList, rows, columns, depth, 0, filterHeight, filterWidth, stride)) {
var cpuResult = cpuReverseIm2Col.AsIndexable();
var gpuResult = gpuReverseIm2Col.AsIndexable();
var cpuXml = cpuResult.AsXml;
var gpuXml = gpuResult.AsXml;
FloatingPointHelper.AssertEqual(cpuResult, gpuResult);

foreach (var filter in gpuFilterList) {
Expand Down

0 comments on commit c586459

Please sign in to comment.