Skip to content

Commit

Permalink
add a test case with the model of sample_multiple_hybrid_dims and m…
Browse files Browse the repository at this point in the history
…odify `sample` model
  • Loading branch information
ancom21c committed May 23, 2024
1 parent 412e70c commit 331d146
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
2 changes: 1 addition & 1 deletion model_repository/sample/1/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def execute(self, requests):
responses = [None for _ in requests]
for idx, request in enumerate(requests):
current_add_value = int(json.loads(request.parameters()).get("add", 0))
in_tensor = [item.as_numpy() + current_add_value for item in request.inputs() if item.name() == "model_in"]
in_tensor = [item.as_numpy() + current_add_value for item in request.inputs() if "model_in" in item.name()]
out_tensor = [
pb_utils.Tensor(output_name, x.astype(output_dtype))
for x, output_name, output_dtype in zip(in_tensor, self.output_name_list, self.output_dtype_list)
Expand Down
45 changes: 45 additions & 0 deletions model_repository/sample_multiple_hybrid_dims/1/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json

import numpy as np
import triton_python_backend_utils as pb_utils


class TritonPythonModel:
def initialize(self, args):
self.model_config = model_config = json.loads(args["model_config"])
output_configs = model_config["output"]

self.output_name_list = [
output_config["name"] for output_config in output_configs
]
self.output_dtype_list = [
pb_utils.triton_string_to_numpy(output_config["data_type"])
for output_config in output_configs
]

def execute(self, requests):
responses = [None for _ in requests]
for idx, request in enumerate(requests):
current_add_value = int(json.loads(request.parameters()).get("add", 0))
in_tensor = [
item.as_numpy() + current_add_value
for item in request.inputs()
if "model_in" in item.name()
]

out_tensor = [
pb_utils.Tensor(output_name, x.astype(output_dtype))
for x, output_name, output_dtype in zip(
in_tensor, self.output_name_list, self.output_dtype_list
)
]
inference_response = pb_utils.InferenceResponse(output_tensors=out_tensor)
out_tensor.append(
pb_utils.Tensor(
"model_out2",
np.array([current_add_value], dtype=self.output_dtype_list[1]),
)
)

responses[idx] = inference_response
return responses
63 changes: 63 additions & 0 deletions model_repository/sample_multiple_hybrid_dims/config.pbtxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: "sample_multiple_hybrid_dims"
backend: "python"
max_batch_size: 2

parameters [
{
key: "add",
value: { string_value: "0" }
}
]
input [
{
name: "model_in0"
data_type: TYPE_FP32
dims: [ -1 ]
},
{
name: "model_in1"
data_type: TYPE_FP32
dims: [ -1 ]
}
]
output [
{
name: "model_out0"
data_type: TYPE_FP32
dims: [ -1 ]
},
{
name: "model_out1"
data_type: TYPE_FP32
dims: [ -1 ]
},
{
name: "model_out2"
data_type: TYPE_FP32
dims: [ 1 ]
}
]
instance_group [{ kind: KIND_CPU, count: 1 }]
model_warmup {
name: "RandomSampleInput"
batch_size: 1
inputs [{
key: "model_in0"
value: {
data_type: TYPE_FP32
dims: [ 10 ]
random_data: true
}
}, {
key: "model_in1"
value: {
data_type: TYPE_FP32
dims: [ 10 ]
zero_data: true
}
}]
}
19 changes: 19 additions & 0 deletions tests/test_model_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,22 @@ def test_reload_model_spec(config):
sample = np.random.rand(8, 100).astype(np.float32)
result = client(sample)
assert np.isclose(result, sample).all()


def test_with_multiple_hybrid_dims(config):
client = get_client(*config, model_name="sample_multiple_hybrid_dims")

samples = [np.random.rand(1, 100).astype(np.float32) for _ in range(2)]
ADD_VALUE = 1
result = client(
{
client.default_model_spec.model_input[0].name: samples[0],
client.default_model_spec.model_input[1].name: samples[1],
},
parameters={"add": f"{ADD_VALUE}"},
)

assert np.isclose(result[0], samples[0][0] + ADD_VALUE).all()
assert np.isclose(result[1], samples[1][0] + ADD_VALUE).all()

assert np.isclose(result[2], ADD_VALUE).all()

0 comments on commit 331d146

Please sign in to comment.