Skip to content

Fix stack overflow containsTensorFlowValue for recursive data structures. #21449

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 5 commits into from
Dec 30, 2018

Conversation

bgogul
Copy link
Contributor

@bgogul bgogul commented Dec 20, 2018

The utility function containsTensorFlowValue fails with a stack overflow error when it is invoked on a recursive data structure such as PairedStructure struct in Prototypes/PatternMatching.swift test.

This PR simply breaks cycles by keeping track of the parent decls.

@bgogul
Copy link
Contributor Author

bgogul commented Dec 20, 2018

@swift-ci please test tensorflow

@bgogul bgogul requested a review from mhong December 20, 2018 01:05
@bgogul bgogul changed the title [WIP] Fix stack overflow containsTensorFlowValue for recursive data structures. Fix stack overflow containsTensorFlowValue for recursive data structures. Dec 20, 2018
Copy link

@mhong mhong left a comment

Choose a reason for hiding this comment

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

Interesting finding! Some thoughts / suggestions:

  1. can you create a stand-alone unit test (which deals with tensors) that exposes this problem?

From the cited example:

struct PairedStructure<I: Comparable> {
  let bounds: Range<I>
  let subStructure: [PairedStructure<I>]
}

subStructure is not itself a struct, and given https://stackoverflow.com/questions/36080491/swift-struct-type-recursion, i wonder if we should tweak the impl to avoid descending into a non-struct type, such as array?

  1. i'm also wondering if we could avoid calling structContainsTensorFlowValue() on structs like PairedStructure, which are not related to tensors / TF. As a general principle, can we avoid triggering any of our S4TF related code, when user code involves no tensors (the "pay only for what you use" principle in language/system design and impl).

@bgogul
Copy link
Contributor Author

bgogul commented Dec 21, 2018

can you create a stand-alone unit test (which deals with tensors) that exposes this problem?

I will try to create one. I think I will have to create the Struct explicitly in the test.

i wonder if we should tweak the impl to avoid descending into a non-struct type, such as array?

Shouldn't we return true if the field is an array of tensors?

on structs like PairedStructure, which are not related to tensors / TF.

Don't we want to know if a struct has fields of Tensors? e.g. Parameters?

@mhong
Copy link

mhong commented Dec 21, 2018

I played with this issue and understood it better now. The test I used is:

struct S {
  let sub: [S] = []
}

func foo() -> S {
  let s = S()
  return s
}

public func bar() {
  _ = Tensor<Float>(1.0)
  _ = foo()
}

In this case the sub field has type:

(array_slice_type
  (struct_type decl=tmp4.(file).S@test/TensorFlow/tmp4.swift:41:8))

So I agree this is a real issue. The fix looks fine. Another option that might be worth considering is extending TypeContainsTensorFlowValue::declContainsTensorFlowValue to also remember which decls / types do not contain tensorflow value. This way hopefully we can hide the info in parentDecls from the function interfaces like containsTensorFlowValueImpl(). Please assess and you can decide.

Following up on another point that was raised earlier -- I'm still curious how this issue got triggered for PairedStructure. I can understand how it's triggered in foo() / bar() above since they have tensor code.

Can you confirm that we will not be calling containsTensorFlowValue() unnecessarily (i.e., when we can decide from the code context that there is no tensor code)? In particular, the existing Swift unit tests (that also run in apple master branch) should hopefully not involve containsTensorFlowValue()?

@bgogul
Copy link
Contributor Author

bgogul commented Dec 29, 2018

@swift-ci please test tensorflow

@bgogul bgogul merged commit 0e7438e into swiftlang:tensorflow Dec 30, 2018
@bgogul bgogul deleted the break_cycles branch January 2, 2019 20:55
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.

2 participants