From bb632869ddd01c2f83436eba20f7b21466298a73 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Sat, 7 Nov 2026 20:33:51 -0500 Subject: [PATCH 1/4] Unit test for min/max preferences --- fidget-core/src/eval/test/grad_slice.rs | 24 ++++++++++++++++++++++++ fidget-core/src/types/grad.rs | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/fidget-core/src/eval/test/grad_slice.rs b/fidget-core/src/eval/test/grad_slice.rs index cf8f8cfb4..2eab625af 100644 --- a/fidget-core/src/eval/test/grad_slice.rs +++ b/fidget-core/src/eval/test/grad_slice.rs @@ -267,6 +267,29 @@ impl TestGradSlice { ); } + pub fn test_g_min_max_equal() { + let mut ctx = Context::new(); + let x = ctx.x(); + let y = ctx.y(); + + let min = ctx.min(x, y).unwrap(); + let max = ctx.max(x, y).unwrap(); + + let shape = F::new(&ctx, &[min]).unwrap(); + let tape = shape.grad_slice_tape(Default::default()); + assert_eq!( + Self::eval_xyz(&tape, &[2.0], &[2.0], &[0.0])[0], + Grad::new(2.0, 1.0, 0.0, 0.0) + ); + + let shape = F::new(&ctx, &[max]).unwrap(); + let tape = shape.grad_slice_tape(Default::default()); + assert_eq!( + Self::eval_xyz(&tape, &[2.0], &[2.0], &[0.0])[0], + Grad::new(2.0, 0.0, 1.0, 0.0) + ); + } + pub fn test_g_max() { let mut ctx = Context::new(); let x = ctx.x(); @@ -807,6 +830,7 @@ macro_rules! grad_slice_tests { $crate::grad_test!(test_g_max, $t); $crate::grad_test!(test_g_add, $t); $crate::grad_test!(test_g_min_max, $t); + $crate::grad_test!(test_g_min_max_equal, $t); $crate::grad_test!(test_g_not, $t); $crate::grad_test!(test_g_div, $t); $crate::grad_test!(test_g_recip, $t); diff --git a/fidget-core/src/types/grad.rs b/fidget-core/src/types/grad.rs index 9bd38f742..14673a0bc 100644 --- a/fidget-core/src/types/grad.rs +++ b/fidget-core/src/types/grad.rs @@ -169,6 +169,8 @@ impl Grad { /// Minimum of two values /// /// If either value is `NAN`, then this returns `NAN` (with empty gradients) + /// + /// If the values are equal, prefers the left-hand argument #[inline] pub fn min(self, rhs: Self) -> Self { if self.v.is_nan() || rhs.v.is_nan() { @@ -183,6 +185,8 @@ impl Grad { /// Maximum of two values /// /// If either value is `NAN`, then this returns `NAN` (with empty gradients) + /// + /// If the values are equal, prefers the right-hand argument #[inline] pub fn max(self, rhs: Self) -> Self { if self.v.is_nan() || rhs.v.is_nan() { From 08bdb0320446ec3787a046b6b87bfb2fc35da8ac Mon Sep 17 00:00:00 2001 From: LukeP0WERS Date: Sun, 25 Oct 2026 14:54:16 -0400 Subject: [PATCH 2/4] fix bug by keeping `lhs` for `op_min` on a tie --- fidget-wgpu/src/shaders/grad_ops.wgsl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fidget-wgpu/src/shaders/grad_ops.wgsl b/fidget-wgpu/src/shaders/grad_ops.wgsl index 2efc1b351..95ab2e87a 100644 --- a/fidget-wgpu/src/shaders/grad_ops.wgsl +++ b/fidget-wgpu/src/shaders/grad_ops.wgsl @@ -143,10 +143,10 @@ fn op_sub(lhs: Value, rhs: Value) -> Value { } fn op_min(lhs: Value, rhs: Value, stack: ptr) -> Value { - if lhs.v.w < rhs.v.w { - return lhs; - } else { + if lhs.v.w > rhs.v.w { return rhs; + } else { + return lhs; } } From aac76301177e7654e27be57f4f7fe3425a035471 Mon Sep 17 00:00:00 2001 From: LukeP0WERS Date: Wed, 4 Nov 2026 11:51:50 -0500 Subject: [PATCH 3/4] update remaining backends to match the convention --- fidget-core/src/context/mod.rs | 4 ++-- fidget-core/src/types/grad.rs | 6 +++--- fidget-jit/src/aarch64/grad_slice.rs | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/fidget-core/src/context/mod.rs b/fidget-core/src/context/mod.rs index 7b7a01bed..b704d3afb 100644 --- a/fidget-core/src/context/mod.rs +++ b/fidget-core/src/context/mod.rs @@ -1427,8 +1427,8 @@ impl Context { } BinaryOpcode::Min => { let cond = - self.less_than(v_lhs, v_rhs).unwrap(); - self.if_nonzero_else(cond, d_lhs, d_rhs) + self.less_than(v_rhs, v_lhs).unwrap(); + self.if_nonzero_else(cond, d_rhs, d_lhs) } BinaryOpcode::Max => { let cond = diff --git a/fidget-core/src/types/grad.rs b/fidget-core/src/types/grad.rs index 14673a0bc..72f7da1cd 100644 --- a/fidget-core/src/types/grad.rs +++ b/fidget-core/src/types/grad.rs @@ -175,10 +175,10 @@ impl Grad { pub fn min(self, rhs: Self) -> Self { if self.v.is_nan() || rhs.v.is_nan() { f32::NAN.into() - } else if self.v < rhs.v { - self - } else { + } else if self.v > rhs.v { rhs + } else { + self } } diff --git a/fidget-jit/src/aarch64/grad_slice.rs b/fidget-jit/src/aarch64/grad_slice.rs index 632902951..ace42f672 100644 --- a/fidget-jit/src/aarch64/grad_slice.rs +++ b/fidget-jit/src/aarch64/grad_slice.rs @@ -427,14 +427,14 @@ impl Assembler for GradSliceAssembler { dynasm!(self.0.ops ; fcmp S(reg(lhs_reg)), S(reg(rhs_reg)) ; b.vs 24 // -> NaN - ; b.lt 12 // -> lhs + ; b.gt 12 // -> rhs // Happy path: v >= 0, so we just copy the register - ; mov V(reg(out_reg)).b16, V(reg(rhs_reg)).b16 + ; mov V(reg(out_reg)).b16, V(reg(lhs_reg)).b16 ; b 20 // -> end - // lhs: - ; mov V(reg(out_reg)).b16, V(reg(lhs_reg)).b16 + // rhs: + ; mov V(reg(out_reg)).b16, V(reg(rhs_reg)).b16 ; b 12 // -> end // NaN handler From c2390a5d4993e3bd1e5b1d0e5b007c5359ccc380 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Sat, 7 Nov 2026 20:46:28 -0500 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6c1d59d6..97eee3861 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ In this release, I rewrote the whole GPU API again! Sorry! This allows a `const` constructor, which in turn allows a default-constructed empty `&VarMap`. - Add `Hash` to `RegionSize` (and therefore to `ImageSize` and `VoxelSize`) +- Change `Grad::min` and `max` implementations (along with their JIT + counterparts) to make different decisions if the two sides are equal. This + ensures that (for example) the expression `min(a, b) + max(a, b)` has the + correct gradient. Thanks to @LukeP0WERS for the fix! ## `fidget-wgpu` changes The overarching theme here is a single `trait Context`, which is used for all of