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.

logfile_loader.py 6.8 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding: UTF-8 -*-
  2. """
  3. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =============================================================
  14. """
  15. import threading
  16. import time
  17. from io import BytesIO
  18. from pathlib import Path
  19. from tbparser import SummaryReader
  20. from tbparser import Projector_Reader
  21. from utils.cache_io import CacheIO
  22. from utils.path_utils import path_parser
  23. from utils.redis_utils import RedisInstance
  24. import pickle
  25. class Trace_Thread(threading.Thread):
  26. def __init__(self, runname, filename, current_size, uid, cache_path):
  27. threading.Thread.__init__(self, name=filename.name)
  28. self.uid = uid
  29. self.runname = runname
  30. self.cache_path = cache_path
  31. self.filename = filename
  32. self.current_size = current_size
  33. self.r = RedisInstance
  34. # 该日志中是否有超参数
  35. self.has_hparams = False
  36. self.first_write = False
  37. self.metrics = []
  38. # 是否完成初始化
  39. self._finish_init = 0
  40. self.redis_tag = []
  41. def run(self):
  42. print('监听文件 %s' % self.filename)
  43. self.trace(self.current_size)
  44. def trace(self, current_size):
  45. filename = Path(self.filename)
  46. if filename.suffix == ".json":
  47. self.load_model_file(filename)
  48. self.finish_init = 1
  49. return
  50. f = open(filename, "rb")
  51. # for event file
  52. if "event" in filename.name:
  53. _io = BytesIO(
  54. f.read(current_size)
  55. )
  56. self.load_event_file(_io)
  57. # 设置初始化完成标志
  58. self.finish_init = 1
  59. while True:
  60. rest = f.read()
  61. if not rest:
  62. time.sleep(2)
  63. continue
  64. _io = BytesIO(rest)
  65. self.load_event_file(_io)
  66. # for projector file
  67. elif "projector" in filename.name:
  68. self.load_projector_file(f)
  69. # 设置初始化完成标志
  70. self.finish_init = 1
  71. @property
  72. def finish_init(self):
  73. return self._finish_init
  74. # 设置标志
  75. @finish_init.setter
  76. def finish_init(self, is_finish):
  77. self.r.set("{}_{}_{}_is_finish".format(self.uid, self.runname,
  78. self.filename.name), 1)
  79. print(self.name + " is finish")
  80. self._finish_init = is_finish
  81. def set_redis_key(self, type, tag, file_path):
  82. _key = self.uid + '_' + self.runname + '_' + type + '_' + tag
  83. if _key in self.redis_tag:
  84. pass
  85. else:
  86. self.r.set(_key, str(file_path))
  87. self.redis_tag.append(_key)
  88. def set_cache(self, file_name, data):
  89. if not file_name.parent.exists():
  90. file_name.parent.mkdir(parents=True, exist_ok=True)
  91. with open(file_name, 'ab') as f:
  92. pickle.dump(data, f)
  93. f.close()
  94. def load_event_file(self, fileIO):
  95. reader = SummaryReader(fileIO, types=[
  96. 'scalar',
  97. 'graph',
  98. 'hist',
  99. 'text',
  100. 'image',
  101. 'audio',
  102. 'hparams'
  103. ])
  104. for items in reader:
  105. if items.type == "graph":
  106. file_path = path_parser(self.cache_path, self.runname,
  107. items.type, tag='c_graph')
  108. CacheIO(file_path).set_cache(data=items.value)
  109. self.set_redis_key(items.type, tag='c_graph',
  110. file_path=file_path)
  111. continue
  112. elif items.type == "hparams":
  113. file_path = path_parser(self.cache_path, self.runname,
  114. type='hyperparm',
  115. tag='hparams')
  116. self.set_cache(file_name=file_path, data=items.value)
  117. self.set_redis_key(type='hyperparm',
  118. tag='hparams',
  119. file_path=file_path)
  120. continue
  121. item_data = {
  122. 'step': items.step,
  123. 'wall_time': items.wall_time,
  124. 'value': items.value,
  125. 'type': items.type
  126. }
  127. file_path = path_parser(self.cache_path, self.runname,
  128. type=items.type,
  129. tag=items.tag)
  130. CacheIO(file_path).set_cache(data=item_data)
  131. self.set_redis_key(type=items.type, tag=items.tag,
  132. file_path=file_path)
  133. def load_projector_file(self, fileIO):
  134. p_reader = Projector_Reader(fileIO).read()
  135. for items in p_reader.projectors:
  136. item_data = {
  137. 'step': items.step,
  138. 'wall_time': items.wall_time,
  139. 'value': items.value.reshape(items.value.shape[0], -1)
  140. if items.value.ndim > 2 else items.value,
  141. 'label': items.label,
  142. }
  143. file_path = path_parser(self.cache_path, self.runname,
  144. type=p_reader.metadata.type,
  145. tag=items.tag)
  146. CacheIO(file_path).set_cache(data=item_data)
  147. self.set_redis_key(type=p_reader.metadata.type, tag=items.tag,
  148. file_path=file_path)
  149. if p_reader.sample:
  150. file_path = path_parser(self.cache_path, self.runname,
  151. type="embedding",
  152. tag="sample_" + items.tag)
  153. CacheIO(file_path).set_cache(data=p_reader.sample)
  154. self.set_redis_key(type="embedding", tag="sample_" + items.tag,
  155. file_path=file_path)
  156. def load_model_file(self, file):
  157. with open(file, "r") as f:
  158. _content = f.read()
  159. file_path = path_parser(self.cache_path, self.runname,
  160. type="graph",
  161. tag="s_graph")
  162. CacheIO(file_path).set_cache(data=_content)
  163. self.set_redis_key(type="graph", tag="s_graph",
  164. file_path=file_path)

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

Contributors (1)