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

[ENHANCEMENT] Support broadcast calls in @interface #3

Open
mtfishman opened this issue Dec 10, 2024 · 0 comments
Open

[ENHANCEMENT] Support broadcast calls in @interface #3

mtfishman opened this issue Dec 10, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@mtfishman
Copy link
Member

mtfishman commented Dec 10, 2024

Support broadcast calls in @interface, for example, @interface SparseArrayInterface 2 .* a and @interface SparseArrayInterface b .= 2 .* a.

I believe those lower to Broadcast.materialize(Broadcast.broadcasted(*, 2, a)) and Broadcast.materialize!(b, Broadcast.broadcasted(*, 2, a)) respectively, so those would need @interface overloads.

Broadcast.materialize(bc::Broadcast.Broadcasted) is defined as Base.copy(instantiate(bc)), and the implementation of Base.copy(::Broadcast.Broadcasted) gets a bit tricky because of the logic around how to instantiate the output when it can't easily be inferred from the inputs (https://github.com/JuliaLang/julia/blob/v1.11.2/base/broadcast.jl#L888-L915). But maybe we can assume types are inferrable/concrete for the time being to simplify things, in which case it can just be something like:

Base.copy(bc::Broadcast.Broadcasted)
  T = combine_eltypes(bc.f, bc.args)
  dest = similar(bc, T)
  copyto!(dest, bc)
  return dest
end

Broadcast.materialize!(dest, bc::Broadcast.Broadcasted) basically just ends up as Base.copyto!(dest, bc::Broadcast.Broadcasted).

@mtfishman mtfishman added the enhancement New feature or request label Dec 10, 2024
@mtfishman mtfishman transferred this issue from ITensor/Derive.jl Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant