Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:LaurentMazare Tch rs Tensor Display

From Leeroopedia
Revision as of 15:32, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/LaurentMazare_Tch_rs_Tensor_Display.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

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} ]

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment