TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

[Feature Request]: Allocation-free ToArray(Array destination) method

Open ADH-LukeBollam opened this issue 2 years ago • 2 comments

Background and Feature Description

When evaluating sequential data, often we split large sequences into batches to be processed by a model. Combining batches back into a sequence requires multiple results.ToArray() calls, allocating a new array each time. If we could pre-allocate one large array and tell Tensorflow.Numpy to copy the results into this one, we can skip many expensive array allocation operations. It could also speed up general use cases of getting results back into C# allowing users to re-use an array.

API Definition and Usage

Add an extension method overload to the ToArray() method that allows passing in an existing Array to copy the data to

public static void ToArray<float>(this NDArray array, Array destination, long destinationIndex, long destinationLength)

Alternatives

No response

Risks

No response

ADH-LukeBollam avatar Jul 06 '23 03:07 ADH-LukeBollam

How large the destination will be? NDArray has a data property, maybe you can utilize the IntPtr type.

Oceania2018 avatar Jul 08 '23 02:07 Oceania2018

destination will be whatever size you want it to be, you initialise it externally then pass it to the function to get filled. The idea is the API is very similar to Array.Copy().

Do you know if using the IntPtr respects any slicing operations? For example at the moment, I read data from a slice of the last dimension: var slice = result[Slice.ParseSlices(":, :, 0")]

will Marshal.Copy(slice.data, ...) respect that?

ADH-LukeBollam avatar Jul 11 '23 01:07 ADH-LukeBollam