Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions fidget-core/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
24 changes: 24 additions & 0 deletions fidget-core/src/eval/test/grad_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,29 @@ impl<F: Function + MathFunction> TestGradSlice<F> {
);
}

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();
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions fidget-core/src/types/grad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,24 @@ 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() {
f32::NAN.into()
} else if self.v < rhs.v {
self
} else {
} else if self.v > rhs.v {
rhs
} else {
self
}
}

/// 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() {
Expand Down
8 changes: 4 additions & 4 deletions fidget-jit/src/aarch64/grad_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions fidget-wgpu/src/shaders/grad_ops.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ fn op_sub(lhs: Value, rhs: Value) -> Value {
}

fn op_min(lhs: Value, rhs: Value, stack: ptr<function, Stack>) -> Value {
if lhs.v.w < rhs.v.w {
return lhs;
} else {
if lhs.v.w > rhs.v.w {
return rhs;
} else {
return lhs;
}
}

Expand Down
Loading