Skip to content

Commit

Permalink
Fix samples after driver refactoring. (#9391)
Browse files Browse the repository at this point in the history
Fixing this failed build from last night: https://buildkite.com/iree/iree-samples/builds/371. Here's a build on this PR: https://buildkite.com/iree/iree-samples/builds/372 (fixed one more test after triggering that)

* Fix CMake target names for web samples
* Enable HAL drivers / executable loaders for web samples
* Update driver names in dynamic_shapes and variables_and_state samples (`dylib`, `vmvx` -> `local-task`), in both scripts and Colab notebooks
* Add `.to_host()` to Colab notebooks that print results
  • Loading branch information
ScottTodd authored Jun 8, 2022
1 parent 718e04e commit 74d741e
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 134 deletions.
2 changes: 1 addition & 1 deletion experimental/web/sample_dynamic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ target_compile_options(${_NAME} PRIVATE ${IREE_DEFAULT_COPTS})
target_link_libraries(${_NAME}
iree_runtime_runtime
iree_hal_local_loaders_system_library_loader
iree_hal_local_sync_driver
iree_hal_drivers_local_sync_sync_driver
)

target_link_options(${_NAME} PRIVATE
Expand Down
1 change: 1 addition & 0 deletions experimental/web/sample_dynamic/build_sample.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ emcmake "${CMAKE_BIN?}" -G Ninja .. \
-DIREE_HOST_BINARY_ROOT=${INSTALL_ROOT} \
-DIREE_BUILD_EXPERIMENTAL_WEB_SAMPLES=ON \
-DIREE_HAL_DRIVER_DEFAULTS=OFF \
-DIREE_HAL_DRIVER_LOCAL_SYNC=ON \
-DIREE_BUILD_COMPILER=OFF \
-DIREE_BUILD_TESTS=OFF

Expand Down
4 changes: 2 additions & 2 deletions experimental/web/sample_static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ target_link_libraries(${_NAME}
${_MNIST_OBJECT_NAME}
iree_runtime_runtime
iree_hal_local_loaders_static_library_loader
iree_hal_local_sync_driver
iree_hal_drivers_local_sync_sync_driver
)

target_link_options(${_NAME} PRIVATE
Expand Down Expand Up @@ -90,7 +90,7 @@ target_link_libraries(${_NAME}
${_MNIST_OBJECT_NAME}
iree_runtime_runtime
iree_hal_local_loaders_static_library_loader
iree_hal_local_task_driver
iree_hal_drivers_local_task_task_driver
iree_task_api
)

Expand Down
2 changes: 2 additions & 0 deletions experimental/web/sample_static/build_sample.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ emcmake "${CMAKE_BIN?}" -G Ninja .. \
-DIREE_HOST_BINARY_ROOT=${INSTALL_ROOT} \
-DIREE_BUILD_EXPERIMENTAL_WEB_SAMPLES=ON \
-DIREE_HAL_DRIVER_DEFAULTS=OFF \
-DIREE_HAL_DRIVER_LOCAL_SYNC=ON \
-DIREE_HAL_DRIVER_LOCAL_TASK=ON \
-DIREE_BUILD_COMPILER=OFF \
-DIREE_BUILD_TESTS=OFF

Expand Down
24 changes: 13 additions & 11 deletions samples/colab/low_level_invoke_function.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "b3810038-9b88-48b8-d3dc-1d8c37300ac8"
"outputId": "ec9dbc63-c510-41d8-98fb-54a2151b4815"
},
"source": [
"!python -m pip install iree-compiler iree-runtime -f https://github.com/google/iree/releases"
Expand All @@ -74,17 +74,18 @@
"output_type": "stream",
"name": "stdout",
"text": [
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Looking in links: https://github.com/google/iree/releases\n",
"Collecting iree-compiler\n",
" Downloading https://github.com/google/iree/releases/download/candidate-20220514.138/iree_compiler-20220514.138-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.0 MB)\n",
"\u001b[K |████████████████████████████████| 49.0 MB 8.9 kB/s \n",
" Downloading https://github.com/google/iree/releases/download/candidate-20220608.163/iree_compiler-20220608.163-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.5 MB)\n",
"\u001b[K |████████████████████████████████| 49.5 MB 151 kB/s \n",
"\u001b[?25hCollecting iree-runtime\n",
" Downloading https://github.com/google/iree/releases/download/candidate-20220514.138/iree_runtime-20220514.138-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB)\n",
"\u001b[K |████████████████████████████████| 2.2 MB 40.2 MB/s \n",
"\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from iree-compiler) (1.21.6)\n",
"Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from iree-compiler) (3.13)\n",
" Downloading https://github.com/google/iree/releases/download/candidate-20220608.163/iree_runtime-20220608.163-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB)\n",
"\u001b[K |████████████████████████████████| 2.1 MB 53.6 MB/s \n",
"\u001b[?25hRequirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from iree-compiler) (3.13)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from iree-compiler) (1.21.6)\n",
"Installing collected packages: iree-runtime, iree-compiler\n",
"Successfully installed iree-compiler-20220514.138 iree-runtime-20220514.138\n"
"Successfully installed iree-compiler-20220608.163 iree-runtime-20220608.163\n"
]
}
]
Expand Down Expand Up @@ -119,6 +120,7 @@
" }\n",
"\"\"\"\n",
"\n",
"# Compile using the vmvx (reference) target:\n",
"compiled_flatbuffer = compile_str(SIMPLE_MUL_ASM, target_backends=[\"vmvx\"])\n",
"vm_module = ireert.VmModule.from_flatbuffer(compiled_flatbuffer)"
],
Expand All @@ -132,12 +134,12 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "933b2530-b3f9-4dbf-bdfd-8d222843e2cf"
"outputId": "25552fbd-cc24-4365-8cac-d166750a0b56"
},
"source": [
"# Register the module with a runtime context.\n",
"# Use the CPU interpreter (which has the most implementation done):\n",
"config = ireert.Config(\"vmvx\")\n",
"# Use the \"local-task\" CPU driver, which can load the vmvx executable:\n",
"config = ireert.Config(\"local-task\")\n",
"ctx = ireert.SystemContext(config=config)\n",
"ctx.add_vm_module(vm_module)\n",
"\n",
Expand Down
7 changes: 3 additions & 4 deletions samples/dynamic_shapes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ them.
```
../iree-build/tools/iree-compile \
--iree-mlir-to-vm-bytecode-module \
--iree-hal-target-backends=dylib-llvm-aot \
--iree-hal-target-backends=cpu \
--iree-input-type=mhlo \
dynamic_shapes.mlir -o dynamic_shapes_dylib.vmfb
dynamic_shapes.mlir -o dynamic_shapes_cpu.vmfb
```
4. Build the `iree_samples_dynamic_shapes` CMake target
Expand All @@ -98,5 +97,5 @@ them.
```
../iree-build/samples/dynamic_shapes/dynamic-shapes \
/path/to/dynamic_shapes_dylib.vmfb dylib
/path/to/dynamic_shapes_cpu.vmfb local-task
```
113 changes: 64 additions & 49 deletions samples/dynamic_shapes/dynamic_shapes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "bfab242f-9bb2-44bd-de62-24fd6df58be4"
"outputId": "8926d9c6-8236-4757-c465-4bedf4e06432"
},
"source": [
"#@title General setup\n",
Expand Down Expand Up @@ -153,7 +153,7 @@
"base_uri": "https://localhost:8080/"
},
"id": "3nSXZiZ_X8-P",
"outputId": "f193b051-1935-4fcc-ba4a-8a5d7a2f3c51"
"outputId": "5f26e6c7-b296-42ad-fd87-f92dba13ac62"
},
"source": [
"#@title Import the TensorFlow program into IREE as MLIR\n",
Expand Down Expand Up @@ -184,44 +184,44 @@
"text": [
"Dynamic Shapes MLIR:\n",
"```\n",
"\"builtin.module\"() ( {\n",
" \"builtin.func\"() ( {\n",
" ^bb0(%arg0: !iree_input.buffer_view): // no predecessors\n",
"\"builtin.module\"() ({\n",
" \"func.func\"() ({\n",
" ^bb0(%arg0: !iree_input.buffer_view):\n",
" %0 = \"iree_input.cast.buffer_view_to_tensor\"(%arg0) : (!iree_input.buffer_view) -> tensor<?xi32>\n",
" %1 = \"std.call\"(%0) {callee = @__inference_add_one_70} : (tensor<?xi32>) -> tensor<?xi32>\n",
" %1 = \"func.call\"(%0) {callee = @__inference_add_one_70} : (tensor<?xi32>) -> tensor<?xi32>\n",
" %2 = \"iree_input.cast.tensor_to_buffer_view\"(%1) : (tensor<?xi32>) -> !iree_input.buffer_view\n",
" \"std.return\"(%2) : (!iree_input.buffer_view) -> ()\n",
" }) {iree.abi = \"{\\22a\\22:[[\\22ndarray\\22,\\22i32\\22,1,null]],\\22r\\22:[[\\22ndarray\\22,\\22i32\\22,1,null]],\\22v\\22:1}\", sym_name = \"add_one\", type = (!iree_input.buffer_view) -> !iree_input.buffer_view} : () -> ()\n",
" \"builtin.func\"() ( {\n",
" ^bb0(%arg0: tensor<?xi32>): // no predecessors\n",
" \"func.return\"(%2) : (!iree_input.buffer_view) -> ()\n",
" }) {function_type = (!iree_input.buffer_view) -> !iree_input.buffer_view, iree.abi = \"{\\22a\\22:[[\\22ndarray\\22,\\22i32\\22,1,null]],\\22r\\22:[[\\22ndarray\\22,\\22i32\\22,1,null]],\\22v\\22:1}\", sym_name = \"add_one\"} : () -> ()\n",
" \"func.func\"() ({\n",
" ^bb0(%arg0: tensor<?xi32>):\n",
" %0 = \"mhlo.constant\"() {value = dense<1> : tensor<i32>} : () -> tensor<i32>\n",
" %1 = \"chlo.broadcast_add\"(%arg0, %0) {broadcast_dimensions = dense<> : tensor<0xi64>} : (tensor<?xi32>, tensor<i32>) -> tensor<?xi32>\n",
" \"std.return\"(%1) : (tensor<?xi32>) -> ()\n",
" }) {arg_attrs = [{tf._user_specified_name = \"values\"}], sym_name = \"__inference_add_one_70\", sym_visibility = \"private\", tf._construction_context = \"kEagerRuntime\", tf._input_shapes = [#tf_type.shape<?>], type = (tensor<?xi32>) -> tensor<?xi32>} : () -> ()\n",
" \"builtin.func\"() ( {\n",
" ^bb0(%arg0: !iree_input.buffer_view): // no predecessors\n",
" \"func.return\"(%1) : (tensor<?xi32>) -> ()\n",
" }) {arg_attrs = [{tf._user_specified_name = \"values\"}], function_type = (tensor<?xi32>) -> tensor<?xi32>, sym_name = \"__inference_add_one_70\", sym_visibility = \"private\", tf._construction_context = \"kEagerRuntime\", tf._input_shapes = [#tf_type.shape<?>]} : () -> ()\n",
" \"func.func\"() ({\n",
" ^bb0(%arg0: !iree_input.buffer_view):\n",
" %0 = \"mhlo.constant\"() {value = dense<0> : tensor<i32>} : () -> tensor<i32>\n",
" %1 = \"iree_input.cast.buffer_view_to_tensor\"(%arg0) : (!iree_input.buffer_view) -> tensor<?xi32>\n",
" %2 = \"mhlo.reduce\"(%1, %0) ( {\n",
" ^bb0(%arg1: tensor<i32>, %arg2: tensor<i32>): // no predecessors\n",
" %2 = \"mhlo.reduce\"(%1, %0) ({\n",
" ^bb0(%arg1: tensor<i32>, %arg2: tensor<i32>):\n",
" %4 = \"mhlo.add\"(%arg1, %arg2) : (tensor<i32>, tensor<i32>) -> tensor<i32>\n",
" \"mhlo.return\"(%4) : (tensor<i32>) -> ()\n",
" }) {dimensions = dense<0> : tensor<1xi64>} : (tensor<?xi32>, tensor<i32>) -> tensor<i32>\n",
" %3 = \"iree_input.cast.tensor_to_buffer_view\"(%2) : (tensor<i32>) -> !iree_input.buffer_view\n",
" \"std.return\"(%3) : (!iree_input.buffer_view) -> ()\n",
" }) {iree.abi = \"{\\22a\\22:[[\\22ndarray\\22,\\22i32\\22,1,null]],\\22r\\22:[[\\22ndarray\\22,\\22i32\\22,0]],\\22v\\22:1}\", sym_name = \"reduce_sum_1d\", type = (!iree_input.buffer_view) -> !iree_input.buffer_view} : () -> ()\n",
" \"builtin.func\"() ( {\n",
" ^bb0(%arg0: !iree_input.buffer_view): // no predecessors\n",
" \"func.return\"(%3) : (!iree_input.buffer_view) -> ()\n",
" }) {function_type = (!iree_input.buffer_view) -> !iree_input.buffer_view, iree.abi = \"{\\22a\\22:[[\\22ndarray\\22,\\22i32\\22,1,null]],\\22r\\22:[[\\22ndarray\\22,\\22i32\\22,0]],\\22v\\22:1}\", sym_name = \"reduce_sum_1d\"} : () -> ()\n",
" \"func.func\"() ({\n",
" ^bb0(%arg0: !iree_input.buffer_view):\n",
" %0 = \"mhlo.constant\"() {value = dense<0> : tensor<i32>} : () -> tensor<i32>\n",
" %1 = \"iree_input.cast.buffer_view_to_tensor\"(%arg0) : (!iree_input.buffer_view) -> tensor<?x3xi32>\n",
" %2 = \"mhlo.reduce\"(%1, %0) ( {\n",
" ^bb0(%arg1: tensor<i32>, %arg2: tensor<i32>): // no predecessors\n",
" %2 = \"mhlo.reduce\"(%1, %0) ({\n",
" ^bb0(%arg1: tensor<i32>, %arg2: tensor<i32>):\n",
" %4 = \"mhlo.add\"(%arg1, %arg2) : (tensor<i32>, tensor<i32>) -> tensor<i32>\n",
" \"mhlo.return\"(%4) : (tensor<i32>) -> ()\n",
" }) {dimensions = dense<0> : tensor<1xi64>} : (tensor<?x3xi32>, tensor<i32>) -> tensor<3xi32>\n",
" %3 = \"iree_input.cast.tensor_to_buffer_view\"(%2) : (tensor<3xi32>) -> !iree_input.buffer_view\n",
" \"std.return\"(%3) : (!iree_input.buffer_view) -> ()\n",
" }) {iree.abi = \"{\\22a\\22:[[\\22ndarray\\22,\\22i32\\22,2,null,3]],\\22r\\22:[[\\22ndarray\\22,\\22i32\\22,1,3]],\\22v\\22:1}\", sym_name = \"reduce_sum_2d\", type = (!iree_input.buffer_view) -> !iree_input.buffer_view} : () -> ()\n",
" \"func.return\"(%3) : (!iree_input.buffer_view) -> ()\n",
" }) {function_type = (!iree_input.buffer_view) -> !iree_input.buffer_view, iree.abi = \"{\\22a\\22:[[\\22ndarray\\22,\\22i32\\22,2,null,3]],\\22r\\22:[[\\22ndarray\\22,\\22i32\\22,1,3]],\\22v\\22:1}\", sym_name = \"reduce_sum_2d\"} : () -> ()\n",
"}) : () -> ()\n",
"\n",
"```\n",
Expand Down Expand Up @@ -264,24 +264,38 @@
{
"cell_type": "code",
"metadata": {
"id": "GF0dzDsbaP2w"
"id": "GF0dzDsbaP2w",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5cf5fdbd-1d04-41ee-ab4a-07a94cfa39d0"
},
"source": [
"#@title Compile the imported MLIR further into an IREE VM bytecode module\n",
"\n",
"from iree.compiler import compile_str\n",
"\n",
"# Note: we'll use the dylib-llvm-aot backend since it has the best support\n",
"# Note: we'll use the cpu (LLVM) backend since it has the best support\n",
"# for dynamic shapes among our compiler targets.\n",
"\n",
"flatbuffer_blob = compile_str(compiler_module, target_backends=[\"dylib-llvm-aot\"], input_type=\"mhlo\")\n",
"flatbuffer_blob = compile_str(compiler_module, target_backends=[\"cpu\"], input_type=\"mhlo\")\n",
"\n",
"# Note: the dylib-llvm-aot target produces platform-specific code. Since you\n",
"# may need to recompile it yourself using the appropriate\n",
"# `-iree-llvm-target-triple` flag, we skip saving it to disk and downloading it."
"# Save the compiled program to disk.\n",
"flatbuffer_path = os.path.join(ARTIFACTS_DIR, \"dynamic_shapes_cpu.vmfb\")\n",
"with open(flatbuffer_path, \"wb\") as output_file:\n",
" output_file.write(flatbuffer_blob)\n",
"print(f\"Wrote compiled program to path '{flatbuffer_path}'\")"
],
"execution_count": 7,
"outputs": []
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Wrote compiled program to path '/tmp/iree/colab_artifacts/dynamic_shapes_cpu.vmfb'\n"
]
}
]
},
{
"cell_type": "code",
Expand All @@ -306,7 +320,7 @@
"from iree import runtime as ireert\n",
"\n",
"vm_module = ireert.VmModule.from_flatbuffer(flatbuffer_blob)\n",
"config = ireert.Config(\"dylib\")\n",
"config = ireert.Config(\"local-task\")\n",
"ctx = ireert.SystemContext(config=config)\n",
"ctx.add_vm_module(vm_module)"
],
Expand All @@ -320,20 +334,20 @@
"base_uri": "https://localhost:8080/"
},
"id": "CQffg1iQatkb",
"outputId": "dab42bd6-0291-41fa-d664-3739fbfa1908"
"outputId": "25820d2f-2b4a-4bec-aca4-e4f2ec38e6c4"
},
"source": [
"import numpy as np\n",
"\n",
"# Our @tf.functions are accessible by name on the module named 'module'\n",
"dynamic_shapes_program = ctx.modules.module\n",
"\n",
"print(dynamic_shapes_program.reduce_sum_1d(np.array([1, 10, 100], dtype=np.int32)))\n",
"print(dynamic_shapes_program.reduce_sum_2d(np.array([[1, 2, 3], [10, 20, 30]], dtype=np.int32)))\n",
"print(dynamic_shapes_program.reduce_sum_2d(np.array([[1, 2, 3], [10, 20, 30], [100, 200, 300]], dtype=np.int32)))\n",
"print(dynamic_shapes_program.add_one(np.array([1, 10, 100], dtype=np.int32)))"
"print(dynamic_shapes_program.reduce_sum_1d(np.array([1, 10, 100], dtype=np.int32)).to_host())\n",
"print(dynamic_shapes_program.reduce_sum_2d(np.array([[1, 2, 3], [10, 20, 30]], dtype=np.int32)).to_host())\n",
"print(dynamic_shapes_program.reduce_sum_2d(np.array([[1, 2, 3], [10, 20, 30], [100, 200, 300]], dtype=np.int32)).to_host())\n",
"print(dynamic_shapes_program.add_one(np.array([1, 10, 100], dtype=np.int32)).to_host())"
],
"execution_count": 10,
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
Expand All @@ -360,10 +374,10 @@
"cell_type": "code",
"metadata": {
"id": "bUaNUkS2ohRj",
"outputId": "0382c67c-72a9-4310-a3a4-550708c515aa",
"outputId": "f33c7864-1bf2-43f1-83f4-bcf83b61cbbd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
"height": 86
}
},
"source": [
Expand All @@ -380,20 +394,24 @@
"except ImportError:\n",
" print(\"Missing google_colab Python package, can't download files\")"
],
"execution_count": 11,
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Zipping '/tmp/iree/colab_artifacts' to '/tmp/dynamic_shapes_colab_artifacts.zip' for download...\n",
" adding: dynamic_shapes.mlir (deflated 80%)\n",
"updating: dynamic_shapes.mlir (deflated 80%)\n",
" adding: dynamic_shapes_cpu.vmfb (deflated 63%)\n",
"Downloading the artifacts zip file...\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"\n",
" async function download(id, filename, size) {\n",
Expand Down Expand Up @@ -436,21 +454,18 @@
" div.remove();\n",
" }\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"application/javascript": [
"download(\"download_f673a666-f490-4fc5-865b-7d6da53b46a9\", \"dynamic_shapes_colab_artifacts.zip\", 831)"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
],
"application/javascript": [
"download(\"download_66541154-4c92-458c-b27d-4adb2370f0db\", \"dynamic_shapes_colab_artifacts.zip\", 5923)"
]
},
"metadata": {}
Expand Down
7 changes: 3 additions & 4 deletions samples/dynamic_shapes/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ cmake --build ${BUILD_DIR} --target iree-compile -- -k 0

# 3. Compile `dynamic_shapes.mlir` using `iree-compile`.
${BUILD_DIR}/tools/iree-compile \
--iree-mlir-to-vm-bytecode-module \
--iree-hal-target-backends=dylib-llvm-aot \
--iree-hal-target-backends=cpu \
--iree-input-type=mhlo \
${ARTIFACTS_DIR}/dynamic_shapes.mlir -o ${ARTIFACTS_DIR}/dynamic_shapes_dylib.vmfb
${ARTIFACTS_DIR}/dynamic_shapes.mlir -o ${ARTIFACTS_DIR}/dynamic_shapes_cpu.vmfb

# 4. Build the `iree_samples_dynamic_shapes` CMake target.
cmake --build ${BUILD_DIR} --target iree_samples_dynamic_shapes -- -k 0

# 5. Run the sample binary.
${BUILD_DIR}/samples/dynamic_shapes/dynamic-shapes \
${ARTIFACTS_DIR}/dynamic_shapes_dylib.vmfb dylib
${ARTIFACTS_DIR}/dynamic_shapes_cpu.vmfb local-task
Loading

0 comments on commit 74d741e

Please sign in to comment.