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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/batchview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,8 @@ 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)
_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}
Base.@propagate_inbounds function Base.getindex(A::BatchView, i)
obsindices = _batchindexes(A, i)
_getbatch(A, obsindices)
end

Expand All @@ -144,6 +139,15 @@ function _getbatch(A::BatchView{TElem, TData, Val{nothing}}, obsindices) where {
getobs(A.data, obsindices)
end

function getobs!(buffer, A::BatchView{TElem, TData, Val{nothing}}, i) where {TElem, TData}
obsindices = _batchindexes(A, i)
return _getbatch!(buffer, A, obsindices)
end

function _getbatch!(buffer, A::BatchView{TElem, TData, Val{nothing}}, obsindices) where {TElem, TData}
return getobs!(buffer, A.data, obsindices)
end
Comment on lines +142 to +149
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
function getobs!(buffer, A::BatchView{TElem, TData, Val{nothing}}, i) where {TElem, TData}
obsindices = _batchindexes(A, i)
return _getbatch!(buffer, A, obsindices)
end
function _getbatch!(buffer, A::BatchView{TElem, TData, Val{nothing}}, obsindices) where {TElem, TData}
return getobs!(buffer, A.data, obsindices)
end
function getobs!(buffer, A::BatchView{<:Any, <:Any, Val{nothing}}, 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

I think we can avoid overspecializing here

Comment on lines +147 to +149
Copy link
Contributor

Choose a reason for hiding this comment

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

I would've assumed this also works for collate=true|false, because unlike getobs/_getbatch the caller has already figured out the return type by passing in buffer. Not sure I understand the interface or API contract that well though, so please correct me if not.


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

Expand All @@ -159,6 +163,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 @@ -168,5 +175,3 @@ function Base.showarg(io::IO, A::BatchView, toplevel)
print(io, ')')
toplevel && print(io, " with eltype ", nameof(eltype(A))) # simplify
end

# --------------------------------------------------------------------
27 changes: 27 additions & 0 deletions test/batchview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,31 @@ 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)

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