Casts `self` to the given `dtype`. ```python exec="true" source="above" session="tensor" result="python" t = Tensor([-1, 2.5, 3], dtype=dtypes.float) print(t.dtype, t.numpy()) ``` ```python exec="true" source="above" session="tensor" result="python" t = t.cast(dtypes.in
(self, dtype:DTypeLike)
| 1385 | # ***** cast ops ***** |
| 1386 | |
| 1387 | def cast(self, dtype:DTypeLike) -> Tensor: |
| 1388 | """ |
| 1389 | Casts `self` to the given `dtype`. |
| 1390 | |
| 1391 | ```python exec="true" source="above" session="tensor" result="python" |
| 1392 | t = Tensor([-1, 2.5, 3], dtype=dtypes.float) |
| 1393 | print(t.dtype, t.numpy()) |
| 1394 | ``` |
| 1395 | ```python exec="true" source="above" session="tensor" result="python" |
| 1396 | t = t.cast(dtypes.int32) |
| 1397 | print(t.dtype, t.numpy()) |
| 1398 | ``` |
| 1399 | ```python exec="true" source="above" session="tensor" result="python" |
| 1400 | t = t.cast(dtypes.uint8) |
| 1401 | print(t.dtype, t.numpy()) |
| 1402 | ``` |
| 1403 | """ |
| 1404 | return self if self.dtype == (dt:=to_dtype(dtype)) else self._apply_uop(UOp.cast, dtype=dt) |
| 1405 | |
| 1406 | def bitcast(self, dtype:DTypeLike) -> Tensor: |
| 1407 | """ |