Repository navigation
Start creating $async functions as part of public ABI generation. - #2411
Conversation
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).
|
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 |
benvanik
left a comment
There was a problem hiding this comment.
(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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
Added hal.check_success with a lowering to vm.cond_fail.
|
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 |
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 :) |
(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 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 The pieces that I think I'm missing are:
|
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.
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)
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 ( |
* 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
Progress on exposing semaphores for bindings to use, per discussions relating to #1285.
At the moment these
$asyncfunctions just wrap the$syncfunctions and include wait/signal semaphore arguments. A future change can go through and invert this ($syncwrap$asyncand wait/signal local semaphores).IR looks roughly like:
Here is a more complete snippet: async_dynamic_add.mlir.
Specifically looking for feedback on:
iree_hal_semaphore_create, then Python bindings etc.SemaphoreListsupporting 0+ or 1+ pairs of[semaphore, value]?