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.

context.py 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. """Context setting interface"""
  16. from mindspore_serving._mindspore_serving import Context_
  17. class Context:
  18. def __init__(self):
  19. self.context_ = Context_.get_instance()
  20. def set_device_type(self, device_type):
  21. self.context_.set_device_type_str(device_type)
  22. def set_device_id(self, device_id):
  23. self.context_.set_device_id(device_id)
  24. _k_context = None
  25. def _context():
  26. """
  27. Get the global _context, if context is not created, create a new one.
  28. Returns:
  29. _Context, the global context in PyNative mode.
  30. """
  31. global _k_context
  32. if _k_context is None:
  33. _k_context = Context()
  34. return _k_context
  35. def set_context(**kwargs):
  36. """The context setting interf. The acceptable parameters including:
  37. device_type: 'Ascend','Davinci', 'None'. Case ignored.
  38. 'Davinci' and 'Ascend' are the same.
  39. 'None' means depend on mindspore register
  40. device_id: reasonable vice id
  41. """
  42. context = _context()
  43. for (k, w) in kwargs.items():
  44. if k == "device_type":
  45. context.set_device_type(w)
  46. elif k == "device_id":
  47. context.set_device_id(w)
  48. else:
  49. raise RuntimeError(f"Not support context key '{k}'")

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