-
Notifications
You must be signed in to change notification settings - Fork 46
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
Add support for numeric match at runtime #143
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8b78b33
Add support for numeric match (switch)
wingertge 89d2f35
Fix switch
wingertge f37c467
Fix typo
wingertge a211e85
Merge remote-tracking branch 'upstream/main' into switch
wingertge e39ff4b
Update to new variable allocator
wingertge 58f1845
Fix clippy warnings
wingertge 0e164a9
Rename based on review feedback
wingertge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use crate as cubecl; | ||
|
||
use cubecl::prelude::*; | ||
|
||
#[cube(launch)] | ||
pub fn kernel_switch_simple(output: &mut Array<f32>, case: u32) { | ||
if UNIT_POS == 0 { | ||
match case { | ||
0 => { | ||
output[0] = 1.0; | ||
} | ||
1 => { | ||
output[0] = 3.0; | ||
} | ||
_ => { | ||
output[0] = 5.0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cube(launch)] | ||
pub fn kernel_switch_value_expr(output: &mut Array<f32>, case: u32) { | ||
if UNIT_POS == 0 { | ||
let value = match case { | ||
0 => 1.0f32, | ||
1 => 3.0f32, | ||
_ => 5.0f32, | ||
}; | ||
output[0] = value; | ||
} | ||
} | ||
wingertge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#[cube(launch)] | ||
pub fn kernel_switch_or_arm(output: &mut Array<f32>, case: u32) { | ||
if UNIT_POS == 0 { | ||
let value = match case { | ||
0 => 1.0f32, | ||
1 | 2 => 3.0f32, | ||
_ => 5.0f32, | ||
}; | ||
output[0] = value; | ||
} | ||
} | ||
|
||
pub fn test_switch_statement<R: Runtime>(client: ComputeClient<R::Server, R::Channel>) { | ||
let handle = client.create(f32::as_bytes(&[0.0, 1.0])); | ||
|
||
let vectorization = 2; | ||
|
||
kernel_switch_simple::launch::<R>( | ||
&client, | ||
CubeCount::Static(1, 1, 1), | ||
CubeDim::default(), | ||
unsafe { ArrayArg::from_raw_parts(&handle, 2, vectorization) }, | ||
ScalarArg::new(0), | ||
); | ||
|
||
let actual = client.read(handle.binding()); | ||
let actual = f32::from_bytes(&actual); | ||
|
||
assert_eq!(actual[0], 1.0); | ||
} | ||
|
||
pub fn test_switch_used_as_value<R: Runtime>(client: ComputeClient<R::Server, R::Channel>) { | ||
let handle = client.create(f32::as_bytes(&[0.0, 1.0])); | ||
|
||
let vectorization = 2; | ||
|
||
kernel_switch_value_expr::launch::<R>( | ||
&client, | ||
CubeCount::Static(1, 1, 1), | ||
CubeDim::default(), | ||
unsafe { ArrayArg::from_raw_parts(&handle, 2, vectorization) }, | ||
ScalarArg::new(1), | ||
); | ||
|
||
let actual = client.read(handle.binding()); | ||
let actual = f32::from_bytes(&actual); | ||
|
||
assert_eq!(actual[0], 3.0); | ||
} | ||
|
||
pub fn test_switch_default<R: Runtime>(client: ComputeClient<R::Server, R::Channel>) { | ||
let handle = client.create(f32::as_bytes(&[0.0, 1.0])); | ||
|
||
let vectorization = 2; | ||
|
||
kernel_switch_value_expr::launch::<R>( | ||
&client, | ||
CubeCount::Static(1, 1, 1), | ||
CubeDim::default(), | ||
unsafe { ArrayArg::from_raw_parts(&handle, 2, vectorization) }, | ||
ScalarArg::new(5), | ||
); | ||
|
||
let actual = client.read(handle.binding()); | ||
let actual = f32::from_bytes(&actual); | ||
|
||
assert_eq!(actual[0], 5.0); | ||
} | ||
|
||
pub fn test_switch_or_branch<R: Runtime>(client: ComputeClient<R::Server, R::Channel>) { | ||
let handle = client.create(f32::as_bytes(&[0.0, 1.0])); | ||
|
||
let vectorization = 2; | ||
|
||
kernel_switch_or_arm::launch::<R>( | ||
&client, | ||
CubeCount::Static(1, 1, 1), | ||
CubeDim::default(), | ||
unsafe { ArrayArg::from_raw_parts(&handle, 2, vectorization) }, | ||
ScalarArg::new(2), | ||
); | ||
|
||
let actual = client.read(handle.binding()); | ||
let actual = f32::from_bytes(&actual); | ||
|
||
assert_eq!(actual[0], 3.0); | ||
} | ||
|
||
#[allow(missing_docs)] | ||
#[macro_export] | ||
macro_rules! testgen_switch { | ||
() => { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_switch_statement() { | ||
let client = TestRuntime::client(&Default::default()); | ||
cubecl_core::runtime_tests::switch::test_switch_statement::<TestRuntime>(client); | ||
} | ||
|
||
#[test] | ||
fn test_switch_used_as_value() { | ||
let client = TestRuntime::client(&Default::default()); | ||
cubecl_core::runtime_tests::switch::test_switch_used_as_value::<TestRuntime>(client); | ||
} | ||
|
||
#[test] | ||
fn test_switch_default() { | ||
let client = TestRuntime::client(&Default::default()); | ||
cubecl_core::runtime_tests::switch::test_switch_default::<TestRuntime>(client); | ||
} | ||
|
||
#[test] | ||
fn test_switch_or_branch() { | ||
let client = TestRuntime::client(&Default::default()); | ||
cubecl_core::runtime_tests::switch::test_switch_or_branch::<TestRuntime>(client); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference between the
SwitchExpand
andSwitchExpandExpr
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SwitchExpandExpr
creates an outer output variables and adds an assign statement to each branch to assign the return to the outer variable. It's the same as theIfExpandExpr
, it's used to emulate Rust behaviour that allows using match as value in assignments. For typing reasons and to avoid a pointless assign in every branch, the two are split based on whether the match has a return value.