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 2.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. """Set context of device, including device id and device type, can only set once currently."""
  19. def __init__(self):
  20. self.context_ = Context_.get_instance()
  21. def set_device_type(self, device_type):
  22. """Set device type, now can be 'None'(default) and 'Ascend', 'Davinci'(same as 'Ascend'), case ignored. """
  23. self.context_.set_device_type_str(device_type)
  24. def set_device_id(self, device_id):
  25. """Set device id, default 0"""
  26. self.context_.set_device_id(device_id)
  27. _k_context = None
  28. def _context():
  29. """
  30. Get the global _context, if context is not created, create a new one.
  31. Returns:
  32. _Context, the global context in PyNative mode.
  33. """
  34. global _k_context
  35. if _k_context is None:
  36. _k_context = Context()
  37. return _k_context
  38. def set_context(**kwargs):
  39. """The context setting interface. The acceptable parameters including:
  40. device_type: 'Ascend','Davinci', 'None'. Case ignored.
  41. - Davinci' and 'Ascend' are the same, means Ascend910 or Ascend310.
  42. - 'None' means depend on MindSpore.
  43. device_id: reasonable device id
  44. """
  45. context = _context()
  46. for (k, w) in kwargs.items():
  47. if k == "device_type":
  48. context.set_device_type(w)
  49. elif k == "device_id":
  50. context.set_device_id(w)
  51. else:
  52. 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.