Skip to content

Commit fda86d0

Browse files
committed
Fix alignment check
1 parent bb93b9d commit fda86d0

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/Microsoft.ML.CpuMath/CpuMathUtils.netstandard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private static bool Compat(AlignedArray a)
2929
{
3030
Contracts.AssertValue(a);
3131
Contracts.Assert(a.Size > 0);
32-
return a.CbAlign == Vector128Alignment;
32+
return a.CbAlign % Vector128Alignment == 0;
3333
}
3434

3535
private static unsafe float* Ptr(AlignedArray a, float* p)

test/Microsoft.ML.Tests/TrainerEstimators/MatrixFactorizationTests.cs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,31 @@ public void MatrixFactorizationInMemoryData()
254254
Assert.True(pred.Score != 0);
255255
}
256256

257+
internal class MatrixElementZeroBased256By256
258+
{
259+
// Matrix column index starts from 0 and is at most _synthesizedMatrixColumnCount.
260+
[KeyType(_matrixColumnCount)]
261+
public uint MatrixColumnIndex;
262+
// Matrix row index starts from 0 and is at most _synthesizedMatrixRowCount.
263+
[KeyType(_matrixRowCount)]
264+
public uint MatrixRowIndex;
265+
// The value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row in the considered matrix.
266+
public float Value;
267+
}
268+
269+
internal class MatrixElementZeroBasedForScore256By256
270+
{
271+
// Matrix column index starts from 0 and is at most _synthesizedMatrixColumnCount.
272+
// Contieuous=true means that all values from 0 to _synthesizedMatrixColumnCount are allowed keys.
273+
[KeyType(_matrixColumnCount)]
274+
public uint MatrixColumnIndex;
275+
// Matrix row index starts from 0 and is at most _synthesizedMatrixRowCount.
276+
// Contieuous=true means that all values from 0 to _synthesizedMatrixRowCount are allowed keys.
277+
[KeyType(_matrixRowCount)]
278+
public uint MatrixRowIndex;
279+
public float Score;
280+
}
281+
257282
internal class MatrixElementZeroBased
258283
{
259284
// Matrix column index starts from 0 and is at most _synthesizedMatrixColumnCount.
@@ -605,15 +630,18 @@ public void OneClassMatrixFactorizationWithUnseenColumnAndRow()
605630
CompareNumbersWithTolerance(0.00316973357, testResults[2].Score, digitsOfPrecision: 5);
606631
}
607632

633+
const int _matrixColumnCount = 256;
634+
const int _matrixRowCount = 256;
635+
608636
[MatrixFactorizationFact]
609637
public void InspectMatrixFactorizationModel()
610638
{
611639
// Create an in-memory matrix as a list of tuples (column index, row index, value).
612640
// Iterators i and j are column and row indexes, respectively.
613-
var dataMatrix = new List<MatrixElementZeroBased>();
614-
for (uint i = 0; i < _synthesizedMatrixColumnCount; ++i)
615-
for (uint j = 0; j < _synthesizedMatrixRowCount; ++j)
616-
dataMatrix.Add(new MatrixElementZeroBased() { MatrixColumnIndex = i, MatrixRowIndex = j, Value = (i + j) % 5 });
641+
var dataMatrix = new List<MatrixElementZeroBased256By256>();
642+
for (uint i = 0; i < _matrixColumnCount; ++i)
643+
for (uint j = 0; j < _matrixRowCount; ++j)
644+
dataMatrix.Add(new MatrixElementZeroBased256By256() { MatrixColumnIndex = i, MatrixRowIndex = j, Value = (i + j) % 5 });
617645

618646
// Convert the in-memory matrix into an IDataView so that ML.NET components can consume it.
619647
var dataView = ML.Data.LoadFromEnumerable(dataMatrix);
@@ -629,7 +657,7 @@ public void InspectMatrixFactorizationModel()
629657
LabelColumnName = nameof(MatrixElement.Value),
630658
NumberOfIterations = 100,
631659
NumberOfThreads = 1, // To eliminate randomness, # of threads must be 1.
632-
ApproximationRank = 32,
660+
ApproximationRank = 64,
633661
LearningRate = 0.5,
634662
};
635663

@@ -639,20 +667,20 @@ public void InspectMatrixFactorizationModel()
639667
var model = pipeline.Fit(dataView);
640668

641669
// Check if the expected types in the trained model are expected.
642-
Assert.True(model.MatrixColumnIndexColumnName == nameof(MatrixElementZeroBased.MatrixColumnIndex));
643-
Assert.True(model.MatrixRowIndexColumnName == nameof(MatrixElementZeroBased.MatrixRowIndex));
670+
Assert.True(model.MatrixColumnIndexColumnName == nameof(MatrixElementZeroBased256By256.MatrixColumnIndex));
671+
Assert.True(model.MatrixRowIndexColumnName == nameof(MatrixElementZeroBased256By256.MatrixRowIndex));
644672
var matColKeyType = model.MatrixColumnIndexColumnType as KeyDataViewType;
645673
Assert.NotNull(matColKeyType);
646674
var matRowKeyType = model.MatrixRowIndexColumnType as KeyDataViewType;
647675
Assert.NotNull(matRowKeyType);
648-
Assert.True(matColKeyType.Count == _synthesizedMatrixColumnCount);
649-
Assert.True(matRowKeyType.Count == _synthesizedMatrixRowCount);
676+
Assert.True(matColKeyType.Count == _matrixColumnCount);
677+
Assert.True(matRowKeyType.Count == _matrixRowCount);
650678

651679
// Create a test set with assigning scores. It stands for the 2nd column of the training matrix.
652-
var testMatrix = new List<MatrixElementZeroBased>();
680+
var testMatrix = new List<MatrixElementZeroBasedForScore256By256>();
653681
for (/* column index */ uint i = 1; i < 2; ++i)
654-
for (/* row index */ uint j = 0; j < _synthesizedMatrixRowCount; ++j)
655-
testMatrix.Add(new MatrixElementZeroBased() { MatrixColumnIndex = i, MatrixRowIndex = j });
682+
for (/* row index */ uint j = 0; j < _matrixRowCount; ++j)
683+
testMatrix.Add(new MatrixElementZeroBasedForScore256By256() { MatrixColumnIndex = i, MatrixRowIndex = j, Score = 0 });
656684

657685
// Load test set as IDataView.
658686
var testData = ML.Data.LoadFromEnumerable(testMatrix);
@@ -661,7 +689,7 @@ public void InspectMatrixFactorizationModel()
661689
var transformedTestData = model.Transform(testData);
662690

663691
// Load back predictions on the 2nd column as IEnumerable<MatrixElementZeroBasedForScore>.
664-
var predictions = mlContext.Data.CreateEnumerable<MatrixElementZeroBasedForScore>(transformedTestData, false).ToList();
692+
var predictions = mlContext.Data.CreateEnumerable<MatrixElementZeroBasedForScore256By256>(transformedTestData, false).ToList();
665693

666694
// Inspect the trained model.
667695
int m = model.Model.NumberOfRows;

0 commit comments

Comments
 (0)