Skip to content

Commit 90ff803

Browse files
aniruddhaadak80vgvassilev
authored andcommitted
docs: add documentation for non_differentiable attribute
Resolves #1271 by documenting the usage of the non_differentiable annotation macro for variables and functions. Signed-off-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
1 parent b222fad commit 90ff803

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

docs/userDocs/source/user/UsingClad.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,42 @@ Currently, class type support have the following limitations:
592592
Class type support is under active development and thus, most of these
593593
limitations will be removed soon.
594594

595+
596+
The ``non_differentiable`` Attribute
597+
------------------------------------
598+
599+
Occasionally, you may want to skip differentiating a specific variable or function call. For example, some variables might be used purely for logging, as constants, or as standalone metrics. Clad provides the ``non_differentiable`` annotation attribute to safely omit generating derivatives for these components.
600+
601+
You can apply this attribute using Clang's annotation syntax. The most common approach is to define a macro alias:
602+
603+
.. code-block:: c++
604+
605+
#define non_differentiable __attribute__((annotate("non_differentiable")))
606+
607+
If the ``non_differentiable`` attribute is applied to a variable, Clad skips generating a derivative counterpart for it:
608+
609+
.. code-block:: c++
610+
611+
class PointData {
612+
public:
613+
double x;
614+
double y;
615+
non_differentiable double weight; // Clad will not compute derivatives with respect to this member
616+
};
617+
618+
If the attribute is applied to a function declaration, Clad refrains from producing any derivative expressions for that specific function. Instead, calls to the primal function are injected directly, behaving as if the result has a zero derivative:
619+
620+
.. code-block:: c++
621+
622+
non_differentiable double get_scaling_factor(double i, double j) {
623+
return i * j;
624+
}
625+
626+
double compute(double i, double j) {
627+
// get_scaling_factor will skip differentiation completely.
628+
return get_scaling_factor(i, j) + i * j;
629+
}
630+
595631
Specifying Custom Derivatives
596632
-------------------------------
597633

0 commit comments

Comments
 (0)