Format a single tensor for display. Parameters ---------- obj : Any The tensor object to format. show_data : bool, optional Whether to include the actual data values, by default True. adapter : TensorAdapter, optional Adapter for accessing tensor properti
(obj, show_data: bool = True, adapter: Optional[TensorAdapter] = None)
| 240 | |
| 241 | |
| 242 | def format_tensor(obj, show_data: bool = True, adapter: Optional[TensorAdapter] = None) -> str: |
| 243 | """Format a single tensor for display. |
| 244 | |
| 245 | Parameters |
| 246 | ---------- |
| 247 | obj : Any |
| 248 | The tensor object to format. |
| 249 | show_data : bool, optional |
| 250 | Whether to include the actual data values, by default True. |
| 251 | adapter : TensorAdapter, optional |
| 252 | Adapter for accessing tensor properties. If None, uses PipelineTensorAdapter. |
| 253 | |
| 254 | Returns |
| 255 | ------- |
| 256 | str |
| 257 | Formatted string representation of the tensor. |
| 258 | """ |
| 259 | |
| 260 | if adapter is None: |
| 261 | adapter = PipelineTensorAdapter() |
| 262 | |
| 263 | indent = " " * 4 |
| 264 | edgeitems = 2 |
| 265 | type_name = adapter.get_type_name(obj) |
| 266 | layout = adapter.get_layout(obj) |
| 267 | device = adapter.get_device(obj).lower() |
| 268 | |
| 269 | if show_data: |
| 270 | data = adapter.to_numpy(obj) |
| 271 | data_str = np.array2string(data, prefix=indent, edgeitems=edgeitems) |
| 272 | else: |
| 273 | data_str = None |
| 274 | |
| 275 | shape = tuple(adapter.get_shape(obj)) |
| 276 | |
| 277 | params = ( |
| 278 | ([f"{data_str}"] if show_data else []) |
| 279 | + [f"dtype={adapter.get_dtype(obj)}"] |
| 280 | + ([f'layout="{layout}"'] if layout else []) |
| 281 | + [f'device="{device}"'] |
| 282 | + [f"shape={shape})"] |
| 283 | ) |
| 284 | |
| 285 | return f"{type_name}(\n{indent}" + _join_string(params, sep=",\n" + indent) |
| 286 | |
| 287 | |
| 288 | def format_batch( |
no test coverage detected