Implementation:LaurentMazare Tch rs Tensor Display
| Knowledge Sources | |
|---|---|
| Domains | Tensor_Operations, Display |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for pretty-printing tensor values provided by the tch-rs library.
Description
The Tensor_Display module implements Rust's Debug and Display formatting traits for the Tensor type, mirroring PyTorch's Python-side tensor printing logic. It classifies tensor element types into BasicKind categories (Float, Int, Bool, Complex) and formats tensor contents with appropriate precision, summarization for large tensors, and shape/dtype annotations.
Usage
This formatting is invoked automatically when using println!("{:?}", tensor) or println!("{}", tensor) in Rust code. No explicit import is needed beyond having a Tensor value in scope.
Code Reference
Source Location
- Repository: LaurentMazare_Tch_rs
- File: src/tensor/display.rs
- Lines: 1-434
Signature
impl std::fmt::Debug for Tensor {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { ... }
}
impl std::fmt::Display for Tensor {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { ... }
}
Import
use tch::Tensor; // Debug and Display are auto-available
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| self | &Tensor | Yes | The tensor to format |
| f | &mut Formatter | Yes | The output formatter |
Outputs
| Name | Type | Description |
|---|---|---|
| Result | fmt::Result | Success or formatting error |
Usage Examples
use tch::Tensor;
let t = Tensor::from_slice(&[1.0f32, 2.0, 3.0]);
println!("{:?}", t); // Debug format with shape info
println!("{}", t); // Display format with values
let matrix = Tensor::from_slice(&[1, 2, 3, 4, 5, 6]).view([2, 3]);
println!("{}", matrix);
// Output:
// 1 2 3
// 4 5 6
// [ CPULongTensor{2,3} ]