Skip to content

Add convenience functions to runtime bindings and disambiguate names - #5697

Merged
phoenix-meadowlark merged 6 commits into
iree-org:mainfrom
phoenix-meadowlark:i-can-still-use-git
May 4, 2021
Merged

phoenix-meadowlark merged 6 commits into
iree-org:mainfrom
phoenix-meadowlark:i-can-still-use-git

Conversation

@phoenix-meadowlark

@phoenix-meadowlark phoenix-meadowlark commented Apr 30, 2021 •

Copy link
Copy Markdown
Contributor

Add iree.runtime.load_vm_flatbuffer and iree.runtime.load_vm_flatbuffer_file to make it easier to bind binaries produced by iree.compiler. e.g.:

vm_flatbuffer = iree.compiler.tf.compile_module(tf_module, target_backends=["dylib-llvm-aot"])
iree_module = iree.runtime.load_vm_flatbuffer(vm_flatbuffer, backend="dylib-llvm-aot")

Also disambiguates module to vm_module in system_api.py where appropriate to reduce (my) confusion.

@phoenix-meadowlark phoenix-meadowlark added the bindings/python Python wrapping IREE's C API label Apr 30, 2021
@google-cla google-cla Bot added the cla: yes label Apr 30, 2021
@phoenix-meadowlark

Copy link
Copy Markdown
Contributor Author

Pytype added more advanced Tuple length checking in some version update since this file was last updated, so I just changed it to Tuple[type, ...] because checking the explicit length doesn't seem worth it here.

vm_instance: _binding.VmInstance
host_type_factory: _binding.HostTypeFactory
default_modules: Tuple[AnyModule]
default_vm_modules: Tuple[_binding.VmModule, ...]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks - I just ran into it and changed it to Sequence. But your way is better.

Comment on lines +54 to +58
# Mapping from IREE target backends to their corresponding drivers.
TARGET_BACKEND_TO_DRIVER = {
"vmla": "vmla",
"dylib-llvm-aot": "dylib",
"vulkan-*": "vulkan",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the runtime component need such a mapping? The runtime should just operate on HAL drivers. Any compiler target could be used to produce compatible executables.

It's perfectly reasonable to write a new compiler target that produces executables for the dylib driver, and indirecting through a mapping like this in Python would make using that awkward (you'd have to piggyback off the existing "dylib-llvm-aot" target backend mapping).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really just intended to be a temporary convenience function for people coming from python to be able to execute the model that they compile easily. A better solution here would be to detect compatible drivers for the provided VmModule, and default to one when the user doesn't specify any, or something similar to that.

In the meantime, I don't think we should require a ML dev who just wants to see if something runs to understand the relationship between target backends and drivers, and have a mental map of which target backends are compatible with which drivers. The names are similar, but I don't think it's obvious that you should specify dylib for dylib-llvm-aot or vulkan for vulkan-* on the face of it.

I could change the API of these two functions to allow for specifying both? e.g.:

def load_vm_flatbuffer*(artifact, *, driver: Optional[str]=None, backend: Optional[str]=None):
  if driver is None and backend is None:
    raise ValueError("Either 'driver' or 'backend' must be specified, but got 'None' for both.")
  if backend is not None and driver is not None:
    raise ValueError("Cannot specify both 'driver' and a 'backend' to infer the driver from.")
  if backend is not None:
    driver = TARGET_BACKEND_TO_DRIVER[backend]
  ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, compiler targets and runtime HAL drivers are low level details that higher level abstractions can be built upon. Expecting users at the higher levels of the stack to work with them is asking a bit much.

I'm concerned about baking too many specific details into the API that are not accurate. There can be a many-to-many relationship between compiler targets and HAL drivers.

Designing towards a generic interface where the high level system just figures out the details on its own would be nice. Right now the Python bindings are pretty thinly layered on top of the C APIs, so these sorts of details keep showing up. I'd rather the bindings commit to either being low level or high level, rather than act low level in some areas then add helpers for certain types of tasks in others.


I'm not sure that specifically helps much here though. For now, what you suggest with accepting either a backend or a driver and trying to infer one from the other seems good enough. That would let users who know what they are doing have explicit control, while others could just lean on the defaults.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be useful to have a thin frontend-agnostic compiler+runtime API that contains the utilities a high-level user would want (e.g. 'cute' names for target triple + driver configurations, named MLIR modules, ...)? That would allow iree.runtime be a low level API intended to be used by high-level users indirectly via frontend specific compiler+runtime APIs like iree.jax while also reducing code duplication between frontend specific APIs.

Comment on lines +356 to +359
def load_vm_flatbuffer(vm_flatbuffer: bytes, backend: str) -> BoundModule:
"""Loads a VM Flatbuffer into a callable module."""
vm_module = _binding.VmModule.from_flatbuffer(vm_flatbuffer)
config = Config(TARGET_BACKEND_TO_DRIVER[backend])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuing from my comment above - this should just take driver: str without the extra lookup.

@phoenix-meadowlark
phoenix-meadowlark merged commit 0b3289a into iree-org:main May 4, 2021
@ScottTodd ScottTodd mentioned this pull request May 4, 2021
not-jenni added a commit that referenced this pull request May 19, 2021
add_module was renamed to add_vm_module in #5697, so this PR updates the usage in colab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bindings/python Python wrapping IREE's C API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants