private static void TransformFeatures(IHost host, ref VBuffer <Float> src, ref VBuffer <Float> dst, TransformInfo transformInfo, AlignedArray featuresAligned, AlignedArray productAligned) { Contracts.AssertValue(host, "host"); host.Check(src.Length == transformInfo.SrcDim, "column does not have the expected dimensionality."); var values = dst.Values; Float scale; if (transformInfo.RotationTerms != null) { if (Utils.Size(values) < transformInfo.NewDim) { values = new Float[transformInfo.NewDim]; } scale = MathUtils.Sqrt((Float)2.0 / transformInfo.NewDim); } else { if (Utils.Size(values) < 2 * transformInfo.NewDim) { values = new Float[2 * transformInfo.NewDim]; } scale = MathUtils.Sqrt((Float)1.0 / transformInfo.NewDim); } if (src.IsDense) { featuresAligned.CopyFrom(src.Values, 0, src.Length); CpuMathUtils.MatTimesSrc(false, false, transformInfo.RndFourierVectors, featuresAligned, productAligned, transformInfo.NewDim); } else { // This overload of MatTimesSrc ignores the values in slots that are not in src.Indices, so there is // no need to zero them out. featuresAligned.CopyFrom(src.Indices, src.Values, 0, 0, src.Count, zeroItems: false); CpuMathUtils.MatTimesSrc(false, false, transformInfo.RndFourierVectors, src.Indices, featuresAligned, 0, 0, src.Count, productAligned, transformInfo.NewDim); } for (int i = 0; i < transformInfo.NewDim; i++) { var dotProduct = productAligned[i]; if (transformInfo.RotationTerms != null) { values[i] = (Float)MathUtils.Cos(dotProduct + transformInfo.RotationTerms[i]) * scale; } else { values[2 * i] = (Float)MathUtils.Cos(dotProduct) * scale; values[2 * i + 1] = (Float)MathUtils.Sin(dotProduct) * scale; } } dst = new VBuffer <Float>(transformInfo.RotationTerms == null ? 2 * transformInfo.NewDim : transformInfo.NewDim, values, dst.Indices); }
public void ZeroMatrixItemsCoreTest(int test, int[] idx, float[] expected) { AlignedArray src = new AlignedArray(4 + 4 * test, SseCbAlign); src.CopyFrom(testSrcVectors[test]); CpuMathUtils.ZeroMatrixItems(src, src.Size / 2 - 1, src.Size / 2, idx); float[] actual = new float[src.Size]; src.CopyTo(actual, 0, src.Size); Assert.Equal(expected, actual, comparer); }
public void ZeroItemsUTest(int test, int[] idx, float[] expected) { AlignedArray src = new AlignedArray(8 + 8 * test, _vectorAlignment); src.CopyFrom(_testSrcVectors[test]); CpuMathUtils.ZeroMatrixItems(src, src.Size, src.Size, idx); float[] actual = new float[src.Size]; src.CopyTo(actual, 0, src.Size); Assert.Equal(expected, actual, _comparer); }
public void Setup() { src = new float[Length]; dst = new float[Length]; src1 = new float[Length]; src2 = new float[Length]; original = new float[Length]; result = new float[Length]; idx = new int[IndexLength]; matrixIdx = new int[MatrixIndexLength]; _seed = GetSeed(); Random rand = new Random(_seed); for (int i = 0; i < Length; i++) { src[i] = NextFloat(rand, ExponentRange); dst[i] = NextFloat(rand, ExponentRange); original[i] = dst[i]; result[i] = dst[i]; src1[i] = NextFloat(rand, ExponentRange); src2[i] = NextFloat(rand, ExponentRange); } for (int i = 0; i < IndexLength; i++) { idx[i] = rand.Next(0, Length); } for (int i = 0; i < MatrixIndexLength; i++) { matrixIdx[i] = rand.Next(0, 1000); } testMatrixAligned = new AlignedArray(matrixLength * matrixLength, align); testMatrixAligned.CopyFrom(src.AsSpan(0, (matrixLength - 1) * (matrixLength - 1))); testSrcVectorAligned = new AlignedArray(matrixLength, align); testSrcVectorAligned.CopyFrom(src1.AsSpan(0, matrixLength - 1)); // odd input testDstVectorAligned = new AlignedArray(matrixLength, align); testDstVectorAligned.CopyFrom(dst.AsSpan(0, matrixLength)); }
public CpuMathUtilsUnitTests() { // Padded array whose length is a multiple of 4 float[] testArray1 = new float[8] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f }; // Unpadded array whose length is not a multiple of 4. float[] testArray2 = new float[7] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f }; testArrays = new float[][] { testArray1, testArray2 }; testIndexArray = new int[4] { 0, 2, 5, 6 }; comparer = new FloatEqualityComparer(); // Padded matrices whose dimensions are multiples of 4 float[] testMatrix1 = new float[4 * 4] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f }; float[] testMatrix2 = new float[4 * 8]; for (int i = 0; i < testMatrix2.Length; i++) { testMatrix2[i] = i + 1; } AlignedArray testMatrixAligned1 = new AlignedArray(4 * 4, SseCbAlign); AlignedArray testMatrixAligned2 = new AlignedArray(4 * 8, SseCbAlign); testMatrixAligned1.CopyFrom(testMatrix1, 0, testMatrix1.Length); testMatrixAligned2.CopyFrom(testMatrix2, 0, testMatrix2.Length); testMatrices = new AlignedArray[] { testMatrixAligned1, testMatrixAligned2 }; // Padded source vectors whose dimensions are multiples of 4 float[] testSrcVector1 = new float[4] { 1f, 2f, 3f, 4f }; float[] testSrcVector2 = new float[8] { 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f }; AlignedArray testSrcVectorAligned1 = new AlignedArray(4, SseCbAlign); AlignedArray testSrcVectorAligned2 = new AlignedArray(8, SseCbAlign); testSrcVectorAligned1.CopyFrom(testSrcVector1, 0, testSrcVector1.Length); testSrcVectorAligned2.CopyFrom(testSrcVector2, 0, testSrcVector2.Length); testSrcVectors = new AlignedArray[] { testSrcVectorAligned1, testSrcVectorAligned2 }; // Padded destination vectors whose dimensions are multiples of 4 float[] testDstVector1 = new float[4] { 0f, 1f, 2f, 3f }; float[] testDstVector2 = new float[8] { 0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f }; AlignedArray testDstVectorAligned1 = new AlignedArray(4, SseCbAlign); AlignedArray testDstVectorAligned2 = new AlignedArray(8, SseCbAlign); testDstVectorAligned1.CopyFrom(testDstVector1, 0, testDstVector1.Length); testDstVectorAligned2.CopyFrom(testDstVector2, 0, testDstVector2.Length); testDstVectors = new AlignedArray[] { testDstVectorAligned1, testDstVectorAligned2 }; }
internal void CopyLatentWeightsTo(AlignedArray latentWeights) { Host.AssertValue(_latentWeightsAligned); Host.AssertValue(latentWeights); latentWeights.CopyFrom(_latentWeightsAligned); }
static CpuMathUtilsUnitTests() { // Padded array whose length is a multiple of 4 float[] testArray1 = new float[16] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f }; // Unpadded array whose length is not a multiple of 4. float[] testArray2 = new float[15] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f }; _testArrays = new float[][] { testArray1, testArray2 }; _testIndexArray = new int[9] { 0, 2, 5, 6, 8, 11, 12, 13, 14 }; _comparer = new FloatEqualityComparer(); _matMulComparer = new FloatEqualityComparerForMatMul(); // Padded matrices whose dimensions are multiples of 8 float[] testMatrix1 = new float[8 * 8] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f }; float[] testMatrix2 = new float[8 * 16]; for (int i = 0; i < testMatrix2.Length; i++) { testMatrix2[i] = i + 1; } AlignedArray testMatrixAligned1 = new AlignedArray(8 * 8, _vectorAlignment); AlignedArray testMatrixAligned2 = new AlignedArray(8 * 16, _vectorAlignment); testMatrixAligned1.CopyFrom(testMatrix1); testMatrixAligned2.CopyFrom(testMatrix2); _testMatrices = new AlignedArray[] { testMatrixAligned1, testMatrixAligned2 }; // Padded source vectors whose dimensions are multiples of 8 float[] testSrcVector1 = new float[8] { 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f }; float[] testSrcVector2 = new float[16] { 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f, 16f }; AlignedArray testSrcVectorAligned1 = new AlignedArray(8, _vectorAlignment); AlignedArray testSrcVectorAligned2 = new AlignedArray(16, _vectorAlignment); testSrcVectorAligned1.CopyFrom(testSrcVector1); testSrcVectorAligned2.CopyFrom(testSrcVector2); _testSrcVectors = new AlignedArray[] { testSrcVectorAligned1, testSrcVectorAligned2 }; // Padded destination vectors whose dimensions are multiples of 8 float[] testDstVector1 = new float[8] { 0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f }; float[] testDstVector2 = new float[16] { 0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f }; AlignedArray testDstVectorAligned1 = new AlignedArray(8, _vectorAlignment); AlignedArray testDstVectorAligned2 = new AlignedArray(16, _vectorAlignment); testDstVectorAligned1.CopyFrom(testDstVector1); testDstVectorAligned2.CopyFrom(testDstVector2); _testDstVectors = new AlignedArray[] { testDstVectorAligned1, testDstVectorAligned2 }; #if NETCOREAPP3_0 DisableAvxEnvironmentVariables = new Dictionary <string, string>() { { disableAvx, "0" } }; DisableAvxAndSseEnvironmentVariables = new Dictionary <string, string>() { { disableAvx, "0" }, { disableSse, "0" } }; #endif }
public CpuMathUtilsUnitTests() { // Padded array whose length is a multiple of 4 float[] testArray1 = new float[16] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f }; // Unpadded array whose length is not a multiple of 4. float[] testArray2 = new float[15] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f }; _testArrays = new float[][] { testArray1, testArray2 }; _testIndexArray = new int[9] { 0, 2, 5, 6, 8, 11, 12, 13, 14 }; _comparer = new FloatEqualityComparer(); _matMulComparer = new FloatEqualityComparerForMatMul(); // Padded matrices whose dimensions are multiples of 8 float[] testMatrix1 = new float[8 * 8] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f }; float[] testMatrix2 = new float[8 * 16]; for (int i = 0; i < testMatrix2.Length; i++) { testMatrix2[i] = i + 1; } AlignedArray testMatrixAligned1 = new AlignedArray(8 * 8, _vectorAlignment); AlignedArray testMatrixAligned2 = new AlignedArray(8 * 16, _vectorAlignment); testMatrixAligned1.CopyFrom(testMatrix1, 0, testMatrix1.Length); testMatrixAligned2.CopyFrom(testMatrix2, 0, testMatrix2.Length); _testMatrices = new AlignedArray[] { testMatrixAligned1, testMatrixAligned2 }; // Padded source vectors whose dimensions are multiples of 8 float[] testSrcVector1 = new float[8] { 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f }; float[] testSrcVector2 = new float[16] { 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f, 16f }; AlignedArray testSrcVectorAligned1 = new AlignedArray(8, _vectorAlignment); AlignedArray testSrcVectorAligned2 = new AlignedArray(16, _vectorAlignment); testSrcVectorAligned1.CopyFrom(testSrcVector1, 0, testSrcVector1.Length); testSrcVectorAligned2.CopyFrom(testSrcVector2, 0, testSrcVector2.Length); _testSrcVectors = new AlignedArray[] { testSrcVectorAligned1, testSrcVectorAligned2 }; // Padded destination vectors whose dimensions are multiples of 8 float[] testDstVector1 = new float[8] { 0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f }; float[] testDstVector2 = new float[16] { 0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f }; AlignedArray testDstVectorAligned1 = new AlignedArray(8, _vectorAlignment); AlignedArray testDstVectorAligned2 = new AlignedArray(16, _vectorAlignment); testDstVectorAligned1.CopyFrom(testDstVector1, 0, testDstVector1.Length); testDstVectorAligned2.CopyFrom(testDstVector2, 0, testDstVector2.Length); _testDstVectors = new AlignedArray[] { testDstVectorAligned1, testDstVectorAligned2 }; }
static CpuMathUtilsUnitTests() { // Padded array whose length is a multiple of 4 float[] testArray1 = new float[32] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f, 16f }; // Unpadded array whose length is not a multiple of 4. float[] testArray2 = new float[30] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f }; // Small Input Size Array float[] testArray3 = new float[15] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f }; _testArrays = new float[][] { testArray1, testArray2, testArray3 }; _testIndexArray = new int[18] { 0, 2, 5, 6, 8, 11, 12, 13, 14, 16, 18, 21, 22, 24, 26, 27, 28, 29 }; _comparer = new FloatEqualityComparer(); _matMulComparer = new FloatEqualityComparerForMatMul(); // Padded matrices whose dimensions are multiples of 8 float[] testMatrix1 = new float[8 * 8] { 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f, 1.96f, -2.38f, -9.76f, 13.84f, -106.37f, -26.93f, 32.45f, 3.29f }; float[] testMatrix2 = new float[8 * 16]; for (int i = 0; i < testMatrix2.Length; i++) { testMatrix2[i] = i + 1; } AlignedArray testMatrixAligned1 = new AlignedArray(8 * 8, _vectorAlignment); AlignedArray testMatrixAligned2 = new AlignedArray(8 * 16, _vectorAlignment); testMatrixAligned1.CopyFrom(testMatrix1); testMatrixAligned2.CopyFrom(testMatrix2); _testMatrices = new AlignedArray[] { testMatrixAligned1, testMatrixAligned2 }; // Padded source vectors whose dimensions are multiples of 8 float[] testSrcVector1 = new float[8] { 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f }; float[] testSrcVector2 = new float[16] { 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f, 16f }; AlignedArray testSrcVectorAligned1 = new AlignedArray(8, _vectorAlignment); AlignedArray testSrcVectorAligned2 = new AlignedArray(16, _vectorAlignment); testSrcVectorAligned1.CopyFrom(testSrcVector1); testSrcVectorAligned2.CopyFrom(testSrcVector2); _testSrcVectors = new AlignedArray[] { testSrcVectorAligned1, testSrcVectorAligned2 }; // Padded destination vectors whose dimensions are multiples of 8 float[] testDstVector1 = new float[8] { 0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f }; float[] testDstVector2 = new float[16] { 0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f, 11f, 12f, 13f, 14f, 15f }; AlignedArray testDstVectorAligned1 = new AlignedArray(8, _vectorAlignment); AlignedArray testDstVectorAligned2 = new AlignedArray(16, _vectorAlignment); testDstVectorAligned1.CopyFrom(testDstVector1); testDstVectorAligned2.CopyFrom(testDstVector2); _testDstVectors = new AlignedArray[] { testDstVectorAligned1, testDstVectorAligned2 }; if ((SkipAvxSse || IsNetCore) && !IsNetCore2OrOlder) { _disableAvxEnvironmentVariables = new Dictionary <string, string>() { { _disableAvx, "0" } }; _disableAvxAndSseEnvironmentVariables = new Dictionary <string, string>() { { _disableAvx, "0" }, { _disableSse, "0" } }; } }