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.

file_handler.py 2.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. """File handler for lineage summary log."""
  16. import os
  17. class FileHandler:
  18. """
  19. Summary log file handler.
  20. Summary log file handler provides Python APIs to manage file IO, including
  21. read, seek. It is not suitable for very large files.
  22. Args:
  23. file_path (str): File path.
  24. """
  25. def __init__(self, file_path):
  26. self._size = os.path.getsize(file_path)
  27. self._cache = self._read_cache(file_path)
  28. self._offset = 0
  29. @property
  30. def size(self):
  31. """
  32. The size of file.
  33. Returns:
  34. int, the size of file.
  35. """
  36. return self._size
  37. def _read_cache(self, file_path):
  38. """
  39. Read file in cache.
  40. Args:
  41. file_path (str): File path.
  42. Returns:
  43. bytes, the file content.
  44. """
  45. with open(file_path, 'rb') as log_file:
  46. return log_file.read()
  47. def seek(self, offset):
  48. """
  49. Set the new offset of file.
  50. Args:
  51. pos (int): The new offset.
  52. """
  53. self._offset = offset
  54. def tell(self):
  55. """
  56. Tell the current offset.
  57. Returns:
  58. int, the offset.
  59. """
  60. return self._offset
  61. def read(self, size=-1, offset=None):
  62. """
  63. Read bytes from buffer by size.
  64. Args:
  65. size (int): Number of bytes to read. If set -1, read the whole file.
  66. Default: -1.
  67. offset (int): The start offset to read bytes from. Default: None.
  68. Returns:
  69. bytes, the content.
  70. """
  71. if offset is None:
  72. offset = self._offset
  73. new_offset = offset + size
  74. result = self._cache[offset:new_offset]
  75. self._offset = new_offset
  76. return result

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。

Contributors (1)