Note that `torch.where` may convert y.dtype to x.dtype.
(condition: NdarrayOrTensor, x=None, y=None)
| 137 | |
| 138 | |
| 139 | def where(condition: NdarrayOrTensor, x=None, y=None) -> NdarrayOrTensor: |
| 140 | """ |
| 141 | Note that `torch.where` may convert y.dtype to x.dtype. |
| 142 | """ |
| 143 | result: NdarrayOrTensor |
| 144 | if isinstance(condition, np.ndarray): |
| 145 | if x is not None: |
| 146 | result = np.where(condition, x, y) |
| 147 | else: |
| 148 | result = np.where(condition) # type: ignore |
| 149 | else: |
| 150 | if x is not None: |
| 151 | x = torch.as_tensor(x, device=condition.device) |
| 152 | y = torch.as_tensor(y, device=condition.device, dtype=x.dtype) |
| 153 | result = torch.where(condition, x, y) |
| 154 | else: |
| 155 | result = torch.where(condition) # type: ignore |
| 156 | return result |
| 157 | |
| 158 | |
| 159 | def argwhere(a: NdarrayTensor) -> NdarrayTensor: |
no test coverage detected
searching dependent graphs…