Views
No views yet
images/more_examples folder and the captions.csv file.in nezha style. at the end of the prompt to trigger the image generation.in nezha style. (must be at the end of the prompt)1git clone https://github.com/Tencent-Hunyuan/HunyuanImage-3.0.git
2cd HunyuanImage-3.0/
3
4hf download tencent/HunyuanImage-3.0 --local-dir ./HunyuanImage-3hf download pixosg/HunyuanImage-3.0-Nezha-Style-Adapter --local-dir ./hunyuanimage-3-nezha-style-adapter1from peft import PeftModel
2from hunyuan_image_3.hunyuan import HunyuanImage3ForCausalMM
3import torch
4
5model_id = "./HunyuanImage-3"
6adapter_model_path = "./hunyuanimage-3-nezha-style-adapter/nezha-standard"
7
8kwargs = dict(
9 attn_implementation="sdpa", # Use "flash_attention_2" if available
10 trust_remote_code=True,
11 dtype=torch.bfloat16,
12 device_map="auto",
13 moe_impl="eager",
14 moe_drop_tokens=False,
15)
16
17model = HunyuanImage3ForCausalMM.from_pretrained(model_id, **kwargs)
18model.load_tokenizer(model_id)
19
20# Option 1
21model.load_adapter(adapter_model_path)
22
23# Option 2
24model.get_input_embeddings = lambda: model.model.wte
25model.set_input_embeddings = lambda value: setattr(model.model, 'wte', value)
26model = PeftModel.from_pretrained(model, adapter_model_path, trust_remote_code=True)
27
28# Generate image
29prompt = "A young girl flying a kite on a windy hill in nezha style."
30image = model.generate_image(prompt=prompt, stream=True)
31image.save("image.png")1import torch
2from transformers import BitsAndBytesConfig
3from peft import PeftModel
4from hunyuan_image_3.hunyuan import HunyuanImage3ForCausalMM
5
6# ------------------------------------------------------------
7# Patch (apply BEFORE model loading)
8# Keep attention mask on the same device as the input tensor.
9# ------------------------------------------------------------
10_orig_prepare = HunyuanImage3ForCausalMM._prepare_attention_mask_for_generation
11
12def _prepare_attention_mask_for_generation_patched(self, inputs_tensor, generation_config, model_kwargs):
13 attn_mask = _orig_prepare(self, inputs_tensor, generation_config, model_kwargs)
14 if attn_mask is not None and attn_mask.device != inputs_tensor.device:
15 attn_mask = attn_mask.to(device=inputs_tensor.device)
16 return attn_mask
17
18HunyuanImage3ForCausalMM._prepare_attention_mask_for_generation = _prepare_attention_mask_for_generation_patched
19# ------------------------------------------------------------
20
21skip_modules = [
22 "vae",
23 "vision_model",
24 "vision_aligner",
25 "patch_embed",
26 "timestep_emb",
27 "time_embed",
28 "time_embed_2",
29 "final_layer",
30 "lm_head",
31]
32
33quant_config = BitsAndBytesConfig(
34 load_in_8bit=True,
35 llm_int8_threshold=6.0,
36 llm_int8_skip_modules=skip_modules,
37 llm_int8_enable_fp32_cpu_offload=True,
38)
39
40model_id = "./HunyuanImage-3"
41adapter_model_path = "./hunyuanimage-3-nezha-style-adapter/nezha-standard"
42
43kwargs = dict(
44 attn_implementation="sdpa", # Use "flash_attention_2" if available
45 trust_remote_code=True,
46 quantization_config=quant_config,
47 dtype="auto",
48 device_map="auto",
49 moe_impl="eager",
50 moe_drop_tokens=False,
51)
52
53model = HunyuanImage3ForCausalMM.from_pretrained(model_id, **kwargs)
54model.load_tokenizer(model_id)
55
56# Apply LoRA adapter
57model.get_input_embeddings = lambda: model.model.wte
58model.set_input_embeddings = lambda value: setattr(model.model, 'wte', value)
59model = PeftModel.from_pretrained(model, adapter_model_path, trust_remote_code=True)
60
61# Generate image
62prompt = "A young girl flying a kite on a windy hill in nezha style."
63image = model.generate_image(prompt=prompt, stream=True)
64image.save("image.png")