Skip to content

Commit 7affcd2

Browse files
committed
Update spec to make kernel validation optional
Several adapters don't support validating kernel signatures when enqueued. To handle this, we now allow urEnqueueKernelLaunch to return `SUCCESS` even when parameters are invalid. Some tests have also been updated. The CUDA adapter has also been updated to handle invalid arguments.
1 parent e2df8ac commit 7affcd2

File tree

6 files changed

+48
-30
lines changed

6 files changed

+48
-30
lines changed

include/ur_api.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7421,6 +7421,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventSetCallback(
74217421
///////////////////////////////////////////////////////////////////////////////
74227422
/// @brief Enqueue a command to execute a kernel
74237423
///
7424+
/// @details
7425+
/// - Adapters may perform validation on the number of arguments set to the
7426+
/// kernel, but are not required to do so and may return
7427+
/// `::UR_RESULT_SUCCESS` even for invalid invocations.
7428+
///
74247429
/// @remarks
74257430
/// _Analogues_
74267431
/// - **clEnqueueNDRangeKernel**
@@ -7448,8 +7453,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventSetCallback(
74487453
/// - ::UR_RESULT_ERROR_INVALID_WORK_DIMENSION
74497454
/// - ::UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE
74507455
/// - ::UR_RESULT_ERROR_INVALID_VALUE
7451-
/// - ::UR_RESULT_ERROR_INVALID_KERNEL_ARGS - "The kernel argument values
7452-
/// have not been specified."
7456+
/// - ::UR_RESULT_ERROR_INVALID_KERNEL_ARGS
7457+
/// + The kernel argument values have not been specified and the adapter
7458+
/// is able to detect this.
74537459
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
74547460
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
74557461
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueKernelLaunch(

scripts/core/enqueue.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type: function
1616
desc: "Enqueue a command to execute a kernel"
1717
class: $xEnqueue
1818
name: KernelLaunch
19+
details:
20+
- "Adapters may perform validation on the number of arguments set to the kernel, but are not required to do so and may
21+
return `$X_RESULT_SUCCESS` even for invalid invocations."
1922
ordinal: "0"
2023
analogue:
2124
- "**clEnqueueNDRangeKernel**"
@@ -65,8 +68,8 @@ returns:
6568
- $X_RESULT_ERROR_INVALID_WORK_DIMENSION
6669
- $X_RESULT_ERROR_INVALID_WORK_GROUP_SIZE
6770
- $X_RESULT_ERROR_INVALID_VALUE
68-
- $X_RESULT_ERROR_INVALID_KERNEL_ARGS
69-
- "The kernel argument values have not been specified."
71+
- $X_RESULT_ERROR_INVALID_KERNEL_ARGS:
72+
- "The kernel argument values have not been specified and the adapter is able to detect this."
7073
- $X_RESULT_ERROR_OUT_OF_HOST_MEMORY
7174
- $X_RESULT_ERROR_OUT_OF_RESOURCES
7275
--- #--------------------------------------------------------------------------

source/adapters/cuda/enqueue.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,18 @@ enqueueKernelLaunch(ur_queue_handle_t hQueue, ur_kernel_handle_t hKernel,
465465
}
466466

467467
auto &ArgPointers = hKernel->getArgPointers();
468-
UR_CHECK_ERROR(cuLaunchKernel(
469-
CuFunc, BlocksPerGrid[0], BlocksPerGrid[1], BlocksPerGrid[2],
470-
ThreadsPerBlock[0], ThreadsPerBlock[1], ThreadsPerBlock[2], LocalSize,
471-
CuStream, const_cast<void **>(ArgPointers.data()), nullptr));
468+
try {
469+
UR_CHECK_ERROR(cuLaunchKernel(
470+
CuFunc, BlocksPerGrid[0], BlocksPerGrid[1], BlocksPerGrid[2],
471+
ThreadsPerBlock[0], ThreadsPerBlock[1], ThreadsPerBlock[2], LocalSize,
472+
CuStream, const_cast<void **>(ArgPointers.data()), nullptr));
473+
} catch (ur_result_t Err) {
474+
// Cuda returns UR_RESULT_ERROR_INVALID_VALUE if the args are incorrect
475+
if (Err == UR_RESULT_ERROR_INVALID_VALUE) {
476+
return UR_RESULT_ERROR_INVALID_KERNEL_ARGS;
477+
}
478+
return Err;
479+
}
472480

473481
if (phEvent) {
474482
UR_CHECK_ERROR(RetImplEvent->record());

source/loader/ur_libapi.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4979,6 +4979,11 @@ ur_result_t UR_APICALL urEventSetCallback(
49794979
///////////////////////////////////////////////////////////////////////////////
49804980
/// @brief Enqueue a command to execute a kernel
49814981
///
4982+
/// @details
4983+
/// - Adapters may perform validation on the number of arguments set to the
4984+
/// kernel, but are not required to do so and may return
4985+
/// `::UR_RESULT_SUCCESS` even for invalid invocations.
4986+
///
49824987
/// @remarks
49834988
/// _Analogues_
49844989
/// - **clEnqueueNDRangeKernel**
@@ -5006,8 +5011,9 @@ ur_result_t UR_APICALL urEventSetCallback(
50065011
/// - ::UR_RESULT_ERROR_INVALID_WORK_DIMENSION
50075012
/// - ::UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE
50085013
/// - ::UR_RESULT_ERROR_INVALID_VALUE
5009-
/// - ::UR_RESULT_ERROR_INVALID_KERNEL_ARGS - "The kernel argument values
5010-
/// have not been specified."
5014+
/// - ::UR_RESULT_ERROR_INVALID_KERNEL_ARGS
5015+
/// + The kernel argument values have not been specified and the adapter
5016+
/// is able to detect this.
50115017
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
50125018
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
50135019
ur_result_t UR_APICALL urEnqueueKernelLaunch(

source/ur_api.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4344,6 +4344,11 @@ ur_result_t UR_APICALL urEventSetCallback(
43444344
///////////////////////////////////////////////////////////////////////////////
43454345
/// @brief Enqueue a command to execute a kernel
43464346
///
4347+
/// @details
4348+
/// - Adapters may perform validation on the number of arguments set to the
4349+
/// kernel, but are not required to do so and may return
4350+
/// `::UR_RESULT_SUCCESS` even for invalid invocations.
4351+
///
43474352
/// @remarks
43484353
/// _Analogues_
43494354
/// - **clEnqueueNDRangeKernel**
@@ -4371,8 +4376,9 @@ ur_result_t UR_APICALL urEventSetCallback(
43714376
/// - ::UR_RESULT_ERROR_INVALID_WORK_DIMENSION
43724377
/// - ::UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE
43734378
/// - ::UR_RESULT_ERROR_INVALID_VALUE
4374-
/// - ::UR_RESULT_ERROR_INVALID_KERNEL_ARGS - "The kernel argument values
4375-
/// have not been specified."
4379+
/// - ::UR_RESULT_ERROR_INVALID_KERNEL_ARGS
4380+
/// + The kernel argument values have not been specified and the adapter
4381+
/// is able to detect this.
43764382
/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY
43774383
/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES
43784384
ur_result_t UR_APICALL urEnqueueKernelLaunch(

test/conformance/enqueue/urEnqueueKernelLaunch.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,26 +142,15 @@ TEST_P(urEnqueueKernelLaunchTest, InvalidWorkGroupSize) {
142142
}
143143

144144
TEST_P(urEnqueueKernelLaunchTest, InvalidKernelArgs) {
145-
// Cuda and hip both lack any way to validate kernel args
146-
UUR_KNOWN_FAILURE_ON(uur::CUDA{}, uur::HIP{});
147-
UUR_KNOWN_FAILURE_ON(uur::LevelZero{}, uur::LevelZeroV2{});
148-
149-
ur_platform_backend_t backend;
150-
ASSERT_SUCCESS(urPlatformGetInfo(platform, UR_PLATFORM_INFO_BACKEND,
151-
sizeof(ur_platform_backend_t), &backend,
152-
nullptr));
153-
154-
if (backend == UR_PLATFORM_BACKEND_CUDA ||
155-
backend == UR_PLATFORM_BACKEND_HIP ||
156-
backend == UR_PLATFORM_BACKEND_LEVEL_ZERO) {
157-
GTEST_FAIL() << "AMD, L0 and Nvidia can't check kernel arguments.";
158-
}
145+
// Seems to segfault
146+
UUR_KNOWN_FAILURE_ON(uur::HIP{});
159147

160148
// Enqueue kernel without setting any args
161-
ASSERT_EQ_RESULT(urEnqueueKernelLaunch(queue, kernel, n_dimensions,
162-
&global_offset, &global_size, nullptr,
163-
0, nullptr, nullptr),
164-
UR_RESULT_ERROR_INVALID_KERNEL_ARGS);
149+
auto error =
150+
urEnqueueKernelLaunch(queue, kernel, n_dimensions, &global_offset,
151+
&global_size, nullptr, 0, nullptr, nullptr);
152+
ASSERT_TRUE(error == UR_RESULT_ERROR_INVALID_KERNEL_ARGS ||
153+
error == UR_RESULT_SUCCESS);
165154
}
166155

167156
TEST_P(urEnqueueKernelLaunchKernelWgSizeTest, Success) {

0 commit comments

Comments
 (0)