main.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # time: 2023/3/2 10:28
  4. # file: config.py
  5. # author: David
  6. # company: shenyang JY
  7. """
  8. 模型调参及系统功能配置
  9. """
  10. from tqdm import tqdm
  11. import pandas as pd
  12. from pathlib import Path
  13. from copy import deepcopy
  14. from concurrent.futures import ProcessPoolExecutor
  15. from app.common.config import parser, logger
  16. from app.model.resource_manager import ResourceController
  17. from app.model.task_worker import station_task, region_task
  18. """"
  19. 调用思路
  20. xxxx 1. 从入口参数中获取IN OUT文件位置 xxxx
  21. 2. 按照训练和预测加载和解析数据
  22. 3. 对数据进行预处理
  23. 4. 执行训练,保存模型,输出状态
  24. 5. 执行预测,输出结果,输出状态
  25. """
  26. """
  27. 训练任务
  28. 1.将一个省份下的所有场站加入队列
  29. 2.队列中的每个场站是一个子任务,还有最终的区域级子任务
  30. """
  31. def main():
  32. # ---------------------------- 解析参数 ----------------------------
  33. # 解析参数,将固定参数和任务参数合并
  34. opt = parser.parse_args_and_yaml()
  35. config = opt.__dict__
  36. # 打印参数
  37. logger.info(f"输入文件目录: {opt.input_file}")
  38. # ---------------------------- 配置计算资源和任务 ----------------------------
  39. # 初始化资源管理器
  40. rc = ResourceController(
  41. max_workers=opt.system['max_workers'],
  42. gpu_list=opt.system['gpu_devices']
  43. )
  44. # 生成任务列表
  45. all_stations = [str(child) for child in Path(opt.input_file).iterdir() if child.is_dir()]
  46. # task_func = partial(station_task, config=config)
  47. # ---------------------------- 监控任务,进度跟踪 ----------------------------
  48. # 场站级功率预测训练
  49. completed = 0
  50. with tqdm(total=len(all_stations)) as pbar:
  51. with ProcessPoolExecutor(max_workers=rc.cpu_cores) as executor:
  52. futures = []
  53. for sid in all_stations:
  54. # 动态分配GPU
  55. gpu_id = rc.get_gpu()
  56. task_config = deepcopy(config)
  57. task_config['gpu_assignment'] = gpu_id
  58. task_config['station_id'] = sid
  59. # 提交任务
  60. future = executor.submit(station_task, task_config)
  61. future.add_done_callback(
  62. lambda _: rc.release_gpu(task_config['gpu_assignment']))
  63. futures.append(future)
  64. # 处理完成情况
  65. for future in futures:
  66. result = future._result
  67. if result == 'success':
  68. completed += 1
  69. pbar.update(1)
  70. pbar.set_postfix_str(f"Completed: {completed}/{len(all_stations)}")
  71. print(f"Final result: {completed} stations trained successfully")
  72. # 区域级功率预测训练
  73. region_task(config)
  74. if __name__ == "__main__":
  75. main()