Repository navigation
Add image_util library for image --> buffer_view - #5655
Conversation
4db2445 to
0090fe4
Compare
ScottTodd
left a comment
There was a problem hiding this comment.
Still recovering from a fever, so take this feedback with a grain of salt...
I have a few high level concerns here:
-
I don't think vm_util should exist, and I hesitate to add any new functions there. If the C APIs are difficult to use from C++, we should improve them or wrap them in a real C++ API. Even if common tasks are useful to wrap up in a utils file, that still adds a layer of indirection for users looking at samples.
-
Samples-only dependencies don't need to be added via submodules and integrated into Bazel and CMake. Each submodule adds complexity to the project, even if only used from a small slice of the tree. That still applies to lightweight dependencies like stb. Limiting to CMake-only and using
find_package()(or equivalent) should be sufficient: https://github.com/google/iree/blob/8b3790d5ba5c86abad176be69176b9a74f898de7/iree/testing/vulkan/CMakeLists.txt#L19-L41
If this isn't a samples-only dep, but is intended to be used through iree-run-module etc. then I can see the benefits to building off of vm_util. I do worry about building more logic into the tools that would require different work to use from a user application though. I guess I'd need to be sold on if tools like iree-run-module are to be treated as "just compiler development tools" or something more user-facing and fully supported. Maybe we could move towards more tools being built on the C API only, with optional CMake dependencies?
I was figuring we'd add a bunch of samples showing how to use images, audio, etc. as inputs/outputs. The "compiler tools" like |
With #5602, the logic/code path of this PR needs to be updated anyway (flag I am happy to move this code to other places in the code base. Please suggest a suitable location for the feature and I will make the change. |
How about a new sample using the C API at |
Sure. My only concern is that |
I don't see the problem? Samples exist for users to reference when integrating IREE (a middleware library) into their own projects. We can make samples build in any way we like, including enabling/disabling automatically based on the availability of dependencies or compatibility with the system. See the vulkan sample for an example: https://github.com/google/iree/blob/6e10621f745816537ff12e8b05a43c4d9cb66928/iree/samples/vulkan/CMakeLists.txt#L32-L39 What would be different for Android or RISCV? |
If we are okay of having more flags controlling the sample build outside of the original IREE_BUILD_SAMPLES, sure. However, the way I would love to use (and am using) the image input is not just show one example with one module, but more like a tool so that I can send in different bytecode modules and different input images to evaluate the input/output. That's really an utility tool instead of a sample IMHO. If the concern is not to grow vm_til with extra features, the util function (e.g., image_util) can live off vm_til, and uses C API in it. From https://google.github.io/iree/developing-iree/developer-overview#iree-run-module it looks like If a vision sample is truly needed, we can add one with specific model (https://github.com/google/iree/blob/main/iree/samples/models/mnist.mlir should be a good candidate), connect to the image_util, and run a simple mnist test. Is that reasonable? |
|
I think I'm motivated to do #5631 today - the intent here is to have a builder-like interface for constructing invocations to hide all the boilerplate of buffer creation. My thinking is that utilities that deal with certain file types can then extend this (in the classic C way): // Initialize a call to the given function.
iree_runtime_call_t call;
iree_runtime_call_initialize(context, function, etc, &call);
// Append an input of whatever type you want (int, float, buffer, custom object, etc):
iree_vm_variant_t some_variant = ...;
iree_runtime_call_append_input(&call, &some_variant);
// Helper for primitive values:
iree_runtime_call_append_input_value(&call, iree_vm_value_make_i32(1234));
// Helper for ref objects (buffer views, custom objects, etc):
iree_runtime_call_append_input_ref(&call, &some_custom_object);
// Helper for wrapping/cloning host memory into a buffer view:
iree_runtime_call_append_buffer_view(&call, SOME_FLAGS_DEFINING_OWNERSHIP, shape, type, data);
etcThe idea is then that we could have a collection of utility functions in a bunch of independent libraries for either directly adding to that call builder and for producing buffer views: // Get a buffer view from a PNG file in RGB/RGBA byte/float/etc:
iree_buffer_view_t* buffer_view;
iree_hal_buffer_view_from_png_data(FLAGS (data format, etc), png_data, device_allocator, &buffer_view);
// Helper that just calls the above to make things cleaner:
iree_runtime_call_append_buffer_view_from_png_data(&call, ....);By having these be functions that layer onto the base The major thing here is that using any one of those helpers does not require you to pull in all of them, and ideally we have them at the granularity that the linker can effectively strip what is not needed. For example, the code path for loading a png as sensible RGB or RGBX bytes is straightforward, while the code path for loading a png and then applying all the weird floating point math needed by legacy models should be separate so that users who aren't trying to run legacy models don't need to have any floating point math at all. Think Now, as for iree-run-module: I think that will not support nearly any of these special formats besides npy/npz (also something I'm going to try to hit this weekend) - there's just too much fragmentation and silliness in ML for format-specific things to ever be scalable. Following the Unix philosophy is the right approach for that: we can have simple command line tools that only ever need to run on the host for jpeg->npy-in-weird-model-specific-format and then load those files in iree-run-module - iree-run-module then stays portable, low-dependency, and easy to build reproducers for (all I/O is through npy/npz), while users can still string together more complex pipelines if they want. Basically, iree-run-module will support I/O that is on the scale of fread/fwrite but not much more: this means we could have support for .hex/.bin/etc along with our current textual formats, but not for any higher-level image/video/etc. I think there is still a question as to whether these file format utilities are required or not. I think having a dedicated cmake flag for them independent of samples would be good ( As to whether things like stb are submodules: I think if we use them in _FILE_UTILS then they should be, whereas if they are exclusively used in _SAMPLES then they should not be. We may for example want to use ffmpeg to extract video frames for a sample but I would absolutely never want that as a submodule, or have a mediapipe sample, etc. I'll try to get a rough draft of the |
Thank you Ben for sharing the plan of how we will move forward in supporting different kinds of input. I will wait for the shareout and change the implementation accordingly. My current approach (after discussed with @ScottTodd yesterday) pretty much aligns with what you laid out: a library to generate Regarding stb as a submodule, I can understand the hesitation Scott has. At the same time, |
|
Yep! I think what you've got is a great start and it's just that vm_util as it exists is bad. Time to finally rip off the bandaid :) I had thought of that too - it's already there for free :P |
Following the discussion in iree-org#5655 and as part of the general image support for iree-org#5631
|
Rewrite the PR based on the runtime APIs in #5707. The library reads an image file using stblib::stb_image, and produces a buffer view that can be add to the runtime call with |
ScottTodd
left a comment
There was a problem hiding this comment.
Usage in iree/samples/vision/iree-run-mnist-module.c LGTM. A few comments about the structure of the new image_util library.
ScottTodd
left a comment
There was a problem hiding this comment.
Thanks for the changes. This is looking good to me, after a few more tweaks :D
| iree_hal_buffer_map_range(buffer, IREE_HAL_MEMORY_ACCESS_DISCARD_WRITE, 0, | ||
| element_byte * buffer_length, &mapped_memory); |
There was a problem hiding this comment.
(continuing the comment thread)
I'm not sure about the memory access here either. @benvanik ?
There was a problem hiding this comment.
what aren't you sure about? pretend I have not read the comment thread.
There was a problem hiding this comment.
When we map the hal buffer here the content would be modified with the rescaled pixel data, so I think marking the access type to DISCARD_WRITE signals the content would be overridden. However, after the rescaling, the buffer will be used as the input data, and from the runtime example, it should be marked as READ to indicate it is read only, similar to the above iree_tools_utils_buffer_view_from_image. Should we update the access type here?
There was a problem hiding this comment.
ah - the access here is just for this particular map_range - so in the future when the buffer is mapped again it can be mapped with different access modes - this is fine as-is!
image_util library produces hal buffer views based on image files. The library utilize third_party/stblib/stb_image.h to produce the uint8_t pixel data, and wrap/rescale the pixel data based on the iree_hal_element_type_t input argument. Only i8, u8, and f32 are supported. Some limitations: * The image dimension has to match the iree_hal_dim_t input argument. No resize or channel selection internally (except dropping the alpha channel). * For f32 element type, the array is rescaled to [0, 1] by default. If a different range is needed, an `input_range` flag needs to be specified. * For i8 element type, the user is responsible for the u8 --> i8 data preparation, as the function wraps the value directly. An example of running the mnist model using an image file and the image_util library is added to `iree/samples/vision`.
Address review comments.
Make the input_range part of the function argument. Split `iree_tools_utils_buffer_view_from_image` into the direct wrapper and the rescaled versions so the user don't need to define the input_range for the int8_t and uint8_t models.
7800995 to
ad8ad0f
Compare
benvanik
left a comment
There was a problem hiding this comment.
approved but would be nice to cleanup a bit of the error handling before landing
| iree_hal_buffer_map_range(buffer, IREE_HAL_MEMORY_ACCESS_DISCARD_WRITE, 0, | ||
| element_byte * buffer_length, &mapped_memory); |
There was a problem hiding this comment.
what aren't you sure about? pretend I have not read the comment thread.
Collision between #5655 making use of `iree_bytecode_module` and 24fbc70911 modifying it.
Import stb library from https://github.com/nothings/stb to load image asinput. This allows us to load 3d input array as an image instead of a
text file.
Some limitations:* Only support one input arguement of the function, since it uses
function_inputs_fileflag to load the image.* Image has to be the same dimension as the function input. No resizeinternally.
* For floating tensor input, the array is scaled to [0 1] by default. Ifa different range is needed, a
input_rangeflag needs to be assigned.* Currently only supports f32 and i8 scalar_type. u8 should be supportedbut it can't be parsed from MLIR. For i8, the user is responsible for
the uint8 --> sint8 data preparation, as the function copies the value
naively.
image_util library produces buffer views based on image
files. The library utilizes third_party/stblib/stb_image.h to produce
the uint8_t pixel data, and wrap/rescale the pixel data based on the
iree_hal_element_type_tinput argument. Only i8, u8, and f32 aresupported.
Some limitations:
No resize or channel selection internally (except dropping the alpha
channel).
If a different range is needed, an
input_rangeflag needs to bespecified.
data preparation, as the function wraps the value directly.
An example of running the mnist model using an image file and the
image_util library is added to
iree/samples/vision.