Skip to content

Commit

Permalink
Add tests for non-contiguous tensors for Python backend (triton-infer…
Browse files Browse the repository at this point in the history
…ence-server#3017)

* Add tests for non-contiguous tensors for Python backend

* Review edits
  • Loading branch information
Tabrizian authored Jun 16, 2021
1 parent 5507772 commit e6fa97b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 5 deletions.
1 change: 0 additions & 1 deletion qa/L0_backend_python/env/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

CLIENT_PY=./lifecycle_test.py
CLIENT_LOG="./client.log"
source ../common.sh
source ../../common/util.sh
Expand Down
28 changes: 26 additions & 2 deletions qa/L0_backend_python/python_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python

# Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved.
# Copyright 2019-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand All @@ -27,6 +27,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import sys

sys.path.append("../common")

import unittest
Expand All @@ -40,7 +41,6 @@


class PythonTest(tu.TestResultCollector):

def _infer_help(self, model_name, shape, data_type):
with httpclient.InferenceServerClient("localhost:8000") as client:
input_data_0 = np.array(np.random.randn(*shape), dtype=data_type)
Expand Down Expand Up @@ -257,6 +257,30 @@ def test_string(self):
self.assertTrue(output0 is not None)
self.assertTrue(output0[0] == input_data.astype(np.bytes_))

def test_non_contiguous(self):
model_name = 'non_contiguous'
shape = [2, 64, 84, 32, 55]
new_shape = [64, 2, 32, 55, 84]
shape_reorder = [1, 0, 4, 2, 3]
with httpclient.InferenceServerClient("localhost:8000") as client:
input_numpy = np.random.rand(*shape)
input_numpy = input_numpy.astype(np.float32)
inputs = [
httpclient.InferInput("INPUT0", shape,
np_to_triton_dtype(input_numpy.dtype))
]
inputs[0].set_data_from_numpy(input_numpy)
result = client.infer(model_name, inputs)
output0 = input_numpy.reshape(new_shape)

# Transpose the tensor to create a non-contiguous tensor.
output1 = input_numpy.T
output2 = np.transpose(input_numpy, shape_reorder)

self.assertTrue(np.all(output0 == result.as_numpy('OUTPUT0')))
self.assertTrue(np.all(output1 == result.as_numpy('OUTPUT1')))
self.assertTrue(np.all(output2 == result.as_numpy('OUTPUT2')))


if __name__ == '__main__':
unittest.main()
8 changes: 6 additions & 2 deletions qa/L0_backend_python/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Copyright (c) 2020-2021, NVIDIA CORPORATION. All rights reserved.
# Copyright 2020-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -27,7 +27,7 @@

CLIENT_PY=./python_test.py
CLIENT_LOG="./client.log"
EXPECTED_NUM_TESTS="7"
EXPECTED_NUM_TESTS="8"
SERVER=/opt/tritonserver/bin/tritonserver
BASE_SERVER_ARGS="--model-repository=`pwd`/models --log-verbose=1"
PYTHON_BACKEND_BRANCH=$PYTHON_BACKEND_REPO_TAG
Expand Down Expand Up @@ -78,6 +78,10 @@ mkdir -p models/init_args/1/
cp ../python_models/init_args/model.py ./models/init_args/1/
cp ../python_models/init_args/config.pbtxt ./models/init_args/

mkdir -p models/non_contiguous/1/
cp ../python_models/non_contiguous/model.py ./models/non_contiguous/1/
cp ../python_models/non_contiguous/config.pbtxt ./models/non_contiguous/config.pbtxt

# Unicode Characters
mkdir -p models/string/1/
cp ../python_models/string/model.py ./models/string/1/
Expand Down
55 changes: 55 additions & 0 deletions qa/python_models/non_contiguous/config.pbtxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

name: "non_contiguous"
backend: "python"

input [
{
name: "INPUT0"
data_type: TYPE_FP32
dims: [ -1, -1, -1, -1, -1 ]
}
]
output [
{
name: "OUTPUT0"
data_type: TYPE_FP32
dims: [ -1, -1, -1, -1, -1 ]
},
{
name: "OUTPUT1"
data_type: TYPE_FP32
dims: [ -1, -1, -1, -1, -1 ]
},
{
name: "OUTPUT2"
data_type: TYPE_FP32
dims: [ -1, -1, -1, -1, -1 ]
}
]

instance_group [{ kind: KIND_CPU }]
51 changes: 51 additions & 0 deletions qa/python_models/non_contiguous/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import numpy as np
import sys

sys.path.append('../../')
import triton_python_backend_utils as pb_utils


class TritonPythonModel:
def execute(self, requests):
responses = []
new_shape = [64, 2, 32, 55, 84]
shape_reorder = [1, 0, 4, 2, 3]
for request in requests:
input_tensor = pb_utils.get_input_tensor_by_name(request, "INPUT0")
input_numpy = input_tensor.as_numpy()
input_shape = input_numpy.shape
output0 = pb_utils.Tensor("OUTPUT0",
input_numpy.reshape(new_shape))
# Transpose the tensor to create a non-contiguous tensor.
output1 = pb_utils.Tensor("OUTPUT1", input_numpy.T)
output2 = pb_utils.Tensor("OUTPUT2",
np.transpose(input_numpy, shape_reorder))
responses.append(
pb_utils.InferenceResponse([output0, output1, output2]))
return responses

0 comments on commit e6fa97b

Please sign in to comment.