feat: initial release

This commit is contained in:
Artificial Sweetener
2026-05-22 15:41:49 -04:00
commit e513baf70a
278 changed files with 62099 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
name: release
on:
push:
branches:
- main
workflow_dispatch:
inputs:
dry_run:
description: "Run semantic-release in dry-run mode without publishing"
required: false
default: "false"
type: choice
options:
- "true"
- "false"
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
env:
SIMPLE_SYRUP_TEST_COMFY_CPU: "1"
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v6
with:
node-version: 22.14.0
cache: npm
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: "3.11"
- name: Install Node dependencies
run: npm ci
- name: Install ComfyUI host dependencies
shell: bash
run: |
set -euo pipefail
git clone --depth 1 https://github.com/comfyanonymous/ComfyUI.git "$RUNNER_TEMP/ComfyUI"
rsync -a --exclude=".git" "$RUNNER_TEMP/ComfyUI/" "$GITHUB_WORKSPACE/../.."/
pip install -r "$GITHUB_WORKSPACE/../../requirements.txt"
- name: Install Python dependencies
run: pip install -e . pytest pytest-xdist ruff mypy
- name: Verify Python formatting
run: ruff format --check .
- name: Verify Python lint
run: ruff check .
- name: Verify Python types
run: mypy --strict simple_syrup tests
- name: Verify Python tests
run: pytest -n auto -q
- name: Verify frontend
run: npm run check:web
- name: Release
id: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: bash
run: |
set -euo pipefail
log_file="$(mktemp)"
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ github.event.inputs.dry_run }}" = "true" ]; then
npx semantic-release --dry-run --no-ci 2>&1 | tee "$log_file"
echo "released=false" >> "$GITHUB_OUTPUT"
else
npx semantic-release 2>&1 | tee "$log_file"
if grep -Eq "Published release |Created tag v[0-9]" "$log_file"; then
echo "released=true" >> "$GITHUB_OUTPUT"
else
echo "released=false" >> "$GITHUB_OUTPUT"
fi
fi
- name: Sync workspace to released commit
if: steps.release.outputs.released == 'true'
shell: bash
run: |
set -euo pipefail
git fetch origin main --tags
git checkout main
git reset --hard origin/main
- name: Publish Custom Node to Comfy Registry
if: steps.release.outputs.released == 'true'
uses: Comfy-Org/publish-node-action@main
with:
skip_checkout: true
personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }}
+7
View File
@@ -0,0 +1,7 @@
__pycache__/
*.py[cod]
.mypy_cache/
.pytest_cache/
.ruff_cache/
node_modules/
npm-debug.log*
+33
View File
@@ -0,0 +1,33 @@
// SimpleSyrup - workflow-focused ComfyUI extensions for image generation
// Copyright (C) 2026 Artificial Sweetener and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
module.exports = {
branches: ["main"],
tagFormat: "v${version}",
plugins: [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
["@semantic-release/changelog", { changelogFile: "CHANGELOG.md" }],
[
"@semantic-release/exec",
{
prepareCmd: "node scripts/update-release-versions.mjs ${nextRelease.version}",
},
],
[
"@semantic-release/git",
{
assets: [
"package.json",
"package-lock.json",
"pyproject.toml",
"simple_syrup/__init__.py",
"CHANGELOG.md",
],
message:
"chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}",
},
],
],
};
+316
View File
@@ -0,0 +1,316 @@
# AGENTS.md
## Mission Statement
This project provides a high-quality Python ComfyUI custom node extension pack.
Engineering priority is strict architecture, strong separation of concerns, complete feature integrations, behavior safety during structural change, deterministic node behavior, explicit validation, ComfyUI compatibility, runtime safety, and long-term maintainability.
## Purpose
- This file defines engineering guardrails for this repository.
- This file governs architecture, code quality, typing, testing, observability, runtime safety, ComfyUI node behavior, workflow behavior, and extension packaging.
- Do not use this file for feature specs or product planning.
## Behavior Boundary
- Preserve existing user-facing behavior unless explicitly approved to change.
- Preserve compatibility for supported ComfyUI node classes, node names, input definitions, output definitions, categories, return types, and execution behavior unless explicitly approved to change.
- Preserve compatibility for serialized ComfyUI workflows that reference this node pack unless explicitly approved to change.
- Treat current node signatures, widget names, return shapes, validation behavior, and workflow-facing identifiers as the contract.
- Change internals freely within that boundary.
## Environment and Gate Execution
- All verification commands must run against the ComfyUI virtual environment located two directories above this repository.
- From this repository root, the required Python environment is `..\..\venv`.
- Do not create or use a repository-local `.venv`.
- Do not run quality gates with global/system Python.
- If the ComfyUI virtual environment is missing or stale, pause and ask for maintainer direction before recreating it.
- If development tools are missing from the ComfyUI virtual environment, install or update this node pack and its development tooling into that environment.
- Run all commands from the repository root.
- Use PowerShell syntax and Windows paths.
- The ComfyUI virtual environment is authoritative because node behavior may depend on the exact installed ComfyUI, PyTorch, image, tensor, and custom node runtime dependencies available to the host application.
### Required Command Forms
- Tests: `..\..\venv\Scripts\python.exe -m pytest -n auto -q`
- Lint: `..\..\venv\Scripts\ruff.exe check .`
- Format: `..\..\venv\Scripts\ruff.exe format .`
- Type check: `..\..\venv\Scripts\mypy.exe --strict simple_syrup tests`
If a required tool is missing from `..\..\venv`, install or update development dependencies in that environment before verification. Do not substitute global tools.
## Core Engineering Principles
- Use strict object-oriented design where ownership, state, lifecycle, or collaboration boundaries exist.
- Enforce strong separation of concerns as the primary architecture objective.
- Keep modules cohesive and boundaries explicit.
- Assign one authoritative owner per concern.
- Other components may participate in a concern only by using the authoritative owner. They must not re-implement that concern in parallel.
- Reassess ownership before extending an existing structure.
- If a change introduces a distinct responsibility, change cadence, or collaboration boundary, split or extract it as part of the change.
- Complete refactors fully. Update all callsites, remove dead code, remove temporary bridges, and make the new design native to the codebase.
- Complete feature additions fully. Wire the feature through the relevant node class, service layer, validation path, tests, typing, and ComfyUI registration path required by the behavior.
- Do not leave partial implementations, unused code paths, TODO-driven behavior, or follow-up cleanup inside the completed change.
- Do not add internal compatibility layers, internal shims, dual internal paths, legacy fallbacks, or transitional adapters.
- Preserve compatibility only at public ComfyUI-facing or persisted workflow boundaries when required by the behavior contract.
- Favor DRY when it reduces repeated change risk.
- Avoid abstractions that hide intent.
## Architecture Rules
- Organize code into clear layers with one-way dependencies.
- ComfyUI integration layer: `NODE_CLASS_MAPPINGS`, `NODE_DISPLAY_NAME_MAPPINGS`, node categories, input/output declarations, and ComfyUI import-time registration.
- Node API layer: thin node classes exposing ComfyUI-facing methods such as `INPUT_TYPES`, `RETURN_TYPES`, `FUNCTION`, and execution entry points.
- Application/service layer: orchestration for node behavior, validation flow, and feature-level use cases.
- Domain layer: stable internal models, value objects, policies, and pure behavior.
- Runtime/adapter layer: filesystem access, image/audio/model IO, ComfyUI object adaptation, subprocess boundaries, network boundaries, and optional external integrations.
- Shared layer: small cross-layer primitives with no higher-level dependencies.
- Higher-level layers may depend on lower-level layers.
- Lower-level layers must not depend on higher-level layers.
- ComfyUI registration must stay thin and must not contain feature logic.
- Node classes must delegate non-trivial behavior to application services or domain objects.
- Domain logic must not import ComfyUI modules.
- Runtime adapters own external system interaction.
- Keep filesystem, subprocess, and network details out of node classes and domain logic.
- Place code by ownership and dependency direction, not convenience or proximity.
- Avoid god classes and monolithic files.
- Split by responsibility, not convenience.
## Structural Change Rules
- For behavior-critical areas, work in two steps:
1. Add characterization/regression tests for existing behavior.
2. Perform structural changes behind those tests.
- Behavior-critical areas include node registration, `INPUT_TYPES`, `RETURN_TYPES`, widget names, output ordering, execution return shapes, workflow compatibility, validation behavior, file IO, model IO, image/audio tensor handling, and ComfyUI import behavior.
- Do not start structural changes in an area without behavior safeguards for that area.
- When behavior spans multiple components, trace the current ownership and data flow before editing.
- Correct the ownership model instead of layering compensating patches across consumers.
- Land structural changes as complete vertical slices.
- Do not land large unverified rewrites.
- If behavior changes are intentional, explicitly call them out and test them as new behavior.
- Current module layout does not constrain improvement.
- Reorganize modules when it improves architecture.
- Align touched modules with the ownership and dependency rules in this file.
## ComfyUI Node Rules
- Public node identifiers are compatibility-sensitive.
- Do not rename node classes, display names, categories, input keys, output names, return types, or function names without explicit approval.
- Keep ComfyUI-facing node classes small and predictable.
- `INPUT_TYPES` must be deterministic and must not perform expensive IO.
- Importing the node pack must not perform heavy computation, network access, model loading, or destructive filesystem operations.
- Node execution must validate inputs before performing side effects.
- Node execution must return exactly the declared output shape.
- Optional behavior must be explicit in inputs and tested.
- Hidden inputs and ComfyUI-specific metadata must be documented in code through clear names and docstrings.
- Any workflow-affecting default value change is a behavior change and requires tests.
- Errors surfaced to ComfyUI users must be actionable.
## Tooltip Standards
- Every ComfyUI-facing node must provide a concise, user-facing `DESCRIPTION`.
- Every visible node input, optional input, hidden input, and output tooltip supported by the API must have tooltip text.
- Tooltips should help a workflow builder make the right choice without reading code.
- Tooltips must be concise, layman-friendly, and technically accurate enough to explain the practical effect of the setting.
- Prefer one sentence. Use two only when the field has an important tradeoff or safety implication.
- Explain what the control does, what changing it affects, and any important tradeoff.
- Include units when relevant, such as pixels, latent pixels, scale multiplier, mask value, batch count, or strength.
- Explain directionality when useful: higher/lower, enabled/disabled, larger/smaller.
- Avoid implementation jargon unless the user needs it to make a good workflow decision.
- Do not repeat the field name as a definition.
- Do not document removed behavior, imagined alternatives, or choices the product does not expose.
- Keep legacy `INPUT_TYPES` tooltips and Comfy v3 schema tooltips aligned when both export paths expose the same node or field.
## ComfyUI Node Export Rules
- When adding, renaming, or removing a ComfyUI node, update and verify every export path used by this repository.
- Legacy ComfyUI mapping exports must be updated in `simple_syrup/nodes/__init__.py`:
- `NODE_CLASS_MAPPINGS`
- `NODE_DISPLAY_NAME_MAPPINGS`
- `__all__`
- Comfy v3 entrypoint exports must be updated when the node should be visible through the v3 API:
- `simple_syrup/nodes_v3/__init__.py`
- `get_nodes()`
- a v3 wrapper class when needed
- The root package export in repository root `__init__.py` must continue exposing the relevant mappings and `comfy_entrypoint`.
- Tests must cover every export path used by the node:
- A registration test must assert the node id and display name exist in `NODE_CLASS_MAPPINGS` and `NODE_DISPLAY_NAME_MAPPINGS`.
- A v3 entrypoint test must assert `comfy_entrypoint().get_node_list()` includes the node when it is expected to be visible through Comfy v3.
- If a v3 node is conditional, tests must cover both the available and unavailable conditions and prove unrelated v3 nodes remain exported.
- Do not consider a node addition complete from `NODE_CLASS_MAPPINGS` alone. A node is not fully exported until every repository-supported ComfyUI export path is updated and tested.
## Code Organization and Readability
- Write self-documenting code with expressive, concise names.
- Place new code deliberately in the module where it naturally belongs.
- Keep files intentionally organized so reading order reflects design intent.
- Do not place code opportunistically "where it works".
- Remove obsolete code paths when replacements are complete.
- Keep ComfyUI registration concerns at the package boundary.
- Keep node declaration concerns in node modules.
- Keep reusable behavior in services or domain modules.
- Keep filesystem, network, subprocess, and external-library adaptation in runtime adapter modules.
## Docstrings and Comments
- Docstrings are mandatory for all new and changed modules, classes, functions, and methods.
- Use concise imperative docstrings for simple logic.
- Use Google-style docstrings for complex logic.
- Docstrings must explain rationale, constraints, and intent.
- Docstrings must not restate obvious mechanics.
- Inline comments are allowed only for non-obvious behavior, invariants, edge cases, or external constraints.
## Documentation Policy
- Do not create new docs files, README variants, design docs, ADRs, roadmap files, or notes unless explicitly requested by the maintainer.
- Required context must live in code, type hints, tests, and docstrings.
- Documentation and explanatory writing must describe the product directly as it exists now.
- Do not document against removed features, imagined alternatives, or non-existent choices.
## Typing Policy
- Strong typing is required for all new code.
- Modified code must be typed as part of the change.
- Type hints are mandatory on function signatures and key internal state.
- Use explicit domain types, dataclasses, TypedDicts, Protocols, and type narrowing instead of `Any`.
- `Any` is allowed only at ComfyUI, JSON, tensor, or dynamic plugin boundaries and must be narrowed before core logic relies on it.
- Run `mypy --strict` for type verification.
- Temporary typing relaxations are allowed only when explicitly justified inline and tracked for removal.
## Logging, Errors, and Observability
- Observability is mandatory.
- Use structured, actionable logging with context identifiers where relevant.
- Include enough context to diagnose failures quickly, such as node class, display name, input name, output name, file path, model name, operation, and ComfyUI workflow-facing identifier.
- Use log levels consistently: `debug`, `info`, `warning`, `error`.
- Preserve exception context and stack traces for unexpected failures.
- `print` is not allowed for runtime diagnostics.
- Bare `except:` is not allowed.
- `except Exception` must be narrow, intentional, and log context plus failure reason.
- Silent exception swallowing is not allowed.
- Errors exposed from node, service, validation, and runtime boundaries must be explicit and actionable.
## Desktop Security and Safety Rules
- Treat workflow inputs, filesystem paths, model paths, image/audio paths, generated data, subprocess execution, and network access as security-sensitive.
- Never execute untrusted code paths from workflows, metadata, file contents, or generated data.
- Validate and sanitize external paths and user-provided file references.
- Use structured parsing and validation for JSON, metadata, and configuration.
- Use subprocess argument lists, never shell-string execution.
- Set explicit timeouts for network operations.
- Fail closed when trust, schema validation, path validation, or version validation is uncertain.
- Never log secrets, tokens, credentials, or sensitive local paths beyond what is necessary for diagnosis.
- Do not silently continue after invalid node inputs, missing files, invalid model references, failed conversions, malformed metadata, or unsupported data shapes.
## Testing Policy
- Add or update tests for every behavior change and every bug fix.
- Add characterization tests before structural changes to behavior-critical areas.
- New behavior must not be unverified.
- Include success and failure path coverage.
- Include regression tests for fixed bugs.
- Keep tests deterministic and isolated.
- Use real behavior tests over excessive mocking.
- Mock only external boundaries such as ComfyUI runtime calls, filesystem errors, network calls, subprocesses, random generation, and time.
- Node behavior must be tested at the narrowest useful level and through integration-style tests when ComfyUI-facing shape matters.
- Node registration changes require tests for exported mappings.
- Tooltip coverage must be tested for exported node descriptions, inputs, and outputs supported by each ComfyUI API path.
- Input/output signature changes require workflow-facing compatibility tests.
- Runtime behavior requires tests for success and failure paths.
- Add or update frontend tests for frontend behavior changes.
- Settings UI changes require tests for registration, default value, persistence call, and failure handling.
- Backend/frontend settings bridges require Python tests for routes and persistence plus TypeScript tests for API calls.
## Test Execution Rules
- Run tests in parallel using xdist.
- Default command: `..\..\venv\Scripts\python.exe -m pytest -n auto -q`.
- If running a focused subset during development, run the full suite before completion.
- Failing tests are blocking.
## Python Toolchain
- Formatter: `ruff format`
- Linter: `ruff check`
- Type checker: `mypy --strict`
- Test runner: `pytest -n auto -q`
## TypeScript / Frontend Toolchain
- Frontend code lives under `web/src`.
- Frontend tests live under `web/tests`.
- Built browser artifacts live under `web/dist`.
- Do not hand-edit generated files in `web/dist`; edit `web/src` and run the build.
- Use TypeScript for ComfyUI frontend extensions.
- Do not add untyped JavaScript for new frontend behavior unless it is generated build output.
- Keep frontend code small, typed, and framework-free unless explicitly approved.
- Frontend code must not own backend policy. It may display and edit settings, but Python runtime settings remain the source of truth.
- If `package.json` exists, run these from repository root:
```powershell
npm ci
npm run lint:web
npm run typecheck:web
npm run test:web
npm run build:web
```
- Do not use global TypeScript, ESLint, or Vitest directly.
## ComfyUI Frontend Rules
- ComfyUI frontend extensions must be registered from TypeScript source under `web/src`.
- Settings-panel behavior must use Comfy's frontend settings API.
- Browser settings are not authoritative for backend behavior.
- Any frontend setting that affects backend node declarations or execution must be mirrored through an explicit backend route or persisted backend settings file.
- Backend code must validate settings read from disk or HTTP.
- Frontend code must handle backend route failures visibly and conservatively.
- Do not perform model discovery, filesystem assumptions, or download decisions in frontend code.
- Do not duplicate model-listing policy in frontend code; call backend APIs or edit backend settings only.
## Verification Workflow
- Run focused checks continuously while implementing.
- Verify the specific reported behavior directly when feasible.
- Do not declare a node, workflow, runtime, validation, or packaging issue fixed from code inspection alone when a direct test is feasible.
- Run full gates before reporting completion.
- Distinguish observed results from inferred results in updates and completion reports.
- Do not introduce new lint/type failures in modified files.
- Do not report completion if any blocking gate fails.
- If a gate is intentionally deferred, explicitly state the reason and risk.
## Definition of Done
Per change, all of the following are required:
- Behavior is safeguarded by tests.
- New/modified code follows architecture boundaries.
- New/modified code placement reflects ownership and dependency rules in this file.
- Refactors are complete, with callsites updated and obsolete internal paths removed.
- Features are complete, with node registration, node API, services, runtime adapters, validation, typing, and tests updated wherever the behavior requires them.
- New/modified code is typed.
- Required docstrings are present and meaningful.
- Logging/error handling is actionable.
- Security-sensitive boundaries validate inputs and fail closed.
- `..\..\venv\Scripts\ruff.exe format .` passes.
- `..\..\venv\Scripts\ruff.exe check .` passes.
- `..\..\venv\Scripts\mypy.exe --strict simple_syrup tests` passes for the enforced scope.
- `..\..\venv\Scripts\python.exe -m pytest -n auto -q` passes.
- Frontend source is typed and tested when touched.
- Generated frontend artifacts are rebuilt from source.
- `npm run lint:web`, `npm run typecheck:web`, `npm run test:web`, and `npm run build:web` pass when frontend code exists or is touched.
- New, renamed, or removed nodes are updated in all legacy and Comfy v3 export paths, with tests proving both paths expose the intended node set.
## Commit Policy
- Use Conventional Commits: `type(scope): subject`.
- Allowed types: `feat`, `fix`, `refactor`, `test`, `chore`, `docs`, `build`, `ci`.
- Keep commits atomic and cohesive.
- Breaking structural changes must be clearly labeled.
## Maintainer Authority
- Maintainer instructions override this file.
- If constraints conflict, pause and ask for maintainer direction before proceeding.
+3
View File
@@ -0,0 +1,3 @@
# Changelog
All notable changes to this project will be documented in this file.
+661
View File
@@ -0,0 +1,661 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license for
software and other kinds of works, specifically designed to ensure
cooperation with the community in the case of network server software.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
our General Public Licenses are intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rights
with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.
A secondary benefit of defending all users' freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
encouraged by the resulting cooperation. However, in the case of
software used on network servers, this result may fail to come about.
The GNU General Public License permits making a modified version and
letting the public access it on a server without ever releasing its
source code to the public.
The GNU Affero General Public License is designed specifically to
ensure that, in such cases, the modified source code becomes available
to the community. It requires the operator of a network server to
provide the source code of the modified version running there to the
users of that server. Therefore, public use of a modified version, on
a publicly accessible server, gives the public access to the source
code of the modified version.
An older license, called the Affero General Public License and
published by Affero, was designed to accomplish similar goals. This is
a different license, not a version of the Affero GPL, but Affero has
released a new version of the Affero GPL which permits relicensing under
this license.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify the
Program, your modified version must prominently offer all users
interacting with it remotely through a computer network (if your version
supports such interaction) an opportunity to receive the Corresponding
Source of your version by providing access to the Corresponding Source
from a network server at no charge, through some standard or customary
means of facilitating copying of software. This Corresponding Source
shall include the Corresponding Source for any work covered by version 3
of the GNU General Public License that is incorporated pursuant to the
following paragraph.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the work with which it is combined will remain governed by version
3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU Affero General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU Affero General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU Affero General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU Affero General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computer
network, you should also make sure that it provides a way for users to
get its source. For example, if your program is a web application, its
interface could display a "Source" link that leads users to an archive
of the code. There are many ways you could offer source, and different
solutions will be better for different programs; see section 13 for the
specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<https://www.gnu.org/licenses/>.
+196
View File
@@ -0,0 +1,196 @@
# SimpleSyrup
**SimpleSyrup** is a ComfyUI node pack that grew out of moving my A1111/WebUI image workflows into ComfyUI graphs.
The WebUI side shows up in the things I kept reaching for: ADetailer-style inline `[SEP]` prompt batches, tiled diffusion, familiar checkpoint loader controls, CLIP skip where WebUI users expect it, and sampler/scheduler extras. The Comfy side matters just as much: the detailers are modeled heavily on ComfyUI Impact Pack's SEGS workflow, and the utility nodes are built for graph readability, ordered data, and explicit runtime behavior.
SimpleSyrup pulls from a few different places:
- Moving from **A1111/WebUI** to **ComfyUI** is the reason this pack brings tiled diffusion, familiar checkpoint loader control grouping, ADetailer-style prompt splitting, and A1111-flavored sampler behavior into node graphs.
- **ComfyUI Impact Pack** is the main influence for the SEGS and detailer shape: detectors create SEGS, detailers sample cropped areas, masks are feathered, and results are composited back into the source image.
- **ADetailer** is the influence for inline `[SEP]` per-segment prompt batches.
- The remaining utility pieces cover practical graph needs: GPU Lanczos resizing through TorchLanc, latent provenance helpers, and smaller nodes for image, prompt, and conditioning workflows.
## Highlights
- Impact-compatible SEGS detection, sorting, combining, tiling, and detailing.
- Detailer nodes modeled heavily on ComfyUI Impact Pack's SEGS workflow.
- ADetailer-style inline `[SEP]` per-segment prompt batches.
- MultiDiffusion, Mixture of Diffusers, and regional MultiDiffusion sampling paths.
- A WebUI-familiar checkpoint loader with CLIP skip and VAE override controls.
- A Simple Anima loader that keeps Anima's model, VAE, dtype, and device controls together.
- Loaders for SAM, GroundingDINO, ViTMatte, Ultralytics, and WD14.
- Tile & Tag SEGS workflows that run WD14 on deterministic tile crops and keep conditioning aligned to tile order.
- KSampler extras including A1111-style Euler ancestral behavior, AYS, GITS, automatic A1111 scheduling, and beta57.
- GPU Lanczos resizing through [TorchLanc](https://github.com/Artificial-Sweetener/TorchLanc), with batch and mask handling.
- Provenance-aware latent helpers for recovering the latent behind an unmodified decoded image.
- Settings-backed model dropdowns that can show known downloadable models or only locally installed ones.
## Installation
**Recommended: install through ComfyUI Manager**
Open **Manager** from the ComfyUI toolbar, click **Custom Nodes Manager**, search for **SimpleSyrup**, and click **Install**. Restart ComfyUI after installation.
**Manual install**
If you would rather install it yourself, clone this repo into `ComfyUI/custom_nodes/`, activate your **ComfyUI venv**, and install this node pack's requirements.
```powershell
cd ComfyUI\custom_nodes
git clone https://github.com/Artificial-Sweetener/SimpleSyrup.git
cd SimpleSyrup
pip install -r requirements.txt
```
ComfyUI already provides the heavy shared runtime stack, including PyTorch. SimpleSyrup adds the packages it needs for specific features, including TorchLanc, Ultralytics, ONNX Runtime, Segment Anything, and Hugging Face download helpers.
## The Nodes
SimpleSyrup is organized around workflow jobs, not socket types.
### Impact-Style SEGS Workflows
SimpleSyrup speaks the Impact Pack SEGS shape on purpose. It can read Impact-style SEGS, sort them, combine them, tile them, and emit SEGS payloads that Impact-style consumers can read.
The detailer nodes are modeled heavily on **ComfyUI Impact Pack**. Detectors create SEGS, SEGS choose the crop areas, crops are sampled, masks are feathered, and the results are composited back into the source image.
- **Prompt SEGS w/ SAM** uses GroundingDINO to find prompt-matched boxes, SAM to segment them, optional negative prompting to subtract unwanted areas, and optional ViTMatte refinement to clean up mask edges. It returns both SEGS and a combined mask.
- **Detect SEGS w/ Ultralytics** runs bbox or segmentation detection, filters by threshold and size, supports label filtering, and returns Impact-compatible SEGS plus a combined mask.
- **Detail SEGS by Scale Factor** upscales each SEG crop, samples it, downsizes it back, and composites it into the original image with feathering and optional denoise masks.
- **Detail SEGS by Scale Factor w/ Tiled Diffusion** uses the same crop/detail idea, but samples large crops through SimpleSyrup's tiled diffusion path.
- **Detail SEGS as Regions** runs one regional MultiDiffusion pass over the image and pairs every SEG with its matching `CONDITIONING_BATCH` entry.
The regional node is different from the per-crop detailers. It samples one full-image latent with regional conditioning, using the global prompt for full-image context while each SEG gets its own positive conditioning.
### Per-Segment Prompt Batches
SimpleSyrup layers the ADetailer habit I missed from WebUI on top of the Impact-style detailer shape: writing per-segment prompt batches inline with `[SEP]`.
Those prompts become an ordered `CONDITIONING_BATCH`, so prompt 1 stays matched to SEG 1, prompt 2 stays matched to SEG 2, and so on. This keeps the graph readable when each detected item needs its own prompt.
- **Encode Prompt Batch** splits prompt text with `[SEP]` and encodes ordered positive and negative `CONDITIONING_BATCH` values.
- **Conditioning Batch Start** and **Conditioning Batch Append** build ordered conditioning batches for per-segment and regional workflows.
### Tile, Tag, and Guide
**Tile & Tag SEGS** is for workflows where tile regions should carry their own generated prompt guidance.
It splits an image into deterministic tile SEGS, crops each tile, runs WD14 tagging on each crop, prefixes your universal positive prompt text, and CLIP-encodes the resulting prompts into a `CONDITIONING_BATCH`. The order matters: the conditioning batch is aligned to the tile SEGS order so downstream per-SEG or regional nodes can pick the right prompt for the right area.
That is the kind of thing that is easy to do once by hand and annoying to keep correct in a real graph.
### Tiled Sampling
**KSampler (Tiled Diffusion)** is a KSampler-style node with selectable **MultiDiffusion** and **Mixture of Diffusers** modes.
It splits the latent into tiles, denoises tile predictions, and blends them back together during sampling. MultiDiffusion averages overlapping predictions. Mixture of Diffusers uses weighted blending. Both are there because large images and large upscale passes often need a different strategy than normal full-latent denoising.
Tiled diffusion is here because it was one of the high-resolution workflow tools I kept reaching for in my WebUI setup. SimpleSyrup brings MultiDiffusion and Mixture of Diffusers behavior into normal Comfy sampling nodes, so large latent jobs can be tiled without giving up Comfy's explicit conditioning and graph wiring.
This also matters for Anima workflows. Anima can produce beautiful images, but pushing beyond its comfortable native size with untiled diffusion upscale can smear detail instead of improving it. The tiled path gives those workflows another route.
### Diffusion Loaders
**Simple Load Checkpoint** is meant to feel familiar if you come from WebUI, where the common generation controls live near the model selection.
- **Simple Load Checkpoint** loads a checkpoint, optionally replaces the checkpoint VAE, and keeps CLIP skip in the same place.
- **Simple Load Anima** is for Anima workflows. It loads Anima with the Qwen text encoder and Qwen image VAE it expects. You can choose the files yourself or let SimpleSyrup resolve the known Anima assets automatically. It keeps model, VAE, dtype, and device decisions together so the rest of the graph can get on with the image.
### Model and Detector Loaders
These nodes load the models used by detection, segmentation, tagging, matting, and compatibility workflows.
- **SAM Model Loader** loads SAM, SAM-HQ, and MobileSAM choices for segmentation workflows.
- **GroundingDINO Model Loader** loads GroundingDINO with an explicit BERT text encoder.
- **ViTMatte Model Loader** loads ViTMatte for mask edge refinement.
- **Load Ultralytics Model** loads an Ultralytics detector and exposes both SimpleSyrup's native detector model and Impact-style compatibility outputs.
- **Load WD14 Tagger** loads a SmilingWolf WD14 ONNX model and its tag CSV.
- **LayerStyle SAM Models Adapter** splits a LayerStyle `LS_SAM_MODELS` bundle into separate `SAM_MODEL` and `DINO_MODEL` outputs.
- **Grounded SAM Model Info** returns JSON metadata for selected SAM and GroundingDINO models.
The LayerStyle adapter exists because good ComfyUI workflows should not make you reload the same SAM or GroundingDINO model just because one node pack uses a different socket shape.
### Sampler and Scheduler Extras
**KSampler (Extras)** keeps the normal Comfy sampler shape, but adds sampler and scheduler behavior I wanted available without dragging in a separate sampler stack.
It includes:
- `euler_a_a1111`, an A1111/k-diffusion-style Euler ancestral sampler.
- **AYS SD1** and **AYS SDXL** schedules.
- **GITS**.
- **automatic_a1111** scheduler behavior.
- **beta57**, a local reimplementation of the RES4LYF beta57 scheduler preset.
The node still uses Comfy-style seed handling, partial denoise behavior, progress callbacks, and normal positive/negative conditioning inputs.
### Image, Prompt, and Latent Utilities
These nodes handle the smaller jobs that show up all over image workflows.
- **Resize Image to Target** resizes image batches with stretch, keep-aspect, crop, and pad modes. It can round output dimensions to a divisibility target, anchor crop or pad placement, process batches in chunks, resize a mask with the image, and use GPU Lanczos through [TorchLanc](https://github.com/Artificial-Sweetener/TorchLanc).
- **Simple VAE Encode** encodes an image to latent space, but reuses the source latent when the graph proves the image came from an unmodified `VAEDecode`.
- **Upscale Latent From Image** finds the latent behind an unmodified decoded image and expands to Comfy's latent upscale behavior.
- **Latent Diagnostics** passes a latent through unchanged while reporting shape, dtype, device, and tiling-fit details.
- **Prompt Encode Style** creates Prompt Control style tags from an encode-style selection.
- **Prompt Encode Style & Normalization** creates Prompt Control style and normalization tags together.
- **Scale Factor** provides a bounded scale multiplier for nodes that expect one.
- **Seed** provides a reusable seed value with ComfyUI seed controls.
The provenance nodes trace the graph. They do not guess from tensor values. If an image has been loaded, edited, cropped, detailed, resized, or otherwise changed, the original latent provenance is broken and the node will not pretend otherwise.
## Settings
SimpleSyrup adds one ComfyUI setting:
- **SimpleSyrup: Show downloadable models in loader dropdowns**
When this is enabled, supported loaders show known downloadable model choices even if the files are not installed yet. When it is disabled, those dropdowns only show models SimpleSyrup can verify locally.
This setting affects SAM, GroundingDINO, ViTMatte, and WD14 loader dropdowns. Anima's automatic Qwen text encoder and VAE resolution is handled by the Anima loader itself.
## License & Acknowledgements
**SimpleSyrup** is licensed under the GNU Affero General Public License v3.0 or later (**AGPL-3.0-or-later**). Please read the full [LICENSE](LICENSE) included with this repo.
AGPL-3.0-or-later is a strong copyleft license. If you convey SimpleSyrup or a modified version, you must provide the corresponding source; and if you let users interact with a modified version over a network, you must offer those users the corresponding source for that modified version.
SimpleSyrup owes a lot to other projects:
- [ComfyUI Impact Pack](https://github.com/ltdrdata/ComfyUI-Impact-Pack) for the SEGS workflow vocabulary and detailer shape this pack is heavily modeled around.
- [ADetailer](https://github.com/Bing-su/adetailer) for the inline `[SEP]` per-segment prompt workflow I missed from WebUI.
- [ComfyUI Layer Style Advance](https://github.com/chflame163/ComfyUI_LayerStyle_Advance) for the SAM workflow surface this pack interoperates with.
- [Tiled Diffusion & VAE for AUTOMATIC1111](https://github.com/pkuliyi2015/multidiffusion-upscaler-for-automatic1111) for practical tiled diffusion and Mixture of Diffusers behavior.
- [RES4LYF](https://github.com/ClownsharkBatwing/RES4LYF) for the beta57 scheduler preset reimplemented here.
SimpleSyrup also vendors or reimplements selected third-party behavior for SAM-HQ, MobileSAM, GroundingDINO, AUTOMATIC1111 sampler behavior, k-diffusion, and tiled diffusion behavior. See [third_party/NOTICE.md](third_party/NOTICE.md) for the full third-party notices.
### Research Citations
SimpleSyrup's tiled diffusion behavior is based on ideas from MultiDiffusion and Mixture of Diffusers.
```bibtex
@article{bar2023multidiffusion,
title={MultiDiffusion: Fusing Diffusion Paths for Controlled Image Generation},
author={Bar-Tal, Omer and Yariv, Lior and Lipman, Yaron and Dekel, Tali},
journal={arXiv preprint arXiv:2302.08113},
year={2023}
}
```
```bibtex
@article{barbero2023mixture,
title={Mixture of Diffusers for scene composition and high resolution image generation},
author={Barbero Jimenez, Alvaro},
journal={arXiv preprint arXiv:2302.02412},
year={2023}
}
```
## From the Developer 💖
- **Buy Me a Coffee**: You can help fuel more projects like this at my [Ko-fi page](https://ko-fi.com/artificial_sweetener).
- **My Website & Socials**: See my art, poetry, and other dev updates at [artificialsweetener.ai](https://artificialsweetener.ai).
- **If you like this project**, it would mean a lot to me if you gave me a star here on GitHub!! ⭐
+49
View File
@@ -0,0 +1,49 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""SimpleSyrup ComfyUI extension entry point."""
from __future__ import annotations
import sys
from . import simple_syrup as _simple_syrup_package
sys.modules.setdefault("simple_syrup", _simple_syrup_package)
from .simple_syrup.nodes import ( # noqa: E402
NODE_CLASS_MAPPINGS,
NODE_DISPLAY_NAME_MAPPINGS,
)
from .simple_syrup.runtime.settings_routes import register_settings_routes # noqa: E402
WEB_DIRECTORY = "./web/dist"
async def comfy_entrypoint() -> object:
"""Return Comfy v3 extension nodes without importing optional integrations."""
from comfy_api.latest import ComfyExtension
class SimpleSyrupExtension(ComfyExtension):
"""Expose SimpleSyrup's Comfy v3 nodes."""
async def get_node_list(self) -> list[type[object]]:
"""Return v3 nodes after Comfy asks for them."""
from .simple_syrup.nodes_v3 import get_nodes
return get_nodes()
return SimpleSyrupExtension()
register_settings_routes()
__all__ = [
"NODE_CLASS_MAPPINGS",
"NODE_DISPLAY_NAME_MAPPINGS",
"WEB_DIRECTORY",
"comfy_entrypoint",
]
+22
View File
@@ -0,0 +1,22 @@
// SimpleSyrup - workflow-focused ComfyUI extensions for image generation
// Copyright (C) 2026 Artificial Sweetener and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
import js from "@eslint/js";
import tseslint from "typescript-eslint";
export default tseslint.config(
js.configs.recommended,
...tseslint.configs.strictTypeChecked,
{
languageOptions: {
parserOptions: {
project: "./tsconfig.json",
tsconfigRootDir: import.meta.dirname
}
},
rules: {
"@typescript-eslint/consistent-type-imports": "error"
}
}
);
+9516
View File
File diff suppressed because it is too large Load Diff
+31
View File
@@ -0,0 +1,31 @@
{
"name": "simple-syrup-comfyui",
"version": "0.1.0",
"private": true,
"license": "AGPL-3.0-or-later",
"type": "module",
"scripts": {
"lint:web": "eslint web/src web/tests",
"typecheck:web": "tsc --noEmit",
"test:web": "vitest run",
"build:web": "esbuild web/src/main.ts --bundle --format=esm --target=es2022 --outfile=web/dist/simple-syrup.js --external:../../../scripts/app.js",
"check:web": "npm run lint:web && npm run typecheck:web && npm run test:web && npm run build:web",
"release": "semantic-release"
},
"devDependencies": {
"@eslint/js": "^9.39.1",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^13.0.1",
"@semantic-release/exec": "^7.1.0",
"@semantic-release/git": "^10.0.1",
"@semantic-release/release-notes-generator": "^14.1.1",
"@types/node": "^25.6.0",
"esbuild": "^0.27.0",
"eslint": "^9.39.1",
"jsdom": "^27.2.0",
"semantic-release": "^25.0.3",
"typescript": "^5.9.3",
"typescript-eslint": "^8.46.4",
"vitest": "^4.0.8"
}
}
+67
View File
@@ -0,0 +1,67 @@
[build-system]
requires = ["setuptools>=77"]
build-backend = "setuptools.build_meta"
[project]
name = "simple-syrup"
description = "Workflow-focused ComfyUI extensions for image generation."
version = "0.1.0"
license = "AGPL-3.0-or-later"
license-files = ["LICENSE"]
requires-python = ">=3.11"
dynamic = ["dependencies"]
classifiers = [
"Operating System :: OS Independent",
"Environment :: GPU :: NVIDIA CUDA",
]
[project.urls]
Repository = "https://github.com/Artificial-Sweetener/SimpleSyrup"
"Bug Tracker" = "https://github.com/Artificial-Sweetener/SimpleSyrup/issues"
[tool.comfy]
PublisherId = "artificialsweetener"
DisplayName = "SimpleSyrup"
Icon = ""
includes = []
[tool.setuptools.dynamic]
dependencies = { file = ["requirements.txt"] }
[tool.setuptools.packages.find]
include = ["simple_syrup*"]
[tool.ruff]
line-length = 88
target-version = "py311"
extend-exclude = [
"simple_syrup/third_party/groundingdino_runtime",
"simple_syrup/third_party/sam_hq_runtime",
]
[tool.ruff.lint]
select = ["E", "F", "I", "UP", "B", "C4", "ANN"]
ignore = ["ANN401"]
[tool.mypy]
python_version = "3.11"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
no_implicit_optional = true
strict_equality = true
explicit_package_bases = true
exclude = [
"simple_syrup/third_party/groundingdino_runtime",
"simple_syrup/third_party/sam_hq_runtime",
]
[[tool.mypy.overrides]]
module = ["comfy.*"]
ignore_missing_imports = true
[tool.pytest.ini_options]
pythonpath = [".", "../.."]
testpaths = ["tests"]
+8
View File
@@ -0,0 +1,8 @@
torchlanc>=1.1.0
ultralytics>=8.3.0
onnxruntime>=1.22.0
segment-anything>=1.0
timm>=0.6.13
addict>=2.4.0
yapf>=0.43.0
huggingface-hub>=0.34.0
+48
View File
@@ -0,0 +1,48 @@
// SimpleSyrup - workflow-focused ComfyUI extensions for image generation
// Copyright (C) 2026 Artificial Sweetener and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
import { readFileSync, writeFileSync } from "node:fs";
const nextVersion = process.argv[2];
if (!nextVersion) {
throw new Error("Expected the next release version as the first argument.");
}
function writeJsonVersion(filePath) {
const metadata = JSON.parse(readFileSync(filePath, "utf8"));
metadata.version = nextVersion;
if (metadata.packages?.[""]) {
metadata.packages[""].version = nextVersion;
}
writeFileSync(filePath, `${JSON.stringify(metadata, null, 2)}\n`, "utf8");
}
function replaceVersionField(filePath, pattern, replacement) {
const originalText = readFileSync(filePath, "utf8");
if (!pattern.test(originalText)) {
throw new Error(`Could not find a version field in ${filePath.pathname}.`);
}
const updatedText = originalText.replace(pattern, replacement);
writeFileSync(filePath, updatedText, "utf8");
}
writeJsonVersion(new URL("../package.json", import.meta.url));
writeJsonVersion(new URL("../package-lock.json", import.meta.url));
replaceVersionField(
new URL("../pyproject.toml", import.meta.url),
/^version = "[^"]+"\r?$/m,
`version = "${nextVersion}"`,
);
replaceVersionField(
new URL("../simple_syrup/__init__.py", import.meta.url),
/^__version__ = "[^"]+"\r?$/m,
`__version__ = "${nextVersion}"`,
);
+11
View File
@@ -0,0 +1,11 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""SimpleSyrup package for organized ComfyUI node implementations."""
from __future__ import annotations
__version__ = "0.1.0"
__all__: list[str] = ["__version__"]
+5
View File
@@ -0,0 +1,5 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Domain behavior for SimpleSyrup nodes."""
+62
View File
@@ -0,0 +1,62 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Domain model for per-segment conditioning batches."""
from __future__ import annotations
import re
from dataclasses import dataclass
from typing import Any, TypeAlias
Conditioning: TypeAlias = Any
@dataclass(frozen=True)
class ConditioningBatch:
"""Store ordered conditioning entries for per-SEG selection."""
entries: tuple[Conditioning, ...]
def __post_init__(self) -> None:
"""Reject batches that cannot select a conditioning."""
if not self.entries:
raise ValueError(
"conditioning batch must contain at least one conditioning."
)
def select(self, index: int) -> Conditioning:
"""Return the entry for an index, reusing the last entry as fallback."""
if index < 0:
raise ValueError("conditioning batch index must be non-negative.")
return self.entries[min(index, len(self.entries) - 1)]
def append(self, conditioning: Conditioning) -> ConditioningBatch:
"""Return a new batch with one conditioning appended."""
return ConditioningBatch((*self.entries, conditioning))
def split_prompt_batch(text: str, separator: str = "[SEP]") -> tuple[str, ...]:
"""Split prompt text into ordered chunks using a configurable separator."""
if separator == "":
raise ValueError("separator must not be empty.")
pattern = rf"\s*{re.escape(separator)}\s*"
return tuple(re.split(pattern, text))
def select_conditioning(
conditioning: Conditioning | ConditioningBatch,
index: int,
) -> Conditioning:
"""Select per-index conditioning or broadcast normal conditioning unchanged."""
if isinstance(conditioning, ConditioningBatch):
return conditioning.select(index)
if index < 0:
raise ValueError("conditioning batch index must be non-negative.")
return conditioning
+64
View File
@@ -0,0 +1,64 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Pure geometry helpers for scale-factor detailing."""
from __future__ import annotations
from dataclasses import dataclass
@dataclass(frozen=True)
class DetailScalePlan:
"""Describe the scaled crop dimensions used for one detail pass."""
width: int
height: int
scale: float
unclamped_long_side: float
target_long_side: float
def build_detail_scale_plan(
detected_width: int,
detected_height: int,
crop_width: int,
crop_height: int,
scale_factor: float,
clamp_size: int,
) -> DetailScalePlan:
"""Calculate a scaled crop size from crop-region geometry."""
_validate_positive_int("detected_width", detected_width)
_validate_positive_int("detected_height", detected_height)
_validate_positive_int("crop_width", crop_width)
_validate_positive_int("crop_height", crop_height)
if scale_factor <= 0.0:
raise ValueError("scale_factor must be greater than 0.")
if clamp_size < 0:
raise ValueError("clamp_size must be 0 or greater. Use 0 for no clamp.")
crop_long_side = float(max(crop_width, crop_height))
unclamped_long_side = crop_long_side * float(scale_factor)
target_long_side = unclamped_long_side
if clamp_size > 0:
target_long_side = min(unclamped_long_side, float(clamp_size))
scale = target_long_side / crop_long_side
width = max(1, int(round(crop_width * scale)))
height = max(1, int(round(crop_height * scale)))
return DetailScalePlan(
width=width,
height=height,
scale=scale,
unclamped_long_side=unclamped_long_side,
target_long_side=target_long_side,
)
def _validate_positive_int(name: str, value: int) -> None:
"""Reject non-positive integer dimensions."""
if int(value) <= 0:
raise ValueError(f"{name} must be greater than 0.")
+38
View File
@@ -0,0 +1,38 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Domain models for graph-level image provenance tracing."""
from __future__ import annotations
from dataclasses import dataclass
from typing import TypeAlias
GraphLink: TypeAlias = tuple[str, int]
@dataclass(frozen=True)
class VaeDecodeProvenance:
"""Record the latent and VAE links that produced a decoded image."""
decode_node_id: str
image_output: GraphLink
samples_link: GraphLink
vae_link: GraphLink | None
@dataclass(frozen=True)
class BrokenProvenance:
"""Describe why an image link cannot be treated as an unmodified decode."""
reason: str
node_id: str | None = None
class_type: str | None = None
@dataclass(frozen=True)
class PassthroughRule:
"""Describe an exact graph pass-through from output slot to input name."""
input_name: str
+19
View File
@@ -0,0 +1,19 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Prompt text composition helpers."""
from __future__ import annotations
def prefix_prompt(prefix: str, prompt: str) -> str:
"""Return prompt text with a blank-safe comma-separated prefix."""
prefix_text = prefix.strip()
prompt_text = prompt.strip()
if not prefix_text:
return prompt_text
if not prompt_text:
return prefix_text
return f"{prefix_text}, {prompt_text}"
+40
View File
@@ -0,0 +1,40 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Format Prompt Control STYLE tags from validated option labels."""
from __future__ import annotations
ENCODE_STYLE_VALUES: dict[str, str] = {
"A1111": "A1111",
"Comfy": "comfy",
"Comfy++": "comfy++",
"Compel": "compel",
"Down Weight": "down_weight",
"Perp": "perp",
}
NORMALIZATION_VALUES: dict[str, str] = {
"none": "none",
"length": "length",
"mean": "mean",
"length+mean": "length+mean",
}
def format_style_tag(encode_style: str) -> str:
"""Return a Prompt Control STYLE tag for an encode-style option."""
return f"STYLE({ENCODE_STYLE_VALUES[encode_style]}) "
def format_style_tag_with_normalization(encode_style: str, normalization: str) -> str:
"""Return a Prompt Control STYLE tag with optional normalization."""
normalization_value = NORMALIZATION_VALUES[normalization]
if normalization_value == "none":
return format_style_tag(encode_style)
return f"STYLE({ENCODE_STYLE_VALUES[encode_style]}, {normalization_value}) "
+153
View File
@@ -0,0 +1,153 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Portions of this file incorporate behavior derived from
# multidiffusion-upscaler-for-automatic1111. See third_party/manifest.toml and
# third_party/NOTICE.md.
"""Domain models for SEGS-driven regional diffusion detailing."""
from __future__ import annotations
from dataclasses import dataclass
import torch
from .conditioning_batch import Conditioning, ConditioningBatch
from .segs import CropRegion, NativeSegs, Segment
OPERATION = "Detail SEGS as Regions"
@dataclass(frozen=True)
class SegmentConditioningPair:
"""Bind one SEG to its exact per-region positive conditioning."""
index: int
segment: Segment
positive: Conditioning
@dataclass(frozen=True)
class ImageRegion:
"""Represent one paired region in image-pixel space."""
index: int
label: str
crop_region: CropRegion
image_mask: torch.Tensor
positive: Conditioning
@dataclass(frozen=True)
class LatentBox:
"""Represent one rectangular region in latent-space coordinates."""
x: int
y: int
width: int
height: int
@dataclass(frozen=True)
class LatentRegion:
"""Represent one paired region in latent space."""
index: int
label: str
latent_box: LatentBox
latent_mask: torch.Tensor
positive: Conditioning
def pair_segments_with_conditioning(
segs: NativeSegs,
region_positive: object,
*,
image_height: int,
image_width: int,
) -> tuple[SegmentConditioningPair, ...]:
"""Return SEGS paired exactly with regional positive conditioning."""
header, segments = segs
validate_segs_image_dimensions(
segs_height=header[0],
segs_width=header[1],
image_height=image_height,
image_width=image_width,
)
if not segments:
return ()
if not isinstance(region_positive, ConditioningBatch):
raise TypeError(
f"{OPERATION} requires region_positive to be CONDITIONING_BATCH."
)
if len(region_positive.entries) != len(segments):
raise ValueError(
f"{OPERATION} requires region_positive to contain exactly one "
"conditioning entry per SEG: got "
f"{len(region_positive.entries)} conditioning entries for "
f"{len(segments)} SEGS."
)
pairs: list[SegmentConditioningPair] = []
for index, (segment, positive) in enumerate(
zip(segments, region_positive.entries, strict=True)
):
if isinstance(positive, ConditioningBatch):
raise TypeError(
f"{OPERATION} region_positive entry {index} must be a normal "
"CONDITIONING value, not CONDITIONING_BATCH."
)
_validate_segment_within_image(
segment,
index=index,
image_height=image_height,
image_width=image_width,
)
pairs.append(
SegmentConditioningPair(
index=index,
segment=segment,
positive=positive,
)
)
return tuple(pairs)
def validate_segs_image_dimensions(
*,
segs_height: int,
segs_width: int,
image_height: int,
image_width: int,
) -> None:
"""Reject SEGS headers that do not describe the current image."""
if (segs_height, segs_width) == (image_height, image_width):
return
raise ValueError(
f"{OPERATION} requires SEGS header dimensions to match the image: "
f"SEGS is {segs_height}x{segs_width}, image is "
f"{image_height}x{image_width}."
)
def _validate_segment_within_image(
segment: Segment,
*,
index: int,
image_height: int,
image_width: int,
) -> None:
"""Reject a segment whose crop region falls outside the image."""
region = segment.crop_region
if region.right <= image_width and region.bottom <= image_height:
return
raise ValueError(
f"{OPERATION} SEG {index} ('{segment.label}') crop_region must fit "
f"inside the image; got ({region.left}, {region.top}, {region.right}, "
f"{region.bottom}) for image {image_height}x{image_width}."
)
+306
View File
@@ -0,0 +1,306 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Domain model and adapters for Impact-compatible SEGS values."""
from __future__ import annotations
from collections.abc import Iterable, Sequence
from dataclasses import dataclass
from typing import NamedTuple, Protocol, TypeAlias, cast
class CropRegion(NamedTuple):
"""Represent a crop region as left, top, right, bottom coordinates."""
left: int
top: int
right: int
bottom: int
@property
def width(self) -> int:
"""Return the region width."""
return self.right - self.left
@property
def height(self) -> int:
"""Return the region height."""
return self.bottom - self.top
class BoundingBox(NamedTuple):
"""Represent a detection box as left, top, right, bottom coordinates."""
left: int
top: int
right: int
bottom: int
@property
def width(self) -> int:
"""Return the box width."""
return self.right - self.left
@property
def height(self) -> int:
"""Return the box height."""
return self.bottom - self.top
@dataclass(frozen=True)
class Segment:
"""Represent one detected region in an Impact-compatible shape."""
cropped_image: object | None
cropped_mask: object
confidence: float
crop_region: CropRegion
bbox: BoundingBox
label: str
control_net_wrapper: object | None = None
class SegmentLike(Protocol):
"""Describe the attribute surface required for SEGS compatibility."""
cropped_image: object | None
cropped_mask: object
confidence: float
crop_region: object
bbox: object
label: str
control_net_wrapper: object | None
SegsHeader: TypeAlias = tuple[int, int]
NativeSegs: TypeAlias = tuple[SegsHeader, tuple[Segment, ...]]
ImpactSegs: TypeAlias = tuple[SegsHeader, list[Segment]]
NativeSegsGroup: TypeAlias = tuple[NativeSegs, ...]
SortKey: TypeAlias = tuple[float, float, int, int, int]
SORT_ORDER_OPTIONS: tuple[str, ...] = (
"largest to smallest",
"smallest to largest",
"widest to thinnest",
"thinnest to widest",
"tallest to shortest",
"shortest to tallest",
"top to bottom",
"bottom to top",
"left to right",
"right to left",
"highest confidence first",
"lowest confidence first",
)
def coerce_segs(value: object) -> NativeSegs:
"""Convert a native or Impact-style SEGS value into native immutable SEGS."""
if not isinstance(value, tuple) or len(value) != 2:
raise ValueError("SEGS must be a tuple of (header, segments).")
header = _coerce_header(value[0])
raw_segments = value[1]
if not isinstance(raw_segments, Iterable):
raise ValueError("SEGS segments must be iterable.")
return header, tuple(coerce_segment(segment) for segment in raw_segments)
def coerce_segment(value: object) -> Segment:
"""Convert a native or attribute-compatible segment into `Segment`."""
if isinstance(value, Segment):
_validate_region(value.crop_region, "crop_region")
_validate_box(value.bbox, "bbox")
return value
required = (
"cropped_image",
"cropped_mask",
"confidence",
"crop_region",
"bbox",
"label",
"control_net_wrapper",
)
missing = [name for name in required if not hasattr(value, name)]
if missing:
names = ", ".join(missing)
raise ValueError(f"SEG item is missing required attribute(s): {names}.")
segment_like = cast(SegmentLike, value)
return Segment(
cropped_image=segment_like.cropped_image,
cropped_mask=segment_like.cropped_mask,
confidence=float(segment_like.confidence),
crop_region=_coerce_region(segment_like.crop_region, "crop_region"),
bbox=_coerce_box(segment_like.bbox, "bbox"),
label=str(segment_like.label),
control_net_wrapper=segment_like.control_net_wrapper,
)
def to_impact_compatible_segs(segs: NativeSegs) -> ImpactSegs:
"""Return raw tuple/list SEGS that Impact-style consumers can read."""
header, segments = coerce_segs(segs)
return header, list(segments)
def coerce_segs_group(value: object) -> NativeSegsGroup:
"""Convert a single SEGS or a Comfy list of SEGS into native SEGS."""
if _looks_like_segs(value):
return (coerce_segs(value),)
if not isinstance(value, Sequence) or isinstance(value, (str, bytes)):
raise ValueError("SEGS group must be a SEGS payload or a sequence of SEGS.")
if len(value) == 0:
raise ValueError("SEGS group must contain one or more SEGS payloads.")
group: list[NativeSegs] = []
for index, item in enumerate(value, start=1):
try:
group.append(coerce_segs(item))
except ValueError as exc:
raise ValueError(f"SEGS group item {index} is invalid: {exc}") from exc
return tuple(group)
def to_impact_compatible_segs_group(segs_group: NativeSegsGroup) -> list[ImpactSegs]:
"""Return a list of Impact-compatible SEGS payloads."""
return [to_impact_compatible_segs(segs) for segs in segs_group]
def sort_segs(segs: NativeSegs, sort_order: str) -> NativeSegs:
"""Return native SEGS sorted by a plain-English policy."""
if sort_order not in SORT_ORDER_OPTIONS:
raise ValueError(f"Unknown SEGS sort order: '{sort_order}'.")
header, segments = coerce_segs(segs)
indexed_segments = tuple(enumerate(segments))
sorted_segments = sorted(
indexed_segments,
key=lambda item: _sort_key(item[1], item[0], sort_order),
)
return header, tuple(segment for _index, segment in sorted_segments)
def _sort_key(segment: Segment, index: int, sort_order: str) -> SortKey:
"""Build a deterministic sort key for one segment."""
region = segment.crop_region
confidence_desc = -float(segment.confidence)
if sort_order == "highest confidence first":
return confidence_desc, float(region.top), region.left, region.left, index
if sort_order == "lowest confidence first":
return (
float(segment.confidence),
float(region.top),
region.left,
region.left,
index,
)
if sort_order == "largest to smallest":
primary = -float(region.width * region.height)
elif sort_order == "smallest to largest":
primary = float(region.width * region.height)
elif sort_order == "widest to thinnest":
primary = -float(region.width)
elif sort_order == "thinnest to widest":
primary = float(region.width)
elif sort_order == "tallest to shortest":
primary = -float(region.height)
elif sort_order == "shortest to tallest":
primary = float(region.height)
elif sort_order == "top to bottom":
primary = float(region.top)
elif sort_order == "bottom to top":
primary = -float(region.top)
elif sort_order == "left to right":
primary = float(region.left)
elif sort_order == "right to left":
primary = -float(region.left)
else:
raise ValueError(f"Unknown SEGS sort order: '{sort_order}'.")
return primary, confidence_desc, region.top, region.left, index
def _coerce_header(value: object) -> SegsHeader:
"""Convert a SEGS header to an image shape tuple."""
if not isinstance(value, Sequence) or len(value) != 2:
raise ValueError("SEGS header must contain exactly height and width.")
height = int(value[0])
width = int(value[1])
if height <= 0 or width <= 0:
raise ValueError("SEGS header height and width must be positive.")
return height, width
def _looks_like_segs(value: object) -> bool:
"""Return whether a value has the outer shape of one SEGS payload."""
if not isinstance(value, tuple) or len(value) != 2:
return False
return _looks_like_header(value[0]) and isinstance(value[1], Iterable)
def _looks_like_header(value: object) -> bool:
"""Return whether a value can represent a SEGS header."""
if not isinstance(value, Sequence) or len(value) != 2:
return False
try:
int(value[0])
int(value[1])
except (TypeError, ValueError):
return False
return True
def _coerce_region(value: object, name: str) -> CropRegion:
"""Convert a four-value coordinate sequence into a crop region."""
if not isinstance(value, Sequence) or len(value) != 4:
raise ValueError(f"{name} must contain left, top, right, bottom.")
region = CropRegion(int(value[0]), int(value[1]), int(value[2]), int(value[3]))
_validate_region(region, name)
return region
def _coerce_box(value: object, name: str) -> BoundingBox:
"""Convert a four-value coordinate sequence into a bounding box."""
if not isinstance(value, Sequence) or len(value) != 4:
raise ValueError(f"{name} must contain left, top, right, bottom.")
box = BoundingBox(int(value[0]), int(value[1]), int(value[2]), int(value[3]))
_validate_box(box, name)
return box
def _validate_region(region: CropRegion, name: str) -> None:
"""Validate crop-region coordinate ordering."""
if region.left < 0 or region.top < 0:
raise ValueError(f"{name} left and top must be non-negative.")
if region.right <= region.left or region.bottom <= region.top:
raise ValueError(f"{name} right/bottom must be greater than left/top.")
def _validate_box(box: BoundingBox, name: str) -> None:
"""Validate bounding-box coordinate ordering."""
if box.left < 0 or box.top < 0:
raise ValueError(f"{name} left and top must be non-negative.")
if box.right <= box.left or box.bottom <= box.top:
raise ValueError(f"{name} right/bottom must be greater than left/top.")
+247
View File
@@ -0,0 +1,247 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Domain policy for deterministic tile SEGS construction."""
from __future__ import annotations
import math
from dataclasses import dataclass
import torch
from ..masking.segs_mask_ops import (
crop_region_for_bbox,
resize_mask,
validate_single_image,
)
from ..shared.logging import get_logger
from .segs import (
BoundingBox,
CropRegion,
NativeSegs,
Segment,
)
LOGGER = get_logger(__name__)
IRREGULAR_MASK_MODES: tuple[str, ...] = (
"Reuse fast",
"Reuse quality",
"All random fast",
"All random quality",
)
@dataclass(frozen=True)
class TileSEGSControls:
"""Validate controls for tile SEGS construction."""
bbox_size: int
crop_factor: float
min_overlap: int
filter_segs_dilation: int
mask_irregularity: float
irregular_mask_mode: str
def __post_init__(self) -> None:
"""Reject controls that would make tile layout ambiguous."""
if self.bbox_size < 1:
raise ValueError("bbox_size must be greater than 0.")
if self.crop_factor < 1.0:
raise ValueError("crop_factor must be greater than or equal to 1.0.")
if self.min_overlap < 0:
raise ValueError("min_overlap must be greater than or equal to 0.")
if not 0.0 <= self.mask_irregularity <= 1.0:
raise ValueError("mask_irregularity must be between 0.0 and 1.0.")
if self.irregular_mask_mode not in IRREGULAR_MASK_MODES:
raise ValueError(
f"irregular_mask_mode must be one of {IRREGULAR_MASK_MODES}."
)
@dataclass(frozen=True)
class TileRegion:
"""Describe one tile bbox and expanded crop region."""
bbox: BoundingBox
crop_region: CropRegion
class TileSEGSBuilder:
"""Build Impact-compatible tile SEGS from an image and optional filters."""
def build(
self,
image: object,
controls: TileSEGSControls,
) -> NativeSegs:
"""Return native tile SEGS in deterministic scan order."""
image_tensor = validate_single_image(image, "Tile & Tag SEGS")
height = int(image_tensor.shape[1])
width = int(image_tensor.shape[2])
bbox_size, min_overlap = _adjust_tile_controls(width, height, controls)
tile_regions = _tile_regions(
width=width,
height=height,
bbox_size=bbox_size,
min_overlap=min_overlap,
crop_factor=controls.crop_factor,
)
mask_factory = _IrregularMaskFactory(controls)
segments: list[Segment] = []
for tile_index, tile_region in enumerate(tile_regions):
mask = _tile_crop_mask(tile_region)
if controls.mask_irregularity > 0:
irregular = mask_factory.mask_like(mask, tile_index)
mask = (mask * irregular).clamp(0.0, 1.0)
if bool(torch.all(mask == 0.0).item()):
continue
label = f"tile_{len(segments) + 1:03}"
segments.append(
Segment(
cropped_image=None,
cropped_mask=mask,
confidence=1.0,
crop_region=tile_region.crop_region,
bbox=tile_region.bbox,
label=label,
)
)
return (height, width), tuple(segments)
class _IrregularMaskFactory:
"""Generate deterministic irregular masks for tile blending."""
def __init__(self, controls: TileSEGSControls) -> None:
"""Initialize the mask generator from node controls."""
self._controls = controls
self._reused_noise: torch.Tensor | None = None
def mask_like(self, reference: torch.Tensor, tile_index: int) -> torch.Tensor:
"""Return a soft irregular mask with the same shape as reference."""
height = int(reference.shape[0])
width = int(reference.shape[1])
if self._controls.irregular_mask_mode.startswith("Reuse"):
if self._reused_noise is None:
self._reused_noise = self._noise(height, width, seed=17)
noise = self._reused_noise
else:
noise = self._noise(height, width, seed=101 + tile_index)
threshold = 1.0 - (self._controls.mask_irregularity * 0.5)
return torch.where(
noise > threshold, torch.zeros_like(noise), torch.ones_like(noise)
)
def _noise(self, height: int, width: int, seed: int) -> torch.Tensor:
"""Return deterministic low-frequency noise resized to tile shape."""
generator = torch.Generator(device="cpu").manual_seed(seed)
quality = 16 if self._controls.irregular_mask_mode.endswith("fast") else 64
base = torch.rand((quality, quality), generator=generator, dtype=torch.float32)
resized = resize_mask(base, height, width)
return resized.clamp(0.0, 1.0)
def _adjust_tile_controls(
width: int,
height: int,
controls: TileSEGSControls,
) -> tuple[int, int]:
"""Clamp tile controls to valid values for the image dimensions."""
bbox_size = min(controls.bbox_size, width, height)
if bbox_size != controls.bbox_size:
LOGGER.warning(
"Tile & Tag SEGS clamped bbox_size to image dimensions",
extra={
"operation": "tile_and_tag_segs",
"requested_bbox_size": controls.bbox_size,
"bbox_size": bbox_size,
},
)
min_overlap = controls.min_overlap
if bbox_size <= 2 * min_overlap:
adjusted = max(0, (bbox_size // 2) - 1)
LOGGER.warning(
"Tile & Tag SEGS adjusted min_overlap for valid tile stepping",
extra={
"operation": "tile_and_tag_segs",
"requested_min_overlap": min_overlap,
"min_overlap": adjusted,
"bbox_size": bbox_size,
},
)
min_overlap = adjusted
return bbox_size, min_overlap
def _tile_regions(
*,
width: int,
height: int,
bbox_size: int,
min_overlap: int,
crop_factor: float,
) -> tuple[TileRegion, ...]:
"""Return row-major tile regions covering the image."""
x_positions = _axis_positions(width, bbox_size, min_overlap)
y_positions = _axis_positions(height, bbox_size, min_overlap)
regions: list[TileRegion] = []
for top in y_positions:
for left in x_positions:
bbox = BoundingBox(left, top, left + bbox_size, top + bbox_size)
regions.append(
TileRegion(
bbox=bbox,
crop_region=crop_region_for_bbox(
bbox,
image_height=height,
image_width=width,
crop_factor=crop_factor,
),
)
)
return tuple(regions)
def _axis_positions(length: int, bbox_size: int, min_overlap: int) -> tuple[int, ...]:
"""Return tile starts for one axis with even overlap distribution."""
if bbox_size >= length:
return (0,)
step = bbox_size - min_overlap
tile_count = max(1, math.ceil(length / step))
overlap_sum = bbox_size * tile_count - length
if overlap_sum < 0:
tile_count += 1
overlap_sum = bbox_size * tile_count - length
overlap = 0 if tile_count == 1 else int(overlap_sum / (tile_count - 1))
if overlap == bbox_size:
return (0,)
starts = []
position = 0
for _index in range(tile_count):
starts.append(min(position, length - bbox_size))
position += bbox_size - overlap
return tuple(dict.fromkeys(starts))
def _tile_crop_mask(tile_region: TileRegion) -> torch.Tensor:
"""Return a rectangular tile mask in crop-region coordinates."""
crop = tile_region.crop_region
bbox = tile_region.bbox
mask = torch.zeros((crop.height, crop.width), dtype=torch.float32)
mask[
bbox.top - crop.top : bbox.bottom - crop.top,
bbox.left - crop.left : bbox.right - crop.left,
] = 1.0
return mask
+230
View File
@@ -0,0 +1,230 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Portions of this file incorporate behavior derived from
# multidiffusion-upscaler-for-automatic1111. See third_party/manifest.toml and
# third_party/NOTICE.md.
"""Pure tiled diffusion planning and mode validation."""
from __future__ import annotations
import math
from dataclasses import dataclass
import torch
TILED_DIFFUSION_MODES = ("multidiffusion", "mixture_of_diffusers")
@dataclass(frozen=True)
class LatentTile:
"""Describe one rectangular latent-space tile."""
x: int
y: int
width: int
height: int
@property
def slicer(self) -> tuple[slice, slice, slice, slice]:
"""Return a tensor slicer for this tile on BCHW latents."""
return (
slice(None),
slice(None),
slice(self.y, self.y + self.height),
slice(self.x, self.x + self.width),
)
@dataclass(frozen=True)
class TiledDiffusionPlan:
"""Describe deterministic latent tiles and balanced tile batches."""
latent_width: int
latent_height: int
tile_width: int
tile_height: int
overlap: int
requested_tile_batch_size: int
tile_batch_size: int
tiles: tuple[LatentTile, ...]
batches: tuple[tuple[LatentTile, ...], ...]
def build_tiled_diffusion_plan(
latent_width: int,
latent_height: int,
tile_width: int,
tile_height: int,
overlap: int,
tile_batch_size: int,
) -> TiledDiffusionPlan:
"""Build a deterministic latent tile plan for tiled denoising."""
_validate_plan_inputs(
latent_width=latent_width,
latent_height=latent_height,
tile_width=tile_width,
tile_height=tile_height,
tile_batch_size=tile_batch_size,
)
effective_tile_width = min(tile_width, latent_width)
effective_tile_height = min(tile_height, latent_height)
effective_overlap = max(0, min(overlap, min(tile_width, tile_height) - 4))
tiles = _split_tiles(
latent_width=latent_width,
latent_height=latent_height,
tile_width=effective_tile_width,
tile_height=effective_tile_height,
overlap=effective_overlap,
)
batches, effective_tile_batch_size = _batch_tiles(tiles, tile_batch_size)
return TiledDiffusionPlan(
latent_width=latent_width,
latent_height=latent_height,
tile_width=effective_tile_width,
tile_height=effective_tile_height,
overlap=effective_overlap,
requested_tile_batch_size=tile_batch_size,
tile_batch_size=effective_tile_batch_size,
tiles=tiles,
batches=batches,
)
def tile_is_splittable(
latent_width: int,
latent_height: int,
tile_width: int,
tile_height: int,
overlap: int,
) -> bool:
"""Return whether the tile grid produces more than one tile."""
plan = build_tiled_diffusion_plan(
latent_width=latent_width,
latent_height=latent_height,
tile_width=tile_width,
tile_height=tile_height,
overlap=overlap,
tile_batch_size=1,
)
return len(plan.tiles) > 1
def validate_tiled_diffusion_mode(diffusion_mode: str) -> None:
"""Reject unsupported tiled diffusion modes."""
if diffusion_mode in TILED_DIFFUSION_MODES:
return
supported = ", ".join(TILED_DIFFUSION_MODES)
raise ValueError(
f"diffusion_mode must be one of: {supported}; got {diffusion_mode!r}."
)
def gaussian_tile_weights(
tile_width: int,
tile_height: int,
*,
device: torch.device,
dtype: torch.dtype,
) -> torch.Tensor:
"""Return Mixture of Diffusers Gaussian tile weights."""
if tile_width < 4:
raise ValueError("tile_width must be at least 4.")
if tile_height < 4:
raise ValueError("tile_height must be at least 4.")
x_values = torch.arange(tile_width, device=device, dtype=torch.float64)
y_values = torch.arange(tile_height, device=device, dtype=torch.float64)
variance = 0.01
x_midpoint = (tile_width - 1) / 2
y_midpoint = tile_height / 2
denominator = math.sqrt(2 * math.pi * variance)
x_probs = (
torch.exp(
-((x_values - x_midpoint) * (x_values - x_midpoint))
/ (tile_width * tile_width)
/ (2 * variance)
)
/ denominator
)
y_probs = (
torch.exp(
-((y_values - y_midpoint) * (y_values - y_midpoint))
/ (tile_width * tile_width)
/ (2 * variance)
)
/ denominator
)
return torch.outer(y_probs, x_probs).to(dtype=dtype)
def _validate_plan_inputs(
*,
latent_width: int,
latent_height: int,
tile_width: int,
tile_height: int,
tile_batch_size: int,
) -> None:
"""Reject invalid tile planning values before sampling."""
if latent_width < 1:
raise ValueError("latent_width must be at least 1.")
if latent_height < 1:
raise ValueError("latent_height must be at least 1.")
if tile_width < 4:
raise ValueError("tile_width must be at least 4.")
if tile_height < 4:
raise ValueError("tile_height must be at least 4.")
if tile_batch_size < 1:
raise ValueError("tile_batch_size must be at least 1.")
def _split_tiles(
*,
latent_width: int,
latent_height: int,
tile_width: int,
tile_height: int,
overlap: int,
) -> tuple[LatentTile, ...]:
"""Split a latent with a symmetric row-major grid formula."""
cols = math.ceil((latent_width - overlap) / (tile_width - overlap))
rows = math.ceil((latent_height - overlap) / (tile_height - overlap))
dx = (latent_width - tile_width) / (cols - 1) if cols > 1 else 0
dy = (latent_height - tile_height) / (rows - 1) if rows > 1 else 0
tiles: list[LatentTile] = []
for row in range(rows):
y = min(int(row * dy), latent_height - tile_height)
for col in range(cols):
x = min(int(col * dx), latent_width - tile_width)
tiles.append(LatentTile(x, y, tile_width, tile_height))
return tuple(tiles)
def _batch_tiles(
tiles: tuple[LatentTile, ...],
requested_tile_batch_size: int,
) -> tuple[tuple[tuple[LatentTile, ...], ...], int]:
"""Group tiles using balanced effective tile batch sizing."""
num_batches = math.ceil(len(tiles) / requested_tile_batch_size)
effective_tile_batch_size = math.ceil(len(tiles) / num_batches)
batches = tuple(
tiles[
index * effective_tile_batch_size : (index + 1) * effective_tile_batch_size
]
for index in range(num_batches)
)
return batches, effective_tile_batch_size
+9
View File
@@ -0,0 +1,9 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Image-domain services and policies for SimpleSyrup."""
from __future__ import annotations
__all__: list[str] = []
+40
View File
@@ -0,0 +1,40 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Image crop compositing helpers for detailer services."""
from __future__ import annotations
import torch
from ..domain.segs import CropRegion
def composite_crop(
image: torch.Tensor,
crop: torch.Tensor,
mask: torch.Tensor,
region: CropRegion,
) -> torch.Tensor:
"""Alpha-composite a detailed crop back into a BHWC image tensor."""
if image.ndim != 4 or crop.ndim != 4:
raise ValueError("image and crop must be BHWC tensors.")
if mask.ndim != 2:
raise ValueError("composite mask must be an HW tensor.")
expected_height = region.height
expected_width = region.width
if int(crop.shape[1]) != expected_height or int(crop.shape[2]) != expected_width:
raise ValueError("detailed crop dimensions must match crop region.")
if int(mask.shape[0]) != expected_height or int(mask.shape[1]) != expected_width:
raise ValueError("composite mask dimensions must match crop region.")
output = image.clone()
alpha = mask.to(device=image.device, dtype=image.dtype).clamp(0.0, 1.0)
alpha_bhwc = alpha.unsqueeze(0).unsqueeze(-1)
original = output[:, region.top : region.bottom, region.left : region.right, :]
blended = crop.to(device=image.device, dtype=image.dtype) * alpha_bhwc
blended = blended + original * (1.0 - alpha_bhwc)
output[:, region.top : region.bottom, region.left : region.right, :] = blended
return output.clamp(0.0, 1.0)
+375
View File
@@ -0,0 +1,375 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Pure geometry planning for target image resizing."""
from __future__ import annotations
import math
from dataclasses import dataclass
from enum import StrEnum
class ResizeMode(StrEnum):
"""Supported target resize modes."""
STRETCH = "Stretch"
KEEP_AR = "Keep AR"
CROP = "Crop (Cover + Crop)"
PAD = "Pad (Fit + Pad)"
class CropPosition(StrEnum):
"""Supported crop and pad anchor positions."""
CENTER = "center"
TOP_LEFT = "top-left"
TOP = "top"
TOP_RIGHT = "top-right"
LEFT = "left"
RIGHT = "right"
BOTTOM_LEFT = "bottom-left"
BOTTOM = "bottom"
BOTTOM_RIGHT = "bottom-right"
@dataclass(frozen=True)
class ResizeTarget:
"""Requested output bounds and divisibility constraint."""
width: int
height: int
divisible_by: int = 1
@dataclass(frozen=True)
class ResizePlan:
"""Concrete resize, crop, and pad geometry for an image batch."""
resize_width: int
resize_height: int
output_width: int
output_height: int
crop_x: int = 0
crop_y: int = 0
pad_left: int = 0
pad_right: int = 0
pad_top: int = 0
pad_bottom: int = 0
@property
def has_crop(self) -> bool:
"""Return whether the plan crops after resizing."""
return self.crop_x > 0 or self.crop_y > 0
@property
def has_pad(self) -> bool:
"""Return whether the plan pads after resizing."""
return any(
side > 0
for side in (self.pad_left, self.pad_right, self.pad_top, self.pad_bottom)
)
def build_resize_plan(
source_width: int,
source_height: int,
target: ResizeTarget,
mode: ResizeMode | str,
position: CropPosition | str,
) -> ResizePlan:
"""Build resize geometry for the requested mode and source dimensions."""
_validate_positive_dimension(source_width, "source_width")
_validate_positive_dimension(source_height, "source_height")
_validate_positive_dimension(target.width, "target.width")
_validate_positive_dimension(target.height, "target.height")
normalized_mode = _coerce_resize_mode(mode)
normalized_position = _coerce_crop_position(position)
divisible_by = _normalize_divisible_by(target.divisible_by)
if normalized_mode is ResizeMode.STRETCH:
output_width, output_height = apply_divisibility(
target.width,
target.height,
divisible_by,
)
return ResizePlan(
resize_width=output_width,
resize_height=output_height,
output_width=output_width,
output_height=output_height,
)
if normalized_mode is ResizeMode.KEEP_AR:
resize_width, resize_height = fit_inside(
source_width,
source_height,
target.width,
target.height,
)
output_width, output_height = apply_divisibility(
resize_width,
resize_height,
divisible_by,
)
return ResizePlan(
resize_width=output_width,
resize_height=output_height,
output_width=output_width,
output_height=output_height,
)
output_width, output_height = apply_divisibility(
target.width,
target.height,
divisible_by,
)
if normalized_mode is ResizeMode.CROP:
resize_width, resize_height = cover_bounds(
source_width,
source_height,
output_width,
output_height,
)
crop_x, crop_y = calculate_crop_offsets(
normalized_position,
resize_width,
resize_height,
output_width,
output_height,
)
return ResizePlan(
resize_width=resize_width,
resize_height=resize_height,
output_width=output_width,
output_height=output_height,
crop_x=crop_x,
crop_y=crop_y,
)
if normalized_mode is ResizeMode.PAD:
resize_width, resize_height = fit_inside(
source_width,
source_height,
output_width,
output_height,
)
pad_width = output_width - resize_width
pad_height = output_height - resize_height
pad_left, pad_right, pad_top, pad_bottom = calculate_pad_sides(
normalized_position,
pad_width,
pad_height,
)
return ResizePlan(
resize_width=resize_width,
resize_height=resize_height,
output_width=output_width,
output_height=output_height,
pad_left=pad_left,
pad_right=pad_right,
pad_top=pad_top,
pad_bottom=pad_bottom,
)
raise ValueError(f"Unsupported resize mode: {mode!r}")
def fit_inside(
source_width: int,
source_height: int,
target_width: int,
target_height: int,
) -> tuple[int, int]:
"""Return dimensions that fit inside target bounds while preserving aspect."""
_validate_positive_dimension(source_width, "source_width")
_validate_positive_dimension(source_height, "source_height")
_validate_positive_dimension(target_width, "target_width")
_validate_positive_dimension(target_height, "target_height")
scale = min(target_width / source_width, target_height / source_height)
return (
max(1, int(round(source_width * scale))),
max(1, int(round(source_height * scale))),
)
def cover_bounds(
source_width: int,
source_height: int,
target_width: int,
target_height: int,
) -> tuple[int, int]:
"""Return dimensions that cover target bounds while preserving aspect."""
_validate_positive_dimension(source_width, "source_width")
_validate_positive_dimension(source_height, "source_height")
_validate_positive_dimension(target_width, "target_width")
_validate_positive_dimension(target_height, "target_height")
scale = max(target_width / source_width, target_height / source_height)
return (
max(1, int(math.ceil(source_width * scale))),
max(1, int(math.ceil(source_height * scale))),
)
def apply_divisibility(
width: int,
height: int,
divisible_by: int,
) -> tuple[int, int]:
"""Step dimensions down to positive multiples of the divisibility value."""
_validate_positive_dimension(width, "width")
_validate_positive_dimension(height, "height")
normalized = _normalize_divisible_by(divisible_by)
if normalized <= 1:
return width, height
output_width = width - (width % normalized)
output_height = height - (height % normalized)
if output_width <= 0 or output_height <= 0:
raise ValueError(
"Requested dimensions cannot satisfy divisible_by="
f"{normalized}: width={width}, height={height}."
)
return output_width, output_height
def calculate_crop_offsets(
position: CropPosition | str,
resized_width: int,
resized_height: int,
output_width: int,
output_height: int,
) -> tuple[int, int]:
"""Return the x and y offsets for cropping resized content."""
normalized_position = _coerce_crop_position(position)
_validate_positive_dimension(resized_width, "resized_width")
_validate_positive_dimension(resized_height, "resized_height")
_validate_positive_dimension(output_width, "output_width")
_validate_positive_dimension(output_height, "output_height")
if resized_width < output_width or resized_height < output_height:
raise ValueError(
"Crop dimensions must be at least as large as output dimensions: "
f"resized={resized_width}x{resized_height}, "
f"output={output_width}x{output_height}."
)
extra_width = resized_width - output_width
extra_height = resized_height - output_height
return (
_offset_for_axis(normalized_position, extra_width, horizontal=True),
_offset_for_axis(normalized_position, extra_height, horizontal=False),
)
def calculate_pad_sides(
position: CropPosition | str,
pad_width: int,
pad_height: int,
) -> tuple[int, int, int, int]:
"""Return left, right, top, and bottom padding for an anchor position."""
normalized_position = _coerce_crop_position(position)
if pad_width < 0 or pad_height < 0:
raise ValueError(
f"Padding cannot be negative: pad_width={pad_width}, "
f"pad_height={pad_height}."
)
left = _offset_for_axis(normalized_position, pad_width, horizontal=True)
top = _offset_for_axis(normalized_position, pad_height, horizontal=False)
right = pad_width - left
bottom = pad_height - top
return left, right, top, bottom
def _offset_for_axis(
position: CropPosition,
extra: int,
*,
horizontal: bool,
) -> int:
"""Resolve a crop or pad offset along one axis."""
if extra <= 0:
return 0
if horizontal:
if position in {
CropPosition.TOP_LEFT,
CropPosition.LEFT,
CropPosition.BOTTOM_LEFT,
}:
return 0
if position in {
CropPosition.TOP_RIGHT,
CropPosition.RIGHT,
CropPosition.BOTTOM_RIGHT,
}:
return extra
return extra // 2
if position in {
CropPosition.TOP_LEFT,
CropPosition.TOP,
CropPosition.TOP_RIGHT,
}:
return 0
if position in {
CropPosition.BOTTOM_LEFT,
CropPosition.BOTTOM,
CropPosition.BOTTOM_RIGHT,
}:
return extra
return extra // 2
def _coerce_resize_mode(mode: ResizeMode | str) -> ResizeMode:
"""Convert a raw resize mode value into a supported enum."""
try:
return mode if isinstance(mode, ResizeMode) else ResizeMode(str(mode))
except ValueError as exc:
raise ValueError(f"Unsupported resize mode: {mode!r}") from exc
def _coerce_crop_position(position: CropPosition | str) -> CropPosition:
"""Convert a raw crop position value into a supported enum."""
try:
return (
position
if isinstance(position, CropPosition)
else CropPosition(str(position))
)
except ValueError as exc:
raise ValueError(f"Unsupported crop_position: {position!r}") from exc
def _normalize_divisible_by(divisible_by: int) -> int:
"""Validate and normalize the divisibility constraint."""
normalized = int(divisible_by)
if normalized < 1:
raise ValueError(f"divisible_by must be at least 1, got {divisible_by}.")
return normalized
def _validate_positive_dimension(value: int, name: str) -> None:
"""Validate a positive integer dimension."""
if int(value) <= 0:
raise ValueError(f"{name} must be greater than 0, got {value}.")
+369
View File
@@ -0,0 +1,369 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Application service for resizing ComfyUI image batches."""
from __future__ import annotations
from collections.abc import Callable
from typing import Protocol, cast
import torch
from ..runtime.image_resamplers import (
NativeComfyResampler,
Processor,
validate_sampling,
)
from ..runtime.torchlanc_resampler import TorchLanczosResampler
from ..shared.logging import get_logger
from ..shared.tensor_validation import (
ImageTensorShape,
validate_image_tensor,
validate_mask_tensor,
)
from .resize_geometry import (
CropPosition,
ResizeMode,
ResizePlan,
ResizeTarget,
build_resize_plan,
)
class ProgressReporter(Protocol):
"""Progress reporting interface used by the resize service."""
def update(self, value: int) -> None:
"""Advance progress by the given number of items."""
NativeResamplerFactory = Callable[[Processor], NativeComfyResampler]
TorchLancFactory = Callable[[], TorchLanczosResampler]
ProgressFactory = Callable[[int], ProgressReporter]
_LOGGER = get_logger(__name__)
class ResizeImageToTargetService:
"""Resize ComfyUI image batches to a target geometry."""
def __init__(
self,
native_resampler_factory: NativeResamplerFactory | None = None,
torchlanc_factory: TorchLancFactory | None = None,
progress_factory: ProgressFactory | None = None,
) -> None:
"""Create the service with injectable runtime boundaries."""
self._native_resampler_factory = native_resampler_factory
self._torchlanc_factory = torchlanc_factory
self._progress_factory = progress_factory
def resize(
self,
image: torch.Tensor,
width: int,
height: int,
resize_mode: str,
sampling: str,
processor: str,
divisible_by: int,
crop_position: str,
pad_color: str,
max_batch_size: int,
sinc_window: int,
precision: str,
mask: torch.Tensor | None = None,
) -> tuple[torch.Tensor, int, int, torch.Tensor]:
"""Return resized image, output width, output height, and resized mask."""
try:
image_shape = validate_image_tensor(image)
if mask is not None:
validate_mask_tensor(mask, image_shape.batch_size)
validate_sampling(sampling)
normalized_processor = _coerce_processor(processor)
normalized_mode = ResizeMode(resize_mode)
normalized_position = CropPosition(crop_position)
plan = build_resize_plan(
image_shape.width,
image_shape.height,
ResizeTarget(
width=int(width), height=int(height), divisible_by=int(divisible_by)
),
normalized_mode,
normalized_position,
)
_LOGGER.debug(
"resize_start",
extra={
"source_width": image_shape.width,
"source_height": image_shape.height,
"output_width": plan.output_width,
"output_height": plan.output_height,
"mode": normalized_mode.value,
"sampling": sampling,
"processor": normalized_processor,
"batch_size": image_shape.batch_size,
},
)
output = self._resize_validated(
image=image,
image_shape=image_shape,
plan=plan,
sampling=sampling,
processor=normalized_processor,
pad_color=pad_color,
max_batch_size=int(max_batch_size),
sinc_window=int(sinc_window),
precision=precision,
mask=mask,
)
_LOGGER.debug(
"resize_complete",
extra={
"output_width": plan.output_width,
"output_height": plan.output_height,
"batch_size": image_shape.batch_size,
},
)
return output
except Exception:
_LOGGER.exception(
"resize_failed",
extra={
"width": width,
"height": height,
"resize_mode": resize_mode,
"sampling": sampling,
"processor": processor,
},
)
raise
def _resize_validated(
self,
image: torch.Tensor,
image_shape: ImageTensorShape,
plan: ResizePlan,
sampling: str,
processor: Processor,
pad_color: str,
max_batch_size: int,
sinc_window: int,
precision: str,
mask: torch.Tensor | None,
) -> tuple[torch.Tensor, int, int, torch.Tensor]:
"""Execute resizing after public inputs have been validated."""
image_bchw = image.float().clamp(0.0, 1.0).movedim(-1, 1)
mask_bchw = (
mask.float().clamp(0.0, 1.0).unsqueeze(1) if mask is not None else None
)
pad_values = parse_pad_color(pad_color, image_shape.channels)
output_images: list[torch.Tensor] = []
output_masks: list[torch.Tensor] = []
progress = self._create_progress(image_shape.batch_size)
for start, end in _chunk_spans(image_shape.batch_size, max_batch_size):
image_chunk = image_bchw[start:end]
resized = self._resize_image_chunk(
image_chunk,
plan,
sampling,
processor,
sinc_window,
precision,
)
finalized = _apply_crop_and_pad_image(resized, plan, pad_values)
output_images.append(finalized.to("cpu").movedim(1, -1))
if mask_bchw is not None:
mask_chunk = mask_bchw[start:end]
resized_mask = self._native_resampler(processor).resize(
mask_chunk,
plan.resize_width,
plan.resize_height,
"nearest-exact",
)
finalized_mask = _apply_crop_and_pad_mask(resized_mask, plan)
output_masks.append(finalized_mask.to("cpu").squeeze(1))
progress.update(end - start)
image_out = torch.cat(output_images, dim=0)
if output_masks:
mask_out = torch.cat(output_masks, dim=0)
else:
mask_out = torch.zeros(
(image_shape.batch_size, plan.output_height, plan.output_width),
dtype=torch.float32,
)
return image_out, plan.output_width, plan.output_height, mask_out
def _resize_image_chunk(
self,
samples: torch.Tensor,
plan: ResizePlan,
sampling: str,
processor: Processor,
sinc_window: int,
precision: str,
) -> torch.Tensor:
"""Route one image chunk to the selected runtime backend."""
with torch.inference_mode():
if processor == "gpu" and sampling == "lanczos":
return self._torchlanc_resampler().resize(
samples,
plan.resize_width,
plan.resize_height,
sinc_window,
precision,
)
return self._native_resampler(processor).resize(
samples,
plan.resize_width,
plan.resize_height,
sampling,
)
def _native_resampler(self, processor: Processor) -> NativeComfyResampler:
"""Create the native resampler for the current processor."""
if self._native_resampler_factory is not None:
return self._native_resampler_factory(processor)
return NativeComfyResampler(processor)
def _torchlanc_resampler(self) -> TorchLanczosResampler:
"""Create the TorchLanc resampler."""
if self._torchlanc_factory is not None:
return self._torchlanc_factory()
return TorchLanczosResampler()
def _create_progress(self, total: int) -> ProgressReporter:
"""Create a progress reporter for the batch."""
if self._progress_factory is not None:
return self._progress_factory(total)
import comfy.utils
return cast(ProgressReporter, comfy.utils.ProgressBar(total))
def parse_pad_color(color: str, channels: int) -> torch.Tensor:
"""Parse an RGB padding color into a normalized channel tensor."""
if channels not in (1, 3, 4):
raise ValueError(f"Unsupported channel count {channels}. Expected 1, 3, or 4.")
parts = [part.strip() for part in color.split(",")]
if len(parts) != 3:
raise ValueError(
"pad_color must contain exactly three comma-separated RGB values."
)
values: list[float] = []
for part in parts:
try:
value = int(part)
except ValueError as exc:
raise ValueError(
f"pad_color value {part!r} is not an integer RGB component."
) from exc
values.append(float(max(0, min(255, value))) / 255.0)
if channels == 1:
return torch.tensor([values[0]], dtype=torch.float32)
if channels == 4:
return torch.tensor([values[0], values[1], values[2], 1.0], dtype=torch.float32)
return torch.tensor(values, dtype=torch.float32)
def _apply_crop_and_pad_image(
samples: torch.Tensor,
plan: ResizePlan,
pad_values: torch.Tensor,
) -> torch.Tensor:
"""Apply final crop and pad operations to BCHW image samples."""
cropped = _crop_samples(samples, plan)
if not plan.has_pad:
return cropped
values = pad_values.to(device=cropped.device, dtype=cropped.dtype).view(1, -1, 1, 1)
canvas = values.expand(
cropped.shape[0],
cropped.shape[1],
plan.output_height,
plan.output_width,
).clone()
canvas[
:,
:,
plan.pad_top : plan.pad_top + cropped.shape[-2],
plan.pad_left : plan.pad_left + cropped.shape[-1],
] = cropped
return canvas
def _apply_crop_and_pad_mask(samples: torch.Tensor, plan: ResizePlan) -> torch.Tensor:
"""Apply final crop and pad operations to BCHW mask samples."""
cropped = _crop_samples(samples, plan)
if not plan.has_pad:
return cropped
canvas = torch.zeros(
(cropped.shape[0], 1, plan.output_height, plan.output_width),
dtype=cropped.dtype,
device=cropped.device,
)
canvas[
:,
:,
plan.pad_top : plan.pad_top + cropped.shape[-2],
plan.pad_left : plan.pad_left + cropped.shape[-1],
] = cropped
return canvas
def _crop_samples(samples: torch.Tensor, plan: ResizePlan) -> torch.Tensor:
"""Crop BCHW samples according to the resize plan."""
return samples[
:,
:,
plan.crop_y : plan.crop_y + plan.output_height,
plan.crop_x : plan.crop_x + plan.output_width,
]
def _chunk_spans(batch_size: int, max_batch_size: int) -> list[tuple[int, int]]:
"""Split a batch into inclusive-exclusive chunk spans."""
if max_batch_size <= 0 or max_batch_size >= batch_size:
return [(0, batch_size)]
spans: list[tuple[int, int]] = []
start = 0
while start < batch_size:
end = min(batch_size, start + max_batch_size)
spans.append((start, end))
start = end
return spans
def _coerce_processor(processor: str) -> Processor:
"""Validate and narrow a raw processor string."""
if processor == "cpu":
return "cpu"
if processor == "gpu":
return "gpu"
raise ValueError(f"processor must be 'cpu' or 'gpu', got {processor!r}.")
+9
View File
@@ -0,0 +1,9 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Masking services and pure mask operations for SimpleSyrup."""
from __future__ import annotations
__all__: list[str] = []
+279
View File
@@ -0,0 +1,279 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Pure mask composition and refinement helpers."""
from __future__ import annotations
from dataclasses import dataclass, replace
import torch
import torch.nn.functional as F
DETAIL_METHODS = (
"GuidedFilter",
"PyMatting",
"VITMatte",
)
@dataclass(frozen=True)
class MaskRefinementSettings:
"""Controls for standalone prompt mask post-processing."""
detail_method: str
detail_erode: int
detail_dilate: int
black_point: float
white_point: float
process_detail: bool
execution_device: str
max_size_pixels: int
def zero_mask_like_image(image: torch.Tensor) -> torch.Tensor:
"""Return an all-zero BHW mask matching a BHWC image tensor."""
batch_size, height, width, _channels = image.shape
return torch.zeros(
(int(batch_size), int(height), int(width)),
dtype=torch.float32,
device=image.device,
)
def compose_prompt_masks(
positive_mask: torch.Tensor,
negative_mask: torch.Tensor | None = None,
) -> torch.Tensor:
"""Return `positive_mask - negative_mask` clamped to ComfyUI mask range."""
positive = positive_mask.float()
if negative_mask is None:
return positive.clamp(0.0, 1.0)
return (positive - negative_mask.float()).clamp(0.0, 1.0)
def refine_prompt_mask(
mask: torch.Tensor,
image: torch.Tensor,
settings: MaskRefinementSettings,
) -> torch.Tensor:
"""Apply level remapping and optional edge-detail refinement."""
_validate_refinement_settings(settings)
output_device = mask.device
work_device = _resolve_refinement_device(settings.execution_device)
working_mask = mask.to(device=work_device, dtype=torch.float32).clamp(0.0, 1.0)
working_image = image.to(device=work_device, dtype=torch.float32).clamp(0.0, 1.0)
if settings.process_detail:
working_mask = _refine_detail(working_mask, working_image, settings)
remapped = histogram_remap(working_mask, settings.black_point, settings.white_point)
return remapped.to(device=output_device, dtype=torch.float32).clamp(0.0, 1.0)
def histogram_remap(
mask: torch.Tensor,
black_point: float,
white_point: float,
) -> torch.Tensor:
"""Remap mask values between black and white points into `[0, 1]`."""
if white_point <= black_point:
raise ValueError("white_point must be greater than black_point.")
return ((mask.float() - black_point) / (white_point - black_point)).clamp(0.0, 1.0)
def _validate_refinement_settings(settings: MaskRefinementSettings) -> None:
"""Reject invalid mask refinement settings."""
if settings.detail_method not in DETAIL_METHODS:
valid = ", ".join(DETAIL_METHODS)
raise ValueError(f"detail_method must be one of: {valid}.")
if settings.detail_erode < 0:
raise ValueError("detail_erode must be greater than or equal to 0.")
if settings.detail_dilate < 0:
raise ValueError("detail_dilate must be greater than or equal to 0.")
if not 0.0 <= settings.black_point < settings.white_point <= 1.0:
raise ValueError(
"black_point and white_point must satisfy 0 <= black < white <= 1."
)
if settings.execution_device not in ("auto", "cpu"):
raise ValueError("execution_device must be 'auto' or 'cpu'.")
if settings.max_size_pixels < 1:
raise ValueError("max_size_pixels must be at least 1.")
def _resolve_refinement_device(device: str) -> torch.device:
"""Return the requested torch device or fail clearly."""
return torch.device("cpu")
def _refine_detail(
mask: torch.Tensor,
image: torch.Tensor,
settings: MaskRefinementSettings,
) -> torch.Tensor:
"""Refine mask edges using a bounded-size native torch implementation."""
if settings.detail_method == "VITMatte":
raise ValueError(
"detail_method 'VITMatte' requires a connected VITMATTE_MODEL when "
"refine_mask is enabled. Connect ViTMatte Model Loader or choose "
"GuidedFilter."
)
height = int(mask.shape[-2])
width = int(mask.shape[-1])
max_dimension = max(height, width)
if max_dimension > settings.max_size_pixels:
scale = settings.max_size_pixels / float(max_dimension)
scaled_height = max(1, int(round(height * scale)))
scaled_width = max(1, int(round(width * scale)))
scaled_settings = replace(
settings,
detail_erode=max(1, int(round(settings.detail_erode * scale))),
detail_dilate=max(1, int(round(settings.detail_dilate * scale))),
)
small_mask = _resize_mask(mask, scaled_height, scaled_width)
small_image = _resize_image(image, scaled_height, scaled_width)
refined = _refine_detail_at_size(small_mask, small_image, scaled_settings)
return _resize_mask(refined, height, width)
return _refine_detail_at_size(mask, image, settings)
def _refine_detail_at_size(
mask: torch.Tensor,
image: torch.Tensor,
settings: MaskRefinementSettings,
) -> torch.Tensor:
"""Run one detail refinement pass at the current tensor size."""
mask_bchw = mask.unsqueeze(1)
edge_band = (
_dilate(mask_bchw, settings.detail_dilate)
- _erode(mask_bchw, settings.detail_erode)
).clamp(0.0, 1.0)
if settings.detail_method == "GuidedFilter":
smoothed = _guided_filter_mask(image, mask, _detail_radius(settings)).unsqueeze(
1
)
else:
smoothed = _box_blur(mask_bchw, _detail_radius(settings))
refined = mask_bchw * (1.0 - edge_band) + smoothed * edge_band
return refined.squeeze(1).clamp(0.0, 1.0)
def _detail_radius(settings: MaskRefinementSettings) -> int:
"""Return a method-specific smoothing radius."""
detail_range = settings.detail_erode + settings.detail_dilate
if settings.detail_method == "GuidedFilter":
return max(1, detail_range // 6 + 1)
if settings.detail_method == "PyMatting":
return max(1, detail_range // 8 + 1)
return max(1, detail_range // 4 + 1)
def _guided_filter_mask(
image: torch.Tensor,
mask: torch.Tensor,
radius: int,
epsilon: float = 1e-4,
) -> torch.Tensor:
"""Apply a grayscale guided filter to a BHW mask using a BHWC image."""
guidance = _grayscale_bchw(image)
target = mask.unsqueeze(1)
mean_i = _box_blur(guidance, radius)
mean_p = _box_blur(target, radius)
corr_i = _box_blur(guidance * guidance, radius)
corr_ip = _box_blur(guidance * target, radius)
var_i = corr_i - mean_i * mean_i
cov_ip = corr_ip - mean_i * mean_p
a = cov_ip / (var_i + epsilon)
b = mean_p - a * mean_i
mean_a = _box_blur(a, radius)
mean_b = _box_blur(b, radius)
return (mean_a * guidance + mean_b).squeeze(1).clamp(0.0, 1.0)
def _grayscale_bchw(image: torch.Tensor) -> torch.Tensor:
"""Convert a BHWC image tensor to B1HW grayscale guidance."""
channels = int(image.shape[-1])
if channels >= 3:
weights = torch.tensor(
[0.299, 0.587, 0.114],
dtype=image.dtype,
device=image.device,
)
gray = (image[..., :3] * weights).sum(dim=-1)
else:
gray = image[..., 0]
return gray.unsqueeze(1)
def _box_blur(samples: torch.Tensor, radius: int) -> torch.Tensor:
"""Blur BCHW samples with edge-aware average pooling."""
if radius <= 0:
return samples
kernel_size = radius * 2 + 1
return F.avg_pool2d(
samples,
kernel_size=kernel_size,
stride=1,
padding=radius,
count_include_pad=False,
)
def _dilate(samples: torch.Tensor, radius: int) -> torch.Tensor:
"""Dilate BCHW mask samples."""
if radius <= 0:
return samples
kernel_size = radius * 2 + 1
padded = F.pad(samples, (radius, radius, radius, radius), value=0.0)
return F.max_pool2d(padded, kernel_size=kernel_size, stride=1)
def _erode(samples: torch.Tensor, radius: int) -> torch.Tensor:
"""Erode BCHW mask samples."""
if radius <= 0:
return samples
kernel_size = radius * 2 + 1
padded = F.pad(samples, (radius, radius, radius, radius), value=0.0)
return -F.max_pool2d(-padded, kernel_size=kernel_size, stride=1)
def _resize_mask(mask: torch.Tensor, height: int, width: int) -> torch.Tensor:
"""Resize a BHW mask tensor."""
return F.interpolate(
mask.unsqueeze(1),
size=(height, width),
mode="bilinear",
align_corners=False,
).squeeze(1)
def _resize_image(image: torch.Tensor, height: int, width: int) -> torch.Tensor:
"""Resize a BHWC image tensor."""
resized = F.interpolate(
image.movedim(-1, 1),
size=(height, width),
mode="bilinear",
align_corners=False,
)
return resized.movedim(1, -1)
@@ -0,0 +1,402 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Application service for prompt-based SAM SEGS construction."""
from __future__ import annotations
from dataclasses import dataclass
import torch
from ..domain.segs import (
BoundingBox,
NativeSegs,
Segment,
sort_segs,
)
from ..masking.mask_ops import MaskRefinementSettings, refine_prompt_mask
from ..masking.segs_mask_ops import (
crop_image,
crop_mask,
crop_region_for_bbox,
dilate_mask,
normalize_mask,
validate_single_image,
)
from ..runtime.sam_segmenter import SAMBoxSegmenter, SAMModelSegmenter
from ..runtime.text_box_detector import (
GroundingDINOTextBoxDetector,
TextBoxDetection,
TextBoxDetector,
)
from ..runtime.vitmatte_refiner import MaskDetailRefiner, ViTMatteRefiner
@dataclass(frozen=True)
class PromptSegsRuntime:
"""Runtime dependencies used to detect prompt boxes and segment masks."""
detector: TextBoxDetector
segmenter: SAMBoxSegmenter
@dataclass(frozen=True)
class PromptSegsSettings:
"""Validated controls for prompt-to-SEGS construction."""
positive_prompt: str
negative_prompt: str
confidence_threshold: float
size_threshold: int
bbox_dilation: int
mask_dilation: int
crop_factor: float
sort_order: str
refinement: MaskRefinementSettings
class PromptSEGSWithSAMService:
"""Validate inputs and build prompt-derived SEGS with SAM masks."""
def __init__(
self,
runtime: PromptSegsRuntime | None = None,
vitmatte_refiner: MaskDetailRefiner | None = None,
) -> None:
"""Create the service with injectable runtime dependencies."""
self._runtime = runtime or PromptSegsRuntime(
detector=GroundingDINOTextBoxDetector(),
segmenter=SAMModelSegmenter(),
)
self._vitmatte_refiner = vitmatte_refiner or ViTMatteRefiner()
def prompt(
self,
image: object,
sam_model: object,
grounding_dino_model: object,
vitmatte_model: object | None,
positive_prompt: str,
negative_prompt: str,
confidence_threshold: float,
size_threshold: int,
bbox_dilation: int,
mask_dilation: int,
detail_method: str,
detail_erode: int,
detail_dilate: int,
black_point: float,
white_point: float,
refine_mask: bool,
mask_refinement_max_size: int,
execution_device: str,
crop_factor: float,
sort_order: str,
) -> NativeSegs:
"""Return sorted native SEGS for a text-prompted SAM detection."""
image_tensor = validate_single_image(image, "Prompt SEGS w/ SAM")
settings = self._validate_settings(
positive_prompt=positive_prompt,
negative_prompt=negative_prompt,
confidence_threshold=confidence_threshold,
size_threshold=size_threshold,
bbox_dilation=bbox_dilation,
mask_dilation=mask_dilation,
detail_method=detail_method,
detail_erode=detail_erode,
detail_dilate=detail_dilate,
black_point=black_point,
white_point=white_point,
refine_mask=refine_mask,
mask_refinement_max_size=mask_refinement_max_size,
execution_device=execution_device,
crop_factor=crop_factor,
sort_order=sort_order,
)
sample = image_tensor[0]
height = int(sample.shape[0])
width = int(sample.shape[1])
positive_regions = self._prompt_regions(
image=sample,
sam_model=sam_model,
grounding_dino_model=grounding_dino_model,
prompt=settings.positive_prompt,
settings=settings,
execution_device=execution_device,
)
negative_mask = self._negative_mask(
image=sample,
sam_model=sam_model,
grounding_dino_model=grounding_dino_model,
settings=settings,
execution_device=execution_device,
height=height,
width=width,
)
segments: list[Segment] = []
for detection, mask in positive_regions:
final_mask = (mask - negative_mask).clamp(0.0, 1.0)
final_mask = dilate_mask(final_mask, settings.mask_dilation)
final_mask = self._refine_mask(
image_tensor=image_tensor,
mask=final_mask,
settings=settings,
vitmatte_model=vitmatte_model,
)
bbox = _bbox_from_mask(final_mask)
if bbox is None:
continue
if (
bbox.width < settings.size_threshold
or bbox.height < settings.size_threshold
):
continue
crop_region = crop_region_for_bbox(
bbox,
image_height=height,
image_width=width,
crop_factor=settings.crop_factor,
)
segments.append(
Segment(
cropped_image=crop_image(image_tensor, crop_region)
.detach()
.clone(),
cropped_mask=crop_mask(final_mask, crop_region).detach().clone(),
confidence=detection.confidence,
crop_region=crop_region,
bbox=bbox,
label=settings.positive_prompt,
)
)
return sort_segs(((height, width), tuple(segments)), settings.sort_order)
def _prompt_regions(
self,
image: torch.Tensor,
sam_model: object,
grounding_dino_model: object,
prompt: str,
settings: PromptSegsSettings,
execution_device: str,
) -> tuple[tuple[TextBoxDetection, torch.Tensor], ...]:
"""Return detected prompt boxes with their SAM masks."""
detections = self._runtime.detector.detect(
grounding_dino_model,
image,
prompt,
settings.confidence_threshold,
execution_device,
)
if not detections:
return ()
height = int(image.shape[0])
width = int(image.shape[1])
expanded_boxes = tuple(
_expand_bbox(detection.bbox, settings.bbox_dilation, height, width)
for detection in detections
)
masks = self._runtime.segmenter.segment_boxes(
sam_model,
image,
_boxes_tensor(expanded_boxes),
settings.confidence_threshold,
execution_device,
)
if len(masks) != len(detections):
raise ValueError(
"Prompt SEGS w/ SAM runtime returned a mask count that does not "
"match prompt detections."
)
return tuple(
(
detection,
normalize_mask(mask, height, width),
)
for detection, mask in zip(detections, masks, strict=True)
)
def _negative_mask(
self,
image: torch.Tensor,
sam_model: object,
grounding_dino_model: object,
settings: PromptSegsSettings,
execution_device: str,
height: int,
width: int,
) -> torch.Tensor:
"""Return one combined negative prompt mask."""
if not settings.negative_prompt:
return torch.zeros((height, width), dtype=torch.float32)
negative_regions = self._prompt_regions(
image=image,
sam_model=sam_model,
grounding_dino_model=grounding_dino_model,
prompt=settings.negative_prompt,
settings=settings,
execution_device=execution_device,
)
if not negative_regions:
return torch.zeros((height, width), dtype=torch.float32)
return torch.stack([mask for _detection, mask in negative_regions]).amax(dim=0)
def _refine_mask(
self,
image_tensor: torch.Tensor,
mask: torch.Tensor,
settings: PromptSegsSettings,
vitmatte_model: object | None,
) -> torch.Tensor:
"""Apply configured mask refinement to one full-image mask."""
mask_batch = mask.unsqueeze(0)
if (
settings.refinement.process_detail
and settings.refinement.detail_method == "VITMatte"
):
vitmatte_refined = self._vitmatte_refiner.refine(
image_tensor,
mask_batch,
settings.refinement,
vitmatte_model,
)
remap_settings = MaskRefinementSettings(
detail_method="GuidedFilter",
detail_erode=0,
detail_dilate=0,
black_point=settings.refinement.black_point,
white_point=settings.refinement.white_point,
process_detail=False,
execution_device=settings.refinement.execution_device,
max_size_pixels=settings.refinement.max_size_pixels,
)
return refine_prompt_mask(
vitmatte_refined,
image_tensor,
remap_settings,
)[0]
return refine_prompt_mask(mask_batch, image_tensor, settings.refinement)[0]
def _validate_settings(
self,
positive_prompt: str,
negative_prompt: str,
confidence_threshold: float,
size_threshold: int,
bbox_dilation: int,
mask_dilation: int,
detail_method: str,
detail_erode: int,
detail_dilate: int,
black_point: float,
white_point: float,
refine_mask: bool,
mask_refinement_max_size: int,
execution_device: str,
crop_factor: float,
sort_order: str,
) -> PromptSegsSettings:
"""Validate public node settings and return normalized values."""
positive_text = positive_prompt.strip()
if not positive_text:
raise ValueError("positive_prompt is required for Prompt SEGS w/ SAM.")
if not 0.0 <= confidence_threshold <= 1.0:
raise ValueError("confidence_threshold must be between 0 and 1.")
if size_threshold < 1:
raise ValueError("size_threshold must be at least 1.")
if crop_factor < 1.0:
raise ValueError("crop_factor must be at least 1.0.")
if mask_refinement_max_size < 1:
raise ValueError("mask_refinement_max_size must be at least 1.")
refinement = MaskRefinementSettings(
detail_method=detail_method,
detail_erode=int(detail_erode),
detail_dilate=int(detail_dilate),
black_point=float(black_point),
white_point=float(white_point),
process_detail=bool(refine_mask),
execution_device=execution_device,
max_size_pixels=int(mask_refinement_max_size),
)
refine_prompt_mask(
torch.zeros((1, 1, 1), dtype=torch.float32),
torch.zeros((1, 1, 1, 3), dtype=torch.float32),
MaskRefinementSettings(
detail_method=refinement.detail_method,
detail_erode=refinement.detail_erode,
detail_dilate=refinement.detail_dilate,
black_point=refinement.black_point,
white_point=refinement.white_point,
process_detail=False,
execution_device=refinement.execution_device,
max_size_pixels=refinement.max_size_pixels,
),
)
return PromptSegsSettings(
positive_prompt=positive_text,
negative_prompt=negative_prompt.strip(),
confidence_threshold=float(confidence_threshold),
size_threshold=int(size_threshold),
bbox_dilation=int(bbox_dilation),
mask_dilation=int(mask_dilation),
crop_factor=float(crop_factor),
sort_order=sort_order,
refinement=refinement,
)
def _expand_bbox(
bbox: BoundingBox,
dilation: int,
image_height: int,
image_width: int,
) -> BoundingBox:
"""Expand a bbox by a signed pixel radius and clamp it to image bounds."""
left = max(0, min(image_width, bbox.left - dilation))
top = max(0, min(image_height, bbox.top - dilation))
right = max(0, min(image_width, bbox.right + dilation))
bottom = max(0, min(image_height, bbox.bottom + dilation))
if right <= left or bottom <= top:
return bbox
return BoundingBox(left, top, right, bottom)
def _boxes_tensor(boxes: tuple[BoundingBox, ...]) -> torch.Tensor:
"""Convert bbox values to an XYXY tensor for SAM runtimes."""
if not boxes:
return torch.empty((0, 4), dtype=torch.float32)
return torch.tensor(
[[box.left, box.top, box.right, box.bottom] for box in boxes],
dtype=torch.float32,
)
def _bbox_from_mask(mask: torch.Tensor) -> BoundingBox | None:
"""Return the tight bbox around a nonzero HW mask."""
y_coords, x_coords = torch.where(mask > 0)
if y_coords.numel() == 0 or x_coords.numel() == 0:
return None
left = int(torch.min(x_coords).item())
top = int(torch.min(y_coords).item())
right = int(torch.max(x_coords).item()) + 1
bottom = int(torch.max(y_coords).item()) + 1
return BoundingBox(left, top, right, bottom)
@@ -0,0 +1,263 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Portions of this file incorporate behavior derived from
# multidiffusion-upscaler-for-automatic1111. See third_party/manifest.toml and
# third_party/NOTICE.md.
"""Mask conversion helpers for regional SEGS detailing."""
from __future__ import annotations
import math
import torch
from ..domain.regional_detailing import (
ImageRegion,
LatentBox,
LatentRegion,
SegmentConditioningPair,
)
from ..domain.segs import CropRegion
from .segs_mask_ops import feather_mask, resize_mask
OPERATION = "Detail SEGS as Regions"
def build_image_regions(
pairs: tuple[SegmentConditioningPair, ...],
*,
image_height: int,
image_width: int,
) -> tuple[ImageRegion, ...]:
"""Create full-image masks from paired SEGS crop-local masks."""
return tuple(
_image_region_from_pair(
pair,
image_height=image_height,
image_width=image_width,
)
for pair in pairs
)
def build_latent_regions(
image_regions: tuple[ImageRegion, ...],
*,
latent_height: int,
latent_width: int,
device: torch.device,
dtype: torch.dtype,
latent_feather: int,
) -> tuple[LatentRegion, ...]:
"""Convert image-space regions into latent-space masks and boxes."""
if latent_height < 1 or latent_width < 1:
raise ValueError("latent dimensions must be positive.")
latent_regions: list[LatentRegion] = []
for region in image_regions:
latent_mask = resize_mask(region.image_mask, latent_height, latent_width)
if latent_feather > 0:
latent_mask = feather_mask(latent_mask, latent_feather)
latent_mask = latent_mask.to(device=device, dtype=dtype).clamp(0.0, 1.0)
latent_box = latent_box_from_mask(
latent_mask,
region_index=region.index,
label=region.label,
)
latent_regions.append(
LatentRegion(
index=region.index,
label=region.label,
latent_box=latent_box,
latent_mask=latent_mask,
positive=region.positive,
)
)
return tuple(latent_regions)
def scale_image_regions(
image_regions: tuple[ImageRegion, ...],
*,
image_height: int,
image_width: int,
) -> tuple[ImageRegion, ...]:
"""Resize full-image region masks into a scaled image coordinate space."""
if image_height < 1 or image_width < 1:
raise ValueError("scaled image dimensions must be positive.")
return tuple(
ImageRegion(
index=region.index,
label=region.label,
crop_region=_scale_crop_region(
region.crop_region,
source_height=int(region.image_mask.shape[0]),
source_width=int(region.image_mask.shape[1]),
target_height=image_height,
target_width=image_width,
),
image_mask=resize_mask(region.image_mask, image_height, image_width),
positive=region.positive,
)
for region in image_regions
)
def latent_box_from_mask(
mask: torch.Tensor,
*,
region_index: int,
label: str,
) -> LatentBox:
"""Return the tight latent box around a non-empty HW mask."""
if mask.ndim != 2:
raise ValueError("latent region mask must be HW shaped.")
coordinates = torch.nonzero(mask > 0, as_tuple=False)
if coordinates.numel() == 0:
raise ValueError(
f"{OPERATION} SEG {region_index} ('{label}') produced an empty "
"latent region."
)
top = int(coordinates[:, 0].min().item())
bottom = int(coordinates[:, 0].max().item()) + 1
left = int(coordinates[:, 1].min().item())
right = int(coordinates[:, 1].max().item()) + 1
return LatentBox(
x=left,
y=top,
width=right - left,
height=bottom - top,
)
def union_masks(masks: tuple[torch.Tensor, ...]) -> torch.Tensor:
"""Return the clamped union of one or more same-shaped HW masks."""
if not masks:
raise ValueError("at least one mask is required.")
shape = masks[0].shape
if any(mask.shape != shape for mask in masks):
raise ValueError("all masks must have the same shape.")
union = torch.zeros_like(masks[0], dtype=torch.float32)
for mask in masks:
union = torch.maximum(union, mask.float())
return union.clamp(0.0, 1.0)
def feather_image_mask(mask: torch.Tensor, feather: int) -> torch.Tensor:
"""Feather an image-space mask while preserving the HW contract."""
return feather_mask(mask, feather)
def proportional_latent_box(
*,
left: int,
top: int,
right: int,
bottom: int,
image_height: int,
image_width: int,
latent_height: int,
latent_width: int,
) -> LatentBox:
"""Convert an image-space box to a clamped latent-space box."""
if image_height < 1 or image_width < 1:
raise ValueError("image dimensions must be positive.")
x = max(0, min(latent_width - 1, math.floor(left * latent_width / image_width)))
y = max(0, min(latent_height - 1, math.floor(top * latent_height / image_height)))
x2 = max(x + 1, min(latent_width, math.ceil(right * latent_width / image_width)))
y2 = max(
y + 1, min(latent_height, math.ceil(bottom * latent_height / image_height))
)
return LatentBox(x=x, y=y, width=x2 - x, height=y2 - y)
def _scale_crop_region(
region: CropRegion,
*,
source_height: int,
source_width: int,
target_height: int,
target_width: int,
) -> CropRegion:
"""Scale a crop region proportionally into a target image shape."""
if source_height < 1 or source_width < 1:
raise ValueError("source image dimensions must be positive.")
left = max(
0, min(target_width - 1, math.floor(region.left * target_width / source_width))
)
top = max(
0,
min(target_height - 1, math.floor(region.top * target_height / source_height)),
)
right = max(
left + 1,
min(target_width, math.ceil(region.right * target_width / source_width)),
)
bottom = max(
top + 1,
min(target_height, math.ceil(region.bottom * target_height / source_height)),
)
return CropRegion(left, top, right, bottom)
def _image_region_from_pair(
pair: SegmentConditioningPair,
*,
image_height: int,
image_width: int,
) -> ImageRegion:
"""Paste one pair's crop-local mask into full-image mask space."""
crop_mask = _crop_mask_to_hw(
pair.segment.cropped_mask,
height=pair.segment.crop_region.height,
width=pair.segment.crop_region.width,
index=pair.index,
label=pair.segment.label,
)
full_mask = torch.zeros((image_height, image_width), dtype=torch.float32)
region = pair.segment.crop_region
full_mask[region.top : region.bottom, region.left : region.right] = crop_mask
return ImageRegion(
index=pair.index,
label=pair.segment.label,
crop_region=region,
image_mask=full_mask.clamp(0.0, 1.0),
positive=pair.positive,
)
def _crop_mask_to_hw(
mask: object,
*,
height: int,
width: int,
index: int,
label: str,
) -> torch.Tensor:
"""Return a crop-local HW mask resized to the segment crop region."""
mask_tensor = torch.as_tensor(mask).float()
if mask_tensor.ndim == 3 and int(mask_tensor.shape[0]) == 1:
mask_tensor = mask_tensor[0]
if mask_tensor.ndim != 2:
raise ValueError(
f"{OPERATION} SEG {index} ('{label}') cropped_mask must be HW or "
"single-batch BHW shaped."
)
resized = resize_mask(mask_tensor, height, width)
if not torch.any(resized > 0):
raise ValueError(
f"{OPERATION} SEG {index} ('{label}') produced an empty image region."
)
return resized.float().clamp(0.0, 1.0)
+197
View File
@@ -0,0 +1,197 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Mask and crop helpers for SEGS detection and detailing."""
from __future__ import annotations
import torch
import torch.nn.functional as F
from ..domain.segs import BoundingBox, CropRegion
def validate_single_image(image: object, operation: str) -> torch.Tensor:
"""Return a validated single-image BHWC tensor."""
image_batch = validate_image_batch(image, operation)
batch_size = int(image_batch.shape[0])
if batch_size != 1:
raise ValueError(
f"{operation} currently supports one image at a time; "
f"received batch size {batch_size}."
)
return image_batch
def validate_image_batch(image: object, operation: str) -> torch.Tensor:
"""Return a validated BHWC image batch tensor."""
if not isinstance(image, torch.Tensor):
raise TypeError(f"{operation} requires a torch IMAGE tensor.")
if image.ndim != 4:
raise ValueError(f"{operation} requires a BHWC IMAGE tensor.")
batch_size = int(image.shape[0])
if batch_size < 1:
raise ValueError(f"{operation} requires at least one image.")
channels = int(image.shape[-1])
if channels < 1:
raise ValueError(f"{operation} requires at least one image channel.")
return image.float().clamp(0.0, 1.0)
def iter_single_images(image_batch: torch.Tensor) -> tuple[torch.Tensor, ...]:
"""Return one-image BHWC slices from a validated image batch."""
return tuple(
image_batch[index : index + 1] for index in range(int(image_batch.shape[0]))
)
def rectangular_mask(height: int, width: int, bbox: BoundingBox) -> torch.Tensor:
"""Create a full-image mask filled inside a bounding box."""
mask = torch.zeros((height, width), dtype=torch.float32)
mask[bbox.top : bbox.bottom, bbox.left : bbox.right] = 1.0
return mask
def normalize_mask(mask: torch.Tensor, height: int, width: int) -> torch.Tensor:
"""Normalize a mask to an unbatched HW tensor in float range."""
working = mask.float()
if working.ndim == 3:
working = working[0]
if working.ndim != 2:
raise ValueError("Detection mask must be HW or BHW shaped.")
if int(working.shape[0]) != height or int(working.shape[1]) != width:
working = (
F.interpolate(
working.unsqueeze(0).unsqueeze(0),
size=(height, width),
mode="bilinear",
align_corners=False,
)
.squeeze(0)
.squeeze(0)
)
return working.clamp(0.0, 1.0)
def dilate_mask(mask: torch.Tensor, dilation: int) -> torch.Tensor:
"""Morph an HW mask by a signed pixel radius."""
if dilation == 0:
return mask.float().clamp(0.0, 1.0)
radius = abs(dilation)
kernel_size = radius * 2 + 1
padded = F.pad(
mask.float().unsqueeze(0).unsqueeze(0),
(radius, radius, radius, radius),
value=0.0,
)
if dilation > 0:
return (
F.max_pool2d(padded, kernel_size=kernel_size, stride=1)
.squeeze(0)
.squeeze(0)
)
return (
(-F.max_pool2d(-padded, kernel_size=kernel_size, stride=1))
.squeeze(0)
.squeeze(0)
)
def feather_mask(mask: torch.Tensor, radius: int) -> torch.Tensor:
"""Soften an HW mask edge by a pixel radius."""
if radius < 0:
raise ValueError("feather radius must be greater than or equal to 0.")
if radius == 0:
return mask.float().clamp(0.0, 1.0)
kernel_size = radius * 2 + 1
blurred = (
F.avg_pool2d(
mask.float().unsqueeze(0).unsqueeze(0),
kernel_size=kernel_size,
stride=1,
padding=radius,
count_include_pad=False,
)
.squeeze(0)
.squeeze(0)
)
return blurred.clamp(0.0, 1.0)
def crop_region_for_bbox(
bbox: BoundingBox,
image_height: int,
image_width: int,
crop_factor: float,
) -> CropRegion:
"""Expand a bbox around its center or select the full image for zero."""
if crop_factor == 0.0:
return CropRegion(0, 0, image_width, image_height)
if crop_factor < 1.0:
raise ValueError("crop_factor must be 0 or greater than or equal to 1.0.")
center_x = (bbox.left + bbox.right) / 2.0
center_y = (bbox.top + bbox.bottom) / 2.0
crop_width = bbox.width * crop_factor
crop_height = bbox.height * crop_factor
left = max(0, int(round(center_x - crop_width / 2.0)))
top = max(0, int(round(center_y - crop_height / 2.0)))
right = min(image_width, int(round(center_x + crop_width / 2.0)))
bottom = min(image_height, int(round(center_y + crop_height / 2.0)))
if right <= left:
right = min(image_width, left + 1)
if bottom <= top:
bottom = min(image_height, top + 1)
return CropRegion(left, top, right, bottom)
def crop_image(image: torch.Tensor, region: CropRegion) -> torch.Tensor:
"""Crop a BHWC image tensor by a crop region."""
return image[:, region.top : region.bottom, region.left : region.right, :]
def crop_mask(mask: torch.Tensor, region: CropRegion) -> torch.Tensor:
"""Crop an HW mask tensor by a crop region."""
return mask[region.top : region.bottom, region.left : region.right]
def resize_image(image: torch.Tensor, height: int, width: int) -> torch.Tensor:
"""Resize a BHWC image tensor with bilinear interpolation."""
resized = F.interpolate(
image.movedim(-1, 1),
size=(height, width),
mode="bilinear",
align_corners=False,
)
return resized.movedim(1, -1).clamp(0.0, 1.0)
def resize_mask(mask: torch.Tensor, height: int, width: int) -> torch.Tensor:
"""Resize an HW or BHW mask tensor with bilinear interpolation."""
if mask.ndim == 2:
working = mask.unsqueeze(0)
elif mask.ndim == 3:
working = mask
else:
raise ValueError("Mask must be HW or BHW shaped.")
resized = F.interpolate(
working.unsqueeze(1).float(),
size=(height, width),
mode="bilinear",
align_corners=False,
).squeeze(1)
if mask.ndim == 2:
return resized.squeeze(0).clamp(0.0, 1.0)
return resized.clamp(0.0, 1.0)
+137
View File
@@ -0,0 +1,137 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node registration for SimpleSyrup."""
from __future__ import annotations
from .conditioning_batch_pack import ConditioningBatchAppend, ConditioningBatchStart
from .detail_segs_as_regions import DetailSEGSAsRegions
from .detail_segs_by_scale_factor import DetailSEGSByScaleFactor
from .detail_segs_by_scale_factor_tiled_diffusion import (
DetailSEGSByScaleFactorTiledDiffusion,
)
from .detect_segs_with_ultralytics import DetectSEGSWithUltralytics
from .encode_prompt_batch import EncodePromptBatch
from .grounded_sam_model_info import GroundedSAMModelInfo
from .grounding_dino_model_loader import GroundingDINOModelLoader
from .image_resize_to_target import ResizeImageToTarget
from .ksampler_extras import KSamplerExtras
from .ksampler_tiled_diffusion import KSamplerTiledDiffusion
from .latent_diagnostics import LatentDiagnostics
from .layerstyle_sam_models_adapter import LayerStyleSAMModelsAdapter
from .load_ultralytics_model import LoadUltralyticsModel
from .prompt_encode_style import PromptEncodeStyle
from .prompt_encode_style_and_normalization import PromptEncodeStyleAndNormalization
from .prompt_segs_with_sam import PromptSEGSWithSAM
from .provenance_latent import SimpleVAEEncode, UpscaleLatentFromImage
from .sam_model_loader import SAMModelLoader
from .scale_factor import ScaleFactor
from .seed import Seed
from .simple_load_anima import SimpleLoadAnima
from .simple_load_checkpoint import SimpleLoadCheckpoint
from .tile_and_tag_segs import TileAndTagSEGS
from .vitmatte_model_loader import ViTMatteModelLoader
from .wd14_tagger_loader import WD14TaggerLoader
NODE_CLASS_MAPPINGS = {
"SimpleSyrup.ConditioningBatchAppend": ConditioningBatchAppend,
"SimpleSyrup.ConditioningBatchStart": ConditioningBatchStart,
"SimpleSyrup.GroundedSAMModelInfo": GroundedSAMModelInfo,
"SimpleSyrup.GroundingDINOModelLoader": GroundingDINOModelLoader,
"SimpleSyrup.KSamplerExtras": KSamplerExtras,
"SimpleSyrup.KSamplerTiledDiffusion": KSamplerTiledDiffusion,
"SimpleSyrup.LayerStyleSAMModelsAdapter": LayerStyleSAMModelsAdapter,
"SimpleSyrup.LatentDiagnostics": LatentDiagnostics,
"SimpleSyrup.PromptEncodeStyle": PromptEncodeStyle,
"SimpleSyrup.PromptEncodeStyleAndNormalization": PromptEncodeStyleAndNormalization,
"SimpleSyrup.PromptSEGSWithSAM": PromptSEGSWithSAM,
"SimpleSyrup.SimpleVAEEncode": SimpleVAEEncode,
"SimpleSyrup.UpscaleLatentFromImage": UpscaleLatentFromImage,
"SimpleSyrup.ResizeImageToTarget": ResizeImageToTarget,
"SimpleSyrup.DetailSEGSAsRegions": DetailSEGSAsRegions,
"SimpleSyrup.DetailSEGSByScaleFactor": DetailSEGSByScaleFactor,
"SimpleSyrup.DetailSEGSByScaleFactorTiledDiffusion": (
DetailSEGSByScaleFactorTiledDiffusion
),
"SimpleSyrup.SAMModelLoader": SAMModelLoader,
"SimpleSyrup.ScaleFactor": ScaleFactor,
"SimpleSyrup.Seed": Seed,
"SimpleSyrup.SimpleLoadAnima": SimpleLoadAnima,
"SimpleSyrup.SimpleLoadCheckpoint": SimpleLoadCheckpoint,
"SimpleSyrup.LoadUltralyticsModel": LoadUltralyticsModel,
"SimpleSyrup.DetectSEGSWithUltralytics": DetectSEGSWithUltralytics,
"SimpleSyrup.EncodePromptBatch": EncodePromptBatch,
"SimpleSyrup.TileAndTagSEGS": TileAndTagSEGS,
"SimpleSyrup.ViTMatteModelLoader": ViTMatteModelLoader,
"SimpleSyrup.WD14TaggerLoader": WD14TaggerLoader,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"SimpleSyrup.ConditioningBatchAppend": "Conditioning Batch Append",
"SimpleSyrup.ConditioningBatchStart": "Conditioning Batch Start",
"SimpleSyrup.GroundedSAMModelInfo": "Grounded SAM Model Info",
"SimpleSyrup.GroundingDINOModelLoader": "GroundingDINO Model Loader",
"SimpleSyrup.KSamplerExtras": "KSampler (Extras)",
"SimpleSyrup.KSamplerTiledDiffusion": "KSampler (Tiled Diffusion)",
"SimpleSyrup.LayerStyleSAMModelsAdapter": "LayerStyle SAM Models Adapter",
"SimpleSyrup.LatentDiagnostics": "Latent Diagnostics",
"SimpleSyrup.PromptEncodeStyle": "Prompt Encode Style",
"SimpleSyrup.PromptEncodeStyleAndNormalization": (
"Prompt Encode Style & Normalization"
),
"SimpleSyrup.PromptSEGSWithSAM": "Prompt SEGS w/ SAM",
"SimpleSyrup.SimpleVAEEncode": "Simple VAE Encode",
"SimpleSyrup.UpscaleLatentFromImage": "Upscale Latent From Image",
"SimpleSyrup.ResizeImageToTarget": "Resize Image to Target",
"SimpleSyrup.DetailSEGSAsRegions": "Detail SEGS as Regions",
"SimpleSyrup.DetailSEGSByScaleFactor": "Detail SEGS by Scale Factor",
"SimpleSyrup.DetailSEGSByScaleFactorTiledDiffusion": (
"Detail SEGS by Scale Factor w/ Tiled Diffusion"
),
"SimpleSyrup.SAMModelLoader": "SAM Model Loader",
"SimpleSyrup.ScaleFactor": "Scale Factor",
"SimpleSyrup.Seed": "Seed",
"SimpleSyrup.SimpleLoadAnima": "Simple Load Anima",
"SimpleSyrup.SimpleLoadCheckpoint": "Simple Load Checkpoint",
"SimpleSyrup.LoadUltralyticsModel": "Load Ultralytics Model",
"SimpleSyrup.DetectSEGSWithUltralytics": "Detect SEGS w/ Ultralytics",
"SimpleSyrup.EncodePromptBatch": "Encode Prompt Batch",
"SimpleSyrup.TileAndTagSEGS": "Tile & Tag SEGS",
"SimpleSyrup.ViTMatteModelLoader": "ViTMatte Model Loader",
"SimpleSyrup.WD14TaggerLoader": "Load WD14 Tagger",
}
__all__ = [
"ConditioningBatchAppend",
"ConditioningBatchStart",
"GroundedSAMModelInfo",
"GroundingDINOModelLoader",
"KSamplerExtras",
"KSamplerTiledDiffusion",
"LayerStyleSAMModelsAdapter",
"LatentDiagnostics",
"DetectSEGSWithUltralytics",
"DetailSEGSAsRegions",
"DetailSEGSByScaleFactor",
"DetailSEGSByScaleFactorTiledDiffusion",
"EncodePromptBatch",
"LoadUltralyticsModel",
"NODE_CLASS_MAPPINGS",
"NODE_DISPLAY_NAME_MAPPINGS",
"PromptEncodeStyle",
"PromptEncodeStyleAndNormalization",
"PromptSEGSWithSAM",
"ResizeImageToTarget",
"SAMModelLoader",
"ScaleFactor",
"Seed",
"SimpleLoadAnima",
"SimpleLoadCheckpoint",
"SimpleVAEEncode",
"TileAndTagSEGS",
"UpscaleLatentFromImage",
"ViTMatteModelLoader",
"WD14TaggerLoader",
]
@@ -0,0 +1,88 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI nodes for constructing SimpleSyrup conditioning batches."""
from __future__ import annotations
from typing import Any
from ..domain.conditioning_batch import ConditioningBatch
class ConditioningBatchStart:
"""Start a conditioning batch from one normal conditioning value."""
RETURN_TYPES = ("CONDITIONING_BATCH",)
RETURN_NAMES = ("batch",)
OUTPUT_TOOLTIPS = (
"Conditioning batch with one entry, ready to align with the first SEGS item.",
)
FUNCTION = "pack"
CATEGORY = "SimpleSyrup/Conditioning"
DESCRIPTION = "Starts a per-segment conditioning batch."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare the first conditioning input."""
return {
"required": {
"conditioning": (
"CONDITIONING",
{
"tooltip": (
"Conditioning for the first SEGS item in a per-region "
"batch."
)
},
),
}
}
def pack(self, conditioning: Any) -> tuple[ConditioningBatch]:
"""Return a new conditioning batch with one entry."""
return (ConditioningBatch((conditioning,)),)
class ConditioningBatchAppend:
"""Append one normal conditioning value to an existing batch."""
RETURN_TYPES = ("CONDITIONING_BATCH",)
RETURN_NAMES = ("batch",)
OUTPUT_TOOLTIPS = ("Conditioning batch with the new entry added at the end.",)
FUNCTION = "append"
CATEGORY = "SimpleSyrup/Conditioning"
DESCRIPTION = "Appends a conditioning entry to a per-segment batch."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare the existing batch and conditioning inputs."""
return {
"required": {
"batch": (
"CONDITIONING_BATCH",
{"tooltip": ("Existing per-region batch to extend in SEGS order.")},
),
"conditioning": (
"CONDITIONING",
{
"tooltip": (
"Conditioning to add as the next per-region batch entry."
)
},
),
}
}
def append(
self,
batch: ConditioningBatch,
conditioning: Any,
) -> tuple[ConditioningBatch]:
"""Return a new batch with one additional conditioning entry."""
return (batch.append(conditioning),)
@@ -0,0 +1,275 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for regional MultiDiffusion SEGS detailing."""
from __future__ import annotations
from typing import Any, ClassVar
import torch
from ..domain.segs import coerce_segs_group
from ..nodes import tooltips
from ..nodes.detailer_input_adapters import (
bool_input,
conditioning_batch_group,
float_input,
image_inputs,
int_input,
single_input,
str_input,
validate_image_segs_pairing,
)
from ..runtime import sampling_samplers, sampling_schedulers
from ..runtime.detail_resize import SUPPORTED_DETAIL_UPSCALE_METHODS
from ..services.detail_segs_as_regions_service import (
DetailSEGSAsRegionsService,
)
from .scale_factor import scale_factor_options
OPERATION = "Detail SEGS as Regions"
class DetailSEGSAsRegions:
"""Detail SEGS in one regional MultiDiffusion sampling pass."""
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
INPUT_IS_LIST = True
OUTPUT_TOOLTIPS = (tooltips.DETAIL_IMAGE_OUTPUT,)
FUNCTION = "detail"
CATEGORY = "SimpleSyrup/Detailing"
DESCRIPTION = "Details SEGS regions with paired regional conditioning."
SEARCH_ALIASES = [
"detailer",
"segs detailer",
"regional detailer",
"multidiffusion detailer",
"tiled diffusion detailer",
"tile and tag",
]
service_class: ClassVar[type[DetailSEGSAsRegionsService]] = (
DetailSEGSAsRegionsService
)
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare ComfyUI inputs for regional SEGS detailing."""
return {
"required": {
"image": ("IMAGE", {"tooltip": tooltips.DETAIL_IMAGE}),
"model": ("MODEL", {"tooltip": tooltips.DETAIL_MODEL}),
"vae": ("VAE", {"tooltip": tooltips.DETAIL_VAE}),
"negative": (
"CONDITIONING",
{"tooltip": tooltips.REGIONAL_GLOBAL_NEGATIVE},
),
"positive": (
"CONDITIONING",
{"tooltip": tooltips.REGIONAL_GLOBAL_POSITIVE},
),
"segs": ("SEGS", {"tooltip": tooltips.DETAIL_SEGS}),
"region_positive": (
"CONDITIONING_BATCH",
{"tooltip": tooltips.REGIONAL_POSITIVE_BATCH},
),
"global_prompt_weight": (
"FLOAT",
{
"default": 0.25,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": (
"Global positive prediction weight inside covered "
"regions; the remaining weight goes to regional prompts."
),
},
),
"scale_factor": (
"FLOAT",
scale_factor_options(default=1.0),
),
"upscale_method": (
list(SUPPORTED_DETAIL_UPSCALE_METHODS),
{
"default": "lanczos",
"tooltip": tooltips.DETAIL_UPSCALE_METHOD,
},
),
"seed": (
"INT",
{
"default": 0,
"min": 0,
"max": 0xFFFFFFFFFFFFFFFF,
"control_after_generate": True,
"tooltip": tooltips.SAMPLING_SEED,
},
),
"steps": (
"INT",
{
"default": 20,
"min": 1,
"max": 10000,
"tooltip": tooltips.SAMPLING_STEPS,
},
),
"cfg": (
"FLOAT",
{
"default": 8.0,
"min": 0.0,
"max": 100.0,
"step": 0.1,
"tooltip": tooltips.SAMPLING_CFG,
},
),
"sampler_name": (
sampling_samplers.available_samplers(),
{"tooltip": tooltips.SAMPLER_NAME},
),
"scheduler": (
sampling_schedulers.available_schedulers(),
{"tooltip": tooltips.SCHEDULER},
),
"denoise": (
"FLOAT",
{
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": tooltips.DENOISE_STRENGTH,
},
),
"feather": (
"INT",
{
"default": 5,
"min": 0,
"max": 512,
"tooltip": tooltips.DETAIL_FEATHER,
},
),
"noise_mask": (
"BOOLEAN",
{
"default": True,
"tooltip": tooltips.DETAIL_NOISE_MASK,
},
),
"noise_mask_feather": (
"INT",
{
"default": 20,
"min": 0,
"max": 512,
"tooltip": tooltips.DETAIL_NOISE_MASK_FEATHER,
},
),
"tiled_encode": (
"BOOLEAN",
{
"default": False,
"tooltip": tooltips.DETAIL_TILED_ENCODE,
},
),
"tiled_decode": (
"BOOLEAN",
{
"default": False,
"tooltip": tooltips.DETAIL_TILED_DECODE,
},
),
}
}
def detail(
self,
image: object,
model: Any,
vae: Any,
negative: Any,
positive: Any,
segs: object,
region_positive: object,
global_prompt_weight: object,
scale_factor: object,
upscale_method: object,
seed: object,
steps: object,
cfg: object,
sampler_name: object,
scheduler: object,
denoise: object,
feather: object,
noise_mask: object,
noise_mask_feather: object,
tiled_encode: object,
tiled_decode: object,
) -> tuple[object]:
"""Run regional detailing and return the detailed image."""
list_mode = isinstance(image, list)
images = image_inputs(image, OPERATION)
segs_group = coerce_segs_group(segs)
validate_image_segs_pairing(images, segs_group, OPERATION)
region_positive_group = conditioning_batch_group(
region_positive,
len(segs_group),
OPERATION,
)
service = self.service_class()
outputs: list[torch.Tensor] = []
for single_image, single_segs, single_region_positive in zip(
images,
segs_group,
region_positive_group,
strict=True,
):
result = service.detail(
image=single_image,
segs=single_segs,
model=single_input(model, "model", list_mode, OPERATION),
vae=single_input(vae, "vae", list_mode, OPERATION),
positive=single_input(positive, "positive", list_mode, OPERATION),
negative=single_input(negative, "negative", list_mode, OPERATION),
region_positive=single_region_positive,
global_prompt_weight=float_input(
global_prompt_weight, "global_prompt_weight", list_mode, OPERATION
),
scale_factor=float_input(
scale_factor, "scale_factor", list_mode, OPERATION
),
upscale_method=str_input(
upscale_method, "upscale_method", list_mode, OPERATION
),
seed=int_input(seed, "seed", list_mode, OPERATION),
steps=int_input(steps, "steps", list_mode, OPERATION),
cfg=float_input(cfg, "cfg", list_mode, OPERATION),
sampler_name=str_input(
sampler_name, "sampler_name", list_mode, OPERATION
),
scheduler=str_input(scheduler, "scheduler", list_mode, OPERATION),
denoise=float_input(denoise, "denoise", list_mode, OPERATION),
feather=int_input(feather, "feather", list_mode, OPERATION),
noise_mask=bool_input(noise_mask, "noise_mask", list_mode, OPERATION),
noise_mask_feather=int_input(
noise_mask_feather, "noise_mask_feather", list_mode, OPERATION
),
tiled_encode=bool_input(
tiled_encode, "tiled_encode", list_mode, OPERATION
),
tiled_decode=bool_input(
tiled_decode, "tiled_decode", list_mode, OPERATION
),
)
outputs.append(result.image)
return (torch.cat(outputs, dim=0),)
@@ -0,0 +1,245 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for SEGS detailing by scale factor."""
from __future__ import annotations
from typing import Any, ClassVar
import torch
from ..domain.segs import coerce_segs_group
from ..nodes import tooltips
from ..nodes.detailer_input_adapters import (
bool_input,
float_input,
image_inputs,
int_input,
single_input,
str_input,
validate_image_segs_pairing,
)
from ..runtime import sampling_samplers, sampling_schedulers
from ..runtime.detail_resize import SUPPORTED_DETAIL_UPSCALE_METHODS
from ..services.detail_segs_by_scale_factor_service import (
DetailSEGSByScaleFactorService,
)
from .scale_factor import scale_factor_options
OPERATION = "Detail SEGS by Scale Factor"
class DetailSEGSByScaleFactor:
"""Detail provided SEGS by scaling crops before inpaint sampling."""
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
INPUT_IS_LIST = True
OUTPUT_TOOLTIPS = (tooltips.DETAIL_IMAGE_OUTPUT,)
FUNCTION = "detail"
CATEGORY = "SimpleSyrup/Detailing"
DESCRIPTION = "Details SEGS regions using crop scale factor sizing."
SEARCH_ALIASES = ["detailer", "automask", "segs", "scale factor"]
service_class: ClassVar[type[DetailSEGSByScaleFactorService]] = (
DetailSEGSByScaleFactorService
)
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare ComfyUI inputs for scale-factor detailing."""
return {
"required": {
"image": ("IMAGE", {"tooltip": tooltips.DETAIL_IMAGE}),
"segs": ("SEGS", {"tooltip": tooltips.DETAIL_SEGS}),
"model": ("MODEL", {"tooltip": tooltips.DETAIL_MODEL}),
"vae": ("VAE", {"tooltip": tooltips.DETAIL_VAE}),
"positive": (
"CONDITIONING,CONDITIONING_BATCH",
{"tooltip": tooltips.DETAIL_POSITIVE},
),
"negative": (
"CONDITIONING,CONDITIONING_BATCH",
{"tooltip": tooltips.DETAIL_NEGATIVE},
),
"scale_factor": (
"FLOAT",
scale_factor_options(default=1.5),
),
"upscale_method": (
list(SUPPORTED_DETAIL_UPSCALE_METHODS),
{
"default": "lanczos",
"tooltip": tooltips.DETAIL_UPSCALE_METHOD,
},
),
"clamp_size": (
"INT",
{
"default": 0,
"min": 0,
"max": 16384,
"tooltip": tooltips.DETAIL_CLAMP_SIZE,
},
),
"seed": (
"INT",
{
"default": 0,
"min": 0,
"max": 0xFFFFFFFFFFFFFFFF,
"control_after_generate": True,
"tooltip": tooltips.SAMPLING_SEED,
},
),
"steps": (
"INT",
{
"default": 20,
"min": 1,
"max": 10000,
"tooltip": tooltips.SAMPLING_STEPS,
},
),
"cfg": (
"FLOAT",
{
"default": 8.0,
"min": 0.0,
"max": 100.0,
"step": 0.1,
"tooltip": tooltips.SAMPLING_CFG,
},
),
"sampler_name": (
sampling_samplers.available_samplers(),
{"tooltip": tooltips.SAMPLER_NAME},
),
"scheduler": (
sampling_schedulers.available_schedulers(),
{"tooltip": tooltips.SCHEDULER},
),
"denoise": (
"FLOAT",
{
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": tooltips.DENOISE_STRENGTH,
},
),
"feather": (
"INT",
{
"default": 5,
"min": 0,
"max": 512,
"tooltip": tooltips.DETAIL_FEATHER,
},
),
"noise_mask": (
"BOOLEAN",
{
"default": True,
"tooltip": tooltips.DETAIL_NOISE_MASK,
},
),
"noise_mask_feather": (
"INT",
{
"default": 20,
"min": 0,
"max": 512,
"tooltip": tooltips.DETAIL_NOISE_MASK_FEATHER,
},
),
"tiled_encode": (
"BOOLEAN",
{
"default": False,
"tooltip": tooltips.DETAIL_TILED_ENCODE,
},
),
"tiled_decode": (
"BOOLEAN",
{
"default": False,
"tooltip": tooltips.DETAIL_TILED_DECODE,
},
),
}
}
def detail(
self,
image: object,
segs: object,
model: Any,
vae: Any,
positive: Any,
negative: Any,
scale_factor: object,
upscale_method: object,
clamp_size: object,
seed: object,
steps: object,
cfg: object,
sampler_name: object,
scheduler: object,
denoise: object,
feather: object,
noise_mask: object,
noise_mask_feather: object,
tiled_encode: object,
tiled_decode: object,
) -> tuple[object]:
"""Run scale-factor detailing and return the detailed image."""
list_mode = isinstance(image, list)
images = image_inputs(image, OPERATION)
segs_group = coerce_segs_group(segs)
validate_image_segs_pairing(images, segs_group, OPERATION)
service = self.service_class()
outputs: list[torch.Tensor] = []
for single_image, single_segs in zip(images, segs_group, strict=True):
result = service.detail(
image=single_image,
segs=single_segs,
model=single_input(model, "model", list_mode, OPERATION),
vae=single_input(vae, "vae", list_mode, OPERATION),
positive=single_input(positive, "positive", list_mode, OPERATION),
negative=single_input(negative, "negative", list_mode, OPERATION),
scale_factor=float_input(
scale_factor, "scale_factor", list_mode, OPERATION
),
upscale_method=str_input(
upscale_method, "upscale_method", list_mode, OPERATION
),
clamp_size=int_input(clamp_size, "clamp_size", list_mode, OPERATION),
seed=int_input(seed, "seed", list_mode, OPERATION),
steps=int_input(steps, "steps", list_mode, OPERATION),
cfg=float_input(cfg, "cfg", list_mode, OPERATION),
sampler_name=str_input(
sampler_name, "sampler_name", list_mode, OPERATION
),
scheduler=str_input(scheduler, "scheduler", list_mode, OPERATION),
denoise=float_input(denoise, "denoise", list_mode, OPERATION),
feather=int_input(feather, "feather", list_mode, OPERATION),
noise_mask=bool_input(noise_mask, "noise_mask", list_mode, OPERATION),
noise_mask_feather=int_input(
noise_mask_feather, "noise_mask_feather", list_mode, OPERATION
),
tiled_encode=bool_input(
tiled_encode, "tiled_encode", list_mode, OPERATION
),
tiled_decode=bool_input(
tiled_decode, "tiled_decode", list_mode, OPERATION
),
)
outputs.append(result.image)
return (torch.cat(outputs, dim=0),)
@@ -0,0 +1,328 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for tiled diffusion SEGS scale-factor detailing."""
from __future__ import annotations
from typing import Any, ClassVar
import torch
from ..domain.segs import coerce_segs_group
from ..domain.tiled_diffusion import TILED_DIFFUSION_MODES
from ..nodes import tooltips
from ..nodes.detailer_input_adapters import (
bool_input,
float_input,
image_inputs,
int_input,
single_input,
str_input,
validate_image_segs_pairing,
)
from ..runtime import sampling_samplers, sampling_schedulers
from ..runtime.detail_resize import SUPPORTED_DETAIL_UPSCALE_METHODS
from ..services.detail_segs_by_scale_factor_tiled_diffusion_service import (
DetailSEGSByScaleFactorTiledDiffusionService,
)
from .scale_factor import scale_factor_options
OPERATION = "Detail SEGS by Scale Factor w/ Tiled Diffusion"
MAX_LATENT_TILE_SIZE = 512
class DetailSEGSByScaleFactorTiledDiffusion:
"""Detail SEGS crops with scale-factor sizing and tiled diffusion."""
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
INPUT_IS_LIST = True
OUTPUT_TOOLTIPS = (tooltips.DETAIL_IMAGE_OUTPUT,)
FUNCTION = "detail"
CATEGORY = "SimpleSyrup/Detailing"
DESCRIPTION = "Details scaled SEGS crops with tiled diffusion sampling."
SEARCH_ALIASES = [
"detailer",
"segs detailer",
"scale factor",
"multidiffusion detailer",
"mixture of diffusers detailer",
"tiled diffusion detailer",
]
service_class: ClassVar[type[DetailSEGSByScaleFactorTiledDiffusionService]] = (
DetailSEGSByScaleFactorTiledDiffusionService
)
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare ComfyUI inputs for tiled scale-factor detailing."""
return {
"required": {
"image": ("IMAGE", {"tooltip": tooltips.DETAIL_IMAGE}),
"segs": ("SEGS", {"tooltip": tooltips.DETAIL_SEGS}),
"model": ("MODEL", {"tooltip": tooltips.DETAIL_MODEL}),
"vae": ("VAE", {"tooltip": tooltips.DETAIL_VAE}),
"positive": (
"CONDITIONING,CONDITIONING_BATCH",
{"tooltip": tooltips.DETAIL_POSITIVE},
),
"negative": (
"CONDITIONING,CONDITIONING_BATCH",
{"tooltip": tooltips.DETAIL_NEGATIVE},
),
"scale_factor": (
"FLOAT",
scale_factor_options(default=1.5),
),
"upscale_method": (
list(SUPPORTED_DETAIL_UPSCALE_METHODS),
{
"default": "lanczos",
"tooltip": tooltips.DETAIL_UPSCALE_METHOD,
},
),
"clamp_size": (
"INT",
{
"default": 0,
"min": 0,
"max": 16384,
"tooltip": tooltips.DETAIL_CLAMP_SIZE,
},
),
"seed": (
"INT",
{
"default": 0,
"min": 0,
"max": 0xFFFFFFFFFFFFFFFF,
"control_after_generate": True,
"tooltip": tooltips.SAMPLING_SEED,
},
),
"steps": (
"INT",
{
"default": 20,
"min": 1,
"max": 10000,
"tooltip": tooltips.SAMPLING_STEPS,
},
),
"cfg": (
"FLOAT",
{
"default": 8.0,
"min": 0.0,
"max": 100.0,
"step": 0.1,
"tooltip": tooltips.SAMPLING_CFG,
},
),
"sampler_name": (
sampling_samplers.available_samplers(),
{"tooltip": tooltips.SAMPLER_NAME},
),
"scheduler": (
sampling_schedulers.available_schedulers(),
{"tooltip": tooltips.SCHEDULER},
),
"denoise": (
"FLOAT",
{
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": tooltips.DENOISE_STRENGTH,
},
),
"feather": (
"INT",
{
"default": 5,
"min": 0,
"max": 512,
"tooltip": tooltips.DETAIL_FEATHER,
},
),
"noise_mask": (
"BOOLEAN",
{
"default": True,
"tooltip": tooltips.DETAIL_NOISE_MASK,
},
),
"noise_mask_feather": (
"INT",
{
"default": 20,
"min": 0,
"max": 512,
"tooltip": tooltips.DETAIL_NOISE_MASK_FEATHER,
},
),
"tiled_encode": (
"BOOLEAN",
{
"default": False,
"tooltip": tooltips.DETAIL_TILED_ENCODE,
},
),
"tiled_decode": (
"BOOLEAN",
{
"default": False,
"tooltip": tooltips.DETAIL_TILED_DECODE,
},
),
"diffusion_mode": (
list(TILED_DIFFUSION_MODES),
{
"default": "multidiffusion",
"tooltip": (
"Tiled sampling blend method. MultiDiffusion is steady; "
"Mixture of Diffusers can blend tile predictions more "
"softly."
),
},
),
"latent_tile_width": (
"INT",
{
"default": 128,
"min": 16,
"max": MAX_LATENT_TILE_SIZE,
"step": 16,
"tooltip": tooltips.LATENT_TILE_WIDTH,
},
),
"latent_tile_height": (
"INT",
{
"default": 128,
"min": 16,
"max": MAX_LATENT_TILE_SIZE,
"step": 16,
"tooltip": tooltips.LATENT_TILE_HEIGHT,
},
),
"latent_tile_overlap": (
"INT",
{
"default": 16,
"min": 0,
"max": 256,
"step": 4,
"tooltip": tooltips.LATENT_TILE_OVERLAP,
},
),
"latent_tile_batch_size": (
"INT",
{
"default": 4,
"min": 1,
"max": 8,
"step": 1,
"tooltip": tooltips.LATENT_TILE_BATCH_SIZE,
},
),
}
}
def detail(
self,
image: object,
segs: object,
model: Any,
vae: Any,
positive: Any,
negative: Any,
scale_factor: object,
upscale_method: object,
clamp_size: object,
seed: object,
steps: object,
cfg: object,
sampler_name: object,
scheduler: object,
denoise: object,
feather: object,
noise_mask: object,
noise_mask_feather: object,
tiled_encode: object,
tiled_decode: object,
diffusion_mode: object,
latent_tile_width: object,
latent_tile_height: object,
latent_tile_overlap: object,
latent_tile_batch_size: object,
) -> tuple[object]:
"""Run tiled diffusion scale-factor detailing and return the image."""
list_mode = isinstance(image, list)
images = image_inputs(image, OPERATION)
segs_group = coerce_segs_group(segs)
validate_image_segs_pairing(images, segs_group, OPERATION)
service = self.service_class()
outputs: list[torch.Tensor] = []
for single_image, single_segs in zip(images, segs_group, strict=True):
result = service.detail(
image=single_image,
segs=single_segs,
model=single_input(model, "model", list_mode, OPERATION),
vae=single_input(vae, "vae", list_mode, OPERATION),
positive=single_input(positive, "positive", list_mode, OPERATION),
negative=single_input(negative, "negative", list_mode, OPERATION),
scale_factor=float_input(
scale_factor, "scale_factor", list_mode, OPERATION
),
upscale_method=str_input(
upscale_method, "upscale_method", list_mode, OPERATION
),
clamp_size=int_input(clamp_size, "clamp_size", list_mode, OPERATION),
seed=int_input(seed, "seed", list_mode, OPERATION),
steps=int_input(steps, "steps", list_mode, OPERATION),
cfg=float_input(cfg, "cfg", list_mode, OPERATION),
sampler_name=str_input(
sampler_name, "sampler_name", list_mode, OPERATION
),
scheduler=str_input(scheduler, "scheduler", list_mode, OPERATION),
denoise=float_input(denoise, "denoise", list_mode, OPERATION),
feather=int_input(feather, "feather", list_mode, OPERATION),
noise_mask=bool_input(noise_mask, "noise_mask", list_mode, OPERATION),
noise_mask_feather=int_input(
noise_mask_feather, "noise_mask_feather", list_mode, OPERATION
),
tiled_encode=bool_input(
tiled_encode, "tiled_encode", list_mode, OPERATION
),
tiled_decode=bool_input(
tiled_decode, "tiled_decode", list_mode, OPERATION
),
diffusion_mode=str_input(
diffusion_mode, "diffusion_mode", list_mode, OPERATION
),
latent_tile_width=int_input(
latent_tile_width, "latent_tile_width", list_mode, OPERATION
),
latent_tile_height=int_input(
latent_tile_height, "latent_tile_height", list_mode, OPERATION
),
latent_tile_overlap=int_input(
latent_tile_overlap, "latent_tile_overlap", list_mode, OPERATION
),
latent_tile_batch_size=int_input(
latent_tile_batch_size,
"latent_tile_batch_size",
list_mode,
OPERATION,
),
)
outputs.append(result.image)
return (torch.cat(outputs, dim=0),)
@@ -0,0 +1,155 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Input normalization helpers shared by SEGS detailer nodes."""
from __future__ import annotations
from collections.abc import Sequence
import torch
from ..domain.conditioning_batch import ConditioningBatch
from ..domain.segs import NativeSegs
from ..masking.segs_mask_ops import iter_single_images, validate_image_batch
def image_inputs(image: object, operation_name: str) -> tuple[torch.Tensor, ...]:
"""Normalize direct or list-mode image inputs into one-image tensors."""
if isinstance(image, list):
if not image:
raise ValueError(f"{operation_name} requires an image input.")
images: list[torch.Tensor] = []
for item in image:
images.extend(
iter_single_images(validate_image_batch(item, operation_name))
)
return tuple(images)
return iter_single_images(validate_image_batch(image, operation_name))
def single_input(
value: object,
name: str,
list_mode: bool,
operation_name: str,
) -> object:
"""Return one scalar input from a direct or Comfy list-mode value."""
if not list_mode:
return value
if not isinstance(value, list):
return value
if len(value) != 1:
raise ValueError(
f"{operation_name} requires one '{name}' value; received {len(value)}."
)
return value[0]
def float_input(
value: object,
name: str,
list_mode: bool,
operation_name: str,
) -> float:
"""Return a float scalar from a direct or list-mode input."""
raw_value = single_input(value, name, list_mode, operation_name)
if isinstance(raw_value, (int, float, str)):
return float(raw_value)
raise TypeError(f"{operation_name} requires '{name}' to be a float.")
def int_input(
value: object,
name: str,
list_mode: bool,
operation_name: str,
) -> int:
"""Return an integer scalar from a direct or list-mode input."""
raw_value = single_input(value, name, list_mode, operation_name)
if isinstance(raw_value, (int, float, str)):
return int(raw_value)
raise TypeError(f"{operation_name} requires '{name}' to be an int.")
def str_input(
value: object,
name: str,
list_mode: bool,
operation_name: str,
) -> str:
"""Return a string scalar from a direct or list-mode input."""
raw_value = single_input(value, name, list_mode, operation_name)
if isinstance(raw_value, str):
return raw_value
raise TypeError(f"{operation_name} requires '{name}' to be a string.")
def bool_input(
value: object,
name: str,
list_mode: bool,
operation_name: str,
) -> bool:
"""Return a boolean scalar from a direct or list-mode input."""
raw_value = single_input(value, name, list_mode, operation_name)
if isinstance(raw_value, bool):
return raw_value
raise TypeError(f"{operation_name} requires '{name}' to be a boolean.")
def validate_image_segs_pairing(
images: tuple[torch.Tensor, ...],
segs_group: tuple[NativeSegs, ...],
operation_name: str,
) -> None:
"""Ensure image and SEGS group cardinality is unambiguous."""
image_count = len(images)
segs_count = len(segs_group)
if image_count == segs_count:
return
raise ValueError(
f"{operation_name} requires one SEGS payload per image when "
f"processing an image batch; received {image_count} images and "
f"{segs_count} SEGS payloads."
)
def conditioning_batch_group(
value: object,
expected_count: int,
operation_name: str,
) -> tuple[ConditioningBatch, ...]:
"""Return one regional conditioning batch per SEGS payload."""
if expected_count == 1 and isinstance(value, ConditioningBatch):
return (value,)
if not isinstance(value, Sequence) or isinstance(value, (str, bytes)):
raise TypeError(
f"{operation_name} requires one region_positive CONDITIONING_BATCH "
"per SEGS payload."
)
if len(value) != expected_count:
raise ValueError(
f"{operation_name} requires one region_positive CONDITIONING_BATCH "
f"per SEGS payload; received {len(value)} conditioning batches for "
f"{expected_count} SEGS payloads."
)
batches: list[ConditioningBatch] = []
for index, item in enumerate(value, start=1):
if not isinstance(item, ConditioningBatch):
raise TypeError(
f"{operation_name} requires region_positive item {index} to be "
"CONDITIONING_BATCH."
)
batches.append(item)
return tuple(batches)
@@ -0,0 +1,213 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for Ultralytics SEGS detection."""
from __future__ import annotations
from collections.abc import Callable
from typing import Any, ClassVar
import torch
from ..domain.segs import (
SORT_ORDER_OPTIONS,
NativeSegs,
sort_segs,
to_impact_compatible_segs,
)
from ..masking.segs_mask_ops import iter_single_images, validate_image_batch
from ..runtime.ultralytics_loader import UltralyticsDetectorModel
from ..services.segs_detection_service import (
SegsDetectionService,
)
from ..services.segs_output_service import (
CombinedSegsResult,
build_combined_segs_result,
)
class DetectSEGSWithUltralytics:
"""Detect regions using Simple Detector SEGS-style controls."""
RETURN_TYPES = ("SEGS", "MASK")
RETURN_NAMES = ("segs", "mask")
OUTPUT_IS_LIST = (True, False)
OUTPUT_TOOLTIPS = (
"Detected regions as separate or combined SEGS based on combine_segs.",
"Combined detected area as a standard ComfyUI mask.",
)
FUNCTION = "detect"
CATEGORY = "SimpleSyrup/Detection"
DESCRIPTION = (
"Detects regions with an Ultralytics model and returns individual SEGS, "
"combined SEGS when requested, and a combined mask."
)
SEARCH_ALIASES = ["ultralytics", "yolo", "segs", "detector"]
service_class: ClassVar[type[SegsDetectionService]] = SegsDetectionService
combined_builder: ClassVar[Callable[[object, NativeSegs], CombinedSegsResult]] = (
build_combined_segs_result
)
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, Any]]:
"""Declare ComfyUI inputs for detector-to-SEGS conversion."""
return {
"required": {
"image": (
"IMAGE",
{"tooltip": "Image to search for detectable regions."},
),
"detector_model": (
"DETECTOR_MODEL",
{
"tooltip": (
"Ultralytics model that finds boxes or masks in the "
"input image."
)
},
),
"confidence_threshold": (
"FLOAT",
{
"default": 0.5,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": (
"Minimum detection confidence required to keep a region."
),
},
),
"size_threshold": (
"INT",
{
"default": 10,
"min": 1,
"max": 8192,
"tooltip": (
"Discard regions whose detected box is smaller than this "
"many pixels wide or tall."
),
},
),
"bbox_dilation": (
"INT",
{
"default": 0,
"min": -512,
"max": 512,
"step": 1,
"tooltip": (
"Grow or shrink initial detection boxes in pixels before "
"masks are built."
),
},
),
"sub_dilation": (
"INT",
{
"default": 0,
"min": -512,
"max": 512,
"step": 1,
"tooltip": (
"Grow or shrink the segmentation refinement mask in "
"pixels before it is applied to detected regions."
),
},
),
"post_dilation": (
"INT",
{
"default": 0,
"min": -512,
"max": 512,
"step": 1,
"tooltip": (
"Grow or shrink each final cropped SEG mask in pixels "
"after detection and refinement."
),
},
),
"crop_factor": (
"FLOAT",
{
"default": 3.0,
"min": 0.0,
"max": 100.0,
"step": 0.1,
"tooltip": (
"How much context to include around each detected region. "
"Use 0 for the full image; higher values make larger "
"SEG crops."
),
},
),
"sort_order": (
SORT_ORDER_OPTIONS,
{
"default": SORT_ORDER_OPTIONS[0],
"tooltip": (
"Order the returned SEGS before output and before the "
"combined mask is built."
),
},
),
"combine_segs": (
"BOOLEAN",
{
"default": False,
"label_on": "combined",
"label_off": "separate",
"tooltip": (
"Return one unioned SEGS region instead of separate "
"regions."
),
},
),
},
}
def detect(
self,
image: object,
detector_model: UltralyticsDetectorModel,
confidence_threshold: float,
size_threshold: int,
bbox_dilation: int,
sub_dilation: int,
post_dilation: int,
crop_factor: float,
sort_order: str,
combine_segs: bool,
) -> tuple[object, object]:
"""Run detection and return SEGS plus the combined mask output."""
image_batch = validate_image_batch(image, "SEGS detector")
service = self.service_class()
segs_outputs: list[object] = []
mask_outputs: list[torch.Tensor] = []
for single_image in iter_single_images(image_batch):
segs = service.detect_simple(
image=single_image,
detector_model=detector_model,
bbox_threshold=confidence_threshold,
bbox_dilation=bbox_dilation,
crop_factor=crop_factor,
drop_size=size_threshold,
sub_threshold=confidence_threshold,
sub_dilation=sub_dilation,
post_dilation=post_dilation,
)
segs = sort_segs(segs, sort_order)
combined = type(self).combined_builder(single_image, segs)
output_segs = combined.segs if combine_segs else segs
segs_outputs.append(to_impact_compatible_segs(output_segs))
mask_outputs.append(combined.mask)
return segs_outputs, torch.cat(mask_outputs, dim=0)
+93
View File
@@ -0,0 +1,93 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for standard prompt batch encoding."""
from __future__ import annotations
from typing import Any, ClassVar
from ..domain.conditioning_batch import split_prompt_batch
from ..runtime.conditioning_encoding import ComfyConditioningEncoder
class EncodePromptBatch:
"""Encode separator-delimited prompts into conditioning batches."""
RETURN_TYPES = ("CONDITIONING_BATCH", "CONDITIONING_BATCH")
RETURN_NAMES = ("positive", "negative")
OUTPUT_TOOLTIPS = (
"Positive conditioning entries selected by SEGS order.",
"Negative conditioning entries selected by SEGS order.",
)
FUNCTION = "encode"
CATEGORY = "SimpleSyrup/Conditioning"
DESCRIPTION = (
"Encodes [SEP]-separated prompts into per-segment conditioning batches."
)
SEARCH_ALIASES = ["conditioning batch", "prompt batch", "segs prompts"]
encoder_class: ClassVar[type[ComfyConditioningEncoder]] = ComfyConditioningEncoder
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare prompt batch encoder inputs."""
return {
"required": {
"clip": (
"CLIP",
{
"tooltip": (
"Text encoder used to turn each prompt entry into "
"conditioning."
)
},
),
"positive_prompt": (
"STRING",
{
"default": "",
"multiline": True,
"tooltip": (
"Positive prompts in SEGS order, separated by [SEP]."
),
},
),
"negative_prompt": (
"STRING",
{
"default": "",
"multiline": True,
"tooltip": (
"Negative prompts in SEGS order, separated by [SEP]."
),
},
),
"separator": (
"STRING",
{
"default": "[SEP]",
"tooltip": "Text marker that separates prompt entries.",
},
),
}
}
def encode(
self,
clip: Any,
positive_prompt: str,
negative_prompt: str,
separator: str,
) -> tuple[object, object]:
"""Encode positive and negative prompt batches."""
encoder = self.encoder_class()
positive_chunks = split_prompt_batch(positive_prompt, separator)
negative_chunks = split_prompt_batch(negative_prompt, separator)
return (
encoder.encode_batch(clip, positive_chunks),
encoder.encode_batch(clip, negative_chunks),
)
@@ -0,0 +1,56 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for grounded SAM model metadata."""
from __future__ import annotations
from typing import Any
from ..runtime.model_catalog import grounding_dino_choices, sam_choices
from ..runtime.model_metadata import GroundedSAMModelMetadata
from . import tooltips
class GroundedSAMModelInfo:
"""Expose selected grounded SAM source and local path metadata."""
_metadata = GroundedSAMModelMetadata()
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("model_info",)
OUTPUT_TOOLTIPS = (
"JSON metadata describing the selected SAM and GroundingDINO model sources.",
)
FUNCTION = "describe"
CATEGORY = "SimpleSyrup/Masking"
DESCRIPTION = "Returns JSON metadata for selected SAM and GroundingDINO models."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare deterministic model metadata inputs."""
return {
"required": {
"sam_model": (
sam_choices(),
{
"default": "sam_hq_vit_b (379MB)",
"tooltip": tooltips.SAM_MODEL_INPUT,
},
),
"grounding_dino_model": (
grounding_dino_choices(),
{
"default": "GroundingDINO_SwinT_OGC (694MB)",
"tooltip": tooltips.GROUNDING_DINO_MODEL_INPUT,
},
),
}
}
def describe(self, sam_model: str, grounding_dino_model: str) -> tuple[str]:
"""Return JSON metadata for selected model entries."""
return (self._metadata.describe_selection(sam_model, grounding_dino_model),)
@@ -0,0 +1,76 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for loading GroundingDINO models."""
from __future__ import annotations
from typing import Any
from ..runtime.grounding_dino_loader import (
TEXT_ENCODER_AUTO,
TEXT_ENCODER_CHOICES,
GroundingDINOLoaderService,
)
from ..runtime.model_choices import ModelChoiceService, default_choice
from ..runtime.model_downloads import ComfyProgressReporter
from . import tooltips
class GroundingDINOModelLoader:
"""Expose GroundingDINO loading with explicit text encoder selection."""
_service = GroundingDINOLoaderService()
_choices = ModelChoiceService()
RETURN_TYPES = ("GROUNDING_DINO_MODEL",)
RETURN_NAMES = ("grounding_dino_model",)
OUTPUT_TOOLTIPS = (tooltips.GROUNDING_DINO_MODEL_OUTPUT,)
FUNCTION = "load_model"
CATEGORY = "SimpleSyrup/Masking"
DESCRIPTION = "Loads GroundingDINO and its explicit BERT text encoder."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare deterministic GroundingDINO loader inputs."""
choices = cls._choices.grounding_dino_choices()
return {
"required": {
"grounding_dino_model": (
choices,
{
"default": default_choice(
choices,
"GroundingDINO_SwinT_OGC (694MB)",
),
"tooltip": tooltips.GROUNDING_DINO_MODEL_INPUT,
},
),
"text_encoder": (
list(TEXT_ENCODER_CHOICES),
{
"default": TEXT_ENCODER_AUTO,
"tooltip": tooltips.GROUNDING_DINO_TEXT_ENCODER_INPUT,
},
),
}
}
def load_model(
self,
grounding_dino_model: str,
text_encoder: str,
) -> tuple[object]:
"""Load and return a GroundingDINO-compatible model object."""
self._choices.reject_sentinel(grounding_dino_model)
return (
self._service.load_model(
grounding_dino_model=grounding_dino_model,
text_encoder=text_encoder,
auto_download=True,
progress=ComfyProgressReporter(),
),
)
@@ -0,0 +1,218 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for target image resizing."""
from __future__ import annotations
from typing import Any
import torch
from ..image.resize_service import ResizeImageToTargetService
class ResizeImageToTarget:
"""Expose target image resizing controls to ComfyUI."""
_service = ResizeImageToTargetService()
RETURN_TYPES = ("IMAGE", "INT", "INT", "MASK")
RETURN_NAMES = ("image", "width", "height", "mask")
OUTPUT_TOOLTIPS = (
"Resized image batch after aspect-ratio handling and divisibility rounding.",
"Final image width in pixels.",
"Final image height in pixels.",
"Resized mask aligned to the output image size.",
)
FUNCTION = "resize"
CATEGORY = "SimpleSyrup/Image"
DESCRIPTION = (
"Resize image batches to a target size with selectable aspect handling, "
"sampler, and CPU/GPU processor."
)
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare ComfyUI inputs for target image resizing."""
return {
"required": {
"image": (
"IMAGE",
{"tooltip": "Image batch to resize to the target dimensions."},
),
"width": (
"INT",
{
"default": 1024,
"min": 1,
"max": 16384,
"step": 1,
"tooltip": (
"Target width in pixels before divisibility rounding."
),
},
),
"height": (
"INT",
{
"default": 1024,
"min": 1,
"max": 16384,
"step": 1,
"tooltip": (
"Target height in pixels before divisibility rounding."
),
},
),
"resize_mode": (
["Stretch", "Keep AR", "Crop (Cover + Crop)", "Pad (Fit + Pad)"],
{
"default": "Keep AR",
"tooltip": (
"How aspect ratio is handled. Stretch fills exactly, crop "
"trims overflow, and pad fills empty space."
),
},
),
"sampling": (
["nearest-exact", "bilinear", "area", "bicubic", "lanczos"],
{
"default": "lanczos",
"tooltip": (
"Resize filter. Sharper filters preserve detail but can "
"show more ringing."
),
},
),
"processor": (
["cpu", "gpu"],
{
"default": "gpu",
"tooltip": (
"Processor used for resizing. GPU is usually faster; CPU "
"can reduce GPU memory pressure."
),
},
),
"divisible_by": (
"INT",
{
"default": 1,
"min": 1,
"max": 4096,
"step": 1,
"tooltip": (
"Round final dimensions to a multiple of this value for "
"model or latent-size compatibility."
),
},
),
"crop_position": (
[
"center",
"top-left",
"top",
"top-right",
"left",
"right",
"bottom-left",
"bottom",
"bottom-right",
],
{
"default": "center",
"tooltip": (
"Anchor used when crop mode trims overflow from the "
"resized image."
),
},
),
"pad_color": (
"STRING",
{
"default": "0, 0, 0",
"multiline": False,
"tooltip": "RGB color used to fill empty space in pad mode.",
},
),
"max_batch_size": (
"INT",
{
"default": 0,
"min": 0,
"max": 4096,
"step": 1,
"tooltip": (
"Maximum images resized at once. Lower values reduce "
"memory use; 0 processes the full batch together."
),
},
),
"sinc_window": (
"INT",
{
"default": 3,
"min": 1,
"max": 8,
"step": 1,
"tooltip": (
"Lanczos window size. Higher values can look sharper but "
"may add ringing."
),
},
),
"precision": (
["fp32", "fp16", "bf16"],
{
"default": "fp32",
"tooltip": (
"Math precision for resizing. Lower precision can save "
"memory but may slightly change results."
),
},
),
},
"optional": {
"mask": (
"MASK",
{"tooltip": "Optional mask to resize with the image batch."},
),
},
}
def resize(
self,
image: torch.Tensor,
width: int,
height: int,
resize_mode: str,
sampling: str,
processor: str,
divisible_by: int,
crop_position: str,
pad_color: str,
max_batch_size: int,
sinc_window: int,
precision: str,
mask: torch.Tensor | None = None,
) -> tuple[torch.Tensor, int, int, torch.Tensor]:
"""Resize an image batch through the application service."""
return self._service.resize(
image=image,
width=width,
height=height,
resize_mode=resize_mode,
sampling=sampling,
processor=processor,
divisible_by=divisible_by,
crop_position=crop_position,
pad_color=pad_color,
max_batch_size=max_batch_size,
sinc_window=sinc_window,
precision=precision,
mask=mask,
)
+181
View File
@@ -0,0 +1,181 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for KSampler Extras."""
from __future__ import annotations
from importlib import import_module
from typing import Any
from ..runtime import sampling_samplers, sampling_schedulers
from . import tooltips
Latent = dict[str, Any]
class KSamplerExtras:
"""Expose KSampler-style sampling with AYS and GITS scheduler options."""
RETURN_TYPES = ("LATENT",)
OUTPUT_TOOLTIPS = (tooltips.DENOISED_LATENT_OUTPUT,)
FUNCTION = "sample"
CATEGORY = "SimpleSyrup/Sampling"
DESCRIPTION = (
"Denoises latents with extended sampler and scheduler options for "
"compatible workflows."
)
SEARCH_ALIASES = ["ksampler", "sampler", "ays", "gits", "lcm"]
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare ComfyUI inputs for extra scheduler sampling."""
return {
"required": {
"model": ("MODEL", {"tooltip": tooltips.SAMPLING_MODEL}),
"seed": (
"INT",
{
"default": 0,
"min": 0,
"max": 0xFFFFFFFFFFFFFFFF,
"control_after_generate": True,
"tooltip": tooltips.SAMPLING_SEED,
},
),
"steps": (
"INT",
{
"default": 20,
"min": 1,
"max": 10000,
"tooltip": tooltips.SAMPLING_STEPS,
},
),
"cfg": (
"FLOAT",
{
"default": 8.0,
"min": 0.0,
"max": 100.0,
"step": 0.1,
"round": 0.01,
"tooltip": tooltips.SAMPLING_CFG,
},
),
"sampler_name": (
sampling_samplers.available_samplers(),
{"tooltip": tooltips.SAMPLER_NAME},
),
"scheduler": (
sampling_schedulers.available_schedulers(),
{"tooltip": tooltips.SCHEDULER},
),
"positive": (
"CONDITIONING",
{"tooltip": tooltips.POSITIVE_CONDITIONING},
),
"negative": (
"CONDITIONING",
{"tooltip": tooltips.NEGATIVE_CONDITIONING},
),
"latent_image": ("LATENT", {"tooltip": tooltips.LATENT_IMAGE}),
"denoise": (
"FLOAT",
{
"default": 1.0,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": tooltips.DENOISE_STRENGTH,
},
),
}
}
def sample(
self,
model: Any,
seed: int,
steps: int,
cfg: float,
sampler_name: str,
scheduler: str,
positive: Any,
negative: Any,
latent_image: Latent,
denoise: float = 1.0,
) -> tuple[Latent]:
"""Sample a latent with ComfyUI samplers and extra scheduler sigmas."""
sampler = sampling_samplers.resolve_sampler(sampler_name)
sigmas = sampling_schedulers.calculate_sigmas(
model=model,
scheduler_name=scheduler,
sampler_name=sampler_name,
steps=steps,
denoise=denoise,
).to(model.load_device)
latent_samples = latent_image["samples"]
comfy_sample = _comfy_sample()
comfy_utils = _comfy_utils()
latent_preview = _latent_preview()
latent_samples = comfy_sample.fix_empty_latent_channels(
model,
latent_samples,
latent_image.get("downscale_ratio_spacial", None),
)
batch_inds = (
latent_image["batch_index"] if "batch_index" in latent_image else None
)
noise = comfy_sample.prepare_noise(latent_samples, seed, batch_inds)
noise_mask = latent_image.get("noise_mask", None)
callback = latent_preview.prepare_callback(model, steps)
disable_pbar = not comfy_utils.PROGRESS_BAR_ENABLED
samples = comfy_sample.sample_custom(
model,
noise,
cfg,
sampler,
sigmas,
positive,
negative,
latent_samples,
noise_mask=noise_mask,
callback=callback,
disable_pbar=disable_pbar,
seed=seed,
)
output = latent_image.copy()
output.pop("downscale_ratio_spacial", None)
output["samples"] = samples
return (output,)
def _comfy_sample() -> Any:
"""Import ComfyUI sample helpers lazily."""
import comfy.sample
return comfy.sample
def _comfy_utils() -> Any:
"""Import ComfyUI utility state lazily."""
import comfy.utils
return comfy.utils
def _latent_preview() -> Any:
"""Import ComfyUI preview helpers lazily."""
return import_module("latent_preview")
@@ -0,0 +1,197 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for selectable tiled diffusion sampling."""
from __future__ import annotations
from typing import Any, ClassVar
from ..domain.tiled_diffusion import TILED_DIFFUSION_MODES
from ..runtime import sampling_samplers, sampling_schedulers
from ..services.tiled_diffusion_sampling_service import TiledDiffusionSamplingService
from . import tooltips
Latent = dict[str, Any]
MAX_LATENT_TILE_SIZE = 512
class KSamplerTiledDiffusion:
"""Sample latents with selectable tiled diffusion denoising."""
RETURN_TYPES = ("LATENT",)
OUTPUT_TOOLTIPS = (tooltips.DENOISED_LATENT_OUTPUT,)
FUNCTION = "sample"
CATEGORY = "SimpleSyrup/Sampling"
DESCRIPTION = "Denoises latents with selectable tiled diffusion sampling."
SEARCH_ALIASES = [
"ksampler",
"sampler",
"tiled diffusion",
"multidiffusion",
"multi diffusion",
"mixture of diffusers",
]
service_class: ClassVar[type[TiledDiffusionSamplingService]] = (
TiledDiffusionSamplingService
)
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare ComfyUI inputs for selectable tiled diffusion sampling."""
return {
"required": {
"model": ("MODEL", {"tooltip": tooltips.SAMPLING_MODEL}),
"seed": (
"INT",
{
"default": 0,
"min": 0,
"max": 0xFFFFFFFFFFFFFFFF,
"control_after_generate": True,
"tooltip": tooltips.SAMPLING_SEED,
},
),
"steps": (
"INT",
{
"default": 20,
"min": 1,
"max": 10000,
"tooltip": tooltips.SAMPLING_STEPS,
},
),
"cfg": (
"FLOAT",
{
"default": 8.0,
"min": 0.0,
"max": 100.0,
"step": 0.1,
"round": 0.01,
"tooltip": tooltips.SAMPLING_CFG,
},
),
"sampler_name": (
sampling_samplers.available_samplers(),
{"tooltip": tooltips.SAMPLER_NAME},
),
"scheduler": (
sampling_schedulers.available_schedulers(),
{"tooltip": tooltips.SCHEDULER},
),
"positive": (
"CONDITIONING",
{"tooltip": tooltips.POSITIVE_CONDITIONING},
),
"negative": (
"CONDITIONING",
{"tooltip": tooltips.NEGATIVE_CONDITIONING},
),
"latent_image": ("LATENT", {"tooltip": tooltips.LATENT_IMAGE}),
"denoise": (
"FLOAT",
{
"default": 1.0,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": tooltips.DENOISE_STRENGTH,
},
),
"diffusion_mode": (
list(TILED_DIFFUSION_MODES),
{
"default": "multidiffusion",
"tooltip": (
"Tiled sampling blend method. MultiDiffusion is steady; "
"Mixture of Diffusers can blend tile predictions more "
"softly."
),
},
),
"latent_tile_width": (
"INT",
{
"default": 128,
"min": 16,
"max": MAX_LATENT_TILE_SIZE,
"step": 16,
"tooltip": tooltips.LATENT_TILE_WIDTH,
},
),
"latent_tile_height": (
"INT",
{
"default": 128,
"min": 16,
"max": MAX_LATENT_TILE_SIZE,
"step": 16,
"tooltip": tooltips.LATENT_TILE_HEIGHT,
},
),
"latent_tile_overlap": (
"INT",
{
"default": 16,
"min": 0,
"max": 256,
"step": 4,
"tooltip": tooltips.LATENT_TILE_OVERLAP,
},
),
"latent_tile_batch_size": (
"INT",
{
"default": 4,
"min": 1,
"max": 8,
"step": 1,
"tooltip": tooltips.LATENT_TILE_BATCH_SIZE,
},
),
}
}
def sample(
self,
model: Any,
seed: int,
steps: int,
cfg: float,
sampler_name: str,
scheduler: str,
positive: Any,
negative: Any,
latent_image: Latent,
denoise: float = 1.0,
diffusion_mode: str = "multidiffusion",
latent_tile_width: int = 128,
latent_tile_height: int = 128,
latent_tile_overlap: int = 16,
latent_tile_batch_size: int = 4,
) -> tuple[Latent]:
"""Sample a latent with the selected tiled diffusion method."""
output = self.service_class().sample(
diffusion_mode=diffusion_mode,
model=model,
seed=seed,
steps=steps,
cfg=cfg,
sampler_name=sampler_name,
scheduler=scheduler,
positive=positive,
negative=negative,
latent_image=latent_image,
denoise=denoise,
latent_tile_width=latent_tile_width,
latent_tile_height=latent_tile_height,
latent_tile_overlap=latent_tile_overlap,
latent_tile_batch_size=latent_tile_batch_size,
preview_context=None,
)
return (output,)
+64
View File
@@ -0,0 +1,64 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for latent metadata diagnostics."""
from __future__ import annotations
from typing import Any, TypedDict
from ..services.latent_diagnostics_service import LatentDiagnosticsService
Latent = dict[str, Any]
class LatentDiagnosticsResult(TypedDict):
"""ComfyUI execution result with UI text and typed output data."""
ui: dict[str, list[str]]
result: tuple[Latent, str]
class LatentDiagnostics:
"""Expose safe latent tensor metadata for debugging model compatibility."""
_service = LatentDiagnosticsService()
RETURN_TYPES = ("LATENT", "STRING")
RETURN_NAMES = ("latent", "report")
OUTPUT_TOOLTIPS = (
"Input latent passed through unchanged for continued workflow use.",
"Text report describing latent shape, dtype, device, and tiling fit.",
)
FUNCTION = "analyze"
CATEGORY = "SimpleSyrup/Utilities"
DESCRIPTION = (
"Reports latent tensor shape, dtype, device, and tiling compatibility."
)
OUTPUT_NODE = True
SEARCH_ALIASES = ["latent", "diagnostics", "inspect latent", "debug latent"]
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare the latent diagnostic input contract."""
return {
"required": {
"latent": (
"LATENT",
{
"tooltip": (
"Latent to inspect. The node reports metadata and passes "
"it through unchanged."
)
},
)
}
}
def analyze(self, latent: Latent) -> LatentDiagnosticsResult:
"""Return the input latent and a metadata report for ComfyUI display."""
report = self._service.describe(latent)
return {"ui": {"text": [report]}, "result": (latent, report)}
@@ -0,0 +1,50 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Adapter node for LayerStyle combined SAM model bundles."""
from __future__ import annotations
class LayerStyleSAMModelsAdapter:
"""Split a LayerStyle SAM/DINO bundle into conventional model sockets."""
RETURN_TYPES = ("SAM_MODEL", "DINO_MODEL")
RETURN_NAMES = ("sam_model", "dino_model")
OUTPUT_TOOLTIPS = (
"SAM model split from the LayerStyle bundle for SimpleSyrup mask nodes.",
"DINO model split from the LayerStyle bundle for SimpleSyrup detection nodes.",
)
FUNCTION = "adapt"
CATEGORY = "SimpleSyrup/Masking"
DESCRIPTION = "Splits LayerStyle LS_SAM_MODELS into SAM_MODEL and DINO_MODEL."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[object, ...]]]:
"""Declare the LayerStyle bundle input."""
return {
"required": {
"sam_models": (
"LS_SAM_MODELS",
{
"tooltip": (
"LayerStyle SAM/DINO bundle to split into separate "
"SimpleSyrup-compatible model sockets."
)
},
)
}
}
def adapt(self, sam_models: object) -> tuple[object, object]:
"""Return SAM and DINO objects from a LayerStyle-style bundle."""
if not isinstance(sam_models, dict):
raise TypeError(
"LS_SAM_MODELS must be a mapping with SAM_MODEL and DINO_MODEL."
)
if "SAM_MODEL" not in sam_models or "DINO_MODEL" not in sam_models:
raise ValueError("LS_SAM_MODELS must contain SAM_MODEL and DINO_MODEL.")
return sam_models["SAM_MODEL"], sam_models["DINO_MODEL"]
@@ -0,0 +1,54 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for loading Ultralytics models."""
from __future__ import annotations
from typing import Any, ClassVar
from ..runtime.ultralytics_loader import UltralyticsLoaderService
class LoadUltralyticsModel:
"""Load one Ultralytics model and expose native and compatibility outputs."""
RETURN_TYPES = ("DETECTOR_MODEL", "BBOX_DETECTOR", "SEGM_DETECTOR")
RETURN_NAMES = ("detector_model", "bbox_detector", "segm_detector")
OUTPUT_TOOLTIPS = (
"Detector model for SimpleSyrup SEGS detection nodes.",
"Bounding-box detector output for nodes that expect a bbox detector.",
"Segmentation detector output for nodes that expect a mask-capable detector.",
)
FUNCTION = "load"
CATEGORY = "SimpleSyrup/Detection"
DESCRIPTION = "Loads an Ultralytics model for detection and SEGS workflows."
SEARCH_ALIASES = ["ultralytics", "yolo", "detector"]
service_class: ClassVar[type[UltralyticsLoaderService]] = UltralyticsLoaderService
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare ComfyUI inputs for Ultralytics model loading."""
choices = cls.service_class().model_choices()
return {
"required": {
"model_name": (
choices,
{
"default": choices[0],
"tooltip": (
"Ultralytics model file in the ComfyUI models folder."
),
},
)
}
}
def load(self, model_name: str) -> tuple[object, object, object]:
"""Load the selected detector and paired compatibility facades."""
loaded = self.service_class().load(model_name)
return loaded.detector_model, loaded.bbox_detector, loaded.segm_detector
+45
View File
@@ -0,0 +1,45 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for Prompt Control encode-style tags."""
from __future__ import annotations
from typing import Any
from ..domain.prompt_style import ENCODE_STYLE_VALUES, format_style_tag
class PromptEncodeStyle:
"""Build Prompt Control STYLE tags from encode-style selections."""
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("style_tag",)
OUTPUT_TOOLTIPS = ("Prompt Control STYLE tag text for prompt encoding workflows.",)
FUNCTION = "build"
CATEGORY = "SimpleSyrup/Prompt"
DESCRIPTION = "Builds a Prompt Control STYLE tag from an encode-style selection."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare Prompt Control encode-style selection inputs."""
return {
"required": {
"encode_style": (
list(ENCODE_STYLE_VALUES),
{
"default": "A1111",
"tooltip": (
"Prompt Control encoding style to write into the STYLE tag."
),
},
),
}
}
def build(self, encode_style: str) -> tuple[str]:
"""Return a Prompt Control STYLE tag."""
return (format_style_tag(encode_style),)
@@ -0,0 +1,64 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for Prompt Control encode-style normalization tags."""
from __future__ import annotations
from typing import Any
from ..domain.prompt_style import (
ENCODE_STYLE_VALUES,
NORMALIZATION_VALUES,
format_style_tag_with_normalization,
)
class PromptEncodeStyleAndNormalization:
"""Build STYLE tags from encode-style and normalization selections."""
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("style_tag",)
OUTPUT_TOOLTIPS = (
"Prompt Control STYLE tag text with the selected normalization behavior.",
)
FUNCTION = "build"
CATEGORY = "SimpleSyrup/Prompt"
DESCRIPTION = (
"Builds a Prompt Control STYLE tag from encode-style and normalization "
"selections."
)
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare Prompt Control encode-style and normalization selection inputs."""
return {
"required": {
"encode_style": (
list(ENCODE_STYLE_VALUES),
{
"default": "A1111",
"tooltip": (
"Prompt Control encoding style to write into the STYLE tag."
),
},
),
"normalization": (
list(NORMALIZATION_VALUES),
{
"default": "none",
"tooltip": (
"Prompt weight normalization mode written into the "
"STYLE tag."
),
},
),
}
}
def build(self, encode_style: str, normalization: str) -> tuple[str]:
"""Return a Prompt Control STYLE tag."""
return (format_style_tag_with_normalization(encode_style, normalization),)
+341
View File
@@ -0,0 +1,341 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for prompt-based SAM SEGS detection."""
from __future__ import annotations
from collections.abc import Callable
from typing import Any, ClassVar
import torch
from ..domain.segs import (
SORT_ORDER_OPTIONS,
NativeSegs,
to_impact_compatible_segs,
)
from ..masking.mask_ops import DETAIL_METHODS
from ..masking.prompt_segs_with_sam_service import PromptSEGSWithSAMService
from ..masking.segs_mask_ops import iter_single_images, validate_image_batch
from ..services.segs_output_service import (
CombinedSegsResult,
build_combined_segs_result,
)
class PromptSEGSWithSAM:
"""Expose prompt-based SAM region detection as SEGS."""
_service = PromptSEGSWithSAMService()
RETURN_TYPES = ("SEGS", "MASK")
RETURN_NAMES = ("segs", "mask")
OUTPUT_IS_LIST = (True, False)
OUTPUT_TOOLTIPS = (
"Prompted regions as separate or combined SEGS based on combine_segs.",
"Combined prompted area as a standard ComfyUI mask.",
)
FUNCTION = "prompt"
CATEGORY = "SimpleSyrup/Detection"
DESCRIPTION = (
"Finds prompt-matched regions with GroundingDINO, segments them with SAM, "
"and returns SEGS plus a combined mask."
)
SEARCH_ALIASES = ["sam", "groundingdino", "prompt", "segs"]
combined_builder: ClassVar[Callable[[object, NativeSegs], CombinedSegsResult]] = (
build_combined_segs_result
)
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, Any]]:
"""Declare deterministic ComfyUI inputs without model IO."""
return {
"required": {
"image": (
"IMAGE",
{"tooltip": "Image to search with the text prompt."},
),
"sam_model": (
"SAM_MODEL",
{
"tooltip": (
"SAM model used to create masks for the prompted boxes."
)
},
),
"grounding_dino_model": (
"GROUNDING_DINO_MODEL,DINO_MODEL",
{
"tooltip": (
"GroundingDINO model used to find boxes that match the "
"prompt."
)
},
),
"positive_prompt": (
"STRING",
{
"default": "",
"multiline": False,
"tooltip": "Text describing the regions to detect.",
},
),
"negative_prompt": (
"STRING",
{
"default": "",
"multiline": False,
"tooltip": (
"Text describing areas to subtract from the detected "
"regions."
),
},
),
"confidence_threshold": (
"FLOAT",
{
"default": 0.3,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": (
"Minimum GroundingDINO confidence required to create a "
"SAM prompt box."
),
},
),
"size_threshold": (
"INT",
{
"default": 10,
"min": 1,
"max": 8192,
"tooltip": (
"Discard final regions whose mask bounds are smaller "
"than this many pixels wide or tall."
),
},
),
"bbox_dilation": (
"INT",
{
"default": 0,
"min": -512,
"max": 512,
"step": 1,
"tooltip": (
"Grow or shrink prompt boxes in pixels before sending "
"them to SAM."
),
},
),
"mask_dilation": (
"INT",
{
"default": 0,
"min": -512,
"max": 512,
"step": 1,
"tooltip": (
"Grow or shrink each final region mask in pixels after "
"prompt subtraction."
),
},
),
"detail_method": (
list(DETAIL_METHODS),
{
"default": "GuidedFilter",
"tooltip": "Method used to refine mask edges.",
},
),
"detail_erode": (
"INT",
{
"default": 6,
"min": 0,
"max": 255,
"step": 1,
"tooltip": (
"Pixels trimmed inside the mask edge during refinement. "
"Higher values pull the refined edge inward."
),
},
),
"detail_dilate": (
"INT",
{
"default": 6,
"min": 0,
"max": 255,
"step": 1,
"tooltip": (
"Pixels added outside the mask edge during refinement. "
"Higher values give refinement more outside context."
),
},
),
"black_point": (
"FLOAT",
{
"default": 0.15,
"min": 0.0,
"max": 0.98,
"step": 0.01,
"tooltip": "Mask values at or below this become black.",
},
),
"white_point": (
"FLOAT",
{
"default": 0.99,
"min": 0.02,
"max": 1.0,
"step": 0.01,
"tooltip": "Mask values at or above this become white.",
},
),
"refine_mask": (
"BOOLEAN",
{
"default": True,
"label_on": "enabled",
"label_off": "disabled",
"tooltip": "Enable edge refinement for the final masks.",
},
),
"mask_refinement_max_size": (
"INT",
{
"default": 2048,
"min": 1,
"max": 16384,
"step": 64,
"tooltip": (
"Maximum working size for mask refinement. Larger "
"images are refined at a bounded size for performance."
),
},
),
"execution_device": (
["auto", "cpu"],
{
"default": "auto",
"tooltip": (
"Device for model-backed refinement. Auto uses normal "
"ComfyUI placement; CPU avoids GPU memory pressure."
),
},
),
"crop_factor": (
"FLOAT",
{
"default": 3.0,
"min": 1.0,
"max": 100.0,
"step": 0.1,
"tooltip": (
"How much context to include around each prompted "
"region. Higher values make larger SEG crops."
),
},
),
"sort_order": (
SORT_ORDER_OPTIONS,
{
"default": SORT_ORDER_OPTIONS[0],
"tooltip": (
"Order the returned SEGS before output and before the "
"combined mask is built."
),
},
),
"combine_segs": (
"BOOLEAN",
{
"default": False,
"label_on": "combined",
"label_off": "separate",
"tooltip": (
"Return one unioned SEGS region instead of separate "
"regions."
),
},
),
},
"optional": {
"vitmatte_model": (
"VITMATTE_MODEL",
{
"tooltip": (
"Optional ViTMatte model used when detail_method is "
"VITMatte."
)
},
),
},
}
def prompt(
self,
image: object,
sam_model: object,
grounding_dino_model: object,
positive_prompt: str,
negative_prompt: str,
confidence_threshold: float,
size_threshold: int,
bbox_dilation: int,
mask_dilation: int,
detail_method: str,
detail_erode: int,
detail_dilate: int,
black_point: float,
white_point: float,
refine_mask: bool,
mask_refinement_max_size: int,
execution_device: str,
crop_factor: float,
sort_order: str,
combine_segs: bool,
vitmatte_model: object | None = None,
) -> tuple[object, object]:
"""Prompt regions and return SEGS plus the combined mask output."""
image_batch = validate_image_batch(image, "Prompt SEGS w/ SAM")
segs_outputs: list[object] = []
mask_outputs: list[torch.Tensor] = []
for single_image in iter_single_images(image_batch):
segs = self._service.prompt(
image=single_image,
sam_model=sam_model,
grounding_dino_model=grounding_dino_model,
vitmatte_model=vitmatte_model,
positive_prompt=positive_prompt,
negative_prompt=negative_prompt,
confidence_threshold=confidence_threshold,
size_threshold=size_threshold,
bbox_dilation=bbox_dilation,
mask_dilation=mask_dilation,
detail_method=detail_method,
detail_erode=detail_erode,
detail_dilate=detail_dilate,
black_point=black_point,
white_point=white_point,
refine_mask=refine_mask,
mask_refinement_max_size=mask_refinement_max_size,
execution_device=execution_device,
crop_factor=crop_factor,
sort_order=sort_order,
)
combined = type(self).combined_builder(single_image, segs)
output_segs = combined.segs if combine_segs else segs
segs_outputs.append(to_impact_compatible_segs(output_segs))
mask_outputs.append(combined.mask)
return segs_outputs, torch.cat(mask_outputs, dim=0)
+278
View File
@@ -0,0 +1,278 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI nodes that reuse latent provenance behind decoded images."""
from __future__ import annotations
from collections.abc import Mapping
from importlib import import_module
from typing import Any, TypedDict, cast
from ..domain.graph_provenance import BrokenProvenance, GraphLink, VaeDecodeProvenance
from ..runtime.comfy_graph_provenance import (
ProvenanceTrace,
links_match,
trace_vae_decode_provenance,
)
LATENT_PROVENANCE_ERROR = (
"Unable to find an unmodified VAE Decode source for this image. Connect an "
"image that is directly decoded from a latent, or place this node immediately "
"after a transparent cube boundary whose image output comes from VAE Decode."
)
class ExpansionResult(TypedDict):
"""ComfyUI dynamic expansion result returned by provenance-aware nodes."""
expand: dict[str, dict[str, Any]]
result: tuple[list[object], ...]
class SimpleVAEEncode:
"""Encode images while reusing source latents when provenance proves safety."""
RETURN_TYPES = ("LATENT",)
RETURN_NAMES = ("latent",)
OUTPUT_TOOLTIPS = (
"Latent recovered from the decoded image source or produced by normal "
"VAE encoding.",
)
FUNCTION = "encode"
CATEGORY = "SimpleSyrup/Latent"
DESCRIPTION = (
"Encodes an image to latent space, reusing the source latent when the image "
"is proven to be an unmodified VAE decode."
)
SEARCH_ALIASES = [
"vae encode",
"encode image",
"recover latent",
"reuse decoded latent",
]
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, Any]]:
"""Declare the provenance-aware VAE encode input contract."""
return {
"required": {
"image": (
"IMAGE",
{
"rawLink": True,
"tooltip": (
"Image to encode. If it comes directly from VAE Decode "
"through transparent pass-through nodes, the original "
"latent is reused; edited or loaded images are encoded "
"normally."
),
},
),
"vae": (
"VAE",
{
"rawLink": True,
"tooltip": (
"VAE used for normal image encoding. Latent reuse is "
"used only when this VAE matches the VAE that decoded "
"the source image."
),
},
),
},
"hidden": {"prompt": "PROMPT"},
}
def encode(
self,
image: object,
vae: object,
prompt: Mapping[str, Any] | None = None,
) -> ExpansionResult:
"""Return the source latent when safe, otherwise expand to `VAEEncode`."""
provenance = _trace_prompt_provenance(prompt, image)
if (
isinstance(provenance, VaeDecodeProvenance)
and provenance.vae_link is not None
and links_match(vae, provenance.vae_link)
):
return _link_result(provenance.samples_link)
return _vae_encode_expansion(image, vae)
class UpscaleLatentFromImage:
"""Upscale the latent behind an unmodified decoded image."""
UPSCALE_METHODS = ["nearest-exact", "bilinear", "area", "bicubic", "bislerp"]
RETURN_TYPES = ("LATENT",)
RETURN_NAMES = ("latent",)
OUTPUT_TOOLTIPS = (
"Upscaled latent produced from the source latent behind the decoded image.",
)
FUNCTION = "upscale"
CATEGORY = "SimpleSyrup/Latent"
DESCRIPTION = "Upscales the latent that produced an unmodified decoded image."
SEARCH_ALIASES = [
"upscale latent",
"latent upscale from image",
"decoded image latent upscale",
]
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, Any]]:
"""Declare the latent-upscale-from-image input contract."""
return {
"required": {
"image": (
"IMAGE",
{
"rawLink": True,
"tooltip": (
"Connect an image that comes directly from VAE Decode, "
"with only transparent pass-through nodes between them. "
"Pixel edits, image upscalers, crops, detailers, "
"loaders, and previews break latent provenance."
),
},
),
"upscale_method": (
cls.UPSCALE_METHODS,
{
"tooltip": (
"Interpolation method passed to Comfy's Upscale Latent "
"By behavior."
)
},
),
"scale_factor": (
"FLOAT",
{
"default": 1.5,
"min": 0.01,
"max": 8.0,
"step": 0.01,
"tooltip": (
"Multiplier for the latent width and height passed to "
"Comfy's Upscale Latent By behavior."
),
},
),
},
"hidden": {"prompt": "PROMPT"},
}
def upscale(
self,
image: object,
upscale_method: str,
scale_factor: float,
prompt: Mapping[str, Any] | None = None,
) -> ExpansionResult:
"""Expand to Comfy's `LatentUpscaleBy` when provenance is valid."""
provenance = _trace_prompt_provenance(prompt, image)
if isinstance(provenance, BrokenProvenance):
raise ValueError(LATENT_PROVENANCE_ERROR)
return _latent_upscale_by_expansion(
provenance.samples_link,
upscale_method,
scale_factor,
)
def _trace_prompt_provenance(
prompt: Mapping[str, Any] | None,
image: object,
) -> ProvenanceTrace:
"""Trace an image link with the active ComfyUI node registry."""
if prompt is None:
return BrokenProvenance("prompt metadata is unavailable")
return trace_vae_decode_provenance(prompt, image, _node_registry())
def _link_result(link: GraphLink) -> ExpansionResult:
"""Return an existing graph link through a dynamic expansion result."""
return {"expand": {}, "result": (_to_comfy_link(link),)}
def _vae_encode_expansion(image: object, vae: object) -> ExpansionResult:
"""Build a dynamic `VAEEncode` fallback graph."""
builder = _graph_builder()
encoded = builder.node(
"VAEEncode",
pixels=_graph_value(image),
vae=_graph_value(vae),
)
return {"expand": builder.finalize(), "result": (encoded.out(0),)}
def _latent_upscale_by_expansion(
samples_link: GraphLink,
upscale_method: str,
scale_factor: float,
) -> ExpansionResult:
"""Build a dynamic `LatentUpscaleBy` graph around the source latent."""
builder = _graph_builder()
upscaled = builder.node(
"LatentUpscaleBy",
samples=_to_comfy_link(samples_link),
upscale_method=upscale_method,
scale_by=scale_factor,
)
return {"expand": builder.finalize(), "result": (upscaled.out(0),)}
def _graph_value(value: object) -> object:
"""Normalize graph-link values while preserving regular fallback values."""
if isinstance(value, tuple):
link = _tuple_link(value)
if link is not None:
return _to_comfy_link(link)
return value
def _tuple_link(value: tuple[object, ...]) -> GraphLink | None:
"""Return a typed graph link from a tuple value when possible."""
if len(value) != 2:
return None
node_id, output_slot = value
if not isinstance(node_id, str) or not isinstance(output_slot, int):
return None
return (node_id, output_slot)
def _to_comfy_link(link: GraphLink) -> list[object]:
"""Convert an internal graph link tuple to Comfy's serialized list shape."""
return [link[0], link[1]]
def _node_registry() -> Mapping[str, type[object]]:
"""Return ComfyUI's active node registry without importing it at module load."""
nodes_module = import_module("nodes")
return cast(
Mapping[str, type[object]],
nodes_module.NODE_CLASS_MAPPINGS,
)
def _graph_builder() -> Any:
"""Return Comfy's dynamic graph builder without import-time Comfy coupling."""
graph_utils = import_module("comfy_execution.graph_utils")
graph_builder = cast(Any, graph_utils.GraphBuilder)
return graph_builder()
+60
View File
@@ -0,0 +1,60 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for loading SAM models."""
from __future__ import annotations
from typing import Any
from ..runtime.model_choices import ModelChoiceService, default_choice
from ..runtime.model_downloads import ComfyProgressReporter
from ..runtime.sam_loader import SAMLoaderService
from . import tooltips
class SAMModelLoader:
"""Expose SAM loading as a conventional ComfyUI model loader."""
_service = SAMLoaderService()
_choices = ModelChoiceService()
RETURN_TYPES = ("SAM_MODEL",)
RETURN_NAMES = ("sam_model",)
OUTPUT_TOOLTIPS = (tooltips.SAM_MODEL_OUTPUT,)
FUNCTION = "load_model"
CATEGORY = "SimpleSyrup/Masking"
DESCRIPTION = "Loads a SAM model for Prompt SEGS w/ SAM and compatible nodes."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare deterministic SAM loader inputs."""
choices = cls._choices.sam_choices()
return {
"required": {
"sam_model": (
choices,
{
"default": default_choice(choices, "sam_vit_b (375MB)"),
"tooltip": tooltips.SAM_MODEL_INPUT,
},
),
}
}
def load_model(
self,
sam_model: str,
) -> tuple[object]:
"""Load and return a SAM-compatible model object."""
self._choices.reject_sentinel(sam_model)
return (
self._service.load_model(
sam_model=sam_model,
auto_download=True,
progress=ComfyProgressReporter(),
),
)
+63
View File
@@ -0,0 +1,63 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for bounded scale-factor values."""
from __future__ import annotations
from typing import Any
from . import tooltips
SCALE_FACTOR_DEFAULT = 1.5
SCALE_FACTOR_MIN = 1.0
SCALE_FACTOR_MAX = 5.0
SCALE_FACTOR_STEP = 0.1
def scale_factor_options(
default: float = SCALE_FACTOR_DEFAULT,
tooltip: str = tooltips.DETAIL_SCALE_FACTOR,
) -> dict[str, object]:
"""Return ComfyUI widget options for scale-factor controls."""
return {
"default": default,
"min": SCALE_FACTOR_MIN,
"max": SCALE_FACTOR_MAX,
"step": SCALE_FACTOR_STEP,
"tooltip": tooltip,
}
class ScaleFactor:
"""Expose a bounded float value for scale-factor inputs."""
RETURN_TYPES = ("FLOAT",)
RETURN_NAMES = ("scale_factor",)
OUTPUT_TOOLTIPS = (tooltips.SCALE_FACTOR_OUTPUT,)
FUNCTION = "get_value"
CATEGORY = "SimpleSyrup/Primitives"
DESCRIPTION = "Provides a bounded multiplier for scaling."
SEARCH_ALIASES = ["scale", "scale factor", "float", "primitive"]
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare the bounded scale-factor value input."""
return {
"required": {
"value": (
"FLOAT",
scale_factor_options(tooltip=tooltips.SCALE_FACTOR_VALUE),
),
}
}
def get_value(self, value: object) -> tuple[float]:
"""Return the provided scale-factor value."""
if isinstance(value, (int, float, str)):
return (float(value),)
raise TypeError("Scale Factor requires 'value' to be a float.")
+46
View File
@@ -0,0 +1,46 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for reusable seed values."""
from __future__ import annotations
class Seed:
"""Expose ComfyUI's native seed widget as a reusable integer output."""
RETURN_TYPES = ("INT",)
RETURN_NAMES = ("seed",)
OUTPUT_TOOLTIPS = ("Same seed value for wiring into multiple nodes.",)
FUNCTION = "execute"
CATEGORY = "SimpleSyrup/Utilities"
DESCRIPTION = "Provides a reusable seed value with ComfyUI seed controls."
SEARCH_ALIASES = ["seed", "random seed"]
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[object, ...]]]:
"""Declare the native ComfyUI seed input contract."""
return {
"required": {
"seed": (
"INT",
{
"default": 0,
"min": 0,
"max": 0xFFFFFFFFFFFFFFFF,
"control_after_generate": True,
"tooltip": (
"Seed value to reuse across nodes. Matching seed and "
"settings make random choices repeatable."
),
},
)
}
}
def execute(self, seed: int) -> tuple[int]:
"""Return the selected seed unchanged."""
return (seed,)
+133
View File
@@ -0,0 +1,133 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for simple Anima model loading."""
from __future__ import annotations
import importlib
from types import ModuleType
from typing import Any
from ..runtime.anima_loader import (
AUTO_CHOICE,
CLIP_DEVICES,
DIFFUSION_WEIGHT_DTYPES,
AnimaLoaderService,
)
from ..runtime.model_downloads import ComfyProgressReporter
from ..runtime.vae_loader import vae_choices
from . import tooltips
class SimpleLoadAnima:
"""Expose Anima diffusion, text encoder, and VAE loading as one node."""
_service = AnimaLoaderService()
RETURN_TYPES = ("MODEL", "CLIP", "VAE")
RETURN_NAMES = ("model", "clip", "vae")
OUTPUT_TOOLTIPS = (
tooltips.MODEL_OUTPUT,
tooltips.CLIP_OUTPUT,
tooltips.VAE_OUTPUT,
)
FUNCTION = "load_models"
CATEGORY = "SimpleSyrup/Loaders"
DESCRIPTION = "Loads Anima model components with auto-resolved Qwen assets."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare deterministic Simple Load Anima inputs."""
folder_paths = _folder_paths()
return {
"required": {
"diffusion_model": (
folder_paths.get_filename_list("diffusion_models"),
{
"tooltip": (
"Anima diffusion model file used as the main image "
"generation model."
)
},
),
"diffusion_weight_dtype": (
list(DIFFUSION_WEIGHT_DTYPES),
{
"default": "default",
"advanced": True,
"tooltip": (
"Weight precision for Anima. Lower precision can reduce "
"memory use but may slightly change results."
),
},
),
"text_encoder": (
_choices_with_auto(folder_paths.get_filename_list("text_encoders")),
{
"default": AUTO_CHOICE,
"tooltip": (
"Qwen text encoder used for Anima prompt understanding. "
"Auto selects the expected model."
),
},
),
"text_encoder_device": (
list(CLIP_DEVICES),
{
"default": "default",
"advanced": True,
"tooltip": (
"Device for the text encoder. Auto follows the normal "
"ComfyUI placement."
),
},
),
"vae": (
_choices_with_auto(vae_choices(folder_paths)),
{
"default": AUTO_CHOICE,
"tooltip": (
"VAE used to decode Anima latents. Auto selects the "
"expected Qwen image VAE."
),
},
),
}
}
def load_models(
self,
diffusion_model: str,
diffusion_weight_dtype: str,
text_encoder: str,
text_encoder_device: str,
vae: str,
) -> tuple[object, object, object]:
"""Load and return Anima's MODEL, CLIP, and VAE objects."""
return self._service.load_models(
diffusion_model=diffusion_model,
diffusion_weight_dtype=diffusion_weight_dtype,
text_encoder=text_encoder,
text_encoder_device=text_encoder_device,
vae=vae,
progress=ComfyProgressReporter(),
)
def _choices_with_auto(choices: list[str]) -> list[str]:
"""Return choices with the automatic selection first and deduplicated."""
return [AUTO_CHOICE, *(choice for choice in choices if choice != AUTO_CHOICE)]
def _folder_paths() -> ModuleType:
"""Import ComfyUI folder paths lazily."""
module: Any = importlib.import_module("folder_paths")
if not isinstance(module, ModuleType):
raise TypeError("folder_paths import did not return a module.")
return module
@@ -0,0 +1,96 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for checkpoint loading with optional VAE override."""
from __future__ import annotations
import importlib
from types import ModuleType
from typing import Any
from ..runtime.checkpoint_loader import (
CLIP_SKIP_DEFAULT,
USE_CHECKPOINT_VAE_CHOICE,
CheckpointLoaderService,
)
from ..runtime.vae_loader import vae_choices
from . import tooltips
class SimpleLoadCheckpoint:
"""Expose checkpoint loading with an optional external VAE override."""
_service = CheckpointLoaderService()
RETURN_TYPES = ("MODEL", "CLIP", "VAE")
RETURN_NAMES = ("model", "clip", "vae")
OUTPUT_TOOLTIPS = (
tooltips.MODEL_OUTPUT,
tooltips.CLIP_OUTPUT,
tooltips.VAE_OUTPUT,
)
FUNCTION = "load_checkpoint"
CATEGORY = "SimpleSyrup/Loaders"
DESCRIPTION = "Loads a checkpoint and optionally replaces its VAE."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare deterministic Simple Load Checkpoint inputs."""
folder_paths = _folder_paths()
return {
"required": {
"ckpt_name": (
folder_paths.get_filename_list("checkpoints"),
{"tooltip": tooltips.CHECKPOINT_MODEL_INPUT},
),
"vae_name": (
_choices_with_checkpoint_vae(vae_choices(folder_paths)),
{
"default": USE_CHECKPOINT_VAE_CHOICE,
"tooltip": tooltips.CHECKPOINT_VAE_INPUT,
},
),
"clip_skip": (
"BOOLEAN",
{
"default": CLIP_SKIP_DEFAULT,
"tooltip": tooltips.CLIP_SKIP_INPUT,
},
),
}
}
def load_checkpoint(
self,
ckpt_name: str,
vae_name: str,
clip_skip: bool,
) -> tuple[object, object, object]:
"""Load checkpoint MODEL and CLIP with the selected VAE."""
return self._service.load_checkpoint(
ckpt_name=ckpt_name,
vae_name=vae_name,
clip_skip=clip_skip,
)
def _choices_with_checkpoint_vae(choices: list[str]) -> list[str]:
"""Return choices with the checkpoint VAE selection first and deduplicated."""
return [
USE_CHECKPOINT_VAE_CHOICE,
*(choice for choice in choices if choice != USE_CHECKPOINT_VAE_CHOICE),
]
def _folder_paths() -> ModuleType:
"""Import ComfyUI folder paths lazily."""
module: Any = importlib.import_module("folder_paths")
if not isinstance(module, ModuleType):
raise TypeError("folder_paths import did not return a module.")
return module
+255
View File
@@ -0,0 +1,255 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for Tile & Tag SEGS."""
from __future__ import annotations
from collections.abc import Callable
from typing import Any, ClassVar
from ..domain.tile_segs import IRREGULAR_MASK_MODES, TileSEGSControls
from ..nodes import tooltips
from ..runtime.wd14_tagger import WD14TagFormattingControls
from ..services.tile_and_tag_segs_service import TileAndTagSEGSService
DEFAULT_EXCLUDE_TAGS = "1girl, solo, long_hair, short_hair, silhouette"
class TileAndTagSEGS:
"""Create tile SEGS and WD14 conditioning for SEGS detailing."""
RETURN_TYPES = ("SEGS", "CONDITIONING_BATCH")
RETURN_NAMES = ("segs", "positive")
OUTPUT_TOOLTIPS = (
tooltips.TILE_SEGS_OUTPUT,
tooltips.TILE_POSITIVE_OUTPUT,
)
FUNCTION = "tile_and_tag"
CATEGORY = "SimpleSyrup/Detailing"
DESCRIPTION = (
"Creates tile SEGS, tags each tile with a connected WD14 tagger, and "
"returns aligned conditioning for SEGS detailing."
)
SEARCH_ALIASES = ["tile", "tag", "wd14", "segs", "detail"]
service_class: ClassVar[Callable[[], TileAndTagSEGSService]] = TileAndTagSEGSService
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare ComfyUI inputs for tile creation and WD14 tagging."""
return {
"required": {
"image": ("IMAGE", {"tooltip": tooltips.TILE_IMAGE}),
"clip": ("CLIP", {"tooltip": tooltips.TILE_CLIP}),
"wd14_tagger": (
"WD14_TAGGER",
{"tooltip": tooltips.TILE_WD14_TAGGER},
),
"universal_positive": (
"STRING",
{
"default": "",
"multiline": False,
"tooltip": tooltips.TILE_UNIVERSAL_POSITIVE,
},
),
"bbox_size": (
"INT",
{
"default": 872,
"min": 64,
"max": 4096,
"step": 8,
"tooltip": tooltips.TILE_BBOX_SIZE,
},
),
"crop_factor": (
"FLOAT",
{
"default": 1.1,
"min": 1.0,
"max": 10.0,
"step": 0.01,
"tooltip": tooltips.TILE_CROP_FACTOR,
},
),
"min_overlap": (
"INT",
{
"default": 16,
"min": 0,
"max": 512,
"step": 1,
"tooltip": tooltips.TILE_MIN_OVERLAP,
},
),
"filter_segs_dilation": (
"INT",
{
"default": 20,
"min": -255,
"max": 255,
"step": 1,
"tooltip": tooltips.TILE_FILTER_SEGS_DILATION,
},
),
"mask_irregularity": (
"FLOAT",
{
"default": 0,
"min": 0.0,
"max": 1.0,
"step": 0.01,
"tooltip": tooltips.TILE_MASK_IRREGULARITY,
},
),
"irregular_mask_mode": (
IRREGULAR_MASK_MODES,
{
"default": "Reuse fast",
"tooltip": tooltips.TILE_IRREGULAR_MASK_MODE,
},
),
"threshold": (
"FLOAT",
{
"default": 0.35,
"min": 0.0,
"max": 1.0,
"step": 0.05,
"tooltip": tooltips.TILE_THRESHOLD,
},
),
"character_threshold": (
"FLOAT",
{
"default": 1.0,
"min": 0.0,
"max": 1.0,
"step": 0.05,
"tooltip": tooltips.TILE_CHARACTER_THRESHOLD,
},
),
"replace_underscore": (
"BOOLEAN",
{
"default": True,
"tooltip": tooltips.TILE_REPLACE_UNDERSCORE,
},
),
"trailing_comma": (
"BOOLEAN",
{
"default": False,
"tooltip": tooltips.TILE_TRAILING_COMMA,
},
),
"exclude_tags": (
"STRING",
{
"default": DEFAULT_EXCLUDE_TAGS,
"multiline": False,
"tooltip": tooltips.TILE_EXCLUDE_TAGS,
},
),
},
}
def tile_and_tag(
self,
image: object,
clip: Any,
wd14_tagger: object,
universal_positive: object,
bbox_size: object,
crop_factor: object,
min_overlap: object,
filter_segs_dilation: object,
mask_irregularity: object,
irregular_mask_mode: object,
threshold: object,
character_threshold: object,
replace_underscore: object,
trailing_comma: object,
exclude_tags: object,
) -> tuple[object, object]:
"""Create ordered tile SEGS and WD14 conditioning."""
tile_controls = TileSEGSControls(
bbox_size=_int_input(bbox_size, "bbox_size"),
crop_factor=_float_input(crop_factor, "crop_factor"),
min_overlap=_int_input(min_overlap, "min_overlap"),
filter_segs_dilation=_int_input(
filter_segs_dilation,
"filter_segs_dilation",
),
mask_irregularity=_float_input(mask_irregularity, "mask_irregularity"),
irregular_mask_mode=_str_input(
irregular_mask_mode,
"irregular_mask_mode",
),
)
tag_controls = WD14TagFormattingControls(
threshold=_float_input(threshold, "threshold"),
character_threshold=_float_input(
character_threshold,
"character_threshold",
),
replace_underscore=_bool_input(
replace_underscore,
"replace_underscore",
),
trailing_comma=_bool_input(trailing_comma, "trailing_comma"),
exclude_tags=_str_input(exclude_tags, "exclude_tags"),
)
result = (
type(self)
.service_class()
.tile_and_tag(
image=image,
clip=clip,
wd14_tagger=wd14_tagger,
tile_controls=tile_controls,
tag_controls=tag_controls,
universal_positive=_str_input(
universal_positive,
"universal_positive",
),
)
)
return result.segs, result.positive
def _int_input(value: object, name: str) -> int:
"""Return an integer node input."""
if isinstance(value, (int, float, str)):
return int(value)
raise TypeError(f"Tile & Tag SEGS requires '{name}' to be an int.")
def _float_input(value: object, name: str) -> float:
"""Return a float node input."""
if isinstance(value, (int, float, str)):
return float(value)
raise TypeError(f"Tile & Tag SEGS requires '{name}' to be a float.")
def _str_input(value: object, name: str) -> str:
"""Return a string node input."""
if isinstance(value, str):
return value
raise TypeError(f"Tile & Tag SEGS requires '{name}' to be a string.")
def _bool_input(value: object, name: str) -> bool:
"""Return a boolean node input."""
if isinstance(value, bool):
return value
raise TypeError(f"Tile & Tag SEGS requires '{name}' to be a boolean.")
+190
View File
@@ -0,0 +1,190 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Shared tooltip text for ComfyUI node declarations."""
from __future__ import annotations
CHECKPOINT_MODEL_INPUT = (
"Checkpoint file to load. This supplies the base MODEL, CLIP, and checkpoint "
"VAE for the workflow."
)
CHECKPOINT_VAE_INPUT = (
"VAE to output with the checkpoint. Use the checkpoint VAE to keep the model's "
"own decoder, or choose another VAE to replace it."
)
CLIP_SKIP_INPUT = (
"Use ComfyUI's clip-skip behavior for prompt encoding. Enable it for models "
"that expect the next-to-last CLIP layer."
)
MODEL_OUTPUT = "Loaded diffusion model for downstream MODEL inputs."
CLIP_OUTPUT = "Loaded text encoder for downstream CLIP inputs."
VAE_OUTPUT = "Loaded VAE used to encode images to latents and decode latents to images."
SAM_MODEL_OUTPUT = "Loaded SAM model for prompt-based mask and SEGS creation."
GROUNDING_DINO_MODEL_OUTPUT = (
"Loaded GroundingDINO model for finding prompt-matched boxes in images."
)
VITMATTE_MODEL_OUTPUT = "Loaded ViTMatte model for refining mask edges."
WD14_TAGGER_OUTPUT = "Loaded WD14 tagger for generating prompt tags from image crops."
SAM_MODEL_INPUT = "SAM model choice used to create masks from detected boxes."
GROUNDING_DINO_MODEL_INPUT = (
"GroundingDINO model choice used to find boxes that match a text prompt."
)
GROUNDING_DINO_TEXT_ENCODER_INPUT = (
"BERT text encoder paired with GroundingDINO for prompt matching."
)
VITMATTE_MODEL_INPUT = "ViTMatte model choice used for mask edge refinement."
WD14_MODEL_INPUT = "WD14 tagger model choice used to generate tags from image crops."
SAMPLING_MODEL = "Diffusion model used to denoise the input latent."
SAMPLING_SEED = (
"Seed used to create sampling noise. Reusing it with matching settings makes "
"results repeatable."
)
SAMPLING_STEPS = (
"Number of denoising steps. More steps can add refinement but take longer."
)
SAMPLING_CFG = (
"Prompt guidance strength. Higher values follow the positive prompt more "
"strongly but can look overcooked."
)
SAMPLER_NAME = "Sampling algorithm. It affects the image's look, speed, and stability."
SCHEDULER = (
"Noise schedule used during sampling. It changes how quickly structure and "
"detail form."
)
POSITIVE_CONDITIONING = "Positive conditioning that guides what the sampler should add."
NEGATIVE_CONDITIONING = (
"Negative conditioning that guides what the sampler should avoid."
)
LATENT_IMAGE = "Latent input whose samples will be denoised."
DENOISE_STRENGTH = (
"Sampling strength. Lower values preserve the input more; higher values allow "
"larger changes."
)
DENOISED_LATENT_OUTPUT = "Denoised latent for VAE decode or more latent processing."
LATENT_TILE_WIDTH = (
"Width of each latent tile. Larger tiles see more context but use more memory."
)
LATENT_TILE_HEIGHT = (
"Height of each latent tile. Larger tiles see more context but use more memory."
)
LATENT_TILE_OVERLAP = (
"Overlap between latent tiles. Larger overlaps reduce seams but increase "
"sampling work."
)
LATENT_TILE_BATCH_SIZE = (
"Number of latent tiles sampled together. Higher values can be faster but use "
"more memory."
)
DETAIL_IMAGE = (
"Source image containing the regions to improve. Detailed crops are blended "
"back into this image."
)
DETAIL_SEGS = "SEGS regions that choose which parts of the image are detailed."
DETAIL_MODEL = "Diffusion model used to resample each detailed crop."
DETAIL_VAE = "VAE used to encode crops to latents and decode the edited crops."
DETAIL_POSITIVE = (
"Positive conditioning for detailing. A conditioning batch is matched to SEGS "
"order."
)
DETAIL_NEGATIVE = (
"Negative conditioning for detailing. A conditioning batch is matched to SEGS "
"order."
)
DETAIL_SCALE_FACTOR = (
"Crop enlargement multiplier. Larger values give the sampler more detail room "
"but use more memory."
)
SCALE_FACTOR_VALUE = (
"Scaling multiplier. 1.0 keeps the target at its current size; larger values "
"scale it up, with a maximum of 5.0x."
)
DETAIL_UPSCALE_METHOD = (
"Resize method for scaled crops. Sharper methods preserve detail but can show "
"more ringing."
)
DETAIL_CLAMP_SIZE = (
"Maximum crop size in pixels after scaling. Use 0 to leave crop size unclamped."
)
DETAIL_FEATHER = (
"Mask edge softness in pixels. Higher values blend edits more gently into the "
"image."
)
DETAIL_NOISE_MASK = (
"Limit sampling noise to the selected mask area so unchanged pixels stay more "
"stable."
)
DETAIL_NOISE_MASK_FEATHER = (
"Noise mask edge softness in pixels. Higher values make the sampled area fade "
"out more gradually."
)
DETAIL_TILED_ENCODE = (
"Encode large crops in tiles. This lowers memory use but is usually slower."
)
DETAIL_TILED_DECODE = (
"Decode large crops in tiles. This lowers memory use but is usually slower."
)
DETAIL_IMAGE_OUTPUT = "Image with the detailed regions blended back into place."
SCALE_FACTOR_OUTPUT = "Multiplier used to scale a connected target."
REGIONAL_GLOBAL_NEGATIVE = (
"Negative conditioning applied across the full regional pass."
)
REGIONAL_GLOBAL_POSITIVE = (
"Positive conditioning that gives full-image context to the regional pass."
)
REGIONAL_POSITIVE_BATCH = (
"Per-region positive conditioning matched to the incoming SEGS order."
)
TILE_IMAGE = "Image to split into tile SEGS for tagging or downstream workflows."
TILE_CLIP = "CLIP model used to encode each generated tile prompt."
TILE_WD14_TAGGER = "WD14 tagger that reads each tile crop and suggests prompt tags."
TILE_UNIVERSAL_POSITIVE = (
"Positive prompt text added before every generated tile tag prompt."
)
TILE_BBOX_SIZE = "Target tile box size in pixels."
TILE_CROP_FACTOR = (
"Tile crop expansion. Larger values include more surrounding context for tags."
)
TILE_MIN_OVERLAP = (
"Minimum pixel overlap between tile regions. Higher values reduce gaps but "
"repeat more image area."
)
TILE_FILTER_SEGS_DILATION = (
"Grow or shrink tile masks before filtering. Positive values expand masks; "
"negative values contract them."
)
TILE_MASK_IRREGULARITY = (
"Organic variation added to tile masks. Higher values make masks less rectangular."
)
TILE_IRREGULAR_MASK_MODE = (
"How irregular masks are generated. Reuse is steadier; random varies each "
"tile; quality modes do more work."
)
TILE_THRESHOLD = (
"Minimum WD14 confidence for general tags. Higher values keep fewer, more "
"certain tags."
)
TILE_CHARACTER_THRESHOLD = (
"Minimum WD14 confidence for character tags. Higher values keep fewer, more "
"certain character tags."
)
TILE_REPLACE_UNDERSCORE = (
"Replace underscores with spaces so generated tags read more naturally."
)
TILE_TRAILING_COMMA = (
"Add a comma after generated tag text for easier prompt composition."
)
TILE_EXCLUDE_TAGS = "Comma-separated tags removed from generated tile prompts."
TILE_SEGS_OUTPUT = "Generated tile SEGS in the same order as the conditioning batch."
TILE_POSITIVE_OUTPUT = (
"Positive conditioning from WD14 tile tags, matched to SEGS order."
)
@@ -0,0 +1,63 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for loading ViTMatte models."""
from __future__ import annotations
from typing import Any
from ..runtime.model_choices import ModelChoiceService, default_choice
from ..runtime.model_downloads import ComfyProgressReporter
from ..runtime.vitmatte_loader import ViTMatteLoaderService
from . import tooltips
class ViTMatteModelLoader:
"""Expose ViTMatte loading as a conventional model loader."""
_service = ViTMatteLoaderService()
_choices = ModelChoiceService()
RETURN_TYPES = ("VITMATTE_MODEL",)
RETURN_NAMES = ("vitmatte_model",)
OUTPUT_TOOLTIPS = (tooltips.VITMATTE_MODEL_OUTPUT,)
FUNCTION = "load_model"
CATEGORY = "SimpleSyrup/Masking"
DESCRIPTION = "Loads a ViTMatte model for Prompt SEGS w/ SAM edge refinement."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare deterministic ViTMatte loader inputs."""
choices = cls._choices.vitmatte_choices()
return {
"required": {
"vitmatte_model": (
choices,
{
"default": default_choice(
choices,
"vitmatte-small-composition-1k",
),
"tooltip": tooltips.VITMATTE_MODEL_INPUT,
},
),
}
}
def load_model(
self,
vitmatte_model: str,
) -> tuple[object]:
"""Load and return a ViTMatte-compatible model object."""
self._choices.reject_sentinel(vitmatte_model)
return (
self._service.load_model(
vitmatte_model=vitmatte_model,
auto_download=True,
progress=ComfyProgressReporter(),
),
)
+61
View File
@@ -0,0 +1,61 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI node declaration for loading WD14 taggers."""
from __future__ import annotations
from typing import Any
from ..runtime.model_catalog import DEFAULT_WD14_TAGGER_MODEL
from ..runtime.model_choices import ModelChoiceService, default_choice
from ..runtime.model_downloads import ComfyProgressReporter
from ..runtime.wd14_tagger_loader import WD14TaggerLoaderService
from . import tooltips
class WD14TaggerLoader:
"""Expose WD14 tagger loading as a conventional ComfyUI model loader."""
_service = WD14TaggerLoaderService()
_choices = ModelChoiceService()
RETURN_TYPES = ("WD14_TAGGER",)
RETURN_NAMES = ("wd14_tagger",)
OUTPUT_TOOLTIPS = (tooltips.WD14_TAGGER_OUTPUT,)
FUNCTION = "load_model"
CATEGORY = "SimpleSyrup/Tagging"
DESCRIPTION = "Loads a WD14 tagger for compatible SimpleSyrup tagging workflows."
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Declare deterministic WD14 tagger loader inputs."""
choices = cls._choices.wd14_tagger_choices()
return {
"required": {
"wd14_model": (
choices,
{
"default": default_choice(
choices,
DEFAULT_WD14_TAGGER_MODEL,
),
"tooltip": tooltips.WD14_MODEL_INPUT,
},
),
}
}
def load_model(self, wd14_model: str) -> tuple[object]:
"""Load and return a WD14 tagger model object."""
self._choices.reject_sentinel(wd14_model)
return (
self._service.load_model(
wd14_model=wd14_model,
auto_download=True,
progress=ComfyProgressReporter(),
),
)
+41
View File
@@ -0,0 +1,41 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Comfy v3 node registration for SimpleSyrup."""
from __future__ import annotations
from ..runtime.prompt_control_availability import prompt_control_is_available
def get_nodes() -> list[type[object]]:
"""Return v3 nodes that can be advertised in this environment."""
from .scale_factor import ScaleFactorV3
from .simple_load_checkpoint import SimpleLoadCheckpointV3
from .tile_and_tag_segs import TileAndTagSEGSV3
from .wd14_tagger_loader import WD14TaggerLoaderV3
if not prompt_control_is_available():
return [
WD14TaggerLoaderV3,
TileAndTagSEGSV3,
SimpleLoadCheckpointV3,
ScaleFactorV3,
]
from .encode_prompt_batch_with_prompt_control import (
EncodePromptBatchWithPromptControl,
)
return [
WD14TaggerLoaderV3,
TileAndTagSEGSV3,
SimpleLoadCheckpointV3,
ScaleFactorV3,
EncodePromptBatchWithPromptControl,
]
__all__ = ["get_nodes"]
@@ -0,0 +1,113 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Comfy v3 node for Prompt Control prompt batch encoding."""
from __future__ import annotations
from importlib import import_module
from typing import TYPE_CHECKING, Any, ClassVar
from ..runtime.prompt_control_batch_graph import PromptControlBatchGraphBuilder
if TYPE_CHECKING:
class _ComfyNodeBase:
"""Type-checking base for Comfy v3 nodes."""
RETURN_TYPES: ClassVar[list[str]]
RETURN_NAMES: ClassVar[list[str]]
@classmethod
def INPUT_TYPES(cls) -> dict[str, dict[str, tuple[Any, ...]]]:
"""Return v1-compatible input metadata."""
raise NotImplementedError
else:
_ComfyNodeBase = import_module("comfy_api.latest").io.ComfyNode
_comfy_io: Any = None if TYPE_CHECKING else import_module("comfy_api.latest").io
ConditioningBatchIO: Any = (
None if TYPE_CHECKING else _comfy_io.Custom("CONDITIONING_BATCH")
)
class EncodePromptBatchWithPromptControl(_ComfyNodeBase):
"""Encode separator-delimited prompts with Prompt Control scheduling."""
@classmethod
def define_schema(cls) -> Any:
"""Declare the Prompt Control batch encoder schema."""
return _comfy_io.Schema(
node_id="SimpleSyrup.EncodePromptBatchWithPromptControl",
display_name="Encode Prompt Batch w/ Prompt Control",
enable_expand=True,
category="SimpleSyrup/Conditioning",
description=(
"Encodes [SEP]-separated prompts into per-segment Prompt Control "
"conditioning batches."
),
inputs=[
_comfy_io.Clip.Input(
"clip",
raw_link=True,
tooltip=(
"Prompt Control-ready CLIP connection used to encode each "
"prompt entry."
),
),
_comfy_io.String.Input(
"positive_prompt",
multiline=True,
default="",
tooltip=(
"Positive Prompt Control prompts in SEGS order, separated "
"by the separator text."
),
),
_comfy_io.String.Input(
"negative_prompt",
multiline=True,
default="",
tooltip=(
"Negative Prompt Control prompts in SEGS order, separated "
"by the separator text."
),
),
_comfy_io.String.Input(
"separator",
default="[SEP]",
tooltip="Text marker that splits prompts into per-SEGS entries.",
),
],
outputs=[
ConditioningBatchIO.Output(
"positive",
tooltip="Positive conditioning entries selected by SEGS order.",
),
ConditioningBatchIO.Output(
"negative",
tooltip="Negative conditioning entries selected by SEGS order.",
),
],
)
@classmethod
def execute(
cls,
clip: Any,
positive_prompt: str,
negative_prompt: str,
separator: str,
) -> Any:
"""Build lazy Prompt Control graph expansion for prompt batches."""
return PromptControlBatchGraphBuilder().build(
clip=clip,
positive_prompt=positive_prompt,
negative_prompt=negative_prompt,
separator=separator,
)
+70
View File
@@ -0,0 +1,70 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Comfy v3 node wrapper for Scale Factor."""
from __future__ import annotations
from importlib import import_module
from typing import TYPE_CHECKING, Any, ClassVar
from ..nodes import tooltips
from ..nodes.scale_factor import (
SCALE_FACTOR_DEFAULT,
SCALE_FACTOR_MAX,
SCALE_FACTOR_MIN,
SCALE_FACTOR_STEP,
ScaleFactor,
)
if TYPE_CHECKING:
class _ComfyNodeBase:
"""Type-checking base for Comfy v3 nodes."""
RETURN_TYPES: ClassVar[list[str]]
RETURN_NAMES: ClassVar[list[str]]
else:
_ComfyNodeBase = import_module("comfy_api.latest").io.ComfyNode
_comfy_io: Any = None if TYPE_CHECKING else import_module("comfy_api.latest").io
class ScaleFactorV3(_ComfyNodeBase):
"""Expose Scale Factor through Comfy's v3 extension API."""
@classmethod
def define_schema(cls) -> Any:
"""Declare the Scale Factor v3 schema."""
return _comfy_io.Schema(
node_id="SimpleSyrup.ScaleFactor",
display_name="Scale Factor",
category="SimpleSyrup/Primitives",
description="Provides a bounded multiplier for scaling.",
search_aliases=["scale", "scale factor", "float", "primitive"],
inputs=[
_comfy_io.Float.Input(
"value",
default=SCALE_FACTOR_DEFAULT,
min=SCALE_FACTOR_MIN,
max=SCALE_FACTOR_MAX,
step=SCALE_FACTOR_STEP,
tooltip=tooltips.SCALE_FACTOR_VALUE,
),
],
outputs=[
_comfy_io.Float.Output(
"scale_factor",
tooltip=tooltips.SCALE_FACTOR_OUTPUT,
),
],
)
@classmethod
def execute(cls, value: float) -> tuple[float]:
"""Return the scale-factor value through the legacy implementation."""
return ScaleFactor().get_value(value=value)
@@ -0,0 +1,99 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Comfy v3 node wrapper for checkpoint loading with optional VAE override."""
from __future__ import annotations
from importlib import import_module
from typing import TYPE_CHECKING, Any, ClassVar
from ..nodes import tooltips
from ..nodes.simple_load_checkpoint import SimpleLoadCheckpoint
if TYPE_CHECKING:
class _ComfyNodeBase:
"""Type-checking base for Comfy v3 nodes."""
RETURN_TYPES: ClassVar[list[str]]
RETURN_NAMES: ClassVar[list[str]]
else:
_ComfyNodeBase = import_module("comfy_api.latest").io.ComfyNode
_comfy_io: Any = None if TYPE_CHECKING else import_module("comfy_api.latest").io
class SimpleLoadCheckpointV3(_ComfyNodeBase):
"""Expose Simple Load Checkpoint through Comfy's v3 extension API."""
@classmethod
def define_schema(cls) -> Any:
"""Declare the Simple Load Checkpoint v3 schema."""
required = SimpleLoadCheckpoint.INPUT_TYPES()["required"]
checkpoint_choices = list(required["ckpt_name"][0])
vae_options = required["vae_name"][1]
vae_choices = list(required["vae_name"][0])
vae_default = str(vae_options["default"])
clip_skip_options = required["clip_skip"][1]
clip_skip_default = clip_skip_options["default"]
if not isinstance(clip_skip_default, bool):
raise TypeError("clip_skip default must be a boolean.")
return _comfy_io.Schema(
node_id="SimpleSyrup.SimpleLoadCheckpoint",
display_name="Simple Load Checkpoint",
category="SimpleSyrup/Loaders",
description="Loads a checkpoint and optionally replaces its VAE.",
search_aliases=["load checkpoint", "checkpoint", "ckpt", "vae"],
inputs=[
_comfy_io.Combo.Input(
"ckpt_name",
options=checkpoint_choices,
tooltip=tooltips.CHECKPOINT_MODEL_INPUT,
),
_comfy_io.Combo.Input(
"vae_name",
options=vae_choices,
default=vae_default,
tooltip=tooltips.CHECKPOINT_VAE_INPUT,
),
_comfy_io.Boolean.Input(
"clip_skip",
default=clip_skip_default,
tooltip=tooltips.CLIP_SKIP_INPUT,
),
],
outputs=[
_comfy_io.Model.Output(
"model",
tooltip=tooltips.MODEL_OUTPUT,
),
_comfy_io.Clip.Output(
"clip",
tooltip=tooltips.CLIP_OUTPUT,
),
_comfy_io.Vae.Output(
"vae",
tooltip=tooltips.VAE_OUTPUT,
),
],
)
@classmethod
def execute(
cls,
ckpt_name: str,
vae_name: str,
clip_skip: bool,
) -> tuple[object, object, object]:
"""Run the legacy loader implementation behind the v3 schema."""
return SimpleLoadCheckpoint().load_checkpoint(
ckpt_name=ckpt_name,
vae_name=vae_name,
clip_skip=clip_skip,
)
+194
View File
@@ -0,0 +1,194 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Comfy v3 node wrapper for Tile & Tag SEGS."""
from __future__ import annotations
from importlib import import_module
from typing import TYPE_CHECKING, Any, ClassVar
from ..domain.tile_segs import IRREGULAR_MASK_MODES
from ..nodes import tooltips
from ..nodes.tile_and_tag_segs import DEFAULT_EXCLUDE_TAGS, TileAndTagSEGS
if TYPE_CHECKING:
class _ComfyNodeBase:
"""Type-checking base for Comfy v3 nodes."""
RETURN_TYPES: ClassVar[list[str]]
RETURN_NAMES: ClassVar[list[str]]
else:
_ComfyNodeBase = import_module("comfy_api.latest").io.ComfyNode
_comfy_io: Any = None if TYPE_CHECKING else import_module("comfy_api.latest").io
ConditioningBatchIO: Any = (
None if TYPE_CHECKING else _comfy_io.Custom("CONDITIONING_BATCH")
)
WD14TaggerIO: Any = None if TYPE_CHECKING else _comfy_io.Custom("WD14_TAGGER")
class TileAndTagSEGSV3(_ComfyNodeBase):
"""Expose Tile & Tag SEGS through Comfy's v3 extension API."""
@classmethod
def define_schema(cls) -> Any:
"""Declare the Tile & Tag SEGS v3 schema."""
return _comfy_io.Schema(
node_id="SimpleSyrup.TileAndTagSEGS",
display_name="Tile & Tag SEGS",
category="SimpleSyrup/Detailing",
description=(
"Creates tile SEGS, tags each tile with a connected WD14 tagger, "
"and returns aligned conditioning for SEGS detailing."
),
search_aliases=["tile", "tag", "wd14", "segs", "detail"],
inputs=[
_comfy_io.Image.Input("image", tooltip=tooltips.TILE_IMAGE),
_comfy_io.Clip.Input(
"clip",
tooltip=tooltips.TILE_CLIP,
),
WD14TaggerIO.Input(
"wd14_tagger",
tooltip=tooltips.TILE_WD14_TAGGER,
),
_comfy_io.String.Input(
"universal_positive",
multiline=False,
default="",
tooltip=tooltips.TILE_UNIVERSAL_POSITIVE,
),
_comfy_io.Int.Input(
"bbox_size",
default=872,
min=64,
max=4096,
step=8,
tooltip=tooltips.TILE_BBOX_SIZE,
),
_comfy_io.Float.Input(
"crop_factor",
default=1.1,
min=1.0,
max=10.0,
step=0.01,
tooltip=tooltips.TILE_CROP_FACTOR,
),
_comfy_io.Int.Input(
"min_overlap",
default=16,
min=0,
max=512,
step=1,
tooltip=tooltips.TILE_MIN_OVERLAP,
),
_comfy_io.Int.Input(
"filter_segs_dilation",
default=20,
min=-255,
max=255,
step=1,
tooltip=tooltips.TILE_FILTER_SEGS_DILATION,
),
_comfy_io.Float.Input(
"mask_irregularity",
default=0.0,
min=0.0,
max=1.0,
step=0.01,
tooltip=tooltips.TILE_MASK_IRREGULARITY,
),
_comfy_io.Combo.Input(
"irregular_mask_mode",
options=list(IRREGULAR_MASK_MODES),
default="Reuse fast",
tooltip=tooltips.TILE_IRREGULAR_MASK_MODE,
),
_comfy_io.Float.Input(
"threshold",
default=0.35,
min=0.0,
max=1.0,
step=0.05,
tooltip=tooltips.TILE_THRESHOLD,
),
_comfy_io.Float.Input(
"character_threshold",
default=1.0,
min=0.0,
max=1.0,
step=0.05,
tooltip=tooltips.TILE_CHARACTER_THRESHOLD,
),
_comfy_io.Boolean.Input(
"replace_underscore",
default=True,
tooltip=tooltips.TILE_REPLACE_UNDERSCORE,
),
_comfy_io.Boolean.Input(
"trailing_comma",
default=False,
tooltip=tooltips.TILE_TRAILING_COMMA,
),
_comfy_io.String.Input(
"exclude_tags",
multiline=False,
default=DEFAULT_EXCLUDE_TAGS,
tooltip=tooltips.TILE_EXCLUDE_TAGS,
),
],
outputs=[
_comfy_io.SEGS.Output(
"segs",
tooltip=tooltips.TILE_SEGS_OUTPUT,
),
ConditioningBatchIO.Output(
"positive",
tooltip=tooltips.TILE_POSITIVE_OUTPUT,
),
],
)
@classmethod
def execute(
cls,
image: object,
clip: Any,
wd14_tagger: object,
universal_positive: str,
bbox_size: int,
crop_factor: float,
min_overlap: int,
filter_segs_dilation: int,
mask_irregularity: float,
irregular_mask_mode: str,
threshold: float,
character_threshold: float,
replace_underscore: bool,
trailing_comma: bool,
exclude_tags: str,
) -> tuple[object, object]:
"""Run the legacy implementation behind the v3 schema."""
return TileAndTagSEGS().tile_and_tag(
image=image,
clip=clip,
wd14_tagger=wd14_tagger,
universal_positive=universal_positive,
bbox_size=bbox_size,
crop_factor=crop_factor,
min_overlap=min_overlap,
filter_segs_dilation=filter_segs_dilation,
mask_irregularity=mask_irregularity,
irregular_mask_mode=irregular_mask_mode,
threshold=threshold,
character_threshold=character_threshold,
replace_underscore=replace_underscore,
trailing_comma=trailing_comma,
exclude_tags=exclude_tags,
)
@@ -0,0 +1,68 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Comfy v3 node wrapper for loading WD14 taggers."""
from __future__ import annotations
from importlib import import_module
from typing import TYPE_CHECKING, Any, ClassVar
from ..nodes import tooltips
from ..nodes.wd14_tagger_loader import WD14TaggerLoader
from ..runtime.model_catalog import DEFAULT_WD14_TAGGER_MODEL
from ..runtime.model_choices import ModelChoiceService, default_choice
if TYPE_CHECKING:
class _ComfyNodeBase:
"""Type-checking base for Comfy v3 nodes."""
RETURN_TYPES: ClassVar[list[str]]
RETURN_NAMES: ClassVar[list[str]]
else:
_ComfyNodeBase = import_module("comfy_api.latest").io.ComfyNode
_comfy_io: Any = None if TYPE_CHECKING else import_module("comfy_api.latest").io
WD14TaggerIO: Any = None if TYPE_CHECKING else _comfy_io.Custom("WD14_TAGGER")
class WD14TaggerLoaderV3(_ComfyNodeBase):
"""Expose Load WD14 Tagger through Comfy's v3 extension API."""
@classmethod
def define_schema(cls) -> Any:
"""Declare the Load WD14 Tagger v3 schema."""
choices = ModelChoiceService().wd14_tagger_choices()
return _comfy_io.Schema(
node_id="SimpleSyrup.WD14TaggerLoader",
display_name="Load WD14 Tagger",
category="SimpleSyrup/Tagging",
description=(
"Loads a WD14 tagger for compatible SimpleSyrup tagging workflows."
),
search_aliases=["wd14", "tagger", "tags"],
inputs=[
_comfy_io.Combo.Input(
"wd14_model",
options=choices,
default=default_choice(choices, DEFAULT_WD14_TAGGER_MODEL),
tooltip=tooltips.WD14_MODEL_INPUT,
),
],
outputs=[
WD14TaggerIO.Output(
"wd14_tagger",
tooltip=tooltips.WD14_TAGGER_OUTPUT,
),
],
)
@classmethod
def execute(cls, wd14_model: str) -> tuple[object]:
"""Run the legacy loader implementation behind the v3 schema."""
return WD14TaggerLoader().load_model(wd14_model=wd14_model)
+9
View File
@@ -0,0 +1,9 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Runtime adapters for SimpleSyrup external integrations."""
from __future__ import annotations
__all__: list[str] = []
+145
View File
@@ -0,0 +1,145 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Portions of this file are adapted from AUTOMATIC1111 stable-diffusion-webui
# and k-diffusion. See third_party/manifest.toml and third_party/NOTICE.md.
"""AUTOMATIC1111-derived sampler functions for SimpleSyrup sampling nodes."""
from __future__ import annotations
from collections.abc import Callable, Mapping
from importlib import import_module
from types import ModuleType
from typing import Any, Protocol, cast
import torch
class DenoiseModel(Protocol):
"""Represent the k-diffusion denoiser callable used by sampler functions."""
def __call__(
self,
x: torch.Tensor,
sigma: torch.Tensor,
**kwargs: object,
) -> torch.Tensor:
"""Denoise a latent tensor at the requested sigma."""
NoiseSampler = Callable[[torch.Tensor, torch.Tensor], torch.Tensor]
SamplerCallback = Callable[[dict[str, object]], None]
@torch.no_grad()
def sample_euler_ancestral_a1111(
model: DenoiseModel,
x: torch.Tensor,
sigmas: torch.Tensor,
extra_args: Mapping[str, object] | None = None,
callback: SamplerCallback | None = None,
disable: bool | None = None,
eta: float = 1.0,
s_noise: float = 1.0,
noise_sampler: NoiseSampler | None = None,
) -> torch.Tensor:
"""Run A1111/k-diffusion Euler ancestral math with ComfyUI seed plumbing."""
del disable
normalized_extra_args = {} if extra_args is None else dict(extra_args)
active_noise_sampler = noise_sampler or _default_noise_sampler(
x,
normalized_extra_args.get("seed"),
)
s_in = x.new_ones([x.shape[0]])
for index in range(len(sigmas) - 1):
denoised = model(x, sigmas[index] * s_in, **normalized_extra_args)
sigma_down, sigma_up = _get_ancestral_step(
sigmas[index],
sigmas[index + 1],
eta=eta,
)
if callback is not None:
callback(
{
"x": x,
"i": index,
"sigma": sigmas[index],
"sigma_hat": sigmas[index],
"denoised": denoised,
}
)
derivative = _to_d(x, sigmas[index], denoised)
dt = sigma_down - sigmas[index]
x = x + derivative * dt
if sigmas[index + 1] > 0:
x = (
x
+ active_noise_sampler(sigmas[index], sigmas[index + 1])
* s_noise
* sigma_up
)
return x
def _default_noise_sampler(x: torch.Tensor, seed: object) -> NoiseSampler:
"""Return ComfyUI's deterministic noise sampler for the active seed."""
try:
default_noise_sampler = cast(
Any,
_comfy_k_diffusion_sampling(),
).default_noise_sampler
except AttributeError as error:
raise ValueError("ComfyUI default_noise_sampler is unavailable.") from error
if not callable(default_noise_sampler):
raise ValueError("ComfyUI default_noise_sampler is unavailable.")
return cast(NoiseSampler, default_noise_sampler(x, seed=seed))
def _get_ancestral_step(
sigma_from: torch.Tensor,
sigma_to: torch.Tensor,
eta: float,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Calculate the ancestral down-step and noise scale with ComfyUI helpers."""
try:
get_ancestral_step = cast(
Any,
_comfy_k_diffusion_sampling(),
).get_ancestral_step
except AttributeError as error:
raise ValueError("ComfyUI get_ancestral_step is unavailable.") from error
if not callable(get_ancestral_step):
raise ValueError("ComfyUI get_ancestral_step is unavailable.")
return cast(
tuple[torch.Tensor, torch.Tensor],
get_ancestral_step(sigma_from, sigma_to, eta=eta),
)
def _to_d(
x: torch.Tensor,
sigma: torch.Tensor,
denoised: torch.Tensor,
) -> torch.Tensor:
"""Convert denoised output to an Euler derivative with ComfyUI helpers."""
try:
to_d = cast(Any, _comfy_k_diffusion_sampling()).to_d
except AttributeError as error:
raise ValueError("ComfyUI to_d is unavailable.") from error
if not callable(to_d):
raise ValueError("ComfyUI to_d is unavailable.")
return cast(torch.Tensor, to_d(x, sigma, denoised))
def _comfy_k_diffusion_sampling() -> ModuleType:
"""Import ComfyUI's k-diffusion sampling helpers lazily."""
return import_module("comfy.k_diffusion.sampling")
+191
View File
@@ -0,0 +1,191 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Load Anima diffusion, text encoder, and VAE models for ComfyUI."""
from __future__ import annotations
import importlib
from pathlib import Path
from types import ModuleType
from typing import Any, Protocol
import torch
from .auto_model_resolver import AutoModelResolution, AutoModelResolver
from .model_catalog import ANIMA_QWEN_TEXT_ENCODER, ANIMA_QWEN_VAE, AutoModelArtifact
from .model_downloads import ProgressReporter
from .vae_loader import VaeLoaderService, load_vae_path
AUTO_CHOICE = "auto"
DIFFUSION_WEIGHT_DTYPES = (
"default",
"fp8_e4m3fn",
"fp8_e4m3fn_fast",
"fp8_e5m2",
)
CLIP_DEVICES = ("default", "cpu")
DEFAULT_CLIP_TYPE = "stable_diffusion"
class AnimaLoaderService:
"""Resolve and load Anima diffusion, text encoder, and VAE models."""
def __init__(
self,
resolver: AutoModelResolverBoundary | None = None,
folder_paths_module: ModuleType | None = None,
) -> None:
"""Create a loader with injectable auto-resolution boundaries."""
self._folder_paths_module = folder_paths_module
self._resolver = resolver or AutoModelResolver(
folder_paths_module=folder_paths_module
)
self._vae_loader = VaeLoaderService(folder_paths_module)
def load_models(
self,
diffusion_model: str,
diffusion_weight_dtype: str,
text_encoder: str,
text_encoder_device: str,
vae: str,
progress: ProgressReporter | None = None,
) -> tuple[object, object, object]:
"""Return ComfyUI MODEL, CLIP, and VAE objects."""
return (
self._load_diffusion_model(diffusion_model, diffusion_weight_dtype),
self._load_clip(
text_encoder,
text_encoder_device,
progress,
),
self._load_vae(vae, progress),
)
def _load_diffusion_model(
self,
diffusion_model: str,
diffusion_weight_dtype: str,
) -> object:
"""Load a diffusion model using ComfyUI's diffusion model loader policy."""
if diffusion_weight_dtype not in DIFFUSION_WEIGHT_DTYPES:
valid = ", ".join(DIFFUSION_WEIGHT_DTYPES)
raise ValueError(f"diffusion_weight_dtype must be one of: {valid}.")
model_options: dict[str, object] = {}
if diffusion_weight_dtype == "fp8_e4m3fn":
model_options["dtype"] = torch.float8_e4m3fn
elif diffusion_weight_dtype == "fp8_e4m3fn_fast":
model_options["dtype"] = torch.float8_e4m3fn
model_options["fp8_optimizations"] = True
elif diffusion_weight_dtype == "fp8_e5m2":
model_options["dtype"] = torch.float8_e5m2
folder_paths = self._folder_paths()
unet_path = folder_paths.get_full_path_or_raise(
"diffusion_models",
diffusion_model,
)
comfy_sd = _comfy_sd()
return comfy_sd.load_diffusion_model(
unet_path,
model_options=model_options,
)
def _load_clip(
self,
text_encoder: str,
text_encoder_device: str,
progress: ProgressReporter | None,
) -> object:
"""Load a CLIP/text encoder using ComfyUI's CLIP loader policy."""
if text_encoder_device not in CLIP_DEVICES:
valid = ", ".join(CLIP_DEVICES)
raise ValueError(f"text_encoder_device must be one of: {valid}.")
if text_encoder == AUTO_CHOICE:
clip_path = self._resolver.resolve(ANIMA_QWEN_TEXT_ENCODER, progress).path
else:
clip_path = Path(
str(
self._folder_paths().get_full_path_or_raise(
"text_encoders",
text_encoder,
)
)
)
comfy_sd = _comfy_sd()
clip_type = getattr(
comfy_sd.CLIPType,
DEFAULT_CLIP_TYPE.upper(),
comfy_sd.CLIPType.STABLE_DIFFUSION,
)
model_options: dict[str, object] = {}
if text_encoder_device == "cpu":
model_options["load_device"] = model_options["offload_device"] = (
torch.device("cpu")
)
return comfy_sd.load_clip(
ckpt_paths=[str(clip_path)],
embedding_directory=self._folder_paths().get_folder_paths("embeddings"),
clip_type=clip_type,
model_options=model_options,
)
def _load_vae(
self,
vae: str,
progress: ProgressReporter | None,
) -> object:
"""Load a VAE using ComfyUI's VAE loader policy."""
if vae == AUTO_CHOICE:
vae_path = self._resolver.resolve(ANIMA_QWEN_VAE, progress).path
return load_vae_path(vae_path)
return self._vae_loader.load_vae(vae)
def _folder_paths(self) -> ModuleType:
"""Return the ComfyUI folder_paths module."""
if self._folder_paths_module is not None:
return self._folder_paths_module
module: Any = importlib.import_module("folder_paths")
if not isinstance(module, ModuleType):
raise TypeError("folder_paths import did not return a module.")
self._folder_paths_module = module
return module
class AutoModelResolverBoundary(Protocol):
"""Resolver interface required by the Anima loader service."""
def resolve(
self,
artifact: AutoModelArtifact,
progress: ProgressReporter | None = None,
) -> AutoModelResolution:
"""Resolve one automatic model artifact."""
def _folder_paths() -> ModuleType:
"""Import ComfyUI folder paths lazily."""
module: Any = importlib.import_module("folder_paths")
if not isinstance(module, ModuleType):
raise TypeError("folder_paths import did not return a module.")
return module
def _comfy_sd() -> Any:
"""Import ComfyUI's stable diffusion loading module lazily."""
return importlib.import_module("comfy.sd")
+215
View File
@@ -0,0 +1,215 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Persist resolved automatic model paths under the ComfyUI user directory."""
from __future__ import annotations
import importlib
import json
from dataclasses import dataclass
from pathlib import Path
from types import ModuleType
from typing import Any, Literal, cast
from ..shared.logging import get_logger
LOGGER = get_logger(__name__)
CACHE_VERSION = 1
CacheSource = Literal["cached", "found", "downloaded"]
class AutoModelCacheError(ValueError):
"""Raised when persisted automatic model cache data is invalid."""
@dataclass(frozen=True)
class AutoModelCacheEntry:
"""A remembered automatic model artifact resolution."""
folder_name: str
filename: str
path: Path
source: CacheSource
sha256: str
def to_payload(self) -> dict[str, str]:
"""Return this cache entry as a JSON-serializable payload."""
return {
"folder_name": self.folder_name,
"filename": self.filename,
"path": str(self.path),
"source": self.source,
"sha256": self.sha256,
}
@classmethod
def from_payload(
cls,
cache_id: str,
payload: object,
) -> AutoModelCacheEntry:
"""Create a cache entry from validated JSON-like data."""
if not isinstance(payload, dict):
raise AutoModelCacheError(
f"Auto model cache entry '{cache_id}' must be an object."
)
folder_name = _required_string(payload, cache_id, "folder_name")
filename = _required_string(payload, cache_id, "filename")
path = _required_string(payload, cache_id, "path")
source = _required_source(payload, cache_id)
sha256 = _required_string(payload, cache_id, "sha256")
return cls(
folder_name=folder_name,
filename=filename,
path=Path(path),
source=source,
sha256=sha256,
)
class AutoModelCache:
"""Load and save automatic model resolutions in ComfyUI user storage."""
def __init__(
self,
folder_paths_module: ModuleType | None = None,
) -> None:
"""Create a cache repository with injectable ComfyUI folder paths."""
self._folder_paths_module = folder_paths_module
def load(self) -> dict[str, AutoModelCacheEntry]:
"""Return every valid cache entry, or an empty cache when absent."""
path = self.cache_path()
if not path.is_file():
return {}
try:
payload: object = json.loads(path.read_text(encoding="utf-8"))
except json.JSONDecodeError as error:
raise AutoModelCacheError(
f"Auto model cache at '{path}' is not valid JSON."
) from error
return _entries_from_payload(payload)
def save(self, entries: dict[str, AutoModelCacheEntry]) -> None:
"""Persist all cache entries atomically."""
path = self.cache_path()
path.parent.mkdir(parents=True, exist_ok=True)
payload: dict[str, object] = {
"version": CACHE_VERSION,
"entries": {
cache_id: entry.to_payload()
for cache_id, entry in sorted(entries.items())
},
}
temporary_path = path.with_name(f"{path.name}.tmp")
temporary_path.write_text(
json.dumps(payload, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
temporary_path.replace(path)
def save_entry(self, cache_id: str, entry: AutoModelCacheEntry) -> None:
"""Upsert one cache entry while preserving unrelated entries."""
entries = self.load()
entries[cache_id] = entry
self.save(entries)
LOGGER.info(
"auto model cache entry saved",
extra={
"cache_id": cache_id,
"folder_name": entry.folder_name,
"path": str(entry.path),
"source": entry.source,
},
)
def cache_path(self) -> Path:
"""Return the JSON cache path under ComfyUI's user directory."""
return (
_user_directory(self._folder_paths()) / "simple_syrup" / "auto_models.json"
)
def _folder_paths(self) -> ModuleType:
"""Return the ComfyUI folder_paths module."""
if self._folder_paths_module is not None:
return self._folder_paths_module
module: Any = importlib.import_module("folder_paths")
if not isinstance(module, ModuleType):
raise TypeError("folder_paths import did not return a module.")
self._folder_paths_module = module
return module
def _entries_from_payload(payload: object) -> dict[str, AutoModelCacheEntry]:
"""Parse cache entries from a JSON-like object."""
if not isinstance(payload, dict):
raise AutoModelCacheError("Auto model cache root must be an object.")
version = payload.get("version")
if version != CACHE_VERSION:
raise AutoModelCacheError(f"Auto model cache version must be {CACHE_VERSION}.")
entries_payload = payload.get("entries")
if not isinstance(entries_payload, dict):
raise AutoModelCacheError("Auto model cache entries must be an object.")
entries: dict[str, AutoModelCacheEntry] = {}
for cache_id, entry_payload in entries_payload.items():
if not isinstance(cache_id, str):
raise AutoModelCacheError("Auto model cache entry keys must be strings.")
entries[cache_id] = AutoModelCacheEntry.from_payload(
cache_id,
entry_payload,
)
return entries
def _required_string(
payload: dict[Any, Any],
cache_id: str,
field_name: str,
) -> str:
"""Return one required string field from a cache entry payload."""
value = payload.get(field_name)
if not isinstance(value, str) or value == "":
raise AutoModelCacheError(
f"Auto model cache entry '{cache_id}' field '{field_name}' "
"must be a non-empty string."
)
return value
def _required_source(payload: dict[Any, Any], cache_id: str) -> CacheSource:
"""Return the validated source field for a cache entry payload."""
value = _required_string(payload, cache_id, "source")
if value not in ("cached", "found", "downloaded"):
raise AutoModelCacheError(
f"Auto model cache entry '{cache_id}' source is invalid."
)
return cast(CacheSource, value)
def _user_directory(folder_paths: ModuleType) -> Path:
"""Return ComfyUI's user directory using the host's current conventions."""
get_user_directory = getattr(folder_paths, "get_user_directory", None)
if callable(get_user_directory):
return Path(str(get_user_directory()))
user_directory_attribute = getattr(folder_paths, "user_directory", None)
if user_directory_attribute is not None:
return Path(str(user_directory_attribute))
models_dir: Any = folder_paths.models_dir
return Path(str(models_dir)).parent / "user"
+253
View File
@@ -0,0 +1,253 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Resolve trusted automatic model artifacts from cache, disk, or download."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path, PurePosixPath, PureWindowsPath
from types import ModuleType
from typing import Protocol
from ..shared.logging import get_logger
from .auto_model_cache import AutoModelCache, AutoModelCacheEntry, CacheSource
from .model_catalog import AutoModelArtifact
from .model_downloads import (
DownloadRequest,
DownloadResult,
ModelDownloader,
ProgressReporter,
)
from .model_folders import get_model_folder_paths
LOGGER = get_logger(__name__)
class AutoModelDownloadBoundary(Protocol):
"""Downloader interface required by automatic model resolution."""
def download(
self,
request: DownloadRequest,
progress: ProgressReporter | None = None,
) -> DownloadResult:
"""Download one trusted artifact and return its final path."""
@dataclass(frozen=True)
class AutoModelResolution:
"""Resolved automatic model path and provenance."""
path: Path
source: CacheSource
class AutoModelResolver:
"""Resolve known model artifacts while maintaining a self-healing cache."""
def __init__(
self,
cache: AutoModelCache | None = None,
downloader: AutoModelDownloadBoundary | None = None,
folder_paths_module: ModuleType | None = None,
) -> None:
"""Create a resolver with injectable persistence and download boundaries."""
self._folder_paths_module = folder_paths_module
self._cache = cache or AutoModelCache(folder_paths_module)
self._downloader = downloader or ModelDownloader()
def resolve(
self,
artifact: AutoModelArtifact,
progress: ProgressReporter | None = None,
) -> AutoModelResolution:
"""Return a valid local path, downloading and caching when necessary."""
entries = self._cache.load()
cached = entries.get(artifact.cache_id)
if cached is not None and self._cache_entry_is_valid(cached, artifact):
LOGGER.info(
"auto model cache hit",
extra={"cache_id": artifact.cache_id, "path": str(cached.path)},
)
return AutoModelResolution(cached.path, "cached")
if cached is not None:
LOGGER.warning(
"auto model cache entry is stale",
extra={"cache_id": artifact.cache_id, "path": str(cached.path)},
)
found = find_model_by_basename(
artifact.folder_name,
artifact.filename,
self._folder_paths_module,
)
if found is not None:
self._save_resolution(artifact, found, "found")
LOGGER.info(
"auto model found on disk",
extra={"cache_id": artifact.cache_id, "path": str(found)},
)
return AutoModelResolution(found, "found")
destination = canonical_auto_destination(artifact, self._folder_paths_module)
root = _containing_model_root(
artifact.folder_name,
destination,
self._folder_paths_module,
)
result = self._downloader.download(
DownloadRequest(
source_url=artifact.source_url,
destination_path=destination,
expected_folder=root,
description=artifact.description,
expected_sha256=artifact.sha256,
),
progress,
)
self._save_resolution(artifact, result.path, "downloaded")
LOGGER.info(
"auto model downloaded",
extra={"cache_id": artifact.cache_id, "path": str(result.path)},
)
return AutoModelResolution(result.path, "downloaded")
def _save_resolution(
self,
artifact: AutoModelArtifact,
path: Path,
source: CacheSource,
) -> None:
"""Persist one successful automatic model resolution."""
self._cache.save_entry(
artifact.cache_id,
AutoModelCacheEntry(
folder_name=artifact.folder_name,
filename=artifact.filename,
path=path,
source=source,
sha256=artifact.sha256,
),
)
def _cache_entry_is_valid(
self,
entry: AutoModelCacheEntry,
artifact: AutoModelArtifact,
) -> bool:
"""Return whether a remembered entry still resolves to the artifact."""
if entry.folder_name != artifact.folder_name:
return False
if entry.filename != artifact.filename:
return False
if entry.sha256 != artifact.sha256:
return False
if entry.path.name != artifact.filename:
return False
if not entry.path.is_file():
return False
try:
_containing_model_root(
artifact.folder_name,
entry.path,
self._folder_paths_module,
)
except ValueError:
return False
return True
def find_model_by_basename(
folder_name: str,
basename: str,
folder_paths_module: ModuleType | None = None,
) -> Path | None:
"""Return the first matching model file under registered folder paths."""
_validate_basename(basename)
for root in get_model_folder_paths(folder_name, folder_paths_module):
if not root.is_dir():
continue
matches = sorted(path for path in root.rglob(basename) if path.is_file())
for match in matches:
if match.name == basename and _path_is_under(match, root):
return match
return None
def canonical_auto_destination(
artifact: AutoModelArtifact,
folder_paths_module: ModuleType | None = None,
) -> Path:
"""Return the canonical download path under the first registered model folder."""
_validate_basename(artifact.filename)
subfolder = _safe_relative_path(artifact.canonical_subfolder)
root = get_model_folder_paths(artifact.folder_name, folder_paths_module)[0]
return root / subfolder / artifact.filename
def relative_model_name(
folder_name: str,
path: Path,
folder_paths_module: ModuleType | None = None,
) -> str:
"""Return the ComfyUI-relative filename for a resolved model path."""
root = _containing_model_root(folder_name, path, folder_paths_module)
return str(path.resolve().relative_to(root.resolve()))
def _containing_model_root(
folder_name: str,
path: Path,
folder_paths_module: ModuleType | None = None,
) -> Path:
"""Return the registered model root containing a path or raise."""
resolved_path = path.resolve()
for root in get_model_folder_paths(folder_name, folder_paths_module):
resolved_root = root.resolve()
try:
resolved_path.relative_to(resolved_root)
except ValueError:
continue
return root
raise ValueError(
f"Model path '{path}' is outside registered '{folder_name}' folders."
)
def _path_is_under(path: Path, root: Path) -> bool:
"""Return whether a path resolves below a root."""
try:
path.resolve().relative_to(root.resolve())
except ValueError:
return False
return True
def _safe_relative_path(value: str) -> Path:
"""Return a safe relative path from trusted catalog metadata."""
path = Path(value)
if path.is_absolute() or ".." in path.parts:
raise ValueError(f"Catalog relative path '{value}' is not safe.")
return path
def _validate_basename(value: str) -> None:
"""Reject unsafe or non-basename model filenames."""
paths = (Path(value), PurePosixPath(value), PureWindowsPath(value))
if any(
path.is_absolute() or path.name != value or ".." in path.parts for path in paths
):
raise ValueError(f"Model basename '{value}' is not safe.")
+136
View File
@@ -0,0 +1,136 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Resolve the BERT text encoder used by GroundingDINO."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from types import ModuleType
from .model_catalog import BERT_ENTRY
from .model_downloads import (
DownloadRequest,
ModelDownloader,
ProgressReporter,
)
from .model_folders import get_primary_model_folder
@dataclass(frozen=True)
class BertResolution:
"""Resolved local BERT directory and how it was obtained."""
path: Path
source: str
downloaded: bool
class BertResolver:
"""Find or download a Hugging Face-style BERT directory."""
def __init__(
self,
downloader: ModelDownloader | None = None,
folder_paths_module: ModuleType | None = None,
) -> None:
"""Create a resolver with injectable runtime dependencies."""
self._downloader = downloader or ModelDownloader()
self._folder_paths_module = folder_paths_module
def resolve(
self,
auto_download: bool,
progress: ProgressReporter | None = None,
) -> BertResolution:
"""Return a usable BERT directory or raise an actionable error."""
layerstyle_path = self._models_dir() / "bert-base-uncased"
if is_valid_bert_directory(layerstyle_path):
return BertResolution(
path=layerstyle_path,
source="models/bert-base-uncased",
downloaded=False,
)
text_encoder_path = self._text_encoder_bert_path()
if is_valid_bert_directory(text_encoder_path):
return BertResolution(
path=text_encoder_path,
source="models/text_encoders/bert",
downloaded=False,
)
if not auto_download:
raise FileNotFoundError(
"BERT text encoder was not found. Checked: "
f"{layerstyle_path}; {text_encoder_path}. "
"Enable auto_download or install a Hugging Face "
"bert-base-uncased snapshot."
)
self._download_bert(text_encoder_path, progress)
if not is_valid_bert_directory(text_encoder_path):
raise FileNotFoundError(
f"Downloaded BERT files in '{text_encoder_path}' are incomplete."
)
return BertResolution(
path=text_encoder_path,
source="downloaded: google-bert/bert-base-uncased",
downloaded=True,
)
def _download_bert(
self,
target_directory: Path,
progress: ProgressReporter | None,
) -> None:
"""Download the known BERT artifact set into the target directory."""
for artifact in BERT_ENTRY.artifacts:
destination = target_directory / artifact.filename
self._downloader.download(
DownloadRequest(
source_url=artifact.source_url,
destination_path=destination,
expected_folder=target_directory,
description=artifact.description,
),
progress,
)
def _text_encoder_bert_path(self) -> Path:
"""Return SimpleSyrup's BERT path under ComfyUI text encoders."""
return (
get_primary_model_folder("text_encoders", self._folder_paths_module)
/ "bert"
)
def _models_dir(self) -> Path:
"""Return ComfyUI's models directory."""
import importlib
folder_paths = self._folder_paths_module or importlib.import_module(
"folder_paths"
)
return Path(str(folder_paths.models_dir))
def is_valid_bert_directory(path: Path) -> bool:
"""Return whether a directory has enough files to load BERT locally."""
if not path.is_dir():
return False
has_config = (path / "config.json").is_file()
has_tokenizer = (path / "tokenizer.json").is_file() or (
path / "vocab.txt"
).is_file()
has_weights = (path / "model.safetensors").is_file() or (
path / "pytorch_model.bin"
).is_file()
return has_config and has_tokenizer and has_weights
+123
View File
@@ -0,0 +1,123 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Load checkpoints with optional external VAE replacement."""
from __future__ import annotations
import importlib
from collections.abc import Iterable
from types import ModuleType
from typing import Any, Protocol, cast, runtime_checkable
from .vae_loader import VaeLoaderService
USE_CHECKPOINT_VAE_CHOICE = "Use Checkpoint VAE"
CLIP_SKIP_DEFAULT = False
CLIP_SKIP_LAYER = -2
class CheckpointLoaderService:
"""Load a checkpoint and optionally adjust its CLIP and VAE outputs."""
def __init__(
self,
folder_paths_module: ModuleType | None = None,
vae_loader: VaeLoaderBoundary | None = None,
) -> None:
"""Create a checkpoint loader with injectable runtime boundaries."""
self._folder_paths_module = folder_paths_module
self._vae_loader = vae_loader or VaeLoaderService(folder_paths_module)
def load_checkpoint(
self,
ckpt_name: str,
vae_name: str,
clip_skip: bool = CLIP_SKIP_DEFAULT,
) -> tuple[object, object, object]:
"""Return MODEL, CLIP, and selected VAE objects."""
_validate_clip_skip(clip_skip)
folder_paths = self._folder_paths()
ckpt_path = folder_paths.get_full_path_or_raise("checkpoints", ckpt_name)
comfy_sd = _comfy_sd()
loaded = tuple(
cast(
Iterable[object],
comfy_sd.load_checkpoint_guess_config(
ckpt_path,
output_vae=True,
output_clip=True,
embedding_directory=folder_paths.get_folder_paths("embeddings"),
),
)
)
model = loaded[0]
clip = loaded[1]
checkpoint_vae = loaded[2]
selected_clip = _selected_clip(clip, clip_skip)
if vae_name == USE_CHECKPOINT_VAE_CHOICE:
return model, selected_clip, checkpoint_vae
return model, selected_clip, self._vae_loader.load_vae(vae_name)
def _folder_paths(self) -> ModuleType:
"""Return the ComfyUI folder_paths module."""
if self._folder_paths_module is not None:
return self._folder_paths_module
module: Any = importlib.import_module("folder_paths")
if not isinstance(module, ModuleType):
raise TypeError("folder_paths import did not return a module.")
self._folder_paths_module = module
return module
class VaeLoaderBoundary(Protocol):
"""VAE loader interface required by the checkpoint loader."""
def load_vae(self, vae_name: str) -> object:
"""Load the named external VAE."""
@runtime_checkable
class ClipLayerBoundary(Protocol):
"""CLIP interface required to apply the ComfyUI clip-skip layer."""
def clone(self) -> ClipLayerBoundary:
"""Return an independent CLIP object."""
def clip_layer(self, layer_idx: int) -> None:
"""Set the CLIP layer index used during prompt encoding."""
def _validate_clip_skip(clip_skip: object) -> None:
"""Reject non-boolean clip-skip selections from runtime callers."""
if not isinstance(clip_skip, bool):
raise TypeError("clip_skip must be a boolean.")
def _selected_clip(clip: object, clip_skip: bool) -> object:
"""Return the loaded CLIP or a cloned CLIP with clip skip applied."""
if not clip_skip:
return clip
if not isinstance(clip, ClipLayerBoundary):
raise TypeError(
"clip_skip requires a CLIP object with clone() and clip_layer()."
)
selected_clip = clip.clone()
selected_clip.clip_layer(CLIP_SKIP_LAYER)
return selected_clip
def _comfy_sd() -> Any:
"""Import ComfyUI's stable diffusion loading module lazily."""
return importlib.import_module("comfy.sd")
@@ -0,0 +1,209 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Trace ComfyUI prompt links back to unmodified VAE decode sources."""
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from ..domain.graph_provenance import (
BrokenProvenance,
GraphLink,
PassthroughRule,
VaeDecodeProvenance,
)
MAX_PROVENANCE_HOPS = 128
PASSTHROUGH_ATTRIBUTE = "GRAPH_PASSTHROUGH_OUTPUTS"
PromptNode = Mapping[str, Any]
PromptGraph = Mapping[str, Any]
NodeRegistry = Mapping[str, type[object]]
ProvenanceTrace = VaeDecodeProvenance | BrokenProvenance
def trace_vae_decode_provenance(
prompt: PromptGraph,
start_link: object,
node_registry: NodeRegistry,
*,
max_hops: int = MAX_PROVENANCE_HOPS,
) -> ProvenanceTrace:
"""Trace an image link through exact pass-through nodes to `VAEDecode`."""
current = parse_graph_link(start_link)
if current is None:
return BrokenProvenance("image input is not a graph link")
visited: set[GraphLink] = set()
for _ in range(max_hops):
if current in visited:
return BrokenProvenance(
"provenance trace contains a cycle",
node_id=current[0],
)
visited.add(current)
node_id, output_slot = current
node = _prompt_node(prompt, node_id)
if node is None:
return BrokenProvenance("source node is missing", node_id=node_id)
class_type = _class_type(node)
if class_type is None:
return BrokenProvenance(
"source node class_type is missing",
node_id=node_id,
)
inputs = _node_inputs(node)
if inputs is None:
return BrokenProvenance(
"source node inputs are missing",
node_id=node_id,
class_type=class_type,
)
if class_type == "VAEDecode":
return _trace_vae_decode(node_id, output_slot, inputs)
class_def = node_registry.get(class_type)
if class_def is None:
return BrokenProvenance(
"source node class is not registered",
node_id=node_id,
class_type=class_type,
)
passthrough = _resolve_passthrough_rule(class_def, output_slot)
if isinstance(passthrough, BrokenProvenance):
return BrokenProvenance(
passthrough.reason,
node_id=node_id,
class_type=class_type,
)
if passthrough is None:
return BrokenProvenance(
"source node does not declare exact pass-through provenance",
node_id=node_id,
class_type=class_type,
)
next_link = parse_graph_link(inputs.get(passthrough.input_name))
if next_link is None:
return BrokenProvenance(
"pass-through input is not a graph link",
node_id=node_id,
class_type=class_type,
)
current = next_link
node_id, _ = current
return BrokenProvenance("provenance trace exceeded the hop limit", node_id=node_id)
def parse_graph_link(value: object) -> GraphLink | None:
"""Convert a Comfy graph link value to a typed link tuple."""
if not isinstance(value, (list, tuple)) or len(value) != 2:
return None
node_id = value[0]
output_slot = value[1]
if not isinstance(node_id, str) or not node_id:
return None
if isinstance(output_slot, bool):
return None
if isinstance(output_slot, int):
return (node_id, output_slot)
if isinstance(output_slot, float) and output_slot.is_integer():
return (node_id, int(output_slot))
return None
def links_match(left: object, right: object) -> bool:
"""Return whether two raw Comfy values refer to the same graph output."""
left_link = parse_graph_link(left)
right_link = parse_graph_link(right)
return left_link is not None and left_link == right_link
def _trace_vae_decode(
node_id: str,
output_slot: int,
inputs: Mapping[str, Any],
) -> ProvenanceTrace:
"""Resolve the latent and VAE links from a `VAEDecode` prompt node."""
image_output = (node_id, output_slot)
if output_slot != 0:
return BrokenProvenance(
"VAEDecode output is not the image output",
node_id=node_id,
class_type="VAEDecode",
)
samples_link = parse_graph_link(inputs.get("samples"))
if samples_link is None:
return BrokenProvenance(
"VAEDecode samples input is not a graph link",
node_id=node_id,
class_type="VAEDecode",
)
return VaeDecodeProvenance(
decode_node_id=node_id,
image_output=image_output,
samples_link=samples_link,
vae_link=parse_graph_link(inputs.get("vae")),
)
def _resolve_passthrough_rule(
class_def: type[object],
output_slot: int,
) -> PassthroughRule | BrokenProvenance | None:
"""Return the exact pass-through rule declared by a node class."""
raw_rules = getattr(class_def, PASSTHROUGH_ATTRIBUTE, None)
if raw_rules is None:
return None
if not isinstance(raw_rules, Mapping):
return BrokenProvenance("pass-through metadata is malformed")
if output_slot not in raw_rules:
return None
input_name = raw_rules[output_slot]
if not isinstance(input_name, str) or not input_name.strip():
return BrokenProvenance("pass-through metadata is malformed")
return PassthroughRule(input_name=input_name.strip())
def _prompt_node(prompt: PromptGraph, node_id: str) -> PromptNode | None:
"""Return a prompt node mapping when the prompt contains one."""
node = prompt.get(node_id)
if not isinstance(node, Mapping):
return None
return node
def _class_type(node: PromptNode) -> str | None:
"""Return a prompt node class type when it is valid."""
class_type = node.get("class_type")
if not isinstance(class_type, str) or not class_type:
return None
return class_type
def _node_inputs(node: PromptNode) -> Mapping[str, Any] | None:
"""Return a prompt node input mapping when it is valid."""
inputs = node.get("inputs")
if not isinstance(inputs, Mapping):
return None
return inputs
@@ -0,0 +1,31 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Runtime adapters for ComfyUI conditioning encoding."""
from __future__ import annotations
from importlib import import_module
from typing import Any, cast
from ..domain.conditioning_batch import ConditioningBatch
class ComfyConditioningEncoder:
"""Encode prompt chunks with ComfyUI's normal CLIP text encoder."""
def encode(self, clip: Any, text: str) -> Any:
"""Encode a single prompt chunk into a normal Comfy conditioning."""
nodes = import_module("nodes")
return cast(Any, nodes.CLIPTextEncode().encode(clip, text)[0])
def encode_batch(
self,
clip: Any,
chunks: tuple[str, ...],
) -> ConditioningBatch:
"""Encode ordered prompt chunks into a conditioning batch."""
return ConditioningBatch(tuple(self.encode(clip, chunk) for chunk in chunks))
+485
View File
@@ -0,0 +1,485 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Full-context preview composition for detailer sampling callbacks."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from importlib import import_module
from typing import Any, Protocol, TypeAlias, cast
import torch
from PIL import Image, ImageDraw
from ..domain.segs import CropRegion
DETAIL_PREVIEW_WASH_OPACITY = 0.55
DETAIL_PREVIEW_OUTLINE_RGB = (255, 0, 0)
DETAIL_PREVIEW_OUTLINE_WIDTH = 1
CropBox: TypeAlias = tuple[int, int, int, int]
PreviewBytes: TypeAlias = tuple[str, Image.Image, int]
DetailPreviewCallback: TypeAlias = Callable[
[int, torch.Tensor, torch.Tensor, int], None
]
class LatentPreviewer(Protocol):
"""Decode latent tensors into preview images."""
def decode_latent_to_preview(self, x0: torch.Tensor) -> Image.Image:
"""Decode a denoised latent tensor to a PIL preview image."""
class ProgressBar(Protocol):
"""Progress sink compatible with ComfyUI's progress bar."""
def update_absolute(
self,
value: int,
total: int,
preview: PreviewBytes | None = None,
) -> None:
"""Update absolute progress with optional preview image bytes."""
class ProgressBarFactory(Protocol):
"""Construct a ComfyUI progress bar for a sampling pass."""
def __call__(self, steps: int) -> ProgressBar:
"""Return a progress bar for the provided step count."""
class LatentPreviewModule(Protocol):
"""ComfyUI latent preview module surface used by detail previews."""
MAX_PREVIEW_RESOLUTION: int
def get_previewer(
self,
device: object,
latent_format: object,
) -> LatentPreviewer | None:
"""Return a latent previewer when ComfyUI previews are enabled."""
class ComfyUtilsModule(Protocol):
"""ComfyUI utility module surface used by detail previews."""
ProgressBar: ProgressBarFactory
class InnerModelPreviewSource(Protocol):
"""Inner ComfyUI model fields needed for previewer lookup."""
latent_format: object
class ModelPreviewSource(Protocol):
"""ComfyUI model fields needed for previewer lookup."""
load_device: object
model: InnerModelPreviewSource
@dataclass(frozen=True)
class DetailPreviewContext:
"""Describe preview composition for one active detailer work area."""
image: torch.Tensor
work_region: CropRegion
work_mask: torch.Tensor
sampled_region: CropRegion | None = None
@dataclass(frozen=True)
class DetailPreviewGeometry:
"""Map source work coordinates into preview image coordinates.
`crop_box` uses PIL's exclusive right/bottom box convention for resizing and
pasting. `outline_box` uses inclusive right/bottom coordinates for
`ImageDraw.rectangle`.
"""
source_size: tuple[int, int]
preview_size: tuple[int, int]
crop_box: CropBox
outline_box: CropBox
@dataclass(frozen=True)
class DetailPreviewCompositor:
"""Compose active detail previews into a washed full-image context."""
geometry: DetailPreviewGeometry
washed_background: Image.Image
detail_alpha_mask: Image.Image
@classmethod
def from_context(
cls,
context: DetailPreviewContext,
max_preview_resolution: int,
) -> DetailPreviewCompositor:
"""Create a compositor with detailer background work precomputed."""
source_image = _image_tensor_to_rgb_pil(context.image)
sampled_region = context.sampled_region or context.work_region
geometry = build_detail_preview_geometry(
source_width=source_image.width,
source_height=source_image.height,
crop_region=sampled_region,
max_preview_resolution=max_preview_resolution,
outline_region=context.work_region,
)
preview_image = source_image.resize(
geometry.preview_size,
Image.Resampling.BILINEAR,
)
wash = Image.new("RGB", preview_image.size, "black")
washed_background = Image.blend(
preview_image,
wash,
DETAIL_PREVIEW_WASH_OPACITY,
)
left, top, right, bottom = geometry.crop_box
crop_size = (max(1, right - left), max(1, bottom - top))
detail_alpha_mask = _detail_alpha_mask(
context.work_mask,
source_size=(source_image.width, source_image.height),
preview_size=geometry.preview_size,
sampled_box=geometry.crop_box,
target_size=crop_size,
)
return cls(
geometry=geometry,
washed_background=washed_background,
detail_alpha_mask=detail_alpha_mask,
)
def compose(self, crop_preview: Image.Image) -> Image.Image:
"""Return one full-context preview with the active detail pasted in."""
left, top, right, bottom = self.geometry.crop_box
crop_width = max(1, right - left)
crop_height = max(1, bottom - top)
resized_crop = crop_preview.convert("RGB").resize(
(crop_width, crop_height),
Image.Resampling.BILINEAR,
)
output = self.washed_background.copy()
output.paste(resized_crop, (left, top), self.detail_alpha_mask)
ImageDraw.Draw(output).rectangle(
self.geometry.outline_box,
outline=DETAIL_PREVIEW_OUTLINE_RGB,
width=DETAIL_PREVIEW_OUTLINE_WIDTH,
)
return output
def fit_preview_size(width: int, height: int, max_size: int) -> tuple[int, int]:
"""Fit dimensions inside the preview limit while preserving aspect ratio."""
_validate_positive_int("width", width)
_validate_positive_int("height", height)
_validate_positive_int("max_size", max_size)
long_side = max(width, height)
if long_side <= max_size:
return width, height
scale = float(max_size) / float(long_side)
return max(1, int(round(width * scale))), max(1, int(round(height * scale)))
def build_detail_preview_geometry(
source_width: int,
source_height: int,
crop_region: CropRegion,
max_preview_resolution: int,
outline_region: CropRegion | None = None,
) -> DetailPreviewGeometry:
"""Build preview-space boxes for the active detail work."""
_validate_positive_int("source_width", source_width)
_validate_positive_int("source_height", source_height)
preview_size = fit_preview_size(
source_width,
source_height,
max_preview_resolution,
)
_validate_crop_region(crop_region, source_width, source_height)
resolved_outline_region = outline_region or crop_region
_validate_crop_region(resolved_outline_region, source_width, source_height)
preview_width, preview_height = preview_size
scale_x = float(preview_width) / float(source_width)
scale_y = float(preview_height) / float(source_height)
crop_box = _map_crop_box(crop_region, scale_x, scale_y, preview_size)
outline_box = _map_outline_box(
resolved_outline_region,
source_width,
source_height,
scale_x,
scale_y,
preview_size,
)
return DetailPreviewGeometry(
source_size=(source_width, source_height),
preview_size=preview_size,
crop_box=crop_box,
outline_box=outline_box,
)
def work_region_from_mask(mask: torch.Tensor) -> CropRegion:
"""Return the tight work region around a non-empty HW or single-item BHW mask."""
working = _normalize_mask_tensor(mask)
coordinates = torch.nonzero(working > 0, as_tuple=False)
if coordinates.numel() == 0:
raise ValueError("detail preview work mask must contain at least one pixel.")
top = int(coordinates[:, 0].min().item())
bottom = int(coordinates[:, 0].max().item()) + 1
left = int(coordinates[:, 1].min().item())
right = int(coordinates[:, 1].max().item()) + 1
return CropRegion(left, top, right, bottom)
def prepare_detail_preview_callback(
model: Any,
steps: int,
context: DetailPreviewContext,
) -> DetailPreviewCallback:
"""Create a ComfyUI sampler callback with full-image detail previews."""
latent_preview = _latent_preview()
comfy_utils = _comfy_utils()
progress = comfy_utils.ProgressBar(steps)
previewer = latent_preview.get_previewer(
_model_load_device(model),
_model_latent_format(model),
)
if previewer is None:
return _progress_only_callback(progress)
compositor = DetailPreviewCompositor.from_context(
context,
max_preview_resolution=latent_preview.MAX_PREVIEW_RESOLUTION,
)
def callback(
step: int,
x0: torch.Tensor,
x: torch.Tensor,
total_steps: int,
) -> None:
"""Send one composed full-context preview to ComfyUI."""
del x
crop_preview = previewer.decode_latent_to_preview(x0)
full_preview = compositor.compose(crop_preview)
progress.update_absolute(
step + 1,
total_steps,
("JPEG", full_preview, latent_preview.MAX_PREVIEW_RESOLUTION),
)
return callback
def _progress_only_callback(progress: ProgressBar) -> DetailPreviewCallback:
"""Create a callback that reports progress without preview images."""
def callback(
step: int,
x0: torch.Tensor,
x: torch.Tensor,
total_steps: int,
) -> None:
"""Update progress when ComfyUI has no active previewer."""
del x0, x
progress.update_absolute(step + 1, total_steps, None)
return callback
def _map_crop_box(
crop_region: CropRegion,
scale_x: float,
scale_y: float,
preview_size: tuple[int, int],
) -> CropBox:
"""Map a source crop to a PIL paste box with exclusive right/bottom."""
preview_width, preview_height = preview_size
left = _clamp(round(crop_region.left * scale_x), 0, preview_width - 1)
top = _clamp(round(crop_region.top * scale_y), 0, preview_height - 1)
right = _clamp(round(crop_region.right * scale_x), left + 1, preview_width)
bottom = _clamp(round(crop_region.bottom * scale_y), top + 1, preview_height)
return left, top, right, bottom
def _map_outline_box(
crop_region: CropRegion,
source_width: int,
source_height: int,
scale_x: float,
scale_y: float,
preview_size: tuple[int, int],
) -> CropBox:
"""Map the preferred outside crop outline to preview coordinates."""
preview_width, preview_height = preview_size
source_outline = _source_outline_box(crop_region, source_width, source_height)
left = _clamp(round(source_outline[0] * scale_x), 0, preview_width - 1)
top = _clamp(round(source_outline[1] * scale_y), 0, preview_height - 1)
right = _clamp(round(source_outline[2] * scale_x), 0, preview_width - 1)
bottom = _clamp(round(source_outline[3] * scale_y), 0, preview_height - 1)
if right < left:
right = left
if bottom < top:
bottom = top
return left, top, right, bottom
def _source_outline_box(
crop_region: CropRegion,
source_width: int,
source_height: int,
) -> CropBox:
"""Return source-space outline coordinates, preferring outside placement."""
return (
crop_region.left - 1 if crop_region.left > 0 else crop_region.left,
crop_region.top - 1 if crop_region.top > 0 else crop_region.top,
crop_region.right
if crop_region.right < source_width
else crop_region.right - 1,
crop_region.bottom
if crop_region.bottom < source_height
else crop_region.bottom - 1,
)
def _image_tensor_to_rgb_pil(image: torch.Tensor) -> Image.Image:
"""Convert a single-image BHWC tensor to an RGB PIL image."""
import numpy as np
if image.ndim != 4:
raise ValueError("detail preview image must be a BHWC tensor.")
if int(image.shape[0]) != 1:
raise ValueError("detail preview image must contain exactly one image.")
if int(image.shape[-1]) < 1:
raise ValueError("detail preview image must contain at least one channel.")
array = image[0].detach().cpu().float().clamp(0.0, 1.0).numpy()
if array.shape[-1] == 1:
array = np.repeat(array, 3, axis=-1)
elif array.shape[-1] >= 3:
array = array[..., :3]
else:
array = np.repeat(array[..., :1], 3, axis=-1)
return Image.fromarray((array * 255.0).round().astype(np.uint8))
def _mask_tensor_to_l_pil(mask: torch.Tensor) -> Image.Image:
"""Convert an HW or single-item BHW mask tensor to a grayscale alpha image."""
import numpy as np
working = _normalize_mask_tensor(mask).detach().cpu().clamp(0.0, 1.0)
return Image.fromarray((working.numpy() * 255.0).round().astype(np.uint8))
def _normalize_mask_tensor(mask: torch.Tensor) -> torch.Tensor:
"""Normalize an HW or single-item BHW mask tensor to HW float."""
working = mask.detach().float()
if working.ndim == 3 and int(working.shape[0]) == 1:
working = working[0]
if working.ndim != 2:
raise ValueError("detail preview work mask must be an HW tensor.")
return working
def _detail_alpha_mask(
mask: torch.Tensor,
*,
source_size: tuple[int, int],
preview_size: tuple[int, int],
sampled_box: CropBox,
target_size: tuple[int, int],
) -> Image.Image:
"""Return an alpha mask aligned to the sampled preview paste box."""
mask_image = _mask_tensor_to_l_pil(mask)
if mask_image.size == source_size:
preview_mask = mask_image.resize(preview_size, Image.Resampling.BILINEAR)
return preview_mask.crop(sampled_box).resize(
target_size,
Image.Resampling.BILINEAR,
)
return mask_image.resize(target_size, Image.Resampling.BILINEAR)
def _validate_crop_region(
crop_region: CropRegion,
source_width: int,
source_height: int,
) -> None:
"""Reject crop regions that cannot be mapped into the source image."""
if crop_region.left < 0 or crop_region.top < 0:
raise ValueError("crop_region left and top must be non-negative.")
if crop_region.right <= crop_region.left or crop_region.bottom <= crop_region.top:
raise ValueError("crop_region right/bottom must be greater than left/top.")
if crop_region.right > source_width or crop_region.bottom > source_height:
raise ValueError("crop_region must fit within the source image.")
def _validate_positive_int(name: str, value: int) -> None:
"""Reject non-positive integer values."""
if int(value) <= 0:
raise ValueError(f"{name} must be greater than 0.")
def _clamp(value: int, minimum: int, maximum: int) -> int:
"""Clamp an integer inside inclusive bounds."""
if minimum > maximum:
return minimum
return min(max(value, minimum), maximum)
def _model_load_device(model: Any) -> object:
"""Return the ComfyUI model load device for previewer lookup."""
preview_model = cast(ModelPreviewSource, model)
return preview_model.load_device
def _model_latent_format(model: Any) -> object:
"""Return the ComfyUI model latent format for previewer lookup."""
preview_model = cast(ModelPreviewSource, model)
return preview_model.model.latent_format
def _latent_preview() -> LatentPreviewModule:
"""Import ComfyUI latent preview support lazily."""
return cast(LatentPreviewModule, import_module("latent_preview"))
def _comfy_utils() -> ComfyUtilsModule:
"""Import ComfyUI utility support lazily."""
return cast(ComfyUtilsModule, import_module("comfy.utils"))
+170
View File
@@ -0,0 +1,170 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Runtime image resizing policy for detailer scaling."""
from __future__ import annotations
from collections.abc import Callable
from typing import Protocol
import torch
from .image_resamplers import NativeComfyResampler, validate_sampling
from .torchlanc_resampler import TorchLanczosResampler
SUPPORTED_DETAIL_UPSCALE_METHODS = (
"nearest-exact",
"bilinear",
"area",
"bicubic",
"lanczos",
)
TORCHLANC_SINC_WINDOW = 3
TORCHLANC_PRECISION = "fp32"
class DetailNativeResampler(Protocol):
"""Resize BCHW tensors with a selected non-Lanczos method."""
def resize(
self,
samples: torch.Tensor,
width: int,
height: int,
sampling: str,
) -> torch.Tensor:
"""Return resized BCHW samples."""
class DetailLanczosResampler(Protocol):
"""Resize BCHW tensors with TorchLanc Lanczos."""
def resize(
self,
samples: torch.Tensor,
width: int,
height: int,
sinc_window: int,
precision: str,
) -> torch.Tensor:
"""Return resized BCHW samples."""
NativeResamplerFactory = Callable[[], DetailNativeResampler]
LanczosResamplerFactory = Callable[[], DetailLanczosResampler]
class DetailImageResizer:
"""Resize detailer image tensors with the project detailer policy."""
def __init__(
self,
native_resampler_factory: NativeResamplerFactory | None = None,
lanczos_resampler_factory: LanczosResamplerFactory | None = None,
) -> None:
"""Create the resizer with injectable runtime adapters."""
self._native_resampler_factory = native_resampler_factory
self._lanczos_resampler_factory = lanczos_resampler_factory
def resize_up(
self,
image: torch.Tensor,
height: int,
width: int,
method: str,
) -> torch.Tensor:
"""Resize a BHWC image upward using the requested detailer method."""
self.validate_method(method)
return (
self._resize_lanczos(image, height, width)
if method == "lanczos"
else (self._resize_native(image, height, width, method))
)
def resize_down_lanczos(
self,
image: torch.Tensor,
height: int,
width: int,
) -> torch.Tensor:
"""Resize a BHWC image downward using fixed TorchLanc Lanczos."""
return self._resize_lanczos(image, height, width)
def validate_method(self, method: str) -> None:
"""Reject unsupported detailer upscale methods."""
validate_sampling(method)
if method not in SUPPORTED_DETAIL_UPSCALE_METHODS:
supported = ", ".join(SUPPORTED_DETAIL_UPSCALE_METHODS)
raise ValueError(
f"Unsupported detailer upscale method {method!r}. Use one of: "
f"{supported}."
)
def _resize_native(
self,
image: torch.Tensor,
height: int,
width: int,
method: str,
) -> torch.Tensor:
"""Resize a BHWC image through ComfyUI's GPU resize path."""
samples = _bhwc_to_bchw(image)
resized = self._native_resampler().resize(samples, width, height, method)
return _bchw_to_bhwc(resized)
def _resize_lanczos(
self,
image: torch.Tensor,
height: int,
width: int,
) -> torch.Tensor:
"""Resize a BHWC image through TorchLanc Lanczos."""
samples = _bhwc_to_bchw(image)
resized = self._lanczos_resampler().resize(
samples,
width,
height,
TORCHLANC_SINC_WINDOW,
TORCHLANC_PRECISION,
)
return _bchw_to_bhwc(resized)
def _native_resampler(self) -> DetailNativeResampler:
"""Return the injected or default GPU native resampler."""
if self._native_resampler_factory is not None:
return self._native_resampler_factory()
return NativeComfyResampler("gpu")
def _lanczos_resampler(self) -> DetailLanczosResampler:
"""Return the injected or default TorchLanc resampler."""
if self._lanczos_resampler_factory is not None:
return self._lanczos_resampler_factory()
return TorchLanczosResampler()
def _bhwc_to_bchw(image: torch.Tensor) -> torch.Tensor:
"""Validate and convert a BHWC image tensor to BCHW."""
if image.ndim != 4:
raise ValueError("detailer image resizing requires a BHWC tensor.")
if int(image.shape[0]) < 1 or int(image.shape[-1]) < 1:
raise ValueError("detailer image resizing requires non-empty image tensors.")
return image.float().clamp(0.0, 1.0).movedim(-1, 1)
def _bchw_to_bhwc(samples: torch.Tensor) -> torch.Tensor:
"""Validate and convert a BCHW image tensor to BHWC."""
if samples.ndim != 4:
raise ValueError("detailer resampler output must be a BCHW tensor.")
return samples.movedim(1, -1).clamp(0.0, 1.0)
+153
View File
@@ -0,0 +1,153 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI VAE and sampling adapters for scale-factor detailing."""
from __future__ import annotations
from importlib import import_module
from typing import Any, TypeAlias, cast
import torch
from . import sampling_samplers, sampling_schedulers
from .detail_previews import DetailPreviewContext, prepare_detail_preview_callback
Latent: TypeAlias = dict[str, Any]
class DetailSampler:
"""Adapt ComfyUI VAE and sampler APIs behind a testable boundary."""
def encode(self, vae: Any, pixels: torch.Tensor, tiled: bool) -> Latent:
"""Encode pixels into a ComfyUI latent dictionary."""
if tiled:
nodes = _nodes()
return cast(
Latent,
nodes.VAEEncodeTiled().encode(vae, pixels, 512, 64)[0],
)
return cast(Latent, _nodes().VAEEncode().encode(vae, pixels)[0])
def decode(self, vae: Any, latent: Latent, tiled: bool) -> torch.Tensor:
"""Decode a ComfyUI latent dictionary into pixels."""
if tiled:
nodes = _nodes()
return cast(
torch.Tensor,
nodes.VAEDecodeTiled().decode(vae, latent, 512, 64)[0],
)
return cast(torch.Tensor, _nodes().VAEDecode().decode(vae, latent)[0])
def sample(
self,
model: Any,
seed: int,
steps: int,
cfg: float,
sampler_name: str,
scheduler: str,
positive: Any,
negative: Any,
latent_image: Latent,
denoise: float,
preview_context: DetailPreviewContext | None = None,
) -> Latent:
"""Sample a latent with SimpleSyrup's sampler and scheduler helpers."""
sampler = sampling_samplers.resolve_sampler(sampler_name)
sigmas = sampling_schedulers.calculate_sigmas(
model=model,
scheduler_name=scheduler,
sampler_name=sampler_name,
steps=steps,
denoise=denoise,
).to(model.load_device)
latent_samples = cast(torch.Tensor, latent_image["samples"])
comfy_sample = _comfy_sample()
comfy_utils = _comfy_utils()
latent_samples = comfy_sample.fix_empty_latent_channels(
model,
latent_samples,
latent_image.get("downscale_ratio_spacial", None),
)
batch_inds = (
latent_image["batch_index"] if "batch_index" in latent_image else None
)
noise = comfy_sample.prepare_noise(latent_samples, seed, batch_inds)
noise_mask = latent_image.get("noise_mask", None)
if preview_context is None:
callback = _latent_preview().prepare_callback(model, steps)
else:
callback = prepare_detail_preview_callback(model, steps, preview_context)
samples = comfy_sample.sample_custom(
model,
noise,
cfg,
sampler,
sigmas,
positive,
negative,
latent_samples,
noise_mask=noise_mask,
callback=callback,
disable_pbar=not comfy_utils.PROGRESS_BAR_ENABLED,
seed=seed,
)
output = latent_image.copy()
output.pop("downscale_ratio_spacial", None)
output["samples"] = samples
return output
def apply_differential_diffusion(self, model: Any) -> Any:
"""Patch a model for feathered denoise masks when ComfyUI supports it."""
options = getattr(model, "model_options", {})
if (
isinstance(options, dict)
and options.get("denoise_mask_function") is not None
):
return model
module = import_module("comfy_extras.nodes_differential_diffusion")
node = module.DifferentialDiffusion
output = node.execute(model, 1.0)
if hasattr(output, "result"):
return output.result[0]
if isinstance(output, tuple):
return output[0]
return output[0]
def _nodes() -> Any:
"""Import ComfyUI core nodes lazily."""
return import_module("nodes")
def _comfy_sample() -> Any:
"""Import ComfyUI sampling helpers lazily."""
import comfy.sample
return comfy.sample
def _comfy_utils() -> Any:
"""Import ComfyUI utility state lazily."""
import comfy.utils
return comfy.utils
def _latent_preview() -> Any:
"""Import ComfyUI preview helpers lazily."""
return import_module("latent_preview")
+77
View File
@@ -0,0 +1,77 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Impact-style detector facades backed by native SimpleSyrup services."""
from __future__ import annotations
from dataclasses import dataclass
from ..domain.segs import to_impact_compatible_segs
from .ultralytics_loader import UltralyticsDetectorModel
@dataclass(frozen=True)
class BBoxDetectorFacade:
"""Expose a bbox detector-shaped object for existing workflows."""
detector_model: UltralyticsDetectorModel
def detect(
self,
image: object,
threshold: float,
dilation: int,
crop_factor: float,
drop_size: int = 1,
detailer_hook: object | None = None,
) -> object:
"""Detect rectangular SEGS through the native detection service."""
del detailer_hook
from ..services.segs_detection_service import SegsDetectionService
segs = SegsDetectionService().detect(
image=image,
detector_model=self.detector_model,
threshold=threshold,
dilation=dilation,
crop_factor=crop_factor,
drop_size=drop_size,
prefer_segmentation=False,
)
return to_impact_compatible_segs(segs)
@dataclass(frozen=True)
class SegmDetectorFacade:
"""Expose a segmentation detector-shaped object with bbox fallback."""
detector_model: UltralyticsDetectorModel
bbox_detector: BBoxDetectorFacade
def detect(
self,
image: object,
threshold: float,
dilation: int,
crop_factor: float,
drop_size: int = 1,
detailer_hook: object | None = None,
) -> object:
"""Detect segmentation SEGS when available, otherwise rectangular SEGS."""
del detailer_hook
from ..services.segs_detection_service import SegsDetectionService
segs = SegsDetectionService().detect(
image=image,
detector_model=self.detector_model,
threshold=threshold,
dilation=dilation,
crop_factor=crop_factor,
drop_size=drop_size,
prefer_segmentation=self.detector_model.supports_segmentation,
)
return to_impact_compatible_segs(segs)
@@ -0,0 +1,365 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Load GroundingDINO models and explicit text encoders."""
from __future__ import annotations
import importlib
from collections.abc import MutableMapping
from dataclasses import dataclass
from pathlib import Path
from types import ModuleType
import torch
from ..shared.logging import get_logger
from .bert_resolver import is_valid_bert_directory
from .loaded_models import LoadedGroundingDINOModel
from .model_catalog import BERT_ENTRY, ModelEntry, get_grounding_dino_entry
from .model_device_manager import TorchModelDeviceManager
from .model_downloads import DownloadRequest, ModelDownloader, ProgressReporter
from .model_folders import (
expected_model_file,
get_primary_model_folder,
register_required_model_folders,
resolve_model_file,
)
from .model_instance_cache import ModelInstanceCache
LOGGER = get_logger(__name__)
GROUNDING_DINO_RUNTIME_PACKAGE = "simple_syrup.third_party.groundingdino_runtime"
TEXT_ENCODER_LAYERSTYLE = "LayerStyle bert-base-uncased"
TEXT_ENCODER_COMFY = "text_encoders/bert"
TEXT_ENCODER_AUTO = "BERT base uncased (auto)"
TEXT_ENCODER_CHOICES = (
TEXT_ENCODER_LAYERSTYLE,
TEXT_ENCODER_COMFY,
TEXT_ENCODER_AUTO,
)
@dataclass(frozen=True)
class TextEncoderResolution:
"""Resolved text encoder directory and selection metadata."""
path: Path
source: str
downloaded: bool
@dataclass(frozen=True)
class GroundingDINOModelCacheKey:
"""Identify a loaded GroundingDINO model for process-level reuse."""
model_id: str
config_path: Path
checkpoint_path: Path
text_encoder_path: Path
_LOADED_GROUNDING_DINO_MODELS: dict[
GroundingDINOModelCacheKey, LoadedGroundingDINOModel
] = {}
class GroundingDINOLoaderService:
"""Resolve, download, and load GroundingDINO with an explicit text encoder."""
def __init__(
self,
downloader: ModelDownloader | None = None,
folder_paths_module: ModuleType | None = None,
device_manager: TorchModelDeviceManager | None = None,
cache: (
MutableMapping[GroundingDINOModelCacheKey, LoadedGroundingDINOModel] | None
) = None,
) -> None:
"""Create a GroundingDINO loader with injectable external boundaries."""
self._downloader = downloader or ModelDownloader()
self._folder_paths_module = folder_paths_module
self._device_manager = device_manager or TorchModelDeviceManager()
self._cache: ModelInstanceCache[
GroundingDINOModelCacheKey, LoadedGroundingDINOModel
] = ModelInstanceCache(
cache if cache is not None else _LOADED_GROUNDING_DINO_MODELS
)
def load_model(
self,
grounding_dino_model: str,
text_encoder: str,
auto_download: bool,
progress: ProgressReporter | None = None,
) -> LoadedGroundingDINOModel:
"""Load GroundingDINO and return a `GROUNDING_DINO_MODEL` object."""
register_required_model_folders(self._folder_paths_module)
entry = get_grounding_dino_entry(grounding_dino_model)
artifact_paths = self._resolve_artifacts(entry, auto_download, progress)
text_encoder_resolution = self.resolve_text_encoder(
text_encoder,
auto_download,
progress,
)
config_path = _artifact_path_with_suffix(artifact_paths, ".py")
checkpoint_path = _artifact_path_with_suffix(artifact_paths, ".pth")
key = GroundingDINOModelCacheKey(
model_id=entry.entry_id,
config_path=config_path.resolve(),
checkpoint_path=checkpoint_path.resolve(),
text_encoder_path=text_encoder_resolution.path.resolve(),
)
already_loaded = key in self._cache.entries
loaded = self._cache.get_or_load(
key,
lambda: self._load_uncached_model(
entry,
config_path,
checkpoint_path,
text_encoder_resolution,
),
)
if already_loaded:
LOGGER.info(
"GroundingDINO model loaded from process cache",
extra={
"operation": "grounding_dino_loader",
"model": entry.entry_id,
"config_path": str(config_path),
"checkpoint_path": str(checkpoint_path),
"text_encoder_path": str(text_encoder_resolution.path),
},
)
return loaded
def _load_uncached_model(
self,
entry: ModelEntry,
config_path: Path,
checkpoint_path: Path,
text_encoder_resolution: TextEncoderResolution,
) -> LoadedGroundingDINOModel:
"""Load and wrap GroundingDINO after resolution and cache lookup."""
model = self._load_grounding_dino_model(
entry,
config_path,
checkpoint_path,
text_encoder_resolution.path,
)
managed_model = self._device_manager.manage(
model,
model_id=entry.entry_id,
source=str(checkpoint_path),
)
loaded = LoadedGroundingDINOModel(
model=model,
text_encoder_path=text_encoder_resolution.path,
source=text_encoder_resolution.source,
model_id=entry.entry_id,
managed_model=managed_model,
)
LOGGER.info(
"GroundingDINO model loaded",
extra={
"operation": "grounding_dino_loader",
"model": entry.entry_id,
"config_path": str(config_path),
"checkpoint_path": str(checkpoint_path),
"text_encoder_path": str(text_encoder_resolution.path),
},
)
return loaded
def resolve_text_encoder(
self,
text_encoder: str,
auto_download: bool,
progress: ProgressReporter | None = None,
) -> TextEncoderResolution:
"""Resolve the selected BERT text encoder mode."""
layerstyle_path = self._models_dir() / "bert-base-uncased"
comfy_path = self._text_encoder_bert_path()
if text_encoder == TEXT_ENCODER_LAYERSTYLE:
return _require_bert_directory(layerstyle_path, text_encoder)
if text_encoder == TEXT_ENCODER_COMFY:
return _require_bert_directory(comfy_path, text_encoder)
if text_encoder != TEXT_ENCODER_AUTO:
valid = ", ".join(TEXT_ENCODER_CHOICES)
raise ValueError(f"text_encoder must be one of: {valid}.")
if is_valid_bert_directory(layerstyle_path):
return TextEncoderResolution(
path=layerstyle_path,
source="models/bert-base-uncased",
downloaded=False,
)
if is_valid_bert_directory(comfy_path):
return TextEncoderResolution(
path=comfy_path,
source="models/text_encoders/bert",
downloaded=False,
)
if not auto_download:
raise FileNotFoundError(
"BERT text encoder was not found. Checked: "
f"{layerstyle_path}; {comfy_path}. Enable auto_download on "
"GroundingDINO Model Loader or install a Hugging Face "
"bert-base-uncased snapshot."
)
self._download_bert(comfy_path, progress)
if not is_valid_bert_directory(comfy_path):
raise FileNotFoundError(
f"Downloaded BERT files in '{comfy_path}' are incomplete."
)
return TextEncoderResolution(
path=comfy_path,
source="downloaded: google-bert/bert-base-uncased",
downloaded=True,
)
def _resolve_artifacts(
self,
entry: ModelEntry,
auto_download: bool,
progress: ProgressReporter | None,
) -> dict[str, Path]:
"""Resolve or download GroundingDINO catalog artifacts."""
artifact_paths: dict[str, Path] = {}
for artifact in entry.artifacts:
existing = resolve_model_file(
artifact.folder_name,
artifact.filename,
self._folder_paths_module,
)
if existing is not None:
artifact_paths[artifact.artifact_id] = existing
continue
destination = expected_model_file(
artifact.folder_name,
artifact.filename,
self._folder_paths_module,
)
if not auto_download or not entry.auto_download_allowed:
raise FileNotFoundError(
f"GroundingDINO model '{entry.display_name}' is missing and "
f"auto_download is disabled. Expected: {destination}. "
"Enable auto_download on GroundingDINO Model Loader or install "
"the model artifact."
)
result = self._downloader.download(
DownloadRequest(
source_url=artifact.source_url,
destination_path=destination,
expected_folder=destination.parent,
description=artifact.description,
),
progress,
)
artifact_paths[artifact.artifact_id] = result.path
return artifact_paths
def _download_bert(
self,
target_directory: Path,
progress: ProgressReporter | None,
) -> None:
"""Download the known BERT artifact set into the selected directory."""
for artifact in BERT_ENTRY.artifacts:
destination = target_directory / artifact.filename
self._downloader.download(
DownloadRequest(
source_url=artifact.source_url,
destination_path=destination,
expected_folder=target_directory,
description=artifact.description,
),
progress,
)
def _load_grounding_dino_model(
self,
entry: ModelEntry,
config_path: Path,
checkpoint_path: Path,
text_encoder_path: Path,
) -> object:
"""Load GroundingDINO from a known config/checkpoint pair."""
importlib.invalidate_caches()
try:
slconfig_module = importlib.import_module(
f"{GROUNDING_DINO_RUNTIME_PACKAGE}.util.slconfig"
)
utils_module = importlib.import_module(
f"{GROUNDING_DINO_RUNTIME_PACKAGE}.util.utils"
)
models_module = importlib.import_module(
f"{GROUNDING_DINO_RUNTIME_PACKAGE}.models"
)
except ImportError as error:
raise RuntimeError(
"SimpleSyrup's GroundingDINO runtime is unavailable. Reinstall "
"SimpleSyrup or restore "
"simple_syrup.third_party.groundingdino_runtime. "
f"Import failed: {error}."
) from error
args = slconfig_module.SLConfig.fromfile(str(config_path))
if getattr(args, "text_encoder_type", "") == "bert-base-uncased":
args.text_encoder_type = str(text_encoder_path)
model = models_module.build_model(args)
checkpoint = torch.load(str(checkpoint_path), map_location="cpu")
clean_state_dict = utils_module.clean_state_dict
model.load_state_dict(clean_state_dict(checkpoint["model"]), strict=False)
model.eval()
model.model_name = entry.entry_id
return model
def _text_encoder_bert_path(self) -> Path:
"""Return SimpleSyrup's BERT path under ComfyUI text encoders."""
return (
get_primary_model_folder("text_encoders", self._folder_paths_module)
/ "bert"
)
def _models_dir(self) -> Path:
"""Return ComfyUI's models directory."""
import importlib as _importlib
folder_paths = self._folder_paths_module or _importlib.import_module(
"folder_paths"
)
return Path(str(folder_paths.models_dir))
def _require_bert_directory(path: Path, label: str) -> TextEncoderResolution:
"""Return an explicit BERT directory or fail with an actionable message."""
if not is_valid_bert_directory(path):
raise FileNotFoundError(
f"Text encoder '{label}' is incomplete. Expected files under {path}: "
"config.json, tokenizer.json or vocab.txt, and model.safetensors or "
"pytorch_model.bin."
)
return TextEncoderResolution(path=path, source=label, downloaded=False)
def _artifact_path_with_suffix(artifact_paths: dict[str, Path], suffix: str) -> Path:
"""Return the first artifact path with a suffix."""
for path in artifact_paths.values():
if path.suffix.lower() == suffix:
return path
raise FileNotFoundError(f"GroundingDINO model does not have a '{suffix}' artifact.")
+101
View File
@@ -0,0 +1,101 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI-native image resampling adapters."""
from __future__ import annotations
from collections.abc import Callable
from typing import Literal, Protocol, cast
import torch
SUPPORTED_SAMPLERS = ("nearest-exact", "bilinear", "area", "bicubic", "lanczos")
Processor = Literal["cpu", "gpu"]
CommonUpscale = Callable[[torch.Tensor, int, int, str, str], torch.Tensor]
DeviceProvider = Callable[[], torch.device | str]
class ImageResampler(Protocol):
"""Resize BCHW image tensors to the requested size."""
def resize(
self,
samples: torch.Tensor,
width: int,
height: int,
sampling: str,
) -> torch.Tensor:
"""Return resized BCHW samples."""
class NativeComfyResampler:
"""Resize images through ComfyUI's native Pillow/PyTorch path."""
def __init__(
self,
processor: Processor,
common_upscale: CommonUpscale | None = None,
device_provider: DeviceProvider | None = None,
) -> None:
"""Create a native resampler for the selected processor."""
if processor not in ("cpu", "gpu"):
raise ValueError(f"processor must be 'cpu' or 'gpu', got {processor!r}.")
self._processor = processor
self._common_upscale = common_upscale
self._device_provider = device_provider
def resize(
self,
samples: torch.Tensor,
width: int,
height: int,
sampling: str,
) -> torch.Tensor:
"""Resize BCHW samples with ComfyUI's native implementation."""
validate_sampling(sampling)
device = torch.device("cpu")
if self._processor == "gpu":
device = self._get_torch_device()
samples_on_device = samples.to(device=device)
return self._get_common_upscale()(
samples_on_device,
int(width),
int(height),
sampling,
"disabled",
)
def _get_common_upscale(self) -> CommonUpscale:
"""Return ComfyUI's common upscale function or an injected test double."""
if self._common_upscale is not None:
return self._common_upscale
import comfy.utils
return cast(CommonUpscale, comfy.utils.common_upscale)
def _get_torch_device(self) -> torch.device:
"""Return ComfyUI's configured torch execution device."""
if self._device_provider is not None:
return torch.device(self._device_provider())
from comfy import model_management
return torch.device(model_management.get_torch_device())
def validate_sampling(sampling: str) -> None:
"""Validate a ComfyUI image resize sampler name."""
if sampling not in SUPPORTED_SAMPLERS:
supported = ", ".join(SUPPORTED_SAMPLERS)
raise ValueError(
f"Unsupported sampling method {sampling!r}. Use one of: {supported}."
)
+99
View File
@@ -0,0 +1,99 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Loaded model containers used by SimpleSyrup masking nodes."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .model_device_manager import ManagedTorchModel
from .wd14_tagger import WD14Session, WD14TagRecord
@dataclass(frozen=True)
class LoadedSAMModel:
"""Loaded SAM-compatible model object with source metadata."""
model: object
source: str
model_id: str
managed_model: ManagedTorchModel | None = None
@dataclass(frozen=True)
class LoadedGroundingDINOModel:
"""Loaded GroundingDINO-compatible model object with text encoder metadata."""
model: object
text_encoder_path: Path
source: str
model_id: str
managed_model: ManagedTorchModel | None = None
@dataclass(frozen=True)
class LoadedViTMatteModel:
"""Loaded ViTMatte-compatible model object with source metadata."""
model: object
processor: object
source: str
model_id: str
model_path: Path
managed_model: ManagedTorchModel | None = None
@dataclass(frozen=True)
class LoadedWD14Tagger:
"""Loaded WD14 tagger runtime shared by compatible nodes."""
model_id: str
source: str
onnx_path: Path
csv_path: Path
providers: tuple[str, ...]
session: WD14Session
tags: tuple[WD14TagRecord, ...]
def unwrap_sam_model(model: object) -> object:
"""Return the underlying SAM object for SimpleSyrup loaded containers."""
if isinstance(model, LoadedSAMModel):
return model.model
return model
def unwrap_grounding_dino_model(model: object) -> object:
"""Return the underlying GroundingDINO object for SimpleSyrup containers."""
if isinstance(model, LoadedGroundingDINOModel):
return model.model
return model
def unwrap_vitmatte_model(model: object) -> LoadedViTMatteModel:
"""Return a validated SimpleSyrup ViTMatte model container."""
if isinstance(model, LoadedViTMatteModel):
return model
raise TypeError(
"VITMATTE_MODEL is not compatible with Prompt SEGS w/ SAM. Expected a "
"ViTMatte model loaded by ViTMatte Model Loader."
)
def unwrap_wd14_tagger(model: object) -> LoadedWD14Tagger:
"""Return a validated SimpleSyrup WD14 tagger container."""
if isinstance(model, LoadedWD14Tagger):
return model
raise TypeError(
"WD14_TAGGER is not compatible with this node. Expected a WD14 tagger "
"loaded by Load WD14 Tagger."
)
@@ -0,0 +1,330 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Portions of this file incorporate behavior derived from
# multidiffusion-upscaler-for-automatic1111. See third_party/manifest.toml and
# third_party/NOTICE.md.
"""ComfyUI runtime adapter for Mixture of Diffusers tiled sampling."""
from __future__ import annotations
from collections.abc import Sequence
from importlib import import_module
from types import ModuleType
from typing import Any, cast
import torch
from ..domain.tiled_diffusion import (
LatentTile,
TiledDiffusionPlan,
build_tiled_diffusion_plan,
gaussian_tile_weights,
)
from ..shared.logging import get_logger
from . import sampling_samplers, sampling_schedulers
from .detail_previews import DetailPreviewContext, prepare_detail_preview_callback
from .tiled_sampling import (
ApplyModel,
Latent,
ModelFunctionWrapper,
make_tiled_model_args,
new_spatial_weight_buffer,
reject_unsupported_conditioning,
spatial_tile_slicer,
validate_latent_samples,
validate_sampling_controls,
validate_tensor_shape,
)
LOGGER = get_logger(__name__)
SAMPLER_LABEL = "Mixture of Diffusers"
def sample_mixture_of_diffusers(
*,
model: Any,
seed: int,
steps: int,
cfg: float,
sampler_name: str,
scheduler: str,
positive: Any,
negative: Any,
latent_image: Latent,
denoise: float,
latent_tile_width: int,
latent_tile_height: int,
latent_tile_overlap: int,
latent_tile_batch_size: int,
preview_context: DetailPreviewContext | None = None,
) -> Latent:
"""Sample a latent with a cloned model patched for Mixture of Diffusers."""
validate_sampling_controls(
steps=steps,
denoise=denoise,
latent_tile_width=latent_tile_width,
latent_tile_height=latent_tile_height,
latent_tile_batch_size=latent_tile_batch_size,
)
reject_unsupported_conditioning(positive, sampler_label=SAMPLER_LABEL)
reject_unsupported_conditioning(negative, sampler_label=SAMPLER_LABEL)
sampler = sampling_samplers.resolve_sampler(sampler_name)
sigmas = sampling_schedulers.calculate_sigmas(
model=model,
scheduler_name=scheduler,
sampler_name=sampler_name,
steps=steps,
denoise=denoise,
).to(model.load_device)
latent_samples = validate_latent_samples(
latent_image,
sampler_label=SAMPLER_LABEL,
)
comfy_sample = _comfy_sample()
comfy_utils = _comfy_utils()
latent_samples = comfy_sample.fix_empty_latent_channels(
model,
latent_samples,
latent_image.get("downscale_ratio_spacial", None),
)
validate_tensor_shape(latent_samples, sampler_label=SAMPLER_LABEL)
latent_height = int(latent_samples.shape[-2])
latent_width = int(latent_samples.shape[-1])
sampling_model, plan = clone_model_with_mixture_of_diffusers(
model,
latent_width=latent_width,
latent_height=latent_height,
tile_width=latent_tile_width,
tile_height=latent_tile_height,
overlap=latent_tile_overlap,
tile_batch_size=latent_tile_batch_size,
)
batch_inds = latent_image["batch_index"] if "batch_index" in latent_image else None
noise = comfy_sample.prepare_noise(latent_samples, seed, batch_inds)
noise_mask = latent_image.get("noise_mask", None)
callback = _sampling_callback(sampling_model, steps, preview_context)
samples = comfy_sample.sample_custom(
sampling_model,
noise,
cfg,
sampler,
sigmas,
positive,
negative,
latent_samples,
noise_mask=noise_mask,
callback=callback,
disable_pbar=not comfy_utils.PROGRESS_BAR_ENABLED,
seed=seed,
)
LOGGER.info(
"KSampler Mixture of Diffusers pass completed",
extra={
"operation": "ksampler_mixture_of_diffusers",
"sampler": sampler_name,
"scheduler": scheduler,
"steps": steps,
"denoise": denoise,
"latent_width": latent_width,
"latent_height": latent_height,
"tile_width": plan.tile_width,
"tile_height": plan.tile_height,
"overlap": plan.overlap,
"tile_count": len(plan.tiles),
"requested_tile_batch_size": plan.requested_tile_batch_size,
"tile_batch_size": plan.tile_batch_size,
},
)
output = latent_image.copy()
output.pop("downscale_ratio_spacial", None)
output["samples"] = samples
return output
def clone_model_with_mixture_of_diffusers(
model: Any,
*,
latent_width: int,
latent_height: int,
tile_width: int,
tile_height: int,
overlap: int,
tile_batch_size: int,
) -> tuple[Any, TiledDiffusionPlan]:
"""Return a model clone patched with a pre-CFG Mixture wrapper."""
plan = build_tiled_diffusion_plan(
latent_width=latent_width,
latent_height=latent_height,
tile_width=tile_width,
tile_height=tile_height,
overlap=overlap,
tile_batch_size=tile_batch_size,
)
cloned_model = model.clone()
old_wrapper = cloned_model.model_options.get("model_function_wrapper")
if old_wrapper is not None and not callable(old_wrapper):
raise ValueError("Existing model_function_wrapper is not callable.")
wrapper = MixtureOfDiffusersModelWrapper(
plan=plan,
existing_wrapper=cast(ModelFunctionWrapper | None, old_wrapper),
)
cloned_model.set_model_unet_function_wrapper(wrapper)
return cloned_model, plan
class MixtureOfDiffusersModelWrapper:
"""Blend tiled model predictions before ComfyUI CFG combines them."""
def __init__(
self,
*,
plan: TiledDiffusionPlan,
existing_wrapper: ModelFunctionWrapper | None,
) -> None:
"""Create the model wrapper for one latent sampling shape."""
self._plan = plan
self._existing_wrapper = existing_wrapper
self._tile_weights_2d: torch.Tensor | None = None
def __call__(
self,
apply_model: ApplyModel,
args: dict[str, Any],
) -> torch.Tensor:
"""Run the original model on latent tiles and blend the predictions."""
x = args["input"]
if not isinstance(x, torch.Tensor):
raise ValueError("Mixture of Diffusers model input must be a tensor.")
validate_tensor_shape(x, sampler_label=SAMPLER_LABEL)
if x.shape[-2:] != (self._plan.latent_height, self._plan.latent_width):
return self._call_original(apply_model, args)
if len(self._plan.tiles) <= 1:
return self._call_original(apply_model, args)
timestep = args["timestep"]
if not isinstance(timestep, torch.Tensor):
raise ValueError("Mixture of Diffusers timestep must be a tensor.")
conditioning = args.get("c", {})
if not isinstance(conditioning, dict):
raise ValueError("Mixture of Diffusers conditioning must be a dict.")
if conditioning.get("control") is not None:
raise ValueError(
"Mixture of Diffusers does not support regional conditioning or "
"ControlNet in the first implementation."
)
output_buffer = torch.zeros_like(x)
weight_buffer = new_spatial_weight_buffer(x, self._plan)
input_batch_size = int(x.shape[0])
weights = self._weights_for(x)
for batch in self._plan.batches:
tiled_args = self._make_tiled_args(
args=args,
tiles=batch,
input_batch_size=input_batch_size,
)
tile_output = self._call_original(apply_model, tiled_args)
for index, tile in enumerate(batch):
tile_slice = spatial_tile_slicer(tile, x.ndim)
start = index * input_batch_size
end = start + input_batch_size
output_buffer[tile_slice] += tile_output[start:end] * weights.to(
dtype=tile_output.dtype
)
weight_buffer[tile_slice] += weights.to(dtype=weight_buffer.dtype)
return output_buffer / weight_buffer.to(dtype=output_buffer.dtype)
def _call_original(
self,
apply_model: ApplyModel,
args: dict[str, Any],
) -> torch.Tensor:
"""Call the preserved model wrapper or raw apply_model."""
if self._existing_wrapper is not None:
return self._existing_wrapper(apply_model, args)
conditioning = args.get("c", {})
if not isinstance(conditioning, dict):
raise ValueError("Mixture of Diffusers conditioning must be a dict.")
return apply_model(args["input"], args["timestep"], **conditioning)
def _make_tiled_args(
self,
*,
args: dict[str, Any],
tiles: Sequence[LatentTile],
input_batch_size: int,
) -> dict[str, Any]:
"""Create apply-model args for one tile batch."""
return make_tiled_model_args(
args=args,
tiles=tiles,
input_batch_size=input_batch_size,
latent_height=self._plan.latent_height,
latent_width=self._plan.latent_width,
)
def _weights_for(self, x: torch.Tensor) -> torch.Tensor:
"""Return cached Gaussian tile weights for the active device and dtype."""
if (
self._tile_weights_2d is None
or self._tile_weights_2d.device != x.device
or self._tile_weights_2d.dtype != x.dtype
):
self._tile_weights_2d = gaussian_tile_weights(
self._plan.tile_width,
self._plan.tile_height,
device=x.device,
dtype=x.dtype,
)
return self._tile_weights_2d.reshape(
(1,) * (x.ndim - 2) + (self._plan.tile_height, self._plan.tile_width)
)
def _comfy_sample() -> ModuleType:
"""Import ComfyUI sample helpers lazily."""
return import_module("comfy.sample")
def _sampling_callback(
model: Any,
steps: int,
preview_context: DetailPreviewContext | None,
) -> Any:
"""Return a generic or detailer-specific sampling preview callback."""
if preview_context is None:
return _latent_preview().prepare_callback(model, steps)
return prepare_detail_preview_callback(model, steps, preview_context)
def _comfy_utils() -> ModuleType:
"""Import ComfyUI utility state lazily."""
return import_module("comfy.utils")
def _latent_preview() -> ModuleType:
"""Import ComfyUI preview helpers lazily."""
return import_module("latent_preview")
+475
View File
@@ -0,0 +1,475 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Known model metadata for grounded SAM masking."""
from __future__ import annotations
from dataclasses import dataclass
from enum import StrEnum
class ModelFamily(StrEnum):
"""Catalog families used by grounded SAM model selection."""
SAM = "sam"
GROUNDING_DINO = "grounding_dino"
TEXT_ENCODER = "text_encoder"
VITMATTE = "vitmatte"
WD14_TAGGER = "wd14_tagger"
@dataclass(frozen=True)
class ModelArtifact:
"""A downloadable file required by a known model entry."""
artifact_id: str
filename: str
folder_name: str
source_url: str
description: str
@dataclass(frozen=True)
class AutoModelArtifact:
"""A trusted auto-resolved model artifact with a canonical destination."""
cache_id: str
filename: str
folder_name: str
canonical_subfolder: str
source_url: str
source_repo: str
description: str
sha256: str
@dataclass(frozen=True)
class ModelEntry:
"""A known model selection and its source metadata."""
entry_id: str
display_name: str
family: ModelFamily
model_type: str
artifacts: tuple[ModelArtifact, ...]
source_repo: str
auto_download_allowed: bool = True
license_note: str = ""
SAM_ENTRIES: tuple[ModelEntry, ...] = (
ModelEntry(
entry_id="sam_vit_h",
display_name="sam_vit_h (2.56GB)",
family=ModelFamily.SAM,
model_type="vit_h",
source_repo="facebookresearch/segment-anything",
artifacts=(
ModelArtifact(
artifact_id="sam_vit_h_checkpoint",
filename="sam_vit_h_4b8939.pth",
folder_name="sams",
source_url=(
"https://dl.fbaipublicfiles.com/segment_anything/"
"sam_vit_h_4b8939.pth"
),
description="SAM ViT-H checkpoint",
),
),
),
ModelEntry(
entry_id="sam_vit_l",
display_name="sam_vit_l (1.25GB)",
family=ModelFamily.SAM,
model_type="vit_l",
source_repo="facebookresearch/segment-anything",
artifacts=(
ModelArtifact(
artifact_id="sam_vit_l_checkpoint",
filename="sam_vit_l_0b3195.pth",
folder_name="sams",
source_url=(
"https://dl.fbaipublicfiles.com/segment_anything/"
"sam_vit_l_0b3195.pth"
),
description="SAM ViT-L checkpoint",
),
),
),
ModelEntry(
entry_id="sam_vit_b",
display_name="sam_vit_b (375MB)",
family=ModelFamily.SAM,
model_type="vit_b",
source_repo="facebookresearch/segment-anything",
artifacts=(
ModelArtifact(
artifact_id="sam_vit_b_checkpoint",
filename="sam_vit_b_01ec64.pth",
folder_name="sams",
source_url=(
"https://dl.fbaipublicfiles.com/segment_anything/"
"sam_vit_b_01ec64.pth"
),
description="SAM ViT-B checkpoint",
),
),
),
ModelEntry(
entry_id="sam_hq_vit_h",
display_name="sam_hq_vit_h (2.57GB)",
family=ModelFamily.SAM,
model_type="sam_hq_vit_h",
source_repo="lkeab/hq-sam",
artifacts=(
ModelArtifact(
artifact_id="sam_hq_vit_h_checkpoint",
filename="sam_hq_vit_h.pth",
folder_name="sams",
source_url=(
"https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_h.pth"
),
description="SAM-HQ ViT-H checkpoint",
),
),
),
ModelEntry(
entry_id="sam_hq_vit_l",
display_name="sam_hq_vit_l (1.25GB)",
family=ModelFamily.SAM,
model_type="sam_hq_vit_l",
source_repo="lkeab/hq-sam",
artifacts=(
ModelArtifact(
artifact_id="sam_hq_vit_l_checkpoint",
filename="sam_hq_vit_l.pth",
folder_name="sams",
source_url=(
"https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_l.pth"
),
description="SAM-HQ ViT-L checkpoint",
),
),
),
ModelEntry(
entry_id="sam_hq_vit_b",
display_name="sam_hq_vit_b (379MB)",
family=ModelFamily.SAM,
model_type="sam_hq_vit_b",
source_repo="lkeab/hq-sam",
artifacts=(
ModelArtifact(
artifact_id="sam_hq_vit_b_checkpoint",
filename="sam_hq_vit_b.pth",
folder_name="sams",
source_url=(
"https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_b.pth"
),
description="SAM-HQ ViT-B checkpoint",
),
),
),
ModelEntry(
entry_id="mobile_sam",
display_name="mobile_sam (39MB)",
family=ModelFamily.SAM,
model_type="mobile_sam",
source_repo="ChaoningZhang/MobileSAM",
artifacts=(
ModelArtifact(
artifact_id="mobile_sam_checkpoint",
filename="mobile_sam.pt",
folder_name="sams",
source_url=(
"https://github.com/ChaoningZhang/MobileSAM/raw/master/"
"weights/mobile_sam.pt"
),
description="MobileSAM checkpoint",
),
),
),
)
GROUNDING_DINO_ENTRIES: tuple[ModelEntry, ...] = (
ModelEntry(
entry_id="groundingdino_swint_ogc",
display_name="GroundingDINO_SwinT_OGC (694MB)",
family=ModelFamily.GROUNDING_DINO,
model_type="swin_t",
source_repo="ShilongLiu/GroundingDINO",
artifacts=(
ModelArtifact(
artifact_id="groundingdino_swint_ogc_config",
filename="GroundingDINO_SwinT_OGC.cfg.py",
folder_name="grounding-dino",
source_url=(
"https://huggingface.co/ShilongLiu/GroundingDINO/"
"resolve/main/GroundingDINO_SwinT_OGC.cfg.py"
),
description="GroundingDINO SwinT OGC config",
),
ModelArtifact(
artifact_id="groundingdino_swint_ogc_checkpoint",
filename="groundingdino_swint_ogc.pth",
folder_name="grounding-dino",
source_url=(
"https://huggingface.co/ShilongLiu/GroundingDINO/"
"resolve/main/groundingdino_swint_ogc.pth"
),
description="GroundingDINO SwinT OGC checkpoint",
),
),
),
ModelEntry(
entry_id="groundingdino_swinb",
display_name="GroundingDINO_SwinB (938MB)",
family=ModelFamily.GROUNDING_DINO,
model_type="swin_b",
source_repo="ShilongLiu/GroundingDINO",
artifacts=(
ModelArtifact(
artifact_id="groundingdino_swinb_config",
filename="GroundingDINO_SwinB.cfg.py",
folder_name="grounding-dino",
source_url=(
"https://huggingface.co/ShilongLiu/GroundingDINO/"
"resolve/main/GroundingDINO_SwinB.cfg.py"
),
description="GroundingDINO SwinB config",
),
ModelArtifact(
artifact_id="groundingdino_swinb_checkpoint",
filename="groundingdino_swinb_cogcoor.pth",
folder_name="grounding-dino",
source_url=(
"https://huggingface.co/ShilongLiu/GroundingDINO/"
"resolve/main/groundingdino_swinb_cogcoor.pth"
),
description="GroundingDINO SwinB checkpoint",
),
),
),
)
BERT_ENTRY = ModelEntry(
entry_id="bert_base_uncased",
display_name="BERT base uncased",
family=ModelFamily.TEXT_ENCODER,
model_type="bert",
source_repo="google-bert/bert-base-uncased",
artifacts=(
ModelArtifact(
artifact_id="bert_config",
filename="config.json",
folder_name="text_encoders",
source_url=(
"https://huggingface.co/google-bert/bert-base-uncased/"
"resolve/main/config.json"
),
description="BERT config",
),
ModelArtifact(
artifact_id="bert_tokenizer",
filename="tokenizer.json",
folder_name="text_encoders",
source_url=(
"https://huggingface.co/google-bert/bert-base-uncased/"
"resolve/main/tokenizer.json"
),
description="BERT tokenizer",
),
ModelArtifact(
artifact_id="bert_tokenizer_config",
filename="tokenizer_config.json",
folder_name="text_encoders",
source_url=(
"https://huggingface.co/google-bert/bert-base-uncased/"
"resolve/main/tokenizer_config.json"
),
description="BERT tokenizer config",
),
ModelArtifact(
artifact_id="bert_vocab",
filename="vocab.txt",
folder_name="text_encoders",
source_url=(
"https://huggingface.co/google-bert/bert-base-uncased/"
"resolve/main/vocab.txt"
),
description="BERT vocabulary",
),
ModelArtifact(
artifact_id="bert_weights",
filename="model.safetensors",
folder_name="text_encoders",
source_url=(
"https://huggingface.co/google-bert/bert-base-uncased/"
"resolve/main/model.safetensors"
),
description="BERT weights",
),
),
)
VITMATTE_ENTRIES: tuple[ModelEntry, ...] = (
ModelEntry(
entry_id="vitmatte-small-composition-1k",
display_name="vitmatte-small-composition-1k",
family=ModelFamily.VITMATTE,
model_type="vitmatte_small",
source_repo="hustvl/vitmatte-small-composition-1k",
artifacts=(),
),
ModelEntry(
entry_id="vitmatte-base-composition-1k",
display_name="vitmatte-base-composition-1k",
family=ModelFamily.VITMATTE,
model_type="vitmatte_base",
source_repo="hustvl/vitmatte-base-composition-1k",
artifacts=(),
),
)
DEFAULT_WD14_TAGGER_MODEL = "wd-eva02-large-tagger-v3"
def _wd14_tagger_entry(model_id: str) -> ModelEntry:
"""Build a WD14 catalog entry from the canonical SmilingWolf repository."""
repo = f"SmilingWolf/{model_id}"
return ModelEntry(
entry_id=model_id,
display_name=model_id,
family=ModelFamily.WD14_TAGGER,
model_type="wd14",
source_repo=repo,
artifacts=(
ModelArtifact(
artifact_id="onnx",
filename=f"{model_id}.onnx",
folder_name="wd14_tagger",
source_url=f"https://huggingface.co/{repo}/resolve/main/model.onnx",
description=f"{model_id} ONNX model",
),
ModelArtifact(
artifact_id="tags",
filename=f"{model_id}.csv",
folder_name="wd14_tagger",
source_url=(
f"https://huggingface.co/{repo}/resolve/main/selected_tags.csv"
),
description=f"{model_id} selected tags CSV",
),
),
)
WD14_TAGGER_ENTRIES: tuple[ModelEntry, ...] = (
_wd14_tagger_entry("wd-eva02-large-tagger-v3"),
_wd14_tagger_entry("wd-vit-tagger-v3"),
_wd14_tagger_entry("wd-swinv2-tagger-v3"),
_wd14_tagger_entry("wd-convnext-tagger-v3"),
_wd14_tagger_entry("wd-v1-4-moat-tagger-v2"),
_wd14_tagger_entry("wd-v1-4-convnextv2-tagger-v2"),
_wd14_tagger_entry("wd-v1-4-convnext-tagger-v2"),
_wd14_tagger_entry("wd-v1-4-convnext-tagger"),
_wd14_tagger_entry("wd-v1-4-vit-tagger-v2"),
_wd14_tagger_entry("wd-v1-4-swinv2-tagger-v2"),
_wd14_tagger_entry("wd-v1-4-vit-tagger"),
)
ANIMA_QWEN_TEXT_ENCODER = AutoModelArtifact(
cache_id="anima_qwen_text_encoder",
filename="qwen_3_06b_base.safetensors",
folder_name="text_encoders",
canonical_subfolder="qwen",
source_url=(
"https://huggingface.co/circlestone-labs/Anima/resolve/main/"
"split_files/text_encoders/qwen_3_06b_base.safetensors"
),
source_repo="circlestone-labs/Anima",
description="Anima Qwen3 0.6B text encoder",
sha256="cd2a512003e2f9f3cd3c32a9c3573f820bb28c940f73c57b1ddaa983d9223eba",
)
ANIMA_QWEN_VAE = AutoModelArtifact(
cache_id="anima_qwen_vae",
filename="qwen_image_vae.safetensors",
folder_name="vae",
canonical_subfolder="qwen",
source_url=(
"https://huggingface.co/circlestone-labs/Anima/resolve/main/"
"split_files/vae/qwen_image_vae.safetensors"
),
source_repo="circlestone-labs/Anima",
description="Anima Qwen Image VAE",
sha256="a70580f0213e67967ee9c95f05bb400e8fb08307e017a924bf3441223e023d1f",
)
ANIMA_AUTO_ARTIFACTS = (ANIMA_QWEN_TEXT_ENCODER, ANIMA_QWEN_VAE)
def sam_choices() -> list[str]:
"""Return deterministic SAM dropdown choices."""
return [entry.display_name for entry in SAM_ENTRIES]
def grounding_dino_choices() -> list[str]:
"""Return deterministic GroundingDINO dropdown choices."""
return [entry.display_name for entry in GROUNDING_DINO_ENTRIES]
def vitmatte_choices() -> list[str]:
"""Return deterministic ViTMatte dropdown choices."""
return [entry.display_name for entry in VITMATTE_ENTRIES]
def wd14_tagger_choices() -> list[str]:
"""Return deterministic WD14 tagger dropdown choices."""
return [entry.display_name for entry in WD14_TAGGER_ENTRIES]
def get_sam_entry(selection: str) -> ModelEntry:
"""Return the SAM catalog entry matching an id or display name."""
return _get_entry(selection, SAM_ENTRIES, "SAM")
def get_grounding_dino_entry(selection: str) -> ModelEntry:
"""Return the GroundingDINO catalog entry matching an id or display name."""
return _get_entry(selection, GROUNDING_DINO_ENTRIES, "GroundingDINO")
def get_vitmatte_entry(selection: str) -> ModelEntry:
"""Return the ViTMatte catalog entry matching an id or display name."""
return _get_entry(selection, VITMATTE_ENTRIES, "ViTMatte")
def get_wd14_tagger_entry(selection: str) -> ModelEntry:
"""Return the WD14 tagger catalog entry matching an id or display name."""
return _get_entry(selection, WD14_TAGGER_ENTRIES, "WD14 tagger")
def _get_entry(
selection: str,
entries: tuple[ModelEntry, ...],
model_label: str,
) -> ModelEntry:
"""Return a catalog entry or raise an actionable selection error."""
for entry in entries:
if selection in (entry.entry_id, entry.display_name):
return entry
valid = ", ".join(entry.display_name for entry in entries)
raise ValueError(
f"Unknown {model_label} model '{selection}'. Expected one of: {valid}."
)
+173
View File
@@ -0,0 +1,173 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Build settings-aware model dropdown choices for loader nodes."""
from __future__ import annotations
from types import ModuleType
from typing import Protocol
from .model_catalog import (
GROUNDING_DINO_ENTRIES,
SAM_ENTRIES,
VITMATTE_ENTRIES,
WD14_TAGGER_ENTRIES,
ModelEntry,
grounding_dino_choices,
sam_choices,
vitmatte_choices,
wd14_tagger_choices,
)
from .model_folders import resolve_model_file
from .settings import SimpleSyrupSettings, SimpleSyrupSettingsRepository
from .vitmatte_loader import ViTMatteLoaderService
NO_LOCAL_SAM_MODELS = "No local SAM models found"
NO_LOCAL_GROUNDING_DINO_MODELS = "No local GroundingDINO models found"
NO_LOCAL_VITMATTE_MODELS = "No local ViTMatte models found"
NO_LOCAL_WD14_TAGGER_MODELS = "No local WD14 tagger models found"
class SettingsProvider(Protocol):
"""Settings dependency used by model choice policy."""
def load(self) -> SimpleSyrupSettings:
"""Return current SimpleSyrup settings."""
class ModelChoiceService:
"""Build model loader dropdown choices from settings and local availability."""
def __init__(
self,
settings_repository: SettingsProvider | None = None,
folder_paths_module: ModuleType | None = None,
) -> None:
"""Create the choice service with injectable external boundaries."""
self._settings_repository = (
settings_repository or SimpleSyrupSettingsRepository()
)
self._folder_paths_module = folder_paths_module
self._vitmatte_loader = ViTMatteLoaderService(
folder_paths_module=folder_paths_module
)
def sam_choices(self) -> list[str]:
"""Return settings-aware SAM dropdown choices."""
if self._show_downloadable_models():
return sam_choices()
choices = [
entry.display_name
for entry in SAM_ENTRIES
if self._entry_artifacts_are_local(entry)
]
return choices or [NO_LOCAL_SAM_MODELS]
def grounding_dino_choices(self) -> list[str]:
"""Return settings-aware GroundingDINO dropdown choices."""
if self._show_downloadable_models():
return grounding_dino_choices()
choices = [
entry.display_name
for entry in GROUNDING_DINO_ENTRIES
if self._entry_artifacts_are_local(entry)
]
return choices or [NO_LOCAL_GROUNDING_DINO_MODELS]
def vitmatte_choices(self) -> list[str]:
"""Return settings-aware ViTMatte dropdown choices."""
if self._show_downloadable_models():
return vitmatte_choices()
choices = [
entry.display_name
for entry in VITMATTE_ENTRIES
if self._vitmatte_entry_is_local(entry)
]
return choices or [NO_LOCAL_VITMATTE_MODELS]
def wd14_tagger_choices(self) -> list[str]:
"""Return settings-aware WD14 tagger dropdown choices."""
if self._show_downloadable_models():
return wd14_tagger_choices()
choices = [
entry.display_name
for entry in WD14_TAGGER_ENTRIES
if self._entry_artifacts_are_local(entry)
]
return choices or [NO_LOCAL_WD14_TAGGER_MODELS]
def reject_sentinel(self, selection: str) -> None:
"""Reject placeholder dropdown selections before loader work begins."""
if selection == NO_LOCAL_SAM_MODELS:
raise ValueError(
"No local SAM models are available. Enable 'Show downloadable "
"models in loader dropdowns' in SimpleSyrup settings or install "
"a SAM model in the sams folder."
)
if selection == NO_LOCAL_GROUNDING_DINO_MODELS:
raise ValueError(
"No local GroundingDINO models are available. Enable 'Show "
"downloadable models in loader dropdowns' in SimpleSyrup "
"settings or install a complete GroundingDINO model in the "
"grounding-dino folder."
)
if selection == NO_LOCAL_VITMATTE_MODELS:
raise ValueError(
"No local ViTMatte models are available. Enable 'Show "
"downloadable models in loader dropdowns' in SimpleSyrup "
"settings or install a ViTMatte model in the vitmatte folder."
)
if selection == NO_LOCAL_WD14_TAGGER_MODELS:
raise ValueError(
"No local WD14 tagger models are available. Enable 'Show "
"downloadable models in loader dropdowns' in SimpleSyrup "
"settings or install a WD14 ONNX model and CSV in the "
"wd14_tagger folder."
)
def _show_downloadable_models(self) -> bool:
"""Return whether known downloadable catalog entries should be visible."""
return self._settings_repository.load().show_downloadable_models
def _entry_artifacts_are_local(self, entry: ModelEntry) -> bool:
"""Return whether every catalog artifact exists locally."""
return all(
resolve_model_file(
artifact.folder_name,
artifact.filename,
self._folder_paths_module,
)
is not None
for artifact in entry.artifacts
)
def _vitmatte_entry_is_local(self, entry: ModelEntry) -> bool:
"""Return whether a valid ViTMatte directory exists locally."""
try:
self._vitmatte_loader.resolve_model_directory(entry, auto_download=False)
except FileNotFoundError:
return False
return True
def default_choice(choices: list[str], preferred: str) -> str:
"""Return the preferred default when visible, otherwise the first choice."""
if preferred in choices:
return preferred
return choices[0]
@@ -0,0 +1,277 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Comfy-aware device management for SimpleSyrup-owned torch models."""
from __future__ import annotations
import importlib
from collections.abc import Iterator
from contextlib import contextmanager
from dataclasses import dataclass, field
from types import ModuleType
from typing import Any, cast
import torch
from ..shared.logging import get_logger
LOGGER = get_logger(__name__)
@dataclass
class ManagedTorchModel:
"""Track one SimpleSyrup-owned model under Comfy's memory manager."""
model: object
model_id: str
source: str
patcher: object | None = field(default=None, init=False, repr=False)
load_device: torch.device | None = field(default=None, init=False)
offload_device: torch.device | None = field(default=None, init=False)
@dataclass(frozen=True)
class LoadedManagedModel:
"""Expose a model loaded for one inference call."""
model: object
device: torch.device
policy: str
class TorchModelDeviceManager:
"""Load SimpleSyrup-owned torch models through Comfy's VRAM policy."""
def manage(self, model: object, model_id: str, source: str) -> ManagedTorchModel:
"""Return a managed handle for a raw model without moving it to CUDA."""
_eval_model(model)
return ManagedTorchModel(model=model, model_id=model_id, source=source)
@contextmanager
def inference(
self,
managed_model: ManagedTorchModel,
execution_device: str,
) -> Iterator[LoadedManagedModel]:
"""Yield the model loaded on the device selected by `execution_device`."""
policy = _validate_execution_device(execution_device)
if policy == "cpu":
device = torch.device("cpu")
_move_model(managed_model.model, device)
_eval_model(managed_model.model)
LOGGER.debug(
"Torch model prepared for CPU inference",
extra={
"operation": "torch_model_device_manager",
"model": managed_model.model_id,
"policy": policy,
"device": str(device),
"source": managed_model.source,
},
)
yield LoadedManagedModel(
model=managed_model.model,
device=device,
policy=policy,
)
return
comfy_model_management, comfy_model_patcher = _comfy_modules()
load_device = torch.device(comfy_model_management.get_torch_device())
if load_device.type == "cpu":
_move_model(managed_model.model, load_device)
_eval_model(managed_model.model)
yield LoadedManagedModel(
model=managed_model.model,
device=load_device,
policy=policy,
)
return
offload_device = _resolve_offload_device(comfy_model_management)
if not _supports_comfy_model_patcher(managed_model.model):
LOGGER.debug(
"Torch model uses bounded device movement outside ModelPatcher",
extra={
"operation": "torch_model_device_manager",
"model": managed_model.model_id,
"policy": policy,
"load_device": str(load_device),
"offload_device": str(offload_device),
"source": managed_model.source,
},
)
_move_model(managed_model.model, load_device)
_eval_model(managed_model.model)
try:
yield LoadedManagedModel(
model=managed_model.model,
device=load_device,
policy=policy,
)
finally:
_move_model(managed_model.model, offload_device)
_soft_empty_cache(comfy_model_management)
return
patcher = self._patcher_for(
managed_model,
comfy_model_patcher,
load_device,
offload_device,
)
LOGGER.debug(
"Loading torch model through Comfy model manager",
extra={
"operation": "torch_model_device_manager",
"model": managed_model.model_id,
"policy": policy,
"load_device": str(load_device),
"offload_device": str(offload_device),
"source": managed_model.source,
},
)
comfy_model_management.load_model_gpu(patcher)
_eval_model(managed_model.model)
yield LoadedManagedModel(
model=managed_model.model,
device=load_device,
policy=policy,
)
def _patcher_for(
self,
managed_model: ManagedTorchModel,
comfy_model_patcher: ModuleType,
load_device: torch.device,
offload_device: torch.device,
) -> object:
"""Return a reusable `ModelPatcher` for the requested devices."""
if (
managed_model.patcher is not None
and managed_model.load_device == load_device
and managed_model.offload_device == offload_device
):
return managed_model.patcher
patcher_class = cast(Any, comfy_model_patcher).ModelPatcher
managed_model.patcher = patcher_class(
managed_model.model,
load_device,
offload_device,
)
managed_model.load_device = load_device
managed_model.offload_device = offload_device
return managed_model.patcher
def resolve_execution_device(execution_device: str) -> torch.device:
"""Resolve a public SimpleSyrup execution device policy."""
policy = _validate_execution_device(execution_device)
if policy == "cpu":
return torch.device("cpu")
comfy_model_management = importlib.import_module("comfy.model_management")
return torch.device(comfy_model_management.get_torch_device())
@contextmanager
def external_model_inference(
model: object,
execution_device: str,
) -> Iterator[LoadedManagedModel]:
"""Move a compatible external raw model for one bounded inference call."""
device = resolve_execution_device(execution_device)
original_device = _model_device(model)
_move_model(model, device)
_eval_model(model)
try:
yield LoadedManagedModel(model=model, device=device, policy=execution_device)
finally:
if original_device is not None and original_device != device:
_move_model(model, original_device)
def _validate_execution_device(execution_device: str) -> str:
"""Return a normalized execution policy or fail clearly."""
if execution_device in {"auto", "cpu"}:
return execution_device
raise ValueError("execution_device must be 'auto' or 'cpu'.")
def _comfy_modules() -> tuple[ModuleType, ModuleType]:
"""Import Comfy model-management modules lazily."""
return (
importlib.import_module("comfy.model_management"),
importlib.import_module("comfy.model_patcher"),
)
def _resolve_offload_device(comfy_model_management: ModuleType) -> torch.device:
"""Return Comfy's preferred offload device for auxiliary torch modules."""
offload_device = cast(Any, comfy_model_management).text_encoder_offload_device()
return torch.device(offload_device)
def _supports_comfy_model_patcher(model: object) -> bool:
"""Return whether Comfy's patcher can assign `model.device` safely."""
class_device = getattr(type(model), "device", None)
if isinstance(class_device, property) and class_device.fset is None:
return False
return True
def _soft_empty_cache(comfy_model_management: ModuleType) -> None:
"""Ask Comfy to release cached memory after bounded manual offload."""
soft_empty_cache = getattr(comfy_model_management, "soft_empty_cache", None)
if callable(soft_empty_cache):
soft_empty_cache()
def _move_model(model: object, device: torch.device) -> None:
"""Move a PyTorch-style model when it exposes `.to(...)`."""
to_method = getattr(model, "to", None)
if callable(to_method):
to_method(device)
def _eval_model(model: object) -> None:
"""Set eval mode when the model exposes `.eval()`."""
eval_method = getattr(model, "eval", None)
if callable(eval_method):
eval_method()
def _model_device(model: object) -> torch.device | None:
"""Return the first known torch device for a raw model, if any."""
device = getattr(model, "device", None)
if device is not None:
return torch.device(device)
parameters = getattr(model, "parameters", None)
if callable(parameters):
try:
first_parameter = next(iter(parameters()))
except StopIteration:
return None
except TypeError:
return None
if isinstance(first_parameter, torch.Tensor):
return first_parameter.device
parameter_device = getattr(first_parameter, "device", None)
if parameter_device is not None:
return torch.device(parameter_device)
return None
+244
View File
@@ -0,0 +1,244 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Download known model artifacts with ComfyUI progress reporting."""
from __future__ import annotations
import hashlib
import importlib
import urllib.request
from dataclasses import dataclass
from pathlib import Path
from typing import BinaryIO, Protocol, cast
from ..shared.logging import get_logger
LOGGER = get_logger(__name__)
CHUNK_SIZE = 1024 * 1024
class ProgressReporter(Protocol):
"""Report artifact download progress."""
def start(self, label: str, total: int | None) -> None:
"""Start reporting one artifact download."""
def advance(self, current: int, total: int | None) -> None:
"""Report absolute bytes downloaded."""
def finish(self) -> None:
"""Report completion."""
class _ComfyProgressBar(Protocol):
"""Small protocol for the ComfyUI progress bar API used here."""
def update_absolute(self, value: int, total: int | None = None) -> None:
"""Update absolute progress."""
class NullProgressReporter:
"""Ignore progress updates."""
def start(self, label: str, total: int | None) -> None:
"""Ignore download start."""
def advance(self, current: int, total: int | None) -> None:
"""Ignore download progress."""
def finish(self) -> None:
"""Ignore download completion."""
class ComfyProgressReporter:
"""Report download progress through ComfyUI's node progress bar."""
def __init__(self) -> None:
"""Initialize an empty ComfyUI progress reporter."""
self._progress_bar: object | None = None
self._total = 1
def start(self, label: str, total: int | None) -> None:
"""Create a ComfyUI progress bar for one artifact."""
comfy_utils = importlib.import_module("comfy.utils")
progress_bar_class = comfy_utils.ProgressBar
self._total = total if total and total > 0 else 1
self._progress_bar = progress_bar_class(self._total)
self.advance(0, total)
LOGGER.info("download progress started", extra={"label": label, "total": total})
def advance(self, current: int, total: int | None) -> None:
"""Update the ComfyUI progress bar."""
if self._progress_bar is None:
return
if total and total > 0 and total != self._total:
self._total = total
value = (
current if total and total > 0 else min(current // CHUNK_SIZE, self._total)
)
progress_bar = cast(_ComfyProgressBar, self._progress_bar)
progress_bar.update_absolute(value, self._total)
def finish(self) -> None:
"""Mark the current ComfyUI progress bar complete."""
if self._progress_bar is None:
return
progress_bar = cast(_ComfyProgressBar, self._progress_bar)
progress_bar.update_absolute(self._total, self._total)
@dataclass(frozen=True)
class DownloadRequest:
"""A trusted catalog download request."""
source_url: str
destination_path: Path
expected_folder: Path
description: str
expected_sha256: str | None = None
@dataclass(frozen=True)
class DownloadResult:
"""The result of resolving or downloading an artifact."""
path: Path
bytes_downloaded: int
skipped_existing: bool
class ModelDownloader:
"""Download trusted model artifacts safely into model folders."""
def download(
self,
request: DownloadRequest,
progress: ProgressReporter | None = None,
) -> DownloadResult:
"""Download an artifact unless the final file already exists."""
reporter = progress or NullProgressReporter()
destination = request.destination_path
self._validate_destination(destination, request.expected_folder)
if destination.is_file():
return DownloadResult(
path=destination,
bytes_downloaded=0,
skipped_existing=True,
)
destination.parent.mkdir(parents=True, exist_ok=True)
temporary_path = destination.with_name(f"{destination.name}.part")
try:
LOGGER.info(
"downloading model artifact",
extra={
"url": request.source_url,
"destination": str(destination),
"description": request.description,
},
)
bytes_downloaded = self._download_to_temporary(
source_url=request.source_url,
temporary_path=temporary_path,
reporter=reporter,
description=request.description,
)
if request.expected_sha256 is not None:
actual_sha256 = _sha256_file(temporary_path)
if actual_sha256.lower() != request.expected_sha256.lower():
raise ValueError(
"Downloaded model artifact checksum mismatch for "
f"'{destination}'. Expected {request.expected_sha256}, "
f"got {actual_sha256}."
)
temporary_path.replace(destination)
reporter.finish()
return DownloadResult(
path=destination,
bytes_downloaded=bytes_downloaded,
skipped_existing=False,
)
except Exception:
if temporary_path.exists():
temporary_path.unlink()
LOGGER.exception(
"model artifact download failed",
extra={
"url": request.source_url,
"destination": str(destination),
"description": request.description,
},
)
raise
def _download_to_temporary(
self,
source_url: str,
temporary_path: Path,
reporter: ProgressReporter,
description: str,
) -> int:
"""Stream a URL to a temporary file."""
with urllib.request.urlopen(source_url, timeout=30) as response:
total = _content_length(response)
reporter.start(f"Downloading {description}", total)
bytes_downloaded = 0
with temporary_path.open("wb") as output:
while True:
chunk = response.read(CHUNK_SIZE)
if not chunk:
break
output.write(chunk)
bytes_downloaded += len(chunk)
reporter.advance(bytes_downloaded, total)
return bytes_downloaded
def _validate_destination(self, destination: Path, expected_folder: Path) -> None:
"""Reject destinations outside the intended model folder."""
destination_parent = destination.parent.resolve()
expected_root = expected_folder.resolve()
try:
destination_parent.relative_to(expected_root)
except ValueError as error:
raise ValueError(
f"Download destination '{destination}' is outside '{expected_folder}'."
) from error
def _content_length(response: BinaryIO) -> int | None:
"""Return response content length when the server supplies it."""
headers = getattr(response, "headers", None)
if headers is None:
return None
value = headers.get("Content-Length")
if value is None:
return None
try:
return int(value)
except ValueError:
return None
def _sha256_file(path: Path) -> str:
"""Return the SHA256 hex digest for a file."""
digest = hashlib.sha256()
with path.open("rb") as file:
while True:
chunk = file.read(CHUNK_SIZE)
if not chunk:
break
digest.update(chunk)
return digest.hexdigest()
+195
View File
@@ -0,0 +1,195 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""ComfyUI model folder registration and bounded discovery helpers."""
from __future__ import annotations
import importlib
from pathlib import Path
from types import ModuleType
from typing import Any
from ..shared.logging import get_logger
LOGGER = get_logger(__name__)
SUPPORTED_MODEL_EXTENSIONS = frozenset({".pt", ".pth", ".safetensors"})
WD14_TAGGER_MODEL_EXTENSIONS = frozenset({".onnx", ".csv"})
MODEL_FOLDER_EXTENSIONS = {
"sams": SUPPORTED_MODEL_EXTENSIONS,
"grounding-dino": SUPPORTED_MODEL_EXTENSIONS,
"vitmatte": SUPPORTED_MODEL_EXTENSIONS,
"wd14_tagger": WD14_TAGGER_MODEL_EXTENSIONS,
}
def register_required_model_folders(
folder_paths_module: ModuleType | None = None,
) -> None:
"""Register model folders required by grounded SAM if ComfyUI has not."""
folder_paths = folder_paths_module or _folder_paths()
models_dir = Path(str(folder_paths.models_dir))
_register_folder(
folder_paths,
"sams",
models_dir / "sams",
MODEL_FOLDER_EXTENSIONS["sams"],
)
_register_folder(
folder_paths,
"grounding-dino",
models_dir / "grounding-dino",
MODEL_FOLDER_EXTENSIONS["grounding-dino"],
)
_register_folder(
folder_paths,
"vitmatte",
models_dir / "vitmatte",
MODEL_FOLDER_EXTENSIONS["vitmatte"],
)
_register_folder(
folder_paths,
"wd14_tagger",
models_dir / "wd14_tagger",
MODEL_FOLDER_EXTENSIONS["wd14_tagger"],
)
def get_model_folder_paths(
folder_name: str,
folder_paths_module: ModuleType | None = None,
) -> list[Path]:
"""Return registered paths plus the conventional fallback for a model folder."""
folder_paths = folder_paths_module or _folder_paths()
models_dir = Path(str(folder_paths.models_dir))
fallback = models_dir / folder_name
registry = getattr(folder_paths, "folder_names_and_paths", {})
paths: list[Path] = []
if folder_name in registry:
registered_paths = registry[folder_name][0]
paths.extend(Path(str(path)) for path in registered_paths)
if fallback not in paths:
paths.append(fallback)
return _unique_paths(paths)
def get_primary_model_folder(
folder_name: str,
folder_paths_module: ModuleType | None = None,
) -> Path:
"""Return the first registered or fallback path for a model folder."""
return get_model_folder_paths(folder_name, folder_paths_module)[0]
def resolve_model_file(
folder_name: str,
filename: str,
folder_paths_module: ModuleType | None = None,
) -> Path | None:
"""Return a model file path when it exists in registered or fallback folders."""
safe_filename = Path(filename)
if safe_filename.is_absolute() or ".." in safe_filename.parts:
raise ValueError(f"Model filename '{filename}' is not a safe relative path.")
for folder in get_model_folder_paths(folder_name, folder_paths_module):
candidate = folder / safe_filename
if candidate.is_file():
return candidate
return None
def expected_model_file(
folder_name: str,
filename: str,
folder_paths_module: ModuleType | None = None,
) -> Path:
"""Return the expected destination for a known model artifact."""
safe_filename = Path(filename)
if safe_filename.is_absolute() or ".." in safe_filename.parts:
raise ValueError(f"Model filename '{filename}' is not a safe relative path.")
return get_primary_model_folder(folder_name, folder_paths_module) / safe_filename
def nonrecursive_model_files(
folder_name: str,
folder_paths_module: ModuleType | None = None,
) -> list[str]:
"""Return supported files directly inside a model folder without recursion."""
files: set[str] = set()
extensions = _folder_extensions(folder_name, folder_paths_module)
for folder in get_model_folder_paths(folder_name, folder_paths_module):
if not folder.is_dir():
continue
for path in folder.iterdir():
if path.is_file() and path.suffix.lower() in extensions:
files.add(path.name)
return sorted(files)
def _register_folder(
folder_paths: ModuleType,
folder_name: str,
path: Path,
extensions: frozenset[str],
) -> None:
"""Register one model folder if ComfyUI has not already done so."""
registry: dict[str, tuple[list[str], set[str]]] = (
folder_paths.folder_names_and_paths
)
if folder_name in registry:
return
add_model_folder_path = folder_paths.add_model_folder_path
add_model_folder_path(folder_name, str(path))
registry[folder_name] = (registry[folder_name][0], set(extensions))
LOGGER.info(
"registered model folder", extra={"folder_name": folder_name, "path": str(path)}
)
def _folder_extensions(
folder_name: str,
folder_paths_module: ModuleType | None,
) -> frozenset[str]:
"""Return supported extensions for a model folder."""
folder_paths = folder_paths_module or _folder_paths()
registry = getattr(folder_paths, "folder_names_and_paths", {})
if folder_name in registry:
_paths, extensions = registry[folder_name]
return frozenset(extensions)
return MODEL_FOLDER_EXTENSIONS.get(folder_name, SUPPORTED_MODEL_EXTENSIONS)
def _unique_paths(paths: list[Path]) -> list[Path]:
"""Return paths in order without duplicates."""
unique: list[Path] = []
seen: set[str] = set()
for path in paths:
key = str(path)
if key in seen:
continue
unique.append(path)
seen.add(key)
return unique
def _folder_paths() -> ModuleType:
"""Import ComfyUI folder paths lazily."""
module: Any = importlib.import_module("folder_paths")
if not isinstance(module, ModuleType):
raise TypeError("folder_paths import did not return a module.")
return module
@@ -0,0 +1,34 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Reuse loaded external model instances by resolved runtime identity."""
from __future__ import annotations
from collections.abc import Callable, MutableMapping
from dataclasses import dataclass, field
from threading import RLock
from typing import Generic, TypeVar
KeyT = TypeVar("KeyT")
ModelT = TypeVar("ModelT")
@dataclass
class ModelInstanceCache(Generic[KeyT, ModelT]):
"""Coordinate process-level reuse for SimpleSyrup-owned model instances."""
entries: MutableMapping[KeyT, ModelT] = field(default_factory=dict)
_lock: RLock = field(default_factory=RLock, init=False, repr=False)
def get_or_load(self, key: KeyT, load: Callable[[], ModelT]) -> ModelT:
"""Return a cached model instance or load and store it atomically."""
with self._lock:
cached = self.entries.get(key)
if cached is not None:
return cached
loaded = load()
self.entries[key] = loaded
return loaded
+145
View File
@@ -0,0 +1,145 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Request model catalog and local installation metadata."""
from __future__ import annotations
import json
from pathlib import Path
from types import ModuleType
from .grounding_dino_loader import TEXT_ENCODER_CHOICES
from .model_catalog import (
VITMATTE_ENTRIES,
ModelEntry,
get_grounding_dino_entry,
get_sam_entry,
)
from .model_folders import (
expected_model_file,
get_primary_model_folder,
register_required_model_folders,
resolve_model_file,
)
from .vitmatte_loader import is_valid_vitmatte_directory
class GroundedSAMModelMetadata:
"""Describe known SAM, GroundingDINO, and text encoder assets."""
def __init__(self, folder_paths_module: ModuleType | None = None) -> None:
"""Create a metadata provider with injectable folder paths."""
self._folder_paths_module = folder_paths_module
def describe_selection(self, sam_model: str, grounding_dino_model: str) -> str:
"""Return JSON metadata for selected model catalog entries."""
register_required_model_folders(self._folder_paths_module)
sam = get_sam_entry(sam_model)
dino = get_grounding_dino_entry(grounding_dino_model)
payload = {
"sam": self._entry_metadata(sam),
"grounding_dino": self._entry_metadata(dino),
"text_encoder_choices": list(TEXT_ENCODER_CHOICES),
"text_encoder_paths": {
"layerstyle": str(self._models_dir() / "bert-base-uncased"),
"text_encoders_bert": str(
expected_model_file(
"text_encoders",
"bert",
self._folder_paths_module,
)
),
},
"vitmatte": [self._vitmatte_metadata(entry) for entry in VITMATTE_ENTRIES],
}
return json.dumps(payload, indent=2, sort_keys=True)
def _entry_metadata(self, entry: ModelEntry) -> dict[str, object]:
"""Return JSON-serializable metadata for one catalog entry."""
artifacts: list[dict[str, object]] = []
for artifact in entry.artifacts:
local_path = resolve_model_file(
artifact.folder_name,
artifact.filename,
self._folder_paths_module,
)
expected = expected_model_file(
artifact.folder_name,
artifact.filename,
self._folder_paths_module,
)
artifacts.append(
{
"artifact_id": artifact.artifact_id,
"filename": artifact.filename,
"source_url": artifact.source_url,
"expected_path": str(expected),
"local_path": str(local_path) if local_path else None,
"installed": local_path is not None,
}
)
return {
"id": entry.entry_id,
"display_name": entry.display_name,
"model_type": entry.model_type,
"source_repo": entry.source_repo,
"auto_download_allowed": entry.auto_download_allowed,
"artifacts": artifacts,
}
def _vitmatte_metadata(self, entry: ModelEntry) -> dict[str, object]:
"""Return metadata for one ViTMatte model directory."""
canonical = (
get_primary_model_folder(
"vitmatte",
self._folder_paths_module,
)
/ entry.entry_id
)
layerstyle = self._vitmatte_layerstyle_path(entry)
candidates = [canonical]
if layerstyle is not None and layerstyle not in candidates:
candidates.append(layerstyle)
installed_path = next(
(
candidate
for candidate in candidates
if is_valid_vitmatte_directory(candidate)
),
None,
)
return {
"id": entry.entry_id,
"display_name": entry.display_name,
"model_type": entry.model_type,
"source_repo": entry.source_repo,
"canonical_path": str(canonical),
"layerstyle_compatible_path": str(layerstyle) if layerstyle else None,
"installed": installed_path is not None,
"local_path": str(installed_path) if installed_path else None,
}
def _vitmatte_layerstyle_path(self, entry: ModelEntry) -> Path | None:
"""Return LayerStyle's compatible ViTMatte path for one entry."""
if entry.entry_id == "vitmatte-small-composition-1k":
return self._models_dir() / "vitmatte"
if entry.entry_id == "vitmatte-base-composition-1k":
return self._models_dir() / "vitmatte-base-composition-1k"
return None
def _models_dir(self) -> Path:
"""Return ComfyUI's model root directory."""
import importlib
folder_paths = self._folder_paths_module or importlib.import_module(
"folder_paths"
)
return Path(str(folder_paths.models_dir))
@@ -0,0 +1,317 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Portions of this file incorporate behavior derived from
# multidiffusion-upscaler-for-automatic1111. See third_party/manifest.toml and
# third_party/NOTICE.md.
"""ComfyUI runtime adapter for MultiDiffusion tiled sampling."""
from __future__ import annotations
from collections.abc import Sequence
from importlib import import_module
from types import ModuleType
from typing import Any, cast
import torch
from ..domain.tiled_diffusion import (
LatentTile,
TiledDiffusionPlan,
build_tiled_diffusion_plan,
)
from ..shared.logging import get_logger
from . import sampling_samplers, sampling_schedulers
from .detail_previews import DetailPreviewContext, prepare_detail_preview_callback
from .tiled_sampling import (
ApplyModel,
Latent,
ModelFunctionWrapper,
make_tiled_model_args,
new_spatial_weight_buffer,
reject_unsupported_conditioning,
spatial_tile_slicer,
validate_latent_samples,
validate_sampling_controls,
validate_tensor_shape,
)
LOGGER = get_logger(__name__)
SAMPLER_LABEL = "MultiDiffusion"
UNIPC_SAMPLERS = frozenset({"uni_pc", "uni_pc_bh2"})
def sample_multidiffusion(
*,
model: Any,
seed: int,
steps: int,
cfg: float,
sampler_name: str,
scheduler: str,
positive: Any,
negative: Any,
latent_image: Latent,
denoise: float,
latent_tile_width: int,
latent_tile_height: int,
latent_tile_overlap: int,
latent_tile_batch_size: int,
preview_context: DetailPreviewContext | None = None,
) -> Latent:
"""Sample a latent with a cloned model patched for MultiDiffusion."""
validate_sampling_controls(
steps=steps,
denoise=denoise,
latent_tile_width=latent_tile_width,
latent_tile_height=latent_tile_height,
latent_tile_batch_size=latent_tile_batch_size,
)
_reject_unipc_sampler(sampler_name)
reject_unsupported_conditioning(positive, sampler_label=SAMPLER_LABEL)
reject_unsupported_conditioning(negative, sampler_label=SAMPLER_LABEL)
sampler = sampling_samplers.resolve_sampler(sampler_name)
sigmas = sampling_schedulers.calculate_sigmas(
model=model,
scheduler_name=scheduler,
sampler_name=sampler_name,
steps=steps,
denoise=denoise,
).to(model.load_device)
latent_samples = validate_latent_samples(
latent_image,
sampler_label=SAMPLER_LABEL,
)
comfy_sample = _comfy_sample()
comfy_utils = _comfy_utils()
latent_samples = comfy_sample.fix_empty_latent_channels(
model,
latent_samples,
latent_image.get("downscale_ratio_spacial", None),
)
validate_tensor_shape(latent_samples, sampler_label=SAMPLER_LABEL)
latent_height = int(latent_samples.shape[-2])
latent_width = int(latent_samples.shape[-1])
sampling_model, plan = clone_model_with_multidiffusion(
model,
latent_width=latent_width,
latent_height=latent_height,
tile_width=latent_tile_width,
tile_height=latent_tile_height,
overlap=latent_tile_overlap,
tile_batch_size=latent_tile_batch_size,
)
batch_inds = latent_image["batch_index"] if "batch_index" in latent_image else None
noise = comfy_sample.prepare_noise(latent_samples, seed, batch_inds)
noise_mask = latent_image.get("noise_mask", None)
callback = _sampling_callback(sampling_model, steps, preview_context)
samples = comfy_sample.sample_custom(
sampling_model,
noise,
cfg,
sampler,
sigmas,
positive,
negative,
latent_samples,
noise_mask=noise_mask,
callback=callback,
disable_pbar=not comfy_utils.PROGRESS_BAR_ENABLED,
seed=seed,
)
LOGGER.info(
"KSampler MultiDiffusion pass completed",
extra={
"operation": "ksampler_multidiffusion",
"sampler": sampler_name,
"scheduler": scheduler,
"steps": steps,
"denoise": denoise,
"latent_width": latent_width,
"latent_height": latent_height,
"latent_ndim": latent_samples.ndim,
"tile_width": plan.tile_width,
"tile_height": plan.tile_height,
"overlap": plan.overlap,
"tile_count": len(plan.tiles),
"requested_tile_batch_size": plan.requested_tile_batch_size,
"tile_batch_size": plan.tile_batch_size,
},
)
output = latent_image.copy()
output.pop("downscale_ratio_spacial", None)
output["samples"] = samples
return output
def clone_model_with_multidiffusion(
model: Any,
*,
latent_width: int,
latent_height: int,
tile_width: int,
tile_height: int,
overlap: int,
tile_batch_size: int,
) -> tuple[Any, TiledDiffusionPlan]:
"""Return a model clone patched with a pre-CFG MultiDiffusion wrapper."""
plan = build_tiled_diffusion_plan(
latent_width=latent_width,
latent_height=latent_height,
tile_width=tile_width,
tile_height=tile_height,
overlap=overlap,
tile_batch_size=tile_batch_size,
)
cloned_model = model.clone()
old_wrapper = cloned_model.model_options.get("model_function_wrapper")
if old_wrapper is not None and not callable(old_wrapper):
raise ValueError("Existing model_function_wrapper is not callable.")
wrapper = MultiDiffusionModelWrapper(
plan=plan,
existing_wrapper=cast(ModelFunctionWrapper | None, old_wrapper),
)
cloned_model.set_model_unet_function_wrapper(wrapper)
return cloned_model, plan
class MultiDiffusionModelWrapper:
"""Average tiled model predictions before ComfyUI CFG combines them."""
def __init__(
self,
*,
plan: TiledDiffusionPlan,
existing_wrapper: ModelFunctionWrapper | None,
) -> None:
"""Create the model wrapper for one latent sampling shape."""
self._plan = plan
self._existing_wrapper = existing_wrapper
def __call__(
self,
apply_model: ApplyModel,
args: dict[str, Any],
) -> torch.Tensor:
"""Run the original model on latent tiles and average overlapping output."""
x = args["input"]
if not isinstance(x, torch.Tensor):
raise ValueError("MultiDiffusion model input must be a tensor.")
validate_tensor_shape(x, sampler_label=SAMPLER_LABEL)
if x.shape[-2:] != (self._plan.latent_height, self._plan.latent_width):
return self._call_original(apply_model, args)
if len(self._plan.tiles) <= 1:
return self._call_original(apply_model, args)
timestep = args["timestep"]
if not isinstance(timestep, torch.Tensor):
raise ValueError("MultiDiffusion timestep must be a tensor.")
conditioning = args.get("c", {})
if not isinstance(conditioning, dict):
raise ValueError("MultiDiffusion conditioning must be a dict.")
if conditioning.get("control") is not None:
raise ValueError(
"MultiDiffusion does not support regional conditioning or "
"ControlNet in the first implementation."
)
output_buffer = torch.zeros_like(x)
weight_buffer = new_spatial_weight_buffer(x, self._plan)
input_batch_size = int(x.shape[0])
for batch in self._plan.batches:
tiled_args = self._make_tiled_args(
args=args,
tiles=batch,
input_batch_size=input_batch_size,
)
tile_output = self._call_original(apply_model, tiled_args)
for index, tile in enumerate(batch):
tile_slice = spatial_tile_slicer(tile, x.ndim)
start = index * input_batch_size
end = start + input_batch_size
output_buffer[tile_slice] += tile_output[start:end]
weight_buffer[tile_slice] += 1.0
return output_buffer / weight_buffer.to(dtype=output_buffer.dtype)
def _call_original(
self,
apply_model: ApplyModel,
args: dict[str, Any],
) -> torch.Tensor:
"""Call the preserved model wrapper or raw apply_model."""
if self._existing_wrapper is not None:
return self._existing_wrapper(apply_model, args)
conditioning = args.get("c", {})
if not isinstance(conditioning, dict):
raise ValueError("MultiDiffusion conditioning must be a dict.")
return apply_model(args["input"], args["timestep"], **conditioning)
def _make_tiled_args(
self,
*,
args: dict[str, Any],
tiles: Sequence[LatentTile],
input_batch_size: int,
) -> dict[str, Any]:
"""Create apply-model args for one tile batch."""
return make_tiled_model_args(
args=args,
tiles=tiles,
input_batch_size=input_batch_size,
latent_height=self._plan.latent_height,
latent_width=self._plan.latent_width,
)
def _reject_unipc_sampler(sampler_name: str) -> None:
"""Reject UniPC samplers because MultiDiffusion is incompatible with them."""
if sampler_name in UNIPC_SAMPLERS:
raise ValueError("MultiDiffusion is not compatible with UniPC samplers.")
def _sampling_callback(
model: Any,
steps: int,
preview_context: DetailPreviewContext | None,
) -> Any:
"""Return a generic or detailer-specific sampling preview callback."""
if preview_context is None:
return _latent_preview().prepare_callback(model, steps)
return prepare_detail_preview_callback(model, steps, preview_context)
def _comfy_sample() -> ModuleType:
"""Import ComfyUI sample helpers lazily."""
return import_module("comfy.sample")
def _comfy_utils() -> ModuleType:
"""Import ComfyUI utility state lazily."""
return import_module("comfy.utils")
def _latent_preview() -> ModuleType:
"""Import ComfyUI preview helpers lazily."""
return import_module("latent_preview")
+33
View File
@@ -0,0 +1,33 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Small progress reporting boundary for ComfyUI node work."""
from __future__ import annotations
from importlib import import_module
from typing import Protocol, cast
class ProgressReporter(Protocol):
"""Progress sink compatible with ComfyUI progress bars."""
def update(self, value: int) -> None:
"""Advance progress by value."""
class NullProgressReporter:
"""Ignore progress updates when no UI progress sink is available."""
def update(self, value: int) -> None:
"""Ignore progress updates."""
def create_comfy_progress(total: int) -> ProgressReporter:
"""Return a ComfyUI progress bar for total units of work."""
if total <= 0:
return NullProgressReporter()
comfy_utils = import_module("comfy.utils")
return cast(ProgressReporter, comfy_utils.ProgressBar(total))
@@ -0,0 +1,67 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Detect Prompt Control installation without importing its node modules."""
from __future__ import annotations
from dataclasses import dataclass
from importlib.util import find_spec
from pathlib import Path
@dataclass(frozen=True)
class PromptControlAvailability:
"""Describe whether Prompt Control can be resolved."""
is_available: bool
root_path: Path | None
def find_prompt_control_install(
custom_nodes_root: Path | None = None,
) -> PromptControlAvailability:
"""Find Prompt Control code without importing Prompt Control nodes."""
spec_availability = _find_prompt_control_from_python_path()
if spec_availability.is_available:
return spec_availability
root = custom_nodes_root or default_custom_nodes_root()
package_path = root / "comfyui-prompt-control" / "prompt_control"
if (package_path / "nodes_lazy.py").is_file():
return PromptControlAvailability(
is_available=True,
root_path=package_path.parent,
)
return PromptControlAvailability(is_available=False, root_path=None)
def prompt_control_is_available() -> bool:
"""Return whether Prompt Control can be advertised safely."""
return find_prompt_control_install().is_available
def default_custom_nodes_root() -> Path:
"""Return the ComfyUI custom_nodes directory for this checkout."""
return Path(__file__).resolve().parents[2].parent
def _find_prompt_control_from_python_path() -> PromptControlAvailability:
"""Inspect Python import specs without importing Prompt Control submodules."""
spec = find_spec("prompt_control")
if spec is None or spec.submodule_search_locations is None:
return PromptControlAvailability(is_available=False, root_path=None)
for location in spec.submodule_search_locations:
package_path = Path(location)
if (package_path / "nodes_lazy.py").is_file():
return PromptControlAvailability(
is_available=True,
root_path=package_path.parent,
)
return PromptControlAvailability(is_available=False, root_path=None)
@@ -0,0 +1,133 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Runtime graph expansion for Prompt Control conditioning batches."""
from __future__ import annotations
import sys
from importlib import import_module
from typing import Any, cast
from ..domain.conditioning_batch import split_prompt_batch
from .prompt_control_availability import find_prompt_control_install
PROMPT_CONTROL_MISSING_MESSAGE = (
"Encode Prompt Batch w/ Prompt Control requires comfyui-prompt-control. "
"Install Prompt Control or use Encode Prompt Batch."
)
class PromptControlBatchGraphBuilder:
"""Build lazy Prompt Control graphs that return conditioning batches."""
def build(
self,
clip: Any,
positive_prompt: str,
negative_prompt: str,
separator: str,
) -> Any:
"""Return an io.NodeOutput with positive and negative batch links."""
io, graph_utils, lazy_node = self._prompt_control_dependencies()
positive_chunks = split_prompt_batch(positive_prompt, separator)
negative_chunks = split_prompt_batch(negative_prompt, separator)
expand: dict[str, dict[str, Any]] = {}
positive_output, positive_expand = self._encode_chunks(
chunks=positive_chunks,
clip=clip,
graph_utils=graph_utils,
lazy_node=lazy_node,
)
negative_output, negative_expand = self._encode_chunks(
chunks=negative_chunks,
clip=clip,
graph_utils=graph_utils,
lazy_node=lazy_node,
)
expand.update(positive_expand)
expand.update(negative_expand)
return io.NodeOutput(positive_output, negative_output, expand=expand)
def _encode_chunks(
self,
chunks: tuple[str, ...],
clip: Any,
graph_utils: Any,
lazy_node: Any,
) -> tuple[list[Any], dict[str, dict[str, Any]]]:
"""Encode chunks with Prompt Control and pack the resulting links."""
expand: dict[str, dict[str, Any]] = {}
conditioning_outputs: list[Any] = []
for chunk in chunks:
node_output = lazy_node.execute(
clip=clip,
text=chunk,
tags="",
start=0.0,
end=1.0,
num_steps=0,
)
node_expand = cast(dict[str, dict[str, Any]], node_output.expand or {})
overlap = set(expand).intersection(node_expand)
if overlap:
overlapping_ids = ", ".join(sorted(overlap))
raise ValueError(
"Prompt Control generated duplicate graph node ids: "
f"{overlapping_ids}."
)
expand.update(node_expand)
conditioning_outputs.append(node_output.args[0])
pack_graph = graph_utils.GraphBuilder()
current = pack_graph.node(
"SimpleSyrup.ConditioningBatchStart",
conditioning=conditioning_outputs[0],
)
for conditioning in conditioning_outputs[1:]:
current = pack_graph.node(
"SimpleSyrup.ConditioningBatchAppend",
batch=current.out(0),
conditioning=conditioning,
)
pack_expand = cast(dict[str, dict[str, Any]], pack_graph.finalize())
overlap = set(expand).intersection(pack_expand)
if overlap:
overlapping_ids = ", ".join(sorted(overlap))
raise ValueError(
f"SimpleSyrup generated duplicate graph node ids: {overlapping_ids}."
)
expand.update(pack_expand)
return current.out(0), expand
def _prompt_control_dependencies(self) -> tuple[Any, Any, Any]:
"""Import Prompt Control and Comfy v3 dependencies on demand."""
try:
io = import_module("comfy_api.latest.io")
except ModuleNotFoundError:
comfy_api = import_module("comfy_api.latest")
io = comfy_api.io
try:
graph_utils = import_module("comfy_execution.graph_utils")
lazy_nodes = self._import_prompt_control_lazy_nodes()
except ModuleNotFoundError as exc:
raise RuntimeError(PROMPT_CONTROL_MISSING_MESSAGE) from exc
return io, graph_utils, lazy_nodes.PCLazyTextEncodeAdvanced
def _import_prompt_control_lazy_nodes(self) -> Any:
"""Import Prompt Control lazy nodes from normal or sibling extension paths."""
try:
return import_module("prompt_control.nodes_lazy")
except ModuleNotFoundError:
availability = find_prompt_control_install()
if availability.root_path is not None:
root_path = str(availability.root_path)
if root_path not in sys.path:
sys.path.insert(0, root_path)
return import_module("prompt_control.nodes_lazy")
@@ -0,0 +1,555 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Portions of this file incorporate behavior derived from
# multidiffusion-upscaler-for-automatic1111. See third_party/manifest.toml and
# third_party/NOTICE.md.
"""ComfyUI runtime adapter for SEGS regional MultiDiffusion sampling."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from importlib import import_module
from types import ModuleType
from typing import Any, TypeAlias, cast
import torch
from ..domain.regional_detailing import LatentRegion
from ..shared.logging import get_logger
from . import sampling_samplers, sampling_schedulers
from .detail_previews import DetailPreviewContext, prepare_detail_preview_callback
from .tiled_sampling import (
Latent,
reject_unsupported_conditioning,
validate_latent_samples,
validate_tensor_shape,
)
LOGGER = get_logger(__name__)
SAMPLER_LABEL = "Regional MultiDiffusion"
UNIPC_SAMPLERS = frozenset({"uni_pc", "uni_pc_bh2"})
CalcCondBatchFunction: TypeAlias = Callable[[dict[str, Any]], list[torch.Tensor]]
@dataclass(frozen=True)
class RegionalMultiDiffusionSummary:
"""Describe regional work installed on a cloned model."""
latent_width: int
latent_height: int
latent_ndim: int
region_count: int
max_region_width: int
max_region_height: int
def sample_regional_multidiffusion(
*,
model: Any,
seed: int,
steps: int,
cfg: float,
sampler_name: str,
scheduler: str,
positive: Any,
negative: Any,
latent_image: Latent,
regions: tuple[LatentRegion, ...],
denoise: float,
global_prompt_weight: float,
preview_context: DetailPreviewContext | None = None,
) -> Latent:
"""Sample a latent with regional MultiDiffusion prompt blending."""
_validate_sampling_controls(
steps=steps,
denoise=denoise,
global_prompt_weight=global_prompt_weight,
)
_reject_unipc_sampler(sampler_name)
if not regions:
raise ValueError("Regional MultiDiffusion requires at least one region.")
reject_unsupported_conditioning(positive, sampler_label=SAMPLER_LABEL)
reject_unsupported_conditioning(negative, sampler_label=SAMPLER_LABEL)
for region in regions:
reject_unsupported_conditioning(region.positive, sampler_label=SAMPLER_LABEL)
sampler = sampling_samplers.resolve_sampler(sampler_name)
sigmas = sampling_schedulers.calculate_sigmas(
model=model,
scheduler_name=scheduler,
sampler_name=sampler_name,
steps=steps,
denoise=denoise,
).to(model.load_device)
latent_samples = validate_latent_samples(
latent_image,
sampler_label=SAMPLER_LABEL,
)
comfy_sample = _comfy_sample()
comfy_utils = _comfy_utils()
latent_samples = comfy_sample.fix_empty_latent_channels(
model,
latent_samples,
latent_image.get("downscale_ratio_spacial", None),
)
validate_tensor_shape(latent_samples, sampler_label=SAMPLER_LABEL)
latent_height = int(latent_samples.shape[-2])
latent_width = int(latent_samples.shape[-1])
sampling_model, summary = clone_model_with_regional_multidiffusion(
model,
latent_width=latent_width,
latent_height=latent_height,
latent_ndim=latent_samples.ndim,
regions=regions,
global_prompt_weight=global_prompt_weight,
)
batch_inds = latent_image["batch_index"] if "batch_index" in latent_image else None
noise = comfy_sample.prepare_noise(latent_samples, seed, batch_inds)
noise_mask = latent_image.get("noise_mask", None)
callback = _sampling_callback(sampling_model, steps, preview_context)
samples = comfy_sample.sample_custom(
sampling_model,
noise,
cfg,
sampler,
sigmas,
positive,
negative,
latent_samples,
noise_mask=noise_mask,
callback=callback,
disable_pbar=not comfy_utils.PROGRESS_BAR_ENABLED,
seed=seed,
)
LOGGER.info(
"Regional MultiDiffusion pass completed",
extra={
"operation": "regional_multidiffusion",
"sampler": sampler_name,
"scheduler": scheduler,
"steps": steps,
"denoise": denoise,
"latent_width": summary.latent_width,
"latent_height": summary.latent_height,
"latent_ndim": summary.latent_ndim,
"region_count": summary.region_count,
"max_region_width": summary.max_region_width,
"max_region_height": summary.max_region_height,
"region_positive_count": len(regions),
"global_prompt_weight": global_prompt_weight,
},
)
output = latent_image.copy()
output.pop("downscale_ratio_spacial", None)
output["samples"] = samples
return output
def clone_model_with_regional_multidiffusion(
model: Any,
*,
latent_width: int,
latent_height: int,
latent_ndim: int,
regions: tuple[LatentRegion, ...],
global_prompt_weight: float = 0.0,
) -> tuple[Any, RegionalMultiDiffusionSummary]:
"""Return a model clone patched with regional calc-cond-batch blending."""
_validate_sampling_controls(
steps=1,
denoise=1.0,
global_prompt_weight=global_prompt_weight,
)
_validate_regions(
latent_width=latent_width,
latent_height=latent_height,
regions=regions,
)
cloned_model = model.clone()
old_wrapper = cloned_model.model_options.get("sampler_calc_cond_batch_function")
if old_wrapper is not None and not callable(old_wrapper):
raise ValueError("Existing sampler_calc_cond_batch_function is not callable.")
wrapper = RegionalMultiDiffusionCalcCondBatch(
latent_width=latent_width,
latent_height=latent_height,
regions=regions,
existing_calc_cond_batch=cast(CalcCondBatchFunction | None, old_wrapper),
global_prompt_weight=global_prompt_weight,
)
cloned_model.set_model_sampler_calc_cond_batch_function(wrapper)
summary = RegionalMultiDiffusionSummary(
latent_width=latent_width,
latent_height=latent_height,
latent_ndim=latent_ndim,
region_count=len(regions),
max_region_width=max(
(region.latent_box.width for region in regions), default=0
),
max_region_height=max(
(region.latent_box.height for region in regions),
default=0,
),
)
return cloned_model, summary
class RegionalMultiDiffusionCalcCondBatch:
"""Blend regional condition predictions before CFG is applied."""
def __init__(
self,
*,
latent_width: int,
latent_height: int,
regions: tuple[LatentRegion, ...],
existing_calc_cond_batch: CalcCondBatchFunction | None,
global_prompt_weight: float,
) -> None:
"""Create a calc-cond-batch wrapper for one latent sampling shape."""
self._latent_width = latent_width
self._latent_height = latent_height
self._regions = regions
self._existing_calc_cond_batch = existing_calc_cond_batch
self._global_prompt_weight = global_prompt_weight
def __call__(self, args: dict[str, Any]) -> list[torch.Tensor]:
"""Return fallback predictions blended with regional predictions."""
x = args["input"]
if not isinstance(x, torch.Tensor):
raise ValueError("Regional MultiDiffusion model input must be a tensor.")
validate_tensor_shape(x, sampler_label=SAMPLER_LABEL)
if x.shape[-2:] != (self._latent_height, self._latent_width):
return self._call_original(args)
if not self._regions:
return self._call_original(args)
timestep = args["sigma"]
if not isinstance(timestep, torch.Tensor):
raise ValueError("Regional MultiDiffusion sigma must be a tensor.")
conds = args["conds"]
if not isinstance(conds, list) or not conds:
raise ValueError("Regional MultiDiffusion conds must be a non-empty list.")
fallback = self._call_original(args)
regional_buffers = [torch.zeros_like(output) for output in fallback]
regional_weights = [_new_spatial_weight(x) for _output in fallback]
input_batch_size = int(x.shape[0])
for region in self._regions:
region_slice = _region_slicer(region, x.ndim)
region_x = x[region_slice]
region_conds = [
_prepare_region_conditioning(
region.positive,
args=args,
x=region_x,
),
*conds[1:],
]
region_args = args.copy()
region_args["conds"] = region_conds
region_args["input"] = region_x
region_args["sigma"] = timestep
region_outputs = self._call_original(region_args)
self._accumulate_region_outputs(
outputs=region_outputs,
buffers=regional_buffers,
weights=regional_weights,
region=region,
input_batch_size=input_batch_size,
)
return [
_blend_prediction(
fallback_output,
region_output,
region_weight,
global_prompt_weight=self._global_prompt_weight,
)
for fallback_output, region_output, region_weight in zip(
fallback,
regional_buffers,
regional_weights,
strict=True,
)
]
def _call_original(self, args: dict[str, Any]) -> list[torch.Tensor]:
"""Call the previous calc-cond-batch hook or ComfyUI default."""
clean_args = args.copy()
clean_options = _clean_model_options(
cast(dict[str, Any], clean_args["model_options"]),
self._existing_calc_cond_batch,
)
clean_args["model_options"] = clean_options
if self._existing_calc_cond_batch is not None:
return self._existing_calc_cond_batch(clean_args)
comfy_samplers = _comfy_samplers()
return cast(
list[torch.Tensor],
comfy_samplers.calc_cond_batch(
clean_args["model"],
clean_args["conds"],
clean_args["input"],
clean_args["sigma"],
clean_options,
),
)
def _accumulate_region_outputs(
self,
*,
outputs: list[torch.Tensor],
buffers: list[torch.Tensor],
weights: list[torch.Tensor],
region: LatentRegion,
input_batch_size: int,
) -> None:
"""Accumulate one region prediction into full-latent buffers."""
for output_index, output in enumerate(outputs[: len(buffers)]):
region_slice = _region_slicer(region, output.ndim)
box = region.latent_box
mask_slice = (
region.latent_mask[
box.y : box.y + box.height,
box.x : box.x + box.width,
]
.reshape((1,) * (output.ndim - 2) + (box.height, box.width))
.to(device=output.device, dtype=torch.float32)
)
buffers[output_index][region_slice] += output[
:input_batch_size
] * mask_slice.to(dtype=output.dtype)
weights[output_index][region_slice] += mask_slice
def _validate_regions(
*,
latent_width: int,
latent_height: int,
regions: tuple[LatentRegion, ...],
) -> None:
"""Reject regions incompatible with the current latent shape."""
for region in regions:
_validate_region(region, latent_width=latent_width, latent_height=latent_height)
def _validate_region(
region: LatentRegion,
*,
latent_width: int,
latent_height: int,
) -> None:
"""Reject regions incompatible with the current latent shape."""
box = region.latent_box
if region.latent_mask.shape != (latent_height, latent_width):
raise ValueError(
f"Regional MultiDiffusion region {region.index} ('{region.label}') "
"latent_mask must match the full latent height and width."
)
if box.x < 0 or box.y < 0 or box.width < 1 or box.height < 1:
raise ValueError(
f"Regional MultiDiffusion region {region.index} ('{region.label}') "
"has an invalid latent box."
)
if box.x + box.width > latent_width or box.y + box.height > latent_height:
raise ValueError(
f"Regional MultiDiffusion region {region.index} ('{region.label}') "
"latent box must fit inside the latent."
)
def _region_slicer(region: LatentRegion, tensor_ndim: int) -> tuple[slice, ...]:
"""Return a slicer that crops a tensor to a latent region box."""
box = region.latent_box
return (
(slice(None),) * (tensor_ndim - 2)
+ (slice(box.y, box.y + box.height),)
+ (slice(box.x, box.x + box.width),)
)
def _new_spatial_weight(x: torch.Tensor) -> torch.Tensor:
"""Create a full-latent spatial weight buffer."""
return torch.zeros(
(1,) * (x.ndim - 2) + (int(x.shape[-2]), int(x.shape[-1])),
device=x.device,
dtype=torch.float32,
)
def _blend_prediction(
fallback: torch.Tensor,
regional: torch.Tensor,
weight: torch.Tensor,
*,
global_prompt_weight: float,
) -> torch.Tensor:
"""Blend normalized regional predictions with global fallback predictions."""
has_region = weight > 0
normalized = torch.where(
has_region,
regional / torch.clamp(weight, min=1.0e-37).to(dtype=regional.dtype),
regional,
)
coverage = torch.clamp(weight, 0.0, 1.0).to(dtype=fallback.dtype)
regional_alpha = coverage * (1.0 - global_prompt_weight)
blended = fallback * (1.0 - regional_alpha) + normalized * regional_alpha
return torch.where(has_region, blended, fallback)
def _validate_sampling_controls(
*,
steps: int,
denoise: float,
global_prompt_weight: float,
) -> None:
"""Reject invalid sampler controls before ComfyUI runtime calls."""
if steps < 1:
raise ValueError("steps must be at least 1.")
if not 0.0 <= denoise <= 1.0:
raise ValueError("denoise must be between 0 and 1.")
_validate_global_prompt_weight(global_prompt_weight)
def _validate_global_prompt_weight(global_prompt_weight: float) -> None:
"""Reject global prompt weights outside the normalized blend range."""
if not 0.0 <= global_prompt_weight <= 1.0:
raise ValueError("global_prompt_weight must be between 0.0 and 1.0.")
def _prepare_region_conditioning(
conditioning: object,
*,
args: dict[str, Any],
x: torch.Tensor,
) -> list[dict[str, Any]]:
"""Convert raw Comfy CONDITIONING into sampler-ready condition dictionaries."""
if _is_processed_conditioning(conditioning):
return cast(list[dict[str, Any]], conditioning)
if not isinstance(conditioning, list):
raise TypeError("Regional MultiDiffusion region_positive must be CONDITIONING.")
sampler_helpers = _comfy_sampler_helpers()
comfy_samplers = _comfy_samplers()
model = args["model"]
converted = cast(list[dict[str, Any]], sampler_helpers.convert_cond(conditioning))
comfy_samplers.resolve_areas_and_cond_masks_multidim(
converted,
tuple(int(dim) for dim in x.shape[2:]),
x.device,
)
comfy_samplers.calculate_start_end_timesteps(model, converted)
if hasattr(model, "extra_conds"):
converted = cast(
list[dict[str, Any]],
comfy_samplers.encode_model_conds(
model.extra_conds,
converted,
x,
x.device,
"positive",
),
)
return converted
def _is_processed_conditioning(conditioning: object) -> bool:
"""Return whether a conditioning value is already sampler-ready."""
if not isinstance(conditioning, list):
return False
if not conditioning:
return True
return all(
isinstance(item, dict) and "model_conds" in item for item in conditioning
)
def _clean_model_options(
model_options: dict[str, Any],
existing_calc_cond_batch: CalcCondBatchFunction | None,
) -> dict[str, Any]:
"""Return model options that cannot recurse into this wrapper."""
clean_options = model_options.copy()
if existing_calc_cond_batch is None:
clean_options.pop("sampler_calc_cond_batch_function", None)
else:
clean_options["sampler_calc_cond_batch_function"] = existing_calc_cond_batch
return clean_options
def _reject_unipc_sampler(sampler_name: str) -> None:
"""Reject UniPC samplers because MultiDiffusion is incompatible with them."""
if sampler_name in UNIPC_SAMPLERS:
raise ValueError(
"Regional MultiDiffusion is not compatible with UniPC samplers."
)
def _sampling_callback(
model: Any,
steps: int,
preview_context: DetailPreviewContext | None,
) -> Any:
"""Return a generic or detailer-specific sampling preview callback."""
if preview_context is None:
return _latent_preview().prepare_callback(model, steps)
return prepare_detail_preview_callback(model, steps, preview_context)
def _comfy_sample() -> ModuleType:
"""Import ComfyUI sample helpers lazily."""
return import_module("comfy.sample")
def _comfy_utils() -> ModuleType:
"""Import ComfyUI utility state lazily."""
return import_module("comfy.utils")
def _comfy_samplers() -> ModuleType:
"""Import ComfyUI sampler helpers lazily."""
return import_module("comfy.samplers")
def _comfy_sampler_helpers() -> ModuleType:
"""Import ComfyUI conditioning conversion helpers lazily."""
return import_module("comfy.sampler_helpers")
def _latent_preview() -> ModuleType:
"""Import ComfyUI preview helpers lazily."""
return import_module("latent_preview")
+215
View File
@@ -0,0 +1,215 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Load SAM models for ComfyUI model-loader nodes."""
from __future__ import annotations
import importlib
from collections.abc import MutableMapping
from dataclasses import dataclass
from pathlib import Path
from types import ModuleType
from typing import Any
from ..shared.logging import get_logger
from .loaded_models import LoadedSAMModel
from .model_catalog import ModelEntry, get_sam_entry
from .model_device_manager import TorchModelDeviceManager
from .model_downloads import DownloadRequest, ModelDownloader, ProgressReporter
from .model_folders import (
expected_model_file,
register_required_model_folders,
resolve_model_file,
)
from .model_instance_cache import ModelInstanceCache
LOGGER = get_logger(__name__)
SAM_HQ_RUNTIME_PACKAGE = "simple_syrup.third_party.sam_hq_runtime"
@dataclass(frozen=True)
class SAMModelCacheKey:
"""Identify a loaded SAM model for process-level reuse."""
model_id: str
model_type: str
checkpoint_path: Path
_LOADED_SAM_MODELS: dict[SAMModelCacheKey, LoadedSAMModel] = {}
class SAMLoaderService:
"""Resolve, download, and load known SAM models."""
def __init__(
self,
downloader: ModelDownloader | None = None,
folder_paths_module: ModuleType | None = None,
device_manager: TorchModelDeviceManager | None = None,
cache: MutableMapping[SAMModelCacheKey, LoadedSAMModel] | None = None,
) -> None:
"""Create a SAM loader with injectable external boundaries."""
self._downloader = downloader or ModelDownloader()
self._folder_paths_module = folder_paths_module
self._device_manager = device_manager or TorchModelDeviceManager()
self._cache: ModelInstanceCache[SAMModelCacheKey, LoadedSAMModel] = (
ModelInstanceCache(cache if cache is not None else _LOADED_SAM_MODELS)
)
def load_model(
self,
sam_model: str,
auto_download: bool,
progress: ProgressReporter | None = None,
) -> LoadedSAMModel:
"""Load a known SAM model and return a `SAM_MODEL`-compatible object."""
register_required_model_folders(self._folder_paths_module)
entry = get_sam_entry(sam_model)
artifact_paths = self._resolve_artifacts(entry, auto_download, progress)
checkpoint_path = artifact_paths[0]
key = SAMModelCacheKey(
model_id=entry.entry_id,
model_type=entry.model_type,
checkpoint_path=checkpoint_path.resolve(),
)
already_loaded = key in self._cache.entries
loaded = self._cache.get_or_load(
key,
lambda: self._load_uncached_model(entry, checkpoint_path),
)
if already_loaded:
LOGGER.info(
"SAM model loaded from process cache",
extra={
"operation": "sam_loader",
"model": entry.entry_id,
"model_type": entry.model_type,
"checkpoint_path": str(checkpoint_path),
},
)
return loaded
def _load_uncached_model(
self,
entry: ModelEntry,
checkpoint_path: Path,
) -> LoadedSAMModel:
"""Load and wrap a SAM model after artifact resolution and cache lookup."""
model = self._load_segment_anything_model(entry, checkpoint_path)
managed_model = self._device_manager.manage(
model,
model_id=entry.entry_id,
source=str(checkpoint_path),
)
LOGGER.info(
"SAM model loaded",
extra={
"operation": "sam_loader",
"model": entry.entry_id,
"model_type": entry.model_type,
"checkpoint_path": str(checkpoint_path),
},
)
return LoadedSAMModel(
model=model,
source=str(checkpoint_path),
model_id=entry.entry_id,
managed_model=managed_model,
)
def _resolve_artifacts(
self,
entry: ModelEntry,
auto_download: bool,
progress: ProgressReporter | None,
) -> list[Path]:
"""Resolve or download every SAM artifact for a catalog entry."""
paths: list[Path] = []
for artifact in entry.artifacts:
existing = resolve_model_file(
artifact.folder_name,
artifact.filename,
self._folder_paths_module,
)
if existing is not None:
paths.append(existing)
continue
destination = expected_model_file(
artifact.folder_name,
artifact.filename,
self._folder_paths_module,
)
if not auto_download or not entry.auto_download_allowed:
raise FileNotFoundError(
f"SAM model '{entry.display_name}' is missing and "
f"auto_download is disabled. Expected: {destination}. "
"Enable auto_download on SAM Model Loader or install the "
"model file."
)
result = self._downloader.download(
DownloadRequest(
source_url=artifact.source_url,
destination_path=destination,
expected_folder=destination.parent,
description=artifact.description,
),
progress,
)
paths.append(result.path)
return paths
def _load_segment_anything_model(
self,
entry: ModelEntry,
checkpoint_path: Path,
) -> object:
"""Load a SAM model from the segment-anything registry."""
try:
importlib.invalidate_caches()
runtime_module = importlib.import_module(
_registry_module_name(entry.model_type)
)
except ImportError as error:
raise RuntimeError(
_registry_import_error_message(entry.model_type, error)
) from error
registry: dict[str, Any] = runtime_module.sam_model_registry
model = registry[entry.model_type](checkpoint=str(checkpoint_path))
model.model_name = checkpoint_path.name
return model
def _registry_module_name(model_type: str) -> str:
"""Return the registry module that owns one SAM-compatible model type."""
if model_type.startswith("sam_hq") or model_type == "mobile_sam":
return f"{SAM_HQ_RUNTIME_PACKAGE}.build_sam_hq"
return "segment_anything"
def _registry_import_error_message(model_type: str, error: ImportError) -> str:
"""Return an actionable registry import failure for a SAM model type."""
if model_type.startswith("sam_hq"):
return (
"SAM-HQ support requires SimpleSyrup's bundled SAM-HQ runtime and "
"its dependencies. Reinstall SimpleSyrup or restore "
f"{SAM_HQ_RUNTIME_PACKAGE}. Import failed: {error}."
)
if model_type == "mobile_sam":
return (
"MobileSAM support requires SimpleSyrup's bundled SAM-HQ/MobileSAM "
"runtime and its dependencies. Reinstall SimpleSyrup or restore "
f"{SAM_HQ_RUNTIME_PACKAGE}. Import failed: {error}."
)
return f"segment-anything is required to load SAM models. Import failed: {error}."
+209
View File
@@ -0,0 +1,209 @@
# SimpleSyrup - workflow-focused ComfyUI extensions for image generation
# Copyright (C) 2026 Artificial Sweetener and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Adapt SAM-compatible model objects into box segmentation."""
from __future__ import annotations
import importlib
from typing import Any, Protocol, cast
import numpy as np
import torch
from numpy.typing import NDArray
from .loaded_models import LoadedSAMModel, unwrap_sam_model
from .model_device_manager import TorchModelDeviceManager, external_model_inference
from .sam_loader import SAM_HQ_RUNTIME_PACKAGE
class SAMBoxSegmenter(Protocol):
"""Segment image boxes into one mask per box."""
def segment_boxes(
self,
sam_model: object,
image: torch.Tensor,
boxes: torch.Tensor,
threshold: float,
execution_device: str,
) -> tuple[torch.Tensor, ...]:
"""Return one HW mask per input box."""
class SAMModelSegmenter:
"""Segment boxes with SimpleSyrup, Impact, or raw SAM model objects."""
def segment_boxes(
self,
sam_model: object,
image: torch.Tensor,
boxes: torch.Tensor,
threshold: float,
execution_device: str,
) -> tuple[torch.Tensor, ...]:
"""Return one HW mask per provided box."""
if boxes.numel() == 0:
return ()
model = unwrap_sam_model(sam_model)
image_array = _tensor_to_rgb_array(image)
wrapper = getattr(model, "sam_wrapper", None)
if wrapper is not None:
return _predict_with_wrapper(wrapper, image_array, boxes, threshold)
predict = getattr(model, "predict", None)
if callable(predict):
return _predict_with_wrapper(model, image_array, boxes, threshold)
if (
isinstance(sam_model, LoadedSAMModel)
and sam_model.managed_model is not None
):
use_hq_predictor = _uses_sam_hq_predictor(sam_model)
with TorchModelDeviceManager().inference(
sam_model.managed_model,
execution_device,
) as loaded:
return _predict_with_raw_segment_anything(
loaded.model,
image_array,
boxes,
loaded.device,
use_hq_predictor=use_hq_predictor,
)
with external_model_inference(model, execution_device) as loaded:
return _predict_with_raw_segment_anything(
loaded.model,
image_array,
boxes,
loaded.device,
use_hq_predictor=_uses_sam_hq_predictor(model),
)
def _predict_with_wrapper(
wrapper: object,
image_array: NDArray[np.uint8],
boxes: torch.Tensor,
threshold: float,
) -> tuple[torch.Tensor, ...]:
"""Segment boxes with Impact-style wrapper objects."""
prepare_device = getattr(wrapper, "prepare_device", None)
release_device = getattr(wrapper, "release_device", None)
if callable(prepare_device):
prepare_device()
try:
masks: list[torch.Tensor] = []
predict = getattr(wrapper, "predict", None)
if not callable(predict):
raise TypeError(
"SAM_MODEL is not compatible with Prompt SEGS w/ SAM. Expected an "
"object with sam_wrapper, predict(...), or a raw Segment Anything "
"model."
)
for box in boxes.cpu().tolist():
predicted = predict(image_array, [], [], box, threshold)
candidate_masks = _normalize_predicted_masks(predicted)
if candidate_masks:
masks.append(
torch.stack(candidate_masks).float().amax(dim=0).clamp(0.0, 1.0)
)
else:
masks.append(torch.zeros(image_array.shape[:2], dtype=torch.float32))
return tuple(masks)
finally:
if callable(release_device):
release_device()
def _predict_with_raw_segment_anything(
model: object,
image_array: NDArray[np.uint8],
boxes: torch.Tensor,
device: torch.device,
*,
use_hq_predictor: bool,
) -> tuple[torch.Tensor, ...]:
"""Segment boxes with a Segment Anything predictor API."""
predictor = _create_raw_sam_predictor(model, use_hq_predictor)
predictor.set_image(image_array)
transformed_boxes = predictor.transform.apply_boxes_torch(
boxes.cpu(),
image_array.shape[:2],
)
masks, _scores, _logits = predictor.predict_torch(
point_coords=None,
point_labels=None,
boxes=transformed_boxes.to(device),
multimask_output=False,
)
box_masks = cast(torch.Tensor, masks[:, 0, :, :].detach().cpu().float())
return tuple(mask.clamp(0.0, 1.0) for mask in box_masks)
def _create_raw_sam_predictor(model: object, use_hq_predictor: bool) -> Any:
"""Create the predictor matching the loaded SAM model family."""
try:
if use_hq_predictor:
predictor_module = importlib.import_module(
f"{SAM_HQ_RUNTIME_PACKAGE}.predictor"
)
predictor_class = cast(Any, predictor_module).SamPredictorHQ
return predictor_class(model, True)
segment_anything = importlib.import_module("segment_anything")
return segment_anything.SamPredictor(model)
except ImportError as error:
raise RuntimeError(
"SAM_MODEL is not compatible with Prompt SEGS w/ SAM. Expected an object "
"with sam_wrapper, predict(...), or a raw Segment Anything model with "
"its required runtime package installed."
) from error
def _uses_sam_hq_predictor(model: object) -> bool:
"""Return whether a model container or raw object needs the HQ predictor."""
if isinstance(model, LoadedSAMModel):
return model.model_id.startswith("sam_hq")
model_name = getattr(model, "model_name", "")
return isinstance(model_name, str) and model_name.startswith("sam_hq")
def _normalize_predicted_masks(predicted: object) -> list[torch.Tensor]:
"""Normalize wrapper prediction output into HW float tensors."""
if predicted is None:
return []
raw_items = predicted if isinstance(predicted, list | tuple) else [predicted]
masks: list[torch.Tensor] = []
for item in raw_items:
tensor = torch.as_tensor(item, dtype=torch.float32).detach().cpu()
while tensor.ndim > 2 and 1 in tensor.shape:
tensor = tensor.squeeze(0)
if tensor.ndim == 3:
tensor = tensor.amax(dim=0)
if tensor.ndim != 2:
raise ValueError(
f"SAM_MODEL returned an invalid mask shape: {tuple(tensor.shape)}."
)
masks.append(tensor.clamp(0.0, 1.0))
return masks
def _tensor_to_rgb_array(image: torch.Tensor) -> NDArray[np.uint8]:
"""Convert a ComfyUI HWC image sample to uint8 RGB."""
array = image.detach().cpu().float().clamp(0.0, 1.0).numpy()
if array.shape[-1] == 1:
array = np.repeat(array, 3, axis=-1)
if array.shape[-1] == 4:
array = array[..., :3]
return (array * 255.0).round().astype(np.uint8)

Some files were not shown because too many files have changed in this diff Show More