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

No method matching metadatatype(..., ::Type{Type}) #126

Open
baggepinnen opened this issue Jun 1, 2019 · 4 comments
Open

No method matching metadatatype(..., ::Type{Type}) #126

baggepinnen opened this issue Jun 1, 2019 · 4 comments
Assignees

Comments

@baggepinnen
Copy link

Not sure if this is my fault or not, but can't seem to figure out why I get this error. MWE below

struct Particles{T} <: Real
    particles::Vector{T}
end

p = Particles(randn(10))

Base.:(^)(p::Particles,r) = Particles(p.particles.^r)
Base.:(>)(p::Particles, r) = Particles(map(>, p.particles, r))

function negsquare(x)
    x > 0 ? x^2 : -x^2
end
Base.promote_rule(::Type{Particles{T}}, ::Type{T}) where T = Particles{T}
Base.convert(::Type{Particles{T}}, f::T) where T<:Real = Particles{T}(fill(f,10))

using Cassette, IRTools
import Cassette: tag, untag, enabletagging, metadatatype, metadata, @context, overdub, prehook, recurse
@context Ctx;
metadatatype(::Type{<:Ctx}, ::Type{Particles{T}}) where T<:Real = T
ctx = enabletagging(Ctx(), negsquare)
tagged = tag(p, ctx, 1.)
function overdub(ctx::Ctx, f::typeof(>), p::Particles, r)
    recurse(ctx, f, mean(p.particles),r)
end

julia> res = overdub(ctx, negsquare, tagged)
ERROR: MethodError: no method matching metadatatype(::Type{Cassette.Context{nametype(Ctx),Nothing,Cassette.Tag{nametype(Ctx),0x56926ddf0ee5f009,Nothing},getfield(Cassette, Symbol("##PassType#373")),IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Nothing}}, ::Type{Type})
@vchuravy vchuravy self-assigned this Jun 1, 2019
@vchuravy
Copy link
Member

vchuravy commented Jun 1, 2019

Can you describe a bit more about what you want to achieve?

If I use the definition:

metadatatype(::Type{<:Ctx}, ::Type{P}) where {T, P<:Particles{T}} = T
metadatatype(::Type{<:Ctx}, ::Type{<:Type}) = Cassette.NoMetaData

I get further, but I also encounter:

julia> overdub(ctx, negsquare, tagged)
ERROR: TypeError: non-boolean (Particles{Bool}) used in boolean context
Stacktrace:
 [1] overdub(::Cassette.Context{nametype(Ctx),Nothing,Cassette.Tag{nametype(Ctx),0xa8104ee550fd8595,Nothing},getfield(Cassette, Symbol("##PassType#365")),IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Nothing}, ::typeof(negsquare), ::Cassette.Tagged{Cassette.Tag{nametype(Ctx),0xa8104ee550fd8595,Nothing},Particles{Float64},Float64,NamedTuple{(:particles,),Tuple{Cassette.Immutable{Cassette.Meta{Cassette.NoMetaData,Array{Cassette.Meta{Cassette.NoMetaData,Cassette.NoMetaMeta},1}}}}},Cassette.Context{nametype(Ctx),Nothing,Cassette.Tag{nametype(Ctx),0xa8104ee550fd8595,Nothing},getfield(Cassette, Symbol("##PassType#365")),IdDict{Module,Dict{Symbol,Cassette.BindingMeta}},Nothing}}) at /home/vchuravy/ctx.jl:11
 [2] top-level scope at none:0

@baggepinnen
Copy link
Author

baggepinnen commented Jun 1, 2019

Currently I'm just exploring the potential of Cassette and am trying some primitive things. I was using the Juliacon 2018 seminar by Jarrett as a documentation for the tagging mechanism and didn't really get it to work.

The problem you ended up in was what I was trying to get around. The final goal is to modify branches in code to allow a n-vector of bool appearing in a boolean context to execute the branching code n times, one for each element of the vector. I have a long way to go before I'm competent enough to pull that off though, these are just my first few steps into the unknown.

It's unclear to me why my signalture in metadatatype did not work, and why I also have to define the method metadatatype(::Type{<:Ctx}, ::Type{<:Type}) = Cassette.NoMetaData

@vchuravy
Copy link
Member

vchuravy commented Jun 1, 2019

Yeah that seems odd and may be a subtyping bug. @jrevels any suggestions?

@jarredbarber
Copy link

To fix the Boolean context issue you have to write an @pass macro to statically transform the conditional branches. It's quite a bit more complicated than just writing an overdub call.

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

No branches or pull requests

3 participants