Skip to content

Commit

Permalink
docs: add docstrings for DeviceIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Aug 28, 2024
1 parent c8d31cf commit 0f8799c
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/iterator.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
# This is based on CuIterator but generalized to work with any device
"""
DeviceIterator(dev::AbstractDevice, iterator)
Create a `DeviceIterator` that iterates through the provided `iterator` via `iterate`. Upon
each iteration, the current batch is copied to the device `dev`, and the previous iteration
is marked as freeable from GPU memory (via `unsafe_free!`) (no-op for a CPU device).
The conversion follows the same semantics as `dev(<item from iterator>)`.
!!! tip "Similarity to `CUDA.CuIterator`"
The design inspiration was taken from `CUDA.CuIterator` and was generalized to work with
other backends and more complex iterators (using `Functors`).
!!! tip "`MLUtils.DataLoader`"
Calling `dev(::MLUtils.DataLoader)` will automatically convert the dataloader to use the
same semantics as `DeviceIterator`. This is generally preferred over looping over the
dataloader directly and transferring the data to the device.
## Examples
The following was run on a computer with an NVIDIA GPU.
```julia-repl
julia> using MLDataDevices, MLUtils
julia> X = rand(Float64, 3, 33);
julia> dataloader = DataLoader(X; batchsize=13, shuffle=false);
julia> for (i, x) in enumerate(dataloader)
@show i, summary(x)
end
(i, summary(x)) = (1, "3×13 Matrix{Float64}")
(i, summary(x)) = (2, "3×13 Matrix{Float64}")
(i, summary(x)) = (3, "3×7 Matrix{Float64}")
julia> for (i, x) in enumerate(CUDADevice()(dataloader))
@show i, summary(x)
end
(i, summary(x)) = (1, "3×13 CuArray{Float32, 2, CUDA.DeviceMemory}")
(i, summary(x)) = (2, "3×13 CuArray{Float32, 2, CUDA.DeviceMemory}")
(i, summary(x)) = (3, "3×7 CuArray{Float32, 2, CUDA.DeviceMemory}")
```
"""
struct DeviceIterator{D <: AbstractDevice, I}
dev::D
iterator::I
Expand Down

2 comments on commit 0f8799c

@avik-pal
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/114062

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" 0f8799cef4b86611fe8edd6d487d90d8e02ececd
git push origin v1.1.0

Please sign in to comment.