Skip to content

Implement allocation tests for FastLDF #1126

@penelopeysm

Description

@penelopeysm

I am actually quite stumped as to why @allocated behaves differently compared to Chairmarks.

module Foo

using DynamicPPL, Distributions, Chairmarks, LogDensityProblems
@model function f()
    x ~ Normal()
    1.0 ~ Normal(x)
end
@model function submodel_inner()
    m ~ Normal(0, 1)
    s ~ Exponential()
    return (m=m, s=s)
end
@model function submodel_outer()
    params ~ to_submodel(submodel_inner())
    y ~ Normal(params.m, params.s)
    1.0 ~ Normal(y)
end
for model in (f(), submodel_inner(), submodel_outer())
    vi = VarInfo(model)
    fldf = DynamicPPL.Experimental.FastLDF(model, DynamicPPL.getlogjoint_internal, vi)
    x = vi[:]
    LogDensityProblems.logdensity(fldf, x) # force compilation
    println(median(@be LogDensityProblems.logdensity(fldf, x)).allocs)
    println(@allocations LogDensityProblems.logdensity(fldf, x))
end

end

prints 0.0, 2, 0.0, 2, 0.0, 2.

The 2 allocations come from not interpolating fldf and x (verifiable by running @allocations fldf and @allocations x, which each return 1). So far so good.

But then if you replace the module with @testset, it prints: 0.0, 2, 0.0, 2, 28.0, 29!

Not sure if this relates to #1127.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions