Skip to content
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
2 changes: 1 addition & 1 deletion src/Microsoft.ML.CpuMath/AlignedMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public void ZeroItems(int[] indices)

// REVIEW: Ideally, we'd adjust the indices once so we wouldn't need to
// repeatedly deal with padding adjustments.
SseUtils.ZeroMatrixItems(Items, ColCount, ColCountPhy, indices);
CpuMathUtils.ZeroMatrixItems(Items, ColCount, ColCountPhy, indices);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.ML.CpuMath/CpuAligenedMathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void AssertCompatible(ICpuFullMatrix values)
#if DEBUG
var mat = values as TMatrix;
Contracts.AssertValue(mat);
Contracts.Assert(mat.Items.CbAlign == SseUtils.CbAlign);
Contracts.Assert(mat.Items.CbAlign == CpuMathUtils.Vector128Alignment);
#endif
}

Expand All @@ -29,7 +29,7 @@ public static void AssertCompatible(ICpuVector values)
#if DEBUG
CpuAlignedVector vec = values as CpuAlignedVector;
Contracts.AssertValue(vec);
Contracts.Assert(vec.Items.CbAlign == SseUtils.CbAlign);
Contracts.Assert(vec.Items.CbAlign == CpuMathUtils.Vector128Alignment);
#endif
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public static void MatTimesSrc(bool add, ICpuFullMatrix mat, ICpuVector src, ICp
bool colMajor = typeof(TMatrix) == typeof(CpuAlignedMatrixCol);
AssertCompatible(mat, src, dst);
var m = A(mat);
SseUtils.MatTimesSrc(colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
CpuMathUtils.MatTimesSrc(colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
}

/// <summary>
Expand All @@ -108,7 +108,7 @@ public static void MatTranTimesSrc(bool add, ICpuFullMatrix mat, ICpuVector src,
bool colMajor = typeof(TMatrix) == typeof(CpuAlignedMatrixCol);
AssertCompatible(mat, dst, src);
var m = A(mat);
SseUtils.MatTimesSrc(!colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
CpuMathUtils.MatTimesSrc(!colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.ML.CpuMath/CpuMathUtils.netcoreapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace Microsoft.ML.Runtime.Internal.CpuMath
{
public static partial class CpuMathUtils
{
// The count of bytes in Vector128<T>, corresponding to _cbAlign in AlignedArray
public const int Vector128Alignment = 16;

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun)
{
Contracts.Assert(mat.Size == dst.Size * src.Size);
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.ML.CpuMath/CpuMathUtils.netstandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace Microsoft.ML.Runtime.Internal.CpuMath
{
public static partial class CpuMathUtils
{
// The count of bytes in Vector128<T>, corresponding to _cbAlign in AlignedArray
public const int Vector128Alignment = 16;

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, AlignedArray src, AlignedArray dst, int crun) => SseUtils.MatTimesSrc(tran, add, mat, src, dst, crun);

public static void MatTimesSrc(bool tran, bool add, AlignedArray mat, int[] rgposSrc, AlignedArray srcValues,
Expand Down
7 changes: 4 additions & 3 deletions src/Microsoft.ML.CpuMath/SseIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ namespace Microsoft.ML.Runtime.Internal.CpuMath
{
internal static class SseIntrinsics
{
private const int CbAlign = 16;
// The count of bytes in Vector128<T>, corresponding to _cbAlign in AlignedArray
private const int Vector128Alignment = 16;

private static bool Compat(AlignedArray a)
{
Contracts.AssertValue(a);
Contracts.Assert(a.Size > 0);
return a.CbAlign == CbAlign;
return a.CbAlign == Vector128Alignment;
}

private static unsafe float* Ptr(AlignedArray a, float* p)
{
Contracts.AssertValue(a);
float* q = p + a.GetBase((long)p);
Contracts.Assert(((long)q & (CbAlign - 1)) == 0);
Contracts.Assert(((long)q & (Vector128Alignment - 1)) == 0);
return q;
}

Expand Down
30 changes: 15 additions & 15 deletions src/Microsoft.ML.Data/Depricated/Vector/VBufferMathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public static Float NormSquared(in VBuffer<Float> a)
{
if (a.Count == 0)
return 0;
return SseUtils.SumSq(a.Values, 0, a.Count);
return CpuMathUtils.SumSq(a.Values, 0, a.Count);
}

/// <summary>
/// Returns the L2 norm squared of the vector (sum of squares of the components).
/// </summary>
public static Float NormSquared(Float[] a, int offset, int count)
{
return SseUtils.SumSq(a, offset, count);
return CpuMathUtils.SumSq(a, offset, count);
}

/// <summary>
Expand All @@ -50,7 +50,7 @@ public static Float L1Norm(ref VBuffer<Float> a)
{
if (a.Count == 0)
return 0;
return SseUtils.SumAbs(a.Values, 0, a.Count);
return CpuMathUtils.SumAbs(a.Values, 0, a.Count);
}

/// <summary>
Expand All @@ -61,7 +61,7 @@ public static Float MaxNorm(ref VBuffer<Float> a)
{
if (a.Count == 0)
return 0;
return SseUtils.MaxAbs(a.Values, 0, a.Count);
return CpuMathUtils.MaxAbs(a.Values, 0, a.Count);
}

/// <summary>
Expand All @@ -71,7 +71,7 @@ public static Float Sum(ref VBuffer<Float> a)
{
if (a.Count == 0)
return 0;
return SseUtils.Sum(a.Values, 0, a.Count);
return CpuMathUtils.Sum(a.Values, 0, a.Count);
}

/// <summary>
Expand All @@ -84,7 +84,7 @@ public static void ScaleBy(ref VBuffer<Float> dst, Float c)
if (c == 1 || dst.Count == 0)
return;
if (c != 0)
SseUtils.Scale(c, dst.Values, dst.Count);
CpuMathUtils.Scale(c, dst.Values, dst.Count);
else // Maintain density of dst.
Array.Clear(dst.Values, 0, dst.Count);
// REVIEW: Any benefit in sparsifying?
Expand Down Expand Up @@ -114,7 +114,7 @@ public static void ScaleBy(in VBuffer<Float> src, ref VBuffer<Float> dst, Float
if (c == 0)
Array.Clear(dstValues, 0, length);
else
SseUtils.Scale(c, src.Values, dstValues, length);
CpuMathUtils.Scale(c, src.Values, dstValues, length);
dst = new VBuffer<Float>(length, dstValues, dst.Indices);
}
else
Expand All @@ -124,7 +124,7 @@ public static void ScaleBy(in VBuffer<Float> src, ref VBuffer<Float> dst, Float
if (c == 0)
Array.Clear(dstValues, 0, count);
else
SseUtils.Scale(c, src.Values, dstValues, count);
CpuMathUtils.Scale(c, src.Values, dstValues, count);
dst = new VBuffer<Float>(length, count, dstValues, dstIndices);
}
}
Expand All @@ -142,9 +142,9 @@ public static void Add(ref VBuffer<Float> src, ref VBuffer<Float> dst)
if (dst.IsDense)
{
if (src.IsDense)
SseUtils.Add(src.Values, dst.Values, src.Length);
CpuMathUtils.Add(src.Values, dst.Values, src.Length);
else
SseUtils.Add(src.Values, src.Indices, dst.Values, src.Count);
CpuMathUtils.Add(src.Values, src.Indices, dst.Values, src.Count);
return;
}
// REVIEW: Should we use SSE for any of these possibilities?
Expand All @@ -168,9 +168,9 @@ public static void AddMult(ref VBuffer<Float> src, Float c, ref VBuffer<Float> d
if (dst.IsDense)
{
if (src.IsDense)
SseUtils.AddScale(c, src.Values, dst.Values, src.Length);
CpuMathUtils.AddScale(c, src.Values, dst.Values, src.Length);
else
SseUtils.AddScale(c, src.Values, src.Indices, dst.Values, src.Count);
CpuMathUtils.AddScale(c, src.Values, src.Indices, dst.Values, src.Count);
return;
}
// REVIEW: Should we use SSE for any of these possibilities?
Expand All @@ -197,7 +197,7 @@ public static void AddMult(ref VBuffer<Float> src, Float c, ref VBuffer<Float> d
if (dst.IsDense && src.IsDense)
{
Float[] resValues = Utils.Size(res.Values) >= length ? res.Values : new Float[length];
SseUtils.AddScaleCopy(c, src.Values, dst.Values, resValues, length);
CpuMathUtils.AddScaleCopy(c, src.Values, dst.Values, resValues, length);
res = new VBuffer<Float>(length, resValues, res.Indices);
return;
}
Expand Down Expand Up @@ -239,9 +239,9 @@ public static void AddMultWithOffset(ref VBuffer<Float> src, Float c, ref VBuffe
{
// This is by far the most common case.
if (src.IsDense)
SseUtils.AddScale(c, src.Values, dst.Values, offset, src.Count);
CpuMathUtils.AddScale(c, src.Values, dst.Values, offset, src.Count);
else
SseUtils.AddScale(c, src.Values, src.Indices, dst.Values, offset, src.Count);
CpuMathUtils.AddScale(c, src.Values, src.Indices, dst.Values, offset, src.Count);
return;
}
// REVIEW: Perhaps implementing an ApplyInto with an offset would be more
Expand Down
42 changes: 21 additions & 21 deletions src/Microsoft.ML.Data/Depricated/Vector/VectorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Float DotProduct(Float[] a, Float[] b)
{
Contracts.Check(Utils.Size(a) == Utils.Size(b), "Arrays must have the same length");
Contracts.Check(Utils.Size(a) > 0);
return SseUtils.DotProductDense(a, b, a.Length);
return CpuMathUtils.DotProductDense(a, b, a.Length);
}

public static Float DotProduct(Float[] a, ref VBuffer<Float> b)
Expand All @@ -33,8 +33,8 @@ public static Float DotProduct(Float[] a, ref VBuffer<Float> b)
if (b.Count == 0)
return 0;
if (b.IsDense)
return SseUtils.DotProductDense(a, b.Values, b.Length);
return SseUtils.DotProductSparse(a, b.Values, b.Indices, b.Count);
return CpuMathUtils.DotProductDense(a, b.Values, b.Length);
return CpuMathUtils.DotProductSparse(a, b.Values, b.Indices, b.Count);
}

public static Float DotProduct(ref VBuffer<Float> a, ref VBuffer<Float> b)
Expand All @@ -47,12 +47,12 @@ public static Float DotProduct(ref VBuffer<Float> a, ref VBuffer<Float> b)
if (a.IsDense)
{
if (b.IsDense)
return SseUtils.DotProductDense(a.Values, b.Values, a.Length);
return SseUtils.DotProductSparse(a.Values, b.Values, b.Indices, b.Count);
return CpuMathUtils.DotProductDense(a.Values, b.Values, a.Length);
return CpuMathUtils.DotProductSparse(a.Values, b.Values, b.Indices, b.Count);
}

if (b.IsDense)
return SseUtils.DotProductSparse(b.Values, a.Values, a.Indices, a.Count);
return CpuMathUtils.DotProductSparse(b.Values, a.Values, a.Indices, a.Count);
return DotProductSparse(a.Values, a.Indices, 0, a.Count, b.Values, b.Indices, 0, b.Count, 0);
}

Expand Down Expand Up @@ -159,7 +159,7 @@ public static void MulElementWise(ref VBuffer<Float> a, ref VBuffer<Float> dst)
Contracts.Check(a.Length == dst.Length, "Vectors must have the same dimensionality.");

if (a.IsDense && dst.IsDense)
SseUtils.MulElementWise(a.Values, dst.Values, dst.Values, a.Length);
CpuMathUtils.MulElementWise(a.Values, dst.Values, dst.Values, a.Length);
else
VBufferUtils.ApplyWithEitherDefined(ref a, ref dst, (int ind, Float v1, ref Float v2) => { v2 *= v1; });
}
Expand Down Expand Up @@ -228,11 +228,11 @@ private static Float L2DistSquaredHalfSparse(Float[] valuesA, int lengthA, Float
Contracts.Assert(0 <= countB && countB <= Utils.Size(indicesB));
Contracts.Assert(countB <= Utils.Size(valuesB));

var normA = SseUtils.SumSq(valuesA, 0, lengthA);
var normA = CpuMathUtils.SumSq(valuesA, 0, lengthA);
if (countB == 0)
return normA;
var normB = SseUtils.SumSq(valuesB, 0, countB);
var dotP = SseUtils.DotProductSparse(valuesA, valuesB, indicesB, countB);
var normB = CpuMathUtils.SumSq(valuesB, 0, countB);
var dotP = CpuMathUtils.DotProductSparse(valuesA, valuesB, indicesB, countB);
var res = normA + normB - 2 * dotP;
return res < 0 ? 0 : res;
}
Expand All @@ -246,7 +246,7 @@ private static Float L2DiffSquaredDense(Float[] valuesA, Float[] valuesB, int le

if (length == 0)
return 0;
return SseUtils.L2DistSquared(valuesA, valuesB, length);
return CpuMathUtils.L2DistSquared(valuesA, valuesB, length);
}

/// <summary>
Expand All @@ -267,8 +267,8 @@ public static Float DotProductWithOffset(ref VBuffer<Float> a, int offset, ref V
if (a.IsDense)
{
if (b.IsDense)
return SseUtils.DotProductDense(a.Values, offset, b.Values, b.Length);
return SseUtils.DotProductSparse(a.Values, offset, b.Values, b.Indices, b.Count);
return CpuMathUtils.DotProductDense(a.Values, offset, b.Values, b.Length);
return CpuMathUtils.DotProductSparse(a.Values, offset, b.Values, b.Indices, b.Count);
}
else
{
Expand Down Expand Up @@ -314,8 +314,8 @@ public static Float DotProductWithOffset(Float[] a, int offset, ref VBuffer<Floa
return 0;

if (b.IsDense)
return SseUtils.DotProductDense(a, offset, b.Values, b.Length);
return SseUtils.DotProductSparse(a, offset, b.Values, b.Indices, b.Count);
return CpuMathUtils.DotProductDense(a, offset, b.Values, b.Length);
return CpuMathUtils.DotProductSparse(a, offset, b.Values, b.Indices, b.Count);
}

private static Float DotProductSparse(Float[] aValues, int[] aIndices, int ia, int iaLim, Float[] bValues, int[] bIndices, int ib, int ibLim, int offset)
Expand Down Expand Up @@ -434,7 +434,7 @@ public static void Add(Float[] src, Float[] dst)
Contracts.CheckParam(src.Length == dst.Length, nameof(dst), "Arrays must have the same dimensionality.");
if (src.Length == 0)
return;
SseUtils.Add(src, dst, src.Length);
CpuMathUtils.Add(src, dst, src.Length);
}

/// <summary>
Expand All @@ -452,7 +452,7 @@ public static void AddMult(ref VBuffer<Float> src, Float[] dst, Float c)
return;

if (src.IsDense)
SseUtils.AddScale(c, src.Values, dst, src.Count);
CpuMathUtils.AddScale(c, src.Values, dst, src.Count);
else
{
for (int i = 0; i < src.Count; i++)
Expand Down Expand Up @@ -502,15 +502,15 @@ public static void AddMult(Float[] src, Float[] dst, Float c)
if (c == 0)
return;

SseUtils.AddScale(c, src, dst, src.Length);
CpuMathUtils.AddScale(c, src, dst, src.Length);
}

/// <summary>
/// Returns the L2 norm of the vector (sum of squares of the components).
/// </summary>
public static Float Norm(Float[] a)
{
return MathUtils.Sqrt(SseUtils.SumSq(a, a.Length));
return MathUtils.Sqrt(CpuMathUtils.SumSq(a, a.Length));
}

/// <summary>
Expand All @@ -520,7 +520,7 @@ public static Float Sum(Float[] a)
{
if (a == null || a.Length == 0)
return 0;
return SseUtils.Sum(a, a.Length);
return CpuMathUtils.Sum(a, a.Length);
}

/// <summary>
Expand All @@ -534,7 +534,7 @@ public static void ScaleBy(Float[] dst, Float c)
return;

if (c != 0)
SseUtils.Scale(c, dst, dst.Length);
CpuMathUtils.Scale(c, dst, dst.Length);
else
Array.Clear(dst, 0, dst.Length);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ public static void Initialize(IHost host, int numThreads, IChannel ch, FeatureFl
{
weights = new Float[totalSamples];
for (int i = 0; i < workStateWeights.Length; i++)
SseUtils.Add(workStateWeights[i], weights, totalSamples);
CpuMathUtils.Add(workStateWeights[i], weights, totalSamples);
},
ref weightBuffer, ref totalWeights);
#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,9 @@ protected virtual void TrainWithoutLock(IProgressChannelProvider progress, Float
}

if (features.IsDense)
SseUtils.SdcaL1UpdateDense(primalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[0].Values, weights[0].Values);
CpuMathUtils.SdcaL1UpdateDense(primalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[0].Values, weights[0].Values);
else if (features.Count > 0)
SseUtils.SdcaL1UpdateSparse(primalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[0].Values, weights[0].Values);
CpuMathUtils.SdcaL1UpdateSparse(primalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[0].Values, weights[0].Values);
}

break;
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.ML.StandardLearners/Standard/SdcaMultiClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ protected override void TrainWithoutLock(IProgressChannelProvider progress, Floa
}

if (features.IsDense)
SseUtils.SdcaL1UpdateDense(-primalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[iClass].Values, weights[iClass].Values);
CpuMathUtils.SdcaL1UpdateDense(-primalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[iClass].Values, weights[iClass].Values);
else if (features.Count > 0)
SseUtils.SdcaL1UpdateSparse(-primalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[iClass].Values, weights[iClass].Values);
CpuMathUtils.SdcaL1UpdateSparse(-primalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[iClass].Values, weights[iClass].Values);
}

break;
Expand All @@ -202,9 +202,9 @@ protected override void TrainWithoutLock(IProgressChannelProvider progress, Floa
: 0;

if (features.IsDense)
SseUtils.SdcaL1UpdateDense(labelPrimalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[label].Values, weights[label].Values);
CpuMathUtils.SdcaL1UpdateDense(labelPrimalUpdate, features.Length, features.Values, l1Threshold, l1IntermediateWeights[label].Values, weights[label].Values);
else if (features.Count > 0)
SseUtils.SdcaL1UpdateSparse(labelPrimalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[label].Values, weights[label].Values);
CpuMathUtils.SdcaL1UpdateSparse(labelPrimalUpdate, features.Length, features.Values, features.Indices, features.Count, l1Threshold, l1IntermediateWeights[label].Values, weights[label].Values);
}

rowCount++;
Expand Down
Loading