On Correctness of Automatic Differentiation for Non-Differentiable Functions
Wonyeol Lee, Hangyeol Yu, Xavier Rival, Hongseok Yang
Introduction
Automatic differentiation or autodiff is one of the key technologies behind the dramatic progress of deep learning in recent years . It refers to the idea of developing and using a generic tool that can differentiate any function expressed as a program in a general-purpose programming language . Effective autodiff systems have been developed for popular programming languages . They have enabled the development of sophisticated models and algorithms in machine learning that, in particular, involve deep neural networks .
This paper is concerned with one seeming contradiction of these autodiff systems: the systems have originally been developed to compute derivatives of differentiable functions, but in practice, they are commonly applied to functions with non-differentiabilities. For instance, neural networks using ReLU define non-differentiable functions in general, but the derivatives of losses involving those functions are computed using autodiff systems in practice. This status quo raises a natural question: are autodiff systems correct in any formal sense when applied to such non-differentiable functions?
A common reaction to the question is: non-differentiabilities arising in deep learning (e.g., from ReLU) do not cause any issues because they occur rarely (i.e., they form a Lebesgue-measure-zero set). In the paper, we first show that this reaction needs to be carefully re-examined at least. Using counterexamples, we point out flaws in three often-used arguments derived from this reaction. We then present our answer. It is also positive, but based on a class of functions that satisfy a condition called piecewise analyticity under analytic partition (in short, PAP). These PAP functions include nearly all (possibly non-differentiable) functions in deep learning nowadays. For these PAP functions, we propose a new type of derivatives, called intensional derivatives, and prove that these derivatives always exist and coincide with standard derivatives for almost all inputs. These intensional derivatives behave almost as well as, and sometimes even better than, usual derivatives for differentiable functions. For instance, they always satisfy a chain rule even if functions are non-differentiable. Using these properties of intensional derivatives, we show that the intensional derivatives are what most autodiff systems compute or try to compute essentially. In this way, we formally establish the correctness of autodiff systems that compute derivatives of non-differentiable functions.
Challenges
As mentioned in the introduction, practitioners frequently apply autodiff systems to functions with non-differentiabilities, and justify these out-of-scope use cases with plausible yet heuristic arguments. In this section, we analyse these arguments. We go through three claims that are often used in the arguments implicitly, and show that although looking innocent at the outset, the claims have serious flaws; they are wrong, and we provide counterexamples.
Recall a notion of correctness for an autodiff system covering non-differentiable functions :
The definition permits a non-differentiable function as an input to an autodiff system, as long as its non-differentiability occurs rarely (i.e., at a measure-zero subset of the input domain). For such a function, it may be impossible to compute the correct derivative for all inputs, simply because the derivative does not exist for inputs where the function is non-differentiable. Thus, the definition just requires that the system should compute the correct derivative for most inputs instead (i.e., for a subset of the input domain whose complement inside the domain is contained in a measure-zero set).
Proving the correctness of an autodiff system is surprisingly difficult. Nearly every autodiff system is based on a chain rule for computing the derivative of function composition, but when the component functions are non-differentiable, designing a correct version of the rule is challenging. To help the reader see this challenge, we analyse three plausible yet flawed claims about the derivative of function composition, which are sometimes used implicitly in heuristic justifications of autodiff systems.
If and are differentiable almost everywhere and continuous, then should be differentiable almost everywhere.
A rationale for the claim goes as follows. In order for to be non-differentiable at , the function has to be non-differentiable at , or it should map to a non-differentiable input to and be able to vary enough in a neighbourhood of . The claim says that such an is rare (from the perspective of measure). Of course, the first case that is non-differentiable at occurs rarely by assumption. The second case seems to happen rarely as well, because the non-differentiable inputs to are rare and is continuous: because of continuity, if maps many ’s (i.e., all the in some non-measure-zero set) to those rare non-differentiable inputs of , it should behave as a constant function in the neighbourhoods of most of those ’s.
The rationale has a flaw, and the claim is false. The inputs falling into the second case are not necessarily rare. Although is continuous, it is possible that maps many ’s to some of those rare non-differentiable inputs of without acting as a constant function in a neighbourhood of each of those ’s. The precise result is summarised in the following proposition:
There exist functions and such that and are differentiable almost everywhere and continuous, but fails to be almost-everywhere differentiable.
Returning to the proof, let be the inverse of the homeomorphism defined by [8, Example 2.3.1]. It is known that [8, Example 2.3.2]. Construct based on the construction of as follows, similarly to [18, Example 8.18]: at each step , define over the -th open interval to be removed, as (); and define over as . See Figure 1 for the graphs of , , and constructed so far. Clearly, is continuous. Also, is continuous, since the height of the parabolas defined at the -th step of ’s construction converges to as , and (see Appendix A for the details). Hence, is continuous. Note that is even Lipschitz continuous. To prove this, observe that can be constructed similarly to , and repeat the proof of the Lipschitz continuity of .
We now show that and are differentiable almost everywhere, but is not. First, since is Lipschitz continuous, it is differentiable almost everywhere by Rademacher’s theorem [41, Theorem 2.2.4]. Next, since is differentiable on by its construction, it is differentiable almost everywhere (as has measure ). Lastly, is non-differentiable on , which has measure , due to that: the parabolas defined at the -th step of ’s construction get sharper as ; ; and is a homeomorphism with (see Appendix A for the details). ∎
If , , and are differentiable almost everywhere and continuous, then the standard chain rule for should hold almost everywhere. In particular, if , then for almost all .
Note that all of , , and in the claim are assumed to be differentiable almost everywhere. The claim comes from heuristic reasoning that if we just avoid those rare non-differentiable inputs of , we should be able to use the standard result for differentiation, including the chain rule.
The second claim is also wrong. The flaw in the heuristic reasoning from above is that the almost-everywhere differentiability of , , and does not stop from being undefined for many ’s in . This is related to the flaw in the justification for the first claim that we explained. The next proposition and its proof provide a concrete example for this phenomenon:
There exist functions such that , , and are differentiable almost everywhere and continuous, but it is not that is defined for almost all .
Let and . Then, . Certainly, , , and are differentiable almost everywhere and Lipschitz continuous. But, is not differentiable at for all . So, it is not that is defined for almost all . ∎
The third claim is a natural reaction to the failure of the second claim. It implements the strategy of making the chain rule in the second claim more permissive such that the counter argument of Proposition 2 no longer applies. The claim expresses a weaker version of the rule that allows one to set the derivatives of and to arbitrary values wherever and are not differentiable.
The functions and in the claim are the extensions of and that set and to arbitrary values whenever and are undefined. The chain rule in the claim is phrased in terms of these extensions and , so that it does not suffer from the problem pointed out in Proposition 2. However, this new rule is still flawed as shown in the next proposition:
There exist functions such that , , and are differentiable almost everywhere and continuous, but for some measurable subset with non-zero measure, they satisfy the following property: and for all .
Consider the function in the proof of Proposition 1. Let be the -Cantor function . Then, is the -Cantor function . We already showed is differentiable almost everywhere and even Lipschitz continuous. Since and are monotone on , they are differentiable almost everywhere by the monotone differentiation theorem [41, Theorem 1.6.25]; and they are clearly continuous. We now show there exists with the desired properties. Since has measure , it suffices to prove and for almost all . The claim indeed holds due to the following: and are Lipschitz, so absolutely continuous; and for all ; and for all whenever these derivatives exist; and has measure . For the details, see [8, Example 2.3.2] and . ∎
The proposition implies the third claim is doomed. The claim says that and for almost all . But both equalities cannot hold simultaneously: if they do, by Proposition 3, but the same proposition also entails , leading to a contradiction.
A lesson from these flawed claims is that although the notion of correctness in Definition 1 only refers to almost-everywhere differentiability, we need a condition stronger than it, which behaves better in handling function composition and gives rise to a chain rule. We describe such a condition next.
PAP Function and Intensional Derivative
Our justification of autodiff systems relies on two key concepts: piecewise analyticity under analytic partition, and intensional derivative. The first is a (strictly) stronger property about functions than almost-everywhere differentiability, and yet it is satisfied by practically all programs targeted at by existing autodiff systems, as we will show in §4. Functions with this new property, called PAP functions, have an unusual type of derivatives, called intensional derivatives, which form the second concept. Intensional derivatives of PAP functions are defined everywhere and satisfy a chain rule, while still agreeing with standard derivatives for almost all inputs. In fact, the PAP functions have not just first-order but also all higher-order intensional derivatives. In §4, we will show that most autodiff systems compute intensional derivatives when applied to functions with non-differentiabilities.
To expand the overview of the two concepts just given, we need a notion of piecewise representation:
A representation from to is piecewise analytic under analytic partition (in short, PAP) if and only if is an analytic partition of and is analytic over its domain for all .
The definitions identify PAP representations and PAP functions as those built by the two-step process: we first split the input domain such that boundaries of the split regions are expressed by the zero sets of analytic functions, and next choose an appropriate analytic function for each piece of the split. Note the use of analytic functions in both steps. Thus, just like the standard analyticity, the PAP property implies almost-everywhere differentiability (Proposition 4), but not vice versa (Proposition 5).
All PAP functions are differentiable almost everywhere.
The proof extends the one for a similar result in . The key idea is to use the fact that the zero set of a non-constant analytic function over a connected open domain has measure zero . To prove the proposition, we show that for each PAP function , there exist countably many non-constant analytic functions over connected open domains such that if is non-differentiable at , then belongs to the zero set of some . For the details, see Appendix B.3. ∎
There is a continuous almost-everywhere differentiable yet non-PAP function.
Nearly all the requirements for to be a PAP representation directly follow from the fact that is PAP. The only exception is the analyticity of . There we use the fact that the operation of taking a (standard) partial derivative of a function preserves analyticity [25, Proposition 2.2.3]. ∎
By Proposition 6, only PAP functions live in . Thus, we can also take intensional derivatives of functions in . We push this observation further and define higher-order intensional derivatives:
The first claim is proven similarly to Proposition 4, except that we additionally use the following: an analytic function is infinitely differentiable. As in Proposition 4, we prove a stronger statement: there exist countably many non-constant analytic functions over connected open domains such that for all , if the -th order standard derivative of is not defined at , then is in the zero set of some . Next, consider the second claim. Its current form is not strong enough to enable inductive proofs. We instead prove a stronger statement by induction on : for each , there exist countably many non-constant analytic functions over connected open domains such that the -th order standard derivative of is well-defined, and agrees with , at all those inputs not in the zero sets of . For the details, see Appendices B.3 and B.4. ∎
Since and are PAP, they have PAP representations and . Define their composition as follows: where . Then, is a representation of . Also, it is PAP as the composition of analytic functions is analytic [25, Proposition 2.2.8]. Thus, is PAP. ∎
We next use these properties to show that existing autodiff systems compute intensional derivatives.
Correctness of Autodiff Systems
Consider a simple programming language that assumes real-valued input variables and has the following syntax for programs:
\begin{array}[]{@{}c@{}}\llbracket{c}\rrbracket v=c,\qquad\llbracket{x_{i}}\rrbracket v=v_{i},\qquad\llbracket{\accentset{\rule{2.79996pt}{0.8pt}}{\mathtt{f}}(e_{1},\ldots,e_{n})}\rrbracket v={\mathtt{f}}(\llbracket{e_{1}}\rrbracket v,\ldots,\llbracket{e_{n}}\rrbracket v),\\[3.00003pt] \llbracket{\mathtt{if}~{}(e_{1}>0)~{}e_{2}~{}e_{3}}\rrbracket v=\text{if }(\llbracket{e_{1}}\rrbracket v>0)\text{ then }\llbracket{e_{2}}\rrbracket v\text{ else }\llbracket{e_{3}}\rrbracket v.\end{array}
\begin{array}[]{@{}c@{}}\llbracket{c}\rrbracket^{\nabla}\;\!\!v=\vec{0}_{1\times N},\quad\llbracket{\accentset{\rule{2.79996pt}{0.8pt}}{\mathtt{f}}(e_{1},\ldots,e_{n})}\rrbracket^{\nabla}\;\!\!v=(\mathchoice{\accentset{\displaystyle\text{\smash[b]{\raisebox{-6.24301pt}{\widetildesym}}}}{D}}{\accentset{\textstyle\text{\smash[b]{\raisebox{-6.24301pt}{\widetildesym}}}}{D}}{\accentset{\scriptstyle\text{\smash[b]{\raisebox{-4.37012pt}{\widetildesym}}}}{D}}{\accentset{\scriptscriptstyle\text{\smash[b]{\raisebox{-3.1215pt}{\widetildesym}}}}{D}}\mathtt{f})(\llbracket{e_{1}}\rrbracket v,\ldots,\llbracket{e_{n}}\rrbracket v)\cdot[\llbracket{e_{1}}\rrbracket^{\nabla}\;\!\!v;\,\ldots;\,\llbracket{e_{n}}\rrbracket^{\nabla}\;\!\!v],\\[3.00003pt] \llbracket{x_{i}}\rrbracket^{\nabla}\;\!\!v\,{=}\,[\vec{0}_{(i-1)\times 1};\vec{1}_{1\times 1};\vec{0}_{(N-i)\times 1}]^{\top}\!\!,\ \;\llbracket{\mathtt{if}\,(e_{1}{>}0)\,e_{2}\,e_{3}}\rrbracket^{\nabla}\;\!\!v\,{=}\,\text{if}\,(\llbracket{e_{1}}\rrbracket v{>}0)\,\text{then}\,{\llbracket{e_{2}}\rrbracket^{\nabla}\;\!\!}v\text{ else}\,{\llbracket{e_{3}}\rrbracket^{\nabla}\;\!\!}v.\end{array}
If \mathchoice{\accentset{\displaystyle\text{\smash[b]{\raisebox{-6.24301pt}{\widetildesym}}}}{D}}{\accentset{\textstyle\text{\smash[b]{\raisebox{-6.24301pt}{\widetildesym}}}}{D}}{\accentset{\scriptstyle\text{\smash[b]{\raisebox{-4.37012pt}{\widetildesym}}}}{D}}{\accentset{\scriptscriptstyle\text{\smash[b]{\raisebox{-3.1215pt}{\widetildesym}}}}{D}}\mathtt{f}\in\partial_{\bullet}{{\mathtt{f}}} for all primitive functions , then for all programs .
Related Work and Discussion
Autodiff has a long history with a large body of literature . Its community has been aware of some issues with non-differentiable functions [20, Chapter 14]. These issues have become ever more important, as autodiff has been increasingly applied to a variety of non-differentiable functions, including sophisticated linear algebra functions . In this paper, we investigate the issues in a more systematic and rigorous way, by presenting non-trivial concrete counterexamples that illuminate subtleties of non-differentiable functions in autodiff, and also proposing intensional derivatives, a new notion of derivatives, that enable us to formally prove the correctness of, and better understand the behaviour of, autodiff systems applied to non-differentiable functions.
Recently and concurrently with this work, Bolte and Pauwels studied some concepts and results similar to ours. They proposed a new class of functions and a new notion of derivatives, called elementary selections and selection derivatives, which roughly correspond to our PAP functions and intensional derivatives; and proved properties of those new functions and derivatives, which roughly correspond to our Propositions 8, 9, 10, 11 and 12. Although having some similarities, our work and their work have three key differences, complementing each other. First, their work is applicable to a strictly smaller class of functions than ours, as any elementary selection is PAP (and locally Lipschitz) but not vice versa. Second, it considers selection derivatives of first order only, whereas our work considers intensional derivatives of higher orders as well. Third, their work provides some results not in our work (e.g., convergence of stochastic gradient descent with selection derivatives), and vice versa (e.g., the results in §2).
Broader Impact
This work focuses mainly on theoretical aspects of autodiff systems. In particular, we formally prove that the systems, though developed to handle differentiable functions, remain correct even when applied to non-differentiable functions. Our result justifies, at least in part, the current situation in machine learning, in which the systems are frequently applied to non-differentiable functions without much consideration to their correctness under such out-of-scope use cases. Other than the justification, this work does not present any other foreseeable societal consequence due to its theoretical nature.
Acknowledgments and Disclosure of Funding
We thank anonymous reviewers for their insightful and constructive comments. Lee, Yang, and Yu were supported by the Engineering Research Center Program through the National Research Foundation of Korea (NRF) funded by the Korean Government MSIT (NRF-2018R1A5A1059921), and also by Next-Generation Information Computing Development Program through the National Research Foundation of Korea (NRF) funded by the Ministry of Science, ICT (2017M3C4A7068177). Rival was supported by a Facebook gift and by the European Research Council (ERC) under the European Union’s Horizon 2020 research and innovation programme (grant agreement No 825492).
References
Appendix A Comments on Results in §2
First, we elaborate on our proof in Proposition 1 that is continuous on . Since is continuous on by its construction, we only need to show that is continuous on . Consider any and . It suffices to show that there is such that
Let be an integer with . Consider the set
By the construction of , is the union of some finitely many closed intervals in that do not contain . (Note that each of those closed intervals is contained in an open interval removed at some -th step of ’s construction.) Hence,
is positive. We now show that satisfies (1). Consider any with . If , then . If , then and by the definition of , and thus by the definition of and . Hence, (1) holds and this completes the proof. ∎
Second, we elaborate on our proof in Proposition 1 that is not differentiable on . Consider any . It suffices to show that for any , there exist such that
Consider any . Since is a limit point of , there exists with . For this , the first equality in (2) holds, since . To find , let be an integer such that
We claim that there exists such that
and thus the second equality in (2) holds. Hence, finding satisfying (4) completes the proof. We now show that such exists. Consider the situation right after the -th step of ’s construction is performed. Then, the total length of the closed intervals that still remain is
so the length of each of those closed intervals is , since those closed intervals have the same length and there are such intervals. Due to this, and by the construction of , there is some open interval that is removed exactly at the -th step of ’s construction and satisfies
Let be the midpoint of . By the construction of and , we have . Furthermore, since the length of is , we have
Hence, satisfies (4), and this concludes the proof. ∎
Next, we make a remark on non-differentiable inputs of , , and in the proof. One might guess that should be non-differentiable exactly on , given that maps onto in a linear way and maps onto in a non-smooth-looking way. Surprisingly, the guess is wrong: is in fact non-differentiable only on a measure-zero subset of . On the other hand, and are non-differentiable exactly on and , respectively. The proof that is non-differentiable on is similar to the above proof that is non-differentiable on , and thus we omit it.
Finally, we connect the examples in the proof with our results in §3. Both and are shown to be non-PAP (Proposition 5). Hence, our results do not guarantee that is PAP and so almost-everywhere differentiable. In fact, is non-PAP, since is not almost-everywhere differentiable.
A.2 Comments on the proof of Proposition 2
We explain how the counterexample in the proof does not contradict to our results in §3. The functions and are PAP (and thus is so). Although is undefined at , we can extend it to an intensional derivative such that is defined everywhere (even at ) and coincides with at all but countably many inputs. With such , the following version of the chain rule holds almost everywhere:
This is because we have the chain rule for intensional derivatives and and these intensional derivatives and standard derivatives coincide almost everywhere (Propositions 10 and 8).
A.3 Comments on the proof of Proposition 3
The functions , , and in the proof do not contradict to our results. Neither nor is a PAP function (Proposition 5). Hence, our results do not guarantee the validity of our version of the chain rule for .
Appendix B Comments on and Proofs for Results in §3
We prove the following argument used in the proof of Proposition 5: the functions listed in the proof satisfy the sufficient condition (i) or (ii) mentioned in the proof.
has positive measure. For the -Cantor function with , is a full measure subset of due to the following: for almost all ; for all ; and has no interior. Since has measure , the claim holds for . For in the proof of Proposition 1, is a full measure subset of due to similar reasons. So the claim holds for . For Volterra’s function, is known to have positive measure [18, Example 8.35]. So the claim holds for Volterra’s function.
B.2 Interior and subinterior of analytic partition
Then, is a finer partition of than . That is, is a partition of , and for all , for some .
We call the set a subinterior of , and the partition a subanalytic partition of . We use to denote the set of all subinteriors of .
is contained in some measure-zero set.
is contained in some measure-zero set.
Then, is a subinterior of , and is a subanalytic partition of , because of the following:
is a partition of .
is a countable set.
For all and , is subanalytic.
is a finer partition of than . This holds because is a partition of , and is a partition of for all , by its construction.
This completes the proof that .
We now prove the remaining claims. Let and be a subanalytic partition of that satisfies the equations in Definition 10.
Since each is analytic, and not everywhere-zero, on its connected open domain (by the definition of subanalytic partition), the above theorem and equation imply that is contained in some measure-zero set. Since any countable union of measure-zero sets has measure zero, is contained in some measure-zero set.
Proof of (d). This follows immediately from (b) and (c). ∎
B.3 Proofs of Proposition 4 and Proposition 8 (part I)
We remind the reader that the notation means the standard derivative of at .
Let be a PAP representation from to . The interior and subinterior of are defined by:
where denotes the -time composition of the operator .
B.4 Proof of Proposition 8 (part II)
Let and be PAP functions, and and be their PAP representations. If for all , then
where both sides are well-defined for each .
Let be a PAP function. Then, for any intensional derivative , there exists a PAP representation of such that
Let be a representation of a function from to , and be a partition of . The refinement of with is defined by:
Moreover, for any representation of a function from to , the refinement of with is defined by:
Let . Since is PAP and is an analytic partition, is an analytic partition. Also, since is countable, is also countable. Thus, is PAP. Since
is a representation of . Finally, we obtain the last claim as follows:
For the second equality, we use the following fact: for any . ∎
The proof proceeds by induction on . For , we have . So any PAP representation of satisfies the claim. Now suppose . By the definition of , there exists such that . We construct the desired PAP representation as follows. First, focus on . By the induction hypothesis on for , there exists a PAP representation of such that
The claim follows from Lemma 21 and the following: and described in Lemma 21 have the full measure in , by Lemma 21 and Lemma 14(d). ∎