Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL][Bindless] Allow 3-channel image formats #15210

Open
wants to merge 7 commits into
base: sycl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ descriptor against the limitations outlined below. If the given descriptor is
deemed invalid, then a `sycl::exception` will be thrown with error code
`sycl::errc::invalid`.

For all image types, the value of `num_channels` must be `1`, `2`, or `4`.
The value of `num_channels` supported by all image types and backends
is `1`, `2`, or `4`.
Some backends also support `num_channels` to be `3`.
gmlueck marked this conversation as resolved.
Show resolved Hide resolved

For the `standard` image type, the value of `num_levels` and `array_size` must
both be `1`.
Expand Down Expand Up @@ -2884,4 +2886,5 @@ These features still need to be handled:
handles and the imported `interop_xxx_handle`.
|5.17|2024-07-30| - Add support for mapping external memory to linear USM using
`map_external_linear_memory`.
|5.18|2024-08-27| - Allow 3-channel image formats on some backends.
|======================
12 changes: 5 additions & 7 deletions sycl/include/sycl/ext/oneapi/bindless_images_descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ namespace ext::oneapi::experimental {

namespace detail {

inline image_channel_order
constexpr image_channel_order
get_image_default_channel_order(unsigned int num_channels) {
switch (num_channels) {
case 1:
return image_channel_order::r;
case 2:
return image_channel_order::rg;
case 3:
return image_channel_order::rgb;
case 4:
return image_channel_order::rgba;
default:
Expand Down Expand Up @@ -120,13 +122,9 @@ struct image_descriptor {
}

void verify() const {

if (this->num_channels != 1 && this->num_channels != 2 &&
this->num_channels != 4) {
// Images can only have 1, 2, or 4 channels.
if ((this->num_channels < 1) || (this->num_channels > 4)) {
throw sycl::exception(sycl::errc::invalid,
"Images must have only 1, 2, or 4 channels! Use a "
"valid number of channels instead.");
"Images must have 1, 2, 3, or 4 channels.");
}

switch (this->type) {
Expand Down
109 changes: 109 additions & 0 deletions sycl/test-e2e/bindless_images/3_channel_format.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// REQUIRES: cuda
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you limiting this to CUDA adapter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it requires extra L0 work, I see you added it here: https://github.com/oneapi-src/unified-runtime/pull/2022/files


// RUN: %{build} -o %t.out
// RUN: %t.out

#include <iostream>
#include <sycl/detail/core.hpp>

#include <sycl/ext/oneapi/bindless_images.hpp>

// Uncomment to print additional test information
// #define VERBOSE_PRINT

class image_kernel;

namespace syclexp = sycl::ext::oneapi::experimental;

int main() {
sycl::device dev;
sycl::queue q(dev);
auto ctxt = q.get_context();

constexpr size_t width = 512;
std::vector<float> out(width);
std::vector<float> expected(width);
std::vector<sycl::float3> dataIn(width);
float exp = 512;
for (int i = 0; i < width; i++) {
expected[i] = exp;
dataIn[i] = sycl::float3(exp, width, i);
}

try {
// Main point of this test is to check creating an image
// with a 3-channel format
syclexp::image_descriptor desc({width}, 3, sycl::image_channel_type::fp32);

syclexp::image_mem imgMem(desc, dev, ctxt);

q.ext_oneapi_copy(dataIn.data(), imgMem.get_handle(), desc);
q.wait_and_throw();

// Some backends don't support 3-channel formats
// We still try to create the image,
// but we expect it to fail with UR_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT
syclexp::unsampled_image_handle imgHandle =
sycl::ext::oneapi::experimental::create_image(imgMem, desc, dev, ctxt);

sycl::buffer<float> buf(out.data(), width);

q.submit([&](sycl::handler &cgh) {
sycl::accessor outAcc{buf};
ProGTX marked this conversation as resolved.
Show resolved Hide resolved

cgh.parallel_for<image_kernel>(width, [=](sycl::id<1> id) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__)
// This shouldn't be hit anyway since CUDA doesn't support
// 3-channel formats, but we need to ensure the kernel can compile
using pixel_t = sycl::float4;
#else
using pixel_t = sycl::float3;
#endif
auto pixel = syclexp::fetch_image<pixel_t>(imgHandle, int(id[0]));
outAcc[id] = pixel[0];
});
});
q.wait_and_throw();

} catch (const sycl::exception &ex) {
const std::string_view errMsg(ex.what());
if (ctxt.get_backend() == sycl::backend::ext_oneapi_cuda) {
if (errMsg.find("UR_RESULT_ERROR_UNSUPPORTED_IMAGE_FORMAT") !=
std::string::npos) {
std::cout << "CUDA doesn't support 3-channel formats, test passed."
<< std::endl;
return 0;
}
}
std::cerr << "Unexpected SYCL exception: " << errMsg << "\n";
return 1;
} catch (...) {
std::cerr << "Unknown exception caught!\n";
return 2;
}

bool validated = true;
for (int i = 0; i < width; i++) {
bool mismatch = false;
if (out[i] != expected[i]) {
mismatch = true;
validated = false;
}

if (mismatch) {
#ifdef VERBOSE_PRINT
std::cout << "Result mismatch! Expected: " << expected[i]
<< ", Actual: " << out[i] << std::endl;
#else
break;
#endif
}
}
if (validated) {
std::cout << "Test passed!" << std::endl;
return 0;
}

std::cout << "Test failed!" << std::endl;
return 3;
}
Loading