Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-14234] [AutoDiff] Conditionally executed statement causes runtime crash instead of zero gradient when not executed #55013

Closed
porterchild opened this issue Apr 10, 2020 · 3 comments
Assignees
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@porterchild
Copy link

Previous ID SR-14234
Radar None
Original Reporter @porterchild
Type Bug
Status Resolved
Resolution Done
Environment

0.8 release

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AutoDiff
Assignee @BradLarson
Priority Medium

md5: 0d299597209d0d52d44fd099073b0ada

is blocked by:

  • TF-1008 SILOptimizer: differentiation transform support for zeroTangentVectorInitializer

Issue Description:

In the following code, S.c doesn't always contribute to the gradient. When it doesn't, instead of producing a zero gradient, it causes a runtime error.

Also, maybe it should be a separate bug, but even when 'condition' is true and S.c contributes to the gradient, the gradient is incorrect.

let condition = false //crash
let condition = true //no crash

struct S: Differentiable{
    var a: Float = 9
    var b: Float = 3
    var c: [Float] = [2.2]
    
    mutating func changeMyself(){
        a += b * 3
        c = c + [a] // when 'condition' is false -> Precondition failed: + should receive gradient with count equal to sum of operand counts, but counts are: gradient 0, lhs 1, rhs 1
        
        if condition{
            a += c.differentiableReduce(0, +)
        }
    }
}


func insertVerbHere(s: S) -> Float{
    var s = s
    s.changeMyself()
    return s.a
}


let grad = valueWithGradient(of: insertVerbHere)
print(grad(S()))

//Separate bug:

//when condition is true:
//prints (value: 38.2, gradient: S.TangentVector(a: 1.0, b: 0.0, c: []))

//but should be 
//(value: 38.2, gradient: S.TangentVector(a: 2.0, b: 6.0, c: [1.0]))
@porterchild
Copy link
Author

Just wondering if anyone has seen this bug yet, getting around it requires some expensive workarounds that make my code very slow.

@dan-zheng
Copy link
Collaborator

Thanks for reporting! At a quick glance, this seems like a dupe of TF-1005 (per-instance zero tangent vectors) - the issue description has a similar example (non-active struct member) with the same precondition.

I plan to look into TF-1005 soon! TF-1008 is the specific ticket that tracks fixing this issue.

@BradLarson
Copy link
Collaborator

I believe this has been fixed by PR #37861. When I build and run the above reproducer in both true and false cases, the application runs without a crash and produces the correct gradient.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants