Spaces:
Runtime error
Runtime error
init demo
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .DS_StoreAdded
- ImageConductor_app.pyAdded+586
- app.pyAdded+577
- configs/.DS_StoreAdded
- configs/inference/flow_condition.yamlAdded+18
- configs/inference/image_condition.yamlAdded+18
- configs/inference/inference.yamlAdded+22
- models/.DS_StoreAdded
- modules/__pycache__/attention.cpython-310.pycAdded
- modules/__pycache__/flow_controlnet.cpython-310.pycAdded
- modules/__pycache__/image_controlnet.cpython-310.pycAdded
- modules/__pycache__/motion_module.cpython-310.pycAdded
- modules/__pycache__/resnet.cpython-310.pycAdded
- modules/__pycache__/unet.cpython-310.pycAdded
- modules/__pycache__/unet_blocks.cpython-310.pycAdded
- modules/attention.pyAdded+396
- modules/flow_controlnet.pyAdded+591
- modules/image_controlnet.pyAdded+721
- modules/motion_module.pyAdded+355
- modules/resnet.pyAdded+261
- modules/unet.pyAdded+591
- modules/unet_blocks.pyAdded+866
- peft/__init__.pyAdded+98
- peft/__pycache__/__init__.cpython-310.pycAdded
- peft/__pycache__/auto.cpython-310.pycAdded
- peft/__pycache__/config.cpython-310.pycAdded
- peft/__pycache__/import_utils.cpython-310.pycAdded
- peft/__pycache__/mapping.cpython-310.pycAdded
- peft/__pycache__/mixed_model.cpython-310.pycAdded
- peft/__pycache__/peft_model.cpython-310.pycAdded
- peft/auto.pyAdded+170
- peft/config.pyAdded+270
- peft/helpers.pyAdded+148
- peft/import_utils.pyAdded+89
- peft/mapping.pyAdded+181
- peft/mixed_model.pyAdded+415
- peft/peft_model.pyAdded
- peft/py.typedAdded
- peft/tuners/__init__.pyAdded+35
- peft/tuners/__pycache__/__init__.cpython-310.pycAdded
- peft/tuners/__pycache__/lycoris_utils.cpython-310.pycAdded
- peft/tuners/__pycache__/tuners_utils.cpython-310.pycAdded
- peft/tuners/adalora/__init__.pyAdded+37
- peft/tuners/adalora/__pycache__/__init__.cpython-310.pycAdded
- peft/tuners/adalora/__pycache__/bnb.cpython-310.pycAdded
- peft/tuners/adalora/__pycache__/config.cpython-310.pycAdded
- peft/tuners/adalora/__pycache__/gptq.cpython-310.pycAdded
- peft/tuners/adalora/__pycache__/layer.cpython-310.pycAdded
- peft/tuners/adalora/__pycache__/model.cpython-310.pycAdded
- peft/tuners/adalora/bnb.pyAdded+145
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586import osimport gradio as grimport numpy as npimport cv2import uuidimport torchimport torchvisionimport json
from PIL import Imagefrom omegaconf import OmegaConffrom einops import rearrange, repeatfrom torchvision import transformsfrom transformers import CLIPTextModel, CLIPTokenizerfrom diffusers import AutoencoderKL, DDIMScheduler
from pipelines.pipeline_imagecoductor import ImageConductorPipelinefrom modules.unet import UNet3DConditionFlowModelfrom utils.gradio_utils import ensure_dirname, split_filename, visualize_drag, image2pil, image2arrfrom utils.utils import create_image_controlnet, create_flow_controlnet, interpolate_trajectory, load_weights, load_model, bivariate_Gaussianfrom utils.lora_utils import add_LoRA_to_controlnetfrom utils.visualizer import Visualizer, vis_flow_to_video#### Description ####title = r"""<h1 align="center">CustomNet: Object Customization with Variable-Viewpoints in Text-to-Image Diffusion Models</h1>"""
head = r"""<div style="text-align: center;"> <h1>Image Conductor: Precision Control for Interactive Video Synthesis</h1> <div style="display: flex; justify-content: center; align-items: center; text-align: center;"> <a href=""></a> <a href='https://liyaowei-stu.github.io/project/ImageConductor/'><img src='https://img.shields.io/badge/Project_Page-ImgaeConductor-green' alt='Project Page'></a> <a href='https://arxiv.org/pdf/2406.15339'><img src='https://img.shields.io/badge/Paper-Arxiv-blue'></a> <a href='https://github.com/liyaowei-stu/ImageConductor'><img src='https://img.shields.io/badge/Code-Github-orange'></a>
</div> </br></div>"""
descriptions = r"""Official Gradio Demo for <a href='https://github.com/liyaowei-stu/ImageConductor'><b>Image Conductor: Precision Control for Interactive Video Synthesis</b></a>.<br>🧙Image Conductor enables precise, fine-grained control for generating motion-controllable videos from images, advancing the practical application of interactive video synthesis.<br>"""
instructions = r""" - ⭐️ <b>step1: </b>Upload or select one image from Example. - ⭐️ <b>step2: </b>Click 'Add Drag' to draw some drags. - ⭐️ <b>step3: </b>Input text prompt that complements the image (Necessary). - ⭐️ <b>step4: </b>Select 'Drag Mode' to specify the control of camera transition or object movement. - ⭐️ <b>step5: </b>Click 'Run' button to generate video assets. - ⭐️ <b>others: </b>Click 'Delete last drag' to delete the whole lastest path. Click 'Delete last step' to delete the lastest clicked control point. """
citation = r"""If Image Conductor is helpful, please help to ⭐ the <a href='https://github.com/liyaowei-stu/ImageConductor' target='_blank'>Github Repo</a>. Thanks! [](https://github.com/liyaowei-stu/ImageConductor)---
📝 **Citation**<br>If our work is useful for your research, please consider citing:```bibtex@misc{li2024imageconductor, title={Image Conductor: Precision Control for Interactive Video Synthesis}, author={Li, Yaowei and Wang, Xintao and Zhang, Zhaoyang and Wang, Zhouxia and Yuan, Ziyang and Xie, Liangbin and Zou, Yuexian and Shan, Ying}, year={2024}, eprint={2406.15339}, archivePrefix={arXiv}, primaryClass={cs.CV}}```
📧 **Contact**<br>If you have any questions, please feel free to reach me out at <b>ywl@stu.pku.edu.cn</b>.
# """
os.makedirs("models/personalized")os.system(f'wget https://huggingface.co/TencentARC/ImageConductor/blob/main/flow_controlnet.ckpt -P models/')os.system(f'wget https://huggingface.co/TencentARC/ImageConductor/blob/main/image_controlnet.ckpt -P models/')os.system(f'wget https://huggingface.co/TencentARC/ImageConductor/blob/main/unet.ckpt -P models/')os.system(f'wget https://huggingface.co/TencentARC/ImageConductor/blob/main/helloobjects_V12c.safetensors -P models/personalized')os.system(f'wget https://huggingface.co/TencentARC/ImageConductor/blob/main/TUSUN.safetensors -P models/personalized')
# - - - - - examples - - - - - #image_examples = [ ["__asset__/images/object/turtle-1.jpg", "a sea turtle gracefully swimming over a coral reef in the clear blue ocean.", "object", 11318446767408804497, "", json.load(open("__asset__/trajs/object/turtle-1.json")), "__asset__/images/object/turtle-1.jpg", ], ["__asset__/images/object/rose-1.jpg", "a red rose engulfed in flames.", "object", 6854275249656120509, "", json.load(open("__asset__/trajs/object/rose-1.json")), "__asset__/images/object/rose-1.jpg", ], ["__asset__/images/object/jellyfish-1.jpg", "intricate detailing,photorealism,hyperrealistic, glowing jellyfish mushroom, flying, starry sky, bokeh, golden ratio composition.", "object", 17966188172968903484, "HelloObject", json.load(open("__asset__/trajs/object/jellyfish-1.json")), "__asset__/images/object/jellyfish-1.jpg", ], ["__asset__/images/camera/lush-1.jpg", "detailed craftsmanship, photorealism, hyperrealistic, roaring waterfall, misty spray, lush greenery, vibrant rainbow, golden ratio composition.", "camera", 7970487946960948963, "HelloObject", json.load(open("__asset__/trajs/camera/lush-1.json")), "__asset__/images/camera/lush-1.jpg", ], ["__asset__/images/camera/tusun-1.jpg", "tusuncub with its mouth open, blurry, open mouth, fangs, photo background, looking at viewer, tongue, full body, solo, cute and lovely, Beautiful and realistic eye details, perfect anatomy, Nonsense, pure background, Centered-Shot, realistic photo, photograph, 4k, hyper detailed, DSLR, 24 Megapixels, 8mm Lens, Full Frame, film grain, Global Illumination, studio Lighting, Award Winning Photography, diffuse reflection, ray tracing.", "camera", 996953226890228361, "TUSUN", json.load(open("__asset__/trajs/camera/tusun-1.json")), "__asset__/images/camera/tusun-1.jpg", ], ["__asset__/images/camera/painting-1.jpg", "A oil painting.", "camera", 16867854766769816385, "", json.load(open("__asset__/trajs/camera/painting-1.json")), "__asset__/images/camera/painting-1.jpg", ], ]
DREAM_BOOTH = { 'HelloObject': 'models/personalized/helloobjects_V12c.safetensors',}
LORA = { 'TUSUN': 'models/personalized/TUSUN.safetensors',}
LORA_ALPHA = { 'TUSUN': 0.6,}
NPROMPT = { "HelloObject": 'FastNegativeV2,(bad-artist:1),(worst quality, low quality:1.4),(bad_prompt_version2:0.8),bad-hands-5,lowres,bad anatomy,bad hands,((text)),(watermark),error,missing fingers,extra digit,fewer digits,cropped,worst quality,low quality,normal quality,((username)),blurry,(extra limbs),bad-artist-anime,badhandv4,EasyNegative,ng_deepnegative_v1_75t,verybadimagenegative_v1.3,BadDream,(three hands:1.6),(three legs:1.2),(more than two hands:1.4),(more than two legs,:1.2)' }
output_dir = "outputs"ensure_dirname(output_dir)
def points_to_flows(track_points, model_length, height, width): input_drag = np.zeros((model_length - 1, height, width, 2)) for splited_track in track_points: if len(splited_track) == 1: # stationary point displacement_point = tuple([splited_track[0][0] + 1, splited_track[0][1] + 1]) splited_track = tuple([splited_track[0], displacement_point]) # interpolate the track splited_track = interpolate_trajectory(splited_track, model_length) splited_track = splited_track[:model_length] if len(splited_track) < model_length: splited_track = splited_track + [splited_track[-1]] * (model_length -len(splited_track)) for i in range(model_length - 1): start_point = splited_track[i] end_point = splited_track[i+1] input_drag[i][int(start_point[1])][int(start_point[0])][0] = end_point[0] - start_point[0] input_drag[i][int(start_point[1])][int(start_point[0])][1] = end_point[1] - start_point[1] return input_drag
class ImageConductor: def __init__(self, device, unet_path, image_controlnet_path, flow_controlnet_path, height, width, model_length, lora_rank=64): self.device = device tokenizer = CLIPTokenizer.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="tokenizer") text_encoder = CLIPTextModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="text_encoder").cuda() vae = AutoencoderKL.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="vae").cuda() inference_config = OmegaConf.load("configs/inference/inference.yaml") unet = UNet3DConditionFlowModel.from_pretrained_2d("runwayml/stable-diffusion-v1-5", subfolder="unet", unet_additional_kwargs=OmegaConf.to_container(inference_config.unet_additional_kwargs))
self.vae = vae
### >>> Initialize UNet module >>> ### load_model(unet, unet_path)
### >>> Initialize image controlnet module >>> ### image_controlnet = create_image_controlnet("configs/inference/image_condition.yaml", unet) load_model(image_controlnet, image_controlnet_path) ### >>> Initialize flow controlnet module >>> ### flow_controlnet = create_flow_controlnet("configs/inference/flow_condition.yaml", unet) add_LoRA_to_controlnet(lora_rank, flow_controlnet) load_model(flow_controlnet, flow_controlnet_path)
unet.eval().to(device) image_controlnet.eval().to(device) flow_controlnet.eval().to(device)
self.pipeline = ImageConductorPipeline( unet=unet, vae=vae, tokenizer=tokenizer, text_encoder=text_encoder, scheduler=DDIMScheduler(**OmegaConf.to_container(inference_config.noise_scheduler_kwargs)), image_controlnet=image_controlnet, flow_controlnet=flow_controlnet, ).to(device)
self.height = height self.width = width # _, model_step, _ = split_filename(model_path) # self.ouput_prefix = f'{model_step}_{width}X{height}' self.model_length = model_length
blur_kernel = bivariate_Gaussian(kernel_size=99, sig_x=10, sig_y=10, theta=0, grid=None, isotropic=True)
self.blur_kernel = blur_kernel
@torch.no_grad() def run(self, first_frame_path, tracking_points, prompt, drag_mode, negative_prompt, seed, randomize_seed, guidance_scale, num_inference_steps, personalized):
original_width, original_height=384, 256 if isinstance(tracking_points, list): input_all_points = tracking_points else: input_all_points = tracking_points.constructor_args['value'] resized_all_points = [tuple([tuple([float(e1[0]*self.width/original_width), float(e1[1]*self.height/original_height)]) for e1 in e]) for e in input_all_points]
dir, base, ext = split_filename(first_frame_path) id = base.split('_')[-1] with open(f'{output_dir}/points-{id}.json', 'w') as f: json.dump(input_all_points, f) visualized_drag, _ = visualize_drag(first_frame_path, resized_all_points, drag_mode, self.width, self.height, self.model_length)
## image condition image_transforms = transforms.Compose([ transforms.RandomResizedCrop( (self.height, self.width), (1.0, 1.0), ratio=(self.width/self.height, self.width/self.height) ), transforms.ToTensor(), ])
image_norm = lambda x: x image_paths = [first_frame_path] controlnet_images = [image_norm(image_transforms(Image.open(path).convert("RGB"))) for path in image_paths] controlnet_images = torch.stack(controlnet_images).unsqueeze(0).cuda() controlnet_images = rearrange(controlnet_images, "b f c h w -> b c f h w") num_controlnet_images = controlnet_images.shape[2] controlnet_images = rearrange(controlnet_images, "b c f h w -> (b f) c h w") controlnet_images = self.vae.encode(controlnet_images * 2. - 1.).latent_dist.sample() * 0.18215 controlnet_images = rearrange(controlnet_images, "(b f) c h w -> b c f h w", f=num_controlnet_images)
# flow condition controlnet_flows = points_to_flows(resized_all_points, self.model_length, self.height, self.width) for i in range(0, self.model_length-1): controlnet_flows[i] = cv2.filter2D(controlnet_flows[i], -1, self.blur_kernel) controlnet_flows = np.concatenate([np.zeros_like(controlnet_flows[0])[np.newaxis, ...], controlnet_flows], axis=0) # pad the first frame with zero flow os.makedirs(os.path.join(output_dir, "control_flows"), exist_ok=True) trajs_video = vis_flow_to_video(controlnet_flows, num_frames=self.model_length) # T-1 x H x W x 3 torchvision.io.write_video(f'{output_dir}/control_flows/sample-{id}-train_flow.mp4', trajs_video, fps=8, video_codec='h264', options={'crf': '10'}) controlnet_flows = torch.from_numpy(controlnet_flows)[None].to(controlnet_images)[:, :self.model_length, ...] controlnet_flows = rearrange(controlnet_flows, "b f h w c-> b c f h w")
dreambooth_model_path = DREAM_BOOTH.get(personalized, '') lora_model_path = LORA.get(personalized, '') lora_alpha = LORA_ALPHA.get(personalized, 0.6) self.pipeline = load_weights( self.pipeline, dreambooth_model_path = dreambooth_model_path, lora_model_path = lora_model_path, lora_alpha = lora_alpha, ).to(device) if NPROMPT.get(personalized, '') != '': negative_prompt = NPROMPT.get(personalized) if randomize_seed: random_seed = torch.seed() else: seed = int(seed) random_seed = seed torch.manual_seed(random_seed) torch.cuda.manual_seed_all(random_seed) print(f"current seed: {torch.initial_seed()}") sample = self.pipeline( prompt, negative_prompt = negative_prompt, num_inference_steps = num_inference_steps, guidance_scale = guidance_scale, width = self.width, height = self.height, video_length = self.model_length, controlnet_images = controlnet_images, # 1 4 1 32 48 controlnet_image_index = [0], controlnet_flows = controlnet_flows,# [1, 2, 16, 256, 384] control_mode = drag_mode, eval_mode = True, ).videos
outputs_path = os.path.join(output_dir, f'output_{i}_{id}.mp4') vis_video = (rearrange(sample[0], 'c t h w -> t h w c') * 255.).clip(0, 255) torchvision.io.write_video(outputs_path, vis_video, fps=8, video_codec='h264', options={'crf': '10'})
return visualized_drag, outputs_path
def reset_states(first_frame_path, tracking_points): first_frame_path = gr.State() tracking_points = gr.State([]) return None, first_frame_path, tracking_points
def preprocess_image(image): image_pil = image2pil(image.name) raw_w, raw_h = image_pil.size resize_ratio = max(384/raw_w, 256/raw_h) image_pil = image_pil.resize((int(raw_w * resize_ratio), int(raw_h * resize_ratio)), Image.BILINEAR) image_pil = transforms.CenterCrop((256, 384))(image_pil.convert('RGB')) id = str(uuid.uuid4())[:4] first_frame_path = os.path.join(output_dir, f"first_frame_{id}.jpg") image_pil.save(first_frame_path, quality=95) return first_frame_path, first_frame_path, gr.State([])
def add_tracking_points(tracking_points, first_frame_path, drag_mode, evt: gr.SelectData): # SelectData is a subclass of EventData if drag_mode=='object': color = (255, 0, 0, 255) elif drag_mode=='camera': color = (0, 0, 255, 255)
print(f"You selected {evt.value} at {evt.index} from {evt.target}") tracking_points.constructor_args['value'][-1].append(evt.index) print(tracking_points.constructor_args) transparent_background = Image.open(first_frame_path).convert('RGBA') w, h = transparent_background.size transparent_layer = np.zeros((h, w, 4)) for track in tracking_points.constructor_args['value']: if len(track) > 1: for i in range(len(track)-1): start_point = track[i] end_point = track[i+1] vx = end_point[0] - start_point[0] vy = end_point[1] - start_point[1] arrow_length = np.sqrt(vx**2 + vy**2) if i == len(track)-2: cv2.arrowedLine(transparent_layer, tuple(start_point), tuple(end_point), color, 2, tipLength=8 / arrow_length) else: cv2.line(transparent_layer, tuple(start_point), tuple(end_point), color, 2,) else: cv2.circle(transparent_layer, tuple(track[0]), 5, color, -1)
transparent_layer = Image.fromarray(transparent_layer.astype(np.uint8)) trajectory_map = Image.alpha_composite(transparent_background, transparent_layer) return tracking_points, trajectory_map
def add_drag(tracking_points): tracking_points.constructor_args['value'].append([]) print(tracking_points.constructor_args) return tracking_points
def delete_last_drag(tracking_points, first_frame_path, drag_mode): if drag_mode=='object': color = (255, 0, 0, 255) elif drag_mode=='camera': color = (0, 0, 255, 255) tracking_points.constructor_args['value'].pop() transparent_background = Image.open(first_frame_path).convert('RGBA') w, h = transparent_background.size transparent_layer = np.zeros((h, w, 4)) for track in tracking_points.constructor_args['value']: if len(track) > 1: for i in range(len(track)-1): start_point = track[i] end_point = track[i+1] vx = end_point[0] - start_point[0] vy = end_point[1] - start_point[1] arrow_length = np.sqrt(vx**2 + vy**2) if i == len(track)-2: cv2.arrowedLine(transparent_layer, tuple(start_point), tuple(end_point), color, 2, tipLength=8 / arrow_length) else: cv2.line(transparent_layer, tuple(start_point), tuple(end_point), color, 2,) else: cv2.circle(transparent_layer, tuple(track[0]), 5, color, -1)
transparent_layer = Image.fromarray(transparent_layer.astype(np.uint8)) trajectory_map = Image.alpha_composite(transparent_background, transparent_layer) return tracking_points, trajectory_map
def delete_last_step(tracking_points, first_frame_path, drag_mode): if drag_mode=='object': color = (255, 0, 0, 255) elif drag_mode=='camera': color = (0, 0, 255, 255) tracking_points.constructor_args['value'][-1].pop() transparent_background = Image.open(first_frame_path).convert('RGBA') w, h = transparent_background.size transparent_layer = np.zeros((h, w, 4)) for track in tracking_points.constructor_args['value']: if len(track) > 1: for i in range(len(track)-1): start_point = track[i] end_point = track[i+1] vx = end_point[0] - start_point[0] vy = end_point[1] - start_point[1] arrow_length = np.sqrt(vx**2 + vy**2) if i == len(track)-2: cv2.arrowedLine(transparent_layer, tuple(start_point), tuple(end_point), color, 2, tipLength=8 / arrow_length) else: cv2.line(transparent_layer, tuple(start_point), tuple(end_point), color, 2,) else: cv2.circle(transparent_layer, tuple(track[0]), 5,color, -1)
transparent_layer = Image.fromarray(transparent_layer.astype(np.uint8)) trajectory_map = Image.alpha_composite(transparent_background, transparent_layer) return tracking_points, trajectory_map
block = gr.Blocks( theme=gr.themes.Soft( radius_size=gr.themes.sizes.radius_none, text_size=gr.themes.sizes.text_md ) ).queue()with block as demo: with gr.Row(): with gr.Column(): gr.HTML(head)
gr.Markdown(descriptions)
with gr.Accordion(label="🛠️ Instructions:", open=True, elem_id="accordion"): with gr.Row(equal_height=True): gr.Markdown(instructions)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") unet_path = 'models/unet.ckpt' image_controlnet_path = 'models/image_controlnet.ckpt' flow_controlnet_path = 'models/flow_controlnet.ckpt' ImageConductor_net = ImageConductor(device=device, unet_path=unet_path, image_controlnet_path=image_controlnet_path, flow_controlnet_path=flow_controlnet_path, height=256, width=384, model_length=16 ) first_frame_path = gr.State() tracking_points = gr.State([])
with gr.Row(): with gr.Column(scale=1): image_upload_button = gr.UploadButton(label="Upload Image",file_types=["image"]) add_drag_button = gr.Button(value="Add Drag") reset_button = gr.Button(value="Reset") delete_last_drag_button = gr.Button(value="Delete last drag") delete_last_step_button = gr.Button(value="Delete last step")
with gr.Column(scale=7): with gr.Row(): with gr.Column(scale=6): input_image = gr.Image(label=None, interactive=True, height=256, width=384,) with gr.Column(scale=6): output_image = gr.Image(label="Motion Path", interactive=False, height=256, width=384,) with gr.Row(): with gr.Column(scale=1): prompt = gr.Textbox(value="a wonderful elf.", label="Prompt (highly-recommended)", interactive=True, visible=True) negative_prompt = gr.Text( label="Negative Prompt", max_lines=5, placeholder="Please input your negative prompt", value='worst quality, low quality, letterboxed',lines=1 ) drag_mode = gr.Radio(['camera', 'object'], label='Drag mode: ', value='object', scale=2) run_button = gr.Button(value="Run")
with gr.Accordion("More input params", open=False, elem_id="accordion1"): with gr.Group(): seed = gr.Textbox( label="Seed: ", value=561793204, ) randomize_seed = gr.Checkbox(label="Randomize seed", value=False) with gr.Group(): with gr.Row(): guidance_scale = gr.Slider( label="Guidance scale", minimum=1, maximum=12, step=0.1, value=8.5, ) num_inference_steps = gr.Slider( label="Number of inference steps", minimum=1, maximum=50, step=1, value=25, ) with gr.Group(): personalized = gr.Dropdown(label="Personalized template", choices=['HelloObject', 'TUSUN'], value="")
with gr.Column(scale=7): output_video = gr.Video(value=None, label="Output Video", width=384, height=256) with gr.Row(): def process_example(input_image, prompt, drag_mode, seed, personalized, tracking_points, first_frame_path):
return input_image, prompt, drag_mode, seed, personalized, tracking_points, first_frame_path example = gr.Examples( label="Input Example", examples=image_examples, inputs=[input_image, prompt, drag_mode, seed, personalized, tracking_points, first_frame_path], outputs=[input_image, prompt, drag_mode, seed, personalized, tracking_points, first_frame_path], fn=process_example, run_on_click=True, examples_per_page=10 ) with gr.Row(): gr.Markdown(citation)
image_upload_button.upload(preprocess_image, image_upload_button, [input_image, first_frame_path, tracking_points])
add_drag_button.click(add_drag, tracking_points, tracking_points)
delete_last_drag_button.click(delete_last_drag, [tracking_points, first_frame_path, drag_mode], [tracking_points, input_image])
delete_last_step_button.click(delete_last_step, [tracking_points, first_frame_path, drag_mode], [tracking_points, input_image])
reset_button.click(reset_states, [first_frame_path, tracking_points], [input_image, first_frame_path, tracking_points])
input_image.select(add_tracking_points, [tracking_points, first_frame_path, drag_mode], [tracking_points, input_image])
run_button.click(ImageConductor_net.run, [first_frame_path, tracking_points, prompt, drag_mode, negative_prompt, seed, randomize_seed, guidance_scale, num_inference_steps, personalized], [output_image, output_video])
demo.launch(server_name="0.0.0.0", debug=True, server_port=12345)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577import osimport gradio as grimport numpy as npimport cv2import uuidimport torchimport torchvisionimport json
from PIL import Imagefrom omegaconf import OmegaConffrom einops import rearrange, repeatfrom torchvision import transformsfrom transformers import CLIPTextModel, CLIPTokenizerfrom diffusers import AutoencoderKL, DDIMScheduler
from pipelines.pipeline_imagecoductor import ImageConductorPipelinefrom modules.unet import UNet3DConditionFlowModelfrom utils.gradio_utils import ensure_dirname, split_filename, visualize_drag, image2pil, image2arrfrom utils.utils import create_image_controlnet, create_flow_controlnet, interpolate_trajectory, load_weights, load_model, bivariate_Gaussianfrom utils.lora_utils import add_LoRA_to_controlnetfrom utils.visualizer import Visualizer, vis_flow_to_video#### Description ####title = r"""<h1 align="center">CustomNet: Object Customization with Variable-Viewpoints in Text-to-Image Diffusion Models</h1>"""
head = r"""<div style="text-align: center;"> <h1>Image Conductor: Precision Control for Interactive Video Synthesis</h1> <div style="display: flex; justify-content: center; align-items: center; text-align: center;"> <a href=""></a> <a href='https://liyaowei-stu.github.io/project/ImageConductor/'><img src='https://img.shields.io/badge/Project_Page-ImgaeConductor-green' alt='Project Page'></a> <a href='https://arxiv.org/pdf/2406.15339'><img src='https://img.shields.io/badge/Paper-Arxiv-blue'></a> <a href='https://github.com/liyaowei-stu/ImageConductor'><img src='https://img.shields.io/badge/Code-Github-orange'></a>
</div> </br></div>"""
descriptions = r"""Official Gradio Demo for <a href='https://github.com/liyaowei-stu/ImageConductor'><b>Image Conductor: Precision Control for Interactive Video Synthesis</b></a>.<br>🧙Image Conductor enables precise, fine-grained control for generating motion-controllable videos from images, advancing the practical application of interactive video synthesis.<br>"""
instructions = r""" - ⭐️ <b>step1: </b>Upload or select one image from Example. - ⭐️ <b>step2: </b>Click 'Add Drag' to draw some drags. - ⭐️ <b>step3: </b>Input text prompt that complements the image (Necessary). - ⭐️ <b>step4: </b>Select 'Drag Mode' to specify the control of camera transition or object movement. - ⭐️ <b>step5: </b>Click 'Run' button to generate video assets. - ⭐️ <b>others: </b>Click 'Delete last drag' to delete the whole lastest path. Click 'Delete last step' to delete the lastest clicked control point. """
citation = r"""If Image Conductor is helpful, please help to ⭐ the <a href='https://github.com/liyaowei-stu/ImageConductor' target='_blank'>Github Repo</a>. Thanks! [](https://github.com/liyaowei-stu/ImageConductor)---
📝 **Citation**<br>If our work is useful for your research, please consider citing:```bibtex@misc{li2024imageconductor, title={Image Conductor: Precision Control for Interactive Video Synthesis}, author={Li, Yaowei and Wang, Xintao and Zhang, Zhaoyang and Wang, Zhouxia and Yuan, Ziyang and Xie, Liangbin and Zou, Yuexian and Shan, Ying}, year={2024}, eprint={2406.15339}, archivePrefix={arXiv}, primaryClass={cs.CV}}```
📧 **Contact**<br>If you have any questions, please feel free to reach me out at <b>ywl@stu.pku.edu.cn</b>.
# """
# - - - - - examples - - - - - #image_examples = [ ["__asset__/images/object/turtle-1.jpg", "a sea turtle gracefully swimming over a coral reef in the clear blue ocean.", "object", 11318446767408804497, "", json.load(open("__asset__/trajs/object/turtle-1.json")), "__asset__/images/object/turtle-1.jpg", ], ["__asset__/images/object/rose-1.jpg", "a red rose engulfed in flames.", "object", 6854275249656120509, "", json.load(open("__asset__/trajs/object/rose-1.json")), "__asset__/images/object/rose-1.jpg", ], ["__asset__/images/object/jellyfish-1.jpg", "intricate detailing,photorealism,hyperrealistic, glowing jellyfish mushroom, flying, starry sky, bokeh, golden ratio composition.", "object", 17966188172968903484, "HelloObject", json.load(open("__asset__/trajs/object/jellyfish-1.json")), "__asset__/images/object/jellyfish-1.jpg", ], ["__asset__/images/camera/lush-1.jpg", "detailed craftsmanship, photorealism, hyperrealistic, roaring waterfall, misty spray, lush greenery, vibrant rainbow, golden ratio composition.", "camera", 7970487946960948963, "HelloObject", json.load(open("__asset__/trajs/camera/lush-1.json")), "__asset__/images/camera/lush-1.jpg", ], ["__asset__/images/camera/tusun-1.jpg", "tusuncub with its mouth open, blurry, open mouth, fangs, photo background, looking at viewer, tongue, full body, solo, cute and lovely, Beautiful and realistic eye details, perfect anatomy, Nonsense, pure background, Centered-Shot, realistic photo, photograph, 4k, hyper detailed, DSLR, 24 Megapixels, 8mm Lens, Full Frame, film grain, Global Illumination, studio Lighting, Award Winning Photography, diffuse reflection, ray tracing.", "camera", 996953226890228361, "TUSUN", json.load(open("__asset__/trajs/camera/tusun-1.json")), "__asset__/images/camera/tusun-1.jpg", ], ["__asset__/images/camera/painting-1.jpg", "A oil painting.", "camera", 16867854766769816385, "", json.load(open("__asset__/trajs/camera/painting-1.json")), "__asset__/images/camera/painting-1.jpg", ], ]
DREAM_BOOTH = { 'HelloObject': 'models/personalized/helloobjects_V12c.safetensors',}
LORA = { 'TUSUN': 'models/personalized/TUSUN.safetensors',}
LORA_ALPHA = { 'TUSUN': 0.6,}
NPROMPT = { "HelloObject": 'FastNegativeV2,(bad-artist:1),(worst quality, low quality:1.4),(bad_prompt_version2:0.8),bad-hands-5,lowres,bad anatomy,bad hands,((text)),(watermark),error,missing fingers,extra digit,fewer digits,cropped,worst quality,low quality,normal quality,((username)),blurry,(extra limbs),bad-artist-anime,badhandv4,EasyNegative,ng_deepnegative_v1_75t,verybadimagenegative_v1.3,BadDream,(three hands:1.6),(three legs:1.2),(more than two hands:1.4),(more than two legs,:1.2)' }
output_dir = "outputs"ensure_dirname(output_dir)
def points_to_flows(track_points, model_length, height, width): input_drag = np.zeros((model_length - 1, height, width, 2)) for splited_track in track_points: if len(splited_track) == 1: # stationary point displacement_point = tuple([splited_track[0][0] + 1, splited_track[0][1] + 1]) splited_track = tuple([splited_track[0], displacement_point]) # interpolate the track splited_track = interpolate_trajectory(splited_track, model_length) splited_track = splited_track[:model_length] if len(splited_track) < model_length: splited_track = splited_track + [splited_track[-1]] * (model_length -len(splited_track)) for i in range(model_length - 1): start_point = splited_track[i] end_point = splited_track[i+1] input_drag[i][int(start_point[1])][int(start_point[0])][0] = end_point[0] - start_point[0] input_drag[i][int(start_point[1])][int(start_point[0])][1] = end_point[1] - start_point[1] return input_drag
class ImageConductor: def __init__(self, device, unet_path, image_controlnet_path, flow_controlnet_path, height, width, model_length, lora_rank=64): self.device = device tokenizer = CLIPTokenizer.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="tokenizer") text_encoder = CLIPTextModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="text_encoder").cuda() vae = AutoencoderKL.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="vae").cuda() inference_config = OmegaConf.load("configs/inference/inference.yaml") unet = UNet3DConditionFlowModel.from_pretrained_2d("runwayml/stable-diffusion-v1-5", subfolder="unet", unet_additional_kwargs=OmegaConf.to_container(inference_config.unet_additional_kwargs))
self.vae = vae
### >>> Initialize UNet module >>> ### load_model(unet, unet_path)
### >>> Initialize image controlnet module >>> ### image_controlnet = create_image_controlnet("configs/inference/image_condition.yaml", unet) load_model(image_controlnet, image_controlnet_path) ### >>> Initialize flow controlnet module >>> ### flow_controlnet = create_flow_controlnet("configs/inference/flow_condition.yaml", unet) add_LoRA_to_controlnet(lora_rank, flow_controlnet) load_model(flow_controlnet, flow_controlnet_path)
unet.eval().to(device) image_controlnet.eval().to(device) flow_controlnet.eval().to(device)
self.pipeline = ImageConductorPipeline( unet=unet, vae=vae, tokenizer=tokenizer, text_encoder=text_encoder, scheduler=DDIMScheduler(**OmegaConf.to_container(inference_config.noise_scheduler_kwargs)), image_controlnet=image_controlnet, flow_controlnet=flow_controlnet, ).to(device)
self.height = height self.width = width # _, model_step, _ = split_filename(model_path) # self.ouput_prefix = f'{model_step}_{width}X{height}' self.model_length = model_length
blur_kernel = bivariate_Gaussian(kernel_size=99, sig_x=10, sig_y=10, theta=0, grid=None, isotropic=True)
self.blur_kernel = blur_kernel
@torch.no_grad() def run(self, first_frame_path, tracking_points, prompt, drag_mode, negative_prompt, seed, randomize_seed, guidance_scale, num_inference_steps, personalized):
original_width, original_height=384, 256 if isinstance(tracking_points, list): input_all_points = tracking_points else: input_all_points = tracking_points.constructor_args['value'] resized_all_points = [tuple([tuple([float(e1[0]*self.width/original_width), float(e1[1]*self.height/original_height)]) for e1 in e]) for e in input_all_points]
dir, base, ext = split_filename(first_frame_path) id = base.split('_')[-1] with open(f'{output_dir}/points-{id}.json', 'w') as f: json.dump(input_all_points, f) visualized_drag, _ = visualize_drag(first_frame_path, resized_all_points, drag_mode, self.width, self.height, self.model_length)
## image condition image_transforms = transforms.Compose([ transforms.RandomResizedCrop( (self.height, self.width), (1.0, 1.0), ratio=(self.width/self.height, self.width/self.height) ), transforms.ToTensor(), ])
image_norm = lambda x: x image_paths = [first_frame_path] controlnet_images = [image_norm(image_transforms(Image.open(path).convert("RGB"))) for path in image_paths] controlnet_images = torch.stack(controlnet_images).unsqueeze(0).cuda() controlnet_images = rearrange(controlnet_images, "b f c h w -> b c f h w") num_controlnet_images = controlnet_images.shape[2] controlnet_images = rearrange(controlnet_images, "b c f h w -> (b f) c h w") controlnet_images = self.vae.encode(controlnet_images * 2. - 1.).latent_dist.sample() * 0.18215 controlnet_images = rearrange(controlnet_images, "(b f) c h w -> b c f h w", f=num_controlnet_images)
# flow condition controlnet_flows = points_to_flows(resized_all_points, self.model_length, self.height, self.width) for i in range(0, self.model_length-1): controlnet_flows[i] = cv2.filter2D(controlnet_flows[i], -1, self.blur_kernel) controlnet_flows = np.concatenate([np.zeros_like(controlnet_flows[0])[np.newaxis, ...], controlnet_flows], axis=0) # pad the first frame with zero flow os.makedirs(os.path.join(output_dir, "control_flows"), exist_ok=True) trajs_video = vis_flow_to_video(controlnet_flows, num_frames=self.model_length) # T-1 x H x W x 3 torchvision.io.write_video(f'{output_dir}/control_flows/sample-{id}-train_flow.mp4', trajs_video, fps=8, video_codec='h264', options={'crf': '10'}) controlnet_flows = torch.from_numpy(controlnet_flows)[None].to(controlnet_images)[:, :self.model_length, ...] controlnet_flows = rearrange(controlnet_flows, "b f h w c-> b c f h w")
dreambooth_model_path = DREAM_BOOTH.get(personalized, '') lora_model_path = LORA.get(personalized, '') lora_alpha = LORA_ALPHA.get(personalized, 0.6) self.pipeline = load_weights( self.pipeline, dreambooth_model_path = dreambooth_model_path, lora_model_path = lora_model_path, lora_alpha = lora_alpha, ).to(device) if NPROMPT.get(personalized, '') != '': negative_prompt = NPROMPT.get(personalized) if randomize_seed: random_seed = torch.seed() else: seed = int(seed) random_seed = seed torch.manual_seed(random_seed) torch.cuda.manual_seed_all(random_seed) print(f"current seed: {torch.initial_seed()}") sample = self.pipeline( prompt, negative_prompt = negative_prompt, num_inference_steps = num_inference_steps, guidance_scale = guidance_scale, width = self.width, height = self.height, video_length = self.model_length, controlnet_images = controlnet_images, # 1 4 1 32 48 controlnet_image_index = [0], controlnet_flows = controlnet_flows,# [1, 2, 16, 256, 384] control_mode = drag_mode, eval_mode = True, ).videos
outputs_path = os.path.join(output_dir, f'output_{i}_{id}.mp4') vis_video = (rearrange(sample[0], 'c t h w -> t h w c') * 255.).clip(0, 255) torchvision.io.write_video(outputs_path, vis_video, fps=8, video_codec='h264', options={'crf': '10'})
return visualized_drag, outputs_path
def reset_states(first_frame_path, tracking_points): first_frame_path = gr.State() tracking_points = gr.State([]) return None, first_frame_path, tracking_points
def preprocess_image(image): image_pil = image2pil(image.name) raw_w, raw_h = image_pil.size resize_ratio = max(384/raw_w, 256/raw_h) image_pil = image_pil.resize((int(raw_w * resize_ratio), int(raw_h * resize_ratio)), Image.BILINEAR) image_pil = transforms.CenterCrop((256, 384))(image_pil.convert('RGB')) id = str(uuid.uuid4())[:4] first_frame_path = os.path.join(output_dir, f"first_frame_{id}.jpg") image_pil.save(first_frame_path, quality=95) return first_frame_path, first_frame_path, gr.State([])
def add_tracking_points(tracking_points, first_frame_path, drag_mode, evt: gr.SelectData): # SelectData is a subclass of EventData if drag_mode=='object': color = (255, 0, 0, 255) elif drag_mode=='camera': color = (0, 0, 255, 255)
print(f"You selected {evt.value} at {evt.index} from {evt.target}") tracking_points.constructor_args['value'][-1].append(evt.index) print(tracking_points.constructor_args) transparent_background = Image.open(first_frame_path).convert('RGBA') w, h = transparent_background.size transparent_layer = np.zeros((h, w, 4)) for track in tracking_points.constructor_args['value']: if len(track) > 1: for i in range(len(track)-1): start_point = track[i] end_point = track[i+1] vx = end_point[0] - start_point[0] vy = end_point[1] - start_point[1] arrow_length = np.sqrt(vx**2 + vy**2) if i == len(track)-2: cv2.arrowedLine(transparent_layer, tuple(start_point), tuple(end_point), color, 2, tipLength=8 / arrow_length) else: cv2.line(transparent_layer, tuple(start_point), tuple(end_point), color, 2,) else: cv2.circle(transparent_layer, tuple(track[0]), 5, color, -1)
transparent_layer = Image.fromarray(transparent_layer.astype(np.uint8)) trajectory_map = Image.alpha_composite(transparent_background, transparent_layer) return tracking_points, trajectory_map
def add_drag(tracking_points): tracking_points.constructor_args['value'].append([]) print(tracking_points.constructor_args) return tracking_points
def delete_last_drag(tracking_points, first_frame_path, drag_mode): if drag_mode=='object': color = (255, 0, 0, 255) elif drag_mode=='camera': color = (0, 0, 255, 255) tracking_points.constructor_args['value'].pop() transparent_background = Image.open(first_frame_path).convert('RGBA') w, h = transparent_background.size transparent_layer = np.zeros((h, w, 4)) for track in tracking_points.constructor_args['value']: if len(track) > 1: for i in range(len(track)-1): start_point = track[i] end_point = track[i+1] vx = end_point[0] - start_point[0] vy = end_point[1] - start_point[1] arrow_length = np.sqrt(vx**2 + vy**2) if i == len(track)-2: cv2.arrowedLine(transparent_layer, tuple(start_point), tuple(end_point), color, 2, tipLength=8 / arrow_length) else: cv2.line(transparent_layer, tuple(start_point), tuple(end_point), color, 2,) else: cv2.circle(transparent_layer, tuple(track[0]), 5, color, -1)
transparent_layer = Image.fromarray(transparent_layer.astype(np.uint8)) trajectory_map = Image.alpha_composite(transparent_background, transparent_layer) return tracking_points, trajectory_map
def delete_last_step(tracking_points, first_frame_path, drag_mode): if drag_mode=='object': color = (255, 0, 0, 255) elif drag_mode=='camera': color = (0, 0, 255, 255) tracking_points.constructor_args['value'][-1].pop() transparent_background = Image.open(first_frame_path).convert('RGBA') w, h = transparent_background.size transparent_layer = np.zeros((h, w, 4)) for track in tracking_points.constructor_args['value']: if len(track) > 1: for i in range(len(track)-1): start_point = track[i] end_point = track[i+1] vx = end_point[0] - start_point[0] vy = end_point[1] - start_point[1] arrow_length = np.sqrt(vx**2 + vy**2) if i == len(track)-2: cv2.arrowedLine(transparent_layer, tuple(start_point), tuple(end_point), color, 2, tipLength=8 / arrow_length) else: cv2.line(transparent_layer, tuple(start_point), tuple(end_point), color, 2,) else: cv2.circle(transparent_layer, tuple(track[0]), 5,color, -1)
transparent_layer = Image.fromarray(transparent_layer.astype(np.uint8)) trajectory_map = Image.alpha_composite(transparent_background, transparent_layer) return tracking_points, trajectory_map
block = gr.Blocks( theme=gr.themes.Soft( radius_size=gr.themes.sizes.radius_none, text_size=gr.themes.sizes.text_md ) ).queue()with block as demo: with gr.Row(): with gr.Column(): gr.HTML(head)
gr.Markdown(descriptions)
with gr.Accordion(label="🛠️ Instructions:", open=True, elem_id="accordion"): with gr.Row(equal_height=True): gr.Markdown(instructions)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") unet_path = 'models/unet.ckpt' image_controlnet_path = 'models/image_controlnet.ckpt' flow_controlnet_path = 'models/flow_controlnet.ckpt' ImageConductor_net = ImageConductor(device=device, unet_path=unet_path, image_controlnet_path=image_controlnet_path, flow_controlnet_path=flow_controlnet_path, height=256, width=384, model_length=16 ) first_frame_path = gr.State() tracking_points = gr.State([])
with gr.Row(): with gr.Column(scale=1): image_upload_button = gr.UploadButton(label="Upload Image",file_types=["image"]) add_drag_button = gr.Button(value="Add Drag") reset_button = gr.Button(value="Reset") delete_last_drag_button = gr.Button(value="Delete last drag") delete_last_step_button = gr.Button(value="Delete last step")
with gr.Column(scale=7): with gr.Row(): with gr.Column(scale=6): input_image = gr.Image(label=None, interactive=True, height=256, width=384,) with gr.Column(scale=6): output_image = gr.Image(label="Motion Path", interactive=False, height=256, width=384,) with gr.Row(): with gr.Column(scale=1): prompt = gr.Textbox(value="a wonderful elf.", label="Prompt (highly-recommended)", interactive=True, visible=True) negative_prompt = gr.Text( label="Negative Prompt", max_lines=5, placeholder="Please input your negative prompt", value='worst quality, low quality, letterboxed',lines=1 ) drag_mode = gr.Radio(['camera', 'object'], label='Drag mode: ', value='object', scale=2) run_button = gr.Button(value="Run")
with gr.Accordion("More input params", open=False, elem_id="accordion1"): with gr.Group(): seed = gr.Textbox( label="Seed: ", value=561793204, ) randomize_seed = gr.Checkbox(label="Randomize seed", value=False) with gr.Group(): with gr.Row(): guidance_scale = gr.Slider( label="Guidance scale", minimum=1, maximum=12, step=0.1, value=8.5, ) num_inference_steps = gr.Slider( label="Number of inference steps", minimum=1, maximum=50, step=1, value=25, ) with gr.Group(): personalized = gr.Dropdown(label="Personalized template", choices=['HelloObject', 'TUSUN'], value="")
with gr.Column(scale=7): output_video = gr.Video(value=None, label="Output Video", width=384, height=256) with gr.Row(): def process_example(input_image, prompt, drag_mode, seed, personalized, tracking_points, first_frame_path):
return input_image, prompt, drag_mode, seed, personalized, tracking_points, first_frame_path example = gr.Examples( label="Input Example", examples=image_examples, inputs=[input_image, prompt, drag_mode, seed, personalized, tracking_points, first_frame_path], outputs=[input_image, prompt, drag_mode, seed, personalized, tracking_points, first_frame_path], fn=process_example, run_on_click=True, examples_per_page=10 ) with gr.Row(): gr.Markdown(citation)
image_upload_button.upload(preprocess_image, image_upload_button, [input_image, first_frame_path, tracking_points])
add_drag_button.click(add_drag, tracking_points, tracking_points)
delete_last_drag_button.click(delete_last_drag, [tracking_points, first_frame_path, drag_mode], [tracking_points, input_image])
delete_last_step_button.click(delete_last_step, [tracking_points, first_frame_path, drag_mode], [tracking_points, input_image])
reset_button.click(reset_states, [first_frame_path, tracking_points], [input_image, first_frame_path, tracking_points])
input_image.select(add_tracking_points, [tracking_points, first_frame_path, drag_mode], [tracking_points, input_image])
run_button.click(ImageConductor_net.run, [first_frame_path, tracking_points, prompt, drag_mode, negative_prompt, seed, randomize_seed, guidance_scale, num_inference_steps, personalized], [output_image, output_video])
demo.launch(server_name="0.0.0.0", debug=True, server_port=12345)123456789101112131415161718controlnet_additional_kwargs: set_noisy_sample_input_to_zero: true use_simplified_condition_embedding: true conditioning_channels: 2 concate_conditioning_mask: false
use_motion_module: true motion_module_resolutions: [1,2,4,8] motion_module_mid_block: false motion_module_type: "Vanilla"
motion_module_kwargs: num_attention_heads: 8 num_transformer_block: 1 attention_block_types: [ "Temporal_Self" ] temporal_position_encoding: true temporal_position_encoding_max_len: 32 temporal_attention_dim_div: 1123456789101112131415161718controlnet_additional_kwargs: set_noisy_sample_input_to_zero: true use_simplified_condition_embedding: true conditioning_channels: 4 concate_conditioning_mask: true
use_motion_module: true motion_module_resolutions: [1,2,4,8] motion_module_mid_block: false motion_module_type: "Vanilla"
motion_module_kwargs: num_attention_heads: 8 num_transformer_block: 1 attention_block_types: [ "Temporal_Self" ] temporal_position_encoding: true temporal_position_encoding_max_len: 32 temporal_attention_dim_div: 112345678910111213141516171819202122unet_additional_kwargs: use_inflated_groupnorm: true use_motion_module: true motion_module_resolutions: [1,2,4,8] motion_module_mid_block: false motion_module_type: Vanilla
motion_module_kwargs: num_attention_heads: 8 num_transformer_block: 1 attention_block_types: [ "Temporal_Self", "Temporal_Self" ] temporal_position_encoding: true temporal_position_encoding_max_len: 32 temporal_attention_dim_div: 1 zero_initialize: true
noise_scheduler_kwargs: beta_start: 0.00085 beta_end: 0.012 beta_schedule: "linear" steps_offset: 1 clip_sample: False123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396# Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention.py
import loggingfrom dataclasses import dataclassfrom typing import Any, Dict, Optional
import torchfrom diffusers.configuration_utils import ConfigMixin, register_to_configfrom diffusers.models import ModelMixinfrom diffusers.models.attention import AdaLayerNorm, Attention, FeedForwardfrom diffusers.utils import BaseOutputfrom diffusers.utils.torch_utils import maybe_allow_in_graphfrom einops import rearrange, repeatfrom torch import Tensor, nn
logger = logging.getLogger(__name__)
@dataclassclass Transformer3DModelOutput(BaseOutput): sample: torch.FloatTensor
@maybe_allow_in_graphclass Transformer3DModel(ModelMixin, ConfigMixin): @register_to_config def __init__( self, num_attention_heads: int = 16, attention_head_dim: int = 88, in_channels: Optional[int] = None, num_layers: int = 1, dropout: float = 0.0, norm_num_groups: int = 32, cross_attention_dim: Optional[int] = None, attention_bias: bool = False, activation_fn: str = "geglu", num_embeds_ada_norm: Optional[int] = None, use_linear_projection: bool = False, only_cross_attention: bool = False, upcast_attention: bool = False, unet_use_cross_frame_attention=None, unet_use_temporal_attention=None, ): super().__init__() self.use_linear_projection = use_linear_projection self.num_attention_heads = num_attention_heads self.attention_head_dim = attention_head_dim inner_dim = num_attention_heads * attention_head_dim
# Define input layers self.in_channels = in_channels
self.norm = torch.nn.GroupNorm( num_groups=norm_num_groups, num_channels=in_channels, eps=1e-6, affine=True ) if use_linear_projection: self.proj_in = nn.Linear(in_channels, inner_dim) else: self.proj_in = nn.Conv2d( in_channels, inner_dim, kernel_size=1, stride=1, padding=0 )
# Define transformers blocks self.transformer_blocks = nn.ModuleList( [ BasicTransformerBlock( inner_dim, num_attention_heads, attention_head_dim, dropout=dropout, cross_attention_dim=cross_attention_dim, activation_fn=activation_fn, num_embeds_ada_norm=num_embeds_ada_norm, attention_bias=attention_bias, only_cross_attention=only_cross_attention, upcast_attention=upcast_attention, unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, ) for d in range(num_layers) ] )
# 4. Define output layers if use_linear_projection: self.proj_out = nn.Linear(in_channels, inner_dim) else: self.proj_out = nn.Conv2d( inner_dim, in_channels, kernel_size=1, stride=1, padding=0 )
def forward( self, hidden_states: torch.Tensor, encoder_hidden_states: Optional[torch.Tensor] = None, timestep: Optional[torch.LongTensor] = None, cross_attention_kwargs: Dict[str, Any] = None, attention_mask: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, return_dict: bool = True, ): # validate input dim if hidden_states.dim() != 5: raise ValueError( f"Expected hidden_states to have ndim=5, but got ndim={hidden_states.dim()}." )
# ensure attention_mask is a bias, and give it a singleton query_tokens dimension. # we may have done this conversion already, e.g. if we came here via UNet2DConditionModel#forward. # we can tell by counting dims; if ndim == 2: it's a mask rather than a bias. # expects mask of shape: # [batch, key_tokens] # adds singleton query_tokens dimension: # [batch, 1, key_tokens] # this helps to broadcast it as a bias over attention scores, which will be in one of the following shapes: # [batch, heads, query_tokens, key_tokens] (e.g. torch sdp attn) # [batch * heads, query_tokens, key_tokens] (e.g. xformers or classic attn) if attention_mask is not None and attention_mask.ndim == 2: # assume that mask is expressed as: # (1 = keep, 0 = discard) # convert mask into a bias that can be added to attention scores: # (keep = +0, discard = -10000.0) attention_mask = (1 - attention_mask.to(hidden_states.dtype)) * -10000.0 attention_mask = attention_mask.unsqueeze(1)
# convert encoder_attention_mask to a bias the same way we do for attention_mask if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2: encoder_attention_mask = ( 1 - encoder_attention_mask.to(hidden_states.dtype) ) * -10000.0 encoder_attention_mask = encoder_attention_mask.unsqueeze(1)
# shenanigans for motion module video_length = hidden_states.shape[2] hidden_states = rearrange(hidden_states, "b c f h w -> (b f) c h w") encoder_hidden_states = repeat( encoder_hidden_states, "b n c -> (b f) n c", f=video_length )
# 1. Input batch, _, height, width = hidden_states.shape residual = hidden_states
hidden_states = self.norm(hidden_states) if not self.use_linear_projection: hidden_states = self.proj_in(hidden_states) inner_dim = hidden_states.shape[1] hidden_states = hidden_states.permute(0, 2, 3, 1).reshape( batch, height * width, inner_dim ) else: inner_dim = hidden_states.shape[1] hidden_states = hidden_states.permute(0, 2, 3, 1).reshape( batch, height * width, inner_dim ) hidden_states = self.proj_in(hidden_states)
# 2. Blocks for block in self.transformer_blocks: hidden_states = block( hidden_states, attention_mask=attention_mask, encoder_hidden_states=encoder_hidden_states, timestep=timestep, video_length=video_length, encoder_attention_mask=encoder_attention_mask, cross_attention_kwargs=cross_attention_kwargs, )
# 3. Output if not self.use_linear_projection: hidden_states = ( hidden_states.reshape(batch, height, width, inner_dim) .permute(0, 3, 1, 2) .contiguous() ) hidden_states = self.proj_out(hidden_states) else: hidden_states = self.proj_out(hidden_states) hidden_states = ( hidden_states.reshape(batch, height, width, inner_dim) .permute(0, 3, 1, 2) .contiguous() )
output = hidden_states + residual
output = rearrange(output, "(b f) c h w -> b c f h w", f=video_length) if not return_dict: return (output,)
return Transformer3DModelOutput(sample=output)
@maybe_allow_in_graphclass BasicTransformerBlock(nn.Module): def __init__( self, dim: int, num_attention_heads: int, attention_head_dim: int, dropout: float = 0.0, cross_attention_dim: Optional[int] = None, activation_fn: str = "geglu", num_embeds_ada_norm: Optional[int] = None, attention_bias: bool = False, only_cross_attention: bool = False, upcast_attention: bool = False, norm_elementwise_affine: bool = True, unet_use_cross_frame_attention: bool = False, unet_use_temporal_attention: bool = False, final_dropout: bool = False, ): super().__init__() self.only_cross_attention = only_cross_attention self.use_ada_layer_norm = num_embeds_ada_norm is not None self.unet_use_cross_frame_attention = unet_use_cross_frame_attention self.unet_use_temporal_attention = unet_use_temporal_attention
# Define 3 blocks. Each block has its own normalization layer. # Self-Attn / SC-Attn if self.use_ada_layer_norm: self.norm1 = AdaLayerNorm(dim, num_embeds_ada_norm) else: self.norm1 = nn.LayerNorm(dim, elementwise_affine=norm_elementwise_affine)
if unet_use_cross_frame_attention: # this isn't actually implemented anywhere in the AnimateDiff codebase or in Diffusers... raise NotImplementedError("SC-Attn is not implemented yet.") else: self.attn1 = Attention( query_dim=dim, cross_attention_dim=( cross_attention_dim if only_cross_attention else None ), heads=num_attention_heads, dim_head=attention_head_dim, dropout=dropout, bias=attention_bias, upcast_attention=upcast_attention, )
# 2. Cross-Attn if cross_attention_dim is not None: self.norm2 = ( AdaLayerNorm(dim, num_embeds_ada_norm) if self.use_ada_layer_norm else nn.LayerNorm(dim, elementwise_affine=norm_elementwise_affine) ) self.attn2 = Attention( query_dim=dim, cross_attention_dim=cross_attention_dim, heads=num_attention_heads, dim_head=attention_head_dim, dropout=dropout, bias=attention_bias, upcast_attention=upcast_attention, ) # is self-attn if encoder_hidden_states is none else: self.norm2 = None self.attn2 = None
# 3. Feed-forward self.norm3 = nn.LayerNorm(dim, elementwise_affine=norm_elementwise_affine) self.ff = FeedForward( dim, dropout=dropout, activation_fn=activation_fn, final_dropout=final_dropout, )
# 4. Temporal Attn assert unet_use_temporal_attention is not None if unet_use_temporal_attention: self.attn_temp = Attention( query_dim=dim, heads=num_attention_heads, dim_head=attention_head_dim, dropout=dropout, bias=attention_bias, upcast_attention=upcast_attention, ) nn.init.zeros_(self.attn_temp.to_out[0].weight.data) if self.use_ada_layer_norm: self.norm1 = AdaLayerNorm(dim, num_embeds_ada_norm) else: self.norm1 = nn.LayerNorm( dim, elementwise_affine=norm_elementwise_affine )
def forward( self, hidden_states: torch.FloatTensor, attention_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, timestep: Optional[torch.LongTensor] = None, cross_attention_kwargs: Dict[str, Any] = None, video_length=None, ): # SparseCausal-Attention # Notice that normalization is always applied before the real computation in the following blocks. # 1. Self-Attention if self.use_ada_layer_norm: norm_hidden_states = self.norm1(hidden_states, timestep) else: norm_hidden_states = self.norm1(hidden_states)
cross_attention_kwargs = ( cross_attention_kwargs if cross_attention_kwargs is not None else {} ) if self.unet_use_cross_frame_attention: cross_attention_kwargs["video_length"] = video_length
attn_output = self.attn1( norm_hidden_states, encoder_hidden_states=( encoder_hidden_states if self.only_cross_attention else None ), attention_mask=attention_mask, **cross_attention_kwargs, )
hidden_states = attn_output + hidden_states
# 2. Cross-Attention if self.attn2 is not None: norm_hidden_states = ( self.norm2(hidden_states, timestep) if self.use_ada_layer_norm else self.norm2(hidden_states) )
attn_output = self.attn2( norm_hidden_states, encoder_hidden_states=encoder_hidden_states, attention_mask=encoder_attention_mask, **cross_attention_kwargs, ) hidden_states = attn_output + hidden_states
# 3. Feed-forward hidden_states = self.ff(self.norm3(hidden_states)) + hidden_states
# 4. Temporal-Attention if self.unet_use_temporal_attention: d = hidden_states.shape[1] hidden_states = rearrange( hidden_states, "(b f) d c -> (b d) f c", f=video_length ) norm_hidden_states = ( self.norm_temp(hidden_states, timestep) if self.use_ada_layer_norm else self.norm_temp(hidden_states) ) hidden_states = self.attn_temp(norm_hidden_states) + hidden_states hidden_states = rearrange(hidden_states, "(b d) f c -> (b f) d c", d=d)
return hidden_states hidden_states = attn_output + hidden_states
# 2. Cross-Attention if self.attn2 is not None: norm_hidden_states = ( self.norm2(hidden_states, timestep) if self.use_ada_layer_norm else self.norm2(hidden_states) )
attn_output = self.attn2( norm_hidden_states, encoder_hidden_states=encoder_hidden_states, attention_mask=encoder_attention_mask, **cross_attention_kwargs, ) hidden_states = attn_output + hidden_states
# 3. Feed-forward hidden_states = self.ff(self.norm3(hidden_states)) + hidden_states
# 4. Temporal-Attention if self.unet_use_temporal_attention: d = hidden_states.shape[1] hidden_states = rearrange( hidden_states, "(b f) d c -> (b d) f c", f=video_length ) norm_hidden_states = ( self.norm_temp(hidden_states, timestep) if self.use_ada_layer_norm else self.norm_temp(hidden_states) ) hidden_states = self.attn_temp(norm_hidden_states) + hidden_states hidden_states = rearrange(hidden_states, "(b d) f c -> (b f) d c", d=d)
return hidden_states123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591# Copyright 2023 The HuggingFace Team. All rights reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# # Changes were made to this source code by Yuwei Guo.from dataclasses import dataclassfrom typing import Any, Dict, List, Optional, Tuple, Union
import torchfrom diffusers import ModelMixinfrom diffusers.configuration_utils import ConfigMixin, register_to_configfrom diffusers.models.attention_processor import AttentionProcessorfrom diffusers.models.embeddings import TimestepEmbedding, Timestepsfrom diffusers.models.unet_2d_condition import UNet2DConditionModelfrom diffusers.loaders import UNet2DConditionLoadersMixin, PeftAdapterMixinfrom diffusers.utils import BaseOutput, loggingfrom einops import rearrange, repeatfrom torch import nnfrom torch.nn import functional as F
from .resnet import InflatedConv3dfrom .unet_blocks import ( CrossAttnDownBlock3D, DownBlock3D, UNetMidBlock3DCrossAttn, get_down_block,)
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
@dataclassclass FlowControlNetOutput(BaseOutput): down_block_res_samples: Tuple[torch.Tensor] mid_block_res_sample: torch.Tensor
class FlowControlNetConditioningEmbedding(nn.Module): def __init__( self, conditioning_embedding_channels: int, conditioning_channels: int = 3, block_out_channels: Tuple[int] = (16, 32, 96, 256), ): super().__init__()
self.conv_in = InflatedConv3d(conditioning_channels, block_out_channels[0], kernel_size=3, padding=1)
self.blocks = nn.ModuleList([])
for i in range(len(block_out_channels) - 1): channel_in = block_out_channels[i] channel_out = block_out_channels[i + 1] self.blocks.append(InflatedConv3d(channel_in, channel_in, kernel_size=3, padding=1)) self.blocks.append(InflatedConv3d(channel_in, channel_out, kernel_size=3, padding=1, stride=2))
self.conv_out = zero_module( InflatedConv3d(block_out_channels[-1], conditioning_embedding_channels, kernel_size=3, padding=1) )
def forward(self, conditioning): embedding = self.conv_in(conditioning) embedding = F.silu(embedding)
for block in self.blocks: embedding = block(embedding) embedding = F.silu(embedding)
embedding = self.conv_out(embedding)
return embedding
class FlowControlNetModel(ModelMixin, ConfigMixin, UNet2DConditionLoadersMixin, PeftAdapterMixin): _supports_gradient_checkpointing = True
@register_to_config def __init__( self, in_channels: int = 4, conditioning_channels: int = 3, flip_sin_to_cos: bool = True, freq_shift: int = 0, down_block_types: Tuple[str] = ( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ), only_cross_attention: Union[bool, Tuple[bool]] = False, block_out_channels: Tuple[int] = (320, 640, 1280, 1280), layers_per_block: int = 2, downsample_padding: int = 1, mid_block_scale_factor: float = 1, act_fn: str = "silu", norm_num_groups: Optional[int] = 32, norm_eps: float = 1e-5, cross_attention_dim: int = 1280, attention_head_dim: Union[int, Tuple[int]] = 8, num_attention_heads: Optional[Union[int, Tuple[int]]] = None, use_linear_projection: bool = False, class_embed_type: Optional[str] = None, num_class_embeds: Optional[int] = None, upcast_attention: bool = False, resnet_time_scale_shift: str = "default", projection_class_embeddings_input_dim: Optional[int] = None, controlnet_conditioning_channel_order: str = "rgb", conditioning_embedding_out_channels: Optional[Tuple[int]] = (16, 32, 96, 256), global_pool_conditions: bool = False,
use_motion_module = True, motion_module_resolutions = ( 1,2,4,8 ), motion_module_mid_block = False, motion_module_type = "Vanilla", motion_module_kwargs = { "num_attention_heads": 8, "num_transformer_block": 1, "attention_block_types": ["Temporal_Self"], "temporal_position_encoding": True, "temporal_position_encoding_max_len": 32, "temporal_attention_dim_div": 1, "causal_temporal_attention": False, },
concate_conditioning_mask: bool = True, use_simplified_condition_embedding: bool = False,
set_noisy_sample_input_to_zero: bool = False, ): super().__init__()
# If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. num_attention_heads = num_attention_heads or attention_head_dim
# Check inputs if len(block_out_channels) != len(down_block_types): raise ValueError( f"Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}." )
if not isinstance(only_cross_attention, bool) and len(only_cross_attention) != len(down_block_types): raise ValueError( f"Must provide the same number of `only_cross_attention` as `down_block_types`. `only_cross_attention`: {only_cross_attention}. `down_block_types`: {down_block_types}." )
if not isinstance(num_attention_heads, int) and len(num_attention_heads) != len(down_block_types): raise ValueError( f"Must provide the same number of `num_attention_heads` as `down_block_types`. `num_attention_heads`: {num_attention_heads}. `down_block_types`: {down_block_types}." )
# input self.set_noisy_sample_input_to_zero = set_noisy_sample_input_to_zero
conv_in_kernel = 3 conv_in_padding = (conv_in_kernel - 1) // 2 self.conv_in = InflatedConv3d( in_channels, block_out_channels[0], kernel_size=conv_in_kernel, padding=conv_in_padding ) conditioning_channels = conditioning_channels * 8 * 8 if concate_conditioning_mask: conditioning_channels = conditioning_channels + 1 self.concate_conditioning_mask = concate_conditioning_mask
# control net conditioning embedding if use_simplified_condition_embedding: self.controlnet_cond_embedding = zero_module( InflatedConv3d(conditioning_channels, block_out_channels[0], kernel_size=conv_in_kernel, padding=conv_in_padding) ) else: self.controlnet_cond_embedding = FlowControlNetConditioningEmbedding( conditioning_embedding_channels=block_out_channels[0], block_out_channels=conditioning_embedding_out_channels, conditioning_channels=conditioning_channels, ) self.use_simplified_condition_embedding = use_simplified_condition_embedding
# time time_embed_dim = block_out_channels[0] * 4
self.time_proj = Timesteps(block_out_channels[0], flip_sin_to_cos, freq_shift) timestep_input_dim = block_out_channels[0]
self.time_embedding = TimestepEmbedding( timestep_input_dim, time_embed_dim, act_fn=act_fn, )
# class embedding if class_embed_type is None and num_class_embeds is not None: self.class_embedding = nn.Embedding(num_class_embeds, time_embed_dim) elif class_embed_type == "timestep": self.class_embedding = TimestepEmbedding(timestep_input_dim, time_embed_dim) elif class_embed_type == "identity": self.class_embedding = nn.Identity(time_embed_dim, time_embed_dim) elif class_embed_type == "projection": if projection_class_embeddings_input_dim is None: raise ValueError( "`class_embed_type`: 'projection' requires `projection_class_embeddings_input_dim` be set" ) # The projection `class_embed_type` is the same as the timestep `class_embed_type` except # 1. the `class_labels` inputs are not first converted to sinusoidal embeddings # 2. it projects from an arbitrary input dimension. # # Note that `TimestepEmbedding` is quite general, being mainly linear layers and activations. # When used for embedding actual timesteps, the timesteps are first converted to sinusoidal embeddings. # As a result, `TimestepEmbedding` can be passed arbitrary vectors. self.class_embedding = TimestepEmbedding(projection_class_embeddings_input_dim, time_embed_dim) else: self.class_embedding = None
self.down_blocks = nn.ModuleList([]) self.controlnet_down_blocks = nn.ModuleList([])
if isinstance(only_cross_attention, bool): only_cross_attention = [only_cross_attention] * len(down_block_types)
if isinstance(attention_head_dim, int): attention_head_dim = (attention_head_dim,) * len(down_block_types)
if isinstance(num_attention_heads, int): num_attention_heads = (num_attention_heads,) * len(down_block_types)
# down output_channel = block_out_channels[0]
controlnet_block = InflatedConv3d(output_channel, output_channel, kernel_size=1) controlnet_block = zero_module(controlnet_block) self.controlnet_down_blocks.append(controlnet_block)
for i, down_block_type in enumerate(down_block_types): res = 2 ** i input_channel = output_channel output_channel = block_out_channels[i] is_final_block = i == len(block_out_channels) - 1
down_block = get_down_block( down_block_type, num_layers=layers_per_block, in_channels=input_channel, out_channels=output_channel, temb_channels=time_embed_dim, add_downsample=not is_final_block, resnet_eps=norm_eps, resnet_act_fn=act_fn, resnet_groups=norm_num_groups, cross_attention_dim=cross_attention_dim, attn_num_head_channels=attention_head_dim[i] if attention_head_dim[i] is not None else output_channel, downsample_padding=downsample_padding, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention[i], upcast_attention=upcast_attention, resnet_time_scale_shift=resnet_time_scale_shift,
use_inflated_groupnorm=True,
use_motion_module=use_motion_module and (res in motion_module_resolutions), motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) self.down_blocks.append(down_block)
for _ in range(layers_per_block): controlnet_block = InflatedConv3d(output_channel, output_channel, kernel_size=1) controlnet_block = zero_module(controlnet_block) self.controlnet_down_blocks.append(controlnet_block)
if not is_final_block: controlnet_block = InflatedConv3d(output_channel, output_channel, kernel_size=1) controlnet_block = zero_module(controlnet_block) self.controlnet_down_blocks.append(controlnet_block)
# mid mid_block_channel = block_out_channels[-1]
controlnet_block = InflatedConv3d(mid_block_channel, mid_block_channel, kernel_size=1) controlnet_block = zero_module(controlnet_block) self.controlnet_mid_block = controlnet_block
self.mid_block = UNetMidBlock3DCrossAttn( in_channels=mid_block_channel, temb_channels=time_embed_dim, resnet_eps=norm_eps, resnet_act_fn=act_fn, output_scale_factor=mid_block_scale_factor, resnet_time_scale_shift=resnet_time_scale_shift, cross_attention_dim=cross_attention_dim, attn_num_head_channels=num_attention_heads[-1], resnet_groups=norm_num_groups, use_linear_projection=use_linear_projection, upcast_attention=upcast_attention,
use_inflated_groupnorm=True, use_motion_module=use_motion_module and motion_module_mid_block, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, )
@classmethod def from_unet( cls, unet: UNet2DConditionModel, controlnet_conditioning_channel_order: str = "rgb", conditioning_embedding_out_channels: Optional[Tuple[int]] = (16, 32, 96, 256), load_weights_from_unet: bool = True,
controlnet_additional_kwargs: dict = {}, ): controlnet = cls( in_channels=unet.config.in_channels, flip_sin_to_cos=unet.config.flip_sin_to_cos, freq_shift=unet.config.freq_shift, down_block_types=unet.config.down_block_types, only_cross_attention=unet.config.only_cross_attention, block_out_channels=unet.config.block_out_channels, layers_per_block=unet.config.layers_per_block, downsample_padding=unet.config.downsample_padding, mid_block_scale_factor=unet.config.mid_block_scale_factor, act_fn=unet.config.act_fn, norm_num_groups=unet.config.norm_num_groups, norm_eps=unet.config.norm_eps, cross_attention_dim=unet.config.cross_attention_dim, attention_head_dim=unet.config.attention_head_dim, num_attention_heads=unet.config.num_attention_heads, use_linear_projection=unet.config.use_linear_projection, class_embed_type=unet.config.class_embed_type, num_class_embeds=unet.config.num_class_embeds, upcast_attention=unet.config.upcast_attention, resnet_time_scale_shift=unet.config.resnet_time_scale_shift, projection_class_embeddings_input_dim=unet.config.projection_class_embeddings_input_dim, controlnet_conditioning_channel_order=controlnet_conditioning_channel_order, conditioning_embedding_out_channels=conditioning_embedding_out_channels,
**controlnet_additional_kwargs, ) controlnet.unshuffle = nn.PixelUnshuffle(8)
if load_weights_from_unet: m, u = controlnet.conv_in.load_state_dict(cls.image_layer_filter(unet.conv_in.state_dict()), strict=False) assert len(u) == 0 m, u = controlnet.time_proj.load_state_dict(cls.image_layer_filter(unet.time_proj.state_dict()), strict=False) assert len(u) == 0 m, u = controlnet.time_embedding.load_state_dict(cls.image_layer_filter(unet.time_embedding.state_dict()), strict=False) assert len(u) == 0
if controlnet.class_embedding: m, u = controlnet.class_embedding.load_state_dict(cls.image_layer_filter(unet.class_embedding.state_dict()), strict=False) assert len(u) == 0 m, u = controlnet.down_blocks.load_state_dict(cls.image_layer_filter(unet.down_blocks.state_dict()), strict=False) assert len(u) == 0 m, u = controlnet.mid_block.load_state_dict(cls.image_layer_filter(unet.mid_block.state_dict()), strict=False) assert len(u) == 0
return controlnet
@staticmethod def image_layer_filter(state_dict): new_state_dict = {} for name, param in state_dict.items(): if "motion_modules." in name or "lora" in name: continue new_state_dict[name] = param return new_state_dict
# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.set_attention_slice def set_attention_slice(self, slice_size): r""" Enable sliced attention computation.
When this option is enabled, the attention module splits the input tensor in slices to compute attention in several steps. This is useful for saving some memory in exchange for a small decrease in speed.
Args: slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`): When `"auto"`, input to the attention heads is halved, so attention is computed in two steps. If `"max"`, maximum amount of memory is saved by running only one slice at a time. If a number is provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim` must be a multiple of `slice_size`. """ sliceable_head_dims = []
def fn_recursive_retrieve_sliceable_dims(module: torch.nn.Module): if hasattr(module, "set_attention_slice"): sliceable_head_dims.append(module.sliceable_head_dim)
for child in module.children(): fn_recursive_retrieve_sliceable_dims(child)
# retrieve number of attention layers for module in self.children(): fn_recursive_retrieve_sliceable_dims(module)
num_sliceable_layers = len(sliceable_head_dims)
if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory slice_size = [dim // 2 for dim in sliceable_head_dims] elif slice_size == "max": # make smallest slice possible slice_size = num_sliceable_layers * [1]
slice_size = num_sliceable_layers * [slice_size] if not isinstance(slice_size, list) else slice_size
if len(slice_size) != len(sliceable_head_dims): raise ValueError( f"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different" f" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}." )
for i in range(len(slice_size)): size = slice_size[i] dim = sliceable_head_dims[i] if size is not None and size > dim: raise ValueError(f"size {size} has to be smaller or equal to {dim}.")
# Recursively walk through all the children. # Any children which exposes the set_attention_slice method # gets the message def fn_recursive_set_attention_slice(module: torch.nn.Module, slice_size: List[int]): if hasattr(module, "set_attention_slice"): module.set_attention_slice(slice_size.pop())
for child in module.children(): fn_recursive_set_attention_slice(child, slice_size)
reversed_slice_size = list(reversed(slice_size)) for module in self.children(): fn_recursive_set_attention_slice(module, reversed_slice_size)
def _set_gradient_checkpointing(self, module, value=False): if isinstance(module, (CrossAttnDownBlock2D, DownBlock2D)): module.gradient_checkpointing = value
def forward( self, sample: torch.FloatTensor, timestep: Union[torch.Tensor, float, int], encoder_hidden_states: torch.Tensor,
controlnet_cond: torch.FloatTensor, conditioning_mask: Optional[torch.FloatTensor] = None,
conditioning_scale: float = 1.0, class_labels: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, cross_attention_kwargs: Optional[Dict[str, Any]] = None, guess_mode: bool = False, return_dict: bool = True, ) -> Union[FlowControlNetOutput, Tuple]: # set input noise to zero if self.set_noisy_sample_input_to_zero: sample = torch.zeros_like(sample).to(sample.device) # prepare attention_mask if attention_mask is not None: attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0 attention_mask = attention_mask.unsqueeze(1)
# 1. time timesteps = timestep if not torch.is_tensor(timesteps): # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can # This would be a good case for the `match` statement (Python 3.10+) is_mps = sample.device.type == "mps" if isinstance(timestep, float): dtype = torch.float32 if is_mps else torch.float64 else: dtype = torch.int32 if is_mps else torch.int64 timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device) elif len(timesteps.shape) == 0: timesteps = timesteps[None].to(sample.device)
timesteps = timesteps.repeat(sample.shape[0] // timesteps.shape[0]) encoder_hidden_states = encoder_hidden_states.repeat(sample.shape[0] // encoder_hidden_states.shape[0], 1, 1)
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML timesteps = timesteps.expand(sample.shape[0])
t_emb = self.time_proj(timesteps)
# timesteps does not contain any weights and will always return f32 tensors # but time_embedding might actually be running in fp16. so we need to cast here. # there might be better ways to encapsulate this. t_emb = t_emb.to(dtype=self.dtype) emb = self.time_embedding(t_emb)
if self.class_embedding is not None: if class_labels is None: raise ValueError("class_labels should be provided when num_class_embeds > 0")
if self.config.class_embed_type == "timestep": class_labels = self.time_proj(class_labels)
class_emb = self.class_embedding(class_labels).to(dtype=self.dtype) emb = emb + class_emb
# 2. pre-process sample = self.conv_in(sample)
if self.concate_conditioning_mask: controlnet_cond = torch.cat([controlnet_cond, conditioning_mask], dim=1) controlnet_cond = self.unshuffle(controlnet_cond.permute(0,2,1,3,4)) controlnet_cond = controlnet_cond.contiguous().permute(0,2,1,3,4) controlnet_cond = self.controlnet_cond_embedding(controlnet_cond) sample = sample + controlnet_cond
# 3. down down_block_res_samples = (sample,) for downsample_block in self.down_blocks: if hasattr(downsample_block, "has_cross_attention") and downsample_block.has_cross_attention: sample, res_samples = downsample_block( hidden_states=sample, temb=emb, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask, # cross_attention_kwargs=cross_attention_kwargs, ) else: sample, res_samples = downsample_block(hidden_states=sample, temb=emb)
down_block_res_samples += res_samples
# 4. mid if self.mid_block is not None: sample = self.mid_block( sample, emb, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask, # cross_attention_kwargs=cross_attention_kwargs, )
# 5. controlnet blocks controlnet_down_block_res_samples = ()
for down_block_res_sample, controlnet_block in zip(down_block_res_samples, self.controlnet_down_blocks): down_block_res_sample = controlnet_block(down_block_res_sample) controlnet_down_block_res_samples = controlnet_down_block_res_samples + (down_block_res_sample,)
down_block_res_samples = controlnet_down_block_res_samples
mid_block_res_sample = self.controlnet_mid_block(sample)
# 6. scaling if guess_mode and not self.config.global_pool_conditions: scales = torch.logspace(-1, 0, len(down_block_res_samples) + 1, device=sample.device) # 0.1 to 1.0
scales = scales * conditioning_scale down_block_res_samples = [sample * scale for sample, scale in zip(down_block_res_samples, scales)] mid_block_res_sample = mid_block_res_sample * scales[-1] # last one else: down_block_res_samples = [sample * conditioning_scale for sample in down_block_res_samples] mid_block_res_sample = mid_block_res_sample * conditioning_scale
if self.config.global_pool_conditions: down_block_res_samples = [ torch.mean(sample, dim=(2, 3), keepdim=True) for sample in down_block_res_samples ] mid_block_res_sample = torch.mean(mid_block_res_sample, dim=(2, 3), keepdim=True)
if not return_dict: return (down_block_res_samples, mid_block_res_sample)
return FlowControlNetOutput( down_block_res_samples=down_block_res_samples, mid_block_res_sample=mid_block_res_sample )
def zero_module(module): for p in module.parameters(): nn.init.zeros_(p) return module123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721# Copyright 2023 The HuggingFace Team. All rights reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.## Changes were made to this source code by Yuwei Guo.from dataclasses import dataclassfrom typing import Any, Dict, List, Optional, Tuple, Union
import torchfrom diffusers import ModelMixinfrom diffusers.configuration_utils import ConfigMixin, register_to_configfrom diffusers.models.attention_processor import AttentionProcessorfrom diffusers.models.embeddings import TimestepEmbedding, Timestepsfrom diffusers.models.unet_2d_condition import UNet2DConditionModelfrom diffusers.utils import BaseOutput, loggingfrom einops import rearrange, repeatfrom torch import nnfrom torch.nn import functional as F
from .resnet import InflatedConv3dfrom .unet_blocks import ( CrossAttnDownBlock3D, DownBlock3D, UNetMidBlock3DCrossAttn, get_down_block,)
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
@dataclassclass ImageControlNetOutput(BaseOutput): down_block_res_samples: Tuple[torch.Tensor] mid_block_res_sample: torch.Tensor
class ImageControlNetConditioningEmbedding(nn.Module): def __init__( self, conditioning_embedding_channels: int, conditioning_channels: int = 3, block_out_channels: Tuple[int] = (16, 32, 96, 256), ): super().__init__()
self.conv_in = InflatedConv3d( conditioning_channels, block_out_channels[0], kernel_size=3, padding=1 )
self.blocks = nn.ModuleList([])
for i in range(len(block_out_channels) - 1): channel_in = block_out_channels[i] channel_out = block_out_channels[i + 1] self.blocks.append( InflatedConv3d(channel_in, channel_in, kernel_size=3, padding=1) ) self.blocks.append( InflatedConv3d( channel_in, channel_out, kernel_size=3, padding=1, stride=2 ) )
self.conv_out = zero_module( InflatedConv3d( block_out_channels[-1], conditioning_embedding_channels, kernel_size=3, padding=1, ) )
def forward(self, conditioning): embedding = self.conv_in(conditioning) embedding = F.silu(embedding)
for block in self.blocks: embedding = block(embedding) embedding = F.silu(embedding)
embedding = self.conv_out(embedding)
return embedding
class ImageControlNetModel(ModelMixin, ConfigMixin): _supports_gradient_checkpointing = True
@register_to_config def __init__( self, in_channels: int = 4, conditioning_channels: int = 3, flip_sin_to_cos: bool = True, freq_shift: int = 0, down_block_types: Tuple[str] = ( "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D", ), only_cross_attention: Union[bool, Tuple[bool]] = False, block_out_channels: Tuple[int] = (320, 640, 1280, 1280), layers_per_block: int = 2, downsample_padding: int = 1, mid_block_scale_factor: float = 1, act_fn: str = "silu", norm_num_groups: Optional[int] = 32, norm_eps: float = 1e-5, cross_attention_dim: int = 1280, attention_head_dim: Union[int, Tuple[int]] = 8, num_attention_heads: Optional[Union[int, Tuple[int]]] = None, use_linear_projection: bool = False, class_embed_type: Optional[str] = None, num_class_embeds: Optional[int] = None, upcast_attention: bool = False, resnet_time_scale_shift: str = "default", projection_class_embeddings_input_dim: Optional[int] = None, controlnet_conditioning_channel_order: str = "rgb", conditioning_embedding_out_channels: Optional[Tuple[int]] = (16, 32, 96, 256), global_pool_conditions: bool = False, use_motion_module=True, motion_module_resolutions=(1, 2, 4, 8), motion_module_mid_block=False, motion_module_type="Vanilla", motion_module_kwargs={ "num_attention_heads": 8, "num_transformer_block": 1, "attention_block_types": ["Temporal_Self"], "temporal_position_encoding": True, "temporal_position_encoding_max_len": 32, "temporal_attention_dim_div": 1, "causal_temporal_attention": False, }, concate_conditioning_mask: bool = True, use_simplified_condition_embedding: bool = False, set_noisy_sample_input_to_zero: bool = False, ): super().__init__()
# If `num_attention_heads` is not defined (which is the case for most models) # it will default to `attention_head_dim`. This looks weird upon first reading it and it is. # The reason for this behavior is to correct for incorrectly named variables that were introduced # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131 # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking # which is why we correct for the naming here. num_attention_heads = num_attention_heads or attention_head_dim
# Check inputs if len(block_out_channels) != len(down_block_types): raise ValueError( f"Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}." )
if not isinstance(only_cross_attention, bool) and len( only_cross_attention ) != len(down_block_types): raise ValueError( f"Must provide the same number of `only_cross_attention` as `down_block_types`. `only_cross_attention`: {only_cross_attention}. `down_block_types`: {down_block_types}." )
if not isinstance(num_attention_heads, int) and len(num_attention_heads) != len( down_block_types ): raise ValueError( f"Must provide the same number of `num_attention_heads` as `down_block_types`. `num_attention_heads`: {num_attention_heads}. `down_block_types`: {down_block_types}." )
# input self.set_noisy_sample_input_to_zero = set_noisy_sample_input_to_zero
conv_in_kernel = 3 conv_in_padding = (conv_in_kernel - 1) // 2 self.conv_in = InflatedConv3d( in_channels, block_out_channels[0], kernel_size=conv_in_kernel, padding=conv_in_padding, )
if concate_conditioning_mask: conditioning_channels = conditioning_channels + 1 self.concate_conditioning_mask = concate_conditioning_mask
# control net conditioning embedding if use_simplified_condition_embedding: self.controlnet_cond_embedding = zero_module( InflatedConv3d( conditioning_channels, block_out_channels[0], kernel_size=conv_in_kernel, padding=conv_in_padding, ) ) else: self.controlnet_cond_embedding = ImageControlNetConditioningEmbedding( conditioning_embedding_channels=block_out_channels[0], block_out_channels=conditioning_embedding_out_channels, conditioning_channels=conditioning_channels, ) self.use_simplified_condition_embedding = use_simplified_condition_embedding
# time time_embed_dim = block_out_channels[0] * 4
self.time_proj = Timesteps(block_out_channels[0], flip_sin_to_cos, freq_shift) timestep_input_dim = block_out_channels[0]
self.time_embedding = TimestepEmbedding( timestep_input_dim, time_embed_dim, act_fn=act_fn, )
# class embedding if class_embed_type is None and num_class_embeds is not None: self.class_embedding = nn.Embedding(num_class_embeds, time_embed_dim) elif class_embed_type == "timestep": self.class_embedding = TimestepEmbedding(timestep_input_dim, time_embed_dim) elif class_embed_type == "identity": self.class_embedding = nn.Identity(time_embed_dim, time_embed_dim) elif class_embed_type == "projection": if projection_class_embeddings_input_dim is None: raise ValueError( "`class_embed_type`: 'projection' requires `projection_class_embeddings_input_dim` be set" ) # The projection `class_embed_type` is the same as the timestep `class_embed_type` except # 1. the `class_labels` inputs are not first converted to sinusoidal embeddings # 2. it projects from an arbitrary input dimension. # # Note that `TimestepEmbedding` is quite general, being mainly linear layers and activations. # When used for embedding actual timesteps, the timesteps are first converted to sinusoidal embeddings. # As a result, `TimestepEmbedding` can be passed arbitrary vectors. self.class_embedding = TimestepEmbedding( projection_class_embeddings_input_dim, time_embed_dim ) else: self.class_embedding = None
self.down_blocks = nn.ModuleList([]) self.controlnet_down_blocks = nn.ModuleList([])
if isinstance(only_cross_attention, bool): only_cross_attention = [only_cross_attention] * len(down_block_types)
if isinstance(attention_head_dim, int): attention_head_dim = (attention_head_dim,) * len(down_block_types)
if isinstance(num_attention_heads, int): num_attention_heads = (num_attention_heads,) * len(down_block_types)
# down output_channel = block_out_channels[0]
controlnet_block = InflatedConv3d(output_channel, output_channel, kernel_size=1) controlnet_block = zero_module(controlnet_block) self.controlnet_down_blocks.append(controlnet_block)
for i, down_block_type in enumerate(down_block_types): res = 2**i input_channel = output_channel output_channel = block_out_channels[i] is_final_block = i == len(block_out_channels) - 1
down_block = get_down_block( down_block_type, num_layers=layers_per_block, in_channels=input_channel, out_channels=output_channel, temb_channels=time_embed_dim, add_downsample=not is_final_block, resnet_eps=norm_eps, resnet_act_fn=act_fn, resnet_groups=norm_num_groups, cross_attention_dim=cross_attention_dim, attn_num_head_channels=( attention_head_dim[i] if attention_head_dim[i] is not None else output_channel ), downsample_padding=downsample_padding, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention[i], upcast_attention=upcast_attention, resnet_time_scale_shift=resnet_time_scale_shift, use_inflated_groupnorm=True, use_motion_module=use_motion_module and (res in motion_module_resolutions), motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) self.down_blocks.append(down_block)
for _ in range(layers_per_block): controlnet_block = InflatedConv3d( output_channel, output_channel, kernel_size=1 ) controlnet_block = zero_module(controlnet_block) self.controlnet_down_blocks.append(controlnet_block)
if not is_final_block: controlnet_block = InflatedConv3d( output_channel, output_channel, kernel_size=1 ) controlnet_block = zero_module(controlnet_block) self.controlnet_down_blocks.append(controlnet_block)
# mid mid_block_channel = block_out_channels[-1]
controlnet_block = InflatedConv3d( mid_block_channel, mid_block_channel, kernel_size=1 ) controlnet_block = zero_module(controlnet_block) self.controlnet_mid_block = controlnet_block
self.mid_block = UNetMidBlock3DCrossAttn( in_channels=mid_block_channel, temb_channels=time_embed_dim, resnet_eps=norm_eps, resnet_act_fn=act_fn, output_scale_factor=mid_block_scale_factor, resnet_time_scale_shift=resnet_time_scale_shift, cross_attention_dim=cross_attention_dim, attn_num_head_channels=num_attention_heads[-1], resnet_groups=norm_num_groups, use_linear_projection=use_linear_projection, upcast_attention=upcast_attention, use_inflated_groupnorm=True, use_motion_module=use_motion_module and motion_module_mid_block, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, )
@classmethod def from_unet( cls, unet: UNet2DConditionModel, controlnet_conditioning_channel_order: str = "rgb", conditioning_embedding_out_channels: Optional[Tuple[int]] = (16, 32, 96, 256), load_weights_from_unet: bool = True, controlnet_additional_kwargs: dict = {}, ): controlnet = cls( in_channels=unet.config.in_channels, flip_sin_to_cos=unet.config.flip_sin_to_cos, freq_shift=unet.config.freq_shift, down_block_types=unet.config.down_block_types, only_cross_attention=unet.config.only_cross_attention, block_out_channels=unet.config.block_out_channels, layers_per_block=unet.config.layers_per_block, downsample_padding=unet.config.downsample_padding, mid_block_scale_factor=unet.config.mid_block_scale_factor, act_fn=unet.config.act_fn, norm_num_groups=unet.config.norm_num_groups, norm_eps=unet.config.norm_eps, cross_attention_dim=unet.config.cross_attention_dim, attention_head_dim=unet.config.attention_head_dim, num_attention_heads=unet.config.num_attention_heads, use_linear_projection=unet.config.use_linear_projection, class_embed_type=unet.config.class_embed_type, num_class_embeds=unet.config.num_class_embeds, upcast_attention=unet.config.upcast_attention, resnet_time_scale_shift=unet.config.resnet_time_scale_shift, projection_class_embeddings_input_dim=unet.config.projection_class_embeddings_input_dim, controlnet_conditioning_channel_order=controlnet_conditioning_channel_order, conditioning_embedding_out_channels=conditioning_embedding_out_channels, **controlnet_additional_kwargs, )
if load_weights_from_unet: m, u = controlnet.conv_in.load_state_dict( cls.image_layer_filter(unet.conv_in.state_dict()), strict=False ) assert len(u) == 0 m, u = controlnet.time_proj.load_state_dict( cls.image_layer_filter(unet.time_proj.state_dict()), strict=False ) assert len(u) == 0 m, u = controlnet.time_embedding.load_state_dict( cls.image_layer_filter(unet.time_embedding.state_dict()), strict=False ) assert len(u) == 0
if controlnet.class_embedding: m, u = controlnet.class_embedding.load_state_dict( cls.image_layer_filter(unet.class_embedding.state_dict()), strict=False, ) assert len(u) == 0 m, u = controlnet.down_blocks.load_state_dict( cls.image_layer_filter(unet.down_blocks.state_dict()), strict=False ) assert len(u) == 0 m, u = controlnet.mid_block.load_state_dict( cls.image_layer_filter(unet.mid_block.state_dict()), strict=False ) assert len(u) == 0
return controlnet
@staticmethod def image_layer_filter(state_dict): new_state_dict = {} for name, param in state_dict.items(): if "motion_modules." in name or "lora" in name: continue new_state_dict[name] = param return new_state_dict
# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.set_attention_slice def set_attention_slice(self, slice_size): r""" Enable sliced attention computation.
When this option is enabled, the attention module splits the input tensor in slices to compute attention in several steps. This is useful for saving some memory in exchange for a small decrease in speed.
Args: slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`): When `"auto"`, input to the attention heads is halved, so attention is computed in two steps. If `"max"`, maximum amount of memory is saved by running only one slice at a time. If a number is provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim` must be a multiple of `slice_size`. """ sliceable_head_dims = []
def fn_recursive_retrieve_sliceable_dims(module: torch.nn.Module): if hasattr(module, "set_attention_slice"): sliceable_head_dims.append(module.sliceable_head_dim)
for child in module.children(): fn_recursive_retrieve_sliceable_dims(child)
# retrieve number of attention layers for module in self.children(): fn_recursive_retrieve_sliceable_dims(module)
num_sliceable_layers = len(sliceable_head_dims)
if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory slice_size = [dim // 2 for dim in sliceable_head_dims] elif slice_size == "max": # make smallest slice possible slice_size = num_sliceable_layers * [1]
slice_size = ( num_sliceable_layers * [slice_size] if not isinstance(slice_size, list) else slice_size )
if len(slice_size) != len(sliceable_head_dims): raise ValueError( f"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different" f" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}." )
for i in range(len(slice_size)): size = slice_size[i] dim = sliceable_head_dims[i] if size is not None and size > dim: raise ValueError(f"size {size} has to be smaller or equal to {dim}.")
# Recursively walk through all the children. # Any children which exposes the set_attention_slice method # gets the message def fn_recursive_set_attention_slice( module: torch.nn.Module, slice_size: List[int] ): if hasattr(module, "set_attention_slice"): module.set_attention_slice(slice_size.pop())
for child in module.children(): fn_recursive_set_attention_slice(child, slice_size)
reversed_slice_size = list(reversed(slice_size)) for module in self.children(): fn_recursive_set_attention_slice(module, reversed_slice_size)
def _set_gradient_checkpointing(self, module, value=False): if isinstance(module, (CrossAttnDownBlock2D, DownBlock2D)): module.gradient_checkpointing = value
def forward( self, sample: torch.FloatTensor, timestep: Union[torch.Tensor, float, int], encoder_hidden_states: torch.Tensor, controlnet_cond: torch.FloatTensor, conditioning_mask: Optional[torch.FloatTensor] = None, conditioning_scale: float = 1.0, class_labels: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, cross_attention_kwargs: Optional[Dict[str, Any]] = None, guess_mode: bool = False, return_dict: bool = True, ) -> Union[ImageControlNetOutput, Tuple]:
# set input noise to zero if self.set_noisy_sample_input_to_zero: sample = torch.zeros_like(sample).to(sample.device)
# prepare attention_mask if attention_mask is not None: attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0 attention_mask = attention_mask.unsqueeze(1)
# 1. time timesteps = timestep if not torch.is_tensor(timesteps): # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can # This would be a good case for the `match` statement (Python 3.10+) is_mps = sample.device.type == "mps" if isinstance(timestep, float): dtype = torch.float32 if is_mps else torch.float64 else: dtype = torch.int32 if is_mps else torch.int64 timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device) elif len(timesteps.shape) == 0: timesteps = timesteps[None].to(sample.device)
timesteps = timesteps.repeat(sample.shape[0] // timesteps.shape[0]) encoder_hidden_states = encoder_hidden_states.repeat( sample.shape[0] // encoder_hidden_states.shape[0], 1, 1 )
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML timesteps = timesteps.expand(sample.shape[0])
t_emb = self.time_proj(timesteps)
# timesteps does not contain any weights and will always return f32 tensors # but time_embedding might actually be running in fp16. so we need to cast here. # there might be better ways to encapsulate this. t_emb = t_emb.to(dtype=self.dtype) emb = self.time_embedding(t_emb)
if self.class_embedding is not None: if class_labels is None: raise ValueError( "class_labels should be provided when num_class_embeds > 0" )
if self.config.class_embed_type == "timestep": class_labels = self.time_proj(class_labels)
class_emb = self.class_embedding(class_labels).to(dtype=self.dtype) emb = emb + class_emb
# 2. pre-process sample = self.conv_in(sample)
if self.concate_conditioning_mask: controlnet_cond = torch.cat([controlnet_cond, conditioning_mask], dim=1) controlnet_cond = self.controlnet_cond_embedding(controlnet_cond)
sample = sample + controlnet_cond
# 3. down down_block_res_samples = (sample,) for downsample_block in self.down_blocks: if ( hasattr(downsample_block, "has_cross_attention") and downsample_block.has_cross_attention ): sample, res_samples = downsample_block( hidden_states=sample, temb=emb, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask, # cross_attention_kwargs=cross_attention_kwargs, ) else: sample, res_samples = downsample_block(hidden_states=sample, temb=emb)
down_block_res_samples += res_samples
# 4. mid if self.mid_block is not None: sample = self.mid_block( sample, emb, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask, # cross_attention_kwargs=cross_attention_kwargs, )
# 5. controlnet blocks controlnet_down_block_res_samples = ()
for down_block_res_sample, controlnet_block in zip( down_block_res_samples, self.controlnet_down_blocks ): down_block_res_sample = controlnet_block(down_block_res_sample) controlnet_down_block_res_samples = controlnet_down_block_res_samples + ( down_block_res_sample, )
down_block_res_samples = controlnet_down_block_res_samples
mid_block_res_sample = self.controlnet_mid_block(sample)
# 6. scaling if guess_mode and not self.config.global_pool_conditions: scales = torch.logspace( -1, 0, len(down_block_res_samples) + 1, device=sample.device ) # 0.1 to 1.0
scales = scales * conditioning_scale down_block_res_samples = [ sample * scale for sample, scale in zip(down_block_res_samples, scales) ] mid_block_res_sample = mid_block_res_sample * scales[-1] # last one else: down_block_res_samples = [ sample * conditioning_scale for sample in down_block_res_samples ] mid_block_res_sample = mid_block_res_sample * conditioning_scale
if self.config.global_pool_conditions: down_block_res_samples = [ torch.mean(sample, dim=(2, 3), keepdim=True) for sample in down_block_res_samples ] mid_block_res_sample = torch.mean( mid_block_res_sample, dim=(2, 3), keepdim=True )
if not return_dict: return (down_block_res_samples, mid_block_res_sample)
return ImageControlNetOutput( down_block_res_samples=down_block_res_samples, mid_block_res_sample=mid_block_res_sample, )
@property def attn_processors(self) -> Dict[str, AttentionProcessor]: r""" Returns: `dict` of attention processors: A dictionary containing all attention processors used in the model with indexed by its weight name. """ # set recursively processors = {}
def fn_recursive_add_processors( name: str, module: torch.nn.Module, processors: Dict[str, AttentionProcessor], ): if hasattr(module, "set_processor"): processors[f"{name}.processor"] = module.processor
for sub_name, child in module.named_children(): if "temporal_transformer" not in sub_name: fn_recursive_add_processors(f"{name}.{sub_name}", child, processors)
return processors
for name, module in self.named_children(): if "temporal_transformer" not in name: fn_recursive_add_processors(name, module, processors)
return processors
def set_attn_processor( self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]] ): r""" Sets the attention processor to use to compute attention. Parameters: processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`): The instantiated processor class or a dictionary of processor classes that will be set as the processor for **all** `Attention` layers. If `processor` is a dict, the key needs to define the path to the corresponding cross attention processor. This is strongly recommended when setting trainable attention processors. """ count = len(self.attn_processors.keys())
if isinstance(processor, dict) and len(processor) != count: raise ValueError( f"A dict of processors was passed, but the number of processors {len(processor)} does not match the" f" number of attention layers: {count}. Please make sure to pass {count} processor classes." )
def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor): if hasattr(module, "set_processor"): if not isinstance(processor, dict): module.set_processor(processor) else: module.set_processor(processor.pop(f"{name}.processor"))
for sub_name, child in module.named_children(): if "temporal_transformer" not in sub_name: fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor)
for name, module in self.named_children(): if "temporal_transformer" not in name: fn_recursive_attn_processor(name, module, processor)
def zero_module(module): for p in module.parameters(): nn.init.zeros_(p) return module
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355import mathfrom dataclasses import dataclassfrom typing import Callable, Optional
import torchimport torch.nn.functional as Ffrom diffusers.models.attention import Attention, FeedForwardfrom diffusers.utils import BaseOutputfrom diffusers.utils.torch_utils import maybe_allow_in_graphfrom einops import rearrange, repeatfrom torch import Tensor, nn
def zero_module(module): # Zero out the parameters of a module and return it. for p in module.parameters(): p.detach().zero_() return module
@dataclassclass TemporalTransformer3DModelOutput(BaseOutput): sample: torch.FloatTensor
def get_motion_module(in_channels, motion_module_type: str, motion_module_kwargs: dict): if motion_module_type == "Vanilla": return VanillaTemporalModule( in_channels=in_channels, **motion_module_kwargs, ) else: raise ValueError
class VanillaTemporalModule(nn.Module): def __init__( self, in_channels, num_attention_heads=8, num_transformer_block=2, attention_block_types=("Temporal_Self", "Temporal_Self"), cross_frame_attention_mode=None, temporal_position_encoding=False, temporal_position_encoding_max_len=24, temporal_attention_dim_div=1, zero_initialize=True, ): super().__init__()
self.temporal_transformer = TemporalTransformer3DModel( in_channels=in_channels, num_attention_heads=num_attention_heads, attention_head_dim=in_channels // num_attention_heads // temporal_attention_dim_div, num_layers=num_transformer_block, attention_block_types=attention_block_types, cross_frame_attention_mode=cross_frame_attention_mode, temporal_position_encoding=temporal_position_encoding, temporal_position_encoding_max_len=temporal_position_encoding_max_len, )
if zero_initialize: self.temporal_transformer.proj_out = zero_module( self.temporal_transformer.proj_out ) self.skip_temporal_layers = False # Whether to skip temporal layer
def forward( self, input_tensor, temb, encoder_hidden_states, attention_mask=None, anchor_frame_idx=None, ): if self.skip_temporal_layers is True: return input_tensor
hidden_states = input_tensor hidden_states = self.temporal_transformer( hidden_states, encoder_hidden_states, attention_mask )
output = hidden_states return output
@maybe_allow_in_graphclass TemporalTransformer3DModel(nn.Module): def __init__( self, in_channels, num_attention_heads, attention_head_dim, num_layers, attention_block_types=( "Temporal_Self", "Temporal_Self", ), dropout=0.0, norm_num_groups=32, cross_attention_dim=768, activation_fn="geglu", attention_bias=False, upcast_attention=False, cross_frame_attention_mode=None, temporal_position_encoding=False, temporal_position_encoding_max_len=24, ): super().__init__()
inner_dim = num_attention_heads * attention_head_dim
self.norm = torch.nn.GroupNorm( num_groups=norm_num_groups, num_channels=in_channels, eps=1e-6, affine=True ) self.proj_in = nn.Linear(in_channels, inner_dim)
self.transformer_blocks = nn.ModuleList( [ TemporalTransformerBlock( dim=inner_dim, num_attention_heads=num_attention_heads, attention_head_dim=attention_head_dim, attention_block_types=attention_block_types, dropout=dropout, norm_num_groups=norm_num_groups, cross_attention_dim=cross_attention_dim, activation_fn=activation_fn, attention_bias=attention_bias, upcast_attention=upcast_attention, cross_frame_attention_mode=cross_frame_attention_mode, temporal_position_encoding=temporal_position_encoding, temporal_position_encoding_max_len=temporal_position_encoding_max_len, ) for d in range(num_layers) ] ) self.proj_out = nn.Linear(inner_dim, in_channels)
def forward( self, hidden_states: Tensor, encoder_hidden_states: Optional[Tensor] = None, attention_mask: Optional[Tensor] = None, ): assert ( hidden_states.dim() == 5 ), f"Expected hidden_states to have ndim=5, but got ndim={hidden_states.dim()}." video_length = hidden_states.shape[2] hidden_states = rearrange(hidden_states, "b c f h w -> (b f) c h w")
batch, channel, height, weight = hidden_states.shape residual = hidden_states
hidden_states = self.norm(hidden_states) inner_dim = hidden_states.shape[1] hidden_states = hidden_states.permute(0, 2, 3, 1).reshape( batch, height * weight, inner_dim ) hidden_states = self.proj_in(hidden_states)
# Transformer Blocks for block in self.transformer_blocks: hidden_states = block( hidden_states, encoder_hidden_states=encoder_hidden_states, video_length=video_length, )
# output hidden_states = self.proj_out(hidden_states) hidden_states = ( hidden_states.reshape(batch, height, weight, inner_dim) .permute(0, 3, 1, 2) .contiguous() )
output = hidden_states + residual output = rearrange(output, "(b f) c h w -> b c f h w", f=video_length)
return output
@maybe_allow_in_graphclass TemporalTransformerBlock(nn.Module): def __init__( self, dim: int, num_attention_heads: int, attention_head_dim: int, attention_block_types=( "Temporal_Self", "Temporal_Self", ), dropout=0.0, norm_num_groups: int = 32, cross_attention_dim: int = 768, activation_fn: str = "geglu", attention_bias: bool = False, upcast_attention: bool = False, cross_frame_attention_mode=None, temporal_position_encoding: bool = False, temporal_position_encoding_max_len: int = 24, ): super().__init__()
attention_blocks = [] norms = []
for block_name in attention_block_types: attention_blocks.append( VersatileAttention( attention_mode=block_name.split("_")[0], cross_attention_dim=( cross_attention_dim if block_name.endswith("_Cross") else None ), query_dim=dim, heads=num_attention_heads, dim_head=attention_head_dim, dropout=dropout, bias=attention_bias, upcast_attention=upcast_attention, cross_frame_attention_mode=cross_frame_attention_mode, temporal_position_encoding=temporal_position_encoding, temporal_position_encoding_max_len=temporal_position_encoding_max_len, ) ) norms.append(nn.LayerNorm(dim))
self.attention_blocks = nn.ModuleList(attention_blocks) self.norms = nn.ModuleList(norms)
self.ff = FeedForward(dim, dropout=dropout, activation_fn=activation_fn) self.ff_norm = nn.LayerNorm(dim)
def forward( self, hidden_states, encoder_hidden_states=None, attention_mask=None, video_length=None, ): for attention_block, norm in zip(self.attention_blocks, self.norms): norm_hidden_states = norm(hidden_states) hidden_states = ( attention_block( norm_hidden_states, encoder_hidden_states=( encoder_hidden_states if attention_block.is_cross_attention else None ), video_length=video_length, ) + hidden_states )
hidden_states = self.ff(self.ff_norm(hidden_states)) + hidden_states
output = hidden_states return output
class PositionalEncoding(nn.Module): def __init__(self, d_model, dropout: float = 0.0, max_len: int = 24): super().__init__() self.dropout: nn.Module = nn.Dropout(p=dropout) position = torch.arange(max_len).unsqueeze(1) div_term = torch.exp( torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model) ) pe: Tensor = torch.zeros(1, max_len, d_model) pe[0, :, 0::2] = torch.sin(position * div_term) pe[0, :, 1::2] = torch.cos(position * div_term) self.register_buffer("pe", pe)
def forward(self, x: Tensor): x = x + self.pe[:, : x.size(1)] return self.dropout(x)
@maybe_allow_in_graphclass VersatileAttention(Attention): def __init__( self, attention_mode: str = None, cross_frame_attention_mode: Optional[str] = None, temporal_position_encoding: bool = False, temporal_position_encoding_max_len: int = 24, *args, **kwargs, ): super().__init__(*args, **kwargs) if attention_mode.lower() != "temporal": raise ValueError(f"Attention mode {attention_mode} is not supported.")
self.attention_mode = attention_mode self.is_cross_attention = kwargs["cross_attention_dim"] is not None
self.pos_encoder = ( PositionalEncoding( kwargs["query_dim"], dropout=0.0, max_len=temporal_position_encoding_max_len, ) if (temporal_position_encoding and attention_mode == "Temporal") else None )
def extra_repr(self): return f"(Module Info) Attention_Mode: {self.attention_mode}, Is_Cross_Attention: {self.is_cross_attention}"
def forward( self, hidden_states: Tensor, encoder_hidden_states=None, attention_mask=None, video_length=None, ): if self.attention_mode == "Temporal": d = hidden_states.shape[1] hidden_states = rearrange( hidden_states, "(b f) d c -> (b d) f c", f=video_length )
if self.pos_encoder is not None: hidden_states = self.pos_encoder(hidden_states)
encoder_hidden_states = ( repeat(encoder_hidden_states, "b n c -> (b d) n c", d=d) if encoder_hidden_states is not None else encoder_hidden_states ) else: raise NotImplementedError
# attention processor makes this easy so that's nice hidden_states = self.processor( self, hidden_states, encoder_hidden_states, attention_mask )
if self.attention_mode == "Temporal": hidden_states = rearrange(hidden_states, "(b d) f c -> (b f) d c", d=d)
return hidden_states
def set_use_memory_efficient_attention_xformers( self, use_memory_efficient_attention_xformers: bool, attention_op: Optional[Callable] = None, ): return None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261# Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py
from typing import Optional
import torchimport torch.nn.functional as Ffrom einops import rearrangefrom torch import Tensor, nn
class InflatedConv3d(nn.Conv2d): def forward(self, x: Tensor) -> Tensor: ori_dim = x.ndim if ori_dim == 5: frames = x.shape[2] x = rearrange(x, "b c f h w -> (b f) c h w") x = F.conv2d( x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups, ) if ori_dim == 5: x = rearrange(x, "(b f) c h w -> b c f h w", f=frames) return x
class InflatedGroupNorm(nn.GroupNorm): def forward(self, x): video_length = x.shape[2]
x = rearrange(x, "b c f h w -> (b f) c h w") x = super().forward(x) x = rearrange(x, "(b f) c h w -> b c f h w", f=video_length)
return x
class Upsample3D(nn.Module): def __init__( self, channels: int, use_conv: bool = False, use_conv_transpose: bool = False, out_channels: Optional[int] = None, name="conv", ): super().__init__() self.channels = channels self.out_channels = out_channels or channels self.use_conv = use_conv self.use_conv_transpose = use_conv_transpose self.name = name
if use_conv_transpose: raise NotImplementedError elif use_conv: self.conv = InflatedConv3d(self.channels, self.out_channels, 3, padding=1)
def forward(self, hidden_states: Tensor, output_size=None): assert hidden_states.shape[1] == self.channels
if self.use_conv_transpose: raise NotImplementedError
# Cast to float32 to as 'upsample_nearest2d_out_frame' op does not support bfloat16 dtype = hidden_states.dtype if dtype == torch.bfloat16: hidden_states = hidden_states.to(torch.float32)
# upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984 if hidden_states.shape[0] >= 64: hidden_states = hidden_states.contiguous()
# if `output_size` is passed we force the interpolation output # size and do not make use of `scale_factor=2` if output_size is None: hidden_states = F.interpolate( hidden_states, scale_factor=[1.0, 2.0, 2.0], mode="nearest" ) else: hidden_states = F.interpolate( hidden_states, size=output_size, mode="nearest" )
# If the input is bfloat16, we cast back to bfloat16 if dtype == torch.bfloat16: hidden_states = hidden_states.to(dtype)
hidden_states = self.conv(hidden_states)
return hidden_states
class Downsample3D(nn.Module): def __init__( self, channels: int, use_conv: bool = False, out_channels: Optional[int] = None, padding: int = 1, name="conv", ): super().__init__() self.channels = channels self.out_channels = out_channels or channels self.use_conv = use_conv self.padding = padding stride = 2 self.name = name
if use_conv: self.conv = InflatedConv3d( self.channels, self.out_channels, 3, stride=stride, padding=padding ) else: raise NotImplementedError
def forward(self, hidden_states): assert hidden_states.shape[1] == self.channels if self.use_conv and self.padding == 0: raise NotImplementedError
assert hidden_states.shape[1] == self.channels hidden_states = self.conv(hidden_states)
return hidden_states
class ResnetBlock3D(nn.Module): def __init__( self, *, in_channels, out_channels=None, conv_shortcut=False, dropout=0.0, temb_channels=512, groups=32, groups_out=None, pre_norm=True, eps=1e-6, non_linearity="swish", time_embedding_norm="default", output_scale_factor=1.0, use_in_shortcut=None, use_inflated_groupnorm=None, ): super().__init__() self.pre_norm = pre_norm self.pre_norm = True self.in_channels = in_channels out_channels = in_channels if out_channels is None else out_channels self.out_channels = out_channels self.use_conv_shortcut = conv_shortcut self.time_embedding_norm = time_embedding_norm self.output_scale_factor = output_scale_factor
if groups_out is None: groups_out = groups
assert use_inflated_groupnorm != None if use_inflated_groupnorm: self.norm1 = InflatedGroupNorm( num_groups=groups, num_channels=in_channels, eps=eps, affine=True ) else: self.norm1 = nn.GroupNorm( num_groups=groups, num_channels=in_channels, eps=eps, affine=True )
self.conv1 = InflatedConv3d( in_channels, out_channels, kernel_size=3, stride=1, padding=1 )
if temb_channels is not None: if self.time_embedding_norm == "default": time_emb_proj_out_channels = out_channels elif self.time_embedding_norm == "scale_shift": time_emb_proj_out_channels = out_channels * 2 else: raise ValueError( f"unknown time_embedding_norm : {self.time_embedding_norm} " )
self.time_emb_proj = nn.Linear(temb_channels, time_emb_proj_out_channels) else: self.time_emb_proj = None
if use_inflated_groupnorm: self.norm2 = InflatedGroupNorm( num_groups=groups_out, num_channels=out_channels, eps=eps, affine=True ) else: self.norm2 = nn.GroupNorm( num_groups=groups_out, num_channels=out_channels, eps=eps, affine=True )
self.dropout = nn.Dropout(dropout) self.conv2 = InflatedConv3d( out_channels, out_channels, kernel_size=3, stride=1, padding=1 )
if non_linearity == "swish": self.nonlinearity = lambda x: F.silu(x) elif non_linearity == "mish": self.nonlinearity = Mish() elif non_linearity == "silu": self.nonlinearity = nn.SiLU()
self.use_in_shortcut = ( self.in_channels != self.out_channels if use_in_shortcut is None else use_in_shortcut )
self.conv_shortcut = None if self.use_in_shortcut: self.conv_shortcut = InflatedConv3d( in_channels, out_channels, kernel_size=1, stride=1, padding=0 )
def forward(self, input_tensor, temb): hidden_states = input_tensor
hidden_states = self.norm1(hidden_states) hidden_states = self.nonlinearity(hidden_states)
hidden_states = self.conv1(hidden_states)
if temb is not None: temb = self.time_emb_proj(self.nonlinearity(temb))[:, :, None, None, None]
if temb is not None and self.time_embedding_norm == "default": hidden_states = hidden_states + temb
hidden_states = self.norm2(hidden_states)
if temb is not None and self.time_embedding_norm == "scale_shift": scale, shift = torch.chunk(temb, 2, dim=1) hidden_states = hidden_states * (1 + scale) + shift
hidden_states = self.nonlinearity(hidden_states)
hidden_states = self.dropout(hidden_states) hidden_states = self.conv2(hidden_states)
if self.conv_shortcut is not None: input_tensor = self.conv_shortcut(input_tensor)
output_tensor = (input_tensor + hidden_states) / self.output_scale_factor
return output_tensor
class Mish(nn.Module): def forward(self, hidden_states): return hidden_states * torch.tanh(torch.nn.functional.softplus(hidden_states))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591# Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/unet_2d_condition.py
import jsonimport osfrom dataclasses import dataclassfrom os import PathLikefrom pathlib import Pathfrom typing import Any, Dict, List, Optional, Tuple, Union
import torchimport torch.utils.checkpointfrom diffusers.configuration_utils import ConfigMixin, register_to_configfrom diffusers.loaders import UNet2DConditionLoadersMixin, PeftAdapterMixinfrom diffusers.models import ModelMixinfrom diffusers.models.attention_processor import AttentionProcessorfrom diffusers.models.embeddings import TimestepEmbedding, Timestepsfrom diffusers.utils import SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, BaseOutput, loggingfrom safetensors.torch import load_filefrom torch import Tensor, nn
from .resnet import InflatedConv3d, InflatedGroupNormfrom .unet_blocks import ( CrossAttnDownBlock3D, CrossAttnUpBlock3D, DownBlock3D, UNetMidBlock3DCrossAttn, UpBlock3D, get_down_block, get_up_block,)
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
@dataclassclass UNet3DConditionFlowModelOutput(BaseOutput): sample: torch.FloatTensor
class UNet3DConditionFlowModel(ModelMixin, ConfigMixin, UNet2DConditionLoadersMixin, PeftAdapterMixin): _supports_gradient_checkpointing = True
@register_to_config def __init__( self, sample_size: Optional[int] = None, in_channels: int = 4, out_channels: int = 4, center_input_sample: bool = False, flip_sin_to_cos: bool = True, freq_shift: int = 0, down_block_types: Tuple[str] = ( "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D", ), mid_block_type: str = "UNetMidBlock3DCrossAttn", up_block_types: Tuple[str] = ( "UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D" ), only_cross_attention: Union[bool, Tuple[bool]] = False, block_out_channels: Tuple[int] = (320, 640, 1280, 1280), layers_per_block: int = 2, downsample_padding: int = 1, mid_block_scale_factor: float = 1, act_fn: str = "silu", norm_num_groups: int = 32, norm_eps: float = 1e-5, cross_attention_dim: int = 1280, attention_head_dim: Union[int, Tuple[int]] = 8, dual_cross_attention: bool = False, use_linear_projection: bool = False, class_embed_type: Optional[str] = None, num_class_embeds: Optional[int] = None, upcast_attention: bool = False, resnet_time_scale_shift: str = "default", use_inflated_groupnorm=False, # Additional use_motion_module = False, motion_module_resolutions = ( 1,2,4,8 ), motion_module_mid_block = False, motion_module_decoder_only = False, motion_module_type = None, motion_module_kwargs = {}, unet_use_cross_frame_attention = False, unet_use_temporal_attention = False, ): super().__init__() self.sample_size = sample_size time_embed_dim = block_out_channels[0] * 4
# input self.conv_in = InflatedConv3d(in_channels, block_out_channels[0], kernel_size=3, padding=(1, 1))
# time self.time_proj = Timesteps(block_out_channels[0], flip_sin_to_cos, freq_shift) timestep_input_dim = block_out_channels[0]
self.time_embedding = TimestepEmbedding(timestep_input_dim, time_embed_dim)
# class embedding if class_embed_type is None and num_class_embeds is not None: self.class_embedding = nn.Embedding(num_class_embeds, time_embed_dim) elif class_embed_type == "timestep": self.class_embedding = TimestepEmbedding(timestep_input_dim, time_embed_dim) elif class_embed_type == "identity": self.class_embedding = nn.Identity(time_embed_dim, time_embed_dim) else: self.class_embedding = None
self.down_blocks = nn.ModuleList([]) self.mid_block = None self.up_blocks = nn.ModuleList([])
if isinstance(only_cross_attention, bool): only_cross_attention = [only_cross_attention] * len(down_block_types)
if isinstance(attention_head_dim, int): attention_head_dim = (attention_head_dim,) * len(down_block_types)
# down output_channel = block_out_channels[0] for i, down_block_type in enumerate(down_block_types): res = 2 ** i input_channel = output_channel output_channel = block_out_channels[i] is_final_block = i == len(block_out_channels) - 1
down_block = get_down_block( down_block_type, num_layers=layers_per_block, in_channels=input_channel, out_channels=output_channel, temb_channels=time_embed_dim, add_downsample=not is_final_block, resnet_eps=norm_eps, resnet_act_fn=act_fn, resnet_groups=norm_num_groups, cross_attention_dim=cross_attention_dim, attn_num_head_channels=attention_head_dim[i], downsample_padding=downsample_padding, dual_cross_attention=dual_cross_attention, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention[i], upcast_attention=upcast_attention, resnet_time_scale_shift=resnet_time_scale_shift,
unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, use_inflated_groupnorm=use_inflated_groupnorm, use_motion_module=use_motion_module and (res in motion_module_resolutions) and (not motion_module_decoder_only), motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) self.down_blocks.append(down_block)
# mid if mid_block_type == "UNetMidBlock3DCrossAttn": self.mid_block = UNetMidBlock3DCrossAttn( in_channels=block_out_channels[-1], temb_channels=time_embed_dim, resnet_eps=norm_eps, resnet_act_fn=act_fn, output_scale_factor=mid_block_scale_factor, resnet_time_scale_shift=resnet_time_scale_shift, cross_attention_dim=cross_attention_dim, attn_num_head_channels=attention_head_dim[-1], resnet_groups=norm_num_groups, dual_cross_attention=dual_cross_attention, use_linear_projection=use_linear_projection, upcast_attention=upcast_attention,
unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, use_inflated_groupnorm=use_inflated_groupnorm, use_motion_module=use_motion_module and motion_module_mid_block, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) else: raise ValueError(f"unknown mid_block_type : {mid_block_type}") # count how many layers upsample the videos self.num_upsamplers = 0
# up reversed_block_out_channels = list(reversed(block_out_channels)) reversed_attention_head_dim = list(reversed(attention_head_dim)) only_cross_attention = list(reversed(only_cross_attention)) output_channel = reversed_block_out_channels[0] for i, up_block_type in enumerate(up_block_types): res = 2 ** (3 - i) is_final_block = i == len(block_out_channels) - 1
prev_output_channel = output_channel output_channel = reversed_block_out_channels[i] input_channel = reversed_block_out_channels[min(i + 1, len(block_out_channels) - 1)]
# add upsample block for all BUT final layer if not is_final_block: add_upsample = True self.num_upsamplers += 1 else: add_upsample = False
up_block = get_up_block( up_block_type, num_layers=layers_per_block + 1, in_channels=input_channel, out_channels=output_channel, prev_output_channel=prev_output_channel, temb_channels=time_embed_dim, add_upsample=add_upsample, resnet_eps=norm_eps, resnet_act_fn=act_fn, resnet_groups=norm_num_groups, cross_attention_dim=cross_attention_dim, attn_num_head_channels=reversed_attention_head_dim[i], dual_cross_attention=dual_cross_attention, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention[i], upcast_attention=upcast_attention, resnet_time_scale_shift=resnet_time_scale_shift,
unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, use_inflated_groupnorm=use_inflated_groupnorm,
use_motion_module=use_motion_module and (res in motion_module_resolutions), motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) self.up_blocks.append(up_block) prev_output_channel = output_channel
# out if use_inflated_groupnorm: self.conv_norm_out = InflatedGroupNorm(num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=norm_eps) else: self.conv_norm_out = nn.GroupNorm(num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=norm_eps) self.conv_act = nn.SiLU() self.conv_out = InflatedConv3d(block_out_channels[0], out_channels, kernel_size=3, padding=1)
def set_attention_slice(self, slice_size): r""" Enable sliced attention computation.
When this option is enabled, the attention module will split the input tensor in slices, to compute attention in several steps. This is useful to save some memory in exchange for a small speed decrease.
Args: slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`): When `"auto"`, halves the input to the attention heads, so attention will be computed in two steps. If `"max"`, maxium amount of memory will be saved by running only one slice at a time. If a number is provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim` must be a multiple of `slice_size`. """ sliceable_head_dims = []
def fn_recursive_retrieve_slicable_dims(module: torch.nn.Module): if hasattr(module, "set_attention_slice"): sliceable_head_dims.append(module.sliceable_head_dim)
for child in module.children(): fn_recursive_retrieve_slicable_dims(child)
# retrieve number of attention layers for module in self.children(): fn_recursive_retrieve_slicable_dims(module)
num_slicable_layers = len(sliceable_head_dims)
if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory slice_size = [dim // 2 for dim in sliceable_head_dims] elif slice_size == "max": # make smallest slice possible slice_size = num_slicable_layers * [1]
slice_size = num_slicable_layers * [slice_size] if not isinstance(slice_size, list) else slice_size
if len(slice_size) != len(sliceable_head_dims): raise ValueError( f"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different" f" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}." )
for i in range(len(slice_size)): size = slice_size[i] dim = sliceable_head_dims[i] if size is not None and size > dim: raise ValueError(f"size {size} has to be smaller or equal to {dim}.")
# Recursively walk through all the children. # Any children which exposes the set_attention_slice method # gets the message def fn_recursive_set_attention_slice(module: torch.nn.Module, slice_size: List[int]): if hasattr(module, "set_attention_slice"): module.set_attention_slice(slice_size.pop())
for child in module.children(): fn_recursive_set_attention_slice(child, slice_size)
reversed_slice_size = list(reversed(slice_size)) for module in self.children(): fn_recursive_set_attention_slice(module, reversed_slice_size)
def _set_gradient_checkpointing(self, module, value=False): if isinstance(module, (CrossAttnDownBlock3D, DownBlock3D, CrossAttnUpBlock3D, UpBlock3D)): module.gradient_checkpointing = value
def get_image_controlnet(self, controlnet_noisy_latents, timesteps, encoder_hidden_states=None, controlnet_cond=None, conditioning_mask=None, conditioning_scale=None, guess_mode=False, return_dict=False,): down_block_additional_residuals, mid_block_additional_residual = self.image_controlnet( controlnet_noisy_latents, timesteps, encoder_hidden_states=encoder_hidden_states, controlnet_cond=controlnet_cond, conditioning_mask=conditioning_mask, conditioning_scale=conditioning_scale, guess_mode=guess_mode, return_dict=return_dict, ) return down_block_additional_residuals, mid_block_additional_residual
def get_flow_controlnet(self, controlnet_noisy_latents, timesteps, encoder_hidden_states=None, controlnet_cond=None, conditioning_mask=None, conditioning_scale=None, guess_mode=False, return_dict=False,): down_block_additional_residuals, mid_block_additional_residual = self.omcm_controlnet( controlnet_noisy_latents, timesteps, encoder_hidden_states=encoder_hidden_states, controlnet_cond=controlnet_cond, conditioning_mask=conditioning_mask, conditioning_scale=conditioning_scale, guess_mode=guess_mode, return_dict=return_dict, ) return down_block_additional_residuals, mid_block_additional_residual
def forward( self, sample: torch.FloatTensor, timestep: Union[torch.Tensor, float, int], encoder_hidden_states: torch.Tensor, class_labels: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None,
# support image controlnet down_block_additional_residuals: Optional[Tuple[torch.Tensor]] = None, mid_block_additional_residual: Optional[torch.Tensor] = None,
# support flow controlnet flow_down_block_additional_residuals: Optional[Tuple[torch.Tensor]] = None, flow_mid_block_additional_residual: Optional[torch.Tensor] = None,
return_dict: bool = True, ) -> Union[UNet3DConditionFlowModelOutput, Tuple]: r""" Args: sample (`torch.FloatTensor`): (batch, channel, height, width) noisy inputs tensor timestep (`torch.FloatTensor` or `float` or `int`): (batch) timesteps encoder_hidden_states (`torch.FloatTensor`): (batch, sequence_length, feature_dim) encoder hidden states return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`models.unet_2d_condition.UNet2DConditionOutput`] instead of a plain tuple.
Returns: [`~models.unet_2d_condition.UNet2DConditionOutput`] or `tuple`: [`~models.unet_2d_condition.UNet2DConditionOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is the sample tensor. """ # By default samples have to be AT least a multiple of the overall upsampling factor. # The overall upsampling factor is equal to 2 ** (# num of upsampling layears). # However, the upsampling interpolation output size can be forced to fit any upsampling size # on the fly if necessary.
default_overall_up_factor = 2**self.num_upsamplers
# upsample size should be forwarded when sample is not a multiple of `default_overall_up_factor` forward_upsample_size = False upsample_size = None
if any(s % default_overall_up_factor != 0 for s in sample.shape[-2:]): logger.info("Forward upsample size to force interpolation output size.") forward_upsample_size = True
# prepare attention_mask if attention_mask is not None: attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0 attention_mask = attention_mask.unsqueeze(1)
# center input if necessary if self.config.center_input_sample: sample = 2 * sample - 1.0
# time timesteps = timestep if not torch.is_tensor(timesteps): # This would be a good case for the `match` statement (Python 3.10+) is_mps = sample.device.type == "mps" if isinstance(timestep, float): dtype = torch.float32 if is_mps else torch.float64 else: dtype = torch.int32 if is_mps else torch.int64 timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device) elif len(timesteps.shape) == 0: timesteps = timesteps[None].to(sample.device)
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML timesteps = timesteps.expand(sample.shape[0])
t_emb = self.time_proj(timesteps)
# timesteps does not contain any weights and will always return f32 tensors # but time_embedding might actually be running in fp16. so we need to cast here. # there might be better ways to encapsulate this. t_emb = t_emb.to(dtype=self.dtype) emb = self.time_embedding(t_emb)
if self.class_embedding is not None: if class_labels is None: raise ValueError("class_labels should be provided when num_class_embeds > 0")
if self.config.class_embed_type == "timestep": class_labels = self.time_proj(class_labels)
class_emb = self.class_embedding(class_labels).to(dtype=self.dtype) emb = emb + class_emb
# pre-process sample = self.conv_in(sample)
# down down_block_res_samples = (sample,) for downsample_block in self.down_blocks: if hasattr(downsample_block, "has_cross_attention") and downsample_block.has_cross_attention: sample, res_samples = downsample_block( hidden_states=sample, temb=emb, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask, ) else: sample, res_samples = downsample_block(hidden_states=sample, temb=emb, encoder_hidden_states=encoder_hidden_states)
down_block_res_samples += res_samples
# support controlnet # image controlnet down_block_res_samples = list(down_block_res_samples) if down_block_additional_residuals is not None: for i, down_block_additional_residual in enumerate(down_block_additional_residuals): if down_block_additional_residual.dim() == 4: # boardcast down_block_additional_residual = down_block_additional_residual.unsqueeze(2) down_block_res_samples[i] = down_block_res_samples[i] + down_block_additional_residual # flow controlnet if flow_down_block_additional_residuals is not None: for i, down_block_additional_residual in enumerate(flow_down_block_additional_residuals): if down_block_additional_residual.dim() == 4: # boardcast down_block_additional_residual = down_block_additional_residual.unsqueeze(2) down_block_res_samples[i] = down_block_res_samples[i] + down_block_additional_residual
# mid sample = self.mid_block( sample, emb, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask )
# support controlnet # image controlnet if mid_block_additional_residual is not None: if mid_block_additional_residual.dim() == 4: # boardcast mid_block_additional_residual = mid_block_additional_residual.unsqueeze(2) sample = sample + mid_block_additional_residual
# flow controlnet if flow_mid_block_additional_residual is not None: if flow_mid_block_additional_residual.dim() == 4: # boardcast flow_mid_block_additional_residual = flow_mid_block_additional_residual.unsqueeze(2) sample = sample + flow_mid_block_additional_residual
# up for i, upsample_block in enumerate(self.up_blocks): is_final_block = i == len(self.up_blocks) - 1
res_samples = down_block_res_samples[-len(upsample_block.resnets) :] down_block_res_samples = down_block_res_samples[: -len(upsample_block.resnets)]
# if we have not reached the final block and need to forward the # upsample size, we do it here if not is_final_block and forward_upsample_size: upsample_size = down_block_res_samples[-1].shape[2:]
if hasattr(upsample_block, "has_cross_attention") and upsample_block.has_cross_attention: sample = upsample_block( hidden_states=sample, temb=emb, res_hidden_states_tuple=res_samples, encoder_hidden_states=encoder_hidden_states, upsample_size=upsample_size, attention_mask=attention_mask, ) else: sample = upsample_block( hidden_states=sample, temb=emb, res_hidden_states_tuple=res_samples, upsample_size=upsample_size, encoder_hidden_states=encoder_hidden_states, )
# post-process sample = self.conv_norm_out(sample) sample = self.conv_act(sample) sample = self.conv_out(sample)
if not return_dict: return (sample,)
return UNet3DConditionFlowModelOutput(sample=sample)
@classmethod def from_pretrained_2d(cls, pretrained_model_path, subfolder=None, unet_additional_kwargs=None): if subfolder is not None: pretrained_model_path = os.path.join(pretrained_model_path, subfolder) print(f"loaded 3D unet's pretrained weights from {pretrained_model_path} ...")
config_file = os.path.join(pretrained_model_path, 'config.json') if not os.path.isfile(config_file): raise RuntimeError(f"{config_file} does not exist") with open(config_file, "r") as f: config = json.load(f) config["_class_name"] = cls.__name__ config["down_block_types"] = [ "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D" ] config["up_block_types"] = [ "UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D" ]
from diffusers.utils import WEIGHTS_NAME model = cls.from_config(config, **unet_additional_kwargs) model_file = os.path.join(pretrained_model_path, WEIGHTS_NAME) if not os.path.isfile(model_file): raise RuntimeError(f"{model_file} does not exist") state_dict = torch.load(model_file, map_location="cpu")
m, u = model.load_state_dict(state_dict, strict=False) print(f"### missing keys: {len(m)}; \n### unexpected keys: {len(u)};") motion_params = [p.numel() if "motion_modules." in n else 0 for n,p in model.named_parameters()] motion_name = [n for n in model.state_dict().keys() if "motion_modules." in n]
print(f"### Motion Module Parameters: {sum(motion_params) / 1e6} M") print(f"### Motion Module keys: {len(motion_name)}")
unnorlmal = [] for n in m: if n not in motion_name: unnorlmal.append(n) return model 'motion_modules.' in 'up_blocks.3.motion_modules.2.temporal_transformer.transformer_blocks.0.attention_blocks.1.pos_encoder.pe'123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866# Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/unet_2d_blocks.py
from typing import Any, Dict, Optional, Tuple, Union
import torchfrom torch import nn
from .attention import Transformer3DModelfrom .motion_module import get_motion_modulefrom .resnet import Downsample3D, ResnetBlock3D, Upsample3D
def get_down_block( down_block_type, num_layers, in_channels, out_channels, temb_channels, add_downsample, resnet_eps, resnet_act_fn, attn_num_head_channels, resnet_groups=None, cross_attention_dim=None, downsample_padding=None, dual_cross_attention=False, use_linear_projection=False, only_cross_attention=False, upcast_attention=False, resnet_time_scale_shift="default", unet_use_cross_frame_attention=False, unet_use_temporal_attention=False, use_inflated_groupnorm=False, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None,): down_block_type = ( down_block_type[7:] if down_block_type.startswith("UNetRes") else down_block_type ) if down_block_type == "DownBlock3D": return DownBlock3D( num_layers=num_layers, in_channels=in_channels, out_channels=out_channels, temb_channels=temb_channels, add_downsample=add_downsample, resnet_eps=resnet_eps, resnet_act_fn=resnet_act_fn, resnet_groups=resnet_groups, downsample_padding=downsample_padding, resnet_time_scale_shift=resnet_time_scale_shift, use_inflated_groupnorm=use_inflated_groupnorm, use_motion_module=use_motion_module, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) elif down_block_type == "CrossAttnDownBlock3D": if cross_attention_dim is None: raise ValueError( "cross_attention_dim must be specified for CrossAttnDownBlock3D" ) return CrossAttnDownBlock3D( num_layers=num_layers, in_channels=in_channels, out_channels=out_channels, temb_channels=temb_channels, add_downsample=add_downsample, resnet_eps=resnet_eps, resnet_act_fn=resnet_act_fn, resnet_groups=resnet_groups, downsample_padding=downsample_padding, cross_attention_dim=cross_attention_dim, attn_num_head_channels=attn_num_head_channels, dual_cross_attention=dual_cross_attention, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention, upcast_attention=upcast_attention, resnet_time_scale_shift=resnet_time_scale_shift, unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, use_inflated_groupnorm=use_inflated_groupnorm, use_motion_module=use_motion_module, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) raise ValueError(f"{down_block_type} does not exist.")
def get_up_block( up_block_type, num_layers, in_channels, out_channels, prev_output_channel, temb_channels, add_upsample, resnet_eps, resnet_act_fn, attn_num_head_channels, resnet_groups=None, cross_attention_dim=None, dual_cross_attention=False, use_linear_projection=False, only_cross_attention=False, upcast_attention=False, resnet_time_scale_shift="default", unet_use_cross_frame_attention=False, unet_use_temporal_attention=False, use_inflated_groupnorm=False, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None,): up_block_type = ( up_block_type[7:] if up_block_type.startswith("UNetRes") else up_block_type ) if up_block_type == "UpBlock3D": return UpBlock3D( num_layers=num_layers, in_channels=in_channels, out_channels=out_channels, prev_output_channel=prev_output_channel, temb_channels=temb_channels, add_upsample=add_upsample, resnet_eps=resnet_eps, resnet_act_fn=resnet_act_fn, resnet_groups=resnet_groups, resnet_time_scale_shift=resnet_time_scale_shift, use_inflated_groupnorm=use_inflated_groupnorm, use_motion_module=use_motion_module, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) elif up_block_type == "CrossAttnUpBlock3D": if cross_attention_dim is None: raise ValueError( "cross_attention_dim must be specified for CrossAttnUpBlock3D" ) return CrossAttnUpBlock3D( num_layers=num_layers, in_channels=in_channels, out_channels=out_channels, prev_output_channel=prev_output_channel, temb_channels=temb_channels, add_upsample=add_upsample, resnet_eps=resnet_eps, resnet_act_fn=resnet_act_fn, resnet_groups=resnet_groups, cross_attention_dim=cross_attention_dim, attn_num_head_channels=attn_num_head_channels, dual_cross_attention=dual_cross_attention, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention, upcast_attention=upcast_attention, resnet_time_scale_shift=resnet_time_scale_shift, unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, use_inflated_groupnorm=use_inflated_groupnorm, use_motion_module=use_motion_module, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) raise ValueError(f"{up_block_type} does not exist.")
class UNetMidBlock3DCrossAttn(nn.Module):
def __init__( self, in_channels: int, temb_channels: int, dropout: float = 0.0, num_layers: int = 1, resnet_eps: float = 1e-6, resnet_time_scale_shift: str = "default", resnet_act_fn: str = "swish", resnet_groups: int = 32, resnet_pre_norm: bool = True, attn_num_head_channels=1, output_scale_factor=1.0, cross_attention_dim=1280, dual_cross_attention=False, use_linear_projection=False, upcast_attention=False, unet_use_cross_frame_attention=False, unet_use_temporal_attention=False, use_inflated_groupnorm=False, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, ): super().__init__()
self.has_cross_attention = True self.attn_num_head_channels = attn_num_head_channels resnet_groups = ( resnet_groups if resnet_groups is not None else min(in_channels // 4, 32) )
# there is always at least one resnet resnets = [ ResnetBlock3D( in_channels=in_channels, out_channels=in_channels, temb_channels=temb_channels, eps=resnet_eps, groups=resnet_groups, dropout=dropout, time_embedding_norm=resnet_time_scale_shift, non_linearity=resnet_act_fn, output_scale_factor=output_scale_factor, pre_norm=resnet_pre_norm, use_inflated_groupnorm=use_inflated_groupnorm, ) ] attentions = [] motion_modules = []
for _ in range(num_layers): if dual_cross_attention: raise NotImplementedError attentions.append( Transformer3DModel( attn_num_head_channels, in_channels // attn_num_head_channels, in_channels=in_channels, num_layers=1, cross_attention_dim=cross_attention_dim, norm_num_groups=resnet_groups, use_linear_projection=use_linear_projection, upcast_attention=upcast_attention, unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, ) ) motion_modules.append( get_motion_module( in_channels=in_channels, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) if use_motion_module else None ) resnets.append( ResnetBlock3D( in_channels=in_channels, out_channels=in_channels, temb_channels=temb_channels, eps=resnet_eps, groups=resnet_groups, dropout=dropout, time_embedding_norm=resnet_time_scale_shift, non_linearity=resnet_act_fn, output_scale_factor=output_scale_factor, pre_norm=resnet_pre_norm, use_inflated_groupnorm=use_inflated_groupnorm, ) )
self.attentions = nn.ModuleList(attentions) self.resnets = nn.ModuleList(resnets) self.motion_modules = nn.ModuleList(motion_modules)
def forward( self, hidden_states: torch.FloatTensor, temb: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, cross_attention_kwargs: Optional[Dict[str, Any]] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, ) -> torch.FloatTensor: hidden_states = self.resnets[0](hidden_states, temb) for attn, resnet, motion_module in zip( self.attentions, self.resnets[1:], self.motion_modules ): hidden_states = attn( hidden_states, encoder_hidden_states=encoder_hidden_states, cross_attention_kwargs=cross_attention_kwargs, attention_mask=attention_mask, encoder_attention_mask=encoder_attention_mask, return_dict=False, )[0] if motion_module is not None: hidden_states = motion_module( hidden_states, temb, encoder_hidden_states=encoder_hidden_states, ) hidden_states = resnet(hidden_states, temb)
return hidden_states
class CrossAttnDownBlock3D(nn.Module):
def __init__( self, in_channels: int, out_channels: int, temb_channels: int, dropout: float = 0.0, num_layers: int = 1, transformer_layers_per_block: int = 1, resnet_eps: float = 1e-6, resnet_time_scale_shift: str = "default", resnet_act_fn: str = "swish", resnet_groups: int = 32, resnet_pre_norm: bool = True, attn_num_head_channels=1, cross_attention_dim=1280, output_scale_factor=1.0, downsample_padding=1, add_downsample=True, dual_cross_attention=False, use_linear_projection=False, only_cross_attention=False, upcast_attention=False, unet_use_cross_frame_attention=False, unet_use_temporal_attention=False, use_inflated_groupnorm=False, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, ): super().__init__() resnets = [] attentions = [] motion_modules = []
self.has_cross_attention = True self.attn_num_head_channels = attn_num_head_channels
for i in range(num_layers): in_channels = in_channels if i == 0 else out_channels resnets.append( ResnetBlock3D( in_channels=in_channels, out_channels=out_channels, temb_channels=temb_channels, eps=resnet_eps, groups=resnet_groups, dropout=dropout, time_embedding_norm=resnet_time_scale_shift, non_linearity=resnet_act_fn, output_scale_factor=output_scale_factor, pre_norm=resnet_pre_norm, use_inflated_groupnorm=use_inflated_groupnorm, ) ) if dual_cross_attention: raise NotImplementedError attentions.append( Transformer3DModel( num_attention_heads=attn_num_head_channels, attention_head_dim=out_channels // attn_num_head_channels, in_channels=out_channels, num_layers=transformer_layers_per_block, cross_attention_dim=cross_attention_dim, norm_num_groups=resnet_groups, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention, upcast_attention=upcast_attention, unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, ) ) motion_modules.append( get_motion_module( in_channels=out_channels, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) if use_motion_module else None )
self.attentions = nn.ModuleList(attentions) self.resnets = nn.ModuleList(resnets) self.motion_modules = nn.ModuleList(motion_modules)
if add_downsample: self.downsamplers = nn.ModuleList( [ Downsample3D( out_channels, use_conv=True, out_channels=out_channels, padding=downsample_padding, name="op", ) ] ) else: self.downsamplers = None
self.gradient_checkpointing = False
def forward( self, hidden_states: torch.FloatTensor, temb: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, cross_attention_kwargs: Optional[Dict[str, Any]] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, ) -> torch.FloatTensor: output_states = ()
for resnet, attn, motion_module in zip( self.resnets, self.attentions, self.motion_modules ): if self.training and self.gradient_checkpointing:
def create_custom_forward(module, return_dict=None): def custom_forward(*inputs): if return_dict is not None: return module(*inputs, return_dict=return_dict) else: return module(*inputs)
return custom_forward
hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(resnet), hidden_states, temb ) hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(attn, return_dict=False), hidden_states, encoder_hidden_states, )[0] if motion_module is not None: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states.requires_grad_(), temb, encoder_hidden_states, )
else: hidden_states = resnet(hidden_states, temb) hidden_states = attn( hidden_states, encoder_hidden_states=encoder_hidden_states, cross_attention_kwargs=cross_attention_kwargs, attention_mask=attention_mask, encoder_attention_mask=encoder_attention_mask, return_dict=False, )[0] # add motion module hidden_states = ( motion_module( hidden_states, temb, encoder_hidden_states=encoder_hidden_states ) if motion_module is not None else hidden_states )
output_states = output_states + (hidden_states,)
if self.downsamplers is not None: for downsampler in self.downsamplers: hidden_states = downsampler(hidden_states)
output_states = output_states + (hidden_states,)
return hidden_states, output_states
class DownBlock3D(nn.Module): def __init__( self, in_channels: int, out_channels: int, temb_channels: int, dropout: float = 0.0, num_layers: int = 1, resnet_eps: float = 1e-6, resnet_time_scale_shift: str = "default", resnet_act_fn: str = "swish", resnet_groups: int = 32, resnet_pre_norm: bool = True, output_scale_factor=1.0, add_downsample=True, downsample_padding=1, use_inflated_groupnorm=None, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, ): super().__init__() resnets = [] motion_modules = []
for i in range(num_layers): in_channels = in_channels if i == 0 else out_channels resnets.append( ResnetBlock3D( in_channels=in_channels, out_channels=out_channels, temb_channels=temb_channels, eps=resnet_eps, groups=resnet_groups, dropout=dropout, time_embedding_norm=resnet_time_scale_shift, non_linearity=resnet_act_fn, output_scale_factor=output_scale_factor, pre_norm=resnet_pre_norm, use_inflated_groupnorm=use_inflated_groupnorm, ) ) motion_modules.append( get_motion_module( in_channels=out_channels, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) if use_motion_module else None )
self.resnets = nn.ModuleList(resnets) self.motion_modules = nn.ModuleList(motion_modules)
if add_downsample: self.downsamplers = nn.ModuleList( [ Downsample3D( out_channels, use_conv=True, out_channels=out_channels, padding=downsample_padding, name="op", ) ] ) else: self.downsamplers = None
self.gradient_checkpointing = False
def forward(self, hidden_states, temb=None, encoder_hidden_states=None): output_states = ()
for resnet, motion_module in zip(self.resnets, self.motion_modules): if self.training and self.gradient_checkpointing:
def create_custom_forward(module): def custom_forward(*inputs): return module(*inputs)
return custom_forward
hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(resnet), hidden_states, temb ) if motion_module is not None: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states.requires_grad_(), temb, encoder_hidden_states, ) else: hidden_states = resnet(hidden_states, temb)
# add motion module if motion_module: hidden_states = motion_module( hidden_states, temb, encoder_hidden_states=encoder_hidden_states )
output_states = output_states + (hidden_states,)
if self.downsamplers is not None: for downsampler in self.downsamplers: hidden_states = downsampler(hidden_states)
output_states = output_states + (hidden_states,)
return hidden_states, output_states
class CrossAttnUpBlock3D(nn.Module):
def __init__( self, in_channels: int, out_channels: int, prev_output_channel: int, temb_channels: int, dropout: float = 0.0, num_layers: int = 1, transformer_layers_per_block: int = 1, resnet_eps: float = 1e-6, resnet_time_scale_shift: str = "default", resnet_act_fn: str = "swish", resnet_groups: int = 32, resnet_pre_norm: bool = True, attn_num_head_channels=1, cross_attention_dim=1280, output_scale_factor=1.0, add_upsample=True, dual_cross_attention=False, use_linear_projection=False, only_cross_attention=False, upcast_attention=False, unet_use_cross_frame_attention=False, unet_use_temporal_attention=False, use_inflated_groupnorm=False, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, ): super().__init__() resnets = [] attentions = [] motion_modules = []
self.has_cross_attention = True self.attn_num_head_channels = attn_num_head_channels
for i in range(num_layers): res_skip_channels = in_channels if (i == num_layers - 1) else out_channels resnet_in_channels = prev_output_channel if i == 0 else out_channels
resnets.append( ResnetBlock3D( in_channels=resnet_in_channels + res_skip_channels, out_channels=out_channels, temb_channels=temb_channels, eps=resnet_eps, groups=resnet_groups, dropout=dropout, time_embedding_norm=resnet_time_scale_shift, non_linearity=resnet_act_fn, output_scale_factor=output_scale_factor, pre_norm=resnet_pre_norm, use_inflated_groupnorm=use_inflated_groupnorm, ) ) if dual_cross_attention: raise NotImplementedError attentions.append( Transformer3DModel( attn_num_head_channels, out_channels // attn_num_head_channels, in_channels=out_channels, num_layers=transformer_layers_per_block, cross_attention_dim=cross_attention_dim, norm_num_groups=resnet_groups, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention, upcast_attention=upcast_attention, unet_use_cross_frame_attention=unet_use_cross_frame_attention, unet_use_temporal_attention=unet_use_temporal_attention, ) ) motion_modules.append( get_motion_module( in_channels=out_channels, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) if use_motion_module else None )
self.attentions = nn.ModuleList(attentions) self.resnets = nn.ModuleList(resnets) self.motion_modules = nn.ModuleList(motion_modules)
if add_upsample: self.upsamplers = nn.ModuleList( [Upsample3D(out_channels, use_conv=True, out_channels=out_channels)] ) else: self.upsamplers = None
self.gradient_checkpointing = False
def forward( self, hidden_states: torch.FloatTensor, res_hidden_states_tuple: Tuple[torch.FloatTensor, ...], temb: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, cross_attention_kwargs: Optional[Dict[str, Any]] = None, upsample_size: Optional[int] = None, attention_mask: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, ): for resnet, attn, motion_module in zip( self.resnets, self.attentions, self.motion_modules ): # pop res hidden states res_hidden_states = res_hidden_states_tuple[-1] res_hidden_states_tuple = res_hidden_states_tuple[:-1] hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1)
if self.training and self.gradient_checkpointing:
def create_custom_forward(module, return_dict=None): def custom_forward(*inputs): if return_dict is not None: return module(*inputs, return_dict=return_dict) else: return module(*inputs)
return custom_forward
hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(resnet), hidden_states, temb ) hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(attn, return_dict=False), hidden_states, encoder_hidden_states, )[0] if motion_module is not None: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states.requires_grad_(), temb, encoder_hidden_states, )
else: hidden_states = resnet(hidden_states, temb) hidden_states = attn( hidden_states, encoder_hidden_states=encoder_hidden_states, cross_attention_kwargs=cross_attention_kwargs, attention_mask=attention_mask, encoder_attention_mask=encoder_attention_mask, return_dict=False, )[0]
# add motion module if motion_module: hidden_states = motion_module( hidden_states, temb, encoder_hidden_states=encoder_hidden_states )
if self.upsamplers is not None: for upsampler in self.upsamplers: hidden_states = upsampler(hidden_states, upsample_size)
return hidden_states
class UpBlock3D(nn.Module): def __init__( self, in_channels: int, prev_output_channel: int, out_channels: int, temb_channels: int, dropout: float = 0.0, num_layers: int = 1, resnet_eps: float = 1e-6, resnet_time_scale_shift: str = "default", resnet_act_fn: str = "swish", resnet_groups: int = 32, resnet_pre_norm: bool = True, output_scale_factor=1.0, add_upsample=True, use_inflated_groupnorm=None, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, ): super().__init__() resnets = [] motion_modules = []
for i in range(num_layers): res_skip_channels = in_channels if (i == num_layers - 1) else out_channels resnet_in_channels = prev_output_channel if i == 0 else out_channels
resnets.append( ResnetBlock3D( in_channels=resnet_in_channels + res_skip_channels, out_channels=out_channels, temb_channels=temb_channels, eps=resnet_eps, groups=resnet_groups, dropout=dropout, time_embedding_norm=resnet_time_scale_shift, non_linearity=resnet_act_fn, output_scale_factor=output_scale_factor, pre_norm=resnet_pre_norm, use_inflated_groupnorm=use_inflated_groupnorm, ) ) motion_modules.append( get_motion_module( in_channels=out_channels, motion_module_type=motion_module_type, motion_module_kwargs=motion_module_kwargs, ) if use_motion_module else None )
self.resnets = nn.ModuleList(resnets) self.motion_modules = nn.ModuleList(motion_modules)
if add_upsample: self.upsamplers = nn.ModuleList( [Upsample3D(out_channels, use_conv=True, out_channels=out_channels)] ) else: self.upsamplers = None
self.gradient_checkpointing = False
def forward( self, hidden_states, res_hidden_states_tuple, temb=None, upsample_size=None, encoder_hidden_states=None, ): for resnet, motion_module in zip(self.resnets, self.motion_modules): # pop res hidden states res_hidden_states = res_hidden_states_tuple[-1] res_hidden_states_tuple = res_hidden_states_tuple[:-1] hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1)
if self.training and self.gradient_checkpointing:
def create_custom_forward(module): def custom_forward(*inputs): return module(*inputs)
return custom_forward
hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(resnet), hidden_states, temb ) if motion_module is not None: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states.requires_grad_(), temb, encoder_hidden_states, ) else: hidden_states = resnet(hidden_states, temb) if motion_module: hidden_states = motion_module( hidden_states, temb, encoder_hidden_states=encoder_hidden_states )
if self.upsamplers is not None: for upsampler in self.upsamplers: hidden_states = upsampler(hidden_states, upsample_size)
return hidden_states1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798# flake8: noqa# There's no way to ignore "F401 '...' imported but unused" warnings in this# module, but to preserve other warnings. So, don't check this module at all.
# coding=utf-8# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
__version__ = "0.11.1"
from .auto import ( AutoPeftModel, AutoPeftModelForCausalLM, AutoPeftModelForSequenceClassification, AutoPeftModelForSeq2SeqLM, AutoPeftModelForTokenClassification, AutoPeftModelForQuestionAnswering, AutoPeftModelForFeatureExtraction,)from .mapping import ( MODEL_TYPE_TO_PEFT_MODEL_MAPPING, PEFT_TYPE_TO_CONFIG_MAPPING, get_peft_config, get_peft_model, inject_adapter_in_model,)from .mixed_model import PeftMixedModelfrom .peft_model import ( PeftModel, PeftModelForCausalLM, PeftModelForSeq2SeqLM, PeftModelForSequenceClassification, PeftModelForTokenClassification, PeftModelForQuestionAnswering, PeftModelForFeatureExtraction, get_layer_status, get_model_status,)from .tuners import ( AdaptionPromptConfig, AdaptionPromptModel, LoraConfig, LoftQConfig, LoraModel, LoHaConfig, LoHaModel, LoKrConfig, LoKrModel, IA3Config, IA3Model, AdaLoraConfig, AdaLoraModel, BOFTConfig, BOFTModel, PrefixEncoder, PrefixTuningConfig, PromptEmbedding, PromptEncoder, PromptEncoderConfig, PromptEncoderReparameterizationType, PromptTuningConfig, PromptTuningInit, MultitaskPromptTuningConfig, MultitaskPromptTuningInit, OFTConfig, OFTModel, PolyConfig, PolyModel, LNTuningConfig, LNTuningModel, VeraConfig, VeraModel,)from .utils import ( TRANSFORMERS_MODELS_TO_PREFIX_TUNING_POSTPROCESS_MAPPING, PeftType, TaskType, bloom_model_postprocess_past_key_value, get_peft_model_state_dict, prepare_model_for_kbit_training, replace_lora_weights_loftq, set_peft_model_state_dict, shift_tokens_right, load_peft_weights, cast_mixed_precision_params,)from .config import PeftConfig, PromptLearningConfig123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
from __future__ import annotations
import importlibimport osfrom typing import Optional
from transformers import ( AutoModel, AutoModelForCausalLM, AutoModelForQuestionAnswering, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoTokenizer,)
from .config import PeftConfigfrom .mapping import MODEL_TYPE_TO_PEFT_MODEL_MAPPINGfrom .peft_model import ( PeftModel, PeftModelForCausalLM, PeftModelForFeatureExtraction, PeftModelForQuestionAnswering, PeftModelForSeq2SeqLM, PeftModelForSequenceClassification, PeftModelForTokenClassification,)from .utils.constants import TOKENIZER_CONFIG_NAMEfrom .utils.other import check_file_exists_on_hf_hub
class _BaseAutoPeftModel: _target_class = None _target_peft_class = None
def __init__(self, *args, **kwargs): # For consistency with transformers: https://github.com/huggingface/transformers/blob/91d7df58b6537d385e90578dac40204cb550f706/src/transformers/models/auto/auto_factory.py#L400 raise EnvironmentError( # noqa: UP024 f"{self.__class__.__name__} is designed to be instantiated " f"using the `{self.__class__.__name__}.from_pretrained(pretrained_model_name_or_path)` or " f"`{self.__class__.__name__}.from_config(config)` methods." )
@classmethod def from_pretrained( cls, pretrained_model_name_or_path, adapter_name: str = "default", is_trainable: bool = False, config: Optional[PeftConfig] = None, **kwargs, ): r""" A wrapper around all the preprocessing steps a user needs to perform in order to load a PEFT model. The kwargs are passed along to `PeftConfig` that automatically takes care of filtering the kwargs of the Hub methods and the config object init. """ peft_config = PeftConfig.from_pretrained(pretrained_model_name_or_path, **kwargs) base_model_path = peft_config.base_model_name_or_path
task_type = getattr(peft_config, "task_type", None)
if cls._target_class is not None: target_class = cls._target_class elif cls._target_class is None and task_type is not None: # this is only in the case where we use `AutoPeftModel` raise ValueError( "Cannot use `AutoPeftModel` with a task type, please use a specific class for your task type. (e.g. `AutoPeftModelForCausalLM` for `task_type='CAUSAL_LM'`)" )
if task_type is not None: expected_target_class = MODEL_TYPE_TO_PEFT_MODEL_MAPPING[task_type] if cls._target_peft_class.__name__ != expected_target_class.__name__: raise ValueError( f"Expected target PEFT class: {expected_target_class.__name__}, but you have asked for: {cls._target_peft_class.__name__ }" " make sure that you are loading the correct model for your task type." ) elif task_type is None and getattr(peft_config, "auto_mapping", None) is not None: auto_mapping = getattr(peft_config, "auto_mapping", None) base_model_class = auto_mapping["base_model_class"] parent_library_name = auto_mapping["parent_library"]
parent_library = importlib.import_module(parent_library_name) target_class = getattr(parent_library, base_model_class) else: raise ValueError( "Cannot infer the auto class from the config, please make sure that you are loading the correct model for your task type." )
base_model = target_class.from_pretrained(base_model_path, **kwargs)
tokenizer_exists = False if os.path.exists(os.path.join(pretrained_model_name_or_path, TOKENIZER_CONFIG_NAME)): tokenizer_exists = True else: token = kwargs.get("token", None) if token is None: token = kwargs.get("use_auth_token", None)
tokenizer_exists = check_file_exists_on_hf_hub( repo_id=pretrained_model_name_or_path, filename=TOKENIZER_CONFIG_NAME, revision=kwargs.get("revision", None), repo_type=kwargs.get("repo_type", None), token=token, )
if tokenizer_exists: tokenizer = AutoTokenizer.from_pretrained( pretrained_model_name_or_path, trust_remote_code=kwargs.get("trust_remote_code", False) ) base_model.resize_token_embeddings(len(tokenizer))
return cls._target_peft_class.from_pretrained( base_model, pretrained_model_name_or_path, adapter_name=adapter_name, is_trainable=is_trainable, config=config, **kwargs, )
class AutoPeftModel(_BaseAutoPeftModel): _target_class = None _target_peft_class = PeftModel
class AutoPeftModelForCausalLM(_BaseAutoPeftModel): _target_class = AutoModelForCausalLM _target_peft_class = PeftModelForCausalLM
class AutoPeftModelForSeq2SeqLM(_BaseAutoPeftModel): _target_class = AutoModelForSeq2SeqLM _target_peft_class = PeftModelForSeq2SeqLM
class AutoPeftModelForSequenceClassification(_BaseAutoPeftModel): _target_class = AutoModelForSequenceClassification _target_peft_class = PeftModelForSequenceClassification
class AutoPeftModelForTokenClassification(_BaseAutoPeftModel): _target_class = AutoModelForTokenClassification _target_peft_class = PeftModelForTokenClassification
class AutoPeftModelForQuestionAnswering(_BaseAutoPeftModel): _target_class = AutoModelForQuestionAnswering _target_peft_class = PeftModelForQuestionAnswering
class AutoPeftModelForFeatureExtraction(_BaseAutoPeftModel): _target_class = AutoModel _target_peft_class = PeftModelForFeatureExtraction123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.import inspectimport jsonimport osfrom dataclasses import asdict, dataclass, fieldfrom typing import Dict, Optional, Union
from huggingface_hub import hf_hub_downloadfrom transformers.utils import PushToHubMixin
from .utils import CONFIG_NAME, PeftType, TaskType
@dataclassclass PeftConfigMixin(PushToHubMixin): r""" This is the base configuration class for PEFT adapter models. It contains all the methods that are common to all PEFT adapter models. This class inherits from [`~transformers.utils.PushToHubMixin`] which contains the methods to push your model to the Hub. The method `save_pretrained` will save the configuration of your adapter model in a directory. The method `from_pretrained` will load the configuration of your adapter model from a directory.
Args: peft_type (Union[[`~peft.utils.config.PeftType`], `str`]): The type of Peft method to use. """
peft_type: Optional[PeftType] = field(default=None, metadata={"help": "The type of PEFT model."}) auto_mapping: Optional[dict] = field( default=None, metadata={"help": "An auto mapping dict to help retrieve the base model class if needed."} )
def to_dict(self) -> Dict: r""" Returns the configuration for your adapter model as a dictionary. """ return asdict(self)
def save_pretrained(self, save_directory: str, **kwargs) -> None: r""" This method saves the configuration of your adapter model in a directory.
Args: save_directory (`str`): The directory where the configuration will be saved. kwargs (additional keyword arguments, *optional*): Additional keyword arguments passed along to the [`~transformers.utils.PushToHubMixin.push_to_hub`] method. """ if os.path.isfile(save_directory): raise AssertionError(f"Provided path ({save_directory}) should be a directory, not a file")
os.makedirs(save_directory, exist_ok=True) auto_mapping_dict = kwargs.pop("auto_mapping_dict", None)
output_dict = asdict(self) # converting set type to list for key, value in output_dict.items(): if isinstance(value, set): output_dict[key] = list(value)
output_path = os.path.join(save_directory, CONFIG_NAME)
# Add auto mapping details for custom models. if auto_mapping_dict is not None: output_dict["auto_mapping"] = auto_mapping_dict
# save it with open(output_path, "w") as writer: writer.write(json.dumps(output_dict, indent=2, sort_keys=True))
@classmethod def from_peft_type(cls, **kwargs): r""" This method loads the configuration of your adapter model from a set of kwargs.
The appropriate configuration type is determined by the `peft_type` argument. If `peft_type` is not provided, the calling class type is instantiated.
Args: kwargs (configuration keyword arguments): Keyword arguments passed along to the configuration initialization. """ # Avoid circular dependency .. TODO: fix this with a larger refactor from peft.mapping import PEFT_TYPE_TO_CONFIG_MAPPING
# TODO: this hack is needed to fix the following issue (on commit 702f937): # if someone saves a default config and loads it back with `PeftConfig` class it yields to # not loading the correct config class.
# from peft import AdaLoraConfig, PeftConfig # peft_config = AdaLoraConfig() # print(peft_config) # >>> AdaLoraConfig(peft_type=<PeftType.ADALORA: 'ADALORA'>, auto_mapping=None, base_model_name_or_path=None, # revision=None, task_type=None, inference_mode=False, r=8, target_modules=None, lora_alpha=8, lora_dropout=0.0, ... # # peft_config.save_pretrained("./test_config") # peft_config = PeftConfig.from_pretrained("./test_config") # print(peft_config) # >>> PeftConfig(peft_type='ADALORA', auto_mapping=None, base_model_name_or_path=None, revision=None, task_type=None, inference_mode=False)
if "peft_type" in kwargs: peft_type = kwargs["peft_type"] config_cls = PEFT_TYPE_TO_CONFIG_MAPPING[peft_type] else: config_cls = cls
return config_cls(**kwargs)
@classmethod def from_pretrained(cls, pretrained_model_name_or_path: str, subfolder: Optional[str] = None, **kwargs): r""" This method loads the configuration of your adapter model from a directory.
Args: pretrained_model_name_or_path (`str`): The directory or the Hub repository id where the configuration is saved. kwargs (additional keyword arguments, *optional*): Additional keyword arguments passed along to the child class initialization. """ path = ( os.path.join(pretrained_model_name_or_path, subfolder) if subfolder is not None else pretrained_model_name_or_path )
hf_hub_download_kwargs, class_kwargs, _ = cls._split_kwargs(kwargs)
if os.path.isfile(os.path.join(path, CONFIG_NAME)): config_file = os.path.join(path, CONFIG_NAME) else: try: config_file = hf_hub_download( pretrained_model_name_or_path, CONFIG_NAME, subfolder=subfolder, **hf_hub_download_kwargs ) except Exception as exc: raise ValueError(f"Can't find '{CONFIG_NAME}' at '{pretrained_model_name_or_path}'") from exc
loaded_attributes = cls.from_json_file(config_file) kwargs = {**class_kwargs, **loaded_attributes} return cls.from_peft_type(**kwargs)
@classmethod def from_json_file(cls, path_json_file: str, **kwargs): r""" Loads a configuration file from a json file.
Args: path_json_file (`str`): The path to the json file. """ with open(path_json_file) as file: json_object = json.load(file)
return json_object
@classmethod def _split_kwargs(cls, kwargs): hf_hub_download_kwargs = {} class_kwargs = {} other_kwargs = {}
for key, value in kwargs.items(): if key in inspect.signature(hf_hub_download).parameters: hf_hub_download_kwargs[key] = value elif key in list(cls.__annotations__): class_kwargs[key] = value else: other_kwargs[key] = value
return hf_hub_download_kwargs, class_kwargs, other_kwargs
@classmethod def _get_peft_type( cls, model_id: str, **hf_hub_download_kwargs, ): subfolder = hf_hub_download_kwargs.get("subfolder", None)
path = os.path.join(model_id, subfolder) if subfolder is not None else model_id
if os.path.isfile(os.path.join(path, CONFIG_NAME)): config_file = os.path.join(path, CONFIG_NAME) else: try: config_file = hf_hub_download( model_id, CONFIG_NAME, **hf_hub_download_kwargs, ) except Exception: raise ValueError(f"Can't find '{CONFIG_NAME}' at '{model_id}'")
loaded_attributes = cls.from_json_file(config_file) return loaded_attributes["peft_type"]
@property def is_prompt_learning(self) -> bool: r""" Utility method to check if the configuration is for prompt learning. """ return False
@property def is_adaption_prompt(self) -> bool: """Return True if this is an adaption prompt config.""" return False
@dataclassclass PeftConfig(PeftConfigMixin): """ This is the base configuration class to store the configuration of a [`PeftModel`].
Args: peft_type (Union[[`~peft.utils.config.PeftType`], `str`]): The type of Peft method to use. task_type (Union[[`~peft.utils.config.TaskType`], `str`]): The type of task to perform. inference_mode (`bool`, defaults to `False`): Whether to use the Peft model in inference mode. """
base_model_name_or_path: Optional[str] = field( default=None, metadata={"help": "The name of the base model to use."} ) revision: Optional[str] = field(default=None, metadata={"help": "The specific model version to use."}) peft_type: Optional[Union[str, PeftType]] = field(default=None, metadata={"help": "Peft type"}) task_type: Optional[Union[str, TaskType]] = field(default=None, metadata={"help": "Task type"}) inference_mode: bool = field(default=False, metadata={"help": "Whether to use inference mode"})
@dataclassclass PromptLearningConfig(PeftConfig): """ This is the base configuration class to store the configuration of [`PrefixTuning`], [`PromptEncoder`], or [`PromptTuning`].
Args: num_virtual_tokens (`int`): The number of virtual tokens to use. token_dim (`int`): The hidden embedding dimension of the base transformer model. num_transformer_submodules (`int`): The number of transformer submodules in the base transformer model. num_attention_heads (`int`): The number of attention heads in the base transformer model. num_layers (`int`): The number of layers in the base transformer model. """
num_virtual_tokens: int = field(default=None, metadata={"help": "Number of virtual tokens"}) token_dim: int = field( default=None, metadata={"help": "The hidden embedding dimension of the base transformer model"} ) num_transformer_submodules: Optional[int] = field( default=None, metadata={"help": "Number of transformer submodules"} ) num_attention_heads: Optional[int] = field(default=None, metadata={"help": "Number of attention heads"}) num_layers: Optional[int] = field(default=None, metadata={"help": "Number of transformer layers"})
@property def is_prompt_learning(self) -> bool: r""" Utility method to check if the configuration is for prompt learning. """ return True123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
import inspectfrom copy import deepcopyfrom functools import update_wrapperfrom types import MethodType
from .peft_model import PeftConfig, PeftModel
def update_forward_signature(model: PeftModel) -> None: """ Updates the forward signature of the PeftModel to include parents class signature model (`PeftModel`): Peft model to update the forward signature
Example:
```python >>> from transformers import WhisperForConditionalGeneration >>> from peft import get_peft_model, LoraConfig, update_forward_signature
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en") >>> peft_config = LoraConfig(r=8, lora_alpha=32, lora_dropout=0.1, target_modules=["q_proj", "v_proj"])
>>> peft_model = get_peft_model(model, peft_config) >>> update_forward_signature(peft_model) ``` """
# Only update signature when the current forward signature only has *args and **kwargs current_signature = inspect.signature(model.forward) if ( len(current_signature.parameters) == 2 and "args" in current_signature.parameters and "kwargs" in current_signature.parameters ): forward = deepcopy(model.forward.__func__) update_wrapper( forward, type(model.get_base_model()).forward, assigned=("__doc__", "__name__", "__annotations__") ) model.forward = MethodType(forward, model)
def update_generate_signature(model: PeftModel) -> None: """ Updates the generate signature of a PeftModel with overriding generate to include parents class signature model (`PeftModel`): Peft model to update the generate signature
Example:
```python >>> from transformers import AutoModelForSeq2SeqLM, AutoTokenizer >>> from peft import get_peft_model, LoraConfig, TaskType, update_generate_signature
>>> model_name_or_path = "bigscience/mt0-large" >>> tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) >>> model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)
>>> peft_config = LoraConfig( ... task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1 ... ) >>> peft_model = get_peft_model(model, peft_config) >>> update_generate_signature(peft_model) >>> help(peft_model.generate) ``` """ if not hasattr(model, "generate"): return current_signature = inspect.signature(model.generate) if ( len(current_signature.parameters) == 2 and "args" in current_signature.parameters and "kwargs" in current_signature.parameters ) or (len(current_signature.parameters) == 1 and "kwargs" in current_signature.parameters): generate = deepcopy(model.generate.__func__) update_wrapper( generate, type(model.get_base_model()).generate, assigned=("__doc__", "__name__", "__annotations__"), ) model.generate = MethodType(generate, model)
def update_signature(model: PeftModel, method: str = "all") -> None: """ Updates the signature of a PeftModel include parents class signature for forward or generate method model (`PeftModel`): Peft model to update generate or forward signature method (`str`): method to update signature choose one of "forward", "generate", "all"
Example: ```python >>> from transformers import AutoModelForSeq2SeqLM, AutoTokenizer >>> from peft import get_peft_model, LoraConfig, TaskType, update_signature
>>> model_name_or_path = "bigscience/mt0-large" >>> tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) >>> model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)
>>> peft_config = LoraConfig( ... task_type=TaskType.SEQ_2_SEQ_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1 ... ) >>> peft_model = get_peft_model(model, peft_config) >>> update_signature(peft_model) >>> help(peft_model.generate) ``` """ if method == "forward": update_forward_signature(model) elif method == "generate": update_generate_signature(model) elif method == "all": update_forward_signature(model) update_generate_signature(model) else: raise ValueError(f"method {method} is not supported please choose one of ['forward', 'generate', 'all']")
def check_if_peft_model(model_name_or_path: str) -> bool: """ Check if the model is a PEFT model.
Args: model_name_or_path (`str`): Model id to check, can be local or on the Hugging Face Hub.
Returns: `bool`: True if the model is a PEFT model, False otherwise. """ is_peft_model = True try: PeftConfig.from_pretrained(model_name_or_path) except Exception: # allow broad exceptions so that this works even if new exceptions are added on HF Hub side is_peft_model = False
return is_peft_model1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.import importlibimport importlib.metadata as importlib_metadatafrom functools import lru_cache
import packaging.version
@lru_cachedef is_bnb_available() -> bool: return importlib.util.find_spec("bitsandbytes") is not None
@lru_cachedef is_bnb_4bit_available() -> bool: if not is_bnb_available(): return False
import bitsandbytes as bnb
return hasattr(bnb.nn, "Linear4bit")
@lru_cachedef is_auto_gptq_available(): if importlib.util.find_spec("auto_gptq") is not None: AUTOGPTQ_MINIMUM_VERSION = packaging.version.parse("0.5.0") version_autogptq = packaging.version.parse(importlib_metadata.version("auto_gptq")) if AUTOGPTQ_MINIMUM_VERSION <= version_autogptq: return True else: raise ImportError( f"Found an incompatible version of auto-gptq. Found version {version_autogptq}, " f"but only versions above {AUTOGPTQ_MINIMUM_VERSION} are supported" )
@lru_cachedef is_optimum_available() -> bool: return importlib.util.find_spec("optimum") is not None
@lru_cachedef is_torch_tpu_available(check_device=True): "Checks if `torch_xla` is installed and potentially if a TPU is in the environment" if importlib.util.find_spec("torch_xla") is not None: if check_device: # We need to check if `xla_device` can be found, will raise a RuntimeError if not try: import torch_xla.core.xla_model as xm
_ = xm.xla_device() return True except RuntimeError: return False return True return False
@lru_cachedef is_aqlm_available(): return importlib.util.find_spec("aqlm") is not None
@lru_cachedef is_auto_awq_available(): return importlib.util.find_spec("awq") is not None
@lru_cachedef is_eetq_available(): return importlib.util.find_spec("eetq") is not None
@lru_cachedef is_hqq_available(): return importlib.util.find_spec("hqq") is not None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
from __future__ import annotations
from typing import TYPE_CHECKING, Any
import torch
from .config import PeftConfigfrom .mixed_model import PeftMixedModelfrom .peft_model import ( PeftModel, PeftModelForCausalLM, PeftModelForFeatureExtraction, PeftModelForQuestionAnswering, PeftModelForSeq2SeqLM, PeftModelForSequenceClassification, PeftModelForTokenClassification,)from .tuners import ( AdaLoraConfig, AdaLoraModel, AdaptionPromptConfig, BOFTConfig, BOFTModel, IA3Config, IA3Model, LNTuningConfig, LNTuningModel, LoHaConfig, LoHaModel, LoKrConfig, LoKrModel, LoraConfig, LoraModel, MultitaskPromptTuningConfig, OFTConfig, OFTModel, PolyConfig, PolyModel, PrefixTuningConfig, PromptEncoderConfig, PromptTuningConfig, VeraConfig, VeraModel,)from .tuners.tuners_utils import BaseTuner as _BaseTunerfrom .utils import _prepare_prompt_learning_config
if TYPE_CHECKING: from transformers import PreTrainedModel
MODEL_TYPE_TO_PEFT_MODEL_MAPPING: dict[str, type[PeftModel]] = { "SEQ_CLS": PeftModelForSequenceClassification, "SEQ_2_SEQ_LM": PeftModelForSeq2SeqLM, "CAUSAL_LM": PeftModelForCausalLM, "TOKEN_CLS": PeftModelForTokenClassification, "QUESTION_ANS": PeftModelForQuestionAnswering, "FEATURE_EXTRACTION": PeftModelForFeatureExtraction,}
PEFT_TYPE_TO_CONFIG_MAPPING: dict[str, type[PeftConfig]] = { "ADAPTION_PROMPT": AdaptionPromptConfig, "PROMPT_TUNING": PromptTuningConfig, "PREFIX_TUNING": PrefixTuningConfig, "P_TUNING": PromptEncoderConfig, "LORA": LoraConfig, "LOHA": LoHaConfig, "LOKR": LoKrConfig, "ADALORA": AdaLoraConfig, "BOFT": BOFTConfig, "IA3": IA3Config, "MULTITASK_PROMPT_TUNING": MultitaskPromptTuningConfig, "OFT": OFTConfig, "POLY": PolyConfig, "LN_TUNING": LNTuningConfig, "VERA": VeraConfig,}
PEFT_TYPE_TO_TUNER_MAPPING: dict[str, type[_BaseTuner]] = { "LORA": LoraModel, "LOHA": LoHaModel, "LOKR": LoKrModel, "ADALORA": AdaLoraModel, "BOFT": BOFTModel, "IA3": IA3Model, "OFT": OFTModel, "POLY": PolyModel, "LN_TUNING": LNTuningModel, "VERA": VeraModel,}
def get_peft_config(config_dict: dict[str, Any]) -> PeftConfig: """ Returns a Peft config object from a dictionary.
Args: config_dict (`Dict[str, Any]`): Dictionary containing the configuration parameters. """
return PEFT_TYPE_TO_CONFIG_MAPPING[config_dict["peft_type"]](**config_dict)
def get_peft_model( model: PreTrainedModel, peft_config: PeftConfig, adapter_name: str = "default", mixed: bool = False) -> PeftModel | PeftMixedModel: """ Returns a Peft model object from a model and a config.
Args: model ([`transformers.PreTrainedModel`]): Model to be wrapped. peft_config ([`PeftConfig`]): Configuration object containing the parameters of the Peft model. adapter_name (`str`, `optional`, defaults to `"default"`): The name of the adapter to be injected, if not provided, the default adapter name is used ("default"). mixed (`bool`, `optional`, defaults to `False`): Whether to allow mixing different (compatible) adapter types. """ model_config = getattr(model, "config", {"model_type": "custom"}) if hasattr(model_config, "to_dict"): model_config = model_config.to_dict()
peft_config.base_model_name_or_path = model.__dict__.get("name_or_path", None)
if mixed: return PeftMixedModel(model, peft_config, adapter_name=adapter_name)
if peft_config.task_type not in MODEL_TYPE_TO_PEFT_MODEL_MAPPING.keys() and not peft_config.is_prompt_learning: return PeftModel(model, peft_config, adapter_name=adapter_name)
if peft_config.is_prompt_learning: peft_config = _prepare_prompt_learning_config(peft_config, model_config) return MODEL_TYPE_TO_PEFT_MODEL_MAPPING[peft_config.task_type](model, peft_config, adapter_name=adapter_name)
def inject_adapter_in_model( peft_config: PeftConfig, model: torch.nn.Module, adapter_name: str = "default") -> torch.nn.Module: r""" A simple API to create and inject adapter in-place into a model. Currently the API does not support prompt learning methods and adaption prompt. Make sure to have the correct `target_names` set in the `peft_config` object. The API calls `get_peft_model` under the hood but would be restricted only to non-prompt learning methods.
Args: peft_config (`PeftConfig`): Configuration object containing the parameters of the Peft model. model (`torch.nn.Module`): The input model where the adapter will be injected. adapter_name (`str`, `optional`, defaults to `"default"`): The name of the adapter to be injected, if not provided, the default adapter name is used ("default"). """ if peft_config.is_prompt_learning or peft_config.is_adaption_prompt: raise ValueError("`create_and_replace` does not support prompt learning and adaption prompt yet.")
if peft_config.peft_type not in PEFT_TYPE_TO_TUNER_MAPPING.keys(): raise ValueError( f"`inject_adapter_in_model` does not support {peft_config.peft_type} yet. Please use `get_peft_model`." )
tuner_cls = PEFT_TYPE_TO_TUNER_MAPPING[peft_config.peft_type]
# By instantiating a peft model we are injecting randomly initialized LoRA layers into the model's modules. peft_model = tuner_cls(model, peft_config, adapter_name=adapter_name)
return peft_model.model123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
from __future__ import annotations
import osfrom contextlib import contextmanagerfrom typing import Any, Optional, Union
import torchfrom accelerate.hooks import remove_hook_from_submodulesfrom torch import nnfrom transformers.utils import PushToHubMixin
from peft.tuners.mixed import COMPATIBLE_TUNER_TYPES
from .config import PeftConfigfrom .peft_model import PeftModelfrom .tuners import ( AdaLoraModel, IA3Model, LoHaModel, LoKrModel, LoraModel, MixedModel, OFTModel,)from .utils import PeftType, _set_adapter, _set_trainable
PEFT_TYPE_TO_MODEL_MAPPING = { PeftType.LORA: LoraModel, PeftType.LOHA: LoHaModel, PeftType.LOKR: LoKrModel, PeftType.ADALORA: AdaLoraModel, PeftType.IA3: IA3Model, PeftType.OFT: OFTModel,}
def _prepare_model_for_gradient_checkpointing(model: nn.Module) -> None: r""" Prepares the model for gradient checkpointing if necessary """ # Note: same as PeftModel._prepare_model_for_gradient_checkpointing if not getattr(model, "is_gradient_checkpointing", True): return model
if not ( getattr(model, "is_loaded_in_8bit", False) or getattr(model, "is_loaded_in_4bit", False) or getattr(model, "is_quantized", False) ): if hasattr(model, "enable_input_require_grads"): model.enable_input_require_grads() elif hasattr(model, "get_input_embeddings"):
def make_inputs_require_grad(module, input, output): output.requires_grad_(True)
model.get_input_embeddings().register_forward_hook(make_inputs_require_grad)
def _check_config_compatible(peft_config: PeftConfig) -> None: if peft_config.peft_type not in COMPATIBLE_TUNER_TYPES: raise ValueError( f"The provided `peft_type` '{peft_config.peft_type.value}' is not compatible with the `PeftMixedModel`. " f"Compatible types are: {COMPATIBLE_TUNER_TYPES}" )
class PeftMixedModel(PushToHubMixin, torch.nn.Module): """ PeftMixedModel for loading mixing different types of adapters for inference.
This class does not support loading/saving, and it shouldn't usually be initialized directly. Instead, use `get_peft_model` with the argument `mixed=True`.
<Tip>
Read the [Mixed adapter types](https://huggingface.co/docs/peft/en/developer_guides/mixed_models) guide to learn more about using different adapter types.
</Tip>
Example:
```py >>> from peft import get_peft_model
>>> base_model = ... # load the base model, e.g. from transformers >>> peft_model = PeftMixedModel.from_pretrained(base_model, path_to_adapter1, "adapter1").eval() >>> peft_model.load_adapter(path_to_adapter2, "adapter2") >>> peft_model.set_adapter(["adapter1", "adapter2"]) # activate both adapters >>> peft_model(data) # forward pass using both adapters ```
Args: model (`torch.nn.Module`): The model to be tuned. config (`PeftConfig`): The config of the model to be tuned. The adapter type must be compatible. adapter_name (`str`, `optional`, defaults to `"default"`): The name of the first adapter. """
def __init__(self, model: nn.Module, peft_config: PeftConfig, adapter_name: str = "default") -> None: super().__init__() _check_config_compatible(peft_config) _prepare_model_for_gradient_checkpointing(model) self.modules_to_save = None self.base_model = MixedModel(model, {adapter_name: peft_config}, adapter_name) self.set_modules_to_save(peft_config, adapter_name)
self.config = getattr(model, "config", {"model_type": "custom"})
# the `pretraining_tp` is set for some models to simulate Tensor Parallelism during inference to avoid # numerical differences, https://github.com/pytorch/pytorch/issues/76232 - to avoid any unexpected # behavior we disable that in this line. if hasattr(self.base_model, "config") and hasattr(self.base_model.config, "pretraining_tp"): self.base_model.config.pretraining_tp = 1
@property def peft_config(self) -> dict[str, PeftConfig]: return self.base_model.peft_config
@property def active_adapter(self) -> str: return self.base_model.active_adapter
@property def active_adapters(self) -> list[str]: return self.base_model.active_adapters
def get_nb_trainable_parameters(self): r""" Returns the number of trainable parameters and number of all parameters in the model. """ # note: same as PeftModel.get_nb_trainable_parameters trainable_params = 0 all_param = 0 for _, param in self.named_parameters(): num_params = param.numel() # if using DS Zero 3 and the weights are initialized empty if num_params == 0 and hasattr(param, "ds_numel"): num_params = param.ds_numel
# Due to the design of 4bit linear layers from bitsandbytes # one needs to multiply the number of parameters by 2 to get # the correct number of parameters if param.__class__.__name__ == "Params4bit": num_params = num_params * 2
all_param += num_params if param.requires_grad: trainable_params += num_params
return trainable_params, all_param
def print_trainable_parameters(self): """ Prints the number of trainable parameters in the model.
Note: print_trainable_parameters() uses get_nb_trainable_parameters() which is different from num_parameters(only_trainable=True) from huggingface/transformers. get_nb_trainable_parameters() returns (trainable parameters, all parameters) of the Peft Model which includes modified backbone transformer model. For techniques like LoRA, the backbone transformer model is modified in place with LoRA modules. However, for prompt tuning, the backbone transformer model is unmodified. num_parameters(only_trainable=True) returns number of trainable parameters of the backbone transformer model which can be different. """ # note: same as PeftModel.print_trainable_parameters trainable_params, all_param = self.get_nb_trainable_parameters()
print( f"trainable params: {trainable_params:,d} || " f"all params: {all_param:,d} || " f"trainable%: {100 * trainable_params / all_param:.4f}" )
def __getattr__(self, name: str): """Forward missing attributes to the wrapped module.""" try: return super().__getattr__(name) # defer to nn.Module's logic except AttributeError: return getattr(self.base_model, name)
def forward(self, *args: Any, **kwargs: Any): """ Forward pass of the model. """ return self.base_model(*args, **kwargs)
def generate(self, *args: Any, **kwargs: Any): """ Generate output. """ return self.base_model.generate(*args, **kwargs)
@contextmanager def disable_adapter(self): """ Disables the adapter module. """ try: self.base_model.disable_adapter_layers() yield finally: self.base_model.enable_adapter_layers()
def add_adapter(self, adapter_name: str, peft_config: PeftConfig): _check_config_compatible(peft_config)
try: self.peft_config[adapter_name] = peft_config self.base_model.inject_adapter(self, adapter_name) except Exception: # something went wrong, roll back if adapter_name in self.peft_config: del self.peft_config[adapter_name] raise
self.set_modules_to_save(peft_config, adapter_name)
def set_modules_to_save(self, peft_config: PeftConfig, adapter_name: str) -> None: if (modules_to_save := getattr(peft_config, "modules_to_save", None)) is None: return
if self.modules_to_save is None: self.modules_to_save = set(modules_to_save) else: self.modules_to_save.update(modules_to_save) _set_trainable(self, adapter_name)
def set_adapter(self, adapter_name: Union[str, list[str]]) -> None: """ Sets the active adapter(s) for the model.
Note that the order in which the adapters are applied during the forward pass may not be the same as the order in which they are passed to this function. Instead, the order during the forward pass is determined by the order in which the adapters were loaded into the model. The active adapters only determine which adapters are active during the forward pass, but not the order in which they are applied.
Additionally, this function will set the specified adapters to trainable (i.e., requires_grad=True). If this is not desired, use the following code.
```py >>> for name, param in model_peft.named_parameters(): ... if ...: # some check on name (ex. if 'lora' in name) ... param.requires_grad = False ```
Args: adapter_name (`str` or `List[str]`): The name of the adapter(s) to be activated. """ if isinstance(adapter_name, str): adapter_name = [adapter_name]
mismatched = set(adapter_name) - set(self.peft_config.keys()) if mismatched: raise ValueError( f"Adapter(s) {sorted(mismatched)} not found, available adapters: {sorted(self.peft_config.keys())}" )
self.base_model.set_adapter(adapter_name) _set_adapter(self, adapter_name)
def delete_adapter(self, adapter_name: Union[str, list[str]]) -> None: if isinstance(adapter_name, str): adapter_name = [adapter_name]
mismatched = set(adapter_name) - set(self.peft_config.keys()) if mismatched: raise ValueError( f"Adapter(s) {sorted(mismatched)} not found, available adapters: {sorted(self.peft_config.keys())}" )
self.base_model.delete_adapter(adapter_name)
def merge_and_unload(self, *args: Any, **kwargs: Any): r""" This method merges the adapter layers into the base model. This is needed if someone wants to use the base model as a standalone model.
Args: progressbar (`bool`): whether to show a progressbar indicating the unload and merge process safe_merge (`bool`): whether to activate the safe merging check to check if there is any potential Nan in the adapter weights adapter_names (`List[str]`, *optional*): The list of adapter names that should be merged. If None, all active adapters will be merged. Defaults to `None`. """ return self.base_model.merge_and_unload(*args, **kwargs)
def unload(self, *args: Any, **kwargs: Any): """ Gets back the base model by removing all the adapter modules without merging. This gives back the original base model. """ return self.base_model.unload(*args, **kwargs)
def get_layer_status(self): raise TypeError(f"get_layer_status is not supported for {self.__class__.__name__}.")
def get_model_status(self): raise TypeError(f"get_model_status is not supported for {self.__class__.__name__}.")
@classmethod def _split_kwargs(cls, kwargs: dict[str, Any]): return PeftModel._split_kwargs(kwargs)
def load_adapter(self, model_id: str, adapter_name: str, *args: Any, **kwargs: Any): output = PeftModel.load_adapter(self, model_id, adapter_name, *args, **kwargs) # TODO: not quite clear why this is necessary but tests fail without it self.set_adapter(self.active_adapters) return output
def create_or_update_model_card(self, output_dir: str): raise NotImplementedError(f"Model card creation is not supported for {self.__class__.__name__} (yet).")
def save_pretrained( self, save_directory: str, safe_serialization: bool = False, selected_adapters: Optional[list[str]] = None, **kwargs: Any, ): raise NotImplementedError(f"Saving is not supported for {self.__class__.__name__} (yet).")
@classmethod def from_pretrained( cls, model: nn.Module, model_id: str | os.PathLike, adapter_name: str = "default", is_trainable: bool = False, config: Optional[PeftConfig] = None, **kwargs: Any, ): r""" Instantiate a PEFT mixed model from a pretrained model and loaded PEFT weights.
Note that the passed `model` may be modified inplace.
Args: model (`nn.Module`): The model to be adapted. model_id (`str` or `os.PathLike`): The name of the PEFT configuration to use. Can be either: - A string, the `model id` of a PEFT configuration hosted inside a model repo on the Hugging Face Hub. - A path to a directory containing a PEFT configuration file saved using the `save_pretrained` method (`./my_peft_config_directory/`). adapter_name (`str`, *optional*, defaults to `"default"`): The name of the adapter to be loaded. This is useful for loading multiple adapters. is_trainable (`bool`, *optional*, defaults to `False`): Whether the adapter should be trainable or not. If `False`, the adapter will be frozen and use for inference config ([`~peft.PeftConfig`], *optional*): The configuration object to use instead of an automatically loaded configuration. This configuration object is mutually exclusive with `model_id` and `kwargs`. This is useful when configuration is already loaded before calling `from_pretrained`. kwargs: (`optional`): Additional keyword arguments passed along to the specific PEFT configuration class. """ # note: adapted from PeftModel.from_pretrained from .mapping import PEFT_TYPE_TO_CONFIG_MAPPING
# load the config if config is None: config = PEFT_TYPE_TO_CONFIG_MAPPING[ PeftConfig._get_peft_type( model_id, subfolder=kwargs.get("subfolder", None), revision=kwargs.get("revision", None), cache_dir=kwargs.get("cache_dir", None), use_auth_token=kwargs.get("use_auth_token", None), ) ].from_pretrained(model_id, **kwargs) elif isinstance(config, PeftConfig): config.inference_mode = not is_trainable else: raise ValueError(f"The input config must be a PeftConfig, got {config.__class__}")
# note: this is different from PeftModel.from_pretrained if config.peft_type not in PEFT_TYPE_TO_MODEL_MAPPING: raise ValueError(f"Adapter of type {config.peft_type} is not supported for mixed models.")
if (getattr(model, "hf_device_map", None) is not None) and len( set(model.hf_device_map.values()).intersection({"cpu", "disk"}) ) > 0: remove_hook_from_submodules(model)
if config.is_prompt_learning and is_trainable: # note: should not be possible to reach, but just in case raise ValueError("Cannot set a prompt learning adapter to trainable when loading pretrained adapter.") else: config.inference_mode = not is_trainable
# note: this is different from PeftModel.from_pretrained, we always return a PeftMixedModel model = cls(model, config, adapter_name) model.load_adapter(model_id, adapter_name, is_trainable=is_trainable, **kwargs) return model1234567891011121314151617181920212223242526272829303132333435# flake8: noqa# There's no way to ignore "F401 '...' imported but unused" warnings in this# module, but to preserve other warnings. So, don't check this module at all
# coding=utf-8# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
from .adaption_prompt import AdaptionPromptConfig, AdaptionPromptModelfrom .lora import LoraConfig, LoraModel, LoftQConfigfrom .loha import LoHaConfig, LoHaModelfrom .lokr import LoKrConfig, LoKrModelfrom .ia3 import IA3Config, IA3Modelfrom .adalora import AdaLoraConfig, AdaLoraModelfrom .boft import BOFTConfig, BOFTModelfrom .p_tuning import PromptEncoder, PromptEncoderConfig, PromptEncoderReparameterizationTypefrom .prefix_tuning import PrefixEncoder, PrefixTuningConfigfrom .prompt_tuning import PromptEmbedding, PromptTuningConfig, PromptTuningInitfrom .multitask_prompt_tuning import MultitaskPromptEmbedding, MultitaskPromptTuningConfig, MultitaskPromptTuningInitfrom .oft import OFTConfig, OFTModelfrom .mixed import MixedModelfrom .poly import PolyConfig, PolyModelfrom .ln_tuning import LNTuningConfig, LNTuningModelfrom .vera import VeraConfig, VeraModel12345678910111213141516171819202122232425262728293031323334353637# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
from peft.import_utils import is_bnb_4bit_available, is_bnb_available
from .config import AdaLoraConfigfrom .gptq import SVDQuantLinearfrom .layer import AdaLoraLayer, RankAllocator, SVDLinearfrom .model import AdaLoraModel
__all__ = ["AdaLoraConfig", "AdaLoraLayer", "AdaLoraModel", "SVDLinear", "RankAllocator", "SVDQuantLinear"]
def __getattr__(name): if (name == "SVDLinear8bitLt") and is_bnb_available(): from .bnb import SVDLinear8bitLt
return SVDLinear8bitLt
if (name == "SVDLinear4bit") and is_bnb_4bit_available(): from .bnb import SVDLinear4bit
return SVDLinear4bit
raise AttributeError(f"module {__name__} has no attribute {name}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145# Copyright 2023-present the HuggingFace Inc. team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
from typing import Any
import torch
from peft.import_utils import is_bnb_4bit_available, is_bnb_available
from .layer import AdaLoraLayer
if is_bnb_available():
class SVDLinear8bitLt(torch.nn.Module, AdaLoraLayer): # Low-rank matrix for SVD-based adaptation def __init__( self, base_layer: torch.nn.Module, adapter_name: str, r: int = 0, lora_alpha: int = 1, lora_dropout: float = 0.0, init_lora_weights: bool = True, **kwargs, ) -> None: super().__init__() AdaLoraLayer.__init__(self, base_layer) # Freezing the pre-trained weight matrix self.get_base_layer().weight.requires_grad = False
self._active_adapter = adapter_name self.update_layer(adapter_name, r, lora_alpha, lora_dropout, init_lora_weights)
def forward(self, x: torch.Tensor) -> torch.Tensor: # note: no check for self.merged because merging is not supported (yet) result = self.base_layer(x)
if self.disable_adapters: return result
for active_adapter in self.active_adapters: if active_adapter not in self.lora_A.keys(): continue requires_conversion = not torch.is_autocast_enabled() if requires_conversion: expected_dtype = result.dtype if x.dtype != torch.float32: x = x.float()
lora_A = self.lora_A[active_adapter] lora_B = self.lora_B[active_adapter] lora_E = self.lora_E[active_adapter] dropout = self.lora_dropout[active_adapter] scaling = self.scaling[active_adapter] ranknum = self.ranknum[active_adapter] + 1e-5
output = dropout(x) @ (lora_A * lora_E).T @ lora_B.T if requires_conversion: output = output.to(expected_dtype) output = output * scaling / ranknum # inplace operation on view is forbidden for MatMul8bitLtBackward, so avoid it result = result + output return result
def __repr__(self) -> str: rep = super().__repr__() return "adalora." + rep
if is_bnb_4bit_available():
class SVDLinear4bit(torch.nn.Module, AdaLoraLayer): # Low-rank matrix for SVD-based adaptation def __init__( self, base_layer: torch.nn.Module, adapter_name: str, r: int = 0, lora_alpha: int = 1, lora_dropout: float = 0.0, init_lora_weights: bool = True, **kwargs, ) -> None: super().__init__() AdaLoraLayer.__init__(self, base_layer) # Freezing the pre-trained weight matrix self.get_base_layer().weight.requires_grad = False
self._active_adapter = adapter_name self.update_layer(adapter_name, r, lora_alpha, lora_dropout, init_lora_weights)
def forward(self, x: torch.Tensor, *args: Any, **kwargs: Any) -> torch.Tensor: # note: no check for self.merged because merging is not supported (yet) result = self.base_layer(x, *args, **kwargs)
if self.disable_adapters: return result
# As per Tim Dettmers, for 4bit, we need to defensively clone here. # The reason is that in some cases, an error can occur that backprop # does not work on a manipulated view. This issue may be solved with # newer PyTorch versions but this would need extensive testing to be # sure. result = result.clone()
for active_adapter in self.active_adapters: if active_adapter not in self.lora_A.keys(): continue
lora_A = self.lora_A[active_adapter] lora_B = self.lora_B[active_adapter] lora_E = self.lora_E[active_adapter] dropout = self.lora_dropout[active_adapter] scaling = self.scaling[active_adapter] ranknum = self.ranknum[active_adapter] + 1e-5
requires_conversion = not torch.is_autocast_enabled() if requires_conversion: expected_dtype = result.dtype compute_dtype = lora_A.dtype if x.dtype != compute_dtype: x = x.to(compute_dtype)
output = dropout(x) @ (lora_A * lora_E).T @ lora_B.T if requires_conversion: output = output.to(expected_dtype) output = output * scaling / ranknum result += output return result
def __repr__(self) -> str: rep = super().__repr__() return "adalora." + rep.DS_StoreAdded
ImageConductor_app.pyAdded+586
app.pyAdded+577
configs/.DS_StoreAdded
configs/inference/flow_condition.yamlAdded+18
configs/inference/image_condition.yamlAdded+18
configs/inference/inference.yamlAdded+22
models/.DS_StoreAdded
modules/__pycache__/attention.cpython-310.pycAdded
modules/__pycache__/flow_controlnet.cpython-310.pycAdded
modules/__pycache__/image_controlnet.cpython-310.pycAdded
modules/__pycache__/motion_module.cpython-310.pycAdded
modules/__pycache__/resnet.cpython-310.pycAdded
modules/__pycache__/unet.cpython-310.pycAdded
modules/__pycache__/unet_blocks.cpython-310.pycAdded
modules/attention.pyAdded+396
modules/flow_controlnet.pyAdded+591
modules/image_controlnet.pyAdded+721
modules/motion_module.pyAdded+355
modules/resnet.pyAdded+261
modules/unet.pyAdded+591
modules/unet_blocks.pyAdded+866
peft/__init__.pyAdded+98
peft/__pycache__/__init__.cpython-310.pycAdded
peft/__pycache__/auto.cpython-310.pycAdded
peft/__pycache__/config.cpython-310.pycAdded
peft/__pycache__/import_utils.cpython-310.pycAdded
peft/__pycache__/mapping.cpython-310.pycAdded
peft/__pycache__/mixed_model.cpython-310.pycAdded
peft/__pycache__/peft_model.cpython-310.pycAdded
peft/auto.pyAdded+170
peft/config.pyAdded+270
peft/helpers.pyAdded+148
peft/import_utils.pyAdded+89
peft/mapping.pyAdded+181
peft/mixed_model.pyAdded+415
peft/peft_model.pyAdded
peft/py.typedAdded
peft/tuners/__init__.pyAdded+35
peft/tuners/__pycache__/__init__.cpython-310.pycAdded
peft/tuners/__pycache__/lycoris_utils.cpython-310.pycAdded
peft/tuners/__pycache__/tuners_utils.cpython-310.pycAdded
peft/tuners/adalora/__init__.pyAdded+37
peft/tuners/adalora/__pycache__/__init__.cpython-310.pycAdded
peft/tuners/adalora/__pycache__/bnb.cpython-310.pycAdded
peft/tuners/adalora/__pycache__/config.cpython-310.pycAdded
peft/tuners/adalora/__pycache__/gptq.cpython-310.pycAdded
peft/tuners/adalora/__pycache__/layer.cpython-310.pycAdded
peft/tuners/adalora/__pycache__/model.cpython-310.pycAdded
peft/tuners/adalora/bnb.pyAdded+145