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 4.2 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: UTF-8 -*-
  2. """
  3. Copyright 2021 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 time
  16. import threading
  17. from pathlib import Path
  18. from tbparser import SummaryReader
  19. from utils.cache_io import CacheIO
  20. from utils.logfile_utils import path_parser
  21. from utils.redis_utils import RedisInstance
  22. from tbparser.event_parser import filter_graph
  23. class Trace_Thread(threading.Thread):
  24. def __init__(self, uid, runname, filename, cache_path, logs=None,
  25. is_init=False, daemon=True):
  26. threading.Thread.__init__(self, name=filename.name)
  27. self.daemon = daemon
  28. self.uid = uid
  29. self.runname = runname
  30. self.cache_path = cache_path
  31. self.filename = filename
  32. self.logs = logs
  33. self.is_init = is_init
  34. self.redis_tags = []
  35. def set_redis_key(self, type, tag, file_path):
  36. _key = self.uid + '_' + self.runname + '_' + type + '_' + tag
  37. if _key not in self.redis_tags:
  38. RedisInstance.set(_key, str(file_path))
  39. self.redis_tags.append(_key)
  40. def run(self):
  41. print('监听文件 %s' % self.filename)
  42. self.trace()
  43. def trace(self):
  44. filename = Path(self.filename)
  45. if filename.suffix == ".json":
  46. with open(filename, "r") as f:
  47. # 结构图内容
  48. _cg_content = f.read()
  49. _sg_content = filter_graph(_cg_content)
  50. sg_file_path = path_parser(self.cache_path, self.runname,
  51. category="graph", tag="s_graph")
  52. CacheIO(sg_file_path).set_cache(data=_sg_content)
  53. self.set_redis_key(type='graph', tag='s_graph', file_path=sg_file_path)
  54. # 已完成graph文件解析,将完成标志放入队列
  55. if self.logs:
  56. self.logs.remove(self.filename)
  57. return
  58. # for event file
  59. if "event" in filename.name:
  60. fd = open(filename, "rb")
  61. while True:
  62. reader = SummaryReader(fd)
  63. for items in reader:
  64. if items['type'] == "graph":
  65. file_path = path_parser(self.cache_path, self.runname,
  66. items['type'], tag='c_graph')
  67. CacheIO(file_path).set_cache(data=items['value'])
  68. self.set_redis_key(type='graph', tag='c_graph', file_path=file_path)
  69. elif items['type'] == "hparams":
  70. file_path = path_parser(self.cache_path, self.runname,
  71. 'hyperparm', tag='hparams')
  72. CacheIO(file_path, mod='ab').set_cache(data=items['value'])
  73. self.set_redis_key(type='hyperparm', tag='hparams', file_path=file_path)
  74. else: # scalar, image, histogram, embedding
  75. file_path = path_parser(self.cache_path, self.runname,
  76. items['type'], tag=items['tag'])
  77. CacheIO(file_path).set_cache(data=items)
  78. self.set_redis_key(type=items['type'], tag=items['tag'], file_path=file_path)
  79. # 已完成event文件解析,将完成标志放入队列
  80. if self.is_init and self.logs:
  81. self.logs.remove(self.filename)
  82. self.is_init = False
  83. # 文件读到末尾后睡眠几秒,然后继续解析文件,实现动态监听
  84. time.sleep(1)

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