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

Cube option #494

Merged
merged 3 commits into from
Feb 24, 2025
Merged

Cube option #494

merged 3 commits into from
Feb 24, 2025

Conversation

maxtremblay
Copy link
Collaborator

@maxtremblay maxtremblay commented Feb 24, 2025

This make Option<T> where T: LaunchArg launchable in a cubecl kernel.
Combined with

match comptime!(option) { \* ... *\ }

it allows to match at comptime data that are runtime known. For example, for the first test named kernel_option_scalar, we can generate two different kernels based on an argument
of type Option<i32>.

// When option is None
extern "C" __global__ void kernel_option_scalar(int output_0[], uint info[]) {

  int threadIdxGlobal = threadIdx.x + threadIdx.y * blockDim.x +
                        threadIdx.z * (blockDim.x * blockDim.y);
  const bool l_0 = threadIdxGlobal == uint(0);
  if (l_0) {
  }
}

// When option is Some(value: i32)
extern "C" __global__ void kernel_option_scalar(int output_0[], uint info[],
                                                int scalars_i32[]) {

  int threadIdxGlobal = threadIdx.x + threadIdx.y * blockDim.x +
                        threadIdx.z * (blockDim.x * blockDim.y);
  const bool l_0 = threadIdxGlobal == uint(0);
  if (l_0) {
    const uint l_1 = info[uint(0)];
    const bool l_2 = uint(0) < l_1;
    if (l_2) {
      output_0[uint(0)] = scalars_i32[0];
    }
  }
}

Limitations

First, we can't use

let x = Some(y);

directly in a kernel. Thus, I introduce a new function named some that does exactly. However, it has some issue with type inference. A proper fix of the way we expand enum variant should solve the issue.

Second, we can't use stuff like option.map(|x| 2 * x) to generate code at comptime. Currently, we have to use a match statement to achieve this behavior.

Design choices

I hesitated between introducing a new enum

pub enum CubeOption<T> {
    Some(T),
    None
}

for which we could implement more utilities similar to std::Option. However, I decided to go with std::Option directly for now as a first step.

Justification

I believe this will quite useful to implement quantization in the matmul without doubling all implementations.

Copy link
Member

@louisfd louisfd left a comment

Choose a reason for hiding this comment

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

LGTM

@maxtremblay maxtremblay merged commit 6863166 into main Feb 24, 2025
5 checks passed
@maxtremblay maxtremblay deleted the cube-option branch February 24, 2025 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants