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.

scalars_processor.py 4.2 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright 2019 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. """Scalar Processor APIs."""
  16. from urllib.parse import unquote
  17. from mindinsight.utils.exceptions import ParamValueError, UrlDecodeError
  18. from mindinsight.datavisual.common.log import logger
  19. from mindinsight.datavisual.utils.tools import if_nan_inf_to_none
  20. from mindinsight.datavisual.common.exceptions import ScalarNotExistError
  21. from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
  22. from mindinsight.datavisual.common.validation import Validation
  23. from mindinsight.datavisual.processors.base_processor import BaseProcessor
  24. class ScalarsProcessor(BaseProcessor):
  25. """Scalar Processor."""
  26. def get_metadata_list(self, train_id, tag):
  27. """
  28. Builds a JSON-serializable object with information about scalars.
  29. Args:
  30. train_id (str): The ID of the events data.
  31. tag (str): The name of the tag the scalars all belonging to.
  32. Returns:
  33. list[dict], a list of dictionaries containing the `wall_time`, `step`, `value` for each scalar.
  34. """
  35. Validation.check_param_empty(train_id=train_id, tag=tag)
  36. job_response = []
  37. try:
  38. tensors = self._data_manager.list_tensors(train_id, tag)
  39. except ParamValueError as ex:
  40. raise ScalarNotExistError(ex.message)
  41. for tensor in tensors:
  42. job_response.append({
  43. 'wall_time': tensor.wall_time,
  44. 'step': tensor.step,
  45. 'value': tensor.value})
  46. return dict(metadatas=job_response)
  47. def get_scalars(self, train_ids, tags):
  48. """
  49. Get scalar data for given train_ids and tags.
  50. Args:
  51. train_ids (list): Specify list of train job ID.
  52. tags (list): Specify list of tags.
  53. Returns:
  54. list[dict], a list of dictionaries containing the `wall_time`, `step`, `value` for each scalar.
  55. """
  56. for index, train_id in enumerate(train_ids):
  57. try:
  58. train_id = unquote(train_id, errors='strict')
  59. except UnicodeDecodeError:
  60. raise UrlDecodeError('Unquote train id error with strict mode')
  61. else:
  62. train_ids[index] = train_id
  63. scalars = []
  64. for train_id in train_ids:
  65. scalars += self._get_train_scalars(train_id, tags)
  66. return scalars
  67. def _get_train_scalars(self, train_id, tags):
  68. """
  69. Get scalar data for given train_id and tags.
  70. Args:
  71. train_id (str): Specify train job ID.
  72. tags (list): Specify list of tags.
  73. Returns:
  74. list[dict], a list of dictionaries containing the `wall_time`, `step`, `value` for each scalar.
  75. """
  76. scalars = []
  77. for tag in tags:
  78. try:
  79. tensors = self._data_manager.list_tensors(train_id, tag)
  80. except ParamValueError:
  81. continue
  82. except TrainJobNotExistError:
  83. logger.warning('Can not find the given train job in cache.')
  84. return []
  85. scalar = {
  86. 'train_id': train_id,
  87. 'tag': tag,
  88. 'values': [],
  89. }
  90. for tensor in tensors:
  91. scalar['values'].append({
  92. 'wall_time': tensor.wall_time,
  93. 'step': tensor.step,
  94. 'value': if_nan_inf_to_none('scalar_value', tensor.value),
  95. })
  96. scalars.append(scalar)
  97. return scalars