Skip to content

Start creating $async functions as part of public ABI generation. - #2411

Merged
ScottTodd merged 4 commits into
iree-org:mainfrom
ScottTodd:semaphore-abi
Jul 13, 2020
Merged

ScottTodd merged 4 commits into
iree-org:mainfrom
ScottTodd:semaphore-abi

Conversation

@ScottTodd

Copy link
Copy Markdown
Member

Progress on exposing semaphores for bindings to use, per discussions relating to #1285.

At the moment these $async functions just wrap the $sync functions and include wait/signal semaphore arguments. A future change can go through and invert this ($sync wrap $async and wait/signal local semaphores).

IR looks roughly like:

func main$sync(buffer_view_args...) -> hal.buffer_view attributes { ... } {
  // buffer_view handling
 call @main
  // buffer_view handling
}
func main$async(wait_semaphore, wait_value, buffer_view_args..., signal_semaphore, signal_value) -> hal.buffer_view {
  hal.semaphore.await wait_semaphore, wait_value
  call @main$sync
  hal.semaphore.signal signal_semaphore, signal_value
}

Here is a more complete snippet: async_dynamic_add.mlir.

Specifically looking for feedback on:

  1. How to populate attributes and reflection metadata in service of getting some C code calling these functions with iree_hal_semaphore_create, then Python bindings etc.
  2. Argument ergonomics. Do we want some sort of SemaphoreList supporting 0+ or 1+ pairs of [semaphore, value]?

At the moment these just wrap the $sync functions and include
wait/signal semaphore arguments. They expose async behavior but
internally are still synchronous. A future change can go through and
invert this ($sync wrap $async and wait/signal transient semaphores).

Attributes and reflection metadata are missing (I don't understand them
yet).
@ScottTodd ScottTodd added the compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm) label Jul 6, 2020
@benvanik

benvanik commented Jul 6, 2020

Copy link
Copy Markdown
Collaborator

RE multiple waits/signals, vm.list could be used to pass these in (as it can contain variant types so elements could be pairs of semaphore+value): #2369
For now I think a single semaphore is fine, though. The multi-wait will be useful when performing joins (like wait until these N invocations complete before starting the next invocation).

@benvanik benvanik left a comment

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.

(will let stella comment on ABI stuff; rest LGTM)

auto waitValue = entryBlock->getArgument(1);
auto waitOp = builder.create<HAL::SemaphoreAwaitOp>(
loc, builder.getIntegerType(32), waitSemaphore, waitValue);
// TODO(scotttodd): if waitOp's result is not 0 (ok), fail

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.

Good question - vm ops should be ok here but I haven't tested that. If we wanted to avoid that we could create another dialect/op that was later turned into the VM ops (like hal.check_success %status -> cmp+cond_fail). I'm fine with either for now (so long as we have something checking).

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.

Iirc, there may be an overly zealous verifier in play for some VM ops (verifies that they are in a VM module). That verifier (if it still exists) probably just needs to go. We should be able to mix low level VM ops in if needed, imo.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

  func @main$async(%arg0: !hal.semaphore, %arg1: index, %arg2: !hal.buffer_view, %arg3: !hal.buffer_view, %arg4: !hal.semaphore, %arg5: index) -> !hal.buffer_view {
    %0 = hal.semaphore.await %arg0, min_value = %arg1 : i32
    %nz = vm.cmp.nz.i32 %0 : i32
    %c13 = vm.const.i32 13 : i32  // 13 == IREE_STATUS_INTERNAL
    vm.cond_fail %nz, %c13
    %1 = call @main$sync(%arg2, %arg3) : (!hal.buffer_view, !hal.buffer_view) -> !hal.buffer_view
    hal.semaphore.signal %arg4, value = %arg5
    return %1 : !hal.buffer_view
  }

fails with error: 'vm.cond_fail' op expects parent op 'vm.func' . I can take a look at adding hal.check_success instead, or this is an "overly zealous verifier"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added hal.check_success with a lowering to vm.cond_fail.

@stellaraccident

Copy link
Copy Markdown
Collaborator

Re question 1: I think we need to head towards a world where we encode the ref type in the reflection metadata but actually do the type check in the generated ABI function. It has to assert that anyway when dereferencing the refobject, and making that point be the thing that is responsible for gating against illegal types involves the smallest amount of runtime code. In that way, the reflection metadata becomes informative for printing and reporting better errors (i.e. imagine that when we get a status indicating a type error that we call a dedicated function that walks the metadata and generates a nice error message vs the situation now where the metadata is used to gate all inputs). It should be pretty easy to extend the reflection metadata with an Object type that contains a string representation of the specific VM type.

If moving to this world, the python (or other) API code becomes really simple: it maintains a type mapping for a python semaphore type to a hook that knows how to pack/unpack that from a variant list. Then it would just blindly attempt to call the function as issued, relying on the ABI wrapper for any signal of failure.

@benvanik

benvanik commented Jul 6, 2020

Copy link
Copy Markdown
Collaborator

Re question 1: I think we need to head towards a world where we encode the ref type in the reflection metadata but actually do the type check in the generated ABI function. It has to assert that anyway when dereferencing the refobject, and making that point be the thing that is responsible for gating against illegal types involves the smallest amount of runtime code. In that way, the reflection metadata becomes informative for printing and reporting better errors (i.e. imagine that when we get a status indicating a type error that we call a dedicated function that walks the metadata and generates a nice error message vs the situation now where the metadata is used to gate all inputs). It should be pretty easy to extend the reflection metadata with an Object type that contains a string representation of the specific VM type.

If moving to this world, the python (or other) API code becomes really simple: it maintains a type mapping for a python semaphore type to a hook that knows how to pack/unpack that from a variant list. Then it would just blindly attempt to call the function as issued, relying on the ABI wrapper for any signal of failure.

Yeah, having a set of assertions for non-null/type would be pretty straightforward to emit, and in the future if we wanted to support polymorphic types we could easily extend that (so function takes !vm.ref<?> and verifies it's of type A or B, etc). Encoding that in the reflection data gets tricky but encoding it in simple assertions is easy.

@ScottTodd

Copy link
Copy Markdown
Member Author

RE multiple waits/signals, vm.list could be used to pass these in (as it can contain variant types so elements could be pairs of semaphore+value): #2369
For now I think a single semaphore is fine, though. The multi-wait will be useful when performing joins (like wait until these N invocations complete before starting the next invocation).

SGTM. A case that I think will be common is not wanting to wait on any semaphores, but still wanting to signal one semaphore on completion. That will be a little verbose with how this is set up at the moment (construct dummy semaphore, wait on value that it already passed). Passing a list would let you include zero or more semaphores/values, though even constructing an empty list could be verbose...

@benvanik

benvanik commented Jul 7, 2020

Copy link
Copy Markdown
Collaborator

SGTM. A case that I think will be common is not wanting to wait on any semaphores, but still wanting to signal one semaphore on completion. That will be a little verbose with how this is set up at the moment (construct dummy semaphore, wait on value that it already passed). Passing a list would let you include zero or more semaphores/values, though even constructing an empty list could be verbose...

Verbose is fine - this is a low-level API :)
Being able to wait on nothing (or signal nothing) is a valid use-case, so we'd want to support the same even for single-semaphore cases (can be done with vm.cmp.nz.ref to compare for != null).

@ScottTodd

Copy link
Copy Markdown
Member Author

Re question 1: I think we need to head towards a world where we encode the ref type in the reflection metadata but actually do the type check in the generated ABI function. It has to assert that anyway when dereferencing the refobject, and making that point be the thing that is responsible for gating against illegal types involves the smallest amount of runtime code. In that way, the reflection metadata becomes informative for printing and reporting better errors (i.e. imagine that when we get a status indicating a type error that we call a dedicated function that walks the metadata and generates a nice error message vs the situation now where the metadata is used to gate all inputs). It should be pretty easy to extend the reflection metadata with an Object type that contains a string representation of the specific VM type.
If moving to this world, the python (or other) API code becomes really simple: it maintains a type mapping for a python semaphore type to a hook that knows how to pack/unpack that from a variant list. Then it would just blindly attempt to call the function as issued, relying on the ABI wrapper for any signal of failure.

Yeah, having a set of assertions for non-null/type would be pretty straightforward to emit, and in the future if we wanted to support polymorphic types we could easily extend that (so function takes !vm.ref<?> and verifies it's of type A or B, etc). Encoding that in the reflection data gets tricky but encoding it in simple assertions is easy.

(Sorry, wall of text - I'm working on error handling and plan on leaving this other part of the discussion to follow-ups)

Each of these points makes sense to me in principle, though there is a gap between my conceptual and practical understanding. I think it'll take me some time fiddling with compiler code and looking at how the bindings are set up before I can wrap my head around DictionaryAttr reflection, RawSignatureParser, and the other pieces of code involved here.

Beyond the basic code familiarity / learning curve questions, I was hoping for a way to push this generated function down to the C API to call as I set up the synchronous code in the Vulkan sample, which would help me anchor this in my existing understanding of the low level interfaces: https://github.com/google/iree/blob/b1b71f43201ce7712cbe8b96e6de9558c45966ec/iree/samples/vulkan/vulkan_inference_gui.cc#L607-L613

https://github.com/google/iree/blob/b1b71f43201ce7712cbe8b96e6de9558c45966ec/iree/samples/vulkan/vulkan_inference_gui.cc#L686-L764

The pieces that I think I'm missing are:

  • Are the $raw / $sync / $async functions accessible to iree_vm_context_resolve_function? Or are they all aliases that the VM understands how to defer to based on the inputs it is given?
  • How is RawSignatureParser used here today? Maybe stepping through with a debugger will help? I'm wondering if I just need to call it differently within this file, or if there are other places to update with the modified argument list. Is there a way to sanity check attributes like iree.reflection = {f = "I19!B7!t7d4d4B7!t7d5d6R10!B7!t7d5d6", perhaps by just reading and comparing with the code that does the mangling/parsing?
  • Does the semaphore type need new refobject dereferencing code as you mention? I think I'm missing a top-down view of how data is expected to flow through this system (e.g. python -> buffer + metadata -> IREE VM magic -> IREE HAL magic -> kernels?) to really understand what is / isn't supported.

@benvanik

benvanik commented Jul 7, 2020

Copy link
Copy Markdown
Collaborator
  • Are the $raw / $sync / $async functions accessible to iree_vm_context_resolve_function? Or are they all aliases that the VM understands how to defer to based on the inputs it is given?

The functions aren't special to the VM so they resolve just like any other - it's up to whoever is calling into the VM to pick the right one.

  • How is RawSignatureParser used here today? Maybe stepping through with a debugger will help? I'm wondering if I just need to call it differently within this file, or if there are other places to update with the modified argument list. Is there a way to sanity check attributes like iree.reflection = {f = "I19!B7!t7d4d4B7!t7d5d6R10!B7!t7d5d6", perhaps by just reading and comparing with the code that does the mangling/parsing?

Not used by the VM but the python/etc bindings to know how to marshal types (from python arg0 to the VM function arg0, etc)

  • Does the semaphore type need new refobject dereferencing code as you mention? I think I'm missing a top-down view of how data is expected to flow through this system (e.g. python -> buffer + metadata -> IREE VM magic -> IREE HAL magic -> kernels?) to really understand what is / isn't supported.

Data flow is code flow; if this were JNI think of the reflection data as the java-side native declaration and the $raw/etc as the C-side. The JNI machinery itself (our VM) doesn't know what a particular argument is, but the java and C side need to agree that a java long becomes a C jlong, etc. The reflection data above is to allow python to map its types to what the VM expects (a python number to a VM number).

Think of the python side exposing a module.foo_sync() and module.foo_async(), and if you call those it would resolve and call foo$sync and foo$async. foo_async would be expected to take semaphores just like the VM function expects. If we made the semaphores a list, then the python function should take a list (foo_async(wait=[sema1, sema1_value], ...., signal=[sema1, sema1_next_value])).

@ScottTodd
ScottTodd requested a review from benvanik July 8, 2020 00:08
@ScottTodd
ScottTodd merged commit 8096658 into iree-org:main Jul 13, 2020
@ScottTodd
ScottTodd deleted the semaphore-abi branch July 13, 2020 18:39
@ScottTodd ScottTodd mentioned this pull request Jul 14, 2020
copybara-service Bot pushed a commit that referenced this pull request Jul 14, 2020
* f4f2fec Update Bazel version in docs workflow (#2511)
* 19e6574 Add lint action for PR title length (#2481)
* 57a9ae3 Moved CompiledModule and BackendInfo to `tf_utils.py` (#2509)
* 7d50357 Update Bazel to 3.3.1 (#2496)
* afa7d8e [ModelBuilder] Fix leak when using llvm::createLowerMatrixIntrinsicsPass (#2499)
* 90ffb71 [ci] Enable build tests for Android cross compilation (#2507)
* 62490cb Add Bazel 3.3.1 to docker images (#2497)
* 028a883 [ci] Run cmake --build twice to temporary fix the flaky build (#2501)
* 8096658 Start creating $async functions as part of public ABI generation. (#2411)
* 27a1f3d Removed gather lowering test (#2503)
* 02cc1e3 Merge google -> main (#2505)
* 6706f3d Add GitHub action to create PRs merging google -> main (#2482)
* 645a88b Add buildifier lint action (#2243)
* fc812fe Support iree-run-mlir to read input values from a file. (#2352)
* 8078fc6 Use iree_py_binary in keras test binaries (#2488)
* 9f22404 Treat warnings as errors and silence them in third party code (#2489)
* cb000c7 Avoid unused result warning (#2469)
* b06a482 Fix some minor typos (#2486)
* ce6c171 Create cooperative matrix performance test (#2443)
* 0880ee9 Add android build badge to README (#2473)
* 8e14d1f [vulkan] Guard against 0-byte buffer allocation (#2467)
* ef019df Improve TF submodule bump action descriptions (#2471)
* c53ad00 Refactors tf_test_utils, creating tf_utils and simplifying module specificatio..
* a90df48 Merge google -> main (#2468)

PiperOrigin-RevId: 321174064
@copybara-service copybara-service Bot mentioned this pull request Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants