MCPcopy
hub / github.com/tinygrad/tinygrad / cast

Method cast

tinygrad/tensor.py:1387–1404  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 """

Callers 15

_bufferMethod · 0.95
tolistMethod · 0.95
whereMethod · 0.95
_attentionMethod · 0.95
__call__Method · 0.95
test_getitem_scalarsMethod · 0.95

Calls 2

_apply_uopMethod · 0.95
to_dtypeFunction · 0.90