Add an explanation of inout params to the impementation documentation

Skip to first unread message

Denis Petrov

Feb 10, 2022, 11:50:10 AM2/10/22
to Swift for TensorFlow, Dan Zheng
Could you please add a section in a part of inout params to Swift Differentiable Programming Implementation Overview (
Currently, I'm working on the issue I truly need an understanding of how it should work and be implemented in order to fix the bug.

Thanks, Denis.

shilpee gupta

Feb 11, 2022, 4:07:12 AM2/11/22
to Swift for TensorFlow, Denis Petrov, Dan Zheng
Changes I made in the file:
1) Explained typing rules for functions with inout parameters.
3) Added link to the pseudocode

I saw the bug that you are trying to resolve and I think the first error is that the value parameter should be preceded with an underscore. The second error I think is in the syntax of the outer function's return type.
Why i think this way is because:
Consider example: In this code, every inner function has 2 params of Int type and is returning an Int type. Hence the outer function if of type (String)->(Int,Int)->Int
func operate(symbol: String) -> (Int, Int) -> Int { // inner function to add two numbers func add(num1:Int, num2:Int) -> Int { return num1 + num2 } // inner function to subtract two numbers func subtract(num1:Int, num2:Int) -> Int { return num1 - num2 } let operation = (symbol == "+") ? add : subtract return operation } let operation = operate(symbol: "+") let result = operation(8, 3) print("Result:", result)

But in this code:
If the inner function is returning 2 params then the outer function should be written like
 func outerFunc( _ value: inout Float) -> (Float, Float) -> (Float, Float)

import _Differentiation func outerFunc(value: inout Float) -> (Float, (Float) -> (Float, Float)) { @differentiable(reverse, wrt: param) func innerFunc(param: Float, other: Float) -> Float { value += param return value * param * 2.0 } let valAndPullback = valueWithPullback(at: value, 2.0, of: innerFunc) return (value + valAndPullback.value, valAndPullback.pullback) }

Denis Petrov

Feb 11, 2022, 12:42:11 PM2/11/22
to Swift for TensorFlow,, Denis Petrov, Dan Zheng
Thank you, Shilpee, for your job! That's useful in terms of common inout behavior. But I expected differentiation-specific information, like what JVP/VJP function type will look like after transformation. What is the proper way to handle inout params/args specifically through the differentiation pipeline, etc? Anyway, I appreciate your efforts!

пятница, 11 февраля 2022 г. в 11:07:12 UTC+2,
Reply all
Reply to author
0 new messages