Skip to content

[AutoDiff][test] Use true and false literals instead of 1 == 1 and 1 == 0 #77760

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ struct MyModel: Differentiable {
property2 = localVar

// `false` may instead be any expression that returns a `Bool`.
// TODO: cannot use literal `false` because it crashes
if 1 == 0 {
if false {
localVar = member3
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ struct BatchNorm<Scalar>: Layer { // Crash requires conformance to `Layer`
@differentiable(reverse)
func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar> {
var offset = self.offset
// TODO: cannot use literal `true` because it crashes
if 1 == 1 { // Crash requires `if true`
if true { // Crash requires `if true`
offset += offset // Using `offset = offset + offset` stops the crash
}
return offset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ AddressOnlyTangentVectorTests.test("LoadableClassAddressOnlyTangentVector") {
@differentiable(reverse)
func conditional<T: Differentiable>(_ s: LoadableClass<T>) -> T {
var tuple = (s, (s, s))
// TODO: cannot use literal `false` because it crashes
if 1 == 0 {}
if false {}
return tuple.1.0.stored
}
expectEqual(.init(stored: 1), gradient(at: LoadableClass<Float>(10), of: conditional))
Expand Down
22 changes: 8 additions & 14 deletions test/AutoDiff/validation-test/control_flow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ ControlFlowTests.test("Conditionals") {

func cond4_var(_ x: Float) -> Float {
var outer = x
// TODO: cannot use literal `true` because it crashes
outerIf: if 1 == 1 {
outerIf: if true {
var inner = outer
inner = inner * x
// TODO: cannot use literal `false` because it crashes
if 1 == 0 {
if false {
break outerIf
}
outer = inner
Expand Down Expand Up @@ -388,9 +386,8 @@ ControlFlowTests.test("NestedConditionals") {
@differentiable(reverse, wrt: self) // wrt only self is important
func callAsFunction(_ input: Float) -> Float {
var x = input
// TODO: cannot use literal `true` because it crashes
if 1 == 1 {
if 1 == 1 {
if true {
if true {
// Function application below should make `self` have non-zero
// derivative.
x = x * w
Expand All @@ -408,9 +405,8 @@ ControlFlowTests.test("NestedConditionals") {
@differentiable(reverse, wrt: x)
func TF_781(_ x: Float, _ y: Float) -> Float {
var result = y
// TODO: cannot use literal `true` because it crashes
if 1 == 1 {
if 1 == 1 {
if true {
if true {
result = result * x
}
}
Expand Down Expand Up @@ -795,8 +791,7 @@ ControlFlowTests.test("ThrowingCalls") {
func testComplexControlFlow(_ x: Float) -> Float {
rethrowing({})
for _ in 0..<Int(x) {
// TODO: cannot use literal `true` because it crashes
if 1 == 1 {
if true {
rethrowing({})
}
rethrowing({}) // non-active `try_apply`
Expand All @@ -810,8 +805,7 @@ ControlFlowTests.test("ThrowingCalls") {
func testComplexControlFlowGeneric<T: Differentiable>(_ x: T) -> T {
rethrowing({})
for _ in 0..<10 {
// TODO: cannot use literal `true` because it crashes
if 1 == 1 {
if true {
rethrowing({})
}
rethrowing({}) // non-active `try_apply`
Expand Down