(var, prefix='Tensor')
| 253 | |
| 254 | |
| 255 | def 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 | |
| 302 | def mask_xpu_bf16_tensor(np_tensor): |
nothing calls this directly
no test coverage detected