Skip to content

Add image_util library for image --> buffer_view - #5655

Merged
hcindyl merged 7 commits into
iree-org:mainfrom
hcindyl:stb-image
May 12, 2021
Merged

hcindyl merged 7 commits into
iree-org:mainfrom
hcindyl:stb-image

Conversation

@hcindyl

@hcindyl hcindyl commented Apr 28, 2021 •

Copy link
Copy Markdown
Contributor

Import stb library from https://github.com/nothings/stb to load image as
input. 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_file flag to load the image.

* Image has to be the same dimension as the function input. No resize
internally.

* For floating tensor input, the array is scaled to [0 1] by default. If
a different range is needed, a input_range flag needs to be assigned.

* Currently only supports f32 and i8 scalar_type. u8 should be supported
but 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_t input argument. Only i8, u8, and f32 are
supported.

Some limitations:

  • The image dimensions have 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.

@ScottTodd ScottTodd left a comment

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.

Still recovering from a fever, so take this feedback with a grain of salt...

I have a few high level concerns here:

  1. 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.

  2. 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?

@ScottTodd

Copy link
Copy Markdown
Member

Still recovering from a fever, so take this feedback with a grain of salt...

I have a few high level concerns here:

  1. 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.
  2. 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 iree-run-module would just operate on opaque data, like the rest of the system internals.

@hcindyl

hcindyl commented Apr 28, 2021

Copy link
Copy Markdown
Contributor Author

Still recovering from a fever, so take this feedback with a grain of salt...
I have a few high level concerns here:

  1. 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.
  2. 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 iree-run-module would just operate on opaque data, like the rest of the system internals.

With #5602, the logic/code path of this PR needs to be updated anyway (flag function_inputs_file was removed). I am okay to have a cmake-only target, and check the libstb-dev package for the stb image support. However, I am not sure under the current code structure, how I could add the input_file support without forking out iree-run-module entirely. It will be nice if we can use the same Run function without copying all other code.

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.

@ScottTodd

Copy link
Copy Markdown
Member

Still recovering from a fever, so take this feedback with a grain of salt...
I have a few high level concerns here:

  1. 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.
  2. 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 iree-run-module would just operate on opaque data, like the rest of the system internals.

With #5602, the logic/code path of this PR needs to be updated anyway (flag function_inputs_file was removed). I am okay to have a cmake-only target, and check the libstb-dev package for the stb image support. However, I am not sure under the current code structure, how I could add the input_file support without forking out iree-run-module entirely. It will be nice if we can use the same Run function without copying all other code.

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 iree/samples/computer_vision/ (or some other name, like a specific model that uses an image as input and/or output)? That's along the same lines as my thinking on #5222. We could add a iree/samples/common/ or iree/samples/utils/ directory as needed if code starts to get duplicated between new samples.

@hcindyl

hcindyl commented Apr 28, 2021

Copy link
Copy Markdown
Contributor Author

Still recovering from a fever, so take this feedback with a grain of salt...
I have a few high level concerns here:

  1. 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.
  2. 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 iree-run-module would just operate on opaque data, like the rest of the system internals.

With #5602, the logic/code path of this PR needs to be updated anyway (flag function_inputs_file was removed). I am okay to have a cmake-only target, and check the libstb-dev package for the stb image support. However, I am not sure under the current code structure, how I could add the input_file support without forking out iree-run-module entirely. It will be nice if we can use the same Run function without copying all other code.
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 iree/samples/computer_vision/ (or some other name, like a specific model that uses an image as input and/or output)? That's along the same lines as my thinking on #5222. We could add a iree/samples/common/ or iree/samples/utils/ directory as needed if code starts to get duplicated between new samples.

Sure. My only concern is that iree/samples is behind IREE_BUILD_SAMPLES flag, so if we want to build this util binary for Android or RISCV we need to create another cmake flag.

@ScottTodd

Copy link
Copy Markdown
Member

Sure. My only concern is that iree/samples is behind IREE_BUILD_SAMPLES flag, so if we want to build this util binary for Android or RISCV we need to create another cmake flag.

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?

@hcindyl

hcindyl commented Apr 29, 2021 •

Copy link
Copy Markdown
Contributor Author

Sure. My only concern is that iree/samples is behind IREE_BUILD_SAMPLES flag, so if we want to build this util binary for Android or RISCV we need to create another cmake flag.

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 iree-run-module is the runtime tool for the user to run the bytecode module, and having the vision support there could be useful.

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?

@benvanik

benvanik commented Apr 30, 2021 •

Copy link
Copy Markdown
Collaborator

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);
etc

The 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 iree_runtime_call_append_buffer_view & co the user can mix/match which functions they want without having to link in everything. We could provide a bunch of the helpers, samples could have some model-specific ones, and the user could write their own. Can imagine iree_hal_buffer_view_from_png_file, iree_hal_buffer_view_from_npy_file, iree_hal_buffer_view_from_pcm_file, iree_hal_buffer_view_from_wav_file, etc etc.

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 iree_hal_buffer_view_from_png_data vs iree_hal_buffer_view_from_png_data_rescaled - if you're running on an embedded system for real usage you're going to want to use a model that does not round-trip through a 4x larger float allocation, while if you are just trying to run a legacy tfhub model and get something working you may be fine with the rescaling.

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 (IREE_ENABLE_RUNTIME_FILE_UTILS or something) with each individual library predicated on whether its dependencies were available. If a user just wanted to quickly get iree-run-module working then they leave both _SAMPLES and _FILE_UTILS off and will have no problem. If they want to use some of the IREE file utils in their tests to start trying their model they can enable _FILE_UTILS. If they want to enable _SAMPLES to run our own pre-written samples then they can do that, with the caveat being that they may need to install some additional deps. We can make _SAMPLES always require _FILE_UTILS.

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 iree_runtime_* stuff together today and share it - from there we can look at how to take what's here - which is 95% of the required code for image loading - and just wrap that in a C function that uses it. In the meantime, I think a separate PR adding stb and getting that landed would be good: there will likely need to be some internal google3 messing around to get it all happy and that can be done in parallel with the other work.

@hcindyl

hcindyl commented Apr 30, 2021

Copy link
Copy Markdown
Contributor Author

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);
etc

The 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 iree_runtime_call_append_buffer_view & co the user can mix/match which functions they want without having to link in everything. We could provide a bunch of the helpers, samples could have some model-specific ones, and the user could write their own. Can imagine iree_hal_buffer_view_from_png_file, iree_hal_buffer_view_from_npy_file, iree_hal_buffer_view_from_pcm_file, iree_hal_buffer_view_from_wav_file, etc etc.

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 iree_hal_buffer_view_from_png_data vs iree_hal_buffer_view_from_png_data_rescaled - if you're running on an embedded system for real usage you're going to want to use a model that does not round-trip through a 4x larger float allocation, while if you are just trying to run a legacy tfhub model and get something working you may be fine with the rescaling.

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 (IREE_ENABLE_RUNTIME_FILE_UTILS or something) with each individual library predicated on whether its dependencies were available. If a user just wanted to quickly get iree-run-module working then they leave both _SAMPLES and _FILE_UTILS off and will have no problem. If they want to use some of the IREE file utils in their tests to start trying their model they can enable _FILE_UTILS. If they want to enable _SAMPLES to run our own pre-written samples then they can do that, with the caveat being that they may need to install some additional deps. We can make _SAMPLES always require _FILE_UTILS.

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 iree_runtime_* stuff together today and share it - from there we can look at how to take what's here - which is 95% of the required code for image loading - and just wrap that in a C function that uses it. In the meantime, I think a separate PR adding stb and getting that landed would be good: there will likely need to be some internal google3 messing around to get it all happy and that can be done in parallel with the other work.

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 iree_hal_buffer_view_t from stb_image.h supported images. it also allocates the iree_hal_buffer_t based on the iree_hal_element_type_t so we can save the buffer as well as skip the conversion if possible. Another helper binary was planned to print out the resulting iree_hal_buffer_view_t so the user can paste it in --function_input in iree/tools/iree-run*, but if the plan is to support npy, we can make the change.

Regarding stb as a submodule, I can understand the hesitation Scott has. At the same time, stb_image.h actually already exists in the repository from https://github.com/wolfpld/tracy/blob/master/profiler/src/stb_image.h. Of course it could be risky to have an implicit dependency of third_party/tracy, but it could be just as much as any change in the submodule. If you think having a designated submodule is better, we can also do that.

@benvanik

benvanik commented Apr 30, 2021 •

Copy link
Copy Markdown
Collaborator

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 :)
(to clarify: vm_util was born out of necessity for dealing with the calling conventions, flags, and wrapping the C API, but with the work to simplifying the calling conventions, cleanup flags, and expose an easier to use C API it won't be needed any longer)

I had thought of that too - it's already there for free :P
I think we'll want our own copy though - if only because tracy is not in google3 and we'd have to use the google3 copy of stb anyway, so may as well not get into a versioning mismatch between tracy and google3. I've touched the google3 copy a few times and scott is also familiar with it so it should be pretty easy to wire up internally. If it's only used for samples we could just say that samples don't run in google3, but if it's part of our extended API meant for users to use it's best to have it covered/supported.

hcindyl added a commit to hcindyl/iree that referenced this pull request Apr 30, 2021
Following the discussion in iree-org#5655
and as part of the general image support for
iree-org#5631
@hcindyl hcindyl mentioned this pull request Apr 30, 2021
ScottTodd pushed a commit that referenced this pull request May 3, 2021
Following the discussion in #5655
and as part of the general image support for
#5631
@hcindyl hcindyl changed the title Support image input in vm_util library Add image_util library for image --> buffer_view May 6, 2021
@hcindyl

hcindyl commented May 6, 2021

Copy link
Copy Markdown
Contributor Author

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 iree_runtime_call_inputs_push_back_buffer_view. An vision sample (mnist) is also added.

@ScottTodd ScottTodd left a comment

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.

Usage in iree/samples/vision/iree-run-mnist-module.c LGTM. A few comments about the structure of the new image_util library.

Comment thread iree/tools/utils/image_util.h Resolved
Comment thread iree/tools/utils/image_util.c Resolved
Comment thread iree/tools/utils/image_util.c Resolved
Comment thread iree/tools/utils/image_util.c Resolved
Comment thread iree/tools/utils/image_util.c Resolved

@ScottTodd ScottTodd left a comment

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.

Thanks for the changes. This is looking good to me, after a few more tweaks :D

Comment thread iree/tools/utils/image_util.c Resolved
Comment thread iree/tools/utils/image_util.c Resolved
Comment thread iree/tools/utils/image_util.c Outdated
Comment on lines +172 to +179
iree_hal_buffer_map_range(buffer, IREE_HAL_MEMORY_ACCESS_DISCARD_WRITE, 0,
element_byte * buffer_length, &mapped_memory);

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 the comment thread)
I'm not sure about the memory access here either. @benvanik ?

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.

what aren't you sure about? pretend I have not read the comment thread.

@hcindyl hcindyl May 12, 2021 •

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.

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?

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.

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!

hcindyl added 3 commits May 10, 2021 12:22
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`.
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.
@hcindyl
hcindyl force-pushed the stb-image branch 2 times, most recently from 7800995 to ad8ad0f Compare May 10, 2021 19:46
Comment thread iree/tools/utils/image_util.c Resolved
Comment thread iree/tools/utils/image_util.c Resolved

@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.

approved but would be nice to cleanup a bit of the error handling before landing

Comment thread iree/tools/utils/image_util.c Resolved
Comment thread iree/tools/utils/image_util.c Resolved
Comment thread iree/tools/utils/image_util.c Outdated
Comment on lines +172 to +179
iree_hal_buffer_map_range(buffer, IREE_HAL_MEMORY_ACCESS_DISCARD_WRITE, 0,
element_byte * buffer_length, &mapped_memory);

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.

what aren't you sure about? pretend I have not read the comment thread.

Comment thread iree/tools/utils/image_util.h Resolved
@hcindyl
hcindyl requested a review from benvanik May 12, 2021 20:31
@hcindyl
hcindyl merged commit 807f457 into iree-org:main May 12, 2021
GMNGeoffrey added a commit that referenced this pull request May 13, 2021
Collision between #5655 making use of
`iree_bytecode_module` and 24fbc70911 modifying
it.
@not-jenni not-jenni mentioned this pull request May 13, 2021
hcindyl added a commit that referenced this pull request May 17, 2021
@hcindyl
hcindyl deleted the stb-image branch June 10, 2021 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

runtime Relating to the IREE runtime library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants