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-9648] [AD] Incorrect activity analysis on side-effecting code #52092

Closed
rxwei opened this issue Jan 12, 2019 · 2 comments
Closed

[SR-9648] [AD] Incorrect activity analysis on side-effecting code #52092

rxwei opened this issue Jan 12, 2019 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. swift for tensorflow

Comments

@rxwei
Copy link
Member

rxwei commented Jan 12, 2019

Previous ID SR-9648
Radar None
Original Reporter @rxwei
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Swift for TensorFlow
Labels Bug
Assignee @dan-zheng
Priority Medium

md5: 59cb9000b1ca9f4ae77fbdc8221c0d86

Issue Description:

Original code:

var a: Float = 10

let _: @autodiff (Float) -> Float = { x in
  a = x
  return x + x
}

Raw SIL:

// closure #​1 in
sil private @$s13test_activityS2fcfU_ : $@convention(thin) (Float) -> Float {
// %0                                             // users: %4, %8, %8, %2
bb0(%0 : $Float):
  %1 = global_addr @$s13test_activity1aSfvp : $*Float // user: %3
  debug_value %0 : $Float, let, name "x", argno 1 // id: %2
  %3 = begin_access [modify] [dynamic] %1 : $*Float // users: %4, %5
  store %0 to %3 : $*Float                        // id: %4
  end_access %3 : $*Float                         // id: %5
  %6 = metatype $@thin Float.Type                 // user: %8
  // function_ref static Float.+ infix(_:_:)
  %7 = function_ref @$sSf1poiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %8
  %8 = apply %7(%0, %0, %6) : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %9
  return %8 : $Float                              // id: %9
} // end sil function '$s13test_activityS2fcfU_'

Activity info:

[AD] Activity info for $s13test_activityS2fcfU_ at (source=0 parameters=(0))
[ACTIVE] %0 = argument of bb0 : $Float                     // users: %4, %8, %8, %2
[USEFUL]   %1 = global_addr @$s13test_activity1aSfvp : $*Float // user: %3
[ACTIVE]   %3 = begin_access [modify] [dynamic] %1 : $*Float // users: %4, %5
[NONE]   %6 = metatype $@thin Float.Type                 // user: %8
[NONE]   // function_ref static Float.+ infix(_:_:)
  %7 = function_ref @$sSf1poiyS2f_SftFZ : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %8
[ACTIVE]   %8 = apply %7(%0, %0, %6) : $@convention(method) (Float, Float, @thin Float.Type) -> Float // user: %9
[AD] Cloning original @$s13test_activityS2fcfU_ to primal @AD__$s13test_activityS2fcfU___primal_src_0_wrt_0
[AD] Found static checkpoint   %3 = begin_access [modify] [dynamic] %1 : $*Float

The activity info is not correct: `%3` does not actually contribute to the return value of this function and thus is not active. This is because activity analysis is set to propagate USEFUL bottom up whenever it reaches a side-effecting operation. We shouldn't treat side effects in the environment as the function's result in activity analysis.

@rxwei
Copy link
Member Author

rxwei commented Jan 12, 2019

This is blocking differentiation of closures that capture mutable variables.

@rxwei
Copy link
Member Author

rxwei commented Jan 13, 2019

#21826

@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. swift for tensorflow
Projects
None yet
Development

No branches or pull requests

1 participant