Diffusers Code Implementation And PR Skill
Use this skill to implement, edit, review, and prepare pull-request-ready changes for the diffusers library with high compliance to diffusers conventions.
When To Use
- Adding or editing diffusers models, pipelines, schedulers, or loaders
- Fixing bugs in inference code paths in diffusers-compatible style
- Refactoring existing diffusers code while preserving behavior
- Adding tests and docs for diffusers changes
- Preparing a PR that targets the diffusers repository
Guidance
- Consult
.github/instructions/core.instructions.md for relevant core runtime and diffusers integration guidance before proceeding.
Primary Objectives
- Keep behavior explicit, minimal, and inference-focused.
- Match existing diffusers architecture and code patterns.
- Preserve numerical behavior unless a behavior change is explicitly documented in the task requirements.
- Produce change sets that are clean, reviewable, and PR-ready.
Hard Rules
- Keep logic simple and readable in the main forward or call path.
- Avoid defensive, speculative, or fallback code paths unless required by existing diffusers APIs.
- Do not silently guess intent. For unsupported inputs, raise concise errors.
- Do not introduce new mandatory dependencies without maintainer agreement.
- If optional dependencies are needed, guard imports and provide proper dummy paths.
- Keep implementation torch.compile-friendly: avoid graph-break patterns in core model paths.
- Prefer native PyTorch tensor ops over external reshape helpers.
Code Structure Rules
Apply these grouped checks in priority order:
- Model-level structure and forward-path clarity.
- Attention and processor integration consistency.
- Pipeline runtime behavior and inference API expectations.
- Scheduler config and mixin conformance.
Models
- Use ModelMixin patterns and register constructor args with register_to_config.
- Keep layer invocation visible in forward, avoid hiding key module calls in extra helpers.
- Avoid hardcoded dtypes in forward paths; infer from tensors or module dtype.
- Follow existing model family patterns in src/diffusers/models/transformers.
Attention
- Keep Attention class and processor together in the model file when following the standard diffusers pattern.
- Processor should perform the compute path and use dispatch_attention_fn pattern where applicable.
- Ensure processor registration and available processor declarations are complete.
Pipelines
- Inherit from DiffusionPipeline.
- Decorate inference call with @torch.no_grad().
- Support generator for reproducibility when workflow requires it.
- Support output_type="latent" where latent output skip is expected.
- Use self.progress_bar(timesteps) in denoising loops.
- Do not build variant behavior by subclassing an unrelated existing pipeline class.
Schedulers
- Use SchedulerMixin and ConfigMixin.
- Keep scheduler config semantics consistent with existing scheduler implementations.
Import And Registration Rules
- Register new classes in relevant init.py lazy import structures.
- Ensure import structure entries are complete for all newly exposed objects.
- Validate that public imports from diffusers work after edits.
Copied Code Rules
- Respect # Copied from linkage.
- Do not manually diverge copied blocks unless intentionally breaking linkage.
- Run make fix-copies after changes that touch copied sources or copied blocks.
Change Workflow
- Gather context
- Confirm target files, model family, and expected behavior.
- Obtain reference implementation and runnable inference flow when porting.
- Plan minimal scope
- Separate structural adaptation from algorithmic changes.
- Keep one coherent workflow per change set.
- Implement
- Edit only required files.
- Preserve naming, config shape, and API contracts unless change requires otherwise.
- Validate
- Run focused tests first, then broader checks as needed.
- Confirm imports and serialization/deserialization behavior.
- Polish
- Run make style.
- Run make fix-copies.
- Re-run impacted tests.
Testing Expectations
Include tests for the exact behavior being changed:
- Model tests for shape, dtype/device behavior, serialization, and config parity
- Pipeline tests for deterministic generation paths, outputs, and parameter handling
- Scheduler tests when scheduler logic or config behavior changes
- Regression tests for any bug fix
When parity with a reference implementation is required:
- Add component-level parity checks
- Add end-to-end parity checks
- Use explicit tolerances and deterministic seeds
PR Preparation For Diffusers
When asked to prepare a PR targeting diffusers, produce:
- Scope statement
- One-paragraph summary: problem, solution, and non-goals.
- Change map
- File-by-file list describing what changed and why.
- Validation evidence
- Commands run, tests passed, and any skipped tests with reasons.
- Compatibility notes
- Backward compatibility, serialization impact, and optional dependency impact.
- Reviewer guidance
- Key files to review first, known tradeoffs, and follow-up items.
PR Quality Checklist
Common Failure Modes To Prevent
- Missing lazy import registration causes runtime ImportError
- New config params not registered, causing from_pretrained mismatch
- Pipeline call missing @torch.no_grad(), causing memory growth
- Hardcoded dtype assumptions break mixed precision usage
- Hidden behavior changes introduced during structural refactor
- Unnecessary dependency additions for simple tensor reshaping
Output Contract For This Skill
When using this skill, provide:
- Implementation summary
- Exact files changed
- Validation summary with command outcomes
- Residual risks or deferred follow-ups
- PR-ready summary text when requested
1---2name: diffusers-code3description: Create or edit code that is compliant with Hugging Face diffusers conventions, including models, pipelines, schedulers, tests, docs, and PR preparation targeting diffusers.4---56# Diffusers Code Implementation And PR Skill78Use this skill to implement, edit, review, and prepare pull-request-ready changes for the diffusers library with high compliance to diffusers conventions.910## When To Use1112- Adding or editing diffusers models, pipelines, schedulers, or loaders13- Fixing bugs in inference code paths in diffusers-compatible style14- Refactoring existing diffusers code while preserving behavior15- Adding tests and docs for diffusers changes16- Preparing a PR that targets the diffusers repository1718## Guidance1920- Consult `.github/instructions/core.instructions.md` for relevant core runtime and diffusers integration guidance before proceeding.2122## Primary Objectives23241. Keep behavior explicit, minimal, and inference-focused.252. Match existing diffusers architecture and code patterns.263. Preserve numerical behavior unless a behavior change is explicitly documented in the task requirements.274. Produce change sets that are clean, reviewable, and PR-ready.2829## Hard Rules3031- Keep logic simple and readable in the main forward or call path.32- Avoid defensive, speculative, or fallback code paths unless required by existing diffusers APIs.33- Do not silently guess intent. For unsupported inputs, raise concise errors.34- Do not introduce new mandatory dependencies without maintainer agreement.35- If optional dependencies are needed, guard imports and provide proper dummy paths.36- Keep implementation torch.compile-friendly: avoid graph-break patterns in core model paths.37- Prefer native PyTorch tensor ops over external reshape helpers.3839## Code Structure Rules4041Apply these grouped checks in priority order:42431. Model-level structure and forward-path clarity.442. Attention and processor integration consistency.453. Pipeline runtime behavior and inference API expectations.464. Scheduler config and mixin conformance.4748### Models4950- Use ModelMixin patterns and register constructor args with register_to_config.51- Keep layer invocation visible in forward, avoid hiding key module calls in extra helpers.52- Avoid hardcoded dtypes in forward paths; infer from tensors or module dtype.53- Follow existing model family patterns in src/diffusers/models/transformers.5455### Attention5657- Keep Attention class and processor together in the model file when following the standard diffusers pattern.58- Processor should perform the compute path and use dispatch_attention_fn pattern where applicable.59- Ensure processor registration and available processor declarations are complete.6061### Pipelines6263- Inherit from DiffusionPipeline.64- Decorate inference __call__ with @torch.no_grad().65- Support generator for reproducibility when workflow requires it.66- Support output_type="latent" where latent output skip is expected.67- Use self.progress_bar(timesteps) in denoising loops.68- Do not build variant behavior by subclassing an unrelated existing pipeline class.6970### Schedulers7172- Use SchedulerMixin and ConfigMixin.73- Keep scheduler config semantics consistent with existing scheduler implementations.7475## Import And Registration Rules7677- Register new classes in relevant __init__.py lazy import structures.78- Ensure import structure entries are complete for all newly exposed objects.79- Validate that public imports from diffusers work after edits.8081## Copied Code Rules8283- Respect # Copied from linkage.84- Do not manually diverge copied blocks unless intentionally breaking linkage.85- Run make fix-copies after changes that touch copied sources or copied blocks.8687## Change Workflow88891. Gather context90- Confirm target files, model family, and expected behavior.91- Obtain reference implementation and runnable inference flow when porting.92932. Plan minimal scope94- Separate structural adaptation from algorithmic changes.95- Keep one coherent workflow per change set.96973. Implement98- Edit only required files.99- Preserve naming, config shape, and API contracts unless change requires otherwise.1001014. Validate102- Run focused tests first, then broader checks as needed.103- Confirm imports and serialization/deserialization behavior.1041055. Polish106- Run make style.107- Run make fix-copies.108- Re-run impacted tests.109110## Testing Expectations111112Include tests for the exact behavior being changed:113114- Model tests for shape, dtype/device behavior, serialization, and config parity115- Pipeline tests for deterministic generation paths, outputs, and parameter handling116- Scheduler tests when scheduler logic or config behavior changes117- Regression tests for any bug fix118119When parity with a reference implementation is required:120121- Add component-level parity checks122- Add end-to-end parity checks123- Use explicit tolerances and deterministic seeds124125## PR Preparation For Diffusers126127When asked to prepare a PR targeting diffusers, produce:1281291. Scope statement130- One-paragraph summary: problem, solution, and non-goals.1311322. Change map133- File-by-file list describing what changed and why.1341353. Validation evidence136- Commands run, tests passed, and any skipped tests with reasons.1371384. Compatibility notes139- Backward compatibility, serialization impact, and optional dependency impact.1401415. Reviewer guidance142- Key files to review first, known tradeoffs, and follow-up items.143144### PR Quality Checklist145146- [ ] Minimal focused diff147- [ ] No unrelated refactors mixed with behavior changes148- [ ] New/updated tests for changed behavior149- [ ] Docs updated when public APIs or user-facing behavior changed150- [ ] make style completed151- [ ] make fix-copies completed152- [ ] Relevant test suites pass153- [ ] Commit messages are clear and scoped154155## Common Failure Modes To Prevent156157- Missing lazy import registration causes runtime ImportError158- New config params not registered, causing from_pretrained mismatch159- Pipeline __call__ missing @torch.no_grad(), causing memory growth160- Hardcoded dtype assumptions break mixed precision usage161- Hidden behavior changes introduced during structural refactor162- Unnecessary dependency additions for simple tensor reshaping163164## Output Contract For This Skill165166When using this skill, provide:167168- Implementation summary169- Exact files changed170- Validation summary with command outcomes171- Residual risks or deferred follow-ups172- PR-ready summary text when requested