Browse Source

!101 Serving, ut6

From: @xu-yfei
Reviewed-by: @zhoufeng54,@zhangyinxia
Signed-off-by: @zhangyinxia
tags/v1.2.0
mindspore-ci-bot Gitee 5 years ago
parent
commit
f3e182534c
15 changed files with 1420 additions and 1288 deletions
  1. +3
    -3
      mindspore_serving/ccsrc/common/log.cc
  2. +17
    -5
      mindspore_serving/ccsrc/common/log.h
  3. +8
    -11
      mindspore_serving/ccsrc/master/restful/http_process.cc
  4. +1
    -1
      mindspore_serving/ccsrc/worker/context.cc
  5. +1
    -1
      mindspore_serving/client/python/client.py
  6. +8
    -12
      mindspore_serving/worker/_check_version.py
  7. +149
    -1
      tests/ut/python/tests/common.py
  8. +32
    -16
      tests/ut/python/tests/common_restful.py
  9. +600
    -0
      tests/ut/python/tests/test_grpc_request.py
  10. +71
    -230
      tests/ut/python/tests/test_mater_worker_client.py
  11. +347
    -298
      tests/ut/python/tests/test_restful_base64_data.py
  12. +137
    -692
      tests/ut/python/tests/test_restful_json_data.py
  13. +42
    -14
      tests/ut/python/tests/test_restful_request.py
  14. +1
    -1
      tests/ut/python/tests/test_start_servable_config.py
  15. +3
    -3
      tests/ut/python/tests/test_start_worker.py

+ 3
- 3
mindspore_serving/ccsrc/common/log.cc View File

@@ -108,7 +108,7 @@ static int GetGlogLevel(MsLogLevel level) {
}
}

void LogWriter::OutputLog(const std::ostringstream &msg) const {
void LogWriter::OutputLog(const std::string &msg_str) const {
if (log_level_ < g_ms_serving_log_level) {
return;
}
@@ -116,7 +116,7 @@ void LogWriter::OutputLog(const std::ostringstream &msg) const {
google::LogMessage("", 0, GetGlogLevel(log_level_)).stream()
<< "[" << GetLogLevel(log_level_) << "] " << submodule_name << "(" << getpid() << "," << GetProcName()
<< "):" << GetTime() << " "
<< "[" << file_ << ":" << line_ << "] " << func_ << "] " << msg.str() << std::endl;
<< "[" << file_ << ":" << line_ << "] " << func_ << "] " << msg_str << std::endl;
}

static MsLogLevel GetGlobalLogLevel() { return static_cast<MsLogLevel>(FLAGS_v); }
@@ -302,7 +302,7 @@ class LogConfigParser {
bool ParseLogLevel(const std::string &str_level, MsLogLevel *ptr_level) {
if (str_level.size() == 1) {
int ch = str_level.c_str()[0];
ch = ch - '0'; // substract ASCII code of '0', which is 48
ch = ch - '0'; // subtract ASCII code of '0', which is 48
if (ch >= LOG_DEBUG && ch <= LOG_ERROR) {
if (ptr_level != nullptr) {
*ptr_level = static_cast<MsLogLevel>(ch);


+ 17
- 5
mindspore_serving/ccsrc/common/log.h View File

@@ -115,19 +115,31 @@ class MS_API LogWriter {
std::string operator<(const LogStream &stream) const noexcept __attribute__((visibility("default"))) {
std::ostringstream msg;
msg << stream.sstream_->rdbuf();
OutputLog(msg);
return msg.str();
auto msg_str = GetOutputMsg(msg);
OutputLog(msg_str);
return msg_str;
}

void operator^(const LogStream &stream) const __attribute__((noreturn, visibility("default"))) {
std::ostringstream msg;
msg << stream.sstream_->rdbuf();
OutputLog(msg);
throw std::runtime_error(msg.str());
auto msg_str = GetOutputMsg(msg);
OutputLog(msg_str);
throw std::runtime_error(msg_str);
}

std::string GetOutputMsg(const std::ostringstream &msg) const {
std::string msg_str = msg.str();
constexpr int max_log_size = 256;
constexpr int msg_log_start_size = 128;
if (msg_str.length() > max_log_size) {
msg_str = msg_str.substr(0, msg_log_start_size) + "..." + msg_str.substr(msg_str.length() - msg_log_start_size);
}
return msg_str;
}

private:
void OutputLog(const std::ostringstream &msg) const;
void OutputLog(const std::string &msg_str) const;

const char *file_;
int line_;


+ 8
- 11
mindspore_serving/ccsrc/master/restful/http_process.cc View File

@@ -42,11 +42,12 @@ static const std::map<HTTP_DATA_TYPE, DataType> http_type2_infer_type{{HTTP_DATA
{HTTP_DATA_OBJ, DataType::kMSI_Bytes}};

static const std::map<std::string, DataType> str2_infer_type{
{"int8", DataType::kMSI_Int8}, {"int16", DataType::kMSI_Int16}, {"int32", DataType::kMSI_Int32},
{"int64", DataType::kMSI_Int64}, {"uint8", DataType::kMSI_Uint8}, {"uint16", DataType::kMSI_Uint16},
{"uint32", DataType::kMSI_Uint32}, {"uint64", DataType::kMSI_Uint64}, {"fp16", DataType::kMSI_Float16},
{"fp32", DataType::kMSI_Float32}, {"fp64", DataType::kMSI_Float64}, {"bool", DataType::kMSI_Bool},
{"str", DataType::kMSI_String}, {"bytes", DataType::kMSI_Bytes}};
{"int8", DataType::kMSI_Int8}, {"int16", DataType::kMSI_Int16}, {"int32", DataType::kMSI_Int32},
{"int64", DataType::kMSI_Int64}, {"uint8", DataType::kMSI_Uint8}, {"uint16", DataType::kMSI_Uint16},
{"uint32", DataType::kMSI_Uint32}, {"uint64", DataType::kMSI_Uint64}, {"fp16", DataType::kMSI_Float16},
{"fp32", DataType::kMSI_Float32}, {"fp64", DataType::kMSI_Float64}, {"float16", DataType::kMSI_Float16},
{"float32", DataType::kMSI_Float32}, {"float64", DataType::kMSI_Float64}, {"bool", DataType::kMSI_Bool},
{"str", DataType::kMSI_String}, {"bytes", DataType::kMSI_Bytes}};

template <typename T>
bool RestfulService::IsString() {
@@ -98,12 +99,9 @@ std::string RestfulService::GetReqTypeStr(RequestType req_type) {
Status RestfulService::CheckObjType(const string &type) {
Status status(SUCCESS);
auto it = str2_infer_type.find(type);

if (it == str2_infer_type.end()) {
return INFER_STATUS_LOG_ERROR(INVALID_INPUTS) << "json object, specified type:"
<< "'" << type << "' is illegal";
return INFER_STATUS_LOG_ERROR(INVALID_INPUTS) << "json object, specified type:'" << type << "' is illegal";
}

return status;
}

@@ -133,7 +131,6 @@ std::string RestfulService::GetStringByDataType(DataType type) {
return item.first;
}
}

return "";
}

@@ -896,7 +893,7 @@ Status RestfulService::ParseScalar(const ProtoTensor &pb_tensor, size_t index, j
status = ParseScalarData<uint64_t>(pb_tensor, false, index, js);
break;
case kMSI_Float16:
status = INFER_STATUS_LOG_ERROR(INVALID_INPUTS) << "fp16 reply is not supported";
status = INFER_STATUS_LOG_ERROR(INVALID_INPUTS) << "float16 reply is not supported";
break;
case kMSI_Float32:
status = ParseScalarData<float>(pb_tensor, false, index, js);


+ 1
- 1
mindspore_serving/ccsrc/worker/context.cc View File

@@ -52,7 +52,7 @@ Status ServableContext::SetDeviceTypeStr(const std::string &device_type) {
type = kDeviceTypeNotSpecified;
} else {
return INFER_STATUS_LOG_ERROR(FAILED)
<< "Unsupport device type '" << device_type
<< "Unsupported device type '" << device_type
<< "', only support 'Ascend', 'Davinci'(same with 'Ascend') and None, case ignored";
}
SetDeviceType(type);


+ 1
- 1
mindspore_serving/client/python/client.py View File

@@ -92,7 +92,7 @@ def _create_numpy_from_tensor(tensor):
ms_service_pb2.MS_BOOL: np.bool,
ms_service_pb2.MS_INT8: np.int8,
ms_service_pb2.MS_UINT8: np.uint8,
ms_service_pb2.MS_INT16: ms_service_pb2.MS_INT16,
ms_service_pb2.MS_INT16: np.int16,
ms_service_pb2.MS_UINT16: np.uint16,
ms_service_pb2.MS_INT32: np.int32,
ms_service_pb2.MS_UINT32: np.uint32,


+ 8
- 12
mindspore_serving/worker/_check_version.py View File

@@ -104,32 +104,28 @@ class AscendEnvChecker:
else:
os.environ['LD_LIBRARY_PATH'] = self.tbe_path
else:
raise EnvironmentError(
f"No such directory: {self.tbe_path}, Please check if Ascend 910 AI software package is "
"installed correctly.")
logger.warning(f"No such directory: {self.tbe_path}, Please check if Ascend 910 AI software package is "
f"installed correctly.")

if Path(self.op_impl_path).is_dir():
sys.path.append(self.op_impl_path)
else:
raise EnvironmentError(
f"No such directory: {self.op_impl_path}, Please check if Ascend 910 AI software package is "
"installed correctly.")
logger.warning(f"No such directory: {self.op_impl_path}, Please check if Ascend 910 AI software package is "
f"installed correctly.")

if Path(self.cce_path).is_dir():
os.environ['PATH'] = self.cce_path + ":" + os.environ['PATH']
else:
raise EnvironmentError(
f"No such directory: {self.cce_path}, Please check if Ascend 910 AI software package is "
"installed correctly.")
logger.warning(f"No such directory: {self.cce_path}, Please check if Ascend 910 AI software package is "
f"installed correctly.")

if self.op_path is None:
pass
elif Path(self.op_path).is_dir():
os.environ['ASCEND_OPP_PATH'] = self.op_path
else:
raise EnvironmentError(
f"No such directory: {self.op_path}, Please check if Ascend 910 AI software package is "
"installed correctly.")
logger.warning(f"No such directory: {self.op_path}, Please check if Ascend 910 AI software package is "
f"installed correctly.")

def _check_env(self):
"""ascend dependence path check"""


+ 149
- 1
tests/ut/python/tests/common.py View File

@@ -17,8 +17,10 @@
import os
from functools import wraps
from shutil import rmtree
from mindspore_serving import worker
from mindspore_serving import master
from mindspore_serving import worker
from mindspore_serving.client import Client

servable_index = 0

@@ -71,6 +73,9 @@ class ServingTestBase:
fp.write(servable_config_content)


client_create_list = []


def serving_test(func):
@wraps(func)
def wrap_test(*args, **kwargs):
@@ -81,10 +86,21 @@ def serving_test(func):
worker.stop()
servable_dir = os.path.join(os.getcwd(), "serving_python_ut_servables")
rmtree(servable_dir, True)
global client_create_list
for client in client_create_list:
del client.stub
client.stub = None
client_create_list = []

return wrap_test


def create_client(ip, port, servable_name, method_name, version_number=0):
client = Client(ip, port, servable_name, method_name, version_number)
client_create_list.append(client)
return client


def release_client(client):
del client.stub
client.stub = None
@@ -119,3 +135,135 @@ def add_cast(x1, x2):
y = register.call_servable(x1, x2)
return y
"""


def init_add_servable():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += servable_config_preprocess_cast
servable_content += servable_config_method_add_common
servable_content += servable_config_method_add_cast
base.init_servable_with_servable_config(1, servable_content)
return base


def init_str_servable():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += r"""
def preprocess(other):
return np.ones([2,2], np.float32), np.ones([2,2], np.float32)
def str_concat_postprocess(text1, text2):
return text1 + text2

@register.register_method(output_names=["text"])
def str_concat(text1, text2):
x1, x2 = register.call_preprocess(preprocess, text1)
y = register.call_servable(x1, x2)
text = register.call_postprocess(str_concat_postprocess, text1, text2)
return text
def str_empty_postprocess(text1, text2):
if len(text1) == 0:
text = text2
else:
text = ""
return text

@register.register_method(output_names=["text"])
def str_empty(text1, text2):
x1, x2 = register.call_preprocess(preprocess, text1)
y = register.call_servable(x1, x2)
text = register.call_postprocess(str_empty_postprocess, text1, text2)
return text
"""
base.init_servable_with_servable_config(1, servable_content)
return base


def init_bytes_servable():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += r"""
def preprocess(other):
return np.ones([2,2], np.float32), np.ones([2,2], np.float32)

def bytes_concat_postprocess(text1, text2):
text1 = bytes.decode(text1.tobytes()) # bytes decode to str
text2 = bytes.decode(text2.tobytes()) # bytes decode to str
return str.encode(text1 + text2) # str encode to bytes

@register.register_method(output_names=["text"])
def bytes_concat(text1, text2):
x1, x2 = register.call_preprocess(preprocess, text1)
y = register.call_servable(x1, x2)
text = register.call_postprocess(bytes_concat_postprocess, text1, text2)
return text

def bytes_empty_postprocess(text1, text2):
text1 = bytes.decode(text1.tobytes()) # bytes decode to str
text2 = bytes.decode(text2.tobytes()) # bytes decode to str
if len(text1) == 0:
text = text2
else:
text = ""
return str.encode(text) # str encode to bytes

@register.register_method(output_names=["text"])
def bytes_empty(text1, text2):
x1, x2 = register.call_preprocess(preprocess, text1)
y = register.call_servable(x1, x2)
text = register.call_postprocess(bytes_empty_postprocess, text1, text2)
return text
"""
base.init_servable_with_servable_config(1, servable_content)
return base


def init_bool_int_float_servable():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += r"""
def preprocess(other):
return np.ones([2,2], np.float32), np.ones([2,2], np.float32)

def bool_postprocess(bool_val):
return ~bool_val

@register.register_method(output_names=["value"])
def bool_not(bool_val):
x1, x2 = register.call_preprocess(preprocess, bool_val)
y = register.call_servable(x1, x2)
value = register.call_postprocess(bool_postprocess, bool_val)
return value

def int_postprocess(int_val):
return int_val + 1

@register.register_method(output_names=["value"])
def int_plus_1(int_val):
x1, x2 = register.call_preprocess(preprocess, int_val)
y = register.call_servable(x1, x2)
value = register.call_postprocess(int_postprocess, int_val)
return value
def float_postprocess(float_val):
value = float_val + 1
if value.dtype == np.float16:
value = value.astype(np.float32)
return value
@register.register_method(output_names=["value"])
def float_plus_1(float_val):
x1, x2 = register.call_preprocess(preprocess, float_val)
y = register.call_servable(x1, x2)
value = register.call_postprocess(float_postprocess, float_val)
return value
"""
base.init_servable_with_servable_config(1, servable_content)
return base

+ 32
- 16
tests/ut/python/tests/common_restful.py View File

@@ -18,8 +18,15 @@ import json
import requests
import numpy as np

from common import init_str_servable, init_bytes_servable, init_bool_int_float_servable
from mindspore_serving import master, worker

def compare_float_value(expect, result):

def compare_float_value(result, expect):
if isinstance(expect, (float, int)):
assert isinstance(result, float)
assert abs(expect - result) < 0.001
return
expect = np.array(expect)
result = np.array(result)
assert (np.abs(expect - result) < 0.001).all()
@@ -37,19 +44,7 @@ def create_multi_instances_fp32(instance_count):
return instances, y_data_list


def create_multi_instances_int32_input_fp32_output(instance_count):
instances = []
# instance 1
y_data_list = []
for i in range(instance_count):
x1 = np.asarray([[1.1, 2.2], [3.3, 4.4]]).astype(np.int32) * (i + 1)
x2 = np.asarray([[5.5, 6.6], [7.7, 8.8]]).astype(np.int32) * (i + 1)
y_data_list.append((x1 + x2).astype(np.float32))
instances.append({"x1": x1.tolist(), "x2": x2.tolist()})
return instances, y_data_list


def check_result(result, y_data_list, output_name="y"):
def check_number_result(result, y_data_list, output_name="y"):
result = result["instances"]
assert len(result) == len(y_data_list)
for result_item, expected_item in zip(result, y_data_list):
@@ -63,13 +58,34 @@ def check_result(result, y_data_list, output_name="y"):
def post_restful(ip, restful_port, servable_name, method_name, json_instances, version_number=None):
instances_map = {"instances": json_instances}
post_payload = json.dumps(instances_map)
print("request:", post_payload)
print("request:", post_payload[:200])
if version_number is not None:
request_url = f"http://{ip}:{restful_port}/model/{servable_name}/version/{version_number}:{method_name}"
result = requests.post(request_url, data=post_payload)
else:
request_url = f"http://{ip}:{restful_port}/model/{servable_name}:{method_name}"
result = requests.post(request_url, data=post_payload)
print("result", result.text)
print("result", result.text[:200])
result = json.loads(result.text)
return result


def start_str_restful_server():
base = init_str_servable()
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_restful_server("0.0.0.0", 5500)
return base


def start_bytes_restful_server():
base = init_bytes_servable()
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_restful_server("0.0.0.0", 5500)
return base


def start_bool_int_float_restful_server():
base = init_bool_int_float_servable()
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_restful_server("0.0.0.0", 5500)
return base

+ 600
- 0
tests/ut/python/tests/test_grpc_request.py View File

@@ -0,0 +1,600 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""test Serving with master, worker and client"""

import numpy as np

from common import init_str_servable, init_bytes_servable, init_bool_int_float_servable
from common import serving_test, create_client
from mindspore_serving import master
from mindspore_serving import worker


def check_result(result, y_data_list):
assert len(result) == len(y_data_list)
for result_item, y_data in zip(result, y_data_list):
assert (result_item["y"] == y_data).all()


def start_str_grpc_server():
base = init_str_servable()
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
return base


def start_bytes_grpc_server():
base = init_bytes_servable()
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
return base


def start_bool_int_float_grpc_server():
base = init_bool_int_float_servable()
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
return base


@serving_test
def test_grpc_request_str_input_output_success():
base = start_str_grpc_server()
# Client
instances = [{}, {}, {}]
str_a = ["ABC", "DEF", "HIJ"]
str_b = ["123", "456", "789"]
for i, instance in enumerate(instances):
instance["text1"] = str_a[i]
instance["text2"] = str_b[i]

client = create_client("localhost", 5500, base.servable_name, "str_concat")
result = client.infer(instances)
assert result[0]["text"] == str_a[0] + str_b[0]
assert result[1]["text"] == str_a[1] + str_b[1]
assert result[2]["text"] == str_a[2] + str_b[2]


@serving_test
def test_grpc_request_empty_str_input_output_success():
base = start_str_grpc_server()
# Client
instances = [{}, {}, {}]
str_a = ["ABC", "", "HIJ"]
str_b = ["123", "456", "789"]
for i, instance in enumerate(instances):
instance["text1"] = str_a[i]
instance["text2"] = str_b[i]

client = create_client("localhost", 5500, base.servable_name, "str_empty")
result = client.infer(instances)
assert result[0]["text"] == ""
assert result[1]["text"] == "456"
assert result[2]["text"] == ""


@serving_test
def test_grpc_request_str_shape1_list_input_failed():
base = start_str_grpc_server()
# Client
instances = [{}, {}, {}]
str_a = ["ABC", "DEF", "HIJ"]
str_b = ["123", "456", "789"]
for i, instance in enumerate(instances):
instance["text1"] = [str_a[i]]
instance["text2"] = [str_b[i]]

client = create_client("localhost", 5500, base.servable_name, "str_concat")
try:
client.infer(instances)
assert False
except RuntimeError as e:
assert "Not support value type <class 'list'>" in str(e)


@serving_test
def test_grpc_request_str_np_1d_array_input_failed():
base = start_str_grpc_server()
# Client
instances = [{}, {}, {}]
str_a = ["ABC", "DEF", "HIJ"]
str_b = ["123", "456", "789"]
for i, instance in enumerate(instances):
instance["text1"] = np.array([str_a[i], str_a[i]])
instance["text2"] = np.array([str_b[i], str_b[i]])
print(instance)

client = create_client("localhost", 5500, base.servable_name, "str_concat")
try:
client.infer(instances)
assert False
except RuntimeError as e:
assert "Unknown data type" in str(e)


@serving_test
def test_grpc_request_bytes_input_output_success():
base = start_bytes_grpc_server()
# Client
instances = [{}, {}, {}]
str_a = ["ABC", "DEF", "HIJ"]
str_b = ["123", "456", "789"]
for i, instance in enumerate(instances):
instance["text1"] = str.encode(str_a[i])
instance["text2"] = str.encode(str_b[i])

client = create_client("localhost", 5500, base.servable_name, "bytes_concat")
result = client.infer(instances)
assert bytes.decode(result[0]["text"]) == str_a[0] + str_b[0]
assert bytes.decode(result[1]["text"]) == str_a[1] + str_b[1]
assert bytes.decode(result[2]["text"]) == str_a[2] + str_b[2]


@serving_test
def test_grpc_request_empty_bytes_input_output_success():
base = start_bytes_grpc_server()
# Client
instances = [{}, {}, {}]
str_a = ["ABC", "", "HIJ"]
str_b = ["123", "456", "789"]
for i, instance in enumerate(instances):
instance["text1"] = str.encode(str_a[i])
instance["text2"] = str.encode(str_b[i])

client = create_client("localhost", 5500, base.servable_name, "bytes_empty")
result = client.infer(instances)
assert bytes.decode(result[0]["text"]) == ""
assert bytes.decode(result[1]["text"]) == str_b[1]
assert bytes.decode(result[2]["text"]) == ""


@serving_test
def test_grpc_request_bytes_1d_array_input_failed():
base = start_bytes_grpc_server()
# Client
instances = [{}, {}, {}]
str_a = ["ABC", "DEF", "HIJ"]
str_b = ["123", "456", "789"]
for i, instance in enumerate(instances):
instance["text1"] = [str.encode(str_a[i])]
instance["text2"] = [str.encode(str_b[i])]

client = create_client("localhost", 5500, base.servable_name, "bytes_concat")
try:
client.infer(instances)
assert False
except RuntimeError as e:
assert "Not support value type <class 'list'>" in str(e)


@serving_test
def test_grpc_request_bool_scalar_input_output_success():
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
instance["bool_val"] = (i % 2 == 0)

client = create_client("localhost", 5500, base.servable_name, "bool_not")
result = client.infer(instances)
assert not result[0]["value"]
assert result[1]["value"]
assert not result[2]["value"]


@serving_test
def test_grpc_request_bool_1d_array_input_output_success():
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i % 2 == 0)
val = [val] * i
instance["bool_val"] = np.array(val).astype(np.bool)

client = create_client("localhost", 5500, base.servable_name, "bool_not")
result = client.infer(instances)
assert result[0]["value"].tolist() == []
assert result[1]["value"].tolist() == [True]
assert result[2]["value"].tolist() == [False, False]


@serving_test
def test_grpc_request_bool_2d_array_input_output_success():
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i % 2 == 0)
val = [[val] * i] * i
if i == 0:
val = [[]]
instance["bool_val"] = np.array(val).astype(np.bool)

client = create_client("localhost", 5500, base.servable_name, "bool_not")
result = client.infer(instances)
assert result[0]["value"].tolist() == [[]]
assert result[1]["value"].tolist() == [[True]]
assert result[2]["value"].tolist() == [[False, False], [False, False]]


@serving_test
def test_grpc_request_bool_invalid_2d_array_input_failed():
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i % 2 == 0)
val = [[val, val], [val]]
instance["bool_val"] = np.array(val)

client = create_client("localhost", 5500, base.servable_name, "bool_not")
try:
client.infer(instances)
assert False
except RuntimeError as e:
assert "Unknown data type object" in str(e)


@serving_test
def test_grpc_request_int_scalar_input_output_success():
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i * 2) * (-1 if i % 2 == 0 else 1) # 0, 2, -4
instance["int_val"] = val

client = create_client("localhost", 5500, base.servable_name, "int_plus_1")
result = client.infer(instances)
assert result[0]["value"] == 1
assert result[1]["value"] == 3
assert result[2]["value"] == -3


def common_test_grpc_request_np_int_type_scalar_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i * 2) * (-1 if i % 2 == 0 else 1) # 0, 2, -4
instance["int_val"] = dtype(val)

client = create_client("localhost", 5500, base.servable_name, "int_plus_1")
result = client.infer(instances)
assert result[0]["value"] == 1
assert result[1]["value"] == 3
assert result[2]["value"] == -3


@serving_test
def test_grpc_request_np_int8_type_scalar_input_output_success():
common_test_grpc_request_np_int_type_scalar_input_output_success(np.int8)


@serving_test
def test_grpc_request_np_int16_type_scalar_input_output_success():
common_test_grpc_request_np_int_type_scalar_input_output_success(np.int16)


@serving_test
def test_grpc_request_np_int32_type_scalar_input_output_success():
common_test_grpc_request_np_int_type_scalar_input_output_success(np.int32)


@serving_test
def test_grpc_request_np_int64_type_scalar_input_output_success():
common_test_grpc_request_np_int_type_scalar_input_output_success(np.int64)


def common_test_grpc_request_np_uint_type_scalar_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i * 2) # 0, 2, 4
instance["int_val"] = dtype(val)

client = create_client("localhost", 5500, base.servable_name, "int_plus_1")
result = client.infer(instances)
assert result[0]["value"] == 1
assert result[1]["value"] == 3
assert result[2]["value"] == 5


@serving_test
def test_grpc_request_np_uint8_type_scalar_input_output_success():
common_test_grpc_request_np_uint_type_scalar_input_output_success(np.uint8)


@serving_test
def test_grpc_request_np_uint16_type_scalar_input_output_success():
common_test_grpc_request_np_uint_type_scalar_input_output_success(np.uint16)


@serving_test
def test_grpc_request_np_uint32_type_scalar_input_output_success():
common_test_grpc_request_np_uint_type_scalar_input_output_success(np.uint32)


@serving_test
def test_grpc_request_np_uint64_type_scalar_input_output_success():
common_test_grpc_request_np_uint_type_scalar_input_output_success(np.uint64)


def common_test_grpc_request_np_int_type_1d_array_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i * 2) * (-1 if i % 2 == 0 else 1) # 0, 2, -4
val = [val] * i
instance["int_val"] = np.array(val).astype(dtype)

client = create_client("localhost", 5500, base.servable_name, "int_plus_1")
result = client.infer(instances)
assert result[0]["value"].tolist() == []
assert result[1]["value"].tolist() == [3]
assert result[2]["value"].tolist() == [-3, -3]


@serving_test
def test_grpc_request_np_int8_type_1d_array_input_output_success():
common_test_grpc_request_np_int_type_1d_array_input_output_success(np.int8)


@serving_test
def test_grpc_request_np_int16_type_1d_array_input_output_success():
common_test_grpc_request_np_int_type_1d_array_input_output_success(np.int16)


@serving_test
def test_grpc_request_np_int32_type_1d_array_input_output_success():
common_test_grpc_request_np_int_type_1d_array_input_output_success(np.int32)


@serving_test
def test_grpc_request_np_int64_type_1d_array_input_output_success():
common_test_grpc_request_np_int_type_1d_array_input_output_success(np.int64)


def common_test_grpc_request_np_uint_type_1d_array_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i * 2) # 0, 2, 4
val = [val] * i
instance["int_val"] = np.array(val).astype(dtype)

client = create_client("localhost", 5500, base.servable_name, "int_plus_1")
result = client.infer(instances)
assert result[0]["value"].tolist() == []
assert result[1]["value"].tolist() == [3]
assert result[2]["value"].tolist() == [5, 5]


@serving_test
def test_grpc_request_np_uint8_type_1d_array_input_output_success():
common_test_grpc_request_np_uint_type_1d_array_input_output_success(np.uint8)


@serving_test
def test_grpc_request_np_uint16_type_1d_array_input_output_success():
common_test_grpc_request_np_uint_type_1d_array_input_output_success(np.uint16)


@serving_test
def test_grpc_request_np_uint32_type_1d_array_input_output_success():
common_test_grpc_request_np_uint_type_1d_array_input_output_success(np.uint32)


@serving_test
def test_grpc_request_np_uint64_type_1d_array_input_output_success():
common_test_grpc_request_np_uint_type_1d_array_input_output_success(np.uint64)


def common_test_grpc_request_np_int_type_2d_array_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i * 2) * (-1 if i % 2 == 0 else 1) # 0, 2, -4
val = [[val] * i] * i
if i == 0:
val = [[]]
instance["int_val"] = np.array(val).astype(dtype)

client = create_client("localhost", 5500, base.servable_name, "int_plus_1")
result = client.infer(instances)
assert result[0]["value"].tolist() == [[]]
assert result[1]["value"].tolist() == [[3]]
assert result[2]["value"].tolist() == [[-3, -3], [-3, -3]]


@serving_test
def test_grpc_request_np_int8_type_2d_array_input_output_success():
common_test_grpc_request_np_int_type_2d_array_input_output_success(np.int8)


@serving_test
def test_grpc_request_np_int16_type_2d_array_input_output_success():
common_test_grpc_request_np_int_type_2d_array_input_output_success(np.int16)


@serving_test
def test_grpc_request_np_int32_type_2d_array_input_output_success():
common_test_grpc_request_np_int_type_2d_array_input_output_success(np.int32)


@serving_test
def test_grpc_request_np_int64_type_2d_array_input_output_success():
common_test_grpc_request_np_int_type_2d_array_input_output_success(np.int64)


def common_test_grpc_request_np_uint_type_2d_array_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
val = (i * 2) # 0, 2, 4
val = [[val] * i] * i
if i == 0:
val = [[]]
instance["int_val"] = np.array(val).astype(dtype)

client = create_client("localhost", 5500, base.servable_name, "int_plus_1")
result = client.infer(instances)
assert result[0]["value"].tolist() == [[]]
assert result[1]["value"].tolist() == [[3]]
assert result[2]["value"].tolist() == [[5, 5], [5, 5]]


@serving_test
def test_grpc_request_np_uint8_type_2d_array_input_output_success():
common_test_grpc_request_np_uint_type_2d_array_input_output_success(np.uint8)


@serving_test
def test_grpc_request_np_uint16_type_2d_array_input_output_success():
common_test_grpc_request_np_uint_type_2d_array_input_output_success(np.uint16)


@serving_test
def test_grpc_request_np_uint32_type_2d_array_input_output_success():
common_test_grpc_request_np_uint_type_2d_array_input_output_success(np.uint32)


@serving_test
def test_grpc_request_np_uint64_type_2d_array_input_output_success():
common_test_grpc_request_np_uint_type_2d_array_input_output_success(np.uint64)


@serving_test
def test_grpc_request_float_scalar_input_output_success():
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
for i, instance in enumerate(instances):
instance["float_val"] = i * 2.2

client = create_client("localhost", 5500, base.servable_name, "float_plus_1")
result = client.infer(instances)
assert result[0]["value"] == 1
assert result[1]["value"] == (2.2 + 1)
assert result[2]["value"] == (4.4 + 1)


def common_test_grpc_request_np_float_type_scalar_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
y_data_list = []
for i, instance in enumerate(instances):
val = (i * 2.2) * (-1 if i % 2 == 0 else 1) # 0, 2.2, -4.4
val = np.array(val).astype(dtype)
y_data_list.append((val + 1).tolist())
instance["float_val"] = val

client = create_client("localhost", 5500, base.servable_name, "float_plus_1")
result = client.infer(instances)
assert result[0]["value"].tolist() == y_data_list[0]
assert result[1]["value"].tolist() == y_data_list[1]
assert result[2]["value"].tolist() == y_data_list[2]


@serving_test
def test_grpc_request_np_float16_scalar_input_output_success():
common_test_grpc_request_np_float_type_scalar_input_output_success(np.float16)


@serving_test
def test_grpc_request_np_float32_scalar_input_output_success():
common_test_grpc_request_np_float_type_scalar_input_output_success(np.float32)


@serving_test
def test_grpc_request_np_float64_scalar_input_output_success():
common_test_grpc_request_np_float_type_scalar_input_output_success(np.float64)


def common_test_grpc_request_np_float_type_1d_array_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
y_data_list = []
for i, instance in enumerate(instances):
val = (i * 2.2) * (-1 if i % 2 == 0 else 1) # 0, 2.2, -4.4
val = [val] * i
val = np.array(val).astype(dtype)
y_data_list.append((val + 1).tolist())
instance["float_val"] = val

client = create_client("localhost", 5500, base.servable_name, "float_plus_1")
result = client.infer(instances)
assert result[0]["value"].tolist() == y_data_list[0]
assert result[1]["value"].tolist() == y_data_list[1]
assert result[2]["value"].tolist() == y_data_list[2]


@serving_test
def test_grpc_request_np_float16_1d_array_input_output_success():
common_test_grpc_request_np_float_type_1d_array_input_output_success(np.float16)


@serving_test
def test_grpc_request_np_float32_1d_array_input_output_success():
common_test_grpc_request_np_float_type_1d_array_input_output_success(np.float32)


@serving_test
def test_grpc_request_np_float64_1d_array_input_output_success():
common_test_grpc_request_np_float_type_1d_array_input_output_success(np.float64)


def common_test_grpc_request_np_float_type_2d_array_input_output_success(dtype):
base = start_bool_int_float_grpc_server()
# Client
instances = [{}, {}, {}]
y_data_list = []
for i, instance in enumerate(instances):
val = (i * 2.2) * (-1 if i % 2 == 0 else 1) # 0, 2.2, -4.4
val = [[val] * i] * i
if i == 0:
val = [[]]
val = np.array(val).astype(dtype)
y_data_list.append((val + 1).tolist())
instance["float_val"] = val

client = create_client("localhost", 5500, base.servable_name, "float_plus_1")
result = client.infer(instances)
assert result[0]["value"].tolist() == y_data_list[0]
assert result[1]["value"].tolist() == y_data_list[1]
assert result[2]["value"].tolist() == y_data_list[2]


@serving_test
def test_grpc_request_np_float16_2d_array_input_output_success():
common_test_grpc_request_np_float_type_2d_array_input_output_success(np.float16)


@serving_test
def test_grpc_request_np_float32_2d_array_input_output_success():
common_test_grpc_request_np_float_type_2d_array_input_output_success(np.float32)


@serving_test
def test_grpc_request_np_float64_2d_array_input_output_success():
common_test_grpc_request_np_float_type_2d_array_input_output_success(np.float64)

+ 71
- 230
tests/ut/python/tests/test_mater_worker_client.py View File

@@ -15,12 +15,12 @@
"""test Serving with master, worker and client"""

import numpy as np
from mindspore_serving import master
from mindspore_serving import worker
from mindspore_serving.client import Client
from common import ServingTestBase, serving_test, release_client

from common import ServingTestBase, serving_test, create_client
from common import servable_config_import, servable_config_declare_servable, servable_config_preprocess_cast
from common import servable_config_method_add_common, servable_config_method_add_cast
from mindspore_serving import master
from mindspore_serving import worker


def create_multi_instances_fp32(instance_count):
@@ -42,105 +42,99 @@ def check_result(result, y_data_list):


@serving_test
def test_master_worker_client_success():
def test_grpc_success():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
master.start_grpc_server("0.0.0.0", 5500)
# Client
client = Client("localhost", 5500, base.servable_name, "add_common")
client = create_client("localhost", 5500, base.servable_name, "add_common")
instance_count = 3
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = client.infer(instances)
release_client(client)

print(result)
check_result(result, y_data_list)


@serving_test
def test_master_worker_client_multi_times_success():
def test_grpc_multi_times_success():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
master.start_grpc_server("0.0.0.0", 5500)
# Client, use with avoid affecting the next use case
client = Client("localhost", 5500, base.servable_name, "add_common")
# Client
client = create_client("localhost", 5500, base.servable_name, "add_common")
for instance_count in range(1, 5):
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = client.infer(instances)
check_result(result, y_data_list)
release_client(client)


@serving_test
def test_master_worker_client_alone_success():
def test_grpc_alone_success():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
master.start_master_server(master_port=7600)
master.start_grpc_server("0.0.0.0", 5500)
worker.start_servable(base.servable_dir, base.servable_name, master_port=7600, worker_port=6600)
# Client
client = Client("localhost", 5500, base.servable_name, "add_common")
client = create_client("localhost", 5500, base.servable_name, "add_common")
instance_count = 3
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = client.infer(instances)
release_client(client)
check_result(result, y_data_list)


@serving_test
def test_master_worker_client_alone_multi_times_success():
def test_grpc_alone_multi_times_success():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
master.start_master_server(master_port=7600)
master.start_grpc_server("0.0.0.0", 5500)
worker.start_servable(base.servable_dir, base.servable_name, master_port=7600, worker_port=6600)
# Client, use with avoid affecting the next use case
client = Client("localhost", 5500, base.servable_name, "add_common")
# Client
client = create_client("localhost", 5500, base.servable_name, "add_common")
for instance_count in range(1, 5):
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = client.infer(instances)
check_result(result, y_data_list)
release_client(client)


@serving_test
def test_master_worker_client_async_success():
def test_grpc_async_success():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
master.start_grpc_server("0.0.0.0", 5500)
# Client
client = Client("localhost", 5500, base.servable_name, "add_common")
client = create_client("localhost", 5500, base.servable_name, "add_common")
instance_count = 3
instances, y_data_list = create_multi_instances_fp32(instance_count)
result_future = client.infer_async(instances)
result = result_future.result()
release_client(client) # avoid affecting the next use case

print(result)
check_result(result, y_data_list)


@serving_test
def test_master_worker_client_async_multi_times_success():
def test_grpc_async_multi_times_success():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
master.start_grpc_server("0.0.0.0", 5500)
# Client, use with avoid affecting the next use case
client = Client("localhost", 5500, base.servable_name, "add_common")
client = create_client("localhost", 5500, base.servable_name, "add_common")
for instance_count in range(1, 5):
instances, y_data_list = create_multi_instances_fp32(instance_count)
result_future = client.infer_async(instances)
result = result_future.result()
check_result(result, y_data_list)
release_client(client)


@serving_test
def test_master_worker_client_start_grpc_twice_failed():
def test_grpc_start_grpc_twice_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
@@ -153,7 +147,7 @@ def test_master_worker_client_start_grpc_twice_failed():


@serving_test
def test_master_worker_client_start_master_grpc_twice_failed():
def test_grpc_start_master_grpc_twice_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
@@ -166,7 +160,7 @@ def test_master_worker_client_start_master_grpc_twice_failed():


@serving_test
def test_master_worker_client_start_restful_server_twice_failed():
def test_grpc_start_restful_server_twice_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
@@ -179,7 +173,7 @@ def test_master_worker_client_start_restful_server_twice_failed():


@serving_test
def test_master_worker_client_alone_repeat_master_and_woker_port_failed():
def test_grpc_alone_repeat_master_and_woker_port_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
master.start_master_server(master_port=7600)
@@ -192,7 +186,7 @@ def test_master_worker_client_alone_repeat_master_and_woker_port_failed():


@serving_test
def test_master_worker_client_alone_repeat_grpc_and_worker_port_failed():
def test_grpc_alone_repeat_grpc_and_worker_port_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
master.start_master_server(master_port=7600)
@@ -205,7 +199,7 @@ def test_master_worker_client_alone_repeat_grpc_and_worker_port_failed():


@serving_test
def test_master_worker_client_alone_repeat_grpc_and_master_port_failed():
def test_grpc_alone_repeat_grpc_and_master_port_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
master.start_master_server(master_port=7600)
@@ -217,7 +211,7 @@ def test_master_worker_client_alone_repeat_grpc_and_master_port_failed():


@serving_test
def test_master_worker_client_alone_repeat_grpc_and_master_port2_failed():
def test_grpc_alone_repeat_grpc_and_master_port2_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
master.start_grpc_server("0.0.0.0", 7600)
@@ -229,7 +223,7 @@ def test_master_worker_client_alone_repeat_grpc_and_master_port2_failed():


@serving_test
def test_master_worker_client_alone_repeat_grpc_and_restful_port_failed():
def test_grpc_alone_repeat_grpc_and_restful_port_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
master.start_grpc_server("0.0.0.0", 7600)
@@ -241,7 +235,7 @@ def test_master_worker_client_alone_repeat_grpc_and_restful_port_failed():


@serving_test
def test_master_worker_client_alone_repeat_grpc_and_restful_port2_failed():
def test_grpc_alone_repeat_grpc_and_restful_port2_failed():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
master.start_restful_server("0.0.0.0", 7600)
@@ -253,7 +247,7 @@ def test_master_worker_client_alone_repeat_grpc_and_restful_port2_failed():


@serving_test
def test_master_worker_client_servable_content_success():
def test_grpc_servable_content_success():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
@@ -267,16 +261,15 @@ def test_master_worker_client_servable_content_success():
# Client
instance_count = 3
instances, y_data_list = create_multi_instances_fp32(instance_count)
client = Client("localhost", 5500, base.servable_name, "add_common")
client = create_client("localhost", 5500, base.servable_name, "add_common")
result = client.infer(instances)
release_client(client)

print(result)
check_result(result, y_data_list)


@serving_test
def test_master_worker_client_preprocess_outputs_count_not_match_failed():
def test_grpc_preprocess_outputs_count_not_match_failed():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
@@ -296,16 +289,15 @@ def add_cast(x1, x2):
# Client
instance_count = 3
instances, _ = create_multi_instances_fp32(instance_count)
client = Client("localhost", 5500, base.servable_name, "add_cast")
client = create_client("localhost", 5500, base.servable_name, "add_cast")
result = client.infer(instances)
release_client(client)

print(result)
assert "Preprocess Failed" in str(result[0]["error"])


@serving_test
def test_master_worker_client_postprocess_outputs_count_not_match_failed():
def test_grpc_postprocess_outputs_count_not_match_failed():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
@@ -325,189 +317,15 @@ def add_cast(x1, x2):
# Client
instance_count = 3
instances, _ = create_multi_instances_fp32(instance_count)
client = Client("localhost", 5500, base.servable_name, "add_cast")
client = create_client("localhost", 5500, base.servable_name, "add_cast")
result = client.infer(instances)
release_client(client)

print(result)
assert "Postprocess Failed" in str(result[0]["error"])


@serving_test
def test_master_worker_client_str_input_output_success():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += r"""
index = 0
list_str = ["123", "456", "789"]
def postprocess(y, label):
global index
text = list_str[index]
index = (index + 1) if index + 1 < len(list_str) else 0
return y.astype(np.int32), label + text

@register.register_method(output_names=["y", "text"])
def add_cast(x1, x2, label):
y = register.call_servable(x1, x2)
y, text = register.call_postprocess(postprocess, y, label)
return y, text
"""
base.init_servable_with_servable_config(1, servable_content)
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
# Client
instance_count = 3
instances, _ = create_multi_instances_fp32(instance_count)
list_str = ["ABC", "DEF", "HIJ"]
for i, instance in enumerate(instances):
instance["label"] = list_str[i]

client = Client("localhost", 5500, base.servable_name, "add_cast")
result = client.infer(instances)
release_client(client)
assert result[0]["text"] == "ABC123"
assert result[1]["text"] == "DEF456"
assert result[2]["text"] == "HIJ789"


@serving_test
def test_master_worker_client_bytes_input_output_success():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += r"""
index = 0
list_str = ["123", "456", "789"]
def postprocess(y, label):
global index
label = bytes.decode(label.tobytes()) # bytes decode to str
text = list_str[index]
index = (index + 1) if index + 1 < len(list_str) else 0
return y.astype(np.int32), str.encode(label + text) # str encode to bytes

@register.register_method(output_names=["y", "text"])
def add_cast(x1, x2, label):
y = register.call_servable(x1, x2)
y, text = register.call_postprocess(postprocess, y, label)
return y, text
"""
base.init_servable_with_servable_config(1, servable_content)
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
# Client
instance_count = 3
instances, _ = create_multi_instances_fp32(instance_count)
list_str = ["ABC", "DEF", "HIJ"]
for i, instance in enumerate(instances):
instance["label"] = str.encode(list_str[i])

client = Client("localhost", 5500, base.servable_name, "add_cast")
result = client.infer(instances)
release_client(client)
assert bytes.decode(result[0]["text"]) == "ABC123"
assert bytes.decode(result[1]["text"]) == "DEF456"
assert bytes.decode(result[2]["text"]) == "HIJ789"


@serving_test
def test_master_worker_client_bool_input_output_success():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += r"""
def postprocess(y, bool_val):
return y.astype(np.int32), not bool_val

@register.register_method(output_names=["y", "value"])
def add_cast(x1, x2, bool_val):
y = register.call_servable(x1, x2)
y, value = register.call_postprocess(postprocess, y, bool_val)
return y, value
"""
base.init_servable_with_servable_config(1, servable_content)
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
# Client
instance_count = 3
instances, _ = create_multi_instances_fp32(instance_count)
for i, instance in enumerate(instances):
instance["bool_val"] = (i % 2 == 0)

client = Client("localhost", 5500, base.servable_name, "add_cast")
result = client.infer(instances)
release_client(client)
assert not result[0]["value"]
assert result[1]["value"]
assert not result[2]["value"]


@serving_test
def test_master_worker_client_int_input_output_success():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += r"""
def postprocess(y, int_val):
return y.astype(np.int32), int_val + 1

@register.register_method(output_names=["y", "value"])
def add_cast(x1, x2, int_val):
y = register.call_servable(x1, x2)
y, value = register.call_postprocess(postprocess, y, int_val)
return y, value
"""
base.init_servable_with_servable_config(1, servable_content)
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
# Client
instance_count = 3
instances, _ = create_multi_instances_fp32(instance_count)
for i, instance in enumerate(instances):
instance["int_val"] = i * 2

client = Client("localhost", 5500, base.servable_name, "add_cast")
result = client.infer(instances)
release_client(client)
assert result[0]["value"] == 1
assert result[1]["value"] == 3
assert result[2]["value"] == 5


@serving_test
def test_master_worker_client_float_input_output_success():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
servable_content += r"""
def postprocess(y, float_val):
return y.astype(np.int32), float_val + 1

@register.register_method(output_names=["y", "value"])
def add_cast(x1, x2, float_val):
y = register.call_servable(x1, x2)
y, value = register.call_postprocess(postprocess, y, float_val)
return y, value
"""
base.init_servable_with_servable_config(1, servable_content)
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
# Client
instance_count = 3
instances, _ = create_multi_instances_fp32(instance_count)
for i, instance in enumerate(instances):
instance["float_val"] = i * 2.2

client = Client("localhost", 5500, base.servable_name, "add_cast")
result = client.infer(instances)
release_client(client)
assert result[0]["value"] == 1
assert result[1]["value"] == (2.2 + 1)
assert result[2]["value"] == (4.4 + 1)


@serving_test
def test_master_worker_client_preprocess_update_numpy_success():
def test_grpc_preprocess_update_numpy_success():
base = ServingTestBase()
servable_content = servable_config_import
servable_content += servable_config_declare_servable
@@ -530,21 +348,44 @@ def add_cast(x1, x2, x3):
worker.start_servable_in_master(base.servable_dir, base.servable_name)
master.start_grpc_server("0.0.0.0", 5500)
# Client
instance_count = 3
instances, _ = create_multi_instances_fp32(instance_count)
instances = [{}, {}, {}]
for instance in instances:
instance["x3"] = np.ones([3])
instance["x1"] = np.ones([2, 2]).astype(np.float32)
instance["x2"] = np.ones([2, 2]).astype(np.float32)
instance["x3"] = np.ones([3]).astype(np.int32)

# Client, use with avoid affecting the next use case
client = Client("localhost", 5500, base.servable_name, "add_cast")
client = create_client("localhost", 5500, base.servable_name, "add_cast")
result = client.infer(instances)
release_client(client)
x3 = np.array([123, 1, 1]) + 1
x4 = np.array([123, 1, 1]) + 2

assert (result[0]["x3"] == x3).all()
assert (result[1]["x3"] == x3).all()
assert (result[2]["x3"] == x3).all()
assert (result[0]["x4"] == x4).all()
assert (result[1]["x4"] == x4).all()
assert (result[2]["x4"] == x4).all()
print(result)

x3 = (np.array([123, 1, 1]) + 1).tolist()
x4 = (np.array([123, 1, 1]) + 2).tolist()

assert result[0]["x3"].tolist() == x3
assert result[0]["x4"].tolist() == x4
assert result[1]["x3"].tolist() == x3
assert result[1]["x4"].tolist() == x4
assert result[2]["x3"].tolist() == x3
assert result[2]["x4"].tolist() == x4


@serving_test
def test_grpc_larger_than_server_receive_max_size():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
master.start_grpc_server("0.0.0.0", 5500, max_msg_mb_size=1) # 1MB
# Client
client = create_client("localhost", 5500, base.servable_name, "add_common")
instances = []
# instance 1
y_data_list = []
x1 = np.ones([1024, 1024], np.float32)
x2 = np.ones([1024, 1024], np.float32)
y_data_list.append(x1 + x2)
instances.append({"x1": x1, "x2": x2})
result = client.infer(instances) # more than 1MB msg

print(result)
assert "Grpc Error, (8, 'resource exhausted')" in str(result["error"])

+ 347
- 298
tests/ut/python/tests/test_restful_base64_data.py
File diff suppressed because it is too large
View File


+ 137
- 692
tests/ut/python/tests/test_restful_json_data.py
File diff suppressed because it is too large
View File


+ 42
- 14
tests/ut/python/tests/test_restful_request.py View File

@@ -15,13 +15,15 @@
"""test Serving RESTful, with master, worker and client"""

import json

import requests
from mindspore_serving import master
from mindspore_serving import worker
import numpy as np
from common import ServingTestBase, serving_test
from common import servable_config_import, servable_config_declare_servable
from common_restful import create_multi_instances_fp32, check_result, post_restful
from common_restful import create_multi_instances_int32_input_fp32_output
from common_restful import create_multi_instances_fp32, check_number_result, post_restful
from mindspore_serving import master
from mindspore_serving import worker


@serving_test
@@ -34,7 +36,7 @@ def test_restful_request_success():
instance_count = 3
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = post_restful("localhost", 5500, base.servable_name, "add_common", instances)
check_result(result, y_data_list)
check_number_result(result, y_data_list)


@serving_test
@@ -46,7 +48,7 @@ def test_restful_request_multi_times_success():
for instance_count in range(1, 5):
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = post_restful("localhost", 5500, base.servable_name, "add_common", instances)
check_result(result, y_data_list)
check_number_result(result, y_data_list)


@serving_test
@@ -55,10 +57,18 @@ def test_restful_request_multi_times_int32_success():
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
master.start_restful_server("0.0.0.0", 5500)

for instance_count in range(1, 5):
instances, y_data_list = create_multi_instances_int32_input_fp32_output(instance_count)
instances = []
# instance 1
y_data_list = []
for i in range(instance_count):
x1 = np.asarray([[1.1, 2.2], [3.3, 4.4]]).astype(np.int32) * (i + 1)
x2 = np.asarray([[5.5, 6.6], [7.7, 8.8]]).astype(np.int32) * (i + 1)
y_data_list.append((x1 + x2).astype(np.float32))
instances.append({"x1": x1.tolist(), "x2": x2.tolist()})
result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances)
check_result(result, y_data_list)
check_number_result(result, y_data_list)


@serving_test
@@ -72,7 +82,7 @@ def test_restful_request_worker_alone_success():
instance_count = 3
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = post_restful("localhost", 5500, base.servable_name, "add_common", instances)
check_result(result, y_data_list)
check_number_result(result, y_data_list)


@serving_test
@@ -86,7 +96,7 @@ def test_restful_request_worker_alone_multi_times_success():
for instance_count in range(1, 5):
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = post_restful("localhost", 5500, base.servable_name, "add_common", instances)
check_result(result, y_data_list)
check_number_result(result, y_data_list)


@serving_test
@@ -128,7 +138,7 @@ def test_restful_request_worker_alone_with_version_number_0_success():
instance_count = 3
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = post_restful("localhost", 5500, base.servable_name, "add_common", instances, 0)
check_result(result, y_data_list)
check_number_result(result, y_data_list)


@serving_test
@@ -142,7 +152,7 @@ def test_restful_request_worker_alone_with_version_number_1_success():
instance_count = 3
instances, y_data_list = create_multi_instances_fp32(instance_count)
result = post_restful("localhost", 5500, base.servable_name, "add_common", instances, 1)
check_result(result, y_data_list)
check_number_result(result, y_data_list)


@serving_test
@@ -187,7 +197,7 @@ def test_restful_request_worker_alone_without_model_invalid_failed():
instances_map = {"instances": instances}
post_payload = json.dumps(instances_map)
print("request:", post_payload)
request_url = f"http://localhost:5500/x/:add_common"
request_url = "http://localhost:5500/x/:add_common"
result = requests.post(request_url, data=post_payload)
print("result", result.text)
result = json.loads(result.text)
@@ -254,7 +264,7 @@ def test_restful_request_worker_alone_servable_version_reverse_success():
result = requests.post(request_url, data=post_payload)
print("result", result.text)
result = json.loads(result.text)
check_result(result, y_data_list)
check_number_result(result, y_data_list)


@serving_test
@@ -333,3 +343,21 @@ def add_cast(x1, x2):
instances, _ = create_multi_instances_fp32(instance_count)
result = post_restful("localhost", 5500, base.servable_name + "_error", "add_common", instances)
assert "servable is not available" in str(result["error_msg"])


@serving_test
def test_restful_request_larger_than_server_receive_max_size():
base = ServingTestBase()
base.init_servable(1, "add_servable_config.py")
worker.start_servable_in_master(base.servable_dir, base.servable_name, 0)
master.start_restful_server("0.0.0.0", 5500, max_msg_mb_size=1) # 1MB
# Client
instances = []
x1 = np.ones([1024, 1024], np.float32)
x2 = np.ones([1024, 1024], np.float32)
instances.append({"x1": x1.tolist(), "x2": x2.tolist()})
# more than 1MB msg
result = post_restful("localhost", 5500, base.servable_name + "_error", "add_common", instances)

print(result)
assert "http message is bigger than 1048576" in str(result["error_msg"])

+ 1
- 1
tests/ut/python/tests/test_start_servable_config.py View File

@@ -14,8 +14,8 @@
# ============================================================================
"""test Serving: test servable_config"""

from mindspore_serving import worker
from common import ServingTestBase, serving_test
from mindspore_serving import worker

# test servable_config.py
servable_config_import = r"""


+ 3
- 3
tests/ut/python/tests/test_start_worker.py View File

@@ -14,9 +14,9 @@
# ============================================================================
"""test Serving with master, worker and client"""

from common import ServingTestBase, serving_test
from mindspore_serving import master
from mindspore_serving import worker
from common import ServingTestBase, serving_test


# start_servable_in_master
@@ -184,7 +184,7 @@ def test_start_servable_in_master_device_type_value_invalid_failed():
worker.start_servable_in_master(base.servable_dir, base.servable_name, device_type="InvalidDeviceType")
assert False
except RuntimeError as e:
assert "Unsupport device type 'InvalidDeviceType'" in str(e)
assert "Unsupported device type 'InvalidDeviceType'" in str(e)


@serving_test
@@ -330,7 +330,7 @@ def test_start_worker_device_type_value_invalid_failed():
worker.start_servable(base.servable_dir, base.servable_name, device_type="InvalidDeviceType")
assert False
except RuntimeError as e:
assert "Unsupport device type 'InvalidDeviceType'" in str(e)
assert "Unsupported device type 'InvalidDeviceType'" in str(e)


@serving_test


Loading…
Cancel
Save