Summary
PTODSL RuntimeValue 目前不能直接用于 Python 原生 and、or 布尔表达式
编译时会尝试把左侧条件使用 bool(...) 转换,引发报错
希望能够支持在 PTODSL 中正常使用 and、or 布尔表达式,并保持短路行为:
x and y => x 为假时不对 y 求值
x or y => x 为真时不对 y 求值
and 简单样例
from ptodsl import pto, scalar
@pto.jit(name="and_kernel", kernel_kind="vector", target="a5")
def kernel(value: pto.i32, divisor: pto.i32, out: pto.ptr(pto.i8, "gm")):
pred = (divisor != 0) and ((value // divisor) > 0)
scalar.store(pred, out, 0)
kernel.compile()
or 简单样例
from ptodsl import pto, scalar
@pto.jit(name="or_kernel", kernel_kind="vector", target="a5")
def kernel(value: pto.i32, divisor: pto.i32, out: pto.ptr(pto.i8, "gm")):
pred = (divisor == 0) or ((value // divisor) > 0)
scalar.store(pred, out, 0)
kernel.compile()
两种情况都会在编译阶段报错:
ptodsl._diagnostics.PTODSLTracingMisuseError: native Python if/while condition cannot consume a PTODSL runtime value during tracing. This value is a device-side SSA/runtime-metadata value, not a Python bool/int. Use pto.if_(...) or pto.for_(...) for device-side control flow, or keep the bound/condition in pto.const_expr.
Motivation / use case
支持 Python 原生 and、or 布尔表达式,能帮助用户更方便地在 PTODSL 中表达复杂一些的条件语句,这在实际算子逻辑中时不时会出现
同时,布尔表达式的短路行为能在不满足左侧(x)条件时减少对右侧(y)无意义的求值,在右侧计算开销较大,或右侧只在左侧满足时才有效的场景中有助于提升计算效率
Proposed API / behavior
No response
Alternatives considered
No response
Additional context
No response
Summary
PTODSL RuntimeValue 目前不能直接用于 Python 原生
and、or布尔表达式编译时会尝试把左侧条件使用
bool(...)转换,引发报错希望能够支持在 PTODSL 中正常使用
and、or布尔表达式,并保持短路行为:x and y=>x为假时不对y求值x or y=>x为真时不对y求值and简单样例or简单样例两种情况都会在编译阶段报错:
Motivation / use case
支持 Python 原生
and、or布尔表达式,能帮助用户更方便地在 PTODSL 中表达复杂一些的条件语句,这在实际算子逻辑中时不时会出现同时,布尔表达式的短路行为能在不满足左侧(
x)条件时减少对右侧(y)无意义的求值,在右侧计算开销较大,或右侧只在左侧满足时才有效的场景中有助于提升计算效率Proposed API / behavior
No response
Alternatives considered
No response
Additional context
No response