Skip to content

Commit

Permalink
Fix Python formatting (#3962)
Browse files Browse the repository at this point in the history
Conformance to pep8 was achieved by running
yapf and adjusting by hand the rest of issues
reported by flake8.
 
Signed-off-by: Krzysztof Lecki <[email protected]>
  • Loading branch information
klecki authored Jun 13, 2022
1 parent df90d36 commit 5b000e7
Show file tree
Hide file tree
Showing 21 changed files with 704 additions and 541 deletions.
45 changes: 27 additions & 18 deletions dali/test/python/test_backend_impl_torch_dlpack.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2020-2022, NVIDIA CORPORATION & AFFILIATES. 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.
Expand All @@ -12,9 +12,7 @@
# 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
from nvidia.dali.tensors import TensorCPU, TensorGPU, TensorListCPU, TensorListGPU
import nvidia.dali.tensors as tensors
import numpy as np
import torch
Expand All @@ -36,55 +34,59 @@ def test_dlpack_tensor_gpu_direct_creation():
arr = torch.rand(size=[3, 5, 6], device="cuda")
tensor = TensorGPU(to_dlpack(arr))
dali_torch_tensor = convert_to_torch(tensor, device=arr.device, dtype=arr.dtype)
assert(torch.all(arr.eq(dali_torch_tensor)))
assert torch.all(arr.eq(dali_torch_tensor))


def test_dlpack_tensor_gpu_to_cpu():
arr = torch.rand(size=[3, 5, 6], device="cuda")
tensor = TensorGPU(to_dlpack(arr))
dali_torch_tensor = convert_to_torch(tensor, device=arr.device, dtype=arr.dtype)
assert(torch.all(arr.cpu().eq(dali_torch_tensor.cpu())))
assert torch.all(arr.cpu().eq(dali_torch_tensor.cpu()))


def test_dlpack_tensor_list_gpu_direct_creation():
arr = torch.rand(size=[3, 5, 6], device="cuda")
tensor_list = TensorListGPU(to_dlpack(arr), "NHWC")
dali_torch_tensor = convert_to_torch(tensor_list, device=arr.device, dtype=arr.dtype)
assert(torch.all(arr.eq(dali_torch_tensor)))
assert torch.all(arr.eq(dali_torch_tensor))


def test_dlpack_tensor_list_gpu_to_cpu():
arr = torch.rand(size=[3, 5, 6], device="cuda")
tensor_list = TensorListGPU(to_dlpack(arr), "NHWC")
dali_torch_tensor = convert_to_torch(tensor_list, device=arr.device, dtype=arr.dtype)
assert(torch.all(arr.cpu().eq(dali_torch_tensor.cpu())))
assert torch.all(arr.cpu().eq(dali_torch_tensor.cpu()))


def check_dlpack_types_gpu(t):
arr = torch.tensor([[-0.39, 1.5], [-1.5, 0.33]], device="cuda", dtype=t)
tensor = TensorGPU(to_dlpack(arr), "NHWC")
dali_torch_tensor = convert_to_torch(tensor, device=arr.device, dtype=arr.dtype, size=tensor.shape())
assert(torch.all(arr.eq(dali_torch_tensor)))
dali_torch_tensor = convert_to_torch(tensor, device=arr.device, dtype=arr.dtype,
size=tensor.shape())
assert torch.all(arr.eq(dali_torch_tensor))


def test_dlpack_interface_types():
for t in [torch.bool, torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8,
torch.float64, torch.float32, torch.float16]:
for t in [
torch.bool, torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8,
torch.float64, torch.float32, torch.float16
]:
yield check_dlpack_types_gpu, t


def test_dlpack_tensor_cpu_direct_creation():
arr = torch.rand(size=[3, 5, 6], device="cpu")
tensor = TensorCPU(to_dlpack(arr))
dali_torch_tensor = convert_to_torch(tensor, device=arr.device, dtype=arr.dtype)
assert(torch.all(arr.eq(dali_torch_tensor)))
assert torch.all(arr.eq(dali_torch_tensor))


def test_dlpack_tensor_list_cpu_direct_creation():
arr = torch.rand(size=[3, 5, 6], device="cpu")
tensor_list = TensorListCPU(to_dlpack(arr), "NHWC")
dali_torch_tensor = convert_to_torch(tensor_list, device=arr.device, dtype=arr.dtype)
assert(torch.all(arr.eq(dali_torch_tensor)))
assert torch.all(arr.eq(dali_torch_tensor))


# Check if dlpack tensors behave correctly when created from temporary objects

Expand All @@ -94,6 +96,7 @@ def create_tmp(idx):
a = np.full((4, 4), idx)
dlt = to_dlpack(torch.from_numpy(a))
return tensors.TensorCPU(dlt, "")

out = [create_tmp(i) for i in range(4)]
for i, t in enumerate(out):
np.testing.assert_array_equal(np.array(t), np.full((4, 4), i))
Expand All @@ -104,6 +107,7 @@ def create_tmp(idx):
a = np.full((4, 4), idx)
dlt = to_dlpack(torch.from_numpy(a))
return tensors.TensorListCPU(dlt, "")

out = [create_tmp(i) for i in range(4)]
for i, tl in enumerate(out):
np.testing.assert_array_equal(tl.as_array(), np.full((4, 4), i))
Expand All @@ -114,6 +118,7 @@ def create_tmp(idx):
a = np.full((4, 4), idx)
dlt = to_dlpack(torch.from_numpy(a).cuda())
return tensors.TensorGPU(dlt, "")

out = [create_tmp(i) for i in range(4)]
for i, t in enumerate(out):
np.testing.assert_array_equal(np.array(t.as_cpu()), np.full((4, 4), i))
Expand All @@ -124,6 +129,7 @@ def create_tmp(idx):
a = np.full((4, 4), idx)
dlt = to_dlpack(torch.from_numpy(a).cuda())
return tensors.TensorListGPU(dlt, "")

out = [create_tmp(i) for i in range(4)]
for i, tl in enumerate(out):
np.testing.assert_array_equal(tl.as_cpu().as_array(), np.full((4, 4), i))
Expand All @@ -132,13 +138,16 @@ def create_tmp(idx):
def check_dlpack_types_cpu(t):
arr = torch.tensor([[-0.39, 1.5], [-1.5, 0.33]], device="cpu", dtype=t)
tensor = TensorCPU(to_dlpack(arr), "NHWC")
dali_torch_tensor = convert_to_torch(tensor, device=arr.device, dtype=arr.dtype, size=tensor.shape())
assert(torch.all(arr.eq(dali_torch_tensor)))
dali_torch_tensor = convert_to_torch(tensor, device=arr.device, dtype=arr.dtype,
size=tensor.shape())
assert torch.all(arr.eq(dali_torch_tensor))


def test_dlpack_interface_types_cpu():
for t in [torch.bool, torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8,
torch.float64, torch.float32]:
for t in [
torch.bool, torch.int8, torch.int16, torch.int32, torch.int64, torch.uint8,
torch.float64, torch.float32
]:
yield check_dlpack_types_cpu, t


Expand Down
7 changes: 2 additions & 5 deletions dali/test/python/test_coco_tfrecord.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2019-2022, NVIDIA CORPORATION & AFFILIATES. 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.
Expand All @@ -14,7 +14,6 @@

import argparse
import os
from math import ceil, sqrt

import nvidia.dali.ops as ops
import nvidia.dali.types as types
Expand All @@ -37,7 +36,7 @@ def __init__(self, args):
path=os.path.join(test_dummy_data_path, 'small_coco.tfrecord'),
index_path=os.path.join(test_dummy_data_path, 'small_coco_index.idx'),
features={
'image/encoded' : tfrec.FixedLenFeature((), tfrec.string, ""),
'image/encoded': tfrec.FixedLenFeature((), tfrec.string, ""),
'image/object/class/label': tfrec.VarLenFeature([], tfrec.int64, 0),
'image/object/bbox': tfrec.VarLenFeature([4], tfrec.float32, 0.0),
},
Expand All @@ -52,7 +51,6 @@ def __init__(self, args):
criteria=0.5,
anchors=coco_anchors())


def define_graph(self):
inputs = self.input()
input_images = inputs["image/encoded"]
Expand Down Expand Up @@ -89,7 +87,6 @@ def __init__(self, args, data_path=test_data_path):
criteria=0.5,
anchors=coco_anchors())


def define_graph(self):
inputs, boxes, labels = self.input(name="Reader")
image_gpu = self.decode_gpu(inputs)
Expand Down
4 changes: 2 additions & 2 deletions dali/test/python/test_dali_tf_dataset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2020-2022, NVIDIA CORPORATION & AFFILIATES. 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.
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_python_operator_not_allowed_in_tf_dataset_error():
dtypes = (tf.float32)

with tf.device('/cpu:0'):
daliset = dali_tf.DALIDataset(
_ = dali_tf.DALIDataset(
pipeline=pipeline,
batch_size=1,
output_shapes=shapes,
Expand Down
16 changes: 5 additions & 11 deletions dali/test/python/test_dali_tf_dataset_mnist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2019-2022, NVIDIA CORPORATION & AFFILIATES. 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.
Expand All @@ -12,16 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import nvidia.dali as dali
import nvidia.dali.plugin.tf as dali_tf
import os
import numpy as np
from nvidia.dali.pipeline import Pipeline
import nvidia.dali.fn as fn
import nvidia.dali.types as types
from test_utils_tensorflow import *
from test_utils_tensorflow import num_available_gpus
from shutil import rmtree as remove_directory
from nose.tools import with_setup
import tensorflow as tf
import tensorflow.compat.v1 as tf_v1

Expand All @@ -44,15 +41,12 @@ def mnist_pipeline(
with pipeline:
jpegs, labels = fn.readers.caffe2(
path=path, random_shuffle=True, shard_id=shard_id, num_shards=num_shards)
images = fn.decoders.image(jpegs, device='mixed' if device == 'gpu' else 'cpu', output_type=types.GRAY)
images = fn.decoders.image(
jpegs, device='mixed' if device == 'gpu' else 'cpu', output_type=types.GRAY)
if device == 'gpu':
labels = labels.gpu()
images = fn.crop_mirror_normalize(
images,
dtype=types.FLOAT,
mean=[0.],
std=[255.],
output_layout="CHW")
images, dtype=types.FLOAT, mean=[0.], std=[255.], output_layout="CHW")

pipeline.set_outputs(images, labels)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2020-2022, NVIDIA CORPORATION & AFFILIATES. 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.
Expand Down Expand Up @@ -40,8 +40,8 @@ def simple_pipeline():

def _test_no_segfault(method, workers_num):
"""
This may cause segmentation fault on Python teardown if shared memory wrappers managed by the py_pool
are garbage collected before pipeline's backend
This may cause segmentation fault on Python teardown if shared memory wrappers managed by the
py_pool are garbage collected before pipeline's backend
"""
pipe = simple_pipeline(
py_start_method=method, py_num_workers=workers_num,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def _test_large_sample(start_method):
def create_pipeline():
large = fn.external_source(
large_sample_cb, batch=False, parallel=True, prefetch_queue_depth=1)
# iteration over array in Python is too slow, so reduce the number of elements to iterate over
# iteration over array in Python is too slow, so reduce the number of elements
# to iterate over
reduced = fn.reductions.sum(large, axes=(1, 2))
return reduced

Expand All @@ -49,9 +50,9 @@ def create_pipeline():
a = np.array(out[idx_in_batch])
assert a.shape == (512,), "Expected shape (512,) but got {}".format(a.shape)
for val in a.flat:
assert val == expected_val, \
"Unexpected value in batch: got {}, expected {}, for batch {}, sample {}".format(
val, expected_val, batch_idx, idx_in_batch)
assert val == expected_val, (
f"Unexpected value in batch: got {val}, expected {expected_val}, "
f"for batch {batch_idx}, sample {idx_in_batch}")


def test_large_sample():
Expand Down
15 changes: 6 additions & 9 deletions dali/test/python/test_nvjpeg_cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2019-2022, NVIDIA CORPORATION & AFFILIATES. 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.
Expand All @@ -15,11 +15,7 @@
from nvidia.dali.pipeline import Pipeline
import nvidia.dali.ops as ops
import nvidia.dali.types as types
import nvidia.dali.tfrecord as tfrec
from timeit import default_timer as timer
import numpy as np
import os
from numpy.testing import assert_array_equal, assert_allclose
from numpy.testing import assert_array_equal
from test_utils import get_dali_extra_path

seed = 1549361629
Expand All @@ -32,8 +28,7 @@
def compare(tl1, tl2):
tl1_cpu = tl1.as_cpu()
tl2_cpu = tl2.as_cpu()
#tl2 = tl2.as_cpu()
assert(len(tl1_cpu) == len(tl2_cpu))
assert len(tl1_cpu) == len(tl2_cpu)
for i in range(0, len(tl1_cpu)):
assert_array_equal(tl1_cpu.at(i), tl2_cpu.at(i), "cached and non-cached images differ")

Expand All @@ -45,7 +40,9 @@ def __init__(self, batch_size, num_threads, device_id, cache_size):
policy = None
if cache_size > 0:
policy = "threshold"
self.decode = ops.decoders.Image(device='mixed', output_type=types.RGB, cache_debug=False, cache_size=cache_size, cache_type=policy, cache_batch_copy=True)
self.decode = ops.decoders.Image(device='mixed', output_type=types.RGB, cache_debug=False,
cache_size=cache_size, cache_type=policy,
cache_batch_copy=True)

def define_graph(self):
jpegs, labels = self.input(name="Reader")
Expand Down
Loading

0 comments on commit 5b000e7

Please sign in to comment.