|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Numerics.Tensors; |
| 8 | +using Microsoft.ML.Internal.CpuMath.Core; |
| 9 | + |
| 10 | +namespace Microsoft.ML.Internal.CpuMath |
| 11 | +{ |
| 12 | + [BestFriend] |
| 13 | + internal static partial class CpuMathUtils |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Adds a value to a destination. |
| 17 | + /// </summary> |
| 18 | + /// <param name="value">The value to add.</param> |
| 19 | + /// <param name="destination">The destination to add the value to.</param> |
| 20 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 21 | + public static void Add(float value, Span<float> destination) |
| 22 | + { |
| 23 | + Contracts.AssertNonEmpty(destination); |
| 24 | + TensorPrimitives.Add(destination, value, destination); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Scales a value to a destination. |
| 29 | + /// </summary> |
| 30 | + /// <param name="value">The value to add.</param> |
| 31 | + /// <param name="destination">The destination to add the value to.</param> |
| 32 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 33 | + public static void Scale(float value, Span<float> destination) |
| 34 | + { |
| 35 | + Contracts.AssertNonEmpty(destination); |
| 36 | + TensorPrimitives.Multiply(destination, value, destination); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Scales a values by a source to a destination. |
| 41 | + /// destination = value * source |
| 42 | + /// </summary> |
| 43 | + /// <param name="value">The value to scale by.</param> |
| 44 | + /// <param name="source">The source values.</param> |
| 45 | + /// <param name="destination">The destination.</param> |
| 46 | + /// <param name="count">The count of items.</param> |
| 47 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 48 | + public static void Scale(float value, ReadOnlySpan<float> source, Span<float> destination, int count) |
| 49 | + { |
| 50 | + Contracts.AssertNonEmpty(source); |
| 51 | + Contracts.AssertNonEmpty(destination); |
| 52 | + Contracts.Assert(count > 0); |
| 53 | + Contracts.Assert(count <= source.Length); |
| 54 | + Contracts.Assert(count <= destination.Length); |
| 55 | + |
| 56 | + TensorPrimitives.Multiply(source.Slice(0, count), value, destination); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Add to the destination from the source by scale. |
| 61 | + /// </summary> |
| 62 | + /// <param name="scale">The scale to add by.</param> |
| 63 | + /// <param name="source">The source values.</param> |
| 64 | + /// <param name="destination">The destination values.</param> |
| 65 | + /// <param name="count">The count of items.</param> |
| 66 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 67 | + public static void AddScale(float scale, ReadOnlySpan<float> source, Span<float> destination, int count) |
| 68 | + { |
| 69 | + Contracts.AssertNonEmpty(source); |
| 70 | + Contracts.AssertNonEmpty(destination); |
| 71 | + Contracts.Assert(count > 0); |
| 72 | + Contracts.Assert(count <= source.Length); |
| 73 | + Contracts.Assert(count <= destination.Length); |
| 74 | + |
| 75 | + TensorPrimitives.MultiplyAdd(source.Slice(0, count), scale, destination.Slice(0, count), destination); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Add to the destination by scale and source into a new result. |
| 80 | + /// </summary> |
| 81 | + /// <param name="scale">The scale to add by.</param> |
| 82 | + /// <param name="source">The source values.</param> |
| 83 | + /// <param name="destination">The destination values.</param> |
| 84 | + /// <param name="result">A new collection of values to be returned.</param> |
| 85 | + /// <param name="count">The count of items.</param> |
| 86 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 87 | + public static void AddScaleCopy(float scale, ReadOnlySpan<float> source, ReadOnlySpan<float> destination, Span<float> result, int count) |
| 88 | + { |
| 89 | + Contracts.AssertNonEmpty(source); |
| 90 | + Contracts.AssertNonEmpty(destination); |
| 91 | + Contracts.AssertNonEmpty(result); |
| 92 | + Contracts.Assert(count > 0); |
| 93 | + Contracts.Assert(count <= source.Length); |
| 94 | + Contracts.Assert(count <= destination.Length); |
| 95 | + Contracts.Assert(count <= result.Length); |
| 96 | + |
| 97 | + TensorPrimitives.MultiplyAdd(source.Slice(0, count), scale, destination.Slice(0, count), result.Slice(0, count)); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Add from a source to a destination. |
| 102 | + /// </summary> |
| 103 | + /// <param name="source">The source values.</param> |
| 104 | + /// <param name="destination">The destination values.</param> |
| 105 | + /// <param name="count">The count of items.</param> |
| 106 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 107 | + public static void Add(ReadOnlySpan<float> source, Span<float> destination, int count) |
| 108 | + { |
| 109 | + Contracts.AssertNonEmpty(source); |
| 110 | + Contracts.AssertNonEmpty(destination); |
| 111 | + Contracts.Assert(count > 0); |
| 112 | + Contracts.Assert(count <= source.Length); |
| 113 | + Contracts.Assert(count <= destination.Length); |
| 114 | + |
| 115 | + TensorPrimitives.Add(source.Slice(0, count), destination.Slice(0, count), destination.Slice(0, count)); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Multiply each element with left and right elements. |
| 120 | + /// </summary> |
| 121 | + /// <param name="left">The left element.</param> |
| 122 | + /// <param name="right">The right element.</param> |
| 123 | + /// <param name="destination">The destination values.</param> |
| 124 | + /// <param name="count">The count of items.</param> |
| 125 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 126 | + public static void MulElementWise(ReadOnlySpan<float> left, ReadOnlySpan<float> right, Span<float> destination, int count) |
| 127 | + { |
| 128 | + Contracts.AssertNonEmpty(left); |
| 129 | + Contracts.AssertNonEmpty(right); |
| 130 | + Contracts.AssertNonEmpty(destination); |
| 131 | + Contracts.Assert(count > 0); |
| 132 | + Contracts.Assert(count <= left.Length); |
| 133 | + Contracts.Assert(count <= right.Length); |
| 134 | + Contracts.Assert(count <= destination.Length); |
| 135 | + |
| 136 | + TensorPrimitives.Multiply(left.Slice(0, count), right.Slice(0, count), destination.Slice(0, count)); |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Sum the values in the source. |
| 141 | + /// </summary> |
| 142 | + /// <param name="source">The source values.</param> |
| 143 | + /// <returns>The sum of all items in <paramref name="source"/>.</returns> |
| 144 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 145 | + public static float Sum(ReadOnlySpan<float> source) |
| 146 | + { |
| 147 | + Contracts.AssertNonEmpty(source); |
| 148 | + |
| 149 | + return TensorPrimitives.Sum(source); |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Sum the squares of each item in the source. |
| 154 | + /// </summary> |
| 155 | + /// <param name="source">The source values.</param> |
| 156 | + /// <returns>The sum of the squares of all items in <paramref name="source"/>.</returns> |
| 157 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 158 | + public static float SumSq(ReadOnlySpan<float> source) |
| 159 | + { |
| 160 | + Contracts.AssertNonEmpty(source); |
| 161 | + |
| 162 | + return TensorPrimitives.SumOfSquares(source); |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Sum the absolute value of each item in the source. |
| 167 | + /// </summary> |
| 168 | + /// <param name="source">The source values.</param> |
| 169 | + /// <returns>The sum of all absolute value of the items in <paramref name="source"/>.</returns> |
| 170 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 171 | + public static float SumAbs(ReadOnlySpan<float> source) |
| 172 | + { |
| 173 | + Contracts.AssertNonEmpty(source); |
| 174 | + |
| 175 | + return TensorPrimitives.SumOfMagnitudes(source); |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// Returns the dot product of each item in the left and right spans. |
| 180 | + /// </summary> |
| 181 | + /// <param name="left">The left span.</param> |
| 182 | + /// <param name="right">The right span.</param> |
| 183 | + /// <param name="count">The count of items.</param> |
| 184 | + /// <returns>The dot product.</returns> |
| 185 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 186 | + public static float DotProductDense(ReadOnlySpan<float> left, ReadOnlySpan<float> right, int count) |
| 187 | + { |
| 188 | + Contracts.AssertNonEmpty(left); |
| 189 | + Contracts.AssertNonEmpty(right); |
| 190 | + Contracts.Assert(count > 0); |
| 191 | + Contracts.Assert(left.Length >= count); |
| 192 | + Contracts.Assert(right.Length >= count); |
| 193 | + |
| 194 | + return TensorPrimitives.Dot(left.Slice(0, count), right.Slice(0, count)); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments