Skip to content

[AutoDiff] Enable variable debugging support for pullback functions. #36071

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

Merged
merged 1 commit into from
Feb 22, 2021

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Feb 21, 2021

  • Properly clone and use debug scopes for all instructions in pullback functions.
  • Emit debug_value instructions for adjoint values.
  • Add debug locations and variable info to adjoint buffer allocations.
  • Add TangentBuilder (a SILBuilder subclass) to unify and simplify special emitter utilities for tangent vector code generation. More simplifications to come.

Pullback variable inspection example:

(lldb) n
Process 50984 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x0000000100003497 main`pullback of foo(x=0) at main.swift:12:11
   9   	import _Differentiation
   10
   11  	func foo(_ x: Float) -> Float {
-> 12  	  let y = sin(x)
   13  	  let z = cos(y)
   14  	  let k = tanh(z) + cos(y)
   15  	  return k
Target 0: (main) stopped.
(lldb) fr v
(Float) x = 0
(Float) k = 1
(Float) z = 0.495846391
(Float) y = -0.689988375

Resolves rdar://68616528 / SR-13535.

@rxwei
Copy link
Contributor Author

rxwei commented Feb 21, 2021

TODO: Add SIL tests.

@rxwei
Copy link
Contributor Author

rxwei commented Feb 21, 2021

@swift-ci please smoke test

Copy link
Contributor

@dan-zheng dan-zheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design LGTM

@rxwei rxwei force-pushed the pullback-debuginfo branch 2 times, most recently from 59659cf to 8ac319a Compare February 21, 2021 23:34
@rxwei
Copy link
Contributor Author

rxwei commented Feb 21, 2021

@swift-ci please test and merge

- Properly clone and use debug scopes for all instructions in pullback functions.
- Emit `debug_value` instructions for adjoint values.
- Add debug locations and variable info to adjoint buffer allocations.
- Add `TangentBuilder` (a `SILBuilder` subclass) to unify and simplify special emitter utilities for tangent vector code generation. More simplifications to come.

Pullback variable inspection example:
```console
(lldb) n
Process 50984 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x0000000100003497 main`pullback of foo(x=0) at main.swift:12:11
   9   	import _Differentiation
   10
   11  	func foo(_ x: Float) -> Float {
-> 12  	  let y = sin(x)
   13  	  let z = cos(y)
   14  	  let k = tanh(z) + cos(y)
   15  	  return k
Target 0: (main) stopped.
(lldb) fr v
(Float) x = 0
(Float) k = 1
(Float) z = 0.495846391
(Float) y = -0.689988375
```

Resolves rdar://68616528 / SR-13535.
@rxwei rxwei force-pushed the pullback-debuginfo branch from 8ac319a to e3b480b Compare February 22, 2021 00:00
@rxwei
Copy link
Contributor Author

rxwei commented Feb 22, 2021

@swift-ci please test and merge

@swift-ci swift-ci merged commit a3671e6 into swiftlang:main Feb 22, 2021
@rxwei rxwei deleted the pullback-debuginfo branch February 22, 2021 03:54
Copy link
Contributor

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, couple of minor comments inline. The most interesting is probably the one about the test.

@@ -269,6 +272,10 @@ class ADContext {
Diag<T...> diag, U &&... args);
};

raw_ostream &getADDebugStream();
SILLocation getValidLocation(SILValue v);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more descriptive name than get_Valid_Location() we could use here? Or perhaps add a Doxygen comment explaining what this function does?

@@ -0,0 +1,202 @@
//===--- TangentBuilder.cpp - Tangent SIL builder ------------*- C++ -*----===//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: A C++ marker is only needed for .h files, where it's ambiguous whether it contains C or C++ code. A .cpp file always contains C++ code.

@@ -69,6 +69,7 @@ func foo(_ x: Float) -> Float {
// CHECK-SIL-LABEL: sil private [ossa] @fooTJpSpSr : $@convention(thin) (Float, @owned _AD__foo_bb0__PB__src_0_wrt_0) -> Float {
// CHECK-SIL: bb0([[DY:%.*]] : $Float, [[PB_STRUCT:%.*]] : @owned $_AD__foo_bb0__PB__src_0_wrt_0):
// CHECK-SIL: [[ADD_PB:%.*]] = destructure_struct [[PB_STRUCT]] : $_AD__foo_bb0__PB__src_0_wrt_0
// CHECK-SIL: debug_value [[DY]] : $Float, let, name "y"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to compile with -Xllvm -sil-print-debuginfo and check that we also attach the correct scope, particularly if this was by cloning another function. You could check that the scope chain points back to this function and not to the original function that was cloned here (all assuming this code is result of a clone action of course :-).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, this would allow you to check that the right location is attached, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants