Skip to content

Add debug asserts #1566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Microsoft.ML.CpuMath/AvxIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ private static Vector256<float> MultiplyAdd(Vector256<float> src1, Vector256<flo
// Multiply matrix times vector into vector.
public static unsafe void MatMul(AlignedArray mat, AlignedArray src, AlignedArray dst, int crow, int ccol)
{
Contracts.Assert(src.Size == dst.Size);

MatMul(mat.Items, src.Items, dst.Items, crow, ccol);
}

public static unsafe void MatMul(ReadOnlySpan<float> mat, ReadOnlySpan<float> src, Span<float> dst, int crow, int ccol)
{
Contracts.Assert(crow % 4 == 0);
Contracts.Assert(ccol % 4 == 0);
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
Expand Down Expand Up @@ -307,6 +310,8 @@ public static unsafe void MatMul(ReadOnlySpan<float> mat, ReadOnlySpan<float> sr
public static unsafe void MatMulP(AlignedArray mat, ReadOnlySpan<int> rgposSrc, AlignedArray src,
int posMin, int iposMin, int iposEnd, AlignedArray dst, int crow, int ccol)
{
Contracts.Assert(src.Size == dst.Size);

MatMulP(mat.Items, rgposSrc, src.Items, posMin, iposMin, iposEnd, dst.Items, crow, ccol);
}

Expand All @@ -315,6 +320,7 @@ public static unsafe void MatMulP(ReadOnlySpan<float> mat, ReadOnlySpan<int> rgp
{
Contracts.Assert(crow % 8 == 0);
Contracts.Assert(ccol % 8 == 0);
Contracts.Assert(src.Length == dst.Length);

// REVIEW: For extremely sparse inputs, interchanging the loops would
// likely be more efficient.
Expand Down Expand Up @@ -468,13 +474,16 @@ Vector256<float> SparseMultiplicationAcrossRow()

public static unsafe void MatMulTran(AlignedArray mat, AlignedArray src, AlignedArray dst, int crow, int ccol)
{
Contracts.Assert(src.Size == dst.Size);

MatMulTran(mat.Items, src.Items, dst.Items, crow, ccol);
}

public static unsafe void MatMulTran(ReadOnlySpan<float> mat, ReadOnlySpan<float> src, Span<float> dst, int crow, int ccol)
{
Contracts.Assert(crow % 4 == 0);
Contracts.Assert(ccol % 4 == 0);
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
Expand Down Expand Up @@ -951,6 +960,8 @@ public static unsafe void Scale(float scale, Span<float> dst)

public static unsafe void ScaleSrcU(float scale, ReadOnlySpan<float> src, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down Expand Up @@ -1044,6 +1055,8 @@ public static unsafe void ScaleAddU(float a, float b, Span<float> dst)

public static unsafe void AddScaleU(float scale, ReadOnlySpan<float> src, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down Expand Up @@ -1096,6 +1109,8 @@ public static unsafe void AddScaleU(float scale, ReadOnlySpan<float> src, Span<f

public static unsafe void AddScaleCopyU(float scale, ReadOnlySpan<float> src, ReadOnlySpan<float> dst, Span<float> result, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
fixed (float* pres = &MemoryMarshal.GetReference(result))
Expand Down Expand Up @@ -1150,6 +1165,8 @@ public static unsafe void AddScaleCopyU(float scale, ReadOnlySpan<float> src, Re

public static unsafe void AddScaleSU(float scale, ReadOnlySpan<float> src, ReadOnlySpan<int> idx, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (int* pidx = &MemoryMarshal.GetReference(idx))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
Expand Down Expand Up @@ -1198,6 +1215,8 @@ public static unsafe void AddScaleSU(float scale, ReadOnlySpan<float> src, ReadO

public static unsafe void AddU(ReadOnlySpan<float> src, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down Expand Up @@ -1245,6 +1264,8 @@ public static unsafe void AddU(ReadOnlySpan<float> src, Span<float> dst, int cou

public static unsafe void AddSU(ReadOnlySpan<float> src, ReadOnlySpan<int> idx, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (int* pidx = &MemoryMarshal.GetReference(idx))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
Expand Down Expand Up @@ -1726,6 +1747,8 @@ public static unsafe float MaxAbsDiffU(float mean, ReadOnlySpan<float> src)

public static unsafe float DotU(ReadOnlySpan<float> src, ReadOnlySpan<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down Expand Up @@ -1778,6 +1801,8 @@ public static unsafe float DotU(ReadOnlySpan<float> src, ReadOnlySpan<float> dst

public static unsafe float DotSU(ReadOnlySpan<float> src, ReadOnlySpan<float> dst, ReadOnlySpan<int> idx, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
fixed (int* pidx = &MemoryMarshal.GetReference(idx))
Expand Down Expand Up @@ -1832,6 +1857,8 @@ public static unsafe float DotSU(ReadOnlySpan<float> src, ReadOnlySpan<float> ds

public static unsafe float Dist2(ReadOnlySpan<float> src, ReadOnlySpan<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down
30 changes: 30 additions & 0 deletions src/Microsoft.ML.CpuMath/SseIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ internal static Vector128<float> GetNewDst128(in Vector128<float> xDst1, in Vect
// Multiply matrix times vector into vector.
public static unsafe void MatMul(AlignedArray mat, AlignedArray src, AlignedArray dst, int crow, int ccol)
{
Contracts.Assert(src.Size == dst.Size);

MatMul(mat.Items, src.Items, dst.Items, crow, ccol);
}

public static unsafe void MatMul(ReadOnlySpan<float> mat, ReadOnlySpan<float> src, Span<float> dst, int crow, int ccol)
{
Contracts.Assert(crow % 4 == 0);
Contracts.Assert(ccol % 4 == 0);
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
Expand Down Expand Up @@ -279,12 +282,15 @@ public static unsafe void MatMul(ReadOnlySpan<float> mat, ReadOnlySpan<float> sr
public static unsafe void MatMulP(AlignedArray mat, int[] rgposSrc, AlignedArray src,
int posMin, int iposMin, int iposEnd, AlignedArray dst, int crow, int ccol)
{
Contracts.Assert(src.Size == dst.Size);

MatMulP(mat.Items, rgposSrc, src.Items, posMin, iposMin, iposEnd, dst.Items, crow, ccol);
}

public static unsafe void MatMulP(ReadOnlySpan<float> mat, ReadOnlySpan<int> rgposSrc, ReadOnlySpan<float> src,
int posMin, int iposMin, int iposEnd, Span<float> dst, int crow, int ccol)
{
Contracts.Assert(src.Length == dst.Length);
Contracts.Assert(crow % 4 == 0);
Contracts.Assert(ccol % 4 == 0);

Expand Down Expand Up @@ -443,11 +449,14 @@ Vector128<float> SparseMultiplicationAcrossRow()

public static unsafe void MatMulTran(AlignedArray mat, AlignedArray src, AlignedArray dst, int crow, int ccol)
{
Contracts.Assert(src.Size == dst.Size);

MatMulTran(mat.Items, src.Items, dst.Items, crow, ccol);
}

public static unsafe void MatMulTran(ReadOnlySpan<float> mat, ReadOnlySpan<float> src, Span<float> dst, int crow, int ccol)
{
Contracts.Assert(src.Length == dst.Length);
Contracts.Assert(crow % 4 == 0);
Contracts.Assert(ccol % 4 == 0);

Expand Down Expand Up @@ -893,6 +902,8 @@ public static unsafe void Scale(float scale, Span<float> dst)

public static unsafe void ScaleSrcU(float scale, ReadOnlySpan<float> src, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down Expand Up @@ -961,6 +972,8 @@ public static unsafe void ScaleAddU(float a, float b, Span<float> dst)

public static unsafe void AddScaleU(float scale, ReadOnlySpan<float> src, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down Expand Up @@ -1000,6 +1013,8 @@ public static unsafe void AddScaleU(float scale, ReadOnlySpan<float> src, Span<f

public static unsafe void AddScaleCopyU(float scale, ReadOnlySpan<float> src, ReadOnlySpan<float> dst, Span<float> result, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
fixed (float* pres = &MemoryMarshal.GetReference(result))
Expand Down Expand Up @@ -1041,6 +1056,8 @@ public static unsafe void AddScaleCopyU(float scale, ReadOnlySpan<float> src, Re

public static unsafe void AddScaleSU(float scale, ReadOnlySpan<float> src, ReadOnlySpan<int> idx, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (int* pidx = &MemoryMarshal.GetReference(idx))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
Expand Down Expand Up @@ -1077,6 +1094,8 @@ public static unsafe void AddScaleSU(float scale, ReadOnlySpan<float> src, ReadO

public static unsafe void AddU(ReadOnlySpan<float> src, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down Expand Up @@ -1112,6 +1131,8 @@ public static unsafe void AddU(ReadOnlySpan<float> src, Span<float> dst, int cou

public static unsafe void AddSU(ReadOnlySpan<float> src, ReadOnlySpan<int> idx, Span<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (int* pidx = &MemoryMarshal.GetReference(idx))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
Expand Down Expand Up @@ -1145,6 +1166,9 @@ public static unsafe void AddSU(ReadOnlySpan<float> src, ReadOnlySpan<int> idx,

public static unsafe void MulElementWiseU(ReadOnlySpan<float> src1, ReadOnlySpan<float> src2, Span<float> dst, int count)
{
Contracts.Assert(src1.Length == dst.Length);
Contracts.Assert(src2.Length == dst.Length);

fixed (float* psrc1 = &MemoryMarshal.GetReference(src1))
fixed (float* psrc2 = &MemoryMarshal.GetReference(src2))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
Expand Down Expand Up @@ -1479,6 +1503,8 @@ public static unsafe float MaxAbsDiffU(float mean, ReadOnlySpan<float> src)

public static unsafe float DotU(ReadOnlySpan<float> src, ReadOnlySpan<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down Expand Up @@ -1518,6 +1544,8 @@ public static unsafe float DotU(ReadOnlySpan<float> src, ReadOnlySpan<float> dst

public static unsafe float DotSU(ReadOnlySpan<float> src, ReadOnlySpan<float> dst, ReadOnlySpan<int> idx, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
fixed (int* pidx = &MemoryMarshal.GetReference(idx))
Expand Down Expand Up @@ -1559,6 +1587,8 @@ public static unsafe float DotSU(ReadOnlySpan<float> src, ReadOnlySpan<float> ds

public static unsafe float Dist2(ReadOnlySpan<float> src, ReadOnlySpan<float> dst, int count)
{
Contracts.Assert(src.Length == dst.Length);

fixed (float* psrc = &MemoryMarshal.GetReference(src))
fixed (float* pdst = &MemoryMarshal.GetReference(dst))
{
Expand Down