Skip to content

Instantly share code, notes, and snippets.

@fs-eire
Created February 3, 2026 18:30
Show Gist options
  • Select an option

  • Save fs-eire/23029f90ba2f3128f01286bbee17d53b to your computer and use it in GitHub Desktop.

Select an option

Save fs-eire/23029f90ba2f3128f01286bbee17d53b to your computer and use it in GitHub Desktop.
Patch onnxruntime-genai to support latest (2026-02-01) onnxruntime
#!/usr/bin/env python3
"""
Patch onnxruntime-genai.dll to replace "WebGPU_Buffer" string with "WebGPU_Buf".
"""
import os
import shutil
import sys
DLL_NAME = "onnxruntime-genai.dll"
BACKUP_NAME = "onnxruntime-genai.dll.bak"
# Original string to search for (ANSI C string)
SEARCH_BYTES = b"WebGPU_Buffer"
# We want to change "WebGPU_Buffer" to "WebGPU_Buf\x00er" (null-terminate after "Buf")
# This means replacing byte at position 10 (the second 'f') with 0x00
REPLACE_BYTES = b"WebGPU_Buf\x00er"
def main():
# Check if DLL exists
if not os.path.isfile(DLL_NAME):
print(f"Error: {DLL_NAME} not found in current directory.", file=sys.stderr)
sys.exit(1)
# Read the DLL content
print(f"Reading {DLL_NAME}...")
with open(DLL_NAME, "rb") as f:
data = f.read()
# Count occurrences
count = data.count(SEARCH_BYTES)
print(f"Found {count} occurrence(s) of '{SEARCH_BYTES.decode('ascii')}'")
if count == 0:
print("Error: String not found in the file.", file=sys.stderr)
sys.exit(1)
if count != 1:
print(f"Error: Expected exactly 1 occurrence, but found {count}.", file=sys.stderr)
sys.exit(1)
# Create backup
print(f"Creating backup: {BACKUP_NAME}...")
shutil.copy2(DLL_NAME, BACKUP_NAME)
# Perform replacement
print("Patching...")
patched_data = data.replace(SEARCH_BYTES, REPLACE_BYTES, 1)
# Verify the replacement
if len(patched_data) != len(data):
print("Error: Patched data size mismatch!", file=sys.stderr)
sys.exit(1)
# Write the patched DLL
print(f"Writing patched {DLL_NAME}...")
with open(DLL_NAME, "wb") as f:
f.write(patched_data)
print("Done! Patch applied successfully.")
print(f"Backup saved as: {BACKUP_NAME}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment