Skip to content

Add swift-numerics-differentiable for safe scalar math in autodiff#9

Merged
pedronahum merged 1 commit intomainfrom
feature/swift-numerics-differentiable
Dec 10, 2025
Merged

Add swift-numerics-differentiable for safe scalar math in autodiff#9
pedronahum merged 1 commit intomainfrom
feature/swift-numerics-differentiable

Conversation

@pedronahum
Copy link
Owner

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

  • Package.swift - Add swift-numerics-differentiable dependency (v1.3.0)
  • Sources/Torch/Core/EuclideanDifferentiable.swift - Re-export RealModuleDifferentiable so users get differentiable scalar math (Double.exp, Float.log, etc.) automatically via import Torch
  • Tests/TorchTests/LossTests.swift - Replace Taylor/Mercator series workarounds with proper Double.exp() and Double.log(onePlus:) calls, tighten tolerances from 1e-4 to 1e-6
  • Tests/TensorTests/TensorMathTests.swift - Replace hardcoded Euler's number with Double.exp(1.0)
  • Tests/TorchTests/SequentialLayerTests.swift - Disable known-failing KeyPath test
  • KNOWN_ISSUES.md - Document the recommended solution

Problem Solved

On Linux, using C library math functions like Foundation.exp() or Darwin.log() in differentiated code causes crashes:

Assertion failed: googGV->isDeclaration() && "global variable already has initializer"
LLVM ERROR: Global is external, but doesn't have external or weak linkage

The swift-numerics-differentiable package provides pure Swift implementations with @derivative attributes, completely avoiding C library symbol conflicts.

Usage

Users now automatically get differentiable scalar math just by importing Torch:

import Torch

// These now work in @differentiable code without crashes:
let result = Double.exp(x)
let logResult = Double.log(x)
let log1pResult = Double.log(onePlus: x)
let powResult = Double.pow(x, y)

Test plan

  • All 161 tests pass (1 skipped - known KeyPath issue)
  • Build succeeds on Linux (Ubuntu 24.04)
  • RealModuleDifferentiable is properly re-exported from Torch

Closes #7

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
@pedronahum pedronahum merged commit 927f49d into main Dec 10, 2025
1 of 2 checks passed
@pedronahum pedronahum deleted the feature/swift-numerics-differentiable branch December 10, 2025 20:36
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.

known issues with C Library Math Functions

1 participant