|
| 1 | +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information. |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics.Contracts; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +#nullable enable |
| 8 | +namespace TorchSharp |
| 9 | +{ |
| 10 | + public static partial class torch |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Create a tensor from an array of values, shaping it based on the shape passed in. |
| 14 | + /// </summary> |
| 15 | + /// <remarks>The Torch runtime does not take ownership of the data, so there is no device argument.</remarks> |
| 16 | + [Pure] |
| 17 | + public static Tensor tensor(IList<BFloat16> rawArray, ReadOnlySpan<long> dimensions, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 18 | + { |
| 19 | + return _tensor_generic(rawArray.ToArray(), dimensions, (sbyte)ScalarType.BFloat16, dtype, device, requires_grad, false, names); |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Create a tensor from an array of values, shaping it based on the shape passed in. |
| 24 | + /// </summary> |
| 25 | + [Pure] |
| 26 | + public static Tensor tensor(BFloat16[] rawArray, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 27 | + { |
| 28 | + return _tensor_generic(rawArray, stackalloc long[] { rawArray.LongLength }, (sbyte)ScalarType.BFloat16, dtype, device, requires_grad, names: names); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Create a tensor from an array of values, shaping it based on the shape passed in. |
| 33 | + /// </summary> |
| 34 | + [Pure] |
| 35 | + public static Tensor tensor(BFloat16[] rawArray, ReadOnlySpan<long> dimensions, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 36 | + { |
| 37 | + return _tensor_generic(rawArray, dimensions, (sbyte)ScalarType.BFloat16, dtype, device, requires_grad, names: names); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Create a 1-D tensor from an array of values, shaping it based on the input array. |
| 42 | + /// </summary> |
| 43 | + /// <remarks>The Torch runtime does not take ownership of the data, so there is no device argument.</remarks> |
| 44 | + [Pure] |
| 45 | + public static Tensor tensor(IList<BFloat16> rawArray, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 46 | + { |
| 47 | + return tensor(rawArray, stackalloc long[] { (long)rawArray.Count }, dtype, device, requires_grad, names: names); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Create a tensor from an array of values, organizing it as a two-dimensional tensor. |
| 52 | + /// </summary> |
| 53 | + /// <remarks> |
| 54 | + /// The Torch runtime does not take ownership of the data, so there is no device argument. |
| 55 | + /// The input array must have rows * columns elements. |
| 56 | + /// </remarks> |
| 57 | + [Pure] |
| 58 | + public static Tensor tensor(IList<BFloat16> rawArray, long rows, long columns, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 59 | + { |
| 60 | + return tensor(rawArray, stackalloc long[] { rows, columns }, dtype, device, requires_grad, names: names); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Create a tensor from an array of values, organizing it as a three-dimensional tensor. |
| 65 | + /// </summary> |
| 66 | + /// <remarks> |
| 67 | + /// The Torch runtime does not take ownership of the data, so there is no device argument. |
| 68 | + /// The input array must have dim0*dim1*dim2 elements. |
| 69 | + /// </remarks> |
| 70 | + [Pure] |
| 71 | + public static Tensor tensor(IList<BFloat16> rawArray, long dim0, long dim1, long dim2, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 72 | + { |
| 73 | + return tensor(rawArray, stackalloc long[] { dim0, dim1, dim2 }, dtype, device, requires_grad, names: names); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Create a tensor from an array of values, organizing it as a four-dimensional tensor. |
| 78 | + /// </summary> |
| 79 | + /// <remarks> |
| 80 | + /// The Torch runtime does not take ownership of the data, so there is no device argument. |
| 81 | + /// The input array must have dim0*dim1*dim2*dim3 elements. |
| 82 | + /// </remarks> |
| 83 | + [Pure] |
| 84 | + public static Tensor tensor(IList<BFloat16> rawArray, long dim0, long dim1, long dim2, long dim3, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 85 | + { |
| 86 | + return tensor(rawArray, stackalloc long[] { dim0, dim1, dim2, dim3 }, dtype, device, requires_grad, names: names); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Create a two-dimensional tensor from a two-dimensional array of values. |
| 91 | + /// </summary> |
| 92 | + [Pure] |
| 93 | + public static Tensor tensor(BFloat16[,] rawArray, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 94 | + { |
| 95 | + return _tensor_generic(rawArray, stackalloc long[] { rawArray.GetLongLength(0), rawArray.GetLongLength(1) }, (sbyte)ScalarType.BFloat16, dtype, device, requires_grad, names: names); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Create a three-dimensional tensor from a three-dimensional array of values. |
| 100 | + /// </summary> |
| 101 | + [Pure] |
| 102 | + public static Tensor tensor(BFloat16[,,] rawArray, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 103 | + { |
| 104 | + return _tensor_generic(rawArray, stackalloc long[] { rawArray.GetLongLength(0), rawArray.GetLongLength(1), rawArray.GetLongLength(2) }, (sbyte)ScalarType.BFloat16, dtype, device, requires_grad, names: names); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Create a four-dimensional tensor from a four-dimensional array of values. |
| 109 | + /// </summary> |
| 110 | + [Pure] |
| 111 | + public static Tensor tensor(BFloat16[,,,] rawArray, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 112 | + { |
| 113 | + return _tensor_generic(rawArray, stackalloc long[] { rawArray.GetLongLength(0), rawArray.GetLongLength(1), rawArray.GetLongLength(2), rawArray.GetLongLength(3) }, (sbyte)ScalarType.BFloat16, dtype, device, requires_grad, names: names); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Create a tensor from an array of values, shaping it based on the shape passed in. |
| 118 | + /// </summary> |
| 119 | + [Pure] |
| 120 | + public static Tensor tensor(Memory<BFloat16> rawArray, ReadOnlySpan<long> dimensions, ScalarType? dtype = null, Device? device = null, bool requires_grad = false, string[]? names = null) |
| 121 | + { |
| 122 | + return _tensor_generic(rawArray, dimensions, (sbyte)ScalarType.BFloat16, dtype, device, requires_grad, names: names); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments