FFTs in probabilistic models

I’m working on a model that involves a Fourier transform, and I’m running the limits of my julia debugging ability

I’m using this AbstractFFTs PR to try to get it working with the ForwardDiff backend. (Zygote fails with what I think is an unrelated issue that has to due with MvNormal, but I haven’t pursued it that hard.)

This MWE model fails with the forwarddiff backend, complaining about (I think?) the type conversion for FFTW. (I actually want to model both the real and complex components of the FFT output, but I don’t think it’s relevant to the MWE)

using Pkg
Pkg.add("Turing")
Pkg.add("FFTW")
Pkg.add(url="https://github.com/devmotion/AbstractFFTs.jl", rev = "dw/chainrules")
using FFTW
using Turing
using Distributions
using LinearAlgebra
using AbstractFFTs

# MWE model with a fourier transform
@model function fftmodel(x, y)
	mu ~ Normal(0, 1)
	q = fft(mu .+ x)
	y ~ MvNormal(real.(q), 0.1)
	return y
end

x = rand(10)
y = fft(3.0 .+ x)
chain = sample(fftmodel(x, real.(y)) , NUTS(0.65), 1000) 

here is the traceback:

type ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1} not supported

  1. error (::String)@ error.jl:33
  2. _fftfloat (::Type{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}})@ definitions.jl:22
  3. _fftfloat (::ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1})@ definitions.jl:23
  4. fftfloat (::ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1})@ definitions.jl:18
  5. complexfloat (::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}})@ definitions.jl:31
  6. fft (::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, ::UnitRange{Int64})@ definitions.jl:198
  7. (::Main.workspace18.var"#fftmodel#1")(::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.ThreadSafeVarInfo{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, Vector{Set{DynamicPPL.Selector}}}}}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, Vector{Base.RefValue{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}}}, ::DynamicPPL.SamplingContext{DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext, Random._GLOBAL_RNG}, ::Vector{Float64}, ::Vector{Float64})@ *[Local: 6]
  8. macro expansion @ model.jl:465 [inlined]
  9. _evaluate (::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.ThreadSafeVarInfo{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, Vector{Set{DynamicPPL.Selector}}}}}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, Vector{Base.RefValue{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}}}, ::DynamicPPL.SamplingContext{DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext, Random._GLOBAL_RNG})@ model.jl:448
  10. evaluate_threadsafe (::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, Vector{Set{DynamicPPL.Selector}}}}}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, ::DynamicPPL.SamplingContext{DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext, Random._GLOBAL_RNG})@ model.jl:438
  11. (::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext})(::DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, Vector{Set{DynamicPPL.Selector}}}}}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, ::DynamicPPL.SamplingContext{DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext, Random._GLOBAL_RNG})@ model.jl:391
  12. (::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext})(::Random._GLOBAL_RNG, ::DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, Vector{Set{DynamicPPL.Selector}}}}}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, ::DynamicPPL.DefaultContext)@ model.jl:383
  13. (::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext})(::DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, Vector{Set{DynamicPPL.Selector}}}}}, ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}, ::Vararg{Any, N} where N)@ model.jl:396
  14. (::Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext})(::Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}})@ ad.jl:111
  15. vector_mode_dual_eval! (::Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}}, ::Vector{Float64})@ apiutils.jl:37
  16. vector_mode_gradient! (::Vector{Float64}, ::Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, ::Vector{Float64}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}})@ gradient.jl:113
  17. gradient! (::Vector{Float64}, ::Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, ::Vector{Float64}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}}, ::Val{true})@ gradient.jl:37
  18. gradient! (::Vector{Float64}, ::Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, ::Vector{Float64}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1, Vector{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#1"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.DefaultContext}, Float64}, Float64, 1}}})@ gradient.jl:35
  19. gradient_logp (::Turing.Core.ForwardDiffAD{40}, ::Vector{Float64}, ::DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, ::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, ::DynamicPPL.DefaultContext)@ ad.jl:121
    ::AdvancedHMC.Hamiltonian{AdvancedHMC.DiagEuclideanMetric{Float64, Vector{Float64}}, Turing.Inference.var"#logπ#52"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}}, Turing.Inference.var"#∂logπ∂θ#51"{DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}}})@ hamiltonian.jl:153
  20. var"#initialstep#41" (::Nothing, ::Int64, ::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(DynamicPPL.initialstep), ::Random._GLOBAL_RNG, ::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, ::DynamicPPL.TypedVarInfo{NamedTuple{(:mu,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:mu, Tuple{}}, Int64}, Vector{Distributions.Normal{Float64}}, Vector{AbstractPPL.VarName{:mu, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64})@ hmc.jl:167
  21. var"#step#17" (::Nothing, ::Base.Iterators.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:nadapts,), Tuple{Int64}}}, ::typeof(AbstractMCMC.step), ::Random._GLOBAL_RNG, ::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}})@ sampler.jl:87
  22. var"#mcmcsample#20" (::Bool, ::String, ::Nothing, ::Int64, ::Int64, ::Type, ::Base.Iterators.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:nadapts,), Tuple{Int64}}}, ::typeof(AbstractMCMC.mcmcsample), ::Random._GLOBAL_RNG, ::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, ::Int64)@ sample.jl:114
  23. var"#sample#40" (::Type, ::Nothing, ::Bool, ::Int64, ::Bool, ::Int64, ::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(StatsBase.sample), ::Random._GLOBAL_RNG, ::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, ::Int64)@ hmc.jl:133
  24. sample (::DynamicPPL.Model{Main.workspace18.var"#fftmodel#1", (:x, :y), (), (), Tuple{Vector{Float64}, Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::Turing.Inference.NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}, ::Int64)@ Inference.jl:132

Any pointers?

version info:

Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin19.6.0)
CPU: Intel(R) Core™ i7-4770HQ CPU @ 2.20GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, haswell)
Environment:
JULIA_NUM_THREADS = 4
JULIA_REVISE_WORKER_ONLY = 1

sorry, the traceback is really long so I had to truncate some of it.

In contrast, explicitly constructing and using the DFT matrix works. I’d like to make it work with FFTW though.


N = 10
ℱ = fft(Matrix(1.0I, N, N), 1)

@model function naivefftmodel(x, y)
	mu ~ Normal(0, 1)
	q = ℱ * (mu .+ x)
	y ~ MvNormal(real.(q), 0.1)
	return y
end

x = rand(N)
y = fft(3.0 .+ x)
chain = sample(naivefftmodel(x, real.(y)) , NUTS(0.65), 1000) 

Any pointers?

Zygote fails on this model also, but I think it’s unrelated. MWE:

Turing.setadbackend(:zygote)

@model function mymodel(y)
	x ~ filldist(Normal(0, 1), 10)
	y ~ MvNormal(x, 1)
end
y = rand(10)	
chain = sample(mymodel(y), NUTS(0.65), 1000) 

the trace:

MethodError: no method matching (::ChainRulesCore.ProjectTo{Float64, NamedTuple{(), Tuple{}}})(::NamedTuple{(:x,), Tuple{Int64}})

Closest candidates are:

(::ChainRulesCore.ProjectTo{var"#s12", D} where {var"#s12"<:Real, D<:NamedTuple})(!Matched::Complex) at /Users/bld/.julia/packages/ChainRulesCore/MhCRV/src/projection.jl:186

(::ChainRulesCore.ProjectTo{var"#s12", D} where {var"#s12"<:Number, D<:NamedTuple})(!Matched::ChainRulesCore.Tangent{var"#s11", T} where {var"#s11"<:Number, T}) at /Users/bld/.julia/packages/ChainRulesCore/MhCRV/src/projection.jl:192

(::ChainRulesCore.ProjectTo{T, D} where D<:NamedTuple)(!Matched::ChainRulesCore.Tangent{var"#s12", T} where {var"#s12"<:T, T}) where T at /Users/bld/.julia/packages/ChainRulesCore/MhCRV/src/projection.jl:142

  1. (::ChainRulesCore.ProjectTo{Ref, NamedTuple{(:type, :x), Tuple{DataType, ChainRulesCore.ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}})(::Base.RefValue{Any})@ projection.jl:284
  2. #56 @ projection.jl:237 [inlined]
  3. #4 @ generator.jl:36 [inlined]
  4. iterate @ generator.jl:47 [inlined]
  5. collect (::Base.Generator{Base.Iterators.Zip{Tuple{Vector{ChainRulesCore.ProjectTo{Ref, NamedTuple{(:type, :x), Tuple{DataType, ChainRulesCore.ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}}}, Vector{Base.RefValue{Any}}}}, Base.var"#4#5"{ChainRulesCore.var"#56#57"}})@ array.jl:678
  6. map @ abstractarray.jl:2383 [inlined]
  7. (::ChainRulesCore.ProjectTo{AbstractArray, NamedTuple{(:elements, :axes), Tuple{Vector{ChainRulesCore.ProjectTo{Ref, NamedTuple{(:type, :x), Tuple{DataType, ChainRulesCore.ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}}}, Tuple{Base.OneTo{Int64}}}}})(::Vector{Base.RefValue{Any}})@ projection.jl:237
  8. (::ChainRules.var"#sum_pullback#1375"{Colon, typeof(getindex), ChainRulesCore.ProjectTo{AbstractArray, NamedTuple{(:elements, :axes), Tuple{Vector{ChainRulesCore.ProjectTo{Ref, NamedTuple{(:type, :x), Tuple{DataType, ChainRulesCore.ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}}}, Tuple{Base.OneTo{Int64}}}}}, Vector{Zygote.var"#ad_pullback#45"{Tuple{typeof(getindex), Base.RefValue{Float64}}, typeof(∂(getindex))}}})(::Int64)@ mapreduce.jl:88
  9. ZBack @ chainrules.jl:168 [inlined]
  10. Pullback @ threadsafe.jl:25 [inlined]
  11. (::typeof(∂(getlogp)))(::Int64)@ interface2.jl:0
  12. Pullback @ model.jl:439 [inlined]
  13. (::typeof(∂(evaluate_threadsafe)))(::Nothing)@ interface2.jl:0
  14. Pullback @ model.jl:391 [inlined]
  15. (::typeof(∂(λ)))(::Nothing)@ interface2.jl:0
  16. Pullback @ model.jl:383 [inlined]
  17. (::typeof(∂(λ)))(::Nothing)@ interface2.jl:0
  18. #203 @ lib.jl:203 [inlined]
  19. #1734#back @ adjoint.jl:67 [inlined]
  20. Pullback @ model.jl:396 [inlined]
  21. (::typeof(∂(λ)))(::Nothing)@ interface2.jl:0
  22. Pullback @ ad.jl:165 [inlined]
  23. (::typeof(∂(λ)))(::Int64)@ interface2.jl:0
  24. (::Zygote.var"#50#51"{typeof(∂(λ))})(::Int64)@ interface.jl:41
  25. gradient_logp (::Turing.Core.ZygoteAD, ::Vector{Float64}, ::DynamicPPL.TypedVarInfo{NamedTuple{(:x,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:x, Tuple{}}, Int64}, Vector{DistributionsAD.TuringScalMvNormal{Vector{Float64}, Float64}}, Vector{AbstractPPL.VarName{:x, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, ::DynamicPPL.Model{Main.workspace388.var"#mymodel#1", (:y,), (), (), Tuple{Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ZygoteAD, (), AdvancedHMC.DiagEuclideanMetric}}, ::DynamicPPL.DefaultContext)@ ad.jl:171
  26. gradient_logp @ ad.jl:83 [inlined]
  27. ∂logπ∂θ @ hmc.jl:433 [inlined]
  28. ∂H∂θ @ hamiltonian.jl:31 [inlined]
  29. phasepoint @ hamiltonian.jl:76 [inlined]
  30. phasepoint (::Random._GLOBAL_RNG, ::Vector{Float64}, ::AdvancedHMC.Hamiltonian{AdvancedHMC.DiagEuclideanMetric{Float64, Vector{Float64}}, Turing.Inference.var"#logπ#52"{DynamicPPL.TypedVarInfo{NamedTuple{(:x,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:x, Tuple{}}, Int64}, Vector{DistributionsAD.TuringScalMvNormal{Vector{Float64}, Float64}}, Vector{AbstractPPL.VarName{:x, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ZygoteAD, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.Model{Main.workspace388.var"#mymodel#1", (:y,), (), (), Tuple{Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}}, Turing.Inference.var"#∂logπ∂θ#51"{DynamicPPL.TypedVarInfo{NamedTuple{(:x,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:x, Tuple{}}, Int64}, Vector{DistributionsAD.TuringScalMvNormal{Vector{Float64}, Float64}}, Vector{AbstractPPL.VarName{:x, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64}, DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ZygoteAD, (), AdvancedHMC.DiagEuclideanMetric}}, DynamicPPL.Model{Main.workspace388.var"#mymodel#1", (:y,), (), (), Tuple{Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}}})@ hamiltonian.jl:153
  31. var"#initialstep#41" (::Nothing, ::Int64, ::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(DynamicPPL.initialstep), ::Random._GLOBAL_RNG, ::DynamicPPL.Model{Main.workspace388.var"#mymodel#1", (:y,), (), (), Tuple{Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ZygoteAD, (), AdvancedHMC.DiagEuclideanMetric}}, ::DynamicPPL.TypedVarInfo{NamedTuple{(:x,), Tuple{DynamicPPL.Metadata{Dict{AbstractPPL.VarName{:x, Tuple{}}, Int64}, Vector{DistributionsAD.TuringScalMvNormal{Vector{Float64}, Float64}}, Vector{AbstractPPL.VarName{:x, Tuple{}}}, Vector{Float64}, Vector{Set{DynamicPPL.Selector}}}}}, Float64})@ hmc.jl:167
  32. var"#step#17" (::Nothing, ::Base.Iterators.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:nadapts,), Tuple{Int64}}}, ::typeof(AbstractMCMC.step), ::Random._GLOBAL_RNG, ::DynamicPPL.Model{Main.workspace388.var"#mymodel#1", (:y,), (), (), Tuple{Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ZygoteAD, (), AdvancedHMC.DiagEuclideanMetric}})@ sampler.jl:87
  33. macro expansion @ sample.jl:123 [inlined]
  34. macro expansion @ ProgressLogging.jl:328 [inlined]
  35. macro expansion @ logging.jl:8 [inlined]
  36. var"#mcmcsample#20" (::Bool, ::String, ::Nothing, ::Int64, ::Int64, ::Type, ::Base.Iterators.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:nadapts,), Tuple{Int64}}}, ::typeof(AbstractMCMC.mcmcsample), ::Random._GLOBAL_RNG, ::DynamicPPL.Model{Main.workspace388.var"#mymodel#1", (:y,), (), (), Tuple{Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ZygoteAD, (), AdvancedHMC.DiagEuclideanMetric}}, ::Int64)@ sample.jl:114
  37. var"#sample#40" (::Type, ::Nothing, ::Bool, ::Int64, ::Bool, ::Int64, ::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(StatsBase.sample), ::Random._GLOBAL_RNG, ::DynamicPPL.Model{Main.workspace388.var"#mymodel#1", (:y,), (), (), Tuple{Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::DynamicPPL.Sampler{Turing.Inference.NUTS{Turing.Core.ZygoteAD, (), AdvancedHMC.DiagEuclideanMetric}}, ::Int64)@ hmc.jl:133
  38. sample @ hmc.jl:116 [inlined]
  39. #sample#2 @ Inference.jl:142 [inlined]
  40. sample @ Inference.jl:142 [inlined]
  41. #sample#1 @ Inference.jl:132 [inlined]
  42. sample (::DynamicPPL.Model{Main.workspace388.var"#mymodel#1", (:y,), (), (), Tuple{Vector{Float64}}, Tuple{}, DynamicPPL.DefaultContext}, ::Turing.Inference.NUTS{Turing.Core.ZygoteAD, (), AdvancedHMC.DiagEuclideanMetric}, ::Int64)@ Inference.jl:132
  43. top-level scope @ *[Local: 11]

version info:

Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin19.6.0)
CPU: Intel(R) Core™ i7-4770HQ CPU @ 2.20GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, haswell)
Environment:
JULIA_NUM_THREADS = 4
JULIA_REVISE_WORKER_ONLY = 1

FFTW is written by C, so ForwardDiff work in code written by pure Julia,so you need fft function written by julia.try FFTA

1 Like

The linked PR won’t help for FFTW + ForwardDiff, since this does not use ChainRules at all. What might work is ForwardDiff#541.

The Zygote issue indeed looks unrelated. It looks a bit like ChainRules#539. But this one can be triggered as follows:

julia> using ChainRulesCore

julia> ProjectTo(Ref(1))(Ref{Any}(2))
Tangent{Base.RefValue{Int64}}(x = 2.0,)

julia> ProjectTo(Ref(3))(Ref{Any}((x=4,)))
ERROR: MethodError: no method matching (::ProjectTo{Float64, NamedTuple{(), Tuple{}}})(::NamedTuple{(:x,), Tuple{Int64}})
  ...
Stacktrace:
 [1] (::ProjectTo{Ref, NamedTuple{(:type, :x), Tuple{DataType, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}})(dx::Base.RefValue{Any})
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/1L9My/src/projection.jl:275

I believe this is probably some mismatch between how Zygote handles mutable structs and its interface to ChainRules. It’s a bug, though.

2 Likes

awesome, thanks for setting me straight! https://github.com/JuliaDiff/ForwardDiff.jl/pull/541 works for my minimal example; I’ll report back if there’s some issue on the real model.

cool library, thanks for sharing! I didn’t find that one when I was looking for a native julia FFT.

The AbstractFFTs PR I linked implements some custom automatic differentiation rules for FFTs to get around the external call to FFTW. I guess my issue in the other fork of this topic was that I have an incomplete understanding of the Julia autodiff ecosystem and was looking in the wrong place.