Download run_diffusion.py from summerAIO/Diffusion-Inkling-2-mini: direct link, hf CLI and curl.
- Browser
- Download file 3.44 kB
-
https://huggingface.co/summerAIO/Diffusion-Inkling-2-mini/resolve/main/run_diffusion.py
- Command line
-
hf download hf://summerAIO/Diffusion-Inkling-2-mini/run_diffusion.py
-
curl -L -o run_diffusion.py https://huggingface.co/summerAIO/Diffusion-Inkling-2-mini/resolve/main/run_diffusion.py
3.44 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Load the merged Diffusion-Inkling-2-mini checkpoint and run masked denoising.""" | |
| import argparse | |
| from pathlib import Path | |
| import torch | |
| from transformers import AutoProcessor | |
| from transformers.models.inkling2_loop.modeling_inkling2_loop import Inkling2LoopForConditionalGeneration | |
| from block_diffusion import generate, install_future_convolutions | |
| from generation_limits import MAX_BLOCK_SIZE, MAX_DIFFUSION_CONTEXT | |
| class DiffusionInkling2Mini(Inkling2LoopForConditionalGeneration): | |
| _keep_in_fp32_modules_strict = [*(Inkling2LoopForConditionalGeneration._keep_in_fp32_modules_strict or []), | |
| 'future_taps'] | |
| def __init__(self, config): | |
| super().__init__(config) | |
| install_future_convolutions(self) | |
| def load_merged(source): | |
| model, info = DiffusionInkling2Mini.from_pretrained( | |
| str(source), device_map={'': 'cuda:0'}, dtype=torch.bfloat16, | |
| attn_implementation='eager', experts_implementation='grouped_mm', use_kernels=False, | |
| output_loading_info=True, | |
| ) | |
| if any(info.get(key) for key in ('missing_keys', 'unexpected_keys', 'mismatched_keys', 'error_msgs')): | |
| raise RuntimeError(f'Incomplete merged checkpoint load: {info}') | |
| return model.eval(), AutoProcessor.from_pretrained(str(source)) | |
| def encode_prompt(processor, prompt): | |
| rendered = processor.apply_chat_template([{'role': 'user', 'content': prompt}], tokenize=False, | |
| add_generation_prompt=False, reasoning_effort='none') | |
| return processor.tokenizer(rendered + '<|message_model|><|content_text|>', | |
| add_special_tokens=False, return_tensors='pt')['input_ids'] | |
| def generate_reply(model, processor, prompt, block_size=128, max_new_tokens=128, threshold=0.7): | |
| tokenizer = processor.tokenizer | |
| mask = tokenizer.convert_tokens_to_ids('<|unused_200054|>') | |
| stop = {tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids('<|end_message|>')} | |
| suppressed = sorted(set(tokenizer.all_special_ids) - stop) | |
| ids = encode_prompt(processor, prompt).to(model.device) | |
| if ids.shape[1] + max_new_tokens > MAX_DIFFUSION_CONTEXT or (ids == mask).any(): | |
| raise ValueError(f'Input and output budget must fit in {MAX_DIFFUSION_CONTEXT} tokens without the reserved mask.') | |
| tokens = generate(model, ids, mask, stop, max_new_tokens=max_new_tokens, threshold=threshold, | |
| confidence_order=True, guarded=True, suppressed=suppressed, block_size=block_size) | |
| return tokenizer.decode(tokens, skip_special_tokens=True) | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('prompt') | |
| parser.add_argument('--model', default=str(Path(__file__).resolve().parent)) | |
| parser.add_argument('--block-size', type=int, default=128) | |
| parser.add_argument('--max-new-tokens', type=int, default=128) | |
| parser.add_argument('--threshold', type=float, default=0.7) | |
| args = parser.parse_args() | |
| if not 1 <= args.block_size <= MAX_BLOCK_SIZE or not 0 <= args.threshold <= 1 or args.max_new_tokens < 1: | |
| parser.error('Specify a valid block size, confidence threshold, and reply budget.') | |
| model, processor = load_merged(args.model) | |
| print(generate_reply(model, processor, args.prompt, args.block_size, args.max_new_tokens, args.threshold)) | |
| if __name__ == '__main__': | |
| main() | |