Skip to content

Commit

Permalink
Properly determine the input device, when the operator backend is `mi…
Browse files Browse the repository at this point in the history
…xed` (#174)

`DaliPipeline::GetInputDevice` function checks, what is the backend of given operator and returns information, what kind of device (CPU or GPU) shall the input data belong to. It worked well for input device CPU or GPU. It broke when introducing `VideoInput` operator, since the device of this operator can be `mixed`. This PR solves the problem and adds proper test for such situation.

* Fix input device check
* Add tests

---------

Signed-off-by: Rafal <[email protected]>
  • Loading branch information
banasraf authored Feb 28, 2023
1 parent 52a0fa7 commit 3fdeffc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
27 changes: 22 additions & 5 deletions qa/L0_video_input_decoupled/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
from tritonclient.utils import *
import tritonclient.grpc as t_client

import nvidia.dali.experimental.eager as eager
import nvidia.dali.fn as fn
from nvidia.dali import pipeline_def

class UserData:

Expand All @@ -49,6 +50,7 @@ def callback(user_data, result, error):
def get_dali_extra_path():
return environ['DALI_EXTRA_PATH']


def input_gen():
filenames = glob(f'{get_dali_extra_path()}/db/video/[cv]fr/*.mp4')
filenames = filter(lambda filename: 'mpeg4' not in filename, filenames)
Expand All @@ -57,22 +59,31 @@ def input_gen():
yield np.fromfile(filename, dtype=np.uint8)



FRAMES_PER_SEQUENCE = 5
BATCH_SIZE = 3
FRAMES_PER_BATCH = FRAMES_PER_SEQUENCE * BATCH_SIZE
model_name = "model.dali"

user_data = UserData()

@pipeline_def(batch_size=1, num_threads=1, device_id=0, prefetch_queue_depth=1)
def ref_pipeline(device):
inp = fn.external_source(name='data')
decoded = fn.experimental.decoders.video(inp, device='mixed' if device == 'gpu' else 'cpu')
return fn.pad(decoded, axes=0, align=FRAMES_PER_SEQUENCE)


def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url', type=str, required=False, default='localhost:8001',
help='Inference server GRPC URL. Default is localhost:8001.')
parser.add_argument('-d', '--device', type=str, required=False, default='cpu', help='cpu or gpu')
parser.add_argument('-n', '--n_iters', type=int, required=False, default=1, help='Number of iterations')
return parser.parse_args()

if __name__ == '__main__':
args = parse_args()
model_name = 'model.dali' if args.device == 'cpu' else 'model_gpu.dali'
with t_client.InferenceServerClient(url=args.url) as triton_client:
triton_client.start_stream(callback=partial(callback, user_data))

Expand All @@ -88,9 +99,15 @@ def parse_args():
request_id=request_id,
outputs=[outp])

ref_pipe = ref_pipeline(device=args.device)
ref_pipe.build()
ref_pipe.feed_input('data', [input_data])

expected_result, = ref_pipe.run()
if args.device == 'gpu':
expected_result = expected_result.as_cpu()
expected_result = expected_result.at(0)

expected_result = eager.experimental.decoders.video([input_data])
expected_result = eager.pad(expected_result, axes=0, align=FRAMES_PER_SEQUENCE).at(0)
n_frames = expected_result.shape[0]
recv_count = 0
expected_count = (n_frames + FRAMES_PER_BATCH - 1) // FRAMES_PER_BATCH
Expand All @@ -112,6 +129,6 @@ def parse_args():
expected_batch = expected_result[i * BATCH_SIZE : min((i+1) * BATCH_SIZE, len(expected_result))]
expected_batch = np.asarray(expected_batch)
result_data = result.as_numpy('OUTPUT')
assert np.array_equal(expected_batch, result_data)
assert np.allclose(expected_batch, result_data)

print(f'ITER {req_id}: OK')
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# The MIT License (MIT)
#
# Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import nvidia.dali as dali
import nvidia.dali.fn as fn
from nvidia.dali.plugin.triton import autoserialize


@autoserialize
@dali.pipeline_def(batch_size=3, num_threads=3, device_id=0, output_ndim=4, output_dtype=dali.types.UINT8)
def pipeline():
return fn.experimental.inputs.video(sequence_length=5, name='INPUT', device='mixed', last_sequence_policy='pad')
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# The MIT License (MIT)
#
# Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


max_batch_size: 0

model_transaction_policy {
decoupled: True
}

output [
{
name: "OUTPUT"
}
]
7 changes: 5 additions & 2 deletions qa/L0_video_input_decoupled/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

echo "RUN SEQUENTIAL CLIENT"
python client.py -u $GRPC_ADDR -n 16
echo "RUN CPU CLIENT"
python client.py -u $GRPC_ADDR -n 16 -d cpu
echo "PASS"
echo "RUN GPU CLIENT"
python client.py -u $GRPC_ADDR -n 16 -d gpu
echo "PASS"
3 changes: 1 addition & 2 deletions src/dali_executor/dali_pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ dali_data_type_t DaliPipeline::GetInputType(const std::string &name) {

device_type_t DaliPipeline::GetInputDevice(const std::string& name) {
auto backend = daliGetOperatorBackend(&handle_, name.c_str());
assert(backend != DALI_BACKEND_MIXED);
return (backend == DALI_BACKEND_CPU) ? device_type_t::CPU : device_type_t::GPU;
return (backend == DALI_BACKEND_GPU) ? device_type_t::GPU : device_type_t::CPU;
}


Expand Down

0 comments on commit 3fdeffc

Please sign in to comment.