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

Turing crash #2256

Open
mhauru opened this issue Jan 7, 2025 · 2 comments
Open

Turing crash #2256

mhauru opened this issue Jan 7, 2025 · 2 comments
Labels
gc garbage collection segfault

Comments

@mhauru
Copy link
Contributor

mhauru commented Jan 7, 2025

As discussed on Slack, this still crashes:

using Distributions
using Enzyme
using Turing  
using ADTypes

function data_poly(x, degree=2)
    return hcat([x .^ d for d in 1:degree]...)
end


@model function model(y, x)

    X = data_poly(x, 2)

    # Priors
    μ_intercept ~ Normal(0, 3)
    μ_x1 ~ Normal(0, 3)
    μ_x2 ~ Normal(0, 3)
    σ ~ Normal(0, 3)

    # Likelihood
    for i in eachindex(y)
        μ = μ_intercept + μ_x1 * X[i, 1] + μ_x2 * X[i, 2]
        y[i] ~ Normal(μ, exp(σ))
    end
end


y = rand(Normal(0, 1), 1000)
x = y .+ rand(Normal(0, 1.5), 1000)

sample(model(y, x), NUTS(), 1000)  # 5.7 seconds
sample(model(y, x), NUTS(; adtype = AutoEnzyme(; mode=Enzyme.set_runtime_activity(Enzyme.Reverse))), 1000)

Example output:

GC error (probable corruption)
Allocations: 384347194 (Pool: 384063169; Big: 284025); GC: 296
Base.RefValue{Float64}(x=0.000228827)

thread 0 ptr queue:
~~~~~~~~~~ ptr queue top ~~~~~~~~~~
Array{Float64, (1,)}[0]
==========
Array{Float64, (1,)}[0]
==========
Array{Float64, (1,)}[0.314644]
==========
Array{Float64, (1,)}[0.314644]
==========
Array{Float64, ()}[0]
==========
Array{Float64, ()}[0.314644]
==========
Array{Float64, (1,)}[0]
==========
Array{Float64, (1,)}[0]
==========
Array{Float64, (1,)}[-0.00963471]
==========
Array{Float64, (1,)}[-0.00963471]
==========
Array{Float64, ()}[0]
==========
Array{Float64, ()}[-0.00963471]
==========
Array{Float64, (1,)}[0]
==========
Array{Float64, (1,)}[0]
==========
Array{Float64, (1,)}[0.0203797]
==========
Array{Float64, (1,)}[0.0203797]
==========
Array{Float64, ()}[0]
==========
Array{Float64, ()}[0.0203797]
==========
Array{Float64, (1,)}[0]
==========
Array{Float64, (1,)}[0]
==========
Array{Float64, (1,)}[-0.145968]
==========
Array{Float64, (1,)}[-0.145968]
==========
Array{Float64, ()}[0]
==========
Array{Float64, ()}[-0.145968]
==========
Array{Array{Float64, 1}, (2,)}[
  Array{Float64, (1000,)}[0.72082, -0.912108, -1.50445, -0.168514, -0.845147, 1.28738, -1.8541, 0.553784, 0.910967, -0.990587, -1.30355, 4.33442, 0.340496, 1.93341, -0.27265, -0.511719, 2.678, 0.972373, -0.739487, 0.899491, 0.748802, -3.85564, 0.437073, 0.658192, 5.5707, -1.91962, -0.85577, 0.485921, -0.337624, -0.970272, -1.67684, 0.529491, -2.52335, 0.821659, 3.78922, 0.233106, -0.599746, 2.23991, 0.541413, 0.519276, 2.14553, 0.180119, 2.13595, 0.123904, 0.826682, -3.27598, -0.533292, -3.50769, -1.29085, -1.00033, 0.764426, -1.36862, -0.244353, -1.19191, -0.181027, -0.00516811, 1.1624, 1.7259, 1.55066, -2.50445, -0.989261, -1.94499, 0.3432, 0.732052, -2.65732, -0.11381, 2.74361, -1.7232, -0.287402, 2.96135, -4.07512, -0.620633, -0.31604, 5.13292, 2.78983, -0.64692, -1.24239, -4.73475, -1.67123, -1.55, 0.406181, -0.182283, 2.28334, 2.18602, -0.579399, -0.624398, 1.38422, -0.275717, -1.26635, -0.379608, 1.72728, -2.06478, -1.65666, -5.12257, -0.184935, 0.196591, 1.86247, -1.678, 1.97431, 1.7496, -0.610494, -0.820406, -1.19016, -0.0321213, -0.636188, 1.31002, -0.877218, 0.157012, -0.705744, 3.90347, 1.63703, 1.30319, -0.571444, 0.480216, -0.414678, 0.416854, 1.17437, 0.925627, -0.0736572, 2.19626, 3.85941, -0.543175, 0.459242, 1.16709, 3.09643, 1.92569, 1.60353, 0.704887, -0.659055, 0.0396975, 3.25197, -2.37492, -0.990999, 0.986482, -1.70749, 0.210084, 1.26726, 0.0151499, 0.376526, -0.312625, -2.92067, -1.18075, 0.98539, 1.05158, -0.775602, 1.92801, -1.06809, -2.09752, -4.45284, -0.98268, 1.78786, -2.22036, 2.08138, -2.03267, 1.32888, -0.198051, 3.55533, 0.666978, -0.694057, -1.55776, 0.240038, 1.26037, -0.931601, 1.79388, 1.92583, -0.208646, -0.529766, -0.244772, 0.168828, 0.933187, -0.613332, 0.125421, 1.65866, 0.990149, 0.151225, 0.940818, 1.35681, -0.0213763, 1.87834, -3.64334, 0.470449, -1.03778, 0.303389, -5.92145, 1.30606, -0.260177, 0.709418, 1.49066, 2.40003, 1.96203, -4.9054, -1.35993, 0.481991, 2.19546, -3.23724, -1.63428, -3.25545, -2.15486, 1.70901, -0.650395, 0.507646, -0.7183, -1.51431, 1.50111, 1.77218, -0.206932, -0.645471, 0.4367, -2.38718, 1.66559, -1.63724, 1.46268, 0.346721, 0.308025, -0.0024251, -0.517646, 0.0494753, 0.592862, 0.989833, 0.302414, -2.21155, -0.495165, 2.67568, -3.97401, -0.371667, 1.50382, 1.78153, 1.72959, 1.52483, -2.8816, -1.05865, 1.11193, 1.71004, 5.36947, 0.498235, 0.0834357, -0.0834138, -1.2455, 0.629825, 0.528859, -1.07563, -2.87789, -1.71753, 0.294299, -4.37226, -1.05423, -0.366973, 0.813247, -3.80367, 0.449278, -4.25771, 3.28259, -2.4837, 0.2588, 0.0441603, -1.69274, -0.0948468, -0.229166, 3.19665, -0.337744, -1.29679, 0.0192339, -2.14941, 0.41482, 3.37336, -2.13828, 0.977663, -1.49505, 1.59482, 0.727345, -2.97564, 3.12106, 2.21048, -0.0242042, 0.272953, -2.20495, -1.95677, -0.167427, 0.219381, -1.17943, -2.61119, 0.937008, -0.362421, -0.235291, -1.0005, 1.69667, -0.586154, 0.321429, -0.78469, 0.141694, 1.084, -2.60444, -2.6225, 1.50208, -2.15051, 0.325289, -2.77138, 0.515211, -3.44372, 0.609794, 1.45028, 2.66215, -2.22331, -1.94188, 1.00303, 0.0353033, 2.43052, -0.347336, -2.04086, 0.0730773, -0.879163, 1.06141, -0.767452, -0.435535, -1.97747, -2.20676, 0.423445, 1.64144, 0.20568, -1.93901, -0.449951, 1.81633, -1.6395, -0.802229, -1.23103, 1.37633, -0.759137, 1.50471, -2.83006, -0.451553, -0.207278, -2.83225, -1.92202, -0.206935, 2.41001, -0.477344, -0.741333, -1.24533, 1.63028, -5.6585, -1.07648, 1.62416, -2.48224, -0.841483, -2.48372, 2.77264, 1.83752, -0.212603, 0.366153, 3.03429, 1.11303, 0.953502, 3.33601, 0.23403, -0.750225, -0.882225, 3.90109, -2.0219, 0.946264, -0.215387, 1.19639, -2.61226, -0.0908718, -3.22671, 1.15361, 1.44933, -3.84698, 0.524839, -2.09136, 0.27374, 1.22286, 1.0618, -2.19249, -4.97289, 0.55232, -4.9197, -2.93919, -0.971969, -0.926121, -2.97543, -2.04813, 3.86798, 0.770829, 0.116357, -1.38711, 0.557163, 1.04014, -0.430101, -2.85436, -3.81544, -1.17688, -0.959916, -1.66399, 0.273732, 0.272044, -2.79079, 0.719207, -0.2477, 1.82924, 1.29171, -1.63285, 0.275512, -2.08496, 1.51639, -2.46413, -1.06508, 1.61075, 3.19916, -3.38219, -2.01166, 0.753497, 0.255645, 1.6308, -0.225032, 1.18988, -0.720526, -2.03424, 1.57901, -0.298524, 0.17083, -1.96575, -0.387409, 1.91631, -3.07082, -1.24057, -0.669696, 0.742382, -1.43988, 2.53948, -1.17986, 2.00987, -1.09513, 0.433979, 0.518757, 0.158446, -0.571454, 0.409301, -1.66569, -0.179481, 0.244025, 0.271703, 0.730026, 1.05786, 2.13143, 0.248839, 1.7654, 3.26025, 0.222154, 0.173134, -0.731309, 2.44948, 0.934224, 0.70511, 0.919636, -1.34257, -0.155799, 0.15772, 0.144855, -1.90555, 1.69486, 1.33281, 1.33461, 0.161927, -2.1641, -3.62613, 0.632322, -0.231686, -1.01368, 0.0480552, -2.41387, -0.941189, -1.73231, 2.25919, -1.0933, -1.65405, -0.120563, -1.0384, 0.893274, -2.47323, -0.997696, 1.45708, 0.798859, 1.99042, 1.78776, 1.29929, -1.50376, -0.349652, 2.36932, -0.111376, -3.97359, -2.6954, -4.57597, 2.09904, 3.82621, -4.00984, 0.378805, -0.497445, 0.710578, 1.46043, 3.22436, -0.473413, -2.93883, -3.28056, -0.673569, -0.131323, 1.4441, -1.90223, 0.159844, 0.13449, 0.285008, -2.44301, 2.67219, 1.64848, -0.458281, 1.01031, -0.689514, -2.2932, -2.04604, -2.7955, -1.7924, 1.31719, 0.376428, 1.08911, 1.92938, -0.291919, 1.20763, 3.24219, -0.76293, -1.63743, 3.40856, -0.928222, -1.09135, 1.03895, 0.532446, -2.39923, 0.800788, -0.101985, 0.359703, 0.207246, -5.07586, -1.61088, 0.687031, 0.751027, 1.53684, 1.49383, -0.617913, -0.0901992, 4.52218, 0.532665, -0.580305, -2.6651, -1.11121, -0.274285, 0.037523, -2.58581, 1.92506, -2.58352, -2.11412, 1.5313, -0.87713, -0.424087, -4.22825, 1.29681, -0.0685195, -0.492802, -1.77972, -0.959883, 3.61604, 3.51904, -2.6344, 1.94663, -2.34709, 3.33863, -0.825081, 0.489182, -3.67842, -2.10059, -4.27491, 2.82339, -3.27882, 2.20283, -1.35892, 0.465544, -0.81038, -2.238, 3.55098, 2.28521, 0.621257, 0.280647, -2.09363, 1.56789, 2.82801, 2.45688, -0.492333, 2.0071, 1.73706, -1.45263, 0.961081, -0.228589, -1.40215, 0.56613, -0.13568, 2.71831, -4.27301, 0.715645, 0.184315, -2.04141, 0.522711, 2.46249, 0.663146, -3.28402, -0.427201, -2.43587, 2.50318, -1.23266, -0.92527, 0.145458, 2.27729, 1.28203, 2.56818, 1.70639, -2.06883, 0.0883358, -0.366195, 1.33974, -3.62328, 0.846921, -1.884, -1.6697, -0.145317, -1.23034, 1.33211, 2.52403, 0.386575, -0.547181, 1.68258, 0.167139, -0.800079, -1.18615, -1.10304, 1.40284, 0.412067, 1.27099, -1.80421, 2.49157, -2.20155, 4.47845, 1.22414, 3.15478, 0.398215, -0.701938, -2.44128, -1.04834, -1.27757, -0.964087, 1.07856, -3.38165, 0.636051, 1.53104, -1.28338, -0.557676, -3.07412, -2.83953, 1.18592, -0.798774, 0.65233, -2.88487, 2.7443, 0.41034, 1.37405, -0.479142, 2.24641, 1.42072, -0.786947, 0.762326, -0.0261038, -4.2118, 0.944587, 0.300708, -1.63242, 0.754803, -0.911329, -1.38567, -2.48388, -1.10331, -3.71863, -0.717019, 0.328189, 1.50944, 2.23131, -1.55852, -0.394423, -1.41138, 0.758694, 1.43436, 1.30534, 3.74054, -0.463584, -0.123704, 2.78718, -7.95631, -0.42505, -0.511133, -3.41561, 2.83493, -4.08921, -1.38095, -0.482917, -0.241786, 1.25513, 2.14885, -1.6695, 0.0676986, -1.22784, -1.99256, -1.2428, 0.981821, 0.189296, 2.39242, -1.11715, -2.54004, 3.04901, 1.80216, 0.640681, 3.28078, -1.89347, -0.694267, -1.96369, 4.64272, 1.69478, -0.908834, -0.861721, 1.77232, 2.00712, -0.559705, 1.60123, 1.0918, -1.06279, -0.0905733, -0.822047, -4.55787, 0.155594, -0.216936, -1.08003, 3.36598, 1.63846, 0.454729, -2.03081, -0.105481, -0.567435, 0.684344, 2.6309, 1.92563, 0.225969, -0.680154, 0.490995, 0.940333, 1.54423, -0.503758, -1.07977, -1.07187, 0.725978, 1.39473, -3.90788, 3.57377, -3.66663, -1.58928, -0.404127, -1.02432, -1.08637, 0.47841, -0.47147, 2.43562, 1.20496, 0.482047, -2.06404, 0.255083, -0.811084, -0.0307394, 1.30916, 0.620064, -1.77147, 2.00531, -1.07547, 0.703966, 1.66023, -0.672188, -3.54917, 0.30056, -0.694073, 0.697182, -2.40318, 0.206597, -0.48805, 1.97261, -3.08245, 3.47995, 1.37043, -1.51922, -0.55879, 2.51809, 2.92809, 1.41288, -1.94036, 0.54329, 0.494117, 0.791931, -0.0215724, 1.02218, -0.448478, 3.06228, 0.313309, -0.475974, -0.732167, -0.406273, -1.17917, 0.678202, -0.19166, 2.78319, -0.339229, -2.44694, 1.03617, -1.24447, 0.0843337, -1.9547, -1.66603, 0.760044, -3.04503, 3.07949, 2.82467, 2.96442, 1.00319, -0.293807, 0.135958, -1.10321, 2.31076, 0.833332, -3.20097, -0.255015, -2.63722, 0.200439, 1.9494, 1.51688, 0.882514, -0.19734, -0.583855, 0.474634, 1.9267, 3.81992, 0.107102, -0.974678, -0.40871, 0.00224294, -3.28316, -0.792176, -0.584251, -0.240237, 0.496486, 1.43441, 2.50402, -1.57538, -0.209294, -0.904751, -0.873172, 2.19537, 1.49313, 0.267617, -0.244904, -0.0138365, 0.93258, 1.85246, 2.68343, -0.825845, 0.641328, -2.572, -0.30803, -0.350258, 1.69465, -1.74627, 2.12174, -0.897053, 0.782217, -1.41928, -2.99199, -0.154668, 1.00256, 0.475173, 0.16824, -0.312288, 0.162442, -1.02045, -2.511, 2.4863, 1.33319, -0.38216, 0.74554, 1.68314, -0.534766, 0.410132, 2.54491, 0.119709, -0.525284, -2.16804, -0.095394, 1.38005, -0.261311, 2.18236, 0.239931, -0.158363, -0.290056, -1.97886, -1.26176, 2.3003, 3.6631, -2.29696, 0.737953, 1.28151, -1.85829, 0.29353, 4.19997, 0.166769, 0.479012, 3.05101, -0.154652, 2.56245, 0.130025, -1.78061, -0.895998, 2.03431, 1.35213, 1.54189, 1.27698, -1.02665, -1.76343, -0.151337, 1.24673, -1.33105, -2.03051, -0.874295, 1.24487, -0.237201, -0.489727, -2.02711, 0.67475, -0.83998, -1.91802, 0.189903, -0.218745, 2.73422, -1.26228, 1.66938, 1.28121, -1.08937, -0.0706697, -0.751791, 1.31776, -1.35563, -3.20648, 1.57778, -1.58572, 3.67206, -2.67527, 0.655417, 0.762302, 2.44214, -2.8177, -0.209669, -0.593204, 0.772296, -1.44173, -1.93731, 0.064943, -2.67898, -0.885738, 1.26428, 0.258637, 1.77936, -0.67721, -0.563525, -1.40634, -1.72326, 0.916171, 0.440768, -0.121693, -0.767061, -1.77073, -0.356421, -0.267845, 0.318749, -2.57159, -0.571963, -1.856, 1.02888, -0.127033, 1.93653, -0.470328, 1.50083, 2.37361, 0.0705632, -0.109358],
  Array{Float64, (1000,)}[0.519581, 0.831941, 2.26337, 0.028397, 0.714274, 1.65734, 3.43769, 0.306677, 0.82986, 0.981263, 1.69924, 18.7872, 0.115937, 3.73806, 0.0743379, 0.261856, 7.17169, 0.945509, 0.54684, 0.809083, 0.560704, 14.8659, 0.191033, 0.433216, 31.0327, 3.68493, 0.732342, 0.23612, 0.11399, 0.941428, 2.8118, 0.280361, 6.36727, 0.675124, 14.3582, 0.0543386, 0.359695, 5.01721, 0.293128, 0.269648, 4.60331, 0.0324428, 4.5623, 0.0153522, 0.683403, 10.7321, 0.284401, 12.3039, 1.6663, 1.00066, 0.584347, 1.87312, 0.0597084, 1.42064, 0.0327708, 2.67093e-05, 1.35117, 2.97873, 2.40456, 6.27228, 0.978638, 3.783, 0.117786, 0.5359, 7.06136, 0.0129527, 7.52741, 2.96941, 0.0825998, 8.76959, 16.6066, 0.385185, 0.0998813, 26.3469, 7.78315, 0.418506, 1.54354, 22.4178, 2.79302, 2.40248, 0.164983, 0.0332271, 5.21364, 4.77866, 0.335703, 0.389873, 1.91607, 0.0760197, 1.60365, 0.144102, 2.9835, 4.26333, 2.74452, 26.2407, 0.0342009, 0.038648, 3.4688, 2.81567, 3.89791, 3.06109, 0.372703, 0.673067, 1.41647, 0.00103178, 0.404735, 1.71614, 0.769512, 0.0246528, 0.498074, 15.237, 2.67988, 1.69831, 0.326548, 0.230608, 0.171958, 0.173768, 1.37914, 0.856786, 0.00542538, 4.82356, 14.8951, 0.295039, 0.210903, 1.3621, 9.5879, 3.70828, 2.5713, 0.496866, 0.434353, 0.00157589, 10.5753, 5.64024, 0.982079, 0.973148, 2.91553, 0.0441353, 1.60596, 0.000229521, 0.141771, 0.0977347, 8.53033, 1.39418, 0.970994, 1.10582, 0.601558, 3.7172, 1.14082, 4.39958, 19.8278, 0.96566, 3.19644, 4.93002, 4.33214, 4.13176, 1.76591, 0.0392243, 12.6404, 0.44486, 0.481716, 2.42663, 0.0576183, 1.58854, 0.867881, 3.21801, 3.70883, 0.0435333, 0.280652, 0.0599136, 0.028503, 0.870838, 0.376177, 0.0157305, 2.75114, 0.980395, 0.0228691, 0.885139, 1.84094, 0.000456944, 3.52815, 13.274, 0.221322, 1.07699, 0.0920451, 35.0635, 1.70578, 0.0676923, 0.503275, 2.22206, 5.76012, 3.84955, 24.0629, 1.8494, 0.232315, 4.82005, 10.4797, 2.67088, 10.598, 4.64341, 2.92073, 0.423014, 0.257704, 0.515955, 2.29314, 2.25333, 3.14063, 0.042821, 0.416632, 0.190707, 5.69864, 2.77418, 2.68055, 2.13944, 0.120215, 0.0948793, 5.88109e-06, 0.267958, 0.0024478, 0.351485, 0.979769, 0.0914545, 4.89093, 0.245189, 7.15928, 15.7927, 0.138136, 2.26148, 3.17385, 2.99148, 2.32512, 8.30363, 1.12074, 1.23639, 2.92423, 28.8312, 0.248238, 0.00696152, 0.00695786, 1.55128, 0.39668, 0.279691, 1.15697, 8.28228, 2.94991, 0.0866118, 19.1166, 1.11139, 0.13467, 0.66137, 14.4679, 0.201851, 18.1281, 10.7754, 6.16878, 0.0669772, 0.00195013, 2.86539, 0.00899591, 0.052517, 10.2186, 0.114071, 1.68166, 0.000369941, 4.61998, 0.172076, 11.3796, 4.57223, 0.955825, 2.23518, 2.54345, 0.52903, 8.85441, 9.74099, 4.88621, 0.000585842, 0.0745033, 4.86182, 3.82896, 0.0280318, 0.0481282, 1.39105, 6.81831, 0.877984, 0.131349, 0.0553617, 1.001, 2.87868, 0.343577, 0.103316, 0.615739, 0.0200771, 1.17506, 6.78312, 6.87753, 2.25623, 4.62467, 0.105813, 7.68056, 0.265442, 11.8592, 0.371849, 2.1033, 7.08703, 4.94313, 3.77089, 1.00607, 0.00124632, 5.90744, 0.120642, 4.1651, 0.00534029, 0.772927, 1.12659, 0.588982, 0.18969, 3.91039, 4.86977, 0.179306, 2.69433, 0.0423042, 3.75976, 0.202456, 3.29905, 2.68796, 0.643572, 1.51545, 1.89429, 0.576289, 2.26416, 8.00924, 0.2039, 0.042964, 8.02164, 3.69418, 0.0428222, 5.80814, 0.227858, 0.549575, 1.55084, 2.65782, 32.0186, 1.15881, 2.63788, 6.16151, 0.708094, 6.16889, 7.68754, 3.37649, 0.0452, 0.134068, 9.20694, 1.23885, 0.909167, 11.1289, 0.0547703, 0.562837, 0.778321, 15.2185, 4.08808, 0.895415, 0.0463916, 1.43136, 6.82392, 0.00825769, 10.4116, 1.33082, 2.10055, 14.7992, 0.275456, 4.37379, 0.0749334, 1.49538, 1.12743, 4.80703, 24.7296, 0.305058, 24.2034, 8.63885, 0.944724, 0.8577, 8.85318, 4.19484, 14.9613, 0.594177, 0.013539, 1.92408, 0.310431, 1.08189, 0.184987, 8.14739, 14.5576, 1.38504, 0.921439, 2.76886, 0.0749291, 0.0740082, 7.78851, 0.517259, 0.0613551, 3.3461, 1.66852, 2.6662, 0.0759068, 4.34704, 2.29943, 6.07196, 1.13439, 2.59452, 10.2346, 11.4392, 4.04676, 0.567757, 0.0653543, 2.65951, 0.0506393, 1.41581, 0.519158, 4.13813, 2.49328, 0.0891169, 0.029183, 3.86418, 0.150086, 3.67224, 9.42994, 1.539, 0.448492, 0.551131, 2.07326, 6.44897, 1.39207, 4.0396, 1.1993, 0.188337, 0.269108, 0.0251052, 0.326559, 0.167528, 2.77453, 0.0322133, 0.0595481, 0.0738223, 0.532938, 1.11906, 4.54298, 0.061921, 3.11665, 10.6292, 0.0493525, 0.0299754, 0.534813, 5.99997, 0.872774, 0.497181, 0.84573, 1.80248, 0.0242735, 0.0248756, 0.0209829, 3.63112, 2.87256, 1.77639, 1.78119, 0.0262204, 4.68332, 13.1488, 0.399831, 0.0536785, 1.02755, 0.0023093, 5.82678, 0.885837, 3.0009, 5.10394, 1.19531, 2.73588, 0.0145354, 1.07827, 0.797938, 6.11688, 0.995397, 2.12309, 0.638176, 3.96178, 3.19609, 1.68814, 2.26129, 0.122257, 5.61366, 0.0124046, 15.7895, 7.26519, 20.9395, 4.40597, 14.6399, 16.0789, 0.143493, 0.247452, 0.50492, 2.13286, 10.3965, 0.22412, 8.63675, 10.7621, 0.453696, 0.0172457, 2.08542, 3.61848, 0.0255501, 0.0180875, 0.0812293, 5.96828, 7.14058, 2.71749, 0.210021, 1.02073, 0.475429, 5.25876, 4.18628, 7.81481, 3.21271, 1.73498, 0.141698, 1.18616, 3.72251, 0.0852167, 1.45838, 10.5118, 0.582063, 2.68116, 11.6183, 0.861596, 1.19106, 1.07942, 0.283499, 5.75631, 0.641262, 0.0104009, 0.129387, 0.0429511, 25.7644, 2.59494, 0.472012, 0.564041, 2.36188, 2.23152, 0.381816, 0.0081359, 20.4501, 0.283732, 0.336753, 7.10274, 1.23479, 0.0752323, 0.00140798, 6.68641, 3.70584, 6.67455, 4.46949, 2.34489, 0.769358, 0.17985, 17.8781, 1.68172, 0.00469493, 0.242854, 3.16742, 0.921375, 13.0757, 12.3836, 6.94007, 3.78938, 5.50885, 11.1464, 0.680758, 0.239299, 13.5307, 4.4125, 18.2748, 7.97153, 10.7506, 4.85244, 1.84667, 0.216731, 0.656716, 5.00864, 12.6095, 5.2222, 0.38596, 0.0787629, 4.3833, 2.45829, 7.99763, 6.03626, 0.242392, 4.02844, 3.01737, 2.11014, 0.923677, 0.0522528, 1.96602, 0.320503, 0.0184091, 7.38919, 18.2586, 0.512148, 0.0339722, 4.16737, 0.273227, 6.06384, 0.439763, 10.7848, 0.182501, 5.93349, 6.2659, 1.51945, 0.856125, 0.0211581, 5.18605, 1.64359, 6.59555, 2.91177, 4.28007, 0.00780321, 0.134099, 1.79489, 13.1281, 0.717274, 3.54944, 2.7879, 0.021117, 1.51374, 1.77453, 6.37071, 0.14944, 0.299407, 2.83109, 0.0279355, 0.640126, 1.40696, 1.21669, 1.96795, 0.169799, 1.61542, 3.25516, 6.20793, 4.84684, 20.0565, 1.49853, 9.95267, 0.158575, 0.492717, 5.95985, 1.09902, 1.63219, 0.929465, 1.1633, 11.4355, 0.404561, 2.34408, 1.64705, 0.311002, 9.4502, 8.06292, 1.40641, 0.63804, 0.425534, 8.32249, 7.53118, 0.168379, 1.888, 0.229577, 5.04634, 2.01844, 0.619286, 0.581142, 0.000681407, 17.7392, 0.892245, 0.0904255, 2.66478, 0.569728, 0.830521, 1.92009, 6.16967, 1.21729, 13.8282, 0.514116, 0.107708, 2.27841, 4.97874, 2.42898, 0.155569, 1.992, 0.575617, 2.0574, 1.70392, 13.9916, 0.21491, 0.0153027, 7.76836, 63.3029, 0.180667, 0.261256, 11.6664, 8.03682, 16.7217, 1.90702, 0.233209, 0.0584606, 1.57536, 4.61755, 2.78723, 0.0045831, 1.50758, 3.97028, 1.54455, 0.963972, 0.0358328, 5.7237, 1.24802, 6.4518, 9.29646, 3.2478, 0.410472, 10.7635, 3.58524, 0.482007, 3.85609, 21.5549, 2.87226, 0.82598, 0.742564, 3.14113, 4.02853, 0.31327, 2.56393, 1.19204, 1.12952, 0.00820352, 0.675761, 20.7742, 0.0242094, 0.0470614, 1.16647, 11.3298, 2.68455, 0.206779, 4.12421, 0.0111263, 0.321982, 0.468327, 6.92162, 3.70804, 0.0510618, 0.462609, 0.241076, 0.884225, 2.38465, 0.253772, 1.1659, 1.1489, 0.527045, 1.94527, 15.2715, 12.7718, 13.4442, 2.52582, 0.163319, 1.04923, 1.1802, 0.228876, 0.222284, 5.93223, 1.45192, 0.232369, 4.26025, 0.0650674, 0.657857, 0.000944911, 1.71391, 0.384479, 3.13811, 4.02128, 1.15665, 0.495568, 2.75635, 0.451837, 12.5966, 0.090336, 0.481737, 0.486063, 5.77527, 0.0426824, 0.238193, 3.8912, 9.50149, 12.1101, 1.87807, 2.30803, 0.312247, 6.3408, 8.57373, 1.99622, 3.765, 0.295164, 0.244151, 0.627155, 0.000465368, 1.04485, 0.201132, 9.37755, 0.0981628, 0.226552, 0.536069, 0.165058, 1.39044, 0.459958, 0.0367335, 7.74614, 0.115076, 5.98751, 1.07365, 1.5487, 0.00711218, 3.82084, 2.77566, 0.577667, 9.27224, 9.48325, 7.97875, 8.78781, 1.00638, 0.0863227, 0.0184847, 1.21708, 5.33961, 0.694442, 10.2462, 0.0650324, 6.95494, 0.0401756, 3.80016, 2.30092, 0.778831, 0.0389431, 0.340887, 0.225278, 3.71219, 14.5918, 0.0114708, 0.949997, 0.167044, 5.0308e-06, 10.7792, 0.627543, 0.341349, 0.0577137, 0.246498, 2.05752, 6.27013, 2.48182, 0.0438041, 0.818574, 0.762429, 4.81965, 2.22942, 0.0716188, 0.0599782, 0.000191449, 0.869705, 3.43161, 7.20078, 0.68202, 0.411302, 6.6152, 0.0948825, 0.122681, 2.87184, 3.04945, 4.50178, 0.804704, 0.611864, 2.01435, 8.95202, 0.023922, 1.00513, 0.225789, 0.0283046, 0.0975235, 0.0263875, 1.04132, 6.30511, 6.18167, 1.77739, 0.146046, 0.555829, 2.83295, 0.285974, 0.168208, 6.47658, 0.0143302, 0.275923, 4.7004, 0.00910001, 1.90454, 0.0682833, 4.7627, 0.057567, 0.0250789, 0.0841327, 3.91588, 1.59205, 5.29136, 13.4183, 5.27604, 0.544575, 1.64228, 3.45323, 0.0861599, 17.6397, 0.0278118, 0.229453, 9.30869, 0.0239173, 6.56617, 0.0169064, 3.17056, 0.802812, 4.13842, 1.82825, 2.37742, 1.63068, 1.05402, 3.1097, 0.022903, 1.55433, 1.77168, 4.12295, 0.764391, 1.54971, 0.0562642, 0.239833, 4.10918, 0.455288, 0.705566, 3.67879, 0.036063, 0.0478496, 7.47595, 1.59335, 2.78685, 1.64149, 1.18672, 0.00499421, 0.565189, 1.73649, 1.83772, 10.2815, 2.48939, 2.51452, 13.484, 7.15706, 0.429572, 0.581105, 5.96407, 7.93941, 0.0439609, 0.351892, 0.596441, 2.07857, 3.75317, 0.00421759, 7.17695, 0.784533, 1.5984, 0.0668929, 3.16611, 0.458613, 0.31756, 1.9778, 2.96963, 0.839369, 0.194276, 0.0148092, 0.588383, 3.13549, 0.127036, 0.0717408, 0.101601, 6.61306, 0.327142, 3.44475, 1.05859, 0.0161375, 3.75016, 0.221208, 2.25248, 5.63401, 0.00497916, 0.0119592]]
==========
Tuple{T, T}
==========
Array{Float64, (1,)}[0.0183511]
==========
Array{Float64, ()}[0.0183511]
==========

!!! ERROR in jl_ -- ABORTING !!!
==========
~~~~~~~~~~ ptr queue bottom ~~~~~~~~~~

[65688] signal (6): Abort trap: 6
in expression starting at REPL[12]:1
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 384347194 (Pool: 384063169; Big: 284025); GC: 296
Abort trap: 6

This is another descendant of #1769, but distinct from #2197, which is now fixed.

I'll try to minimise to get rid of any TuringLang dependencies.

@wsmoses
Copy link
Member

wsmoses commented Jan 7, 2025

Just because GC errors are massively tricky (and often bugs in Julia itself), would you be able get rid of the distributions dep?

@mhauru
Copy link
Contributor Author

mhauru commented Jan 10, 2025

Progress, though still not very minimal. Now only depends on DPPL and Distributions:

module MWE

using Distributions
using Enzyme
using Turing: DynamicPPL
using ADTypes
using Random

Random.seed!(23)

mode = Enzyme.WithPrimal(Enzyme.set_runtime_activity(Enzyme.Reverse))

function data_poly(x)
    return hcat([x]...)
end

function model(__model__, __varinfo__, __context__, x::Any;)
    X = data_poly(x)
    (var"##value#229", __varinfo__) = (DynamicPPL.tilde_assume!!)(__context__, Normal(0, 1), DynamicPPL.VarName{:σ}(), __varinfo__)
    var"##retval#231" = for i = 1:1000
        mu = X[i, 1]
        dist = Uniform(mu, mu + 1.0)
        DynamicPPL.tilde_observe!!(__context__, dist, 0.0, __varinfo__)
    end
    return (0.0, __varinfo__)
end

function model(x::Any;)
    return DynamicPPL.Model(model, NamedTuple{(:x,)}((x,));)
end

struct EnzymeGradientLogDensity{L,M<:Union{Enzyme.ForwardMode,Enzyme.ReverseMode},S}
    ℓ::L
    mode::M
    shadow::S # only used in forward mode
end

function logdensity_and_gradient(ldf, x)
    ∂ℓ_∂x = zero(x)
    _, y = Enzyme.autodiff(mode, logdensity, Enzyme.Active,
        Enzyme.Const(ldf), Enzyme.Duplicated(x, ∂ℓ_∂x))
    y, ∂ℓ_∂x
end

struct LogDensityFunction{V,M,C}
    "varinfo used for evaluation"
    varinfo::V
    "model used for evaluation"
    model::M
    "context used for evaluation; if `nothing`, `leafcontext(model.context)` will be used when applicable"
    context::C
end

function logdensity(f::LogDensityFunction, θ::AbstractVector)
    context = f.context
    vi_new = DynamicPPL.unflatten(f.varinfo, context, θ)
    return DynamicPPL.getlogp(last(DynamicPPL.evaluate!!(f.model, vi_new, context)))
end

function initialstep( model, vi)
    ldf = LogDensityFunction(
        vi,
        model,
        DynamicPPL.leafcontext(model.context),
    )
    ∂logπ∂θ(x) = logdensity_and_gradient(ldf, x)

    # function logp(x)
    #     vi = DynamicPPL.unflatten(vi, x)
    #     logp = DynamicPPL.evaluate!!(model, vi, DynamicPPL.SamplingContext(Random.default_rng(), DynamicPPL.SampleFromPrior(), DynamicPPL.leafcontext(model.context)),)[2].logp[]
    #     return logp
    # end
    #
    # function ∂logπ∂θ(x)
    #     return Enzyme.autodiff(mode, Enzyme.Const(logp), Enzyme.Active, Enzyme.Duplicated(x, zero(x)))
    # end

    hamiltonian = (; ∂ℓπ∂θ=∂logπ∂θ)
    return (; hamiltonian=hamiltonian)
end

x = rand(Normal(0, 1.5), 1000)

m = model(x)

vi_original = DynamicPPL.VarInfo(m)
state = initialstep(
    m,
    vi_original;
)
theta = [1.2260841057562286]

for _ in 1:5000
    state.hamiltonian.∂ℓπ∂θ(theta)
end

end

Will have to attend to other stuff now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gc garbage collection segfault
Projects
None yet
Development

No branches or pull requests

2 participants