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-14168] Memory Leak - related to array differentiation #56071

Open
swift-ci opened this issue Sep 30, 2020 · 4 comments
Open

[SR-14168] Memory Leak - related to array differentiation #56071

swift-ci opened this issue Sep 30, 2020 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-14168
Radar rdar://problem/74094400
Original Reporter chip1967 (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, RunTimeCrash
Assignee @marcrasi
Priority Medium

md5: 0fdc56ed15e7cccc0a38c25e4e879e4c

Issue Description:

I have been having great trouble diagnosing a memory leak issue related to Conv3D and Arrays. The following code is the best short example I can generate.

the following code run as "main.swift" using

/Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2020-09-16-a.xctoolchain/usr/bin/swift main.swift

fails on OSX with both Development and Release tool chain 0.11.

Will produce segmentation fault with output included below

// main.swift

import Foundation
import TensorFlow


extension Array: Module where Element: Layer, Element.Input == Element.Output {
    public typealias Input = Element.Input
    public typealias Output = Element.Output


    @differentiable(wrt: (self, input))
    public func callAsFunction(_ input: Input) -> Output {
        return self.differentiableReduce(input) { $1.callAsFunction($0) }
    }
}
extension Array: Layer where Element: Layer, Element.Input == Element.Output {}


public struct MyLayer<Scalar: TensorFlowFloatingPoint>: Layer
{
    public typealias Input = Tensor<Scalar>
    public typealias Output = Tensor<Scalar>
    
    var conv1: Conv3D<Scalar>
    @noDerivative
    let conv2: Conv3D<Scalar>
    @noDerivative
    let batchNorm2: BatchNorm<Scalar>
    @noDerivative
    let activation2 : Function<Tensor<Scalar>, Tensor<Scalar>>


    public init(kernel: (Int,Int,Int), channels: Int) {
        let filter = (kernel.0, kernel.1, kernel.2, channels, channels)
        self.conv1 = Conv3D(filterShape: filter,
                            padding: Padding.same,
                            useBias: false,
                            filterInitializer: zeros())
        self.conv2 = Conv3D(filterShape: filter,
                            padding: Padding.same,
                            useBias: false,
                            filterInitializer: zeros())
        self.batchNorm2 = BatchNorm<Scalar>(featureCount:channels, axis:-1)
        self.activation2 = Function(relu)
    }
    
    @differentiable
    public func callAsFunction(_ input: Input) -> Output {
        return self.conv1(input)
    }
}


if true {
    let myL = [ MyLayer<Double>(kernel: (2,2,3), channels: 1) ]
    let policy2 = myL 
    let shape   = TensorShape([100, 3, 3, 3, 1])
    let encoding = Tensor<Double>.init(zeros: shape) 
    let (_, _) = valueWithPullback(at: policy2, in: { $0(encoding) })
}

Program output is

Program arguments: /Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2020-09-16-a.xctoolchain/usr/bin/swift-frontend -frontend -interpret main.swift -enable-objc-interop -sdk /Applications/Xcode-12.2-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -color-diagnostics -target-sdk-version 11.0 -module-name main 
1.  Swift version 5.3-dev (LLVM 14a10d635b229fb, Swift c08d0804e2e44ac)
2.  While running user code "main.swift"
0  swift-frontend           0x00000001105d30f5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift-frontend           0x00000001105d2365 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x00000001105d36c6 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007fff6f2615fd _sigtramp + 29
4  libsystem_malloc.dylib   0x00007fff6f2200c6 szone_malloc_should_clear + 66
5  libsystem_malloc.dylib   0x0000000118f46d48 szone_malloc_should_clear + 18446603343365303492
6  libswiftCore.dylib       0x0000000117514676 $sSass14DifferentiableRzlE04_vjpA6Reduceyqd__5value_Sa0A4ViewVy13TangentVectorQz_G_AFQyd__tAIc8pullbacktqd___qd__qd___xtXFtsAARd__lF + 998
7  libswiftCore.dylib       0x0000000117514ca3 AD__$sSass14DifferentiableRzlE20differentiableReduceyqd__qd___qd__qd___xtXFtsAARd__lF__vjp_src_0_wrt_0_2_s14DifferentiableRzsAARd__r__l + 67
8  libswiftCore.dylib       0x0000000118f4552a AD__$sSass14DifferentiableRzlE20differentiableReduceyqd__qd___qd__qd___xtXFtsAARd__lF__vjp_src_0_wrt_0_2_s14DifferentiableRzsAARd__r__l + 27461834
9  libswiftCore.dylib       0x0000000118f42f01 AD__$sSass14DifferentiableRzlE20differentiableReduceyqd__qd___qd__qd___xtXFtsAARd__lF__vjp_src_0_wrt_0_2_s14DifferentiableRzsAARd__r__l + 27452065
10 libswiftCore.dylib       0x0000000118f4660d AD__$sSass14DifferentiableRzlE20differentiableReduceyqd__qd___qd__qd___xtXFtsAARd__lF__vjp_src_0_wrt_0_2_s14DifferentiableRzsAARd__r__l + 27466157
11 libswiftCore.dylib       0x0000000118f41ea8 AD__$sSass14DifferentiableRzlE20differentiableReduceyqd__qd___qd__qd___xtXFtsAARd__lF__vjp_src_0_wrt_0_2_s14DifferentiableRzsAARd__r__l + 27447880
12 libswiftCore.dylib       0x0000000118f4209e AD__$sSass14DifferentiableRzlE20differentiableReduceyqd__qd___qd__qd___xtXFtsAARd__lF__vjp_src_0_wrt_0_2_s14DifferentiableRzsAARd__r__l + 27448382
13 libswiftCore.dylib       0x0000000117505c18 $ss17valueWithPullback2at2inq_0A0_13TangentVectorQzAEQy_c8pullbacktx_q_xXFts14DifferentiableRzsAIR_r0_lF + 104
14 libswiftCore.dylib       0x0000000118f3b3ec $ss17valueWithPullback2at2inq_0A0_13TangentVectorQzAEQy_c8pullbacktx_q_xXFts14DifferentiableRzsAIR_r0_lF + 27482172
15 swift-frontend           0x000000010c4ba848 llvm::orc::runAsMain(int (*)(int, char**), llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::Optional<llvm::StringRef>) + 1224
16 swift-frontend           0x000000010c4a9ba5 swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, swift::IRGenOptions const&, swift::SILOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >&&) + 7429
17 swift-frontend           0x000000010c47ad87 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1687
18 swift-frontend           0x000000010c47a4eb performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 603
19 swift-frontend           0x000000010c472a62 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4658
20 swift-frontend           0x000000010c411a63 main + 835
21 libdyld.dylib            0x00007fff6f068cc9 start + 1
22 libdyld.dylib            0x000000000000000c start + 18446603338653463364
zsh: segmentation fault   main.swift
@dabrahams
Copy link
Collaborator

Sorry, "immediate mode" (swift file.swift) is currently supported. Please compile the code with swiftc and run the resulting executable.

@swift-ci
Copy link
Collaborator Author

Comment by Chris Pratten (JIRA)

I dont think that is the problem. If I run compile with

xcrun /Library/Developer/Toolchains/swift-tensorflow-DEVELOPMENT-2020-09-16-a.xctoolchain/usr/bin/swiftc -g main.swift

and run the resultant executable I get the exact same problem.

I also find that the program runs to completion just fine if i replace the lines

    let myL = [ MyLayer<Double>(kernel: (2,2,3), channels: 1) ]
    let policy2 = myL

with

    let policy2 = MyLayer<Double>(kernel: (2,2,3), channels: 1)

@dabrahams
Copy link
Collaborator

OK, we'll take another look, thanks!

@rxwei
Copy link
Member

rxwei commented Feb 8, 2021

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
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. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

4 participants