Open vpenades opened 1 week ago
Tagging subscribers to this area: @dotnet/area-system-numerics-tensors See info in area-owners.md if you want to be subscribed.
Converting between two different types is supported via the generic TensorPrimitives.ConvertTruncating, ConvertSaturating, and ConvertChecked methods. There's no built-in operation that currently combines that with the divide, but it can be done with a second call:
ReadOnlySpan<byte> bytes = RandomNumberGenerator.GetBytes(100);
Span<float> floats = new float[bytes.Length];
TensorPrimitives.ConvertTruncating(bytes, floats);
TensorPrimitives.Divide(floats, 255.0f, floats);
No wonder I could not find these methods.... they're in the Net9 preview packages, is that right?
Anyway, given it's in preview, I would still include a byte to float conversion with divide: it's such a common operation that any future improvement would automatically be used by everybody, that is, imagine that some platform introduces a fast path for this exact operation...
they're in the Net9 preview packages, is that right?
Yes
Looking at the code, I don't see that ConvertTruncating has any kind of vectorial acceleration code to convert byte to float. Right now, is there an advantage of using ConvertTruncating, over a plain loop?
Looking at the code, I don't see that ConvertTruncating has any kind of vectorial acceleration code to convert byte to float. Right now, is there an advantage of using ConvertTruncating, over a plain loop?
Not in what's merged, but that's just a matter of adding an additional path for these types, e.g. https://github.com/dotnet/runtime/pull/103820
Background and motivation
Working with images and tensors, it is fairly common to convert an array of bytes to an array of floats.
Also, in many cases the conversion requires some normalization or scaling, that is, byte values in the range of 0-255 are converted to float values in the range of 0-1
The usual operation is this:
Now, I don't know if there's faster way of doing this using SIMD, or any other exotic, platform specific mechanism, but if it doesn't exist now, it may exist in the future, so by using a system function, any current application may benefit from future improvements on the API.
Also, I don't know if this conversion already exists somewhere else in the APIs, I've looked for it without success. But certainly, I would expect such conversions to be available in TensorPrimitives.
My needs are limited to byte-float conversions, but certainly, other types may be included.
API Proposal
API Usage
Alternative Designs
No response
Risks
No response