Parametron.jl icon indicating copy to clipboard operation
Parametron.jl copied to clipboard

`LazyExpression`s and broadcasting

Open schmrlng opened this issue 7 years ago • 2 comments

When defining constraints like @constraint(m, v >= 0) for Vector-valued v on Julia 0.7 we get this deprecation warning

┌ Warning: `a::AbstractArray - b::Number` is deprecated, use `a .- b` instead.
│   caller = optimize_toplevel at lazyexpression.jl:59 [inlined]
└ @ Core ~/.julia/packages/SimpleQP/pHx4o/src/lazyexpression.jl:59

This comes from the minus expressions here but naively replacing those with broadcasted equivalents interferes with the current LazyExpression optimizations. I feel like the answer is to either integrate base broadcasting into LazyExpressions (a potentially daunting task) or to make a localized fix (e.g., maybe turn just this line into a broadcasted equivalent?); what do you think?

schmrlng avatar Aug 08 '18 02:08 schmrlng

I've been using @constraint(m, v >= zeros(n)) everywhere so I hadn't run into this yet. That will always remain a valid way to do this, so it's certainly an option for if you want to get rid of the depwarns on 0.7 right now.

Transforming . syntax into broadcast calls in @expression will be annoying, but is doable. I'm hoping that Julia will provide a function for this kind of 'normalization' of the AST (it used to just be expand), but its 0.7-equivalent, lower, unfortunately returns something entirely different.

After that's done, we'd need to add optimize methods for broadcast calls. Unfortunately, it's not as simply as turning broadcast into broadcast!, because if v is a Vector{<:AffineFunction}, then scalar subtraction also allocates.

So it's certainly not impossible, but it's quite a task to get this right.

tkoolen avatar Aug 08 '18 20:08 tkoolen

This probably also becomes easier if we switch to a Cassette-based implementation.

tkoolen avatar Aug 09 '18 15:08 tkoolen