-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Python API] Fix python api for bytecode (#17343)
This fixes an issue seen in #17278. MLIR bytecode can have a zero as the last byte depending on the IR. If this happens by accident parsing will fail. This PR checks if a buffer begins with the _magic number_ from https://mlir.llvm.org/docs/BytecodeFormat/#magic-number and sets zero-termination to false if it does. However I could not generate MLIR bytecode that ends with a zero other than the one in the PR. Even if I save the IR used in textual format and parse it again, some additional bytes are appended and it is not zero terminated anymore. I think this is because of some behavior in TF. Still after reading a bit through https://mlir.llvm.org/docs/BytecodeFormat I think the check implemented in this PR is the correct behavior for bytecode. --------- Signed-off-by: Maximilian Bartel <[email protected]>
- Loading branch information
Showing
6 changed files
with
140 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file added
BIN
+507 Bytes
compiler/bindings/python/test/api/testdata/bytecode_zero_terminated_testfile.bc
Binary file not shown.
47 changes: 47 additions & 0 deletions
47
compiler/bindings/python/test/api/testdata/generate_mlir_bytecode.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2024 The IREE Authors | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
from iree.compiler.api import ( | ||
Session, | ||
Source, | ||
Output, | ||
) | ||
import os | ||
import iree.compiler.tools.tflite | ||
|
||
|
||
def generate_test_bytecode(): | ||
session = Session() | ||
inv = session.invocation() | ||
source = Source.wrap_buffer(session, b"builtin.module {}") | ||
inv.parse_source(source) | ||
out = Output.open_membuffer() | ||
inv.output_ir_bytecode(out) | ||
mem = out.map_memory() | ||
|
||
this_dir = os.path.dirname(__file__) | ||
with open(os.path.join(this_dir, "bytecode_testfile.bc"), "wb") as file: | ||
file.write(bytes(mem)) | ||
|
||
|
||
def generate_zero_terminated_bytecode(): | ||
"""MLIR Bytecode can also be zero terminated. I couldn't find a way to generate zero terminated | ||
bytecode apart from this. Printing as textual IR and then reparsing and printing as bytecode | ||
removes the zero termination on this IR. This might very well be an odity of TF.""" | ||
if not iree.compiler.tools.tflite.is_available(): | ||
return | ||
this_dir = os.path.dirname(__file__) | ||
path = os.path.join(this_dir, "..", "..", "tools", "testdata", "tflite_sample.fb") | ||
bytecode = iree.compiler.tools.tflite.compile_file(path, import_only=True) | ||
with open( | ||
os.path.join(this_dir, "bytecode_zero_terminated_testfile.bc"), "wb" | ||
) as file: | ||
file.write(bytecode) | ||
|
||
|
||
if __name__ == "__main__": | ||
generate_test_bytecode() | ||
generate_zero_terminated_bytecode() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters