-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-13698] Conditionally conform Optional to Differentiable #53072
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
Comments
Comment by Molly Beavers (JIRA) Started working on this. |
Updated the issue with a prototype - sorry for not updating earlier. |
If the |
Comment by Molly Beavers (JIRA) I am still trying to figure this one out, though it has been two weeks so I thought I should give an update by at least this point. I added the prototype code to Optional.swift and went to add tests in order to see what might need to be changed. I sort of copied how arrays(9c349704123fa4f2a783cafef0251e7299a1273b) and enums were tested below OptionalTests.test("Optional Dense") {
struct optDense : Differentiable {
var w1: Float?
@differentiable
func callAsFunction(_ x: Float) -> Float {
if let w1 = w1 {
return x * w1
}
return x
}
}
let dense=optDense(w1: 5)
let grad = gradient(at: 3, in: { x in dense(x) })
expectEqual(5.0,grad) //expected failure
} The above fails with "differentiating enum values is not yet supported" I then tried to follow the way enums were tested in TF-582 OptionalTests.test("Optional Dense") {
struct optDense : Differentiable {
var w1: Float?
@differentiable
func callAsFunction(_ x: Float) -> Float {
if let w1 = w1 {
return x * w1
}
return x
}
}
let grad = optDense(w1: 4).gradient(at: 2, in: { dense, x in dense(x) })
expectEqual(0,1)
} This fails with: I have also rebuilt swift/tensorflow quiet a few times now from scratch, so I do not believe it is a version issue. I have tried a bit more than this, but I always seem to come back to these two issues. I have since been looking at SilGen and my next plan is to ask on the fastai forum to see if anyone else has run into similar issues. If I am making an obvious mistake is there a resource I can go to learn more in this area? Currently using most recent code from swift/tensorflow. Can upload docker image if needed. |
Differentiating through enum switches is not possible yet because differentiating data-dependent control flow is not yet supported. So even if there's a fully correct The crasher you encountered is a bug in the compiler. The |
Comment by Molly Beavers (JIRA) Thank you for the help! I was stuck because I had a misunderstanding on the amount I should be expecting to change the prototype. Knowing that I can make significant changes allowed me to try a lot more. "The public enum DifferentiableView : Equatable, AdditiveArithmetic, Differentiable {
case none
case some(Wrapped.TangentVector) should instead be(Code #2)?: public struct DifferentiableView : Differentiable {
private var _base: Optional<Wrapped.TangentVector> //very similar pattern to Array.DifferentialView I also tried(Code #3), Though I found that this did not work as Wrapped does not have to conform to Additive arithmetic, and therefore Wrapped is not guaranteed to have a valid zero. public struct DifferentiableView : Differentiable {
private var _base: Optional<Wrapped> //exactly like Array.DifferentialView My current .zero code. (Code #4) public static var zero: Optional<Wrapped>.DifferentiableView {
let zero:Wrapped.TangentVector? = Wrapped.TangentVector.zero //Wrapped.zero not guaranteed
return Optional.DifferentiableView(zero)
) I am currently working with the approach in (Code #2) and having results as I am able to test Differential view against AdditiveArithmetic tests before turning on Differentiation. After going through and switching this to a struct I have found that "Assertion `Outputs.back().getType() == SGF.getSILType(result)' failed." still happens. Therefore I think you may have meant that (Code #1) should have instead been: public enum DifferentiableView : Equatable, AdditiveArithmetic, Differentiable {
case some(Wrapped) So I am going to try and convert everything tomorrow. |
Comment by Molly Beavers (JIRA) Tried converting everything today, but not much luck as of yet. Unsure on how to debug compiler issues, is there any topics I can lookup to get started on learning? Still running into the same "Assertion `Outputs.back().getType() == SGF.getSILType(result)' failed." issue. Continuing to attempt to find a way around it. Keeeping code here: https://github.com/marii-moe/swift/tree/opt-scratch |
Comment by Molly Beavers (JIRA) Tried having the DifferentialView wrap a Optional<Wrapped>, and ran into the same compiler bug. Currently attempting trying to debug the compiler referencing this: https://github.com/apple/swift/blob/master/docs/DebuggingTheCompiler.rst#debugging-the-swift-compiler Not really familiar with C++, but I can hopefully at least be able to figure out more about the problem. |
Comment by Molly Beavers (JIRA) Upon checking SILGen I found that the issue seems to be that they are comparing the value to a pointer. $*optDense.TangentVector Going to continue looking at this... |
Comment by Molly Beavers (JIRA) Still having issues getting debugging working. Seems SIGTRAP is being caught, so breakpoints are not working. Trying to figure out what to do here. |
Comment by Molly Beavers (JIRA) After looking through this a bit more I wanted to double check on something. https://github.com/apple/swift/blob/tensorflow/stdlib/public/core/Array.swift extension Array.DifferentiableView : AdditiveArithmetic
where Element : AdditiveArithmetic {
public static func +
public static func -
} I have been struggling trying to determine if there was a way to do it with only Wrapped.TangentVector conforming to additive Arithmetic, to allow things like Optional<Optional<Wrapped>>. I figured this is why Wrapped did not conform to AdditiveArithmetic in the prototype. I am going to now work with the assumption that Wrapped conforming to AdditiveArithmetic is okay. So, is it okay if Wrapped conforms to additive Arithmetic? |
Comment by Molly Beavers (JIRA) I now have something that looks like it is mostly working. Currently requesting access to the presentation today, because it went over what an "active Differentiable" means, which if I remember correctly looks like might be the issue with my tests. |
Hi Molly (JIRA User), I'm so sorry for the late reply - thanks for investigating this issue! Support for differentiating enums isn't quite robust (TF-583) and differentiation of enums hasn't been tested (it's not expected to work yet). I saw that you encountered some of these issues (e.g. Please see the " |
Comment by Molly Beavers (JIRA) @dan-zheng Thank you! I am going to start a thread and update my code according to that document. I had previously been updating my code by referencing other code in the repository. I has skimmed the document previously, but did not realize the importance of it. I can always update the code whenever active enums are supported. |
Thank you Molly (JIRA User), I look forward to your thread 🙂 |
Comment by Molly Beavers (JIRA) Was not able to get past the compiler errors, so dropping this ticket as I do not think I am a good fit. |
Comment by arnoegw (JIRA) +1 to Differentiable Optionals: they would have helped me today in expressing ResNet-style "add conv 1x1 if needed to align depths". |
Comment by Mustafa YALCIN (JIRA) 3 |
12 similar comments
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
3 similar comments
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Conditional conformance of Thanks Molly (JIRA User) for helping to explore this direction! |
Comment by Mustafa YALCIN (JIRA) 3 |
14 similar comments
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Comment by Mustafa YALCIN (JIRA) 3 |
Additional Detail from JIRA
md5: 13b8627652d2f04d11be34db0a4c54ae
Parent-Task:
blocks:
is duplicated by:
Optional
conditionally conform toDifferentiable
Issue Description:
Optional
can conditionally conform toDifferentiable
when theWrapped
type does.Changes should be made in
Optional.swift
.Prototype:
Optional
has a separateTangentVector
type calledDifferentiableView
type, similar toArray.DifferentiableView
. This is important to avoid conformingOptional
toAdditiveArithmetic
.Note that differentiation of active
Optional
values is blocked by TF-583 (support for active enum values).The text was updated successfully, but these errors were encountered: