diff --git a/data/reference-community.json b/data/reference-community.json index b76aab420..bc9642c60 100644 --- a/data/reference-community.json +++ b/data/reference-community.json @@ -128,5 +128,12 @@ "preview": "shuttleai--shuttle-jaguar.jpg", "tags": "community", "skip": true + }, + "Anima": { + "path": "CalamitousFelicitousness/Anima-sdnext-diffusers", + "preview": "CalamitousFelicitousness--Anima-sdnext-diffusers.png", + "desc": "Modified Cosmos-Predict-2B that replaces the T5-11B text encoder with Qwen3-0.6B. Anima is a 2 billion parameter text-to-image model created via a collaboration between CircleStone Labs and Comfy Org. It is focused mainly on anime concepts, characters, and styles, but is also capable of generating a wide variety of other non-photorealistic content. The model is designed for making illustrations and artistic images, and will not work well at realism.", + "tags": "community", + "skip": true } } diff --git a/models/Reference/CalamitousFelicitousness--Anima-sdnext-diffusers.png b/models/Reference/CalamitousFelicitousness--Anima-sdnext-diffusers.png new file mode 100755 index 000000000..70dfa2682 Binary files /dev/null and b/models/Reference/CalamitousFelicitousness--Anima-sdnext-diffusers.png differ diff --git a/modules/modeldata.py b/modules/modeldata.py index 2fcfac26a..7e2164095 100644 --- a/modules/modeldata.py +++ b/modules/modeldata.py @@ -58,7 +58,7 @@ def get_model_type(pipe): model_type = 'sana' elif "HiDream" in name: model_type = 'h1' - elif "Cosmos2TextToImage" in name: + elif "Cosmos2TextToImage" in name or "AnimaTextToImage" in name: model_type = 'cosmos' elif "FLite" in name: model_type = 'flite' diff --git a/modules/processing_args.py b/modules/processing_args.py index dc63ea84c..7a827ebea 100644 --- a/modules/processing_args.py +++ b/modules/processing_args.py @@ -269,7 +269,7 @@ def set_pipeline_args(p, model, prompts:list, negative_prompts:list, prompts_2:t kwargs['output_type'] = 'np' # only set latent if model has vae # model specific - if 'Kandinsky' in model.__class__.__name__ or 'Cosmos2' in model.__class__.__name__ or 'OmniGen2' in model.__class__.__name__: + if 'Kandinsky' in model.__class__.__name__ or 'Cosmos2' in model.__class__.__name__ or 'Anima' in model.__class__.__name__ or 'OmniGen2' in model.__class__.__name__: kwargs['output_type'] = 'np' # only set latent if model has vae if 'StableCascade' in model.__class__.__name__: kwargs.pop("guidance_scale") # remove diff --git a/modules/sd_detect.py b/modules/sd_detect.py index a1cb6e913..440b79a44 100644 --- a/modules/sd_detect.py +++ b/modules/sd_detect.py @@ -103,6 +103,8 @@ def guess_by_name(fn, current_guess): new_guess = 'FLUX' elif 'flex.2' in fn.lower(): new_guess = 'FLEX' + elif 'anima' in fn.lower() and 'animat' not in fn.lower(): + new_guess = 'Anima' elif 'cosmos-predict2' in fn.lower(): new_guess = 'Cosmos' elif 'f-lite' in fn.lower(): diff --git a/modules/sd_models.py b/modules/sd_models.py index 46548de6f..9d93ebe96 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -406,6 +406,10 @@ def load_diffuser_force(detected_model_type, checkpoint_info, diffusers_load_con from pipelines.model_cosmos import load_cosmos_t2i sd_model = load_cosmos_t2i(checkpoint_info, diffusers_load_config) allow_post_quant = False + elif model_type in ['Anima']: + from pipelines.model_anima import load_anima + sd_model = load_anima(checkpoint_info, diffusers_load_config) + allow_post_quant = False elif model_type in ['FLite']: from pipelines.model_flite import load_flite sd_model = load_flite(checkpoint_info, diffusers_load_config) @@ -1248,6 +1252,8 @@ def set_diffuser_pipe(pipe, new_pipe_type): def add_noise_pred_to_diffusers_callback(pipe): if not hasattr(pipe, "_callback_tensor_inputs"): return pipe + if pipe.__class__.__name__.startswith("Anima"): + return pipe if pipe.__class__.__name__.startswith("StableCascade") and ("predicted_image_embedding" not in pipe._callback_tensor_inputs): # pylint: disable=protected-access pipe.prior_pipe._callback_tensor_inputs.append("predicted_image_embedding") # pylint: disable=protected-access elif "noise_pred" not in pipe._callback_tensor_inputs: # pylint: disable=protected-access diff --git a/modules/shared_items.py b/modules/shared_items.py index 3177c80b8..ba5e7c038 100644 --- a/modules/shared_items.py +++ b/modules/shared_items.py @@ -64,6 +64,7 @@ pipelines = { 'X-Omni': getattr(diffusers, 'DiffusionPipeline', None), 'HunyuanImage3': getattr(diffusers, 'DiffusionPipeline', None), 'ChronoEdit': getattr(diffusers, 'DiffusionPipeline', None), + 'Anima': getattr(diffusers, 'DiffusionPipeline', None), } diff --git a/pipelines/model_anima.py b/pipelines/model_anima.py new file mode 100644 index 000000000..2937cd7e3 --- /dev/null +++ b/pipelines/model_anima.py @@ -0,0 +1,85 @@ +import sys +import importlib.util +import transformers +import diffusers +import huggingface_hub as hf +from modules import shared, devices, sd_models, model_quant, sd_hijack_te, sd_hijack_vae +from pipelines import generic + + +def _import_from_file(module_name, file_path): + spec = importlib.util.spec_from_file_location(module_name, file_path) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + +def load_anima(checkpoint_info, diffusers_load_config=None): + if diffusers_load_config is None: + diffusers_load_config = {} + repo_id = sd_models.path_to_repo(checkpoint_info) + sd_models.hf_auth_check(checkpoint_info) + + load_args, _quant_args = model_quant.get_dit_args(diffusers_load_config, allow_quant=False) + shared.log.debug(f'Load model: type=Anima repo="{repo_id}" config={diffusers_load_config} offload={shared.opts.diffusers_offload_mode} dtype={devices.dtype} args={load_args}') + + # download custom pipeline modules from repo + try: + pipeline_file = hf.hf_hub_download(repo_id, filename='pipeline.py', cache_dir=shared.opts.diffusers_dir) + adapter_file = hf.hf_hub_download(repo_id, filename='llm_adapter/modeling_llm_adapter.py', cache_dir=shared.opts.diffusers_dir) + except Exception as e: + shared.log.error(f'Load model: type=Anima failed to download custom modules: {e}') + return None + + # dynamically import custom classes and register in sys.modules so + # Diffusers' from_pretrained can resolve them via trust_remote_code + adapter_mod = _import_from_file('modeling_llm_adapter', adapter_file) + sys.modules['modeling_llm_adapter'] = adapter_mod + pipeline_mod = _import_from_file('pipeline', pipeline_file) + sys.modules['pipeline'] = pipeline_mod + AnimaTextToImagePipeline = pipeline_mod.AnimaTextToImagePipeline + AnimaLLMAdapter = adapter_mod.AnimaLLMAdapter + + # load components + transformer = generic.load_transformer(repo_id, cls_name=diffusers.CosmosTransformer3DModel, load_config=diffusers_load_config, subfolder="transformer") + text_encoder = generic.load_text_encoder(repo_id, cls_name=transformers.Qwen3Model, load_config=diffusers_load_config, subfolder="text_encoder", allow_shared=False) + + shared.state.begin('Load adapter') + try: + llm_adapter = AnimaLLMAdapter.from_pretrained( + repo_id, + subfolder="llm_adapter", + cache_dir=shared.opts.diffusers_dir, + torch_dtype=devices.dtype, + ) + except Exception as e: + shared.log.error(f'Load model: type=Anima adapter: {e}') + return None + finally: + shared.state.end() + + tokenizer = transformers.AutoTokenizer.from_pretrained(repo_id, subfolder="tokenizer", cache_dir=shared.opts.diffusers_dir) + t5_tokenizer = transformers.AutoTokenizer.from_pretrained(repo_id, subfolder="t5_tokenizer", cache_dir=shared.opts.diffusers_dir) + + # assemble pipeline + pipe = AnimaTextToImagePipeline.from_pretrained( + repo_id, + transformer=transformer, + text_encoder=text_encoder, + llm_adapter=llm_adapter, + tokenizer=tokenizer, + t5_tokenizer=t5_tokenizer, + cache_dir=shared.opts.diffusers_dir, + trust_remote_code=True, + **load_args, + ) + + del text_encoder + del transformer + del llm_adapter + + sd_hijack_te.init_hijack(pipe) + sd_hijack_vae.init_hijack(pipe) + + devices.torch_gc() + return pipe