Wraps all torch functions.
(cls, func, types, args=(), kwargs=None)
| 277 | |
| 278 | @classmethod |
| 279 | def __torch_function__(cls, func, types, args=(), kwargs=None) -> Any: |
| 280 | """Wraps all torch functions.""" |
| 281 | if kwargs is None: |
| 282 | kwargs = {} |
| 283 | ret = super().__torch_function__(func, types, args, kwargs) |
| 284 | # if `out` has been used as argument, metadata is not copied, nothing to do. |
| 285 | # if "out" in kwargs: |
| 286 | # return ret |
| 287 | if _not_requiring_metadata(ret): |
| 288 | return ret |
| 289 | if _get_named_tuple_like_type(func) is not None and isinstance(ret, _get_named_tuple_like_type(func)): |
| 290 | # for torch.max(torch.tensor(1.0), dim=0), the return type is named-tuple like |
| 291 | out_items = MetaTensor.update_meta(ret, func, args, kwargs) |
| 292 | for idx in range(ret.n_fields): |
| 293 | ret[idx].meta = out_items[idx].meta |
| 294 | ret[idx].applied_operations = out_items[idx].applied_operations |
| 295 | return ret |
| 296 | # we might have 1 or multiple outputs. Might be MetaTensor, might be something |
| 297 | # else (e.g., `__repr__` returns a string). |
| 298 | # Convert to list (if necessary), process, and at end remove list if one was added. |
| 299 | if not isinstance(ret, Sequence): |
| 300 | ret = [ret] |
| 301 | unpack = True |
| 302 | else: |
| 303 | unpack = False |
| 304 | ret = MetaTensor.update_meta(ret, func, args, kwargs) |
| 305 | return ret[0] if unpack else ret |
| 306 | |
| 307 | @staticmethod |
| 308 | def _convert(x): |
nothing calls this directly
no test coverage detected