Back to Diffusers

Coding style and copied code

.ai/references/code_style.md

0.40.01.4 KB
Original Source

Coding style and copied code

Shared reference for how diffusers code should read, and for the # Copied from mechanism.

Coding style

Strive to write code as simple and explicit as possible.

  • Prefer inlining small helper/utility functions over factoring them out — a reader should be able to follow the full flow without jumping between functions. If a private helper has only one caller, inlining it at the call site is usually the cleaner choice.
  • No defensive code, unused code paths, or legacy stubs — do not add fallback paths, safety checks, or configuration options "just in case"; do not carry unused method parameters "for API consistency", backwards-compatibility aliases for names that never shipped, or deprecation shims for code that was never released. When porting from a research repo, delete training-time code paths, experimental flags, and ablation branches entirely — only keep the inference path you are actually integrating.
  • Do not guess user intent and silently correct behavior. Make the expected inputs clear in the docstring, and raise a concise error for unsupported cases rather than adding complex fallback logic.

Copied code

  • Many classes are kept in sync with a source via a # Copied from ... header comment
  • Do not edit a # Copied from block directly — run make fix-copies to propagate changes from the source
  • Remove the header to intentionally break the link