Consume self and create a Tensor, avoiding a copy on CPU by reusing the data buffer.
(self, device: &Device)
| 130 | |
| 131 | /// Consume self and create a Tensor, avoiding a copy on CPU by reusing the data buffer. |
| 132 | pub fn into_tensor(self, device: &Device) -> Result<Tensor> { |
| 133 | let dtype = u8_to_dtype(self.dtype)?; |
| 134 | if matches!(device, Device::Cpu) { |
| 135 | // Reinterpret owned Vec<u8> directly as typed storage — avoids memcpy. |
| 136 | Self::vec_u8_to_tensor(self.data, dtype, &self.shape) |
| 137 | } else { |
| 138 | // GPU path must copy to device memory regardless. |
| 139 | Ok(Tensor::from_raw_buffer(&self.data, dtype, &self.shape, device)?) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// Zero-copy conversion of Vec<u8> → CPU Tensor by reinterpreting bytes. |
| 144 | fn vec_u8_to_tensor(data: Vec<u8>, dtype: DType, shape: &[usize]) -> Result<Tensor> { |
no test coverage detected