MCPcopy Index your code
hub / github.com/tinygrad/tinygrad / hash

Method hash

tinygrad/tensor.py:1167–1182  ·  view source on GitHub ↗

Calculates a 16-byte hash of the tensor. ```python exec="false source="above" session="tensor" result="python" t = Tensor(b"Hello World!").hash() print(t.data().hex()) ```

(self)

Source from the content-addressed store, hash-verified

1165 return self.reshape(-1, 4096).keccak("shake_128").reshape(self.shape[0], -1).keccak("shake_128")
1166
1167 def hash(self) -> Tensor:
1168 """
1169 Calculates a 16-byte hash of the tensor.
1170 ```python exec="false source="above" session="tensor" result="python"
1171 t = Tensor(b"Hello World!").hash()
1172 print(t.data().hex())
1173 ```
1174 """
1175 data = self.flatten().bitcast(dtypes.uint8)
1176 n = data.shape[0]
1177 assert isinstance(n, int), "hash requires concrete shape"
1178 chunks = ceildiv(n, 2**20)
1179 while chunks > 1:
1180 data = data.pad_to(chunks * 2**20).reshape(chunks, 2**20)._hash_1mb().flatten()
1181 chunks = ceildiv(chunks, 65536)
1182 return data.pad_to(2**20).unsqueeze(0)._hash_1mb().flatten()[:16]
1183
1184 # ***** processing ops *****
1185

Callers 1

test_abcMethod · 0.80

Calls 7

ceildivFunction · 0.90
flattenMethod · 0.80
_hash_1mbMethod · 0.80
reshapeMethod · 0.80
pad_toMethod · 0.80
unsqueezeMethod · 0.80
bitcastMethod · 0.45

Tested by 1

test_abcMethod · 0.64