You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

servable_config.py 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """add model servable config"""
  16. from mindspore_serving.worker import register
  17. import numpy as np
  18. # define preprocess pipeline, the function arg is multi instances, every instance is tuple of inputs
  19. # this example has one input and one output
  20. def add_trans_datatype(instances):
  21. """preprocess python implement"""
  22. for instance in instances:
  23. x1 = instance[0]
  24. x2 = instance[1]
  25. yield x1.astype(np.float32), x2.astype(np.float32)
  26. # when with_batch_dim set to False, only support 2x2 add
  27. # when with_batch_dim set to True(default), support Nx2 add, while N is view as batch
  28. # float32 inputs/outputs
  29. register.declare_servable(servable_file="tensor_add.mindir", model_format="MindIR", with_batch_dim=False)
  30. # register add_common method in add
  31. @register.register_method(output_names=["y"])
  32. def add_common(x1, x2): # only support float32 inputs
  33. """method add_common data flow definition, only call model servable"""
  34. y = register.call_servable(x1, x2)
  35. return y
  36. # register add_cast method in add
  37. @register.register_method(output_names=["y"])
  38. def add_cast(x1, x2):
  39. """method add_cast data flow definition, only call preprocess and model servable"""
  40. x1, x2 = register.call_preprocess(add_trans_datatype, x1, x2) # cast input to float32
  41. y = register.call_servable(x1, x2)
  42. return y

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.