shared_init.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import torch
  3. from modules import shared
  4. from modules.shared import cmd_opts
  5. import sys
  6. sys.setrecursionlimit(1000)
  7. def initialize():
  8. """Initializes fields inside the shared module in a controlled manner.
  9. Should be called early because some other modules you can import mingt need these fields to be already set.
  10. """
  11. os.makedirs(cmd_opts.hypernetwork_dir, exist_ok=True)
  12. from modules import options, shared_options
  13. shared.options_templates = shared_options.options_templates
  14. shared.opts = options.Options(shared_options.options_templates, shared_options.restricted_opts)
  15. if os.path.exists(shared.config_filename):
  16. shared.opts.load(shared.config_filename)
  17. from modules import devices
  18. devices.device, devices.device_interrogate, devices.device_gfpgan, devices.device_esrgan, devices.device_codeformer = \
  19. (devices.cpu if any(y in cmd_opts.use_cpu for y in [x, 'all']) else devices.get_optimal_device() for x in ['sd', 'interrogate', 'gfpgan', 'esrgan', 'codeformer'])
  20. devices.dtype = torch.float32 if cmd_opts.no_half else torch.float16
  21. devices.dtype_vae = torch.float32 if cmd_opts.no_half or cmd_opts.no_half_vae else torch.float16
  22. shared.device = devices.device
  23. shared.weight_load_location = None if cmd_opts.lowram else "cpu"
  24. from modules import shared_state
  25. shared.state = shared_state.State()
  26. from modules import styles
  27. shared.prompt_styles = styles.StyleDatabase(shared.styles_filename)
  28. from modules import interrogate
  29. shared.interrogator = interrogate.InterrogateModels("interrogate")
  30. from modules import shared_total_tqdm
  31. shared.total_tqdm = shared_total_tqdm.TotalTQDM()
  32. from modules import memmon, devices
  33. shared.mem_mon = memmon.MemUsageMonitor("MemMon", devices.device, shared.opts)
  34. shared.mem_mon.start()