MCPcopy Create free account
hub / github.com/tinygrad/tinygrad / patch

Function patch

extra/usbgpu/patch.py:13–36  ·  view source on GitHub ↗
(input_filepath, file_hash, patches)

Source from the content-addressed store, hash-verified

11if getenv("USBDEV", ""): SUPPORTED_CONTROLLERS.insert(0, (int(x, 16) for x in getenv("USBDEV", "").split(":")))
12
13def patch(input_filepath, file_hash, patches):
14 with open(input_filepath, 'rb') as infile: data = bytearray(infile.read())
15
16 if_hash = hashlib.md5(data).hexdigest()
17 if if_hash != file_hash:
18 raise ValueError(f"File hash mismatch: expected {file_hash}, got {if_hash}")
19
20 for offset, expected_bytes, new_bytes in patches:
21 if len(expected_bytes) != len(new_bytes):
22 raise ValueError("Expected bytes and new bytes must be the same length")
23
24 if offset + len(new_bytes) > len(data): return False
25 current_bytes = data[offset:offset + len(expected_bytes)]
26 assert bytes(current_bytes) == expected_bytes, f"Expected {expected_bytes} at offset {offset:x}, but got {current_bytes}"
27 data[offset:offset + len(new_bytes)] = new_bytes
28
29 checksum = sum(data[4:-6]) & 0xff
30 crc32 = zlib.crc32(data[4:-6]).to_bytes(4, 'little')
31 data[-5] = checksum
32 data[-4] = crc32[0]
33 data[-3] = crc32[1]
34 data[-2] = crc32[2]
35 data[-1] = crc32[3]
36 return data
37
38path = os.path.dirname(os.path.abspath(__file__))
39file_hash = "5284e618d96ef804c06f47f3b73656b7"

Callers 5

patch.pyFile · 0.70
test_unit_scale_exactMethod · 0.50
test_si_boundaryMethod · 0.50

Calls 2

readMethod · 0.45
to_bytesMethod · 0.45

Tested by 4

test_unit_scale_exactMethod · 0.40
test_si_boundaryMethod · 0.40

Used in the wild real call sites across dependent graphs

searching dependent graphs…