lowvram.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import torch
  2. from modules import devices
  3. module_in_gpu = None
  4. cpu = torch.device("cpu")
  5. def send_everything_to_cpu():
  6. global module_in_gpu
  7. if module_in_gpu is not None:
  8. module_in_gpu.to(cpu)
  9. module_in_gpu = None
  10. def setup_for_low_vram(sd_model, use_medvram):
  11. sd_model.lowvram = True
  12. parents = {}
  13. def send_me_to_gpu(module, _):
  14. """send this module to GPU; send whatever tracked module was previous in GPU to CPU;
  15. we add this as forward_pre_hook to a lot of modules and this way all but one of them will
  16. be in CPU
  17. """
  18. global module_in_gpu
  19. module = parents.get(module, module)
  20. if module_in_gpu == module:
  21. return
  22. if module_in_gpu is not None:
  23. module_in_gpu.to(cpu)
  24. module.to(devices.device)
  25. module_in_gpu = module
  26. # see below for register_forward_pre_hook;
  27. # first_stage_model does not use forward(), it uses encode/decode, so register_forward_pre_hook is
  28. # useless here, and we just replace those methods
  29. first_stage_model = sd_model.first_stage_model
  30. first_stage_model_encode = sd_model.first_stage_model.encode
  31. first_stage_model_decode = sd_model.first_stage_model.decode
  32. def first_stage_model_encode_wrap(x):
  33. send_me_to_gpu(first_stage_model, None)
  34. return first_stage_model_encode(x)
  35. def first_stage_model_decode_wrap(z):
  36. send_me_to_gpu(first_stage_model, None)
  37. return first_stage_model_decode(z)
  38. # for SD1, cond_stage_model is CLIP and its NN is in the tranformer frield, but for SD2, it's open clip, and it's in model field
  39. if hasattr(sd_model.cond_stage_model, 'model'):
  40. sd_model.cond_stage_model.transformer = sd_model.cond_stage_model.model
  41. # remove several big modules: cond, first_stage, depth/embedder (if applicable), and unet from the model and then
  42. # send the model to GPU. Then put modules back. the modules will be in CPU.
  43. stored = sd_model.cond_stage_model.transformer, sd_model.first_stage_model, getattr(sd_model, 'depth_model', None), getattr(sd_model, 'embedder', None), sd_model.model
  44. sd_model.cond_stage_model.transformer, sd_model.first_stage_model, sd_model.depth_model, sd_model.embedder, sd_model.model = None, None, None, None, None
  45. sd_model.to(devices.device)
  46. sd_model.cond_stage_model.transformer, sd_model.first_stage_model, sd_model.depth_model, sd_model.embedder, sd_model.model = stored
  47. # register hooks for those the first three models
  48. sd_model.cond_stage_model.transformer.register_forward_pre_hook(send_me_to_gpu)
  49. sd_model.first_stage_model.register_forward_pre_hook(send_me_to_gpu)
  50. sd_model.first_stage_model.encode = first_stage_model_encode_wrap
  51. sd_model.first_stage_model.decode = first_stage_model_decode_wrap
  52. if sd_model.depth_model:
  53. sd_model.depth_model.register_forward_pre_hook(send_me_to_gpu)
  54. if sd_model.embedder:
  55. sd_model.embedder.register_forward_pre_hook(send_me_to_gpu)
  56. parents[sd_model.cond_stage_model.transformer] = sd_model.cond_stage_model
  57. if hasattr(sd_model.cond_stage_model, 'model'):
  58. sd_model.cond_stage_model.model = sd_model.cond_stage_model.transformer
  59. del sd_model.cond_stage_model.transformer
  60. if use_medvram:
  61. sd_model.model.register_forward_pre_hook(send_me_to_gpu)
  62. else:
  63. diff_model = sd_model.model.diffusion_model
  64. # the third remaining model is still too big for 4 GB, so we also do the same for its submodules
  65. # so that only one of them is in GPU at a time
  66. stored = diff_model.input_blocks, diff_model.middle_block, diff_model.output_blocks, diff_model.time_embed
  67. diff_model.input_blocks, diff_model.middle_block, diff_model.output_blocks, diff_model.time_embed = None, None, None, None
  68. sd_model.model.to(devices.device)
  69. diff_model.input_blocks, diff_model.middle_block, diff_model.output_blocks, diff_model.time_embed = stored
  70. # install hooks for bits of third model
  71. diff_model.time_embed.register_forward_pre_hook(send_me_to_gpu)
  72. for block in diff_model.input_blocks:
  73. block.register_forward_pre_hook(send_me_to_gpu)
  74. diff_model.middle_block.register_forward_pre_hook(send_me_to_gpu)
  75. for block in diff_model.output_blocks:
  76. block.register_forward_pre_hook(send_me_to_gpu)
  77. def is_enabled(sd_model):
  78. return getattr(sd_model, 'lowvram', False)