Add an explanation of inout params to the impementation documentation

46 views
Skip to first unread message

Denis Petrov

unread,
Feb 10, 2022, 11:50:10 AMFeb 10
to Swift for TensorFlow, Dan Zheng
Could you please add a section in a part of inout params to Swift Differentiable Programming Implementation Overview (https://docs.google.com/document/d/1_BirmTqdotglwNTOcYAW-ib6mx_jl-gH9Dbg4WmHZh0/edit?disco=AAAAVPa6o8Y)?
Currently, I'm working on the issue https://bugs.swift.org/browse/SR-15205. I truly need an understanding of how it should work and be implemented in order to fix the bug.

Thanks, Denis.


shilpee gupta

unread,
Feb 11, 2022, 4:07:12 AMFeb 11
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

unread,
Feb 11, 2022, 12:42:11 PMFeb 11
to Swift for TensorFlow, guptash...@gmail.com, 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, guptash...@gmail.com:
Reply all
Reply to author
Forward
0 new messages