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 build/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<SystemReflectionEmitLightweightPackageVersion>4.3.0</SystemReflectionEmitLightweightPackageVersion>
<SystemThreadingTasksDataflowPackageVersion>4.8.0</SystemThreadingTasksDataflowPackageVersion>
<SystemComponentModelCompositionVersion>4.5.0</SystemComponentModelCompositionVersion>
<MathNumericPackageVersion>4.6.0</MathNumericPackageVersion>
<MathNumericPackageVersion>4.6.0</MathNumericPackageVersion>
</PropertyGroup>

<!-- Other/Non-Core Product Dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void ConcatTransform()
Console.WriteLine($"{outputColumnName} column obtained post-transformation.");
foreach (var featureRow in featuresColumn)
{
foreach (var value in featureRow.Features.Values)
foreach (var value in featureRow.Features.GetValues())
Console.Write($"{value} ");
Console.WriteLine("");
}
Expand Down
4 changes: 2 additions & 2 deletions docs/samples/Microsoft.ML.Samples/Dynamic/KeyToValue_Term.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void KeyToValue_Term()
Console.WriteLine($"{columnName} column obtained post-transformation.");
foreach (var row in column)
{
foreach (var value in row.Values)
foreach (var value in row.GetValues())
Console.Write($"{value} ");
Console.WriteLine("");
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public static void KeyToValue_Term()

foreach (var row in originalColumnBack)
{
foreach (var value in row.Values)
foreach (var value in row.GetValues())
Console.Write($"{value} ");
Console.WriteLine("");
}
Expand Down
2 changes: 1 addition & 1 deletion docs/samples/Microsoft.ML.Samples/Dynamic/TextTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void TextTransform()
Console.WriteLine($"{columnName} column obtained post-transformation.");
foreach (var featureRow in column)
{
foreach (var value in featureRow.Values)
foreach (var value in featureRow.GetValues())
Comment thread
eerhardt marked this conversation as resolved.
Resolved
Console.Write($"{value} ");
Console.WriteLine("");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public static void LightGbmRegression()
VBuffer<float> weights = default;
pred.GetFeatureWeights(ref weights);

Console.WriteLine($"weight 0 - {weights.Values[0]}");
Console.WriteLine($"weight 1 - {weights.Values[1]}");
var weightsValues = weights.GetValues();
Console.WriteLine($"weight 0 - {weightsValues[0]}");
Console.WriteLine($"weight 1 - {weightsValues[1]}");

// Evaluate how the model is doing on the test data
var dataWithPredictions = model.Transform(testData);
Expand Down
5 changes: 3 additions & 2 deletions docs/samples/Microsoft.ML.Samples/Static/SDCARegression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public static void SdcaRegression()
VBuffer<float> weights = default;
pred.GetFeatureWeights(ref weights);

Console.WriteLine($"weight 0 - {weights.Values[0]}");
Console.WriteLine($"weight 1 - {weights.Values[1]}");
var weightsValues = weights.GetValues();
Console.WriteLine($"weight 0 - {weightsValues[0]}");
Console.WriteLine($"weight 1 - {weightsValues[1]}");

// Evaluate how the model is doing on the test data
var dataWithPredictions = model.Transform(testData);
Expand Down
23 changes: 12 additions & 11 deletions src/Microsoft.ML.Api/DataViewConstructionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,10 @@ private Delegate CreateConvertingArrayGetterDelegate<TSrc, TDst>(Delegate peekDe
{
peek(GetCurrentRowObject(), Position, ref buf);
var n = Utils.Size(buf);
dst = new VBuffer<TDst>(n, Utils.Size(dst.Values) < n
? new TDst[n]
: dst.Values, dst.Indices);
var dstEditor = VBufferEditor.Create(ref dst, n);
for (int i = 0; i < n; i++)
dst.Values[i] = convert(buf[i]);
dstEditor.Values[i] = convert(buf[i]);
dst = dstEditor.Commit();
});
}

Expand All @@ -267,10 +266,10 @@ private Delegate CreateDirectArrayGetterDelegate<TDst>(Delegate peekDel)
{
peek(GetCurrentRowObject(), Position, ref buf);
var n = Utils.Size(buf);
dst = new VBuffer<TDst>(n, Utils.Size(dst.Values) < n ? new TDst[n] : dst.Values,
dst.Indices);
var dstEditor = VBufferEditor.Create(ref dst, n);
if (buf != null)
Array.Copy(buf, dst.Values, n);
buf.AsSpan(0, n).CopyTo(dstEditor.Values);
dst = dstEditor.Commit();
});
}

Expand Down Expand Up @@ -955,11 +954,12 @@ private void GetStringArray(ref VBuffer<ReadOnlyMemory<char>> dst)
{
var value = (string[])(object)Value;
var n = Utils.Size(value);
dst = new VBuffer<ReadOnlyMemory<char>>(n, Utils.Size(dst.Values) < n ? new ReadOnlyMemory<char>[n] : dst.Values, dst.Indices);
var dstEditor = VBufferEditor.Create(ref dst, n);

for (int i = 0; i < n; i++)
dst.Values[i] = value[i].AsMemory();
dstEditor.Values[i] = value[i].AsMemory();

dst = dstEditor.Commit();
}

private ValueGetter<VBuffer<TDst>> GetArrayGetter<TDst>()
Expand All @@ -968,9 +968,10 @@ private ValueGetter<VBuffer<TDst>> GetArrayGetter<TDst>()
var n = Utils.Size(value);
return (ref VBuffer<TDst> dst) =>
{
dst = new VBuffer<TDst>(n, Utils.Size(dst.Values) < n ? new TDst[n] : dst.Values, dst.Indices);
var dstEditor = VBufferEditor.Create(ref dst, n);
if (value != null)
Array.Copy(value, dst.Values, n);
value.AsSpan(0, n).CopyTo(dstEditor.Values);
dst = dstEditor.Commit();
};
}

Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.ML.Api/TypedCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Runtime.Internal.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Runtime.Internal.Utilities;

namespace Microsoft.ML.Runtime.Api
{
Expand Down Expand Up @@ -381,21 +381,21 @@ private Action<TRow> CreateDirectVBufferSetter<TDst>(IRow input, int col, Delega
return row =>
{
typedPeek(row, Position, ref buf);
value = new VBuffer<TDst>(0, buf, value.Indices);
getter(ref value);
if (value.Length == Utils.Size(buf) && value.IsDense)
{
// In this case, value.Values alone is enough to represent the vector.
// Otherwise, we are either sparse (and need densifying), or value.Values is too large,
// and we need to truncate.
buf = value.Values;
// In this case, buf (which came from the input object) is the
// right size to represent the vector.
// Otherwise, we are either sparse (and need densifying), or value.GetValues()
// is a different length than buf.
value.CopyTo(buf);
}
else
{
buf = new TDst[value.Length];

if (value.IsDense)
Array.Copy(value.Values, buf, value.Length);
value.GetValues().CopyTo(buf);
else
{
foreach (var pair in value.Items(true))
Expand Down
11 changes: 0 additions & 11 deletions src/Microsoft.ML.Core/Data/VBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ public readonly struct VBuffer<T>
/// </summary>
public readonly int Length;

/// <summary>
/// The values. Only the first Count of these are valid.
/// </summary>
public T[] Values => _values;

/// <summary>
/// The indices. For a dense representation, this array is not used. For a sparse representation
/// it is parallel to values and specifies the logical indices for the corresponding values.
/// </summary>
public int[] Indices => _indices;

/// <summary>
/// The explicitly represented values.
/// </summary>
Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.ML.Core/Utilities/BigArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,12 @@ public void AddRange(ReadOnlySpan<T> src)
/// Concurrent calls to this method is valid even with one single concurrent call
/// to <see cref="M:AddRange"/>.
/// </summary>
public void CopyTo(long idx, T[] dst, int length)
public void CopyTo(long idx, Span<T> dst, int length)
{
// Accesses on the internal arrays of this class should be valid even if
// some other thread is utilizing AddRange, since Utils.EnsureSize(...) will
// not replace the array until any allocation or copying has already happened.
Contracts.Assert(0 <= length && length <= Utils.Size(dst));
Contracts.Assert(0 <= length && length <= dst.Length);
Contracts.Assert(idx <= Length && length <= Length - idx);
if (length == 0)
return;
Expand All @@ -404,15 +404,15 @@ public void CopyTo(long idx, T[] dst, int length)
// Spans only one subarray, most common case and simplest implementation.
Contracts.Assert(miLim - miMin == length);
Contracts.Assert(miLim <= Utils.Size(_entries[maMax]));
Array.Copy(_entries[maMax], miMin, dst, 0, length);
_entries[maMax].AsSpan(miMin, length).CopyTo(dst);
break;
case 1:
// Spans two subarrays.
Contracts.Assert((BlockSize - miMin) + miLim == length);
Contracts.Assert(BlockSize <= Utils.Size(_entries[maMin]));
Array.Copy(_entries[maMin], miMin, dst, 0, BlockSize - miMin);
_entries[maMin].AsSpan(miMin, BlockSize - miMin).CopyTo(dst);
Contracts.Assert(miLim <= Utils.Size(_entries[maMax]));
Array.Copy(_entries[maMax], 0, dst, BlockSize - miMin, miLim);
_entries[maMax].AsSpan(0, miLim).CopyTo(dst.Slice(BlockSize - miMin));
break;
default:
// Spans three or more subarrays. Very rare.
Expand All @@ -421,19 +421,19 @@ public void CopyTo(long idx, T[] dst, int length)
// Copy the first segment.
Contracts.Assert(BlockSize <= Utils.Size(_entries[maMin]));
int dstSoFar = BlockSize - miMin;
Array.Copy(_entries[maMin], miMin, dst, 0, dstSoFar);
_entries[maMin].AsSpan(miMin, dstSoFar).CopyTo(dst);
// Copy the internal segments.
for (int major = maMin + 1; major < maMax; ++major)
{
Contracts.Assert(BlockSize <= Utils.Size(_entries[major]));
Array.Copy(_entries[major], 0, dst, dstSoFar, BlockSize);
_entries[major].AsSpan(0, BlockSize).CopyTo(dst.Slice(dstSoFar));
dstSoFar += BlockSize;
Contracts.Assert(dstSoFar < length);
}
// Copy the last segment.
Contracts.Assert(length - dstSoFar == miLim);
Contracts.Assert(miLim <= Utils.Size(_entries[maMax]));
Array.Copy(_entries[maMax], 0, dst, dstSoFar, miLim);
_entries[maMax].AsSpan(0, miLim).CopyTo(dst.Slice(dstSoFar));
break;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/Microsoft.ML.Core/Utilities/HashArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,15 @@ private void DumpStats()
}

/// <summary>
/// Copies all items to the passed in array. Requires the passed in array to be at least the
/// Copies all items to the passed in span. Requires the passed in span to be at least the
/// same length as Count.
/// </summary>
public void CopyTo(TItem[] array)
public void CopyTo(Span<TItem> destination)
{
Contracts.Check(array != null);
Contracts.Check(array.Length >= _ct);
Contracts.Check(destination.Length >= _ct);

for (int i = 0; i < _ct; i++)
array[i] = _entries[i].Value;
destination[i] = _entries[i].Value;
}

private static class HashHelpers
Expand Down
14 changes: 5 additions & 9 deletions src/Microsoft.ML.Core/Utilities/MathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,14 @@ public static int ArgMin(ReadOnlySpan<Float> a)
/// <summary>
/// computes the "softmax" function: log sum_i exp x_i
/// </summary>
/// <param name="inputs">Array of numbers to softmax</param>
/// <param name="count">the number of input array elements to process</param>
/// <param name="inputs">Span of numbers to softmax</param>
/// <returns>the softmax of the numbers</returns>
/// <remarks>may have slightly lower roundoff error if inputs are sorted, smallest first</remarks>
public static Float SoftMax(Float[] inputs, int count)
public static Float SoftMax(ReadOnlySpan<float> inputs)
{
Contracts.AssertValue(inputs);
Contracts.Assert(0 < count & count <= inputs.Length);

int maxIdx = 0;
Float max = Float.NegativeInfinity;
for (int i = 0; i < count; i++)
for (int i = 0; i < inputs.Length; i++)
{
if (inputs[i] > max)
{
Expand All @@ -222,13 +218,13 @@ public static Float SoftMax(Float[] inputs, int count)
if (Float.IsNegativeInfinity(max))
return Float.NegativeInfinity;

if (count == 1)
if (inputs.Length == 1)
return max;

double intermediate = 0.0;
Float cutoff = max - LogTolerance;

for (int i = 0; i < count; i++)
for (int i = 0; i < inputs.Length; i++)
{
if (i == maxIdx)
continue;
Expand Down
27 changes: 23 additions & 4 deletions src/Microsoft.ML.Core/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ public static void Push<T>(ref Stack<T> stack, T item)
stack.Push(item);
}

/// <summary>
/// Copies the values from src to dst.
/// </summary>
/// <remarks>
/// This can be removed once we have the APIs from https://github.com/dotnet/corefx/issues/33006.
/// </remarks>
public static void CopyTo<T>(this List<T> src, Span<T> dst, int? count = null)
{
Contracts.Assert(src != null);
Contracts.Assert(!count.HasValue || (0 <= count && count <= src.Count));
Contracts.Assert(src.Count <= dst.Length);

count = count ?? src.Count;
for (int i = 0; i < count; i++)
{
dst[i] = src[i];
}
}

/// <summary>
/// Assumes input is sorted and finds value using BinarySearch.
/// If value is not found, returns the logical index of 'value' in the sorted list i.e index of the first element greater than value.
Expand Down Expand Up @@ -675,9 +694,9 @@ public static bool IsSorted(int[] values)
/// and between an inclusive lower and exclusive upper bound for
/// the first and last items, respectively.
/// </summary>
public static bool IsIncreasing(int min, int[] values, int lim)
public static bool IsIncreasing(int min, ReadOnlySpan<int> values, int lim)
{
if (Utils.Size(values) < 1)
if (values.Length < 1)
return true;

var prev = values[0];
Expand All @@ -697,9 +716,9 @@ public static bool IsIncreasing(int min, int[] values, int lim)
/// is sorted and unique, and between an inclusive lower and exclusive
/// upper bound for the first and last items, respectively.
/// </summary>
public static bool IsIncreasing(int min, int[] values, int len, int lim)
public static bool IsIncreasing(int min, ReadOnlySpan<int> values, int len, int lim)
{
Contracts.Check(Utils.Size(values) >= len);
Contracts.Check(values.Length >= len);
if (len < 1)
return true;

Expand Down
17 changes: 6 additions & 11 deletions src/Microsoft.ML.CpuMath/AlignedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,16 @@ public void CopyTo(int start, Float[] dst, int index, int count)
Array.Copy(Items, start + _base, dst, index, count);
}

public void CopyFrom(Float[] src, int index, int count)
public void CopyFrom(ReadOnlySpan<Float> src)
{
Contracts.Assert(0 <= count && count <= _size);
Contracts.Assert(src != null);
Contracts.Assert(0 <= index && index <= src.Length - count);
Array.Copy(src, index, Items, _base, count);
Contracts.Assert(src.Length <= _size);
src.CopyTo(Items.AsSpan(_base));
}

public void CopyFrom(int start, Float[] src, int index, int count)
public void CopyFrom(int start, ReadOnlySpan<Float> src)
{
Contracts.Assert(0 <= count);
Contracts.Assert(0 <= start && start <= _size - count);
Contracts.Assert(src != null);
Contracts.Assert(0 <= index && index <= src.Length - count);
Array.Copy(src, index, Items, start + _base, count);
Contracts.Assert(0 <= start && start <= _size - src.Length);
src.CopyTo(Items.AsSpan(start + _base));
}

// Copies values from a sparse vector.
Expand Down
Loading