Skip to content

Commit 4f46756

Browse files
PetroZarytskyivgvassilev
authored andcommitted
Add a test for non-differentiable pointer params
When the user requests a derivative and doesn't include a non-const pointer parameter to independent parameters, we produce an error and refuse to generate such a derivative. This is done because we don't know how to generate the adjoint. There is, however, no issue if the parameter is marked as non-differentiable, as the adjoint is not needed anyway. This PR adds a test ensuring this works. Fixes #1350.
1 parent ffa3f5f commit 4f46756

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

test/Gradient/NonDifferentiable.C

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ double fn_non_diff_param_call(double i, double j) {
115115
return fn_non_diff_param(i, obj1);
116116
}
117117

118+
// Normally, not differentiating w.r.t. `float *buffer` results in
119+
// error diagnostics. Check that non_differentiable resolves the issue.
120+
float fn_non_diff_ptr_param(float *input, float const *factors, non_differentiable float *buffer) {
121+
return input[0] + factors[0] + buffer[0];
122+
}
123+
118124
#define INIT_EXPR(classname) \
119125
classname expr_1(2, 3); \
120126
classname expr_2(3, 5);
@@ -260,4 +266,10 @@ int main() {
260266
// CHECK-NEXT: result(out, in);
261267
// CHECK-NEXT: result_pullback(out, in, _d_out);
262268
// CHECK-NEXT:}
269+
270+
clad::gradient(fn_non_diff_ptr_param, "input");
271+
272+
// CHECK: void fn_non_diff_ptr_param_grad_0(float *input, const float *factors, float *buffer, float *_d_input) {
273+
// CHECK-NEXT: _d_input[0] += 1;
274+
// CHECK-NEXT: }
263275
}

0 commit comments

Comments
 (0)