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

Customizable lazy fused broadcasting in pure Julia #26891

Merged
merged 7 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,6 @@ This section lists changes that do not have deprecation warnings.
Its return value has been removed. Use the `process_running` function
to determine if a process has already exited.

* Broadcasting has been redesigned with an extensible public interface. The new API is
documented at https://docs.julialang.org/en/latest/manual/interfaces/#Interfaces-1.
`AbstractArray` types that specialized broadcasting using the old internal API will
need to switch to the new API. ([#20740])

* The logging system has been redesigned - `info` and `warn` are deprecated
and replaced with the logging macros `@info`, `@warn`, `@debug` and
`@error`. The `logging` function is also deprecated and replaced with
Expand All @@ -418,6 +413,14 @@ This section lists changes that do not have deprecation warnings.
* `findn(x::AbstractArray)` has been deprecated in favor of `findall(!iszero, x)`, which
now returns cartesian indices for multidimensional arrays (see below, [#25532]).

* Broadcasting operations are no longer fused into a single operation by Julia's parser.
Instead, a lazy `Broadcasted` wrapper is created, and the parser will call
`copy(bc::Broadcasted)` or `copyto!(dest, bc::Broadcasted)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the lowering actually calls Broadcast.materialize or Broadcast.materialize!?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, that's true. I still want to highlight copy and copyto! as those are the methods to specialize. Let's just remove that "parser will call" to make it technically correct.

Copy link
Member

@stevengj stevengj Apr 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the parser doesn't lower to these copy calls directly?

Copy link
Member Author

@mbauman mbauman Apr 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. There are several reasons here, and they're different for in-place (broadcast! / .=) vs. out-of-place broadcasting.

For in-place broadcasts, we use materialize! for two purposes: (1) the outermost broadcasted might not return a Broadcasted object, but we need to support broadcasting into the destination (e.g., copyto!(rand(3,3), 1:3) doesn't broadcast) and we want to use the same code-paths here. It solves both problems at once by wrapping the non-Broadcasted object x in a Broadcasted(identity, (x,)). (2) The Broadcasted argument wants to know about its axes, but those are dependent upon the axes of the destination. It explicitly sets the axes of the Broadcasted object to those of the destination, and then those are checked to ensure they are a compatible shape with instantiate.

For out-of-place broadcasts, materialize also serves two purposes: (1) for Broadcasted arguments, it computes and caches the axes of the outermost Broadcasted, recursively checking the axes of its arguments by instantiate-ing them. Now, we may be able to get away with not storing the axes since axes should theoretically only be called once and it can be computed on demand, but I haven't tested it and it seems strange to throw a DimensionError from a call to axes. (2) for all other arguments it defaults to identity but can serve as a hook for custom types to do similar sorts of specializations decoupled from copy.

I'm also aiming for some semblance of symmetry between the two cases.

Now there are two is one caveat here: (1) A wart that I really dislike is broadcast itself is hard-coded to call copy(instantiate(broadcasted(…))) instead of materialize. For reasons beyond my comprehension, materialize sometimes failed to inline even though it is explicitly marked as @inline. (Fixed in 8a2d88a — this hack is no longer necessary). (2) The expression A .= x still lowers to broadcast!(identity, A, x). If otherwise unspecialized, it will fall back to the default implementation, which constructs the appropriate Broadcasted object and then goes through the above machinery as expected. We may want to change this purely to be able to simplify our description of this parser transformation.

to evaluate the wrapper. Consequently, package authors generally need to specialize
`copy` and `copyto!` methods rather than `broadcast` and `broadcast!`.
See the [Interfaces chapter](https://docs.julialang.org/en/latest/manual/interfaces/#Interfaces-1)
for more information.

* `find` has been renamed to `findall`. `findall`, `findfirst`, `findlast`, `findnext`
now take and/or return the same type of indices as `keys`/`pairs` for `AbstractArray`,
`AbstractDict`, `AbstractString`, `Tuple` and `NamedTuple` objects ([#24774], [#25545]).
Expand Down
40 changes: 0 additions & 40 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1097,19 +1097,6 @@ function (-)(B::BitArray)
end
broadcast(::typeof(sign), B::BitArray) = copy(B)

function broadcast(::typeof(~), B::BitArray)
C = similar(B)
Bc = B.chunks
if !isempty(Bc)
Cc = C.chunks
for i = 1:length(Bc)
Cc[i] = ~Bc[i]
end
Cc[end] &= _msk_end(B)
end
return C
end

"""
flipbits!(B::BitArray{N}) -> BitArray{N}

Expand Down Expand Up @@ -1166,33 +1153,6 @@ end
(/)(B::BitArray, x::Number) = (/)(Array(B), x)
(/)(x::Number, B::BitArray) = (/)(x, Array(B))

# broadcast specializations for &, |, and xor/⊻
broadcast(::typeof(&), B::BitArray, x::Bool) = x ? copy(B) : falses(size(B))
broadcast(::typeof(&), x::Bool, B::BitArray) = broadcast(&, B, x)
broadcast(::typeof(|), B::BitArray, x::Bool) = x ? trues(size(B)) : copy(B)
broadcast(::typeof(|), x::Bool, B::BitArray) = broadcast(|, B, x)
broadcast(::typeof(xor), B::BitArray, x::Bool) = x ? .~B : copy(B)
broadcast(::typeof(xor), x::Bool, B::BitArray) = broadcast(xor, B, x)
for f in (:&, :|, :xor)
@eval begin
function broadcast(::typeof($f), A::BitArray, B::BitArray)
F = BitArray(undef, promote_shape(size(A),size(B))...)
Fc = F.chunks
Ac = A.chunks
Bc = B.chunks
(isempty(Ac) || isempty(Bc)) && return F
for i = 1:length(Fc)
Fc[i] = ($f)(Ac[i], Bc[i])
end
Fc[end] &= _msk_end(F)
return F
end
broadcast(::typeof($f), A::DenseArray{Bool}, B::BitArray) = broadcast($f, BitArray(A), B)
broadcast(::typeof($f), B::BitArray, A::DenseArray{Bool}) = broadcast($f, B, BitArray(A))
end
end


## promotion to complex ##

# TODO?
Expand Down
Loading