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.

events_reader.py 4.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- coding: UTF-8 -*-
  2. # MIT License
  3. #
  4. # Copyright (c) 2019 Vadim Velicodnii
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining
  7. # a copy of this software and associated documentation files
  8. # (the "Software"), to deal in the Software without restriction,
  9. # including without limitation the rights to use, copy, modify, merge,
  10. # publish, distribute, sublicense, and/or sell copies of the Software,
  11. # and to permit persons to whom the Software is furnished to do so,
  12. # subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included
  15. # in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. import io
  25. from typing import Iterable
  26. from oneflow.customized.utils.event_pb2 import Event
  27. from crc32c import crc32c
  28. import struct
  29. class EventsFileReader(Iterable):
  30. """
  31. An iterator over a Tensorboard events file
  32. """
  33. def __init__(self, file_block: io.BytesIO):
  34. """
  35. Initialize an iterator over an events file
  36. :param file_block: An opened file-like object.
  37. """
  38. self.fd = EventFileIO(file_block)
  39. def __iter__(self) -> Event:
  40. """
  41. Iterates over events in the current events file
  42. :return: An Event object
  43. :except: NotImplementedError if the stream is in non-blocking mode.
  44. :except: EventReadingError on reading error.
  45. """
  46. while True:
  47. event_raw = self.fd.read()
  48. if event_raw is None:
  49. break
  50. else:
  51. event = Event()
  52. event.ParseFromString(event_raw)
  53. yield event
  54. def _u32(x):
  55. return x & 0xffffffff
  56. def _masked_crc32c(data):
  57. x = _u32(crc32c(data))
  58. return _u32(((x >> 15) | _u32(x << 17)) + 0xa282ead8)
  59. class EventFileIO(object):
  60. def __init__(self, fileIo):
  61. self.fd = fileIo #open(path, 'wb')
  62. def _read(self, size: int):
  63. """
  64. Read exactly next `size` bytes from the current stream.
  65. :param size: A size in bytes to be read.
  66. :return: A `bytes` object with read data or `None` on EOF.
  67. :except: NotImplementedError if the stream is in non-blocking mode.
  68. :except: EventReadingError on reading error.
  69. """
  70. data = self.fd.read(size)
  71. if data is None:
  72. raise NotImplementedError(
  73. 'Reading of a stream in non-blocking mode'
  74. )
  75. if 0 < len(data) < size:
  76. raise Exception(
  77. 'File read error, the size of read data is less than requested size'
  78. )
  79. if len(data) == 0:
  80. return None
  81. return data
  82. def _read_and_check(self, size: int, checksum_size: int):
  83. """
  84. Read and check data described by a format string.
  85. :param size: A size in bytes to be read.
  86. :return: A decoded number.
  87. :except: NotImplementedError if the stream is in non-blocking mode.
  88. :except: EventReadingError on reading error.
  89. """
  90. data = self._read(size)
  91. if not data:
  92. return None
  93. checksum = struct.unpack('I', self.fd.read(checksum_size))[0]
  94. checksum_computed = _masked_crc32c(data)
  95. if checksum != checksum_computed:
  96. raise Exception(
  97. 'Invalid checksum. {checksum} != {crc32}'.format(
  98. checksum=checksum, crc32=checksum_computed)
  99. )
  100. return data
  101. def read(self):
  102. header_size = struct.calcsize('Q')
  103. checksum_size = struct.calcsize('I')
  104. header = self._read_and_check(header_size, checksum_size)
  105. if header is None:
  106. return None
  107. data_size = struct.unpack('Q', header)[0]
  108. data = self._read_and_check(data_size, checksum_size)
  109. return data
  110. def close(self):
  111. self.fd.close()

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能