-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Open
Description
🚀 The feature
Problem
The wrap() function currently only supports the built-in TVTensor types from torchvision.
If I make a custom TVTensor, I cannot use wrap() to restore metadata in the same way.
Proposal
Allow users to register custom wrapping logic, for example using functools.singledispatch.
Alternatively, add a method or API like CustomTensor.wrap(tensor, like=obj) so each custom type can define its own wrapping.
Example
class CustomTensor(tv_tensors.TVTensor):
extra_info: str
@classmethod
def _wrap(cls, tensor, *, extra_info):
out = tensor.as_subclass(cls)
out.extra_info = extra_info
return out
# Current wrap does not handle extra_info
custom = CustomTensor(torch.ones(2,2), extra_info="abc")
result = custom + 1
wrapped = tv_tensors.wrap(result, like=custom) # lost metadata
# 1st Proposal (dispatch)
@wrap.register(CustomTensor)
def _(tensor, *, like, **kwargs):
return CustomTensor._wrap(tensor, extra_info=like.extra_info)
# 2nd proposal (wrap method)
def wrap(wrappee, *, like, **kwargs):
return like.wrap(wrapee, like=like) # like.__wrap__(...)Motivation, pitch
- I want to create my own TVTensor types for domain-specific tasks (like depth maps, segmentation, classification, 3d point clouds etc).
- Ideally, I want to use the same wrap/unwrap workflow as built-in TVTensors so transforms and kernels remain easy and clean.
- Supporting custom TVTensors in
wrap()helps keep the code extensible but simple. No breaking changes. - Some users are already requesting functionality that would be possible by this feature (e.g tv_tensors.wrap doesn't work with subclasses of BoundingBoxes or KeyPoints #9328 )
Alternatives
- Manually re-implementing wrapping logic in every custom transform (hard to maintain)
- Monkeypatching
wrap()or patching transforms - Not supporting custom TVTensor use-cases at all
Additional context
This proposal enables advanced users to extend TVTensors for new applications, following existing patterns like register_kernel.
The implementation can be very minimal at first, using the existing Python singledispatch pattern.
If maintainers accept, I'm happy to draft a small PR or demo.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels