[inductor][codecache] Print bytes in codecache debug output (#164898)

Summary: We have an internal request to help understand why the hash of `post_grad_custom_post_pass` is changing between attempts. We don't get useful info from the debug output, because we just print "<bytes>". Instead, attempt to print at least _some_ of the value in case it contains readable characters.

Test Plan:
Registered a dummy post_grad_custom_pass and printed codecache debug output
`TORCH_LOGS=+torch._inductor.codecache python ~/foo.py`

Yields something like:
```
V1007 16:41:19.024000 3546009 /data/users/slarsen/pytorch-3.10_4/torch/_inductor/codecache.py:989] [0/0] [law2ujt2wzjb5tyiu6jh64r2lxpvl62yvxcsmdouhg3qyelhhdv] post_grad_custom_post_pass: HelloWorld!����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������...
```

Differential Revision: [D84108770](https://our.internmc.facebook.com/intern/diff/D84108770)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164898
Approved by: https://github.com/oulgen
This commit is contained in:
Sam Larsen
2025-10-07 16:48:16 -07:00
committed by PyTorch MergeBot
parent 086dec3235
commit 608792153f

View File

@ -657,7 +657,8 @@ class FxGraphCachePickler(pickle.Pickler):
if isinstance(obj, torch.Tensor):
return str(extract_tensor_metadata_for_cache_key(obj))
elif isinstance(obj, bytes):
return "<bytes>"
val = obj.decode("utf-8", errors="replace")
return val if len(val) <= 1024 else val[:1024] + "..."
elif type(obj) in self.dispatch_table:
# Run the reducer on the object
return str(self.dispatch_table[type(obj)](obj)[1])