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 input type validation to feed_ndarray in MXNet and PyTorch #3308

Merged
merged 2 commits into from
Sep 6, 2021

Conversation

JanuszL
Copy link
Contributor

@JanuszL JanuszL commented Sep 2, 2021

  • adds type validation between DALI Tensor/TensorList and MXNet/PyTorch tensor
    inside feed_ndarray just in case anyone wants to use feed_ndarray directly
  • it doesn't cover PaddlePaddle as feed_ndarray accepts a raw pointer to
    PaddlePaddle tensor and there is no API to check the type
  • updates usage of raises, assert_raises in test_fw_iterators.py to use
    implementation from nose_utils

Signed-off-by: Janusz Lisiecki jlisiecki@nvidia.com

Description

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactoring (Redesign of existing code that doesn't affect functionality)
  • Other (e.g. Documentation, Tests, Configuration)

What happened in this PR

  • adds type validation between DALI Tensor/TensorList and MXNet/PyTorch tensor
    inside feed_ndarray just in case anyone wants to use feed_ndarray directly
  • it doesn't cover PaddlePaddle as feed_ndarray accepts a raw pointer to
    PaddlePaddle tensor and there is no API to check the type
  • updates usage of raises, assert_raises in test_fw_iterators.py to use
    implementation from nose_utils

Additional information

  • Affected modules and functionalities:
    • test_fw_iterators.py
    • plugin/mxnet.py
    • plugin/pytorch.py
  • Key points relevant for the review:

Checklist

Tests

  • Existing tests apply
  • New tests added
    • Python tests
    • GTests
    • Benchmark
    • Other
  • N/A

Documentation

  • Existing documentation applies
  • Documentation updated
    • Docstring
    • Doxygen
    • RST
    • Jupyter
    • Other
  • N/A

DALI team only

Requirements

  • Implements new requirements
  • Affects existing requirements
  • N/A

REQ IDs: N/A

JIRA TASK: N/A

- adds type validation between DALI Tensor/TensorList and MXNet/PyTorch tensor
  inside feed_ndarray just in case anyone wants to use feed_ndarray directly
- it doesn't cover PaddlePaddle as feed_ndarray accepts a raw pointer to
  PaddlePaddle tensor and there is no API to check the type
- updates usage of raises, assert_raises in test_fw_iterators.py to use
  implementation from nose_utils

Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
@JanuszL
Copy link
Contributor Author

JanuszL commented Sep 2, 2021

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2901568]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2901568]: BUILD PASSED

@@ -52,6 +52,15 @@ def feed_ndarray(dali_tensor, arr, cuda_stream = None):
In most cases, using the default internal user stream or stream 0
is expected.
"""
if isinstance(dali_tensor, (TensorListCPU, TensorListGPU)):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We cannot do such thing for PaddlePaddle as feed_ndarray there accepts raw pointer. We cannot accept tensor itself there as we cannot extract pointer itself to it without providing placement and type, so we would need to extend feed_ndarray signature and move allocation of data there as well (setting shape and placement does that). See the API.

pipe.build()
out = pipe.run()[0]
torch_tensor = torch.empty((1), dtype=torch.int8, device = 'cpu')
assert_raises(AssertionError, feed_ndarray, out, torch_tensor, glob="Type of DALI Tensor/TensorList doesn't match Torch tensor type:*")
Copy link
Member

Choose a reason for hiding this comment

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

Just a nitpick, error message checking looks for any occurrence of the pattern, so stars are not necessary at the beginning and end.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@JanuszL
Copy link
Contributor Author

JanuszL commented Sep 3, 2021

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2909038]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2909038]: BUILD FAILED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2909038]: BUILD PASSED

@JanuszL JanuszL marked this pull request as draft September 4, 2021 07:27
@JanuszL JanuszL marked this pull request as ready for review September 6, 2021 09:45
@@ -344,7 +345,8 @@ def check_mxnet_iterator_pass_reader_name(shards_num, pipes_number, batch_size,

if batch_size > data_set_size // shards_num and last_batch_policy == LastBatchPolicy.DROP:
assert_raises(AssertionError, MXNetIterator, pipes, [
("ids", MXNetIterator.DATA_TAG)], reader_name="Reader", last_batch_policy=last_batch_policy)
("ids", MXNetIterator.DATA_TAG)], reader_name="Reader", last_batch_policy=last_batch_policy,
glob="It seems that there is no data in the pipeline. This may happen if `last_batch_policy` is set to PARTIAL and the requested batch size is greater than the shard size.")
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 think we should put verbatim copies of the entire message here - just enough to make sure it's the error we expect - like:

Suggested change
glob="It seems that there is no data in the pipeline. This may happen if `last_batch_policy` is set to PARTIAL and the requested batch size is greater than the shard size.")
glob="It seems that there is no data in the pipeline*last_batch_policy*")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment on lines 61 to 62
assert dali_type == arr.dtype, ("Type of DALI Tensor/TensorList"
" doesn't match MXNet tensor type: {} vs {}".format(dali_type, np.dtype(arr.dtype)))
Copy link
Contributor

@mzient mzient Sep 6, 2021

Choose a reason for hiding this comment

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

Suggested change
assert dali_type == arr.dtype, ("Type of DALI Tensor/TensorList"
" doesn't match MXNet tensor type: {} vs {}".format(dali_type, np.dtype(arr.dtype)))
assert dali_type == arr.dtype, ("The element type of DALI Tensor/TensorList"
" doesn't match the element type of the target MXNet NDArray: {} vs {}".format(dali_type, np.dtype(arr.dtype)))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment on lines 60 to 61
assert to_torch_type[dali_type] == arr.dtype, ("Type of DALI Tensor/TensorList"
" doesn't match Torch tensor type: {} vs {}".format(to_torch_type[dali_type], arr.dtype))
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
assert to_torch_type[dali_type] == arr.dtype, ("Type of DALI Tensor/TensorList"
" doesn't match Torch tensor type: {} vs {}".format(to_torch_type[dali_type], arr.dtype))
assert to_torch_type[dali_type] == arr.dtype, ("The element type of DALI Tensor/TensorList"
" doesn't match the element type of the target PyTorch Tensor: {} vs {}".format(to_torch_type[dali_type], arr.dtype))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

pipe.build()
out = pipe.run()[0]
mxnet_tensor = mxnet.nd.empty([1], None, np.int8)
assert_raises(AssertionError, feed_ndarray, out, mxnet_tensor, glob="Type of DALI Tensor/TensorList doesn't match MXNet tensor type:")
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the pattern here if you update the message as suggested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted

Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
@JanuszL
Copy link
Contributor Author

JanuszL commented Sep 6, 2021

!build

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2925593]: BUILD STARTED

@dali-automaton
Copy link
Collaborator

CI MESSAGE: [2925593]: BUILD PASSED

@JanuszL JanuszL merged commit a49640d into NVIDIA:main Sep 6, 2021
@JanuszL JanuszL deleted the feed_ndarray_validation branch September 6, 2021 21:50
@JanuszL JanuszL restored the feed_ndarray_validation branch September 6, 2021 21:50
@JanuszL JanuszL deleted the feed_ndarray_validation branch September 6, 2021 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants