nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: JuliaDiff/FiniteDifferences.jl
Pull request #71 (2020-04-11 20:57:13)

Revert some of 0.9.4
As it turns out I was over-zealous with the type checking. Currently if you provide an `Int` input when computing differences, `to_vec` will make it a float if needs be. While it's debatable whether or not this is desirable behaviour, it's clearly breaking if it changes.

The changes to `adjoint` / `transpose` were just wrong, and they've now been reverted and a test added.

@oxinabox I would appreciate it greatly if you could verify whether these changes make all of the `ChainRules` tests pass on your machine. For some reason, but machine is throwing very strange errors in places where CI doesn't have them. I'm not sure whether it's an issue with my Julia install, my computer is dying (it could very well be, it's old and has been behaving a little strangely recently), something else that's weird, or an actual issue with FiniteDifferences.


Patch:
diff --git a/Project.toml b/Project.toml
index b4a2a38..52e8c6d 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,6 +1,6 @@
 name = "FiniteDifferences"
 uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
-version = "0.9.4"
+version = "0.9.5"
 
 [deps]
 LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
diff --git a/src/to_vec.jl b/src/to_vec.jl
index d0c23aa..002512d 100644
--- a/src/to_vec.jl
+++ b/src/to_vec.jl
@@ -6,7 +6,7 @@ transformation.
 """
 function to_vec(x::Number)
     function Number_from_vec(x_vec)
-        return oftype(x, first(x_vec))
+        return first(x_vec)
     end
     return [x], Number_from_vec
 end
@@ -61,25 +61,17 @@ function to_vec(X::Diagonal)
 end
 
 function to_vec(X::Transpose)
-
-    x_vec, x_from_vec = to_vec(X.parent)
-
     function Transpose_from_vec(x_vec)
-        return Transpose(x_from_vec(x_vec))
+        return Transpose(permutedims(reshape(x_vec, size(X))))
     end
-
-    return x_vec, Transpose_from_vec
+    return vec(Matrix(X)), Transpose_from_vec
 end
 
 function to_vec(X::Adjoint)
-
-    x_vec, x_from_vec = to_vec(X.parent)
-
     function Adjoint_from_vec(x_vec)
-        return Adjoint(x_from_vec(x_vec))
+        return Adjoint(conj!(permutedims(reshape(x_vec, size(X)))))
     end
-
-    return x_vec, Adjoint_from_vec
+    return vec(Matrix(X)), Adjoint_from_vec
 end
 
 # Non-array data structures


Test patch:
diff --git a/test/to_vec.jl b/test/to_vec.jl
index 229746f..8e3406c 100644
--- a/test/to_vec.jl
+++ b/test/to_vec.jl
@@ -25,7 +25,6 @@ function test_to_vec(x::T) where {T}
     x_vec, back = to_vec(x)
     @test x_vec isa Vector
     @test x == back(x_vec)
-    @test back(x_vec) isa T
     return nothing
 end
 
@@ -56,6 +55,9 @@ end
             test_to_vec(Op(randn(T, 4, 4)))
             test_to_vec(Op(randn(T, 6)))
             test_to_vec(Op(randn(T, 2, 5)))
+
+            A = randn(T, 3, 3)
+            @test reshape(first(to_vec(Op(A))), 3, 3) == Op(A)
         end
 
         @testset "Tuples" begin
Repository: JuliaDiff/FiniteDifferences.jl
Pull request #86 (2020-05-19 08:27:06)

Reconstruct Symmetric using uplo, and generalize for Hermitian also
The `to_vec` function for `Hermitian` uses the same approach as used in #85.

willtebbutt: I guess we merge #85 then this. Please bump the patch version to 0.10.2
sethaxen: Can this also be merged?

Patch:
diff --git a/Project.toml b/Project.toml
index 7ab7851..e937326 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,6 +1,6 @@
 name = "FiniteDifferences"
 uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
-version = "0.10.1"
+version = "0.10.2"
 
 [deps]
 LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
diff --git a/src/to_vec.jl b/src/to_vec.jl
index 2f9b462..120e49c 100644
--- a/src/to_vec.jl
+++ b/src/to_vec.jl
@@ -53,12 +53,12 @@ function to_vec(x::T) where {T<:LinearAlgebra.AbstractTriangular}
     return x_vec, AbstractTriangular_from_vec
 end
 
-function to_vec(x::Symmetric)
+function to_vec(x::T) where {T<:LinearAlgebra.HermOrSym}
     x_vec, back = to_vec(Matrix(x))
-    function Symmetric_from_vec(x_vec)
-        return Symmetric(back(x_vec))
+    function HermOrSym_from_vec(x_vec)
+        return T(back(x_vec), x.uplo)
     end
-    return x_vec, Symmetric_from_vec
+    return x_vec, HermOrSym_from_vec
 end
 
 function to_vec(X::Diagonal)


Test patch:
diff --git a/test/to_vec.jl b/test/to_vec.jl
index c1f75dc..b19076c 100644
--- a/test/to_vec.jl
+++ b/test/to_vec.jl
@@ -46,12 +46,21 @@ end
         test_to_vec([randn(T, 5, 4, 3), (5, 4, 3), 2.0])
         test_to_vec(reshape([1.0, randn(T, 5, 4, 3), randn(T, 4, 3), 2.0], 2, 2))
         test_to_vec(UpperTriangular(randn(T, 13, 13)))
-        test_to_vec(Symmetric(randn(T, 11, 11)))
         test_to_vec(Diagonal(randn(T, 7)))
         test_to_vec(DummyType(randn(T, 2, 9)))
         test_to_vec(SVector{2, T}(1.0, 2.0))
         test_to_vec(SMatrix{2, 2, T}(1.0, 2.0, 3.0, 4.0))
 
+        @testset "$Op" for Op in (Symmetric, Hermitian)
+            test_to_vec(Op(randn(T, 11, 11)))
+            @testset "$uplo" for uplo in (:L, :U)
+                A = Op(randn(T, 11, 11), uplo)
+                test_to_vec(A)
+                x_vec, back = to_vec(A)
+                @test back(x_vec).uplo == A.uplo
+            end
+        end
+
         @testset "$Op" for Op in (Adjoint, Transpose)
             test_to_vec(Op(randn(T, 4, 4)))
             test_to_vec(Op(randn(T, 6)))
Repository: JuliaDiff/FiniteDifferences.jl
Pull request #194 (2021-11-05 10:17:05)

Implement `to_vec` for `DataType`s
I am surprised we didn't run into this earlier.
```julia
julia> v, b = to_vec(Float64)
ERROR: StackOverflowError:
Stacktrace:
     [1] fieldnames
       @ ./reflection.jl:177 [inlined]
     [2] to_vec(x::Type{Float64})
       @ FiniteDifferences ~/JuliaEnvs/test/alex/dev/FiniteDifferences/src/to_vec.jl:42
     [3] (::FiniteDifferences.var"#206#209"{Core.TypeName})(name::Symbol)
       @ FiniteDifferences ~/JuliaEnvs/test/alex/dev/FiniteDifferences/src/to_vec.jl:38
     [4] map (repeats 4 times)
       @ ./tuple.jl:216 [inlined]
     [5] to_vec(x::Core.TypeName)
       @ FiniteDifferences ~/JuliaEnvs/test/alex/dev/FiniteDifferences/src/to_vec.jl:54
     [6] (::FiniteDifferences.var"#206#209"{DataType})(name::Symbol)
       @ FiniteDifferences ~/JuliaEnvs/test/alex/dev/FiniteDifferences/src/to_vec.jl:38
     [7] map(f::FiniteDifferences.var"#206#209"{DataType}, t::NTuple{20, Symbol})
       @ Base ./tuple.jl:226
     [8] to_vec(x::Type{Float64})
       @ FiniteDifferences ~/JuliaEnvs/test/alex/dev/FiniteDifferences/src/to_vec.jl:54--- the last 6 lines are repeated 3905 more times ---
```
Found since it caused problems with `unflatten` from ParameterHandling. cc @AlexRobson 


Patch:
diff --git a/Project.toml b/Project.toml
index 279bd47..21e4474 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,6 +1,6 @@
 name = "FiniteDifferences"
 uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
-version = "0.12.18"
+version = "0.12.19"
 
 [deps]
 ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
diff --git a/src/to_vec.jl b/src/to_vec.jl
index 838078d..e1916c0 100644
--- a/src/to_vec.jl
+++ b/src/to_vec.jl
@@ -235,6 +235,13 @@ function to_vec(d::Dict)
     return d_vec, Dict_from_vec
 end
 
+# types
+function FiniteDifferences.to_vec(x::DataType)
+    function DataType_from_vec(x_vec::Vector)
+        return x
+    end
+    return Bool[], DataType_from_vec
+end
 
 # ChainRulesCore Differentials
 function FiniteDifferences.to_vec(x::Tangent{P}) where{P}


Test patch:
diff --git a/test/to_vec.jl b/test/to_vec.jl
index 7e430c8..4e7a224 100644
--- a/test/to_vec.jl
+++ b/test/to_vec.jl
@@ -195,6 +195,11 @@ end
         end
     end
 
+    @testset "DataType" begin
+        test_to_vec(Float64; check_inferred=false) # isa DataType
+        test_to_vec(Vector; check_inferred=false) # isa UnionAll
+    end
+
     @testset "ChainRulesCore Differentials" begin
         @testset "Tangent{Tuple}" begin
             @testset "basic" begin
Repository: JuliaDiff/FiniteDifferences.jl
Pull request #200 (2022-01-27 22:59:00)

Fix and test type inference of `jacobian`
As suggested in https://github.com/JuliaDiff/FiniteDifferences.jl/issues/199#issuecomment-1023526284.


Patch:
diff --git a/Project.toml b/Project.toml
index 19ba357..44b26dd 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,6 +1,6 @@
 name = "FiniteDifferences"
 uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
-version = "0.12.20"
+version = "0.12.21"
 
 [deps]
 ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
diff --git a/src/grad.jl b/src/grad.jl
index f02f849..d09abf0 100644
--- a/src/grad.jl
+++ b/src/grad.jl
@@ -21,7 +21,7 @@ function jacobian(fdm, f, x::Vector{<:Real}; len=nothing)
             return ret
         end
     end
-    return (hcat(ẏs...), )
+    return (reduce(hcat, ẏs), )
 end
 
 function jacobian(fdm, f, x; len=nothing)


Test patch:
diff --git a/test/grad.jl b/test/grad.jl
index 4374553..231e373 100644
--- a/test/grad.jl
+++ b/test/grad.jl
@@ -9,8 +9,8 @@ using FiniteDifferences: grad, jacobian, _jvp, jvp, j′vp, _j′vp, to_vec
             ẋ, ẏ = randn(rng, T, N), randn(rng, T, M)
             xy, ẋẏ = vcat(x, y), vcat(ẋ, ẏ)
             ż_manual = _jvp(fdm, (xy)->sum(sin, xy), xy, ẋẏ)[1]
-            ż_auto = jvp(fdm, x->sum(sin, x[1]) + sum(sin, x[2]), ((x, y), (ẋ, ẏ)))
-            ż_multi = jvp(fdm, (x, y)->sum(sin, x) + sum(sin, y), (x, ẋ), (y, ẏ))
+            ż_auto = @inferred(jvp(fdm, x->sum(sin, x[1]) + sum(sin, x[2]), ((x, y), (ẋ, ẏ))))
+            ż_multi = @inferred(jvp(fdm, (x, y)->sum(sin, x) + sum(sin, y), (x, ẋ), (y, ẏ)))
             @test ż_manual ≈ ż_auto
             @test ż_manual ≈ ż_multi
         end
@@ -52,7 +52,7 @@ using FiniteDifferences: grad, jacobian, _jvp, jvp, j′vp, _j′vp, to_vec
         @assert length(ȳ) == length(f(x))
 
         # Check that the jacobian is as expected.
-        J_fdm = jacobian(fdm, f, x)[1]
+        J_fdm = @inferred(jacobian(fdm, f, x))[1]
         @test size(J_fdm) == (length(ȳ), length(x))
         @test J_fdm ≈ J_exact
         @test J_fdm == jacobian(fdm, f, x)[1]
@@ -79,7 +79,8 @@ using FiniteDifferences: grad, jacobian, _jvp, jvp, j′vp, _j′vp, to_vec
         @test Ac == A
 
         # Prevent regression against https://github.com/JuliaDiff/FiniteDifferences.jl/issues/67
-        J = first(jacobian(fdm, identity, x))
+        # Type inference: https://github.com/JuliaDiff/FiniteDifferences.jl/issues/199
+        J = first(@inferred(jacobian(fdm, identity, x)))
         @test J ≈ one(Matrix{T}(undef, size(J)))
     end
Repository: JuliaDiff/FiniteDifferences.jl
Pull request #203 (2022-02-21 11:59:50)

to_vec for InplaceableThunk
what it says on the tin, needed for https://github.com/JuliaDiff/ChainRules.jl/pull/490#discussion_r811058883


Patch:
diff --git a/Project.toml b/Project.toml
index 0c3f88b..68561cc 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,6 +1,6 @@
 name = "FiniteDifferences"
 uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
-version = "0.12.23"
+version = "0.12.24"
 
 [deps]
 ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
diff --git a/src/to_vec.jl b/src/to_vec.jl
index 37287f9..6dd1fbc 100644
--- a/src/to_vec.jl
+++ b/src/to_vec.jl
@@ -287,3 +287,14 @@ function FiniteDifferences.to_vec(t::Thunk)
     Thunk_from_vec = v -> @thunk(back(v))
     return v, Thunk_from_vec
 end
+
+function FiniteDifferences.to_vec(t::InplaceableThunk)
+    v, back = to_vec(unthunk(t))
+    function InplaceableThunk_from_vec(v)
+        return InplaceableThunk(
+            Δ -> Δ += back(b),
+            @thunk(back(v))
+        )
+    end
+    return v, InplaceableThunk_from_vec
+end


Test patch:
diff --git a/test/to_vec.jl b/test/to_vec.jl
index 54e18ab..4f01ff1 100644
--- a/test/to_vec.jl
+++ b/test/to_vec.jl
@@ -249,7 +249,9 @@ end
         end
 
         @testset "Thunks" begin
-            test_to_vec(@thunk(3.2+4.3))
+            t = @thunk(3.2+4.3)
+            test_to_vec(t)
+            test_to_vec(InplaceableThunk(Δ -> Δ += t, t))
         end
     end
Repository: JuliaDiff/FiniteDifferences.jl
Pull request #226 (2023-08-03 15:03:12)

support empty tuples and named tuples
On the main branch, `to_vec(t::Tuple{})` fails as it attempts to call `cumsum` on an empty vector of type `Union{}` (obtained by collecting the empty tuple). It is a bit of a corner case, but it can cause `test_rrule` to fail when the output has an empty tuple or named tuple.

This PR specialcases empty tuples to fix that.

On main:

```julia
julia> using FiniteDifferences

julia> v, reconstruct = FiniteDifferences.to_vec(())
ERROR: MethodError: Base.ArithmeticStyle(::Type{Union{}}) is ambiguous.
```

With this PR:

```julia
julia> using FiniteDifferences
[ Info: Precompiling FiniteDifferences [26cc04aa-876d-5657-8c51-4c34ba976000]

julia> v, reconstruct = FiniteDifferences.to_vec(())
(Bool[], FiniteDifferences.var"#Tuple_from_vec#51"())

julia> reconstruct(v)
()
```


Patch:
diff --git a/src/to_vec.jl b/src/to_vec.jl
index 6dd1fbc..af991f3 100644
--- a/src/to_vec.jl
+++ b/src/to_vec.jl
@@ -227,6 +227,14 @@ end
 
 # Non-array data structures
 
+function to_vec(::Tuple{})
+    vec = Bool[]
+    function Tuple_from_vec(_)
+        return ()
+    end
+    return vec, Tuple_from_vec
+end
+
 function to_vec(x::Tuple)
     x_vecs_and_backs = map(to_vec, x)
     x_vecs, x_backs = first.(x_vecs_and_backs), last.(x_vecs_and_backs)


Test patch:
diff --git a/test/to_vec.jl b/test/to_vec.jl
index b16b876..5e5ab5f 100644
--- a/test/to_vec.jl
+++ b/test/to_vec.jl
@@ -226,6 +226,12 @@ end
                 x_comp = Tangent{typeof(x_outer)}(1, Tangent{typeof(x_inner)}(2, 3))
                 test_to_vec(x_comp; check_inferred=false)
             end
+
+            @testset "empty" begin
+                x_tup = ()
+                x_comp = Tangent{typeof(x_tup)}(x_tup...)
+                test_to_vec(x_comp)
+            end
         end
 
         @testset "Tangent Struct" begin
@@ -235,6 +241,12 @@ end
                 test_to_vec(comp)
             end
 
+            @testset "NamedTuple empty" begin
+                nt = NamedTuple()
+                comp = Tangent{typeof(nt)}(; nt...)
+                test_to_vec(comp)
+            end
+
             @testset "Struct" begin
                 test_to_vec(Tangent{ThreeFields}(; a=10.0, b=20.0, c=30.0))
                 test_to_vec(Tangent{ThreeFields}(; a=10.0, b=20.0,)) # broken on Julia 1.6.0, fixed on 1.6.1
Repository: JuliaDiff/FiniteDifferences.jl
Pull request #237 (2024-05-20 13:40:47)

Support Tridiagonal in to_vec
Encountered in https://github.com/JuliaDiff/ChainRules.jl/pull/758
Problem was there is a `du2` field which isn't used except as an implementation detail of `lu`.
It is the value on the diag(x, 2)` but its not used by `==` etc because it's not part of the actual tridiagonal.

Reading this was causing errors with reading undefined values.





Patch:
diff --git a/Project.toml b/Project.toml
index 4107c5a..d6c26eb 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,6 +1,6 @@
 name = "FiniteDifferences"
 uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
-version = "0.12.31"
+version = "0.12.32"
 
 [deps]
 ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
diff --git a/src/to_vec.jl b/src/to_vec.jl
index af991f3..2d20313 100644
--- a/src/to_vec.jl
+++ b/src/to_vec.jl
@@ -111,14 +111,26 @@ function to_vec(x::T) where {T<:LinearAlgebra.HermOrSym}
     return x_vec, HermOrSym_from_vec
 end
 
-function to_vec(X::Diagonal)
-    x_vec, back = to_vec(Matrix(X))
+function to_vec(x::Diagonal)
+    x_vec, back = to_vec(Matrix(x))
     function Diagonal_from_vec(x_vec)
         return Diagonal(back(x_vec))
     end
     return x_vec, Diagonal_from_vec
 end
 
+function to_vec(x::Tridiagonal)
+    x_vec, back = to_vec((x.dl, x.d, x.du))
+    # Other field (du2) of a Tridiagonal is not part of its value and is really a kind of cache
+    function Tridiagonal_from_vec(x_vec)
+        return Tridiagonal(back(x_vec)...)
+    end
+    return x_vec, Tridiagonal_from_vec
+end
+
+
+
+
 function to_vec(X::Transpose)
     x_vec, back = to_vec(Matrix(X))
     function Transpose_from_vec(x_vec)


Test patch:
diff --git a/test/to_vec.jl b/test/to_vec.jl
index 5e5ab5f..f18e756 100644
--- a/test/to_vec.jl
+++ b/test/to_vec.jl
@@ -88,6 +88,8 @@ end
         test_to_vec(reshape([1.0, randn(T, 5, 4, 3), randn(T, 4, 3), 2.0], 2, 2); check_inferred=false)
         test_to_vec(UpperTriangular(randn(T, 13, 13)))
         test_to_vec(Diagonal(randn(T, 7)))
+        test_to_vec(Tridiagonal(randn(T, 3), randn(T, 4), randn(T, 3)))
+
         test_to_vec(DummyType(randn(T, 2, 9)))
         test_to_vec(SVector{2, T}(1.0, 2.0); check_inferred=false)
         test_to_vec(SMatrix{2, 2, T}(1.0, 2.0, 3.0, 4.0); check_inferred=false)