FluxML / IRTools.jl

Mike's Little Intermediate Representation
MIT License
111 stars 36 forks source link

iterate(::typeof(copyto!)) #126

Open maartenvd opened 3 months ago

maartenvd commented 3 months ago

Trying @dynamo to "highjack" all calls to a certain previously defined methods. I seem to run into issues when combining with broadcasting:


using IRTools: IR, @dynamo, recurse!
@dynamo function quicktest(a...)
    ir = IR(a...)
    isnothing(ir) && return
    recurse!(ir)
    return ir
end

quicktest() do
  a = rand(5);
  reduce(&, a .== a)
end

fails on 1.10.4 with


ERROR: MethodError: no method matching iterate(::typeof(copyto!))

Closest candidates are:
  iterate(::Pkg.Types.Manifest, ::Int64)
   @ Pkg ~/julia/usr/share/julia/stdlib/v1.10/Pkg/src/Types.jl:314
  iterate(::Pkg.Types.Manifest)
   @ Pkg ~/julia/usr/share/julia/stdlib/v1.10/Pkg/src/Types.jl:313
  iterate(::Combinatorics.IntegerPartitions)
   @ Combinatorics ~/.julia/packages/Combinatorics/Udg6X/src/partitions.jl:20
  ...

Stacktrace:
  [1] macro expansion
    @ ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0 [inlined]
  [2] quicktest(::typeof(iterate), ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:177
  [3] _foldl_impl
    @ ./reduce.jl:56 [inlined]
  [4] quicktest(::typeof(Base._foldl_impl), ::Base.BottomRF{typeof(&)}, ::Base._InitialValue, ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
  [5] foldl_impl
    @ ./reduce.jl:48 [inlined]
  [6] quicktest(::typeof(Base.foldl_impl), ::Base.BottomRF{typeof(&)}, ::Base._InitialValue, ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
  [7] mapfoldl_impl
    @ ./reduce.jl:44 [inlined]
  [8] quicktest(::typeof(Base.mapfoldl_impl), ::typeof(identity), ::typeof(&), ::Base._InitialValue, ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
  [9] #mapfoldl#298
    @ ./reduce.jl:175 [inlined]
 [10] 
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
 [11] mapfoldl
    @ ./reduce.jl:175 [inlined]
 [12] quicktest(::typeof(mapfoldl), ::typeof(identity), ::typeof(&), ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
 [13] #mapreduce#302
    @ ./reduce.jl:307 [inlined]
 [14] quicktest(::Base.var"##mapreduce#302", ::@Kwargs{}, ::typeof(mapreduce), ::typeof(identity), ::typeof(&), ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
 [15] mapreduce
    @ ./reduce.jl:307 [inlined]
 [16] quicktest(::typeof(mapreduce), ::typeof(identity), ::typeof(&), ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
 [17] #reduce#304
    @ ./reduce.jl:490 [inlined]
 [18] quicktest(::Base.var"##reduce#304", ::@Kwargs{}, ::typeof(reduce), ::typeof(&), ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
 [19] reduce
    @ ./reduce.jl:490 [inlined]
 [20] quicktest(::typeof(reduce), ::typeof(&), ::typeof(copyto!))
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
 [21] #10
    @ ~/projects/MPSKitExperimental.jl/examples/qchem.jl:157 [inlined]
 [22] quicktest(args::var"#10#11")
    @ Main ~/.julia/packages/IRTools/ntYVg/src/reflection/dynamo.jl:0
 [23] top-level scope
    @ ~/projects/MPSKitExperimental.jl/examples/qchem.jl:155
Some type information was truncated. Use `show(err)` to see complete types.
maartenvd commented 3 months ago

Also, if recurse!(::Nothing) was defined, couldn't the dynamo example be even prettier:

@dynamo function quicktest(a...)
    ir = IR(a...)
    recurse!(ir)
    return ir
end
oxinabox commented 2 months ago

TBH @dynamo is largely unmaintained as it isn't used by Zygote, and keeping Zygote working is main thing that prompts PRs against this repo

maartenvd commented 2 months ago

That is sad, is there a maintained alternative? I tried cassette but run into the llvmcall issue, cassetteoverlay runs into different issues still. At some point there ways mixtape, but that seems discontinued too

What is working, but is far too slow, is overloading juliainterpreters framedict