MCPcopy Create free account
hub / github.com/NVIDIA/DALI / format_batch

Function format_batch

dali/python/nvidia/dali/_tensor_formatting.py:288–389  ·  view source on GitHub ↗

Format a tensorlist/batch for display. Parameters ---------- obj : Any The tensorlist/batch object to format. show_data : bool, optional Whether to include the actual data values, by default True. indent : str, optional Optional indentation prefix for the

(
    obj, show_data: bool = True, indent: str = "", adapter: Optional[BatchAdapter] = None
)

Source from the content-addressed store, hash-verified

286
287
288def format_batch(
289 obj, show_data: bool = True, indent: str = "", adapter: Optional[BatchAdapter] = None
290) -> str:
291 """Format a tensorlist/batch for display.
292
293 Parameters
294 ----------
295 obj : Any
296 The tensorlist/batch object to format.
297 show_data : bool, optional
298 Whether to include the actual data values, by default True.
299 indent : str, optional
300 Optional indentation prefix for the output, by default "".
301 adapter : BatchAdapter, optional
302 Adapter for accessing batch properties. If None, uses PipelineBatchAdapter.
303
304 Returns
305 -------
306 str
307 Formatted string representation of the tensorlist/batch.
308 """
309
310 if adapter is None:
311 adapter = PipelineBatchAdapter()
312
313 spaces_indent = indent + " " * 4
314 edgeitems = 2
315 edgeitem_samples = 2
316 type_name = adapter.get_type_name(obj)
317 layout = adapter.get_layout(obj)
318 device = adapter.get_device(obj).lower()
319
320 if show_data:
321 data = adapter.to_cpu(obj)
322 data_str = "[]"
323 else:
324 data = None
325 data_str = ""
326
327 crop = False
328
329 if data:
330 if adapter.get_length(data) == 0:
331 data_str = "[]"
332 else:
333 # First check if we need to crop based on shapes
334 shapes = adapter.get_shape(data)
335 num_samples = len(shapes)
336
337 # Compute total elements from shapes to decide if we need summarization
338 # (empty tensor is treated as 1 element).
339 total_elements = sum(max(np.prod(shape, dtype=int), 1) for shape in shapes)
340 crop = num_samples > 2 * edgeitem_samples + 1 and total_elements > 1000
341
342 # Let adapter handle the cropping efficiently
343 data_arrays = adapter.to_numpy(data, edgeitems=edgeitem_samples if crop else None)
344
345 # Separator between samples in batch.

Callers 1

_tensorlist_to_stringFunction · 0.90

Calls 12

get_lengthMethod · 0.95
to_numpyMethod · 0.95
sumClass · 0.85
_join_stringFunction · 0.85
maxFunction · 0.70
get_type_nameMethod · 0.45
get_layoutMethod · 0.45
get_deviceMethod · 0.45
to_cpuMethod · 0.45
get_shapeMethod · 0.45
get_dtypeMethod · 0.45

Tested by

no test coverage detected