From 21a9abac7956ed6bee4174efc78e3d588cf55f06 Mon Sep 17 00:00:00 2001 From: xuyongfei Date: Fri, 22 Jan 2021 17:08:59 +0800 Subject: [PATCH] Serving, ut6 --- mindspore_serving/ccsrc/common/log.cc | 6 +- mindspore_serving/ccsrc/common/log.h | 22 +- .../ccsrc/master/restful/http_process.cc | 19 +- mindspore_serving/ccsrc/worker/context.cc | 2 +- mindspore_serving/client/python/client.py | 2 +- mindspore_serving/worker/_check_version.py | 20 +- tests/ut/python/tests/common.py | 150 +++- tests/ut/python/tests/common_restful.py | 48 +- tests/ut/python/tests/test_grpc_request.py | 600 +++++++++++++ .../python/tests/test_mater_worker_client.py | 301 ++----- .../python/tests/test_restful_base64_data.py | 645 +++++++------- .../ut/python/tests/test_restful_json_data.py | 829 +++--------------- tests/ut/python/tests/test_restful_request.py | 56 +- .../tests/test_start_servable_config.py | 2 +- tests/ut/python/tests/test_start_worker.py | 6 +- 15 files changed, 1420 insertions(+), 1288 deletions(-) create mode 100644 tests/ut/python/tests/test_grpc_request.py diff --git a/mindspore_serving/ccsrc/common/log.cc b/mindspore_serving/ccsrc/common/log.cc index 71b6de6..b0d6233 100644 --- a/mindspore_serving/ccsrc/common/log.cc +++ b/mindspore_serving/ccsrc/common/log.cc @@ -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(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(ch); diff --git a/mindspore_serving/ccsrc/common/log.h b/mindspore_serving/ccsrc/common/log.h index 8064488..8d0e4de 100644 --- a/mindspore_serving/ccsrc/common/log.h +++ b/mindspore_serving/ccsrc/common/log.h @@ -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_; diff --git a/mindspore_serving/ccsrc/master/restful/http_process.cc b/mindspore_serving/ccsrc/master/restful/http_process.cc index bb25ac1..f271db4 100644 --- a/mindspore_serving/ccsrc/master/restful/http_process.cc +++ b/mindspore_serving/ccsrc/master/restful/http_process.cc @@ -42,11 +42,12 @@ static const std::map http_type2_infer_type{{HTTP_DATA {HTTP_DATA_OBJ, DataType::kMSI_Bytes}}; static const std::map 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 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(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(pb_tensor, false, index, js); diff --git a/mindspore_serving/ccsrc/worker/context.cc b/mindspore_serving/ccsrc/worker/context.cc index accd0b3..3cdfd63 100644 --- a/mindspore_serving/ccsrc/worker/context.cc +++ b/mindspore_serving/ccsrc/worker/context.cc @@ -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); diff --git a/mindspore_serving/client/python/client.py b/mindspore_serving/client/python/client.py index 821fcca..6ccedf9 100644 --- a/mindspore_serving/client/python/client.py +++ b/mindspore_serving/client/python/client.py @@ -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, diff --git a/mindspore_serving/worker/_check_version.py b/mindspore_serving/worker/_check_version.py index 21a21dc..ee49340 100644 --- a/mindspore_serving/worker/_check_version.py +++ b/mindspore_serving/worker/_check_version.py @@ -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""" diff --git a/tests/ut/python/tests/common.py b/tests/ut/python/tests/common.py index 670e6bd..fb6ac43 100644 --- a/tests/ut/python/tests/common.py +++ b/tests/ut/python/tests/common.py @@ -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 diff --git a/tests/ut/python/tests/common_restful.py b/tests/ut/python/tests/common_restful.py index 9c16087..10968ad 100644 --- a/tests/ut/python/tests/common_restful.py +++ b/tests/ut/python/tests/common_restful.py @@ -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 diff --git a/tests/ut/python/tests/test_grpc_request.py b/tests/ut/python/tests/test_grpc_request.py new file mode 100644 index 0000000..2592369 --- /dev/null +++ b/tests/ut/python/tests/test_grpc_request.py @@ -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 " 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 " 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) diff --git a/tests/ut/python/tests/test_mater_worker_client.py b/tests/ut/python/tests/test_mater_worker_client.py index 07238db..d7b156f 100644 --- a/tests/ut/python/tests/test_mater_worker_client.py +++ b/tests/ut/python/tests/test_mater_worker_client.py @@ -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"]) diff --git a/tests/ut/python/tests/test_restful_base64_data.py b/tests/ut/python/tests/test_restful_base64_data.py index 76f6527..487a0bb 100644 --- a/tests/ut/python/tests/test_restful_base64_data.py +++ b/tests/ut/python/tests/test_restful_base64_data.py @@ -15,156 +15,42 @@ """test Serving RESTful, with master, worker and client""" import base64 + 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 compare_float_value, check_number_result, post_restful +from common_restful import start_str_restful_server, start_bytes_restful_server, start_bool_int_float_restful_server from mindspore_serving import master from mindspore_serving import worker -def start_str_restful_server(): - 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_common(x1, x2, label): - y = register.call_servable(x1, x2) - y, text = register.call_postprocess(postprocess, y, label) - return y, text - -def empty_postprocess(y, label): - global index - if len(label) == 0: - text = list_str[index] - else: - text = "" - index = (index + 1) if index + 1 < len(list_str) else 0 - return y.astype(np.int32), text - -@register.register_method(output_names=["y", "text"]) -def add_empty(x1, x2, label): - y = register.call_servable(x1, x2) - y, text = register.call_postprocess(empty_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_restful_server("0.0.0.0", 5500) - return base - - -def start_bytes_restful_server(): - 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_common(x1, x2, label): - y = register.call_servable(x1, x2) - y, text = register.call_postprocess(postprocess, y, label) - return y, text - -def empty_postprocess(y, label): - global index - label = bytes.decode(label.tobytes()) # bytes decode to str - if len(label) == 0: - text = list_str[index] - else: - text = "" - index = (index + 1) if index + 1 < len(list_str) else 0 - return y.astype(np.int32), str.encode(text) # str encode to bytes - -@register.register_method(output_names=["y", "text"]) -def add_empty(x1, x2, label): - y = register.call_servable(x1, x2) - y, text = register.call_postprocess(empty_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_restful_server("0.0.0.0", 5500) - return base - - -def start_bool_int_float_restful_server(): - base = ServingTestBase() - servable_content = servable_config_import - servable_content += servable_config_declare_servable - servable_content += r""" -def bool_postprocess(y, bool_val): - return y.astype(np.int32), ~bool_val - -@register.register_method(output_names=["y", "value"]) -def add_bool(x1, x2, bool_val): - y = register.call_servable(x1, x2) - y, value = register.call_postprocess(bool_postprocess, y, bool_val) - return y, value - -def int_postprocess(y, int_val): - return y.astype(np.int32), int_val + 1 - -@register.register_method(output_names=["y", "value"]) -def add_int(x1, x2, int_val): - y = register.call_servable(x1, x2) - y, value = register.call_postprocess(int_postprocess, y, int_val) - return y, value - -def float_postprocess(y, float_val): - value = float_val + 1 - if value.dtype == np.float16: - value = value.astype(np.float32) - return y, value - -@register.register_method(output_names=["y", "value"]) -def add_float(x1, x2, float_val): - y = register.call_servable(x1, x2) - y, value = register.call_postprocess(float_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_restful_server("0.0.0.0", 5500) - return base +def b64_decode_to_str(a): + return bytes.decode(base64.b64decode(a["b64"])) def common_test_restful_base64_str_scalar_input_output_success(shape): base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): if shape is None: - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode(), "type": "str"} + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode(), "type": "str"} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode(), "type": "str"} else: - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode(), "type": "str", + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode(), "type": "str", + 'shape': shape} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode(), "type": "str", 'shape': shape} - result = post_restful("localhost", 5500, base.servable_name, "add_common", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) result = result["instances"] - assert result[0]["text"] == "ABC123" - assert result[1]["text"] == "DEF456" - assert result[2]["text"] == "HIJ789" + 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 @@ -186,13 +72,14 @@ def test_restful_base64_str_scalar_shape_empty_input_output_success(): def test_restful_base64_empty_str_input_output_success(): base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode(), "type": "str"} + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode(), "type": "str"} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode(), "type": "str"} - result = post_restful("localhost", 5500, base.servable_name, "add_empty", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_empty", instances) result = result["instances"] assert result[0]["text"] == "" assert result[1]["text"] == "456" @@ -203,13 +90,14 @@ def test_restful_base64_empty_str_input_output_success(): def test_restful_base64_str_scalar_invalid_shape0_input_failed(): base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode(), "type": "str", "shape": [0]} + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode(), "type": "str", "shape": [0]} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode(), "type": "str", "shape": [0]} - result = post_restful("localhost", 5500, base.servable_name, "add_common", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) assert "only support scalar when data type is string or bytes, please check 'type' or 'shape'" \ in str(result["error_msg"]) @@ -218,13 +106,14 @@ def test_restful_base64_str_scalar_invalid_shape0_input_failed(): def test_restful_base64_str_scalar_invalid_shape_input_failed(): base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode(), "type": "str", 'shape': [2]} + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode(), "type": "str", 'shape': [2]} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode(), "type": "str", 'shape': [2]} - result = post_restful("localhost", 5500, base.servable_name, "add_common", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) assert "json object, only support scalar when data type is string or bytes, please check 'type' or 'shape'" \ in str(result["error_msg"]) @@ -233,35 +122,38 @@ def test_restful_base64_str_scalar_invalid_shape_input_failed(): def test_restful_base64_str_1d_array_failed(): base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = [{"b64": base64.b64encode(str.encode(list_str[i])).decode(), "type": "str"}, - {"b64": base64.b64encode(str.encode(list_str[i])).decode(), "type": "str"}] + instance["text1"] = [{"b64": base64.b64encode(str.encode(str_a[i])).decode(), "type": "str"}, + {"b64": base64.b64encode(str.encode(str_a[i])).decode(), "type": "str"}] + instance["text2"] = [{"b64": base64.b64encode(str.encode(str_b[i])).decode(), "type": "str"}, + {"b64": base64.b64encode(str.encode(str_b[i])).decode(), "type": "str"}] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) assert "json array, string or bytes type only support one item" in str(result["error_msg"]) def common_test_restful_bytes_input_output_success(shape): base = start_bytes_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): if shape is not None: - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode(), "shape": shape} + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode(), "shape": shape} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode(), "shape": shape} else: - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode()} + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode()} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode()} - result = post_restful("localhost", 5500, base.servable_name, "add_common", instances) + result = post_restful("localhost", 5500, base.servable_name, "bytes_concat", instances) result = result["instances"] - b64_decode_to_str = lambda a: bytes.decode(base64.b64decode(a["b64"])) - assert b64_decode_to_str(result[0]["text"]) == "ABC123" - assert b64_decode_to_str(result[1]["text"]) == "DEF456" - assert b64_decode_to_str(result[2]["text"]) == "HIJ789" + assert b64_decode_to_str(result[0]["text"]) == str_a[0] + str_b[0] + assert b64_decode_to_str(result[1]["text"]) == str_a[1] + str_b[1] + assert b64_decode_to_str(result[2]["text"]) == str_a[2] + str_b[2] @serving_test @@ -283,15 +175,15 @@ def test_restful_bytes_shape1_success(): def test_restful_empty_bytes_input_output_success(): base = start_bytes_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode()} + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode()} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode()} - result = post_restful("localhost", 5500, base.servable_name, "add_empty", instances) + result = post_restful("localhost", 5500, base.servable_name, "bytes_empty", instances) result = result["instances"] - b64_decode_to_str = lambda a: bytes.decode(base64.b64decode(a["b64"])) assert b64_decode_to_str(result[0]["text"]) == "" assert b64_decode_to_str(result[1]["text"]) == "456" assert b64_decode_to_str(result[2]["text"]) == "" @@ -301,14 +193,16 @@ def test_restful_empty_bytes_input_output_success(): def test_restful_bytes_1d_array_failed(): base = start_bytes_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = [{"b64": base64.b64encode(str.encode(list_str[i])).decode()}, - {"b64": base64.b64encode(str.encode(list_str[i])).decode()}] + instance["text1"] = [{"b64": base64.b64encode(str.encode(str_a[i])).decode()}, + {"b64": base64.b64encode(str.encode(str_a[i])).decode()}] + instance["text2"] = [{"b64": base64.b64encode(str.encode(str_b[i])).decode()}, + {"b64": base64.b64encode(str.encode(str_b[i])).decode()}] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bytes_concat", instances) assert "json array, string or bytes type only support one item" in str(result["error_msg"]) @@ -316,13 +210,14 @@ def test_restful_bytes_1d_array_failed(): def test_restful_bytes_invalid_shape_input_failed(): base = start_bytes_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = {"b64": base64.b64encode(str.encode(list_str[i])).decode(), 'shape': [0]} + instance["text1"] = {"b64": base64.b64encode(str.encode(str_a[i])).decode(), 'shape': [0]} + instance["text2"] = {"b64": base64.b64encode(str.encode(str_b[i])).decode(), 'shape': [0]} - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bytes_concat", instances) assert "only support scalar when data type is string or bytes, please check 'type' or 'shape'" \ in result["error_msg"] @@ -331,13 +226,12 @@ def test_restful_bytes_invalid_shape_input_failed(): def test_restful_base64_bool_scalar_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = np.int8(i % 2 == 0) instance["bool_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "bool"} - result = post_restful("localhost", 5500, base.servable_name, "add_bool", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) result = result["instances"] assert not result[0]["value"] assert result[1]["value"] @@ -348,14 +242,13 @@ def test_restful_base64_bool_scalar_input_output_success(): def test_restful_base64_bool_1d_array_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = [(i % 2 == 0)] * (i + 1) val = np.array(val) instance["bool_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "bool", "shape": [i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_bool", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) result = result["instances"] assert result[0]["value"] == [False] assert result[1]["value"] == [True, True] @@ -366,8 +259,7 @@ def test_restful_base64_bool_1d_array_input_output_success(): def test_restful_base64_bool_2d_array_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = (i % 2 == 0) val = [[val] * (i + 1)] * (i + 1) @@ -375,7 +267,7 @@ def test_restful_base64_bool_2d_array_input_output_success(): instance["bool_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "bool", "shape": [i + 1, i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_bool", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) result = result["instances"] assert result[0]["value"] == [[False]] assert result[1]["value"] == [[True, True], [True, True]] @@ -386,13 +278,12 @@ def test_restful_base64_bool_2d_array_input_output_success(): def test_restful_base64_int_scalar_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = np.int32(i * 2) instance["int_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "int32"} - result = post_restful("localhost", 5500, base.servable_name, "add_int", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == 1 assert result[1]["value"] == 3 @@ -403,8 +294,7 @@ def test_restful_base64_int_scalar_input_output_success(): def test_restful_base64_int_1d_empty_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): if i % 2 == 0: val = [] @@ -413,7 +303,7 @@ def test_restful_base64_int_1d_empty_input_output_success(): val = np.array(val).astype(np.int32) instance["int_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "int32", "shape": val.shape} - result = post_restful("localhost", 5500, base.servable_name, "add_int", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == [] assert result[1]["value"] == [3, 3] @@ -424,8 +314,7 @@ def test_restful_base64_int_1d_empty_input_output_success(): def test_restful_base64_int_2d_empty_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): if i % 2 == 0: val = [[]] @@ -434,7 +323,7 @@ def test_restful_base64_int_2d_empty_input_output_success(): val = np.array(val).astype(np.int32) instance["int_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "int32", "shape": val.shape} - result = post_restful("localhost", 5500, base.servable_name, "add_int", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == [[]] assert result[1]["value"] == [3, 3] @@ -445,14 +334,13 @@ def test_restful_base64_int_2d_empty_input_output_success(): def test_restful_base64_int_2d_empty_invalid_shape_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for _, instance in enumerate(instances): val = [[]] val = np.array(val).astype(np.int32) instance["int_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "int32", "shape": [1, 2, 0, 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_int", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) assert "json object, key is 'shape', invalid shape value" in result["error_msg"] @@ -460,15 +348,14 @@ def test_restful_base64_int_2d_empty_invalid_shape_failed(): def test_restful_base64_int_1d_array_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = i * 2 val = [val] * (i + 1) val = np.array(val).astype(np.int32) instance["int_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "int32", "shape": val.shape} - result = post_restful("localhost", 5500, base.servable_name, "add_int", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == [1] assert result[1]["value"] == [3, 3] @@ -478,8 +365,7 @@ def test_restful_base64_int_1d_array_input_output_success(): def common_test_restful_base64_int_type_2d_array_input_output_success(dtype): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype_str_map = {np.int8: "int8", np.int16: "int16", np.int32: "int32", np.int64: "int64"} assert dtype in dtype_str_map for i, instance in enumerate(instances): @@ -489,7 +375,7 @@ def common_test_restful_base64_int_type_2d_array_input_output_success(dtype): instance["int_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], "shape": val.shape} - result = post_restful("localhost", 5500, base.servable_name, "add_int", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == [[-1]] assert result[1]["value"] == [[5, 5], [5, 5]] @@ -519,8 +405,7 @@ def test_restful_base64_int64_2d_array_input_output_success(): def common_test_restful_base64_uint_type_2d_array_input_output_success(dtype): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype_str_map = {np.uint8: "uint8", np.uint16: "uint16", np.uint32: "uint32", np.uint64: "uint64"} assert dtype in dtype_str_map for i, instance in enumerate(instances): @@ -530,7 +415,7 @@ def common_test_restful_base64_uint_type_2d_array_input_output_success(dtype): instance["int_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], "shape": val.shape} - result = post_restful("localhost", 5500, base.servable_name, "add_int", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == [[1]] assert result[1]["value"] == [[3, 3], [3, 3]] @@ -561,13 +446,12 @@ def test_restful_base64_uint64_2d_array_input_output_success(): def test_restful_base64_float_scalar_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = np.float32(i * 2.2) instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp32"} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) result = result["instances"] assert result[0]["value"] == 1.0 assert abs(result[1]["value"] - (2.2 + 1)) < 0.001 @@ -578,8 +462,7 @@ def test_restful_base64_float_scalar_input_output_success(): def test_restful_base64_float_1d_array_input_output_success(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] y_data_list = [] for i, instance in enumerate(instances): val = [i * 2.2 * (-1 if i % 2 == 0 else 1)] * (i + 1) # [0], [2.2, 2.2], [-4.4, -4.4, -4.4] @@ -587,18 +470,19 @@ def test_restful_base64_float_1d_array_input_output_success(): y_data_list.append(val + 1) instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp32", 'shape': [i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) - check_result(result, y_data_list, "value") + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + check_number_result(result, y_data_list, "value") -def common_test_restful_base64_float_type_2d_array_input_output_success(dtype): +def common_test_restful_base64_float_type_2d_array_input_output_success(dtype, dtype_str=None): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} assert dtype in dtype_str_map + if dtype_str is None: + dtype_str = dtype_str_map[dtype] y_data_list = [] for i, instance in enumerate(instances): @@ -606,11 +490,11 @@ def common_test_restful_base64_float_type_2d_array_input_output_success(dtype): val = [[val] * (i + 1)] * (i + 1) val = np.array(val).astype(dtype) y_data_list.append(val + 1) - instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], + instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str, 'shape': [i + 1, i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) - check_result(result, y_data_list, "value") + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + check_number_result(result, y_data_list, "value") @serving_test @@ -628,27 +512,97 @@ def test_restful_base64_float64_2d_array_input_output_success(): common_test_restful_base64_float_type_2d_array_input_output_success(np.float64) +@serving_test +def test_restful_base64_float16_2_2d_array_input_output_success(): + common_test_restful_base64_float_type_2d_array_input_output_success(np.float16, "float16") + + +@serving_test +def test_restful_base64_float32_2_2d_array_input_output_success(): + common_test_restful_base64_float_type_2d_array_input_output_success(np.float32, "float32") + + +@serving_test +def test_restful_base64_float64_2_2d_array_input_output_success(): + common_test_restful_base64_float_type_2d_array_input_output_success(np.float64, "float64") + + +@serving_test +def test_restful_base64_mix_all_type_success(): + base = ServingTestBase() + servable_content = servable_config_import + servable_content += servable_config_declare_servable + servable_content += r""" +def preprocess(float_val): + return np.ones([2,2], np.float32), np.ones([2,2], np.float32) + +def postprocess(bool_val, int_val, float_val, str_val, bytes_val): + return ~bool_val, int_val+1, float_val+1, str_val+"123", str.encode(bytes.decode(bytes_val.tobytes()) + "456") + +@register.register_method(output_names=['bool_val', 'int_val', 'float_val', 'str_val', 'bytes_val']) +def mix_all_type(bool_val, int_val, float_val, str_val, bytes_val): + x1, x2 = register.call_preprocess(preprocess, float_val) + y = register.call_servable(x1, x2) + bool_val, int_val, float_val, str_val, bytes_val = \ + register.call_postprocess(postprocess, bool_val, int_val, float_val, str_val, bytes_val) + return bool_val, int_val, float_val, str_val, bytes_val +""" + base.init_servable_with_servable_config(1, servable_content) + worker.start_servable_in_master(base.servable_dir, base.servable_name) + master.start_restful_server("0.0.0.0", 5500) + # Client + instances = [{}, {}, {}] + for i, instance in enumerate(instances): + float_val = np.array([2.2, 3.3]).astype(np.float32) + instance["float_val"] = {"b64": base64.b64encode(float_val.tobytes()).decode(), 'type': "fp32", 'shape': [2]} + + int_val = np.array([2, 3]).astype(np.int32) + instance["int_val"] = {"b64": base64.b64encode(int_val.tobytes()).decode(), 'type': "int32", 'shape': [2]} + + bool_val = np.array([True, False]) + instance["bool_val"] = {"b64": base64.b64encode(bool_val.tobytes()).decode(), 'type': "bool", 'shape': [2]} + + str_val = "ABC" + instance["str_val"] = {"b64": base64.b64encode(str.encode(str_val)).decode(), 'type': "str", 'shape': []} + + bytes_val = "DEF" + instance["bytes_val"] = {"b64": base64.b64encode(str.encode(bytes_val)).decode(), 'type': "bytes", 'shape': []} + + result = post_restful("localhost", 5500, base.servable_name, "mix_all_type", instances) + result = result["instances"] + + for i in range(3): + compare_float_value(result[i]["float_val"], [3.2, 4.3]) + assert result[i]["int_val"] == [3, 4] + assert result[i]["bool_val"] == [False, True] + assert result[i]["str_val"] == "ABC123" + assert b64_decode_to_str(result[i]["bytes_val"]) == "DEF456" + + @serving_test def test_restful_base64_float16_2d_array_not_support_fp16_output_failed(): base = ServingTestBase() servable_content = servable_config_import servable_content += servable_config_declare_servable servable_content += r""" -def postprocess(y, float_val): - return y, float_val + 1 +def preprocess(float_val): + return np.ones([2,2], np.float32), np.ones([2,2], np.float32) + +def postprocess(float_val): + return float_val + 1 -@register.register_method(output_names=["y", "value"]) -def add_cast(x1, x2, float_val): +@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) - y, value = register.call_postprocess(postprocess, y, float_val) - return y, value + value = register.call_postprocess(postprocess, float_val) + return value """ base.init_servable_with_servable_config(1, servable_content) worker.start_servable_in_master(base.servable_dir, base.servable_name) master.start_restful_server("0.0.0.0", 5500) # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} @@ -663,16 +617,100 @@ def add_cast(x1, x2, float_val): instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], 'shape': [i + 1, i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) - assert "fp16 reply is not supported" in result["error_msg"] + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + assert "float16 reply is not supported" in result["error_msg"] + + +@serving_test +def test_restful_base64_without_b64_key_failed(): + base = start_bool_int_float_restful_server() + # Client + instances = [{}, {}, {}] + + dtype = np.float16 + dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} + assert dtype in dtype_str_map + + 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 + 1)] * (i + 1) + val = np.array(val).astype(dtype) + y_data_list.append(val + 1) + instance["float_val"] = {'type': dtype_str_map[dtype], 'shape': [i + 1, i + 1]} + + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + assert "'b64' should be specified only one time" in result["error_msg"] + + +@serving_test +def test_restful_base64_b64_invalid_type_failed(): + base = start_bool_int_float_restful_server() + # Client + instances = [{}, {}, {}] + + dtype = np.float16 + dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} + assert dtype in dtype_str_map + + 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 + 1)] * (i + 1) + val = np.array(val).astype(dtype) + y_data_list.append(val + 1) + instance["float_val"] = {'b64': 123, 'type': dtype_str_map[dtype], 'shape': [i + 1, i + 1]} + + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + assert "get scalar data failed, type is string, but json is not string type" in result["error_msg"] + + +@serving_test +def test_restful_base64_b64_invalid_value_failed(): + base = start_bool_int_float_restful_server() + # Client + instances = [{}, {}, {}] + + dtype = np.float16 + dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} + assert dtype in dtype_str_map + + 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 + 1)] * (i + 1) + val = np.array(val).astype(dtype) + y_data_list.append(val + 1) + b64_val = base64.b64encode(val.tobytes()).decode() + b64_val = '+==+==' + b64_val[:len('+==+==')] + instance["float_val"] = {'b64': b64_val, 'type': dtype_str_map[dtype], 'shape': [i + 1, i + 1]} + + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + assert "is illegal b64 encode string" in result["error_msg"] + + +@serving_test +def test_restful_base64_b64_value_empty_failed(): + base = start_bool_int_float_restful_server() + # Client + instances = [{}, {}, {}] + + dtype = np.float16 + dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} + assert dtype in dtype_str_map + + for i, instance in enumerate(instances): + instance["float_val"] = {'b64': "", 'type': dtype_str_map[dtype], 'shape': [i + 1, i + 1]} + + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + assert "decode base64 size:0; Given info: type:float16; type size:2; element nums:1" in result["error_msg"] @serving_test def test_restful_base64_dtype_unknow_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} @@ -687,7 +725,7 @@ def test_restful_base64_dtype_unknow_failed(): instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "dtype_unknow", 'shape': [i + 1, i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "Parser request failed, json object, specified type:'dtype_unknow' is illegal" in result["error_msg"] @@ -695,8 +733,7 @@ def test_restful_base64_dtype_unknow_failed(): def test_restful_base64_dtype_empty_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} @@ -711,16 +748,15 @@ def test_restful_base64_dtype_empty_failed(): instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "", 'shape': [i + 1, i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "Parser request failed, json object, specified type:'' is illegal" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_shape_not_match1_large_failed(): +def test_restful_base64_dtype_invalid_type_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} @@ -732,43 +768,57 @@ def test_restful_base64_float16_2d_array_shape_not_match1_large_failed(): val = [[val] * (i + 1)] * (i + 1) val = np.array(val).astype(dtype) y_data_list.append(val + 1) - instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], - 'shape': [i + 2, i + 2]} + instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': 1, + 'shape': [i + 1, i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) - assert "Parser request failed, size is not matched" in result["error_msg"] + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + assert "json object, key is 'type', value should be string type" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_shape_not_match2_small_failed(): +def test_restful_base64_float16_2d_array_dtype_not_match_empty_data_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 - dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} - assert dtype in dtype_str_map + y_data_list = [] + for i, instance in enumerate(instances): + val = [[]] + val = np.array(val).astype(dtype) + y_data_list.append(val + 1) + instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16", + 'shape': [i + 1, i + 1]} + + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) + assert "Parser request failed, size is not matched" in result["error_msg"] + +@serving_test +def test_restful_base64_float16_2d_array_dtype_not_match_size_failed(): + base = start_bool_int_float_restful_server() + # Client + instances = [{}, {}, {}] + + dtype = np.float16 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 + 2)] * (i + 2) val = np.array(val).astype(dtype) y_data_list.append(val + 1) - instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], - 'shape': [i + 1, i + 1]} + instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp32", + 'shape': [i + 2, i + 2]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "Parser request failed, size is not matched" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_shape_not_match3_small_failed(): +def test_restful_base64_float16_2d_array_shape_large_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} @@ -777,63 +827,67 @@ def test_restful_base64_float16_2d_array_shape_not_match3_small_failed(): 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 + 2)] * (i + 2) + val = [[val] * (i + 1)] * (i + 1) val = np.array(val).astype(dtype) y_data_list.append(val + 1) instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], - 'shape': [i + 2, i]} + 'shape': [i + 2, i + 2]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "Parser request failed, size is not matched" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_dtype_not_match4_empty_data_failed(): +def test_restful_base64_float16_2d_array_shape_small_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 + dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} + assert dtype in dtype_str_map + y_data_list = [] for i, instance in enumerate(instances): - val = [[]] + val = i * 2.2 * (-1 if i % 2 == 0 else 1) # 0, 2.2 ,-4.4 + val = [[val] * (i + 2)] * (i + 2) val = np.array(val).astype(dtype) y_data_list.append(val + 1) - instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16", + instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], 'shape': [i + 1, i + 1]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "Parser request failed, size is not matched" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_dtype_not_match5_empty_shape_failed(): +def test_restful_base64_float16_2d_array_shape_small2_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 + dtype_str_map = {np.float16: "fp16", np.float32: "fp32", np.float64: "fp64"} + assert dtype in dtype_str_map + 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 + 2)] * (i + 2) val = np.array(val).astype(dtype) y_data_list.append(val + 1) - instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16", - 'shape': []} + instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': dtype_str_map[dtype], + 'shape': [i + 2, i]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "Parser request failed, size is not matched" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_dtype_not_match6_empty_shape3_failed(): +def test_restful_base64_float16_2d_array_empty_shape_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 y_data_list = [] @@ -842,18 +896,18 @@ def test_restful_base64_float16_2d_array_dtype_not_match6_empty_shape3_failed(): val = [[val] * (i + 2)] * (i + 2) val = np.array(val).astype(dtype) y_data_list.append(val + 1) - instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16"} + instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16", + 'shape': []} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "Parser request failed, size is not matched" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_dtype_not_match_failed(): +def test_restful_base64_float16_2d_array_none_shape_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 y_data_list = [] @@ -862,19 +916,17 @@ def test_restful_base64_float16_2d_array_dtype_not_match_failed(): val = [[val] * (i + 2)] * (i + 2) val = np.array(val).astype(dtype) y_data_list.append(val + 1) - instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp32", - 'shape': [i + 2, i + 2]} + instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16"} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "Parser request failed, size is not matched" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_invalid_shape_2d_shape_failed(): +def test_restful_base64_float16_2d_array_invalid_2d_shape_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 y_data_list = [] @@ -885,16 +937,15 @@ def test_restful_base64_float16_2d_array_invalid_shape_2d_shape_failed(): y_data_list.append(val + 1) instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16", "shape": [[]]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "json object, key is 'shape', array value should be unsigned integer" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_invalid_shape2_str_shape_failed(): +def test_restful_base64_float16_2d_array_invalid_shape_str_shape_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 y_data_list = [] @@ -905,16 +956,15 @@ def test_restful_base64_float16_2d_array_invalid_shape2_str_shape_failed(): y_data_list.append(val + 1) instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16", "shape": ["abc"]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "json object, key is 'shape', array value should be unsigned integer" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_invalid_shape3_float_shape_failed(): +def test_restful_base64_float16_2d_array_float_shape_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 y_data_list = [] @@ -925,16 +975,15 @@ def test_restful_base64_float16_2d_array_invalid_shape3_float_shape_failed(): y_data_list.append(val + 1) instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16", "shape": [1.1]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "json object, key is 'shape', array value should be unsigned integer" in result["error_msg"] @serving_test -def test_restful_base64_float16_2d_array_invalid_shape4_negative_shape_failed(): +def test_restful_base64_float16_2d_array_negative_shape_failed(): base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] dtype = np.float16 y_data_list = [] @@ -945,5 +994,5 @@ def test_restful_base64_float16_2d_array_invalid_shape4_negative_shape_failed(): y_data_list.append(val + 1) instance["float_val"] = {"b64": base64.b64encode(val.tobytes()).decode(), 'type': "fp16", "shape": [-1]} - result = post_restful("localhost", 5500, base.servable_name, "add_float", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) assert "json object, key is 'shape', array value should be unsigned integer" in result["error_msg"] diff --git a/tests/ut/python/tests/test_restful_json_data.py b/tests/ut/python/tests/test_restful_json_data.py index de065c7..eaee0b7 100644 --- a/tests/ut/python/tests/test_restful_json_data.py +++ b/tests/ut/python/tests/test_restful_json_data.py @@ -14,121 +14,59 @@ # ============================================================================ """test Serving RESTful, with master, worker and client""" -from mindspore_serving import master -from mindspore_serving import worker -from common import ServingTestBase, serving_test -from common import servable_config_import, servable_config_declare_servable -from common_restful import compare_float_value, create_multi_instances_fp32, post_restful +from common import serving_test +from common_restful import compare_float_value, post_restful +from common_restful import start_str_restful_server, start_bool_int_float_restful_server @serving_test def test_restful_str_scalar_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_restful_server("0.0.0.0", 5500) + base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = list_str[i] + instance["text1"] = str_a[i] + instance["text2"] = str_b[i] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) result = result["instances"] - assert result[0]["text"] == "ABC123" - assert result[1]["text"] == "DEF456" - assert result[2]["text"] == "HIJ789" + 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_restful_str_scalar_shape1_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_restful_server("0.0.0.0", 5500) + base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = [list_str[i]] + instance["text1"] = [str_a[i]] + instance["text2"] = [str_b[i]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) result = result["instances"] - assert result[0]["text"] == "ABC123" - assert result[1]["text"] == "DEF456" - assert result[2]["text"] == "HIJ789" + 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_restful_empty_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 - if len(label) == 0: - text = list_str[index] - else: - text = "" - index = (index + 1) if index + 1 < len(list_str) else 0 - return y.astype(np.int32), 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_restful_server("0.0.0.0", 5500) + base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = list_str[i] + instance["text1"] = str_a[i] + instance["text2"] = str_b[i] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_empty", instances) result = result["instances"] assert result[0]["text"] == "" assert result[1]["text"] == "456" @@ -137,135 +75,59 @@ def add_cast(x1, x2, label): @serving_test def test_restful_str_2d_array_one_item_input_output_failed(): - 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 - if len(label) == 0: - text = list_str[index] - else: - text = "" - index = (index + 1) if index + 1 < len(list_str) else 0 - return y.astype(np.int32), 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_restful_server("0.0.0.0", 5500) + base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = [[list_str[i]]] + instance["text1"] = [[str_a[i]]] + instance["text2"] = [[str_b[i]]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) assert "bytes or string type input shape can only be (1,) or empty, but given shape is [1, 1]" \ in result["error_msg"] @serving_test def test_restful_str_1d_array_input_failed(): - 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_restful_server("0.0.0.0", 5500) + base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = [list_str[i], list_str[i]] + instance["text1"] = [str_a[i], str_a[i]] + instance["text2"] = [str_b[i], str_b[i]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) assert "json array, string or bytes type only support one item" in str(result["error_msg"]) @serving_test def test_restful_str_invalid_array_input_failed(): - 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_restful_server("0.0.0.0", 5500) + base = start_str_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) - list_str = ["ABC", "DEF", "HIJ"] + instances = [{}, {}, {}] + str_a = ["ABC", "DEF", "HIJ"] + str_b = ["123", "456", "789"] for i, instance in enumerate(instances): - instance["label"] = [list_str[i], [list_str[i]]] + instance["text1"] = [str_a[i], [str_a[i]]] + instance["text2"] = [str_b[i], [str_b[i]]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "str_concat", instances) assert "json array, string or bytes type only support one item" in str(result["error_msg"]) @serving_test def test_restful_bool_scalar_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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): instance["bool_val"] = (i % 2 == 0) - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) result = result["instances"] assert not result[0]["value"] assert result[1]["value"] @@ -274,29 +136,13 @@ def add_cast(x1, x2, bool_val): @serving_test def test_restful_bool_1d_array_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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): instance["bool_val"] = [(i % 2 == 0)] * (i + 1) - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) result = result["instances"] assert result[0]["value"] == [False] assert result[1]["value"] == [True, True] @@ -305,31 +151,15 @@ def add_cast(x1, x2, bool_val): @serving_test def test_restful_bool_2d_array_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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = (i % 2 == 0) val = [[val] * (i + 1)] * (i + 1) instance["bool_val"] = val - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) result = result["instances"] assert result[0]["value"] == [[False]] assert result[1]["value"] == [[True, True], [True, True]] @@ -338,142 +168,62 @@ def add_cast(x1, x2, bool_val): @serving_test def test_restful_bool_invalid_array_array_scalar_mix_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[False], True] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "invalid json array: json type is not array" in result['error_msg'] @serving_test def test_restful_bool_invalid_array2_scalar_array_mix_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [False, [True]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, data should be number, bool, string or bytes" in result['error_msg'] @serving_test def test_restful_bool_invalid_array3_array_dim_not_match_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[False, True], [True]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "invalid json array: json size is 1, the dim 1 expected to be 2" in result['error_msg'] @serving_test def test_restful_bool_invalid_array4_array_dim_not_match_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[[False, True]], [[True]]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "invalid json array: json size is 1, the dim 2 expected to be 2" in result['error_msg'] @serving_test def test_restful_int_scalar_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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = i * 2 instance["int_val"] = val - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == 1 assert result[1]["value"] == 3 @@ -482,25 +232,9 @@ def add_cast(x1, x2, int_val): @serving_test def test_restful_int_empty_input_output_failed(): - 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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): if i % 2 == 0: val = [] @@ -508,37 +242,21 @@ def add_cast(x1, x2, int_val): val = [i * 2] * (i + 1) instance["int_val"] = val - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) assert "json array, shape is empty" in result["error_msg"] @serving_test def test_restful_int_1d_array_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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = i * 2 val = [val] * (i + 1) instance["int_val"] = val - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == [1] assert result[1]["value"] == [3, 3] @@ -547,32 +265,15 @@ def add_cast(x1, x2, int_val): @serving_test def test_restful_int_2d_array_input_output_success(): - base = ServingTestBase() - servable_content = servable_config_import - servable_content += servable_config_declare_servable - servable_content += r""" -def postprocess(y, int_val): - print("-----------------------int val ", int_val, int_val + 1) - 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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = i * 2 val = [[val] * (i + 1)] * (i + 1) instance["int_val"] = val - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "int_plus_1", instances) result = result["instances"] assert result[0]["value"] == [[1]] assert result[1]["value"] == [[3, 3], [3, 3]] @@ -581,460 +282,204 @@ def add_cast(x1, x2, int_val): @serving_test def test_restful_float_scalar_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, 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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = i * 2.2 instance["float_val"] = val - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) result = result["instances"] - assert result[0]["value"] == 1 - assert abs(result[1]["value"] - (2.2 + 1)) < 0.001 - assert abs(result[2]["value"] - (4.4 + 1)) < 0.001 + compare_float_value(result[0]["value"], 1.0) + compare_float_value(result[1]["value"], 2.2 + 1) + compare_float_value(result[2]["value"], 4.4 + 1) @serving_test def test_restful_float_1d_array_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, 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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = [i * 2.2] * (i + 1) instance["float_val"] = val - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) result = result["instances"] - compare_float_value(result[0]["value"], [1]) + compare_float_value(result[0]["value"], [1.0]) compare_float_value(result[1]["value"], [3.2, 3.2]) compare_float_value(result[2]["value"], [5.4, 5.4, 5.4]) @serving_test def test_restful_float_2d_array_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, 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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for i, instance in enumerate(instances): val = i * 2.2 val = [[val] * (i + 1)] * (i + 1) instance["float_val"] = val - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "float_plus_1", instances) result = result["instances"] - compare_float_value(result[0]["value"], [[1]]) + compare_float_value(result[0]["value"], [[1.0]]) compare_float_value(result[1]["value"], [[3.2, 3.2], [3.2, 3.2]]) compare_float_value(result[2]["value"], [[5.4, 5.4, 5.4], [5.4, 5.4, 5.4], [5.4, 5.4, 5.4]]) @serving_test def test_restful_mix_bool_int_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[False, True], [1, 1]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, elements type is not equal" in result['error_msg'] @serving_test def test_restful_mix_bool_int2_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[False, 1]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, elements type is not equal" in result['error_msg'] @serving_test def test_restful_mix_float_int_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[1.1, 1.2], [1, 1]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, elements type is not equal" in result['error_msg'] @serving_test def test_restful_mix_float_int2_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[1.1, 1]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, elements type is not equal" in result['error_msg'] @serving_test def test_restful_mix_int_float_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[1, 1], [1.1, 1.2]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, elements type is not equal" in result['error_msg'] @serving_test def test_restful_mix_int_float2_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[1, 1.2]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, elements type is not equal" in result['error_msg'] @serving_test def test_restful_mix_str_float_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [["a", "b"], [1.1, 1.2]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "string or bytes type only support one item" in result['error_msg'] @serving_test def test_restful_mix_str_float2_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [["a", 1.2]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "string or bytes type only support one item" in result['error_msg'] @serving_test def test_restful_mix_float_str_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[1.1, 1.2], ["a", "b"]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, elements type is not equal" in result['error_msg'] @serving_test def test_restful_mix_float_str2_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[1.1, "b"]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, elements type is not equal" in result['error_msg'] @serving_test def test_restful_mix_bytes_str_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[{"b64": ""}, {"b64": ""}], ["a", "b"]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "string or bytes type only support one item" in result['error_msg'] @serving_test def test_restful_mix_bytes_bool_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[{"b64": ""}, {"b64": ""}], [True, False]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "string or bytes type only support one item" in result['error_msg'] @serving_test def test_restful_mix_bool_bytes_input_failed(): - 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), ~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_restful_server("0.0.0.0", 5500) + base = start_bool_int_float_restful_server() # Client - instance_count = 3 - instances, _ = create_multi_instances_fp32(instance_count) + instances = [{}, {}, {}] for instance in instances: instance["bool_val"] = [[True, False], [{"b64": ""}, {"b64": ""}]] - result = post_restful("localhost", 5500, base.servable_name, "add_cast", instances) + result = post_restful("localhost", 5500, base.servable_name, "bool_not", instances) assert "json array, data should be number, bool, string or bytes" in result['error_msg'] diff --git a/tests/ut/python/tests/test_restful_request.py b/tests/ut/python/tests/test_restful_request.py index 114a8af..dff36a9 100644 --- a/tests/ut/python/tests/test_restful_request.py +++ b/tests/ut/python/tests/test_restful_request.py @@ -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"]) diff --git a/tests/ut/python/tests/test_start_servable_config.py b/tests/ut/python/tests/test_start_servable_config.py index df24d14..981af96 100644 --- a/tests/ut/python/tests/test_start_servable_config.py +++ b/tests/ut/python/tests/test_start_servable_config.py @@ -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""" diff --git a/tests/ut/python/tests/test_start_worker.py b/tests/ut/python/tests/test_start_worker.py index b964a95..34ea3bc 100644 --- a/tests/ut/python/tests/test_start_worker.py +++ b/tests/ut/python/tests/test_start_worker.py @@ -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