Convert to user tensor (defined in `backend`).
(self, ctx=None)
| 125 | return self._pydata |
| 126 | |
| 127 | def tousertensor(self, ctx=None): |
| 128 | """Convert to user tensor (defined in `backend`).""" |
| 129 | if ctx is None: |
| 130 | ctx = F.cpu() |
| 131 | if len(self._user_tensor_data) == 0: |
| 132 | if self._dgl_tensor_data is not None: |
| 133 | # zero copy from dgl tensor |
| 134 | dlpack = self._dgl_tensor_data.to_dlpack() |
| 135 | self._user_tensor_data[F.cpu()] = F.zerocopy_from_dlpack(dlpack) |
| 136 | else: |
| 137 | # zero copy from numpy array |
| 138 | self._user_tensor_data[F.cpu()] = F.zerocopy_from_numpy( |
| 139 | self.tonumpy() |
| 140 | ) |
| 141 | if ctx not in self._user_tensor_data: |
| 142 | # copy from cpu to another device |
| 143 | data = next(iter(self._user_tensor_data.values())) |
| 144 | self._user_tensor_data[ctx] = F.copy_to(data, ctx) |
| 145 | return self._user_tensor_data[ctx] |
| 146 | |
| 147 | def todgltensor(self): |
| 148 | """Convert to dgl.NDArray.""" |