-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
data_ptr
method to Tensor and TensorList (#1773)
- adds an ability to expose a raw data pointer form Tensor and TensorList on CPU and GPU - adds tests for `data_ptr` which created numpy or cupy arrays form an exposed pointers Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
- Loading branch information
Showing
9 changed files
with
211 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from nvidia.dali.backend_impl import * | ||
from nvidia.dali.pipeline import Pipeline | ||
import nvidia.dali.ops as ops | ||
import numpy as np | ||
from nose.tools import assert_raises | ||
import cupy as cp | ||
from test_utils import py_buffer_from_address | ||
|
||
class ExternalSourcePipe(Pipeline): | ||
def __init__(self, batch_size, data): | ||
super(ExternalSourcePipe, self).__init__(batch_size, 1, 0) | ||
self.output = ops.ExternalSource(device="gpu") | ||
self.data = data | ||
|
||
def define_graph(self): | ||
self.out = self.output() | ||
return self.out | ||
|
||
def iter_setup(self): | ||
self.feed_input(self.out, self.data) | ||
|
||
def test_tensorlist_getitem_gpu(): | ||
arr = np.random.rand(3, 5, 6) | ||
pipe = ExternalSourcePipe(arr.shape[0], arr) | ||
pipe.build() | ||
tensorlist = pipe.run()[0] | ||
list_of_tensors = [x for x in tensorlist] | ||
|
||
assert(type(tensorlist[0]) != cp.ndarray) | ||
assert(type(tensorlist[0]) == TensorGPU) | ||
assert(type(tensorlist[-3]) == TensorGPU) | ||
assert(len(list_of_tensors) == len(tensorlist)) | ||
with assert_raises(IndexError): | ||
tensorlist[len(tensorlist)] | ||
with assert_raises(IndexError): | ||
tensorlist[-len(tensorlist) - 1] | ||
|
||
def test_data_ptr_tensor_gpu(): | ||
arr = np.random.rand(3, 5, 6) | ||
pipe = ExternalSourcePipe(arr.shape[0], arr) | ||
pipe.build() | ||
tensor = pipe.run()[0][0] | ||
from_tensor = py_buffer_from_address(tensor.data_ptr(), tensor.shape(), tensor.dtype(), gpu=True) | ||
# from_tensor is cupy array, convert arr to cupy as well | ||
assert(cp.allclose(cp.array(arr[0]), from_tensor)) | ||
|
||
def test_data_ptr_tensor_list_gpu(): | ||
arr = np.random.rand(3, 5, 6) | ||
pipe = ExternalSourcePipe(arr.shape[0], arr) | ||
pipe.build() | ||
tensor_list = pipe.run()[0] | ||
tensor = tensor_list.as_tensor() | ||
from_tensor = py_buffer_from_address(tensor_list.data_ptr(), tensor.shape(), tensor.dtype(), gpu=True) | ||
# from_tensor is cupy array, convert arr to cupy as well | ||
assert(cp.allclose(cp.array(arr), from_tensor)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,3 @@ | ||
#!/bin/bash -e | ||
# used pip packages | ||
pip_packages="nose numpy opencv-python pillow librosa" | ||
target_dir=./dali/test/python | ||
|
||
# test_body definition is in separate file so it can be used without setup | ||
source test_body.sh | ||
|
||
pushd ../.. | ||
source ./qa/test_template.sh | ||
popd | ||
./test_nofw.sh | ||
./test_cupy.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.