Add swift-numerics-differentiable for safe scalar math in autodiff#9
Merged
pedronahum merged 1 commit intomainfrom Dec 10, 2025
Merged
Conversation
Integrates the swift-numerics-differentiable package to solve SIL linker crashes when using C library math functions (exp, log, sqrt, pow) in Swift automatic differentiation on Linux. Changes: - Add swift-numerics-differentiable dependency (v1.3.0) to Package.swift - Re-export RealModuleDifferentiable from Torch so users get differentiable scalar math (Double.exp, Float.log, etc.) automatically via import Torch - Replace Taylor/Mercator series workarounds in tests with proper Double.exp() and Double.log(onePlus:) calls - Disable known-failing KeyPath test (sequential_parameter_keypaths) - Update KNOWN_ISSUES.md with the recommended solution This addresses the issue where Foundation.exp(), Darwin.log(), etc. cause crashes like: Assertion failed: googGV->isDeclaration() LLVM ERROR: Global is external, but doesnt have external linkage The swift-numerics-differentiable package provides pure Swift implementations with @Derivative attributes, avoiding C library symbol conflicts entirely. Closes #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Integrates the swift-numerics-differentiable package to solve SIL linker crashes when using C library math functions (
exp,log,sqrt,pow) in Swift automatic differentiation on Linux.This addresses issue #7 - thanks to @JaapWijnen for the suggestion!
Changes
swift-numerics-differentiabledependency (v1.3.0)RealModuleDifferentiableso users get differentiable scalar math (Double.exp,Float.log, etc.) automatically viaimport TorchDouble.exp()andDouble.log(onePlus:)calls, tighten tolerances from 1e-4 to 1e-6Double.exp(1.0)Problem Solved
On Linux, using C library math functions like
Foundation.exp()orDarwin.log()in differentiated code causes crashes:The
swift-numerics-differentiablepackage provides pure Swift implementations with@derivativeattributes, completely avoiding C library symbol conflicts.Usage
Users now automatically get differentiable scalar math just by importing Torch:
Test plan
RealModuleDifferentiableis properly re-exported from TorchCloses #7