|
2 | 2 | using Microsoft.ML.OnnxRuntime.Tensors;
|
3 | 3 | using OnnxStack.Core.Config;
|
4 | 4 | using System;
|
| 5 | +using System.Buffers; |
5 | 6 | using System.Collections.Concurrent;
|
6 | 7 | using System.Collections.Generic;
|
7 | 8 | using System.Linq;
|
8 | 9 | using System.Numerics;
|
| 10 | +using System.Runtime.InteropServices; |
9 | 11 |
|
10 | 12 | namespace OnnxStack.Core
|
11 | 13 | {
|
@@ -205,26 +207,166 @@ public static T GetBufferLength<T>(this ReadOnlySpan<T> array) where T : INumber
|
205 | 207 | }
|
206 | 208 |
|
207 | 209 |
|
| 210 | + /// <summary> |
| 211 | + /// Converts to long. |
| 212 | + /// </summary> |
| 213 | + /// <param name="array">The array.</param> |
| 214 | + /// <returns></returns> |
208 | 215 | public static long[] ToLong(this ReadOnlySpan<int> array)
|
209 | 216 | {
|
210 | 217 | return Array.ConvertAll(array.ToArray(), Convert.ToInt64);
|
211 | 218 | }
|
212 |
| - |
| 219 | + |
| 220 | + |
| 221 | + /// <summary> |
| 222 | + /// Converts the string representation of a number to an integer. |
| 223 | + /// </summary> |
| 224 | + /// <param name="array">The array.</param> |
| 225 | + /// <returns></returns> |
213 | 226 | public static int[] ToInt(this long[] array)
|
214 | 227 | {
|
215 | 228 | return Array.ConvertAll(array, Convert.ToInt32);
|
216 | 229 | }
|
217 | 230 |
|
| 231 | + |
| 232 | + /// <summary> |
| 233 | + /// Converts to long. |
| 234 | + /// </summary> |
| 235 | + /// <param name="array">The array.</param> |
| 236 | + /// <returns></returns> |
218 | 237 | public static long[] ToLong(this int[] array)
|
219 | 238 | {
|
220 | 239 | return Array.ConvertAll(array, Convert.ToInt64);
|
221 | 240 | }
|
222 | 241 |
|
223 | 242 |
|
224 |
| - public static OrtValue ToOrtValue<T>(this DenseTensor<T> tensor) where T : unmanaged |
| 243 | + /// <summary> |
| 244 | + /// Creates and OrtValue form the DenseTensor and NodeMetaData provided |
| 245 | + /// </summary> |
| 246 | + /// <param name="tensor">The tensor.</param> |
| 247 | + /// <param name="nodeMetadata">The node metadata.</param> |
| 248 | + /// <returns></returns> |
| 249 | + public static OrtValue ToOrtValue(this DenseTensor<float> tensor, NodeMetadata nodeMetadata) |
225 | 250 | {
|
226 |
| - return OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer, tensor.Dimensions.ToLong()); |
| 251 | + var dimensions = tensor.Dimensions.ToLong(); |
| 252 | + return nodeMetadata.ElementDataType switch |
| 253 | + { |
| 254 | + TensorElementType.Float16 => OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer.ToFloat16(), dimensions), |
| 255 | + TensorElementType.BFloat16 => OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer.ToBFloat16(), dimensions), |
| 256 | + _ => OrtValue.CreateTensorValueFromMemory(OrtMemoryInfo.DefaultInstance, tensor.Buffer, dimensions) |
| 257 | + }; |
227 | 258 | }
|
228 | 259 |
|
| 260 | + |
| 261 | + /// <summary> |
| 262 | + /// Creates and allocates output tensors buffer. |
| 263 | + /// </summary> |
| 264 | + /// <param name="nodeMetadata">The node metadata.</param> |
| 265 | + /// <param name="dimensions">The dimensions.</param> |
| 266 | + /// <returns></returns> |
| 267 | + public static OrtValue CreateOutputBuffer(this NodeMetadata nodeMetadata, ReadOnlySpan<int> dimensions) |
| 268 | + { |
| 269 | + return OrtValue.CreateAllocatedTensorValue(OrtAllocator.DefaultInstance, nodeMetadata.ElementDataType, dimensions.ToLong()); |
| 270 | + } |
| 271 | + |
| 272 | + |
| 273 | + /// <summary> |
| 274 | + /// Converts to DenseTensor<float>. |
| 275 | + /// </summary> |
| 276 | + /// <param name="ortValue">The ort value.</param> |
| 277 | + /// <returns></returns> |
| 278 | + public static DenseTensor<float> ToDenseTensor(this OrtValue ortValue) |
| 279 | + { |
| 280 | + var typeInfo = ortValue.GetTensorTypeAndShape(); |
| 281 | + var dimensions = typeInfo.Shape.ToInt(); |
| 282 | + return typeInfo.ElementDataType switch |
| 283 | + { |
| 284 | + TensorElementType.Float16 => new DenseTensor<float>(ortValue.GetTensorDataAsSpan<Float16>().ToFloat(), dimensions), |
| 285 | + TensorElementType.BFloat16 => new DenseTensor<float>(ortValue.GetTensorDataAsSpan<BFloat16>().ToFloat(), dimensions), |
| 286 | + _ => new DenseTensor<float>(ortValue.GetTensorDataAsSpan<float>().ToArray(), dimensions) |
| 287 | + }; |
| 288 | + } |
| 289 | + |
| 290 | + |
| 291 | + /// <summary> |
| 292 | + /// Converts to array. |
| 293 | + /// </summary> |
| 294 | + /// <param name="ortValue">The ort value.</param> |
| 295 | + /// <returns></returns> |
| 296 | + public static float[] ToArray(this OrtValue ortValue) |
| 297 | + { |
| 298 | + var typeInfo = ortValue.GetTensorTypeAndShape(); |
| 299 | + var dimensions = typeInfo.Shape.ToInt(); |
| 300 | + return typeInfo.ElementDataType switch |
| 301 | + { |
| 302 | + TensorElementType.Float16 => ortValue.GetTensorDataAsSpan<Float16>().ToFloat().ToArray(), |
| 303 | + TensorElementType.BFloat16 => ortValue.GetTensorDataAsSpan<BFloat16>().ToFloat().ToArray(), |
| 304 | + _ => ortValue.GetTensorDataAsSpan<float>().ToArray() |
| 305 | + }; |
| 306 | + } |
| 307 | + |
| 308 | + |
| 309 | + /// <summary> |
| 310 | + /// Converts to float16. |
| 311 | + /// </summary> |
| 312 | + /// <param name="inputMemory">The input memory.</param> |
| 313 | + /// <returns></returns> |
| 314 | + internal static Memory<Float16> ToFloat16(this Memory<float> inputMemory) |
| 315 | + { |
| 316 | + var elementCount = inputMemory.Length; |
| 317 | + var floatArray = new Float16[inputMemory.Length]; |
| 318 | + for (int i = 0; i < elementCount; i++) |
| 319 | + floatArray[i] = (Float16)inputMemory.Span[i]; |
| 320 | + |
| 321 | + return floatArray.AsMemory(); |
| 322 | + } |
| 323 | + |
| 324 | + |
| 325 | + /// <summary> |
| 326 | + /// Converts to BFloat16. |
| 327 | + /// </summary> |
| 328 | + /// <param name="inputMemory">The input memory.</param> |
| 329 | + /// <returns></returns> |
| 330 | + internal static Memory<BFloat16> ToBFloat16(this Memory<float> inputMemory) |
| 331 | + { |
| 332 | + var elementCount = inputMemory.Length; |
| 333 | + var floatArray = new BFloat16[inputMemory.Length]; |
| 334 | + for (int i = 0; i < elementCount; i++) |
| 335 | + floatArray[i] = (BFloat16)inputMemory.Span[i]; |
| 336 | + |
| 337 | + return floatArray.AsMemory(); |
| 338 | + } |
| 339 | + |
| 340 | + |
| 341 | + /// <summary> |
| 342 | + /// Converts to float. |
| 343 | + /// </summary> |
| 344 | + /// <param name="inputMemory">The input memory.</param> |
| 345 | + /// <returns></returns> |
| 346 | + internal static Memory<float> ToFloat(this ReadOnlySpan<Float16> inputMemory) |
| 347 | + { |
| 348 | + var elementCount = inputMemory.Length; |
| 349 | + var floatArray = new float[elementCount]; |
| 350 | + for (int i = 0; i < elementCount; i++) |
| 351 | + floatArray[i] = (float)inputMemory[i]; |
| 352 | + |
| 353 | + return floatArray.AsMemory(); |
| 354 | + } |
| 355 | + |
| 356 | + |
| 357 | + /// <summary> |
| 358 | + /// Converts to float. |
| 359 | + /// </summary> |
| 360 | + /// <param name="inputMemory">The input memory.</param> |
| 361 | + /// <returns></returns> |
| 362 | + internal static Memory<float> ToFloat(this ReadOnlySpan<BFloat16> inputMemory) |
| 363 | + { |
| 364 | + var elementCount = inputMemory.Length; |
| 365 | + var floatArray = new float[elementCount]; |
| 366 | + for (int i = 0; i < elementCount; i++) |
| 367 | + floatArray[i] = (float)inputMemory[i]; |
| 368 | + |
| 369 | + return floatArray.AsMemory(); |
| 370 | + } |
229 | 371 | }
|
230 | 372 | }
|
0 commit comments