public void WriteAndReadByteTensorViaDiskFile() { const int size = 10; var file = new DiskFile("test1B.dat", "rwb"); Assert.NotNull(file); Assert.True(file.CanWrite); var tensor0 = new ByteTensor(size); for (var i = 0; i < size; ++i) { tensor0[i] = (byte)i; } file.WriteTensor(tensor0); Assert.Equal(size * sizeof(byte), file.Position); file.Seek(0); var tensor1 = new ByteTensor(size); var rd = file.ReadTensor(tensor1); Assert.Equal(rd, size); Assert.Equal(size * sizeof(byte), file.Position); for (var i = 0; i < rd; ++i) { Assert.Equal(tensor1[i], tensor1[i]); } file.Close(); Assert.False(file.IsOpen); }
public void CreateByteTensorOnes() { var shape = new long[] { 2, 2 }; TorchTensor t = ByteTensor.Ones(shape); Assert.Equal(shape, t.Shape); Assert.Equal((byte)1, t[0, 0].DataItem <byte>()); Assert.Equal((byte)1, t[1, 1].DataItem <byte>()); }
public Tensor forward(Tensor input) { if (input.dtype != ScalarType.Byte) { throw new ArgumentException("Only torch.byte image tensors are supported"); } var mask = -(1 << (8 - bits)); return(input & ByteTensor.from((byte)mask)); }
public static Tensor ToTensor <T>(this T[] rawArray, long[] dimensions, bool doCopy = false, bool requiresGrad = false) { var array = doCopy ? (T[])rawArray.Clone() : rawArray; switch (true) { case bool _ when typeof(T) == typeof(byte): { return(ByteTensor.from(array as byte[], dimensions, requiresGrad));; } case bool _ when typeof(T) == typeof(sbyte): { return(Int8Tensor.from(array as sbyte[], dimensions, requiresGrad));; } case bool _ when typeof(T) == typeof(short): { return(Int16Tensor.from(array as short[], dimensions, requiresGrad));; } case bool _ when typeof(T) == typeof(int): { return(Int32Tensor.from(array as int[], dimensions, requiresGrad)); } case bool _ when typeof(T) == typeof(long): { return(Int64Tensor.from(array as long[], dimensions, requiresGrad)); } case bool _ when typeof(T) == typeof(double): { return(Float64Tensor.from(array as double[], dimensions, requiresGrad)); } case bool _ when typeof(T) == typeof(float): { return(Float32Tensor.from(array as float[], dimensions, requiresGrad)); } case bool _ when typeof(T) == typeof(bool): { return(BoolTensor.from(array as bool[], dimensions, requiresGrad)); } //case bool _ when typeof(T) == typeof(System.Numerics.Complex): // { // return ComplexFloat64Tensor.from(array as System.Numerics.Complex[], dimensions, requiresGrad); // } default: throw new NotImplementedException($"Creating tensor of type {typeof(T)} is not supported."); } }
public void RandomTest() { var res = FloatTensor.Random(new long[] { 2 }); Assert.Equal(new long[] { 2 }, res.Shape); var res1 = ShortTensor.RandomIntegers(10, new long[] { 200 }); Assert.Equal(new long[] { 200 }, res1.Shape); var res2 = IntTensor.RandomIntegers(10, new long[] { 200 }); Assert.Equal(new long[] { 200 }, res2.Shape); var res3 = LongTensor.RandomIntegers(10, new long[] { 200 }); Assert.Equal(new long[] { 200 }, res3.Shape); var res4 = ByteTensor.RandomIntegers(10, new long[] { 200 }); Assert.Equal(new long[] { 200 }, res4.Shape); var res5 = SByteTensor.RandomIntegers(10, new long[] { 200 }); Assert.Equal(new long[] { 200 }, res5.Shape); var res6 = HalfTensor.RandomIntegers(10, new long[] { 200 }); Assert.Equal(new long[] { 200 }, res6.Shape); //var res7 = ComplexHalfTensor.RandomIntegers(10, new long[] { 200 }); //Assert.Equal(new long[] { 200 }, res7.Shape); //var res8 = ComplexFloatTensor.RandomIntegers(10, new long[] { 200 }); //Assert.Equal(new long[] { 200 }, res8.Shape); //var res9 = ComplexDoubleTensor.RandomIntegers(10, new long[] { 200 }); //Assert.Equal(new long[] { 200 }, res9.Shape); }
public static Tensor ToTensor <T>(this T scalar, torch.Device?device = null, bool requiresGrad = false) where T : struct { if (requiresGrad && typeof(T) != typeof(float) && typeof(T) != typeof(double)) { throw new ArgumentException(nameof(requiresGrad), "Only floating point types support gradients."); } if (typeof(T) == typeof(byte)) { return(ByteTensor.from((byte)(object)scalar, device, requiresGrad)); } if (typeof(T) == typeof(sbyte)) { return(Int8Tensor.from((sbyte)(object)scalar, device, requiresGrad)); } if (typeof(T) == typeof(short)) { return(Int16Tensor.from((short)(object)scalar, device, requiresGrad)); } if (typeof(T) == typeof(int)) { return(Int32Tensor.from((int)(object)scalar, device, requiresGrad)); } if (typeof(T) == typeof(long)) { return(Int64Tensor.from((long)(object)scalar, device, requiresGrad)); } if (typeof(T) == typeof(double)) { return(Float64Tensor.from((double)(object)scalar, device, requiresGrad)); } if (typeof(T) == typeof(float)) { return(Float32Tensor.from((float)(object)scalar, device, requiresGrad)); } throw new NotImplementedException($"Creating tensor of type {typeof(T)} is not supported."); }
/// <summary> /// Read bytes from the file into the given byte tensor. /// </summary> /// <param name="tensor">A tensor to place the data in after reading it from the file.</param> /// <returns>The number of bytes read.</returns> public long ReadTensor(ByteTensor tensor) { return(THFile_readByteRaw(this.handle, tensor.Data, tensor.NumElements)); }
/// <summary> /// Write bytes to the file from the given byte tensor. /// </summary> /// <param name="tensor">A tensor containing data to be written to the file.</param> /// <returns>The number of bytes written.</returns> public long WriteTensor(ByteTensor tensor) { return(THFile_writeByteRaw(this.handle, tensor.Data, tensor.NumElements)); }