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 DaliIterator #189

Merged
merged 5 commits into from
Oct 21, 2019
Merged

Add DaliIterator #189

merged 5 commits into from
Oct 21, 2019

Conversation

TakuyaNarihira
Copy link
Contributor

@TakuyaNarihira TakuyaNarihira commented Oct 21, 2019

This PR adds a capability of using NVIDIA's data loading library DALI in nnabla.

The following example creates a DALI iterator from nnabla's data iterator. You can also create your own DALI pipeline fed into DaliIterator.

import numpy as np
import nnabla as nn

from nnabla.ext_utils import get_extension_context
nn.set_default_context(get_extension_context('cudnn', device_id='0'))

# Create a dummy data iterator which yeilds two `numpy.ndarray`s.
from nnabla.utils.data_iterator import data_iterator_simple
batch_size = 32
data_shape = (4, 256, 256)
nn_it = data_iterator_simple(
    lambda i: (np.full(data_shape, 0, dtype=np.float32),
                np.full(data_shape, i, dtype=np.int32)),
    512, batch_size, shuffle=True)

# Create a pipeline from data iterator
from nnabla_ext.cuda.experimental import dali_iterator
p = dali_iterator.DataIteratorPipeline(nn_it, device_id=0)
dali_it = dali_iterator.DaliIterator(p)

DaliIterator can be used as following.

# Create a whatever network
v1 = nn.Variable((batch_size,) + data_shape)
v2 = nn.Variable((batch_size,) + data_shape)
y = 0
for i in range(500):
    y += v1 + v2
    y -= v1 + v2
    
# In a data processing loop
for i in range(5):
    # Set inputs from nnabla data iterator, which is slower
    # because it transfers the data to GPU synchronously.
    # v1.d, v2.d = nn_it.next()
    
    # Set inputs from DALI iterator
    # This automatically transfers array values
    # to GPU asynchronously to computation
    v1.data, v2.data = dali_it.next()
    y.forward(clear_buffer=True)

@TakuyaNarihira TakuyaNarihira added the release-note-utility Auto-release; Addition or improvement in utilities label Oct 21, 2019
@AkioHayakawa-sony AkioHayakawa-sony merged commit 314e6cf into master Oct 21, 2019
@YukioOobuchi YukioOobuchi deleted the feature/20191011-dali-iterator branch January 20, 2020 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-utility Auto-release; Addition or improvement in utilities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants