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

add topk #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
30 changes: 30 additions & 0 deletions src/pooling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,33 @@ for pool in [:maxpool, :meanpool]
return Ω, $pullback
end
end


function topk(x::AbstractArray{T,N}, k; rev=false, dims=nothing) where {T,N}
Copy link
Member

Choose a reason for hiding this comment

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

It seems a little odd that "top k" gives the smallest values:

julia> r = [3,11,5,13,7];

julia> topk(r, 2)
([3, 5], CartesianIndex{1}[CartesianIndex(1,), CartesianIndex(3,)])

julia> sort(r)
5-element Vector{Int64}:
  3
  5
  7
 11
 13

I understand it's following what sort does, but perhaps it needs a better name, or a different default? Is the typical use to select the largest elements?

Copy link
Member

@ToucheSir ToucheSir Oct 10, 2021

Choose a reason for hiding this comment

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

PyTorch has a largest param to control this: https://pytorch.org/docs/stable/generated/torch.topk.html#torch.topk. As you noted, the default is to return the largest k values. I think passing rev=!rev to partialsortperm would do the trick.

Copy link
Member

@mcabbott mcabbott Oct 10, 2021

Choose a reason for hiding this comment

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

I see, thanks. So they default to "largest=True" i.e. "rev=false" here.

They allow "sorted=False", but with partialsortperm, they will be sorted by default. I presume there are no downsides to getting a sorted result, it's just that they (perhaps) do something cheaper when this isn't required?

One nice thing about following sort is that you can pass all of its keywords along, by=..., lt=..., in case people want these... and you can just point at Base's documentation. It wouldn't be crazy to do that and just note that rev=true is the default here.

if dims === nothing
y = vec(x)
perm = partialsortperm(y, 1:k; rev)
return y[perm], linear_to_cartesian(x, perm)
else
@assert dims isa Int
sz1 = size(x)[1:dims-1]
sz2 = size(x)[dims+1:end]
slice1 = CartesianIndices(sz1)
slice2 = CartesianIndices(sz2)
perm = similar(x, Int, (sz1..., k, sz2...))
y = similar(x, (sz1..., k, sz2...))
for I1 in slice1
for I2 in slice2
xI = x[I1,:,I2]
permI = partialsortperm(x[I1,:,I2], 1:k; rev)
perm[I1,:,I2] .= permI
y[I1,:,I2] .= xI[permI]
end
end
return y, perm
end
end

function linear_to_cartesian(x, i)
CartesianIndices(x)[i]
end