MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / to_string

Function to_string

python/paddle/tensor/to_string.py:255–299  ·  view source on GitHub ↗
(var, prefix='Tensor')

Source from the content-addressed store, hash-verified

253
254
255def to_string(var, prefix='Tensor'):
256 indent = len(prefix) + 1
257
258 dtype = convert_dtype(var.dtype)
259 if var.dtype == paddle.bfloat16:
260 dtype = 'bfloat16'
261
262 _template = "{prefix}(shape={shape}, dtype={dtype}, place={place}, stop_gradient={stop_gradient},\n{indent}{data})"
263
264 tensor = var.value().get_tensor()
265 if not tensor._is_initialized():
266 return "Tensor(Not initialized)"
267
268 if var.dtype == paddle.bfloat16:
269 if not var.place.is_cpu_place():
270 paddle.device.synchronize()
271 var = var.astype('float32')
272 np_var = var.numpy(False)
273
274 if len(var.shape) == 0:
275 size = 0
276 else:
277 size = 1
278 for dim in var.shape:
279 size *= dim
280
281 summary = False
282 if size > DEFAULT_PRINT_OPTIONS.threshold:
283 summary = True
284
285 max_width, signed = _get_max_width(_to_summary(np_var))
286
287 data = _format_tensor(
288 np_var, summary, indent=indent, max_width=max_width, signed=signed
289 )
290
291 return _template.format(
292 prefix=prefix,
293 shape=var.shape,
294 dtype=dtype,
295 place=var._place_str,
296 stop_gradient=var.stop_gradient,
297 indent=' ' * indent,
298 data=data,
299 )
300
301
302def mask_xpu_bf16_tensor(np_tensor):

Callers

nothing calls this directly

Calls 10

convert_dtypeFunction · 0.90
_get_max_widthFunction · 0.85
_to_summaryFunction · 0.85
_format_tensorFunction · 0.85
astypeMethod · 0.80
numpyMethod · 0.80
get_tensorMethod · 0.45
valueMethod · 0.45
synchronizeMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected