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

BasicGradientDescent example crashes #22

Open
steppity opened this issue Feb 6, 2024 · 3 comments
Open

BasicGradientDescent example crashes #22

steppity opened this issue Feb 6, 2024 · 3 comments

Comments

@steppity
Copy link

steppity commented Feb 6, 2024

The BasicGradientDescent example is crashing here with EXC_BAD_ACCESS on the first call to the loss function.

This is on an M1 MacBook with:
Xcode: 15.2
Toolchain: Swift Development Snapshot 2024-02-04 (a)

Project setup is via New Project with template MacOS Command Line Tool, then pasting your main.swift over the generated one.
(The other examples build and run correctly with this setup).

For some reason the crash can be fixed by unrolling the loop inside the loss function like this:

var data = andGateData[0]
var prediction = model(data.x1, data.x2)
var error = data.y - prediction
loss = loss + error * error / 2

data = andGateData[1]
prediction = model(data.x1, data.x2)
error = data.y - prediction
loss = loss + error * error / 2

data = andGateData[2]
prediction = model(data.x1, data.x2)
error = data.y - prediction
loss = loss + error * error / 2

data = andGateData[3]
prediction = model(data.x1, data.x2)
error = data.y - prediction
loss = loss + error * error / 2
@BradLarson
Copy link
Contributor

Thanks for reporting. This is something we're tracking down right now.

As you suspected, it's related to the use of loops in a differentiable function. When generating the derivative (pullback) of a differentiable function containing loops, a special memory allocation function is called in the Swift runtime. In mid-2023, we found that this function was leaking memory in specific cases, so a fix was provided in the form of a new allocator function. We used a new allocator function so as to not disrupt the operation of code that was using the old, leaky runtime function.

Unfortunately, if you try to deploy code compiled with a new compiler to a macOS system with the old Swift runtime that lacks this, the program segfaults. Our instructions here for setting the DYLD_LIBRARY_PATH to the toolchain location should tell the application to instead use the new Swift runtime contained in the toolchain (that would have this new function), but that is not working as it should. We're investigating why this is.

This is only a problem with Swift toolchains newer than mid-September of 2023 and the command line on current macOS versions. It does not occur on Linux, when running in Xcode (which uses the toolchain Swift runtime), or with the 5.9 Swift toolchains from swift.org. Still, we're trying to get to the bottom of it. Sorry for the hassle.

@JaapWijnen
Copy link
Collaborator

Extra note here. Running swift run will unset that DYLD_LIBRARY_PATH.
Running the benchmarks is possible as follows:
swift build -c release
and then manually run the executable.
Unfortunately that doesn't run it with the nice interface and overview you get by running using
swift package benchmark
but it's a step in the right direction at least :)
We're hoping this will be resolved once macOS starts using Swift 5.10

@BrianHenryIE
Copy link
Contributor

FWIW, it works ok for me:

  • Swift 6.0-dev (2024-08-02)
  • M1 MacBook Air
  • macOS 14.5
  • VS Code 1.93.0-insider
  • vscode-swift v1.11.0
[Running] swift "/Users/brianhenry/Sites/differentiable-swift-examples-tutorial/basicgradientdescent.swift"
Loss: 0.58349204
Loss: 0.51880944
...
Loss: 0.011055167
Loss: 0.0108381165
Trained model results:
Value at (1.0, 0.0): 0.062424123
Value at (1.0, 1.0): 0.9149991

[Done] exited with code=0 in 0.955 seconds

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

No branches or pull requests

4 participants