public static void Save(this Tensor tensor, System.IO.BinaryWriter writer) { bool copied = false; if (tensor.device_type != DeviceType.CPU) { tensor = tensor.to(torch.CPU); copied = true; } // First, write the type writer.Encode((int)tensor.dtype); // 4 bytes // Then, the shape. writer.Encode(tensor.shape.Length); // 4 bytes foreach (var s in tensor.shape) { writer.Encode(s); // n * 8 bytes } // Then, the data writer.Write(tensor.bytes); // ElementSize * NumberofElements if (copied) { tensor.Dispose(); } }
public static void Save(this TorchTensor tensor, System.IO.BinaryWriter writer) { // First, write the type writer.Encode((int)tensor.Type); // 4 bytes // Then, the shape. writer.Encode(tensor.shape.Length); // 4 bytes foreach (var s in tensor.shape) { writer.Encode(s); // n * 8 bytes } // Then, the data writer.Write(tensor.Bytes()); // ElementSize * NumberofElements }