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.

_summary_writer.py 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. """Writes events to disk in a logdir."""
  16. import os
  17. import stat
  18. from shutil import disk_usage
  19. from ..._c_expression import EventWriter_
  20. from ._summary_adapter import package_init_event
  21. FREE_DISK_SPACE_TIMES = 32
  22. class BaseWriter:
  23. """BaseWriter to be subclass."""
  24. def __init__(self, filepath, max_file_size=None) -> None:
  25. self._filepath, self._max_file_size = filepath, max_file_size
  26. self._writer: EventWriter_ = None
  27. def init_writer(self):
  28. """Write some metadata etc."""
  29. @property
  30. def writer(self) -> EventWriter_:
  31. """Get the writer."""
  32. if self._writer is not None:
  33. return self._writer
  34. with open(self._filepath, 'w'):
  35. os.chmod(self._filepath, stat.S_IWUSR | stat.S_IRUSR)
  36. self._writer = EventWriter_(self._filepath)
  37. self.init_writer()
  38. return self._writer
  39. def write(self, plugin, data):
  40. """Write data to file."""
  41. # 8: data length
  42. # 4: crc32 of data length
  43. # 4: crc32 of data
  44. metadata_length = 8 + 4 + 4
  45. required_length = len(data) + metadata_length
  46. if self.writer and disk_usage(self._filepath).free < required_length * FREE_DISK_SPACE_TIMES:
  47. raise RuntimeError(f"The disk space may be soon exhausted by the '{self._filepath}'.")
  48. if self._max_file_size is None:
  49. self.writer.Write(data)
  50. elif self._max_file_size >= required_length:
  51. self._max_file_size -= required_length
  52. self.writer.Write(data)
  53. else:
  54. raise RuntimeError(f"'max_file_size' reached: There are {self._max_file_size} bytes remaining, "
  55. f"but the '{self._filepath}' requires to write {required_length} bytes.")
  56. def flush(self):
  57. """Flush the writer."""
  58. if self._writer is not None:
  59. self._writer.Flush()
  60. def close(self):
  61. """Close the writer."""
  62. if self._writer is not None:
  63. self._writer.Shut()
  64. class SummaryWriter(BaseWriter):
  65. """SummaryWriter for write summaries."""
  66. def init_writer(self):
  67. """Write some metadata etc."""
  68. self.write('summary', package_init_event().SerializeToString())
  69. def write(self, plugin, data):
  70. """Write data to file."""
  71. if plugin in ('summary', 'graph'):
  72. super().write(plugin, data)
  73. class LineageWriter(BaseWriter):
  74. """LineageWriter for write lineage."""
  75. def write(self, plugin, data):
  76. """Write data to file."""
  77. if plugin in ('dataset_graph', 'train_lineage', 'eval_lineage', 'custom_lineage_data'):
  78. super().write(plugin, data)