-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for numeric constants. (#112)
- Loading branch information
Showing
8 changed files
with
164 additions
and
3 deletions.
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
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,114 @@ | ||
use half::{bf16, f16}; | ||
use paste::paste; | ||
|
||
use cubecl_core::{self as cubecl, prelude::*}; | ||
|
||
macro_rules! gen_cube { | ||
($trait:ident, [ $($constant:ident $(| $ret_type:ty)?),* ]) => { | ||
$( | ||
gen_cube!($trait, $constant, $($ret_type)?); | ||
)* | ||
}; | ||
($trait:ident, $constant:ident,) => { | ||
gen_cube!($trait, $constant, T); | ||
}; | ||
($trait:ident, $constant:ident, $ret_type:ty) => { | ||
paste! { | ||
gen_cube!([< $trait:lower _ $constant:lower >], $trait, $constant, $ret_type); | ||
} | ||
}; | ||
($func_name:ident, $trait:ident, $constant:ident, $ret_type:ty) => { | ||
#[cube] | ||
pub fn $func_name<T: $trait>() -> $ret_type { | ||
T::$constant | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! gen_tests { | ||
($trait:ident, [ $($type:ident),* ], $constants:tt) => { | ||
$( | ||
gen_tests!($trait, $type, $constants); | ||
)* | ||
}; | ||
($trait:ident, $type:ident, [ $($constant:ident $(| $ret_type:ty)?),* ]) => { | ||
$( | ||
gen_tests!($trait, $type, $constant, $($ret_type)?); | ||
)* | ||
}; | ||
($trait:ident, $type:ident, $constant:ident,) => { | ||
gen_tests!($trait, $type, $constant, $type); | ||
}; | ||
($trait:ident, $type:ident, $constant:ident, $ret_type:ty) => { | ||
paste! { | ||
gen_tests!([< cube_ $trait:lower _ $constant:lower _ $type _test >], [< $trait:lower _ $constant:lower >], $type, $constant, $ret_type); | ||
} | ||
}; | ||
($test_name:ident, $func_name:ident, $type:ty, $constant:ident, $ret_type:ty) => { | ||
#[test] | ||
fn $test_name() { | ||
let mut context = CubeContext::root(); | ||
$func_name::expand::<$type>(&mut context); | ||
let scope = context.into_scope(); | ||
|
||
let mut scope1 = CubeContext::root().into_scope(); | ||
let item = Item::new(<$ret_type>::as_elem()); | ||
scope1.create_with_value(<$type>::$constant, item); | ||
|
||
assert_eq!( | ||
format!("{:?}", scope.operations), | ||
format!("{:?}", scope1.operations) | ||
); | ||
} | ||
}; | ||
} | ||
|
||
gen_cube!(Numeric, [MAX, MIN]); | ||
gen_cube!(Int, [BITS | u32]); | ||
gen_cube!( | ||
Float, | ||
[ | ||
DIGITS | u32, | ||
EPSILON, | ||
INFINITY, | ||
MANTISSA_DIGITS | u32, | ||
MAX_10_EXP | i32, | ||
MAX_EXP | i32, | ||
MIN_10_EXP | i32, | ||
MIN_EXP | i32, | ||
MIN_POSITIVE, | ||
NAN, | ||
NEG_INFINITY, | ||
RADIX | u32 | ||
] | ||
); | ||
|
||
mod tests { | ||
use super::*; | ||
use cubecl_core::{ | ||
frontend::{CubeContext, CubePrimitive}, | ||
ir::Item, | ||
}; | ||
use pretty_assertions::assert_eq; | ||
|
||
gen_tests!(Numeric, [bf16, f16, f32, f64, i32, i64, u32], [MAX, MIN]); | ||
gen_tests!(Int, [i32, i64, u32], [BITS | u32]); | ||
gen_tests!( | ||
Float, | ||
[bf16, f16, f32, f64], | ||
[ | ||
DIGITS | u32, | ||
EPSILON, | ||
INFINITY, | ||
MANTISSA_DIGITS | u32, | ||
MAX_10_EXP | i32, | ||
MAX_EXP | i32, | ||
MIN_10_EXP | i32, | ||
MIN_EXP | i32, | ||
MIN_POSITIVE, | ||
NAN, | ||
NEG_INFINITY, | ||
RADIX | u32 | ||
] | ||
); | ||
} |
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