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

use buffer in BatchView #157

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
44 changes: 32 additions & 12 deletions src/batchview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,45 @@ Base.@propagate_inbounds function getobs(A::BatchView)
return _getbatch(A, 1:numobs(A.data))
end

Base.@propagate_inbounds function Base.getindex(A::BatchView, i::Int)
obsindices = _batchrange(A, i)
Base.@propagate_inbounds function Base.getindex(A::BatchView, i)
obsindices = _batchindexes(A, i)
_getbatch(A, obsindices)
end

Base.@propagate_inbounds function Base.getindex(A::BatchView, is::AbstractVector)
obsindices = union((_batchrange(A, i) for i in is)...)::Vector{Int}
_getbatch(A, obsindices)
end

function _getbatch(A::BatchView{TElem, TData, Val{true}}, obsindices) where {TElem, TData}
function _getbatch(A::BatchView{<:Any, <:Any, Val{true}}, obsindices)
batch([getobs(A.data, i) for i in obsindices])
end
function _getbatch(A::BatchView{TElem, TData, Val{false}}, obsindices) where {TElem, TData}
function _getbatch(A::BatchView{<:Any, <:Any, Val{false}}, obsindices)
return [getobs(A.data, i) for i in obsindices]
end
function _getbatch(A::BatchView{TElem, TData, Val{nothing}}, obsindices) where {TElem, TData}
function _getbatch(A::BatchView{<:Any, <:Any, Val{nothing}}, obsindices)
getobs(A.data, obsindices)
end

function getobs!(buffer, A::BatchView, i)
obsindices = _batchindexes(A, i)
return _getbatch!(buffer, A, obsindices)
end

function _getbatch!(buffer, A::BatchView{<:Any, <:Any, Val{nothing}}, obsindices)
return getobs!(buffer, A.data, obsindices)
end

# This collate=true specialization doesn't seem to be particularly useful, use collate=nothing instead.
function _getbatch!(buffer, A::BatchView{<:Any, <:Any, Val{true}}, obsindices)
for (i, idx) in enumerate(obsindices)
getobs!(buffer[i], A.data, idx)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why this isn't

Suggested change
getobs!(buffer[i], A.data, idx)
getobs!(buffer, A.data, idx)

Can you explain how it works to me briefly?

Copy link
Member Author

Choose a reason for hiding this comment

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

When collate == false, the minibatch returned is

[getobs(A.data, idx) for idx in obsindexes]

so in this context the buffer has to be used for the single observation. We have to use a different buffer for all observations in the mini-batch though, therefore the mini-batch buffer has to be a vector of single-observation-buffers of the length of the batchsize:

[getobs!(buffer[i], A.data, idx) for (i, idx) in for enumerate(obsindexes)]

end
return batch(buffer)
end

function _getbatch!(buffer, A::BatchView{<:Any, <:Any, Val{false}}, obsindices)
for (i, idx) in enumerate(obsindices)
getobs!(buffer[i], A.data, idx)
end
return buffer
end

Base.parent(A::BatchView) = A.data
Base.eltype(::BatchView{Tel}) where Tel = Tel

Expand All @@ -169,6 +188,9 @@ Base.iterate(A::BatchView, state = 1) =
return startidx:endidx
end

@inline _batchindexes(A::BatchView, i::Integer) = _batchrange(A, i)
@inline _batchindexes(A::BatchView, is::AbstractVector{<:Integer}) = union((_batchrange(A, i) for i in is)...)::Vector{Int}

function Base.showarg(io::IO, A::BatchView, toplevel)
print(io, "BatchView(")
Base.showarg(io, parent(A), false)
Expand All @@ -178,5 +200,3 @@ function Base.showarg(io::IO, A::BatchView, toplevel)
print(io, ')')
toplevel && print(io, " with eltype ", nameof(eltype(A))) # simplify
end

# --------------------------------------------------------------------
32 changes: 32 additions & 0 deletions test/batchview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,36 @@ using MLUtils: obsview
@test bv[2] == 6:10
@test_throws BoundsError bv[3]
end


@testset "getobs!" begin
buf1 = rand(4, 3)
bv = BatchView(X, batchsize=3)
@test @inferred(getobs!(buf1, bv, 2)) === buf1
@test buf1 == getobs(bv, 2)

buf12 = [rand(4) for _=1:3]
bv12 = BatchView(X, batchsize=3, collate=false)
@test @inferred(getobs!(buf12, bv12, 2)) === buf12
@test buf12 == getobs(bv12, 2)

buf2 = rand(4, 6)
@test @inferred(getobs!(buf2, bv, [1,3])) === buf2
@test buf2 == getobs(bv, [1,3])

@testset "custom type" begin # issue #156
struct DummyData{X}
x::X
end
MLUtils.numobs(data::DummyData) = numobs(data.x)
MLUtils.getobs(data::DummyData, idx) = getobs(data.x, idx)
MLUtils.getobs!(buffer, data::DummyData, idx) = getobs!(buffer, data.x, idx)

data = DummyData(X)
buf = rand(4, 3)
bv = BatchView(data, batchsize=3)
@test @inferred(getobs!(buf, bv, 2)) === buf
@test buf == getobs(bv, 2)
end
end
end
Loading