(self)
| 69 | return getattr(self.diffusers_pipeline, name) |
| 70 | |
| 71 | def load_diffusion_model(self): |
| 72 | dtype = self.model_config['dtype'] |
| 73 | transformer_dtype = self.model_config.get('transformer_dtype', dtype) |
| 74 | |
| 75 | llama3_path = self.model_config['llama3_path'] |
| 76 | if self.model_config.get('llama3_4bit', False): |
| 77 | quantization_config = transformers.BitsAndBytesConfig( |
| 78 | load_in_4bit=True, |
| 79 | bnb_4bit_quant_type='nf4', |
| 80 | bnb_4bit_compute_dtype=dtype, |
| 81 | ) |
| 82 | else: |
| 83 | quantization_config = None |
| 84 | text_encoder_4 = LlamaForCausalLM.from_pretrained( |
| 85 | llama3_path, |
| 86 | output_hidden_states=True, |
| 87 | quantization_config=quantization_config, |
| 88 | torch_dtype=dtype, |
| 89 | ) |
| 90 | for p in text_encoder_4.parameters(): |
| 91 | p.requires_grad_(False) |
| 92 | p.data = p.data.to('cpu') |
| 93 | empty_cuda_cache() |
| 94 | self.diffusers_pipeline.text_encoder_4 = text_encoder_4 |
| 95 | |
| 96 | if transformer_dtype == 'nf4': |
| 97 | quantization_config = diffusers.BitsAndBytesConfig( |
| 98 | load_in_4bit=True, |
| 99 | bnb_4bit_quant_type='nf4', |
| 100 | bnb_4bit_compute_dtype=dtype, |
| 101 | llm_int8_skip_modules=KEEP_IN_HIGH_PRECISION, |
| 102 | ) |
| 103 | else: |
| 104 | quantization_config = None |
| 105 | self.diffusers_pipeline.transformer = HiDreamImageTransformer2DModel.from_pretrained( |
| 106 | self.model_config['diffusers_path'], |
| 107 | subfolder='transformer', |
| 108 | torch_dtype=dtype, |
| 109 | quantization_config=quantization_config, |
| 110 | ) |
| 111 | if transformer_dtype != 'nf4': |
| 112 | for name, p in self.transformer.named_parameters(): |
| 113 | if not (any(x in name for x in KEEP_IN_HIGH_PRECISION)): |
| 114 | p.data = p.data.to(transformer_dtype) |
| 115 | |
| 116 | self.transformer.train() |
| 117 | for name, p in self.transformer.named_parameters(): |
| 118 | p.original_name = name |
| 119 | |
| 120 | # Critically important! Official code saves MoE aux losses in global state if alpha > 0. Without special handling of |
| 121 | # this, it causes massive memory leak during backward pass and immediately OOMs you. |
| 122 | for module in self.transformer.modules(): |
| 123 | if isinstance(module, MoEGate): |
| 124 | module.alpha = 0 |
| 125 | |
| 126 | def get_vae(self): |
| 127 | return self.vae |
nothing calls this directly
no test coverage detected