@@ -50,6 +50,7 @@ struct WgpuContext {
5050 matmul_pipeline : wgpu:: ComputePipeline ,
5151 matmul_large_pipeline : wgpu:: ComputePipeline ,
5252 gather_pipeline : wgpu:: ComputePipeline ,
53+ where_pipeline : wgpu:: ComputePipeline ,
5354 reduce_pipeline : wgpu:: ComputePipeline ,
5455}
5556
@@ -255,6 +256,7 @@ impl WgpuDevice {
255256 MATMUL_LARGE_WGSL ,
256257 ) ,
257258 gather_pipeline : make_pipeline ( & device, "oxideon-gather" , GATHER_WGSL ) ,
259+ where_pipeline : make_pipeline ( & device, "oxideon-where" , WHERE_WGSL ) ,
258260 reduce_pipeline : make_pipeline ( & device, "oxideon-reduce" , REDUCE_WGSL ) ,
259261 device,
260262 queue,
@@ -608,6 +610,58 @@ impl WgpuStorage {
608610 Ok ( Self :: from_buffer ( out, numel, & self . device ) )
609611 }
610612
613+ /// Elementwise comparison on the device (`f32` operands; the Bool result is stored as 0 / 1 like every non-`f32`
614+ /// device tensor). `None` when the operands are not `f32` or do not fit one dispatch: the caller uses the CPU path.
615+ pub fn cmp_op ( & self , lhs_l : & Layout , rhs : & Self , rhs_l : & Layout , op : super :: CmpOp ) -> Result < Option < Self > > {
616+ let numel = lhs_l. numel ( ) ;
617+ if self . dtype != DType :: F32 || rhs. dtype != DType :: F32 || numel == 0 || !self . device . fits_grid ( numel, WG ) {
618+ return Ok ( None ) ;
619+ }
620+ let op_code = match op {
621+ super :: CmpOp :: Eq => 10 ,
622+ super :: CmpOp :: Ne => 11 ,
623+ super :: CmpOp :: Lt => 12 ,
624+ super :: CmpOp :: Le => 13 ,
625+ super :: CmpOp :: Gt => 14 ,
626+ super :: CmpOp :: Ge => 15 ,
627+ } ;
628+ let a = self . to_contiguous ( lhs_l) ?;
629+ let b = rhs. to_contiguous ( rhs_l) ?;
630+ self . device . check_bytes ( "GPU tensor" , numel as u64 * 4 ) ?;
631+ let out = self . device . out_buffer ( numel) ;
632+ let params = BinaryParams { numel : numel as u32 , op_code, _pad0 : 0 , _pad1 : 0 } ;
633+ self . device . run ( & self . device . inner . binary_pipeline , & [ & a. buffer , & b. buffer , & out] , bytemuck:: bytes_of ( & params) , self . device . grid_1d ( numel, WG ) ) ;
634+ let mut s = Self :: from_buffer ( out, numel, & self . device ) ;
635+ s. dtype = DType :: Bool ;
636+ Ok ( Some ( s) )
637+ }
638+
639+ /// `cond ? t : f` on the device for a Bool condition and `f32` branches (all three already broadcast to one shape).
640+ /// `None` for other dtypes or sizes beyond one dispatch.
641+ pub fn where_cond ( cond : & Self , cond_l : & Layout , t : & Self , t_l : & Layout , f : & Self , f_l : & Layout ) -> Result < Option < Self > > {
642+ let numel = t_l. numel ( ) ;
643+ if cond. dtype != DType :: Bool
644+ || t. dtype != DType :: F32
645+ || f. dtype != DType :: F32
646+ || numel == 0
647+ || cond_l. numel ( ) != numel
648+ || f_l. numel ( ) != numel
649+ || !t. device . fits_grid ( numel, WG )
650+ {
651+ return Ok ( None ) ;
652+ }
653+ // a Bool tensor's buffer already holds 0 / 1 as f32; gather it like any other layout
654+ let as_f32 = Self { buffer : cond. buffer . clone ( ) , len : cond. len , dtype : DType :: F32 , device : cond. device . clone ( ) } ;
655+ let c = as_f32. to_contiguous ( cond_l) ?;
656+ let a = t. to_contiguous ( t_l) ?;
657+ let b = f. to_contiguous ( f_l) ?;
658+ t. device . check_bytes ( "GPU tensor" , numel as u64 * 4 ) ?;
659+ let out = t. device . out_buffer ( numel) ;
660+ let params = BinaryParams { numel : numel as u32 , op_code : 0 , _pad0 : 0 , _pad1 : 0 } ;
661+ t. device . run ( & t. device . inner . where_pipeline , & [ & c. buffer , & a. buffer , & b. buffer , & out] , bytemuck:: bytes_of ( & params) , t. device . grid_1d ( numel, WG ) ) ;
662+ Ok ( Some ( Self :: from_buffer ( out, numel, & t. device ) ) )
663+ }
664+
611665 pub fn reduce_op ( & self , layout : & Layout , axes : & [ usize ] , op : ReduceOp ) -> Result < Self > {
612666 if self . dtype == DType :: F32 {
613667 if let Some ( out) = self . reduce_on_device ( layout, axes, op) ? {
@@ -928,12 +982,40 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups)
928982 case 7u: { y = a % b; }
929983 case 8u: { let m = a % b; y = select(m, m + b, m != 0.0 && ((m < 0.0) != (b < 0.0))); }
930984 case 9u: { y = floor_div_f(a, b); }
985+ case 10u: { y = f32(a == b); }
986+ case 11u: { y = f32(!(a == b)); }
987+ case 12u: { y = f32(a < b); }
988+ case 13u: { y = f32(a <= b); }
989+ case 14u: { y = f32(a > b); }
990+ case 15u: { y = f32(a >= b); }
931991 default: { y = a; }
932992 }
933993 dst[i] = y;
934994}
935995"# ;
936996
997+ /// `dst[i] = cond[i] != 0 ? on_true[i] : on_false[i]`.
998+ const WHERE_WGSL : & str = r#"
999+ struct Params {
1000+ numel: u32,
1001+ pad0: u32,
1002+ pad1: u32,
1003+ pad2: u32,
1004+ }
1005+ @group(0) @binding(0) var<storage, read> cond: array<f32>;
1006+ @group(0) @binding(1) var<storage, read> on_true: array<f32>;
1007+ @group(0) @binding(2) var<storage, read> on_false: array<f32>;
1008+ @group(0) @binding(3) var<storage, read_write> dst: array<f32>;
1009+ @group(0) @binding(4) var<uniform> params: Params;
1010+
1011+ @compute @workgroup_size(256)
1012+ fn main(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
1013+ let i = gid.y * (nwg.x * 256u) + gid.x;
1014+ if (i >= params.numel) { return; }
1015+ dst[i] = select(on_false[i], on_true[i], cond[i] != 0.0);
1016+ }
1017+ "# ;
1018+
9371019/// `dst[i] = src[offset + sum_d coord_d(i) * stride_d]` for the row-major coordinates of `i` in `shape`.
9381020/// `layout_info` holds the `ndim` sizes followed by the `ndim` strides (strides may be zero or negative).
9391021const GATHER_WGSL : & str = r#"
0 commit comments