Creates a DLpack `capsule https://data-apis.org/array-api/latest/design_topics/data_interchange.html#data-interchange`_ of the current tensor to be exported to other libraries. This function will be called from the `from_dlpack` method of the library that will consu
(self, stream=None)
| 1424 | __torch_dispatch__ = _C._disabled_torch_dispatch_impl |
| 1425 | |
| 1426 | def __dlpack__(self, stream=None): |
| 1427 | """ |
| 1428 | Creates a DLpack `capsule https://data-apis.org/array-api/latest/design_topics/data_interchange.html#data-interchange`_ |
| 1429 | of the current tensor to be exported to other libraries. |
| 1430 | |
| 1431 | This function will be called from the `from_dlpack` method |
| 1432 | of the library that will consume the capsule. `from_dlpack` passes the current |
| 1433 | stream to this method as part of the specification. |
| 1434 | |
| 1435 | Args: |
| 1436 | stream (integer or None): An optional Python integer representing a |
| 1437 | pointer to a CUDA stream. The current stream is synchronized with |
| 1438 | this stream before the capsule is created, and since the capsule |
| 1439 | shares its storage with the tensor this make it safe to access from |
| 1440 | both streams. If None or -1 is passed then no synchronization is performed. |
| 1441 | If 1 (on CUDA) or 0 (on ROCM) then the default stream is used for |
| 1442 | synchronization. |
| 1443 | """ |
| 1444 | if has_torch_function_unary(self): |
| 1445 | return handle_torch_function(Tensor.__dlpack__, (self,), self, stream) |
| 1446 | |
| 1447 | # DLPack capsules can't capture all of PyTorch's semantics, |
| 1448 | # so we prohibit exporting tensors that would lose their properties like |
| 1449 | # requires_grad and having the conjugate bit set. |
| 1450 | if self.requires_grad: |
| 1451 | raise RuntimeError( |
| 1452 | "Can't export tensors that require gradient, use tensor.detach()" |
| 1453 | ) |
| 1454 | if self.is_conj(): |
| 1455 | raise RuntimeError("Can't export tensors with the conjugate bit set") |
| 1456 | if self.layout != torch.strided: |
| 1457 | raise RuntimeError( |
| 1458 | "Can't export tensors with layout other than torch.strided" |
| 1459 | ) |
| 1460 | |
| 1461 | if stream is not None and type(stream) is not int: |
| 1462 | # Stream pointers in CUDA/ROCm are uniquely numbered and can |
| 1463 | # be retrieved from their integer value. |
| 1464 | raise TypeError("stream must be ``int`` or ``none``") |
| 1465 | elif stream is not None and stream != -1: |
| 1466 | if self.device.type == "cuda": |
| 1467 | # NB: This logic handles the special case values for default |
| 1468 | # streams and must be kept in sync with from_dlpack in |
| 1469 | # torch/utils/dlpack.py |
| 1470 | if stream == 1 and torch.version.hip is None: |
| 1471 | stream = torch.cuda.default_stream() |
| 1472 | elif stream == 0 and torch.version.hip is not None: |
| 1473 | stream = torch.cuda.default_stream() |
| 1474 | else: |
| 1475 | stream = torch.cuda.ExternalStream(stream) |
| 1476 | # Only synchronize on different streams |
| 1477 | sync_stream = torch.cuda.current_stream() |
| 1478 | if stream != sync_stream: |
| 1479 | event = torch.cuda.Event() |
| 1480 | event.record(sync_stream) |
| 1481 | stream.wait_event(event) |
| 1482 | return torch.to_dlpack(self) |
| 1483 |
nothing calls this directly
no test coverage detected