route refmods per generated chunk
This commit is contained in:
@@ -12,6 +12,15 @@ import sys
|
|||||||
REFERENCE_IMAGES_V39_TYPE = "H3_CONTINUUM_REFERENCE_IMAGES_V39"
|
REFERENCE_IMAGES_V39_TYPE = "H3_CONTINUUM_REFERENCE_IMAGES_V39"
|
||||||
|
|
||||||
|
|
||||||
|
class DumasH3RefModBundle(list):
|
||||||
|
"""H3_REF_MODS list with optional per-chunk routing metadata."""
|
||||||
|
|
||||||
|
def __init__(self, values=(), *, chunk_refs=None):
|
||||||
|
super().__init__(values)
|
||||||
|
self.chunk_refs = tuple(tuple(int(index) for index in refs)
|
||||||
|
for refs in (chunk_refs or ()))
|
||||||
|
|
||||||
|
|
||||||
def _continuum_reference_bundle_type():
|
def _continuum_reference_bundle_type():
|
||||||
"""Find the upstream H3 bundle class without copying its implementation."""
|
"""Find the upstream H3 bundle class without copying its implementation."""
|
||||||
candidates = [
|
candidates = [
|
||||||
@@ -273,6 +282,14 @@ class DumasH3SequencePromptBuilder:
|
|||||||
if index in referenced_slots
|
if index in referenced_slots
|
||||||
]
|
]
|
||||||
selected_slots = [index for index in range(1, len(refmods) + 1) if index in referenced_slots]
|
selected_slots = [index for index in range(1, len(refmods) + 1) if index in referenced_slots]
|
||||||
|
selected_position = {slot: position for position, slot in enumerate(selected_slots)}
|
||||||
|
chunk_refs = []
|
||||||
|
for text in authored_text_by_chunk:
|
||||||
|
chunk_refs.append(tuple(
|
||||||
|
selected_position[index]
|
||||||
|
for index in selected_slots
|
||||||
|
if re.search(rf"@R{index}(?![A-Za-z0-9_])", text, re.IGNORECASE)
|
||||||
|
))
|
||||||
generated = _refmod_directive(selected_refmods, 1, selected_slots)
|
generated = _refmod_directive(selected_refmods, 1, selected_slots)
|
||||||
if generated:
|
if generated:
|
||||||
directive = "\n\n".join(part for part in (directive, generated) if part)
|
directive = "\n\n".join(part for part in (directive, generated) if part)
|
||||||
@@ -314,7 +331,7 @@ class DumasH3SequencePromptBuilder:
|
|||||||
len(active_sequence["clips"]),
|
len(active_sequence["clips"]),
|
||||||
float(default_chunk_seconds),
|
float(default_chunk_seconds),
|
||||||
reference_bundle,
|
reference_bundle,
|
||||||
selected_refmods,
|
DumasH3RefModBundle(selected_refmods, chunk_refs=chunk_refs),
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+19
-1
@@ -185,7 +185,25 @@ def _add_direct_refmod_input(node_class):
|
|||||||
scramble_mode="shuffle",
|
scramble_mode="shuffle",
|
||||||
max_total_tokens=int(refmod_max_total_tokens),
|
max_total_tokens=int(refmod_max_total_tokens),
|
||||||
)
|
)
|
||||||
kwargs["model"] = bridge.attach(model, blocks, enabled=True)
|
chunk_blocks = None
|
||||||
|
chunk_refs = getattr(refmods, "chunk_refs", None)
|
||||||
|
if chunk_refs:
|
||||||
|
# Build the per-chunk payloads from the same selected
|
||||||
|
# RefMods. Ordinary H3_REF_MODS lists have no routing
|
||||||
|
# metadata and retain the upstream all-chunks behavior.
|
||||||
|
chunk_blocks = []
|
||||||
|
for positions in chunk_refs:
|
||||||
|
chunk_mods = [refmods[index] for index in positions]
|
||||||
|
chunk_blocks.append(pack._ref_blocks(
|
||||||
|
chunk_mods,
|
||||||
|
refmod_retention,
|
||||||
|
seed=-1,
|
||||||
|
scramble_mode="shuffle",
|
||||||
|
max_total_tokens=int(refmod_max_total_tokens),
|
||||||
|
))
|
||||||
|
kwargs["model"] = bridge.attach(
|
||||||
|
model, blocks, enabled=True, chunk_blocks=chunk_blocks,
|
||||||
|
)
|
||||||
print(
|
print(
|
||||||
"[DUMAS H3 Continuum Sampler V3.9] direct RefMods active "
|
"[DUMAS H3 Continuum Sampler V3.9] direct RefMods active "
|
||||||
f"({len(blocks)} block(s))"
|
f"({len(blocks)} block(s))"
|
||||||
|
|||||||
@@ -145,13 +145,15 @@ class H3NodeTests(unittest.TestCase):
|
|||||||
sys.modules[reference_module.__name__] = reference_module
|
sys.modules[reference_module.__name__] = reference_module
|
||||||
try:
|
try:
|
||||||
result = PROMPT.DumasH3SequencePromptBuilder().build(
|
result = PROMPT.DumasH3SequencePromptBuilder().build(
|
||||||
2, 4, "Shared @R2 definition.", '{"clips":['
|
2, 4, "", '{"clips":['
|
||||||
'{"slots":[{"start":0,"end":4,"text":"Use @R2."}]},'
|
'{"slots":[{"start":0,"end":4,"text":"Use @R2."}]},'
|
||||||
'{"slots":[{"start":0,"end":4,"text":"No extra reference."}]}]}',
|
'{"slots":[{"start":0,"end":4,"text":"No extra reference."}]}]}',
|
||||||
"", "", ref_mod_1=[(FakeRefMod("unused"), 1.0)],
|
"", "", ref_mod_1=[(FakeRefMod("unused"), 1.0)],
|
||||||
ref_mod_2=[(FakeRefMod("used"), 1.0)],
|
ref_mod_2=[(FakeRefMod("used"), 1.0)],
|
||||||
|
ref_mod_3=[(FakeRefMod("also_unused"), 1.0)],
|
||||||
)
|
)
|
||||||
self.assertEqual([mod.name for mod, _strength in result[4]], ["used"])
|
self.assertEqual([mod.name for mod, _strength in result[4]], ["used"])
|
||||||
|
self.assertEqual(result[4].chunk_refs, ((0,), ()))
|
||||||
self.assertIn("@R2 is the used visual reference", result[0])
|
self.assertIn("@R2 is the used visual reference", result[0])
|
||||||
self.assertNotIn("unused visual reference", result[0])
|
self.assertNotIn("unused visual reference", result[0])
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
+14
-2
@@ -9,19 +9,31 @@ from comfy.patcher_extension import WrappersMP
|
|||||||
BRIDGE_KEY = "minimax_h3_refmod_bridge"
|
BRIDGE_KEY = "minimax_h3_refmod_bridge"
|
||||||
|
|
||||||
|
|
||||||
def attach(model, blocks, enabled=True):
|
def attach(model, blocks, enabled=True, chunk_blocks=None):
|
||||||
model = model.clone()
|
model = model.clone()
|
||||||
model.remove_wrappers_with_key(WrappersMP.OUTER_SAMPLE, BRIDGE_KEY)
|
model.remove_wrappers_with_key(WrappersMP.OUTER_SAMPLE, BRIDGE_KEY)
|
||||||
if not enabled or not blocks:
|
if not enabled or not blocks:
|
||||||
return model
|
return model
|
||||||
refs = tuple(dict(b) for b in blocks)
|
refs = tuple(dict(b) for b in blocks)
|
||||||
|
per_chunk = tuple(
|
||||||
|
tuple(dict(b) for b in chunk)
|
||||||
|
for chunk in (chunk_blocks or ())
|
||||||
|
)
|
||||||
|
call_index = 0
|
||||||
|
|
||||||
def inject(executor, *args, **kwargs):
|
def inject(executor, *args, **kwargs):
|
||||||
|
nonlocal call_index
|
||||||
|
# Continuum samples physical groups in sequence order. The DUMAS
|
||||||
|
# prompt builder supplies one block set per logical chunk; use that
|
||||||
|
# set for the corresponding sampling call. A normal RefMod bundle
|
||||||
|
# has no per-chunk map and continues to apply to every chunk.
|
||||||
|
selected = per_chunk[call_index] if call_index < len(per_chunk) else refs
|
||||||
|
call_index += 1
|
||||||
guider = executor.class_obj
|
guider = executor.class_obj
|
||||||
original = guider.conds
|
original = guider.conds
|
||||||
guider.conds = {
|
guider.conds = {
|
||||||
key: [dict(entry, minimax_refs=list(entry.get("minimax_refs") or [])
|
key: [dict(entry, minimax_refs=list(entry.get("minimax_refs") or [])
|
||||||
+ [dict(ref) for ref in refs]) for entry in entries]
|
+ [dict(ref) for ref in selected]) for entry in entries]
|
||||||
for key, entries in original.items()
|
for key, entries in original.items()
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user