cuda.tile.ensure_constant#
- cuda.tile.ensure_constant(value, /)#
Asserts that the argument is a compile-time constant and returns it.
For example, this can be used to ensure that an if statement is folded at compile time:
if ct.ensure_constant(2 > 1): print("Two is greater than one.") else: ct.static_assert(False) # This branch will be discarded
import cuda.tile as ct import torch @ct.kernel def kernel(): if ct.ensure_constant(2 > 1): print("Two is greater than one.") else: ct.static_assert(False) # This branch will be discarded torch.cuda.init() ct.launch(torch.cuda.current_stream(), (1,), kernel, ()) torch.cuda.synchronize()
Output
Two is greater than one.
StaticAssertionErroris raised if the argument is not a compile-time constant:ct.ensure_constant(ct.bid(0))
import cuda.tile as ct import torch @ct.kernel def kernel(): ct.ensure_constant(ct.bid(0)) torch.cuda.init() ct.launch(torch.cuda.current_stream(), (1,), kernel, ()) torch.cuda.synchronize()
Output
Traceback (most recent call last): ... StaticAssertionError: Argument of ensure_constant() is not a compile-time constant