Repository: pytorch
Issue: Add ability to add custom suffixes to tensor repr
Labels: module: printing,triaged,enhancement,needs design,tensor subclass,module: python frontend

Amongst other things #73459 cc @jbschlosser added the ability add custom "prefixes" to `Tensor.__repr__` through the `tensor_contents` parameter:

```py
>>> t = torch.rand(3)
>>> t.__repr__(tensor_contents="foo")
'tensor(foo)'
```

In `torchvision` we have custom tensors that would benefit from also setting custom suffixes as it is done internally for sparse tensors and the like. For example, we have a `class Image(Tensor)` that has an additional `color_space` field. It would be awesome if we had an easy way to add it to the repr like

```py
>>> image = features.Image(torch.rand(3, 2, 2))
>>> image.color_space
<ColorSpace.RGB: 4>
>>> image
Image([[[0.4907, 0.5785],
        [0.4636, 0.2560]],
       [[0.7399, 0.1161],
        [0.5769, 0.4998]],
       [[0.9076, 0.6455],
        [0.0953, 0.7192]]], color_space=ColorSpace.RGB)
```

Alternatively, we could do something like

```py
class Image(Tensor):
    def __repr__(self, tensor_contents: Optional[str] = None) -> str:
        return f"{super().__repr__()[:-1]}, color_space={self.color_space})"
```

That works fine for small tensors

```py
>>> t = torch.rand(3, 2, 2)
>>> t.to(torch.float64)
tensor([[[0.6500, 0.5603],
         [0.2210, 0.3353]],
        [[0.9003, 0.9231],
         [0.1136, 0.9700]],
        [[0.0781, 0.5636],
         [0.2230, 0.7293]]], dtype=torch.float64)
>>> Image(t)
Image([[[0.6500, 0.5603],
        [0.2210, 0.3353]],
       [[0.9003, 0.9231],
        [0.1136, 0.9700]],
       [[0.0781, 0.5636],
        [0.2230, 0.7293]]], color_space=ColorSpace.RGB)
```

but for larger tensors, the suffixes get placed differently:


```py
>>> t = torch.rand(3, 100, 100)
>>> t.to(torch.float64)
tensor([[[0.9821, 0.1938, 0.8468,  ..., 0.2203, 0.9435, 0.2541],
         [0.8894, 0.3334, 0.7329,  ..., 0.7636, 0.2397, 0.1859],
         [0.8137, 0.5910, 0.8349,  ..., 0.9995, 0.3999, 0.5790],
         ...,
         [0.3414, 0.2053, 0.1435,  ..., 0.7163, 0.4972, 0.3687],
         [0.3409, 0.5285, 0.2580,  ..., 0.8293, 0.3085, 0.9854],
         [0.5308, 0.3242, 0.0516,  ..., 0.0413, 0.7607, 0.7092]],
        [[0.2756, 0.9657, 0.0439,  ..., 0.0201, 0.2599, 0.8245],
         [0.5095, 0.4416, 0.1496,  ..., 0.3786, 0.1401, 0.7142],
         [0.0590, 0.2262, 0.3407,  ..., 0.2077, 0.0955, 0.4379],
         ...,
         [0.4888, 0.5828, 0.0633,  ..., 0.9463, 0.0943, 0.0329],
         [0.1803, 0.8766, 0.2687,  ..., 0.5351, 0.2911, 0.9938],
         [0.8441, 0.1051, 0.7040,  ..., 0.8154, 0.4500, 0.5589]],
        [[0.4472, 0.2326, 0.8268,  ..., 0.5057, 0.2554, 0.2825],
         [0.2801, 0.7943, 0.0670,  ..., 0.8276, 0.0093, 0.0674],
         [0.5124, 0.1265, 0.8184,  ..., 0.4340, 0.4333, 0.1293],
         ...,
         [0.1073, 0.1239, 0.9396,  ..., 0.4503, 0.4583, 0.8074],
         [0.3336, 0.2680, 0.1699,  ..., 0.6623, 0.9152, 0.9129],
         [0.2971, 0.5474, 0.4108,  ..., 0.3925, 0.8302, 0.7814]]],
       dtype=torch.float64)
>>> Image(t)
Image([[[0.9821, 0.1938, 0.8468,  ..., 0.2203, 0.9435, 0.2541],
        [0.8894, 0.3334, 0.7329,  ..., 0.7636, 0.2397, 0.1859],
        [0.8137, 0.5910, 0.8349,  ..., 0.9995, 0.3999, 0.5790],
        ...,
        [0.3414, 0.2053, 0.1435,  ..., 0.7163, 0.4972, 0.3687],
        [0.3409, 0.5285, 0.2580,  ..., 0.8293, 0.3085, 0.9854],
        [0.5308, 0.3242, 0.0516,  ..., 0.0413, 0.7607, 0.7092]],
       [[0.2756, 0.9657, 0.0439,  ..., 0.0201, 0.2599, 0.8245],
        [0.5095, 0.4416, 0.1496,  ..., 0.3786, 0.1401, 0.7142],
        [0.0590, 0.2262, 0.3407,  ..., 0.2077, 0.0955, 0.4379],
        ...,
        [0.4888, 0.5828, 0.0633,  ..., 0.9463, 0.0943, 0.0329],
        [0.1803, 0.8766, 0.2687,  ..., 0.5351, 0.2911, 0.9938],
        [0.8441, 0.1051, 0.7040,  ..., 0.8154, 0.4500, 0.5589]],
       [[0.4472, 0.2326, 0.8268,  ..., 0.5057, 0.2554, 0.2825],
        [0.2801, 0.7943, 0.0670,  ..., 0.8276, 0.0093, 0.0674],
        [0.5124, 0.1265, 0.8184,  ..., 0.4340, 0.4333, 0.1293],
        ...,
        [0.1073, 0.1239, 0.9396,  ..., 0.4503, 0.4583, 0.8074],
        [0.3336, 0.2680, 0.1699,  ..., 0.6623, 0.9152, 0.9129],
        [0.2971, 0.5474, 0.4108,  ..., 0.3925, 0.8302, 0.7814]]], color_space=ColorSpace.RGB)
```