File size: 46,408 Bytes
77c266c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
# Copyright 2026 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Native Diffusers pipeline for MMDiff multi-modal remote-sensing generation."""

from __future__ import annotations

from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Callable

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, CLIPVisionModelWithProjection

from diffusers.image_processor import PipelineImageInput
from diffusers.loaders import FromSingleFileMixin, IPAdapterMixin, StableDiffusionLoraLoaderMixin, TextualInversionLoaderMixin
from diffusers.models import AutoencoderKL, UNet2DConditionModel
from diffusers.models.attention_processor import Attention, AttnProcessor
from diffusers.pipelines.pipeline_utils import DiffusionPipeline
from diffusers.pipelines.stable_diffusion.pipeline_output import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import (
    StableDiffusionPipeline,
    rescale_noise_cfg,
    retrieve_timesteps,
)
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.schedulers import KarrasDiffusionSchedulers
from diffusers.utils import BaseOutput, logging, replace_example_docstring

logger = logging.get_logger(__name__)

DEFAULT_RESOLUTION = 256
DEFAULT_SCENE = "ship"
DEFAULT_ATTN_LAYERS = (1, 2, 3, 4, 5, 6, 7, 8, 9)
DEFAULT_RESNET_LAYERS = (2,)
SUPPORTED_MODALITIES = ("opt", "sar", "ir")
SUPPORTED_SCENES = (
    "beach",
    "bridge",
    "desert",
    "farmland",
    "lake",
    "mountain",
    "residential",
    "river",
    "ship",
)

EXAMPLE_DOC_STRING = """
    Examples:
        ```py
        >>> from pathlib import Path
        >>> import torch
        >>> from diffusers import DiffusionPipeline

        >>> model_dir = Path("/path/to/mmdiff-diffusers")
        >>> pipe = DiffusionPipeline.from_pretrained(
        ...     str(model_dir),
        ...     local_files_only=True,
        ...     custom_pipeline=str(model_dir / "pipeline.py"),
        ...     trust_remote_code=True,
        ...     torch_dtype=torch.bfloat16,
        ... )
        >>> pipe = pipe.to("cuda")

        >>> generator = torch.Generator(device="cpu").manual_seed(2026)
        >>> output = pipe(
        ...     "There is a ship in the blue water on the shore.",
        ...     scene="ship",
        ...     height=256,
        ...     width=256,
        ...     num_inference_steps=50,
        ...     generator=generator,
        ... )
        >>> output.opt[0].save("opt.png")
        >>> output.sar[0].save("sar.png")
        >>> output.ir[0].save("ir.png")

        >>> # Hugging Face Hub style model id: UserID/RepoID
        >>> # Example: "XinRan-Tang/MM-Diff" after conversion, or a packaged `mmdiff-diffusers` repo.
        ```
"""


def collect_up_self_attentions(unet: UNet2DConditionModel) -> list[Attention]:
    r"""
    Collect up-block self-attention modules in the same depth-first order used by the
    original MMDiff hook registration.

    Args:
        unet (`UNet2DConditionModel`):
            UNet whose `up_blocks` should be scanned.

    Returns:
        `list[Attention]`: Self-attention modules in 1-based transfer order.
    """

    modules: list[Attention] = []

    def _recurse(module: torch.nn.Module) -> None:
        if module.__class__.__name__ == "Attention":
            if module.to_q.in_features == module.to_k.in_features:
                modules.append(module)
            return
        for child in module.children():
            _recurse(child)

    _recurse(unet.up_blocks)
    return modules


def collect_up_resnets(unet: UNet2DConditionModel) -> list[torch.nn.Module]:
    r"""
    Collect up-block `ResnetBlock2D` modules in the original 0-based transfer order.

    Args:
        unet (`UNet2DConditionModel`):
            UNet whose `up_blocks` should be scanned.

    Returns:
        `list[torch.nn.Module]`: Residual blocks in injection-index order.
    """

    modules: list[torch.nn.Module] = []

    def _recurse(module: torch.nn.Module) -> None:
        if module.__class__.__name__ == "ResnetBlock2D":
            modules.append(module)
            return
        for child in module.children():
            _recurse(child)

    _recurse(unet.up_blocks)
    return modules


class SpatialFeatureStore:
    r"""
    In-memory store for OPT self-attention queries and up-block residual features.

    Features are keyed by integer scheduler timestep, then by layer index. This replaces
    the original disk dump under `features/visible_attn_maps` and `features/visible_resnet_maps`.
    """

    def __init__(self) -> None:
        self.attn: dict[int, dict[int, torch.Tensor]] = {}
        self.resnet: dict[int, dict[int, torch.Tensor]] = {}
        self.current_timestep: int | None = None
        self.mode: str = "off"
        self.attn_layers: set[int] = set(DEFAULT_ATTN_LAYERS)
        self.resnet_layers: set[int] = set(DEFAULT_RESNET_LAYERS)
        self.inject_attn_timesteps: set[int] | None = None
        self.inject_resnet_timesteps: set[int] | None = None

    def reset(self) -> None:
        r"""Clear captured features and timestep state without changing layer settings."""

        self.attn = {}
        self.resnet = {}
        self.current_timestep = None

    def set_timestep(self, timestep: int | torch.Tensor) -> None:
        r"""
        Record the scheduler timestep used by the current UNet forward.

        Args:
            timestep (`int` or `torch.Tensor`):
                Scalar diffusion timestep. Tensors are stored as `int`.
        """

        self.current_timestep = int(timestep)

    def _timestep_allowed(self, allowed: set[int] | None) -> bool:
        if self.current_timestep is None:
            return False
        if allowed is None:
            return True
        return self.current_timestep in allowed

    def save_attn(self, layer_idx: int, query: torch.Tensor) -> None:
        r"""
        Cache a self-attention query tensor for the current timestep.

        Args:
            layer_idx (`int`):
                1-based up-block self-attention index.
            query (`torch.Tensor`):
                Query tensor after `head_to_batch_dim`.
        """

        if self.mode != "save" or self.current_timestep is None:
            return
        if layer_idx not in self.attn_layers:
            return
        self.attn.setdefault(self.current_timestep, {})[layer_idx] = query.detach()

    def get_attn(self, layer_idx: int) -> torch.Tensor | None:
        r"""
        Return the cached query for the current timestep and layer, if injection is active.

        Args:
            layer_idx (`int`):
                1-based up-block self-attention index.

        Returns:
            `torch.Tensor` or `None`: Cached query, or `None` when injection does not apply.
        """

        if self.mode != "inject" or not self._timestep_allowed(self.inject_attn_timesteps):
            return None
        if layer_idx not in self.attn_layers:
            return None
        return self.attn.get(self.current_timestep, {}).get(layer_idx)

    def save_resnet(self, layer_idx: int, residual: torch.Tensor) -> None:
        r"""
        Cache an up-block residual tensor for the current timestep.

        Args:
            layer_idx (`int`):
                0-based up-block ResNet index.
            residual (`torch.Tensor`):
                `ResnetBlock2D` output.
        """

        if self.mode != "save" or self.current_timestep is None:
            return
        if layer_idx not in self.resnet_layers:
            return
        self.resnet.setdefault(self.current_timestep, {})[layer_idx] = residual.detach()

    def get_resnet(self, layer_idx: int) -> torch.Tensor | None:
        r"""
        Return the cached residual for the current timestep and layer, if injection is active.

        Args:
            layer_idx (`int`):
                0-based up-block ResNet index.

        Returns:
            `torch.Tensor` or `None`: Cached residual, or `None` when injection does not apply.
        """

        if self.mode != "inject" or not self._timestep_allowed(self.inject_resnet_timesteps):
            return None
        if layer_idx not in self.resnet_layers:
            return None
        return self.resnet.get(self.current_timestep, {}).get(layer_idx)

    def to_state(self) -> dict[str, Any]:
        r"""
        Export captured features for reuse in a later `__call__`.

        Returns:
            `dict`: Detached CPU tensors plus layer configuration.
        """

        def _cpu(store: dict[int, dict[int, torch.Tensor]]) -> dict[int, dict[int, torch.Tensor]]:
            return {
                timestep: {layer: tensor.detach().cpu() for layer, tensor in layers.items()}
                for timestep, layers in store.items()
            }

        return {
            "attn": _cpu(self.attn),
            "resnet": _cpu(self.resnet),
            "attn_layers": sorted(self.attn_layers),
            "resnet_layers": sorted(self.resnet_layers),
        }

    def load_state(self, state: dict[str, Any]) -> None:
        r"""
        Restore features previously returned by `to_state`.

        Args:
            state (`dict`):
                Mapping produced by `to_state`.
        """

        if not isinstance(state, dict) or "attn" not in state:
            raise ValueError("`spatial_features` must be a dict created by MMDiffPipeline.")
        self.attn = {int(t): {int(i): v for i, v in layers.items()} for t, layers in state["attn"].items()}
        self.resnet = {
            int(t): {int(i): v for i, v in layers.items()} for t, layers in state.get("resnet", {}).items()
        }
        if "attn_layers" in state:
            self.attn_layers = set(int(i) for i in state["attn_layers"])
        if "resnet_layers" in state:
            self.resnet_layers = set(int(i) for i in state["resnet_layers"])


class MMDiffAttnProcessor(AttnProcessor):
    r"""
    Attention processor that records or replaces self-attention queries during spatial transfer.

    Args:
        layer_idx (`int`):
            1-based up-block self-attention index.
        store (`SpatialFeatureStore`):
            Shared feature store used by the current denoising loop.
    """

    def __init__(self, layer_idx: int, store: SpatialFeatureStore) -> None:
        super().__init__()
        self.layer_idx = layer_idx
        self.store = store

    def __call__(
        self,
        attn: Attention,
        hidden_states: torch.Tensor,
        encoder_hidden_states: torch.Tensor | None = None,
        attention_mask: torch.Tensor | None = None,
        temb: torch.Tensor | None = None,
        *args: Any,
        **kwargs: Any,
    ) -> torch.Tensor:
        residual = hidden_states
        if attn.spatial_norm is not None:
            hidden_states = attn.spatial_norm(hidden_states, temb)

        input_ndim = hidden_states.ndim
        if input_ndim == 4:
            batch_size, channel, height, width = hidden_states.shape
            hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)

        batch_size, sequence_length, _ = (
            hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
        )
        attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)

        if attn.group_norm is not None:
            hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)

        query = attn.to_q(hidden_states)
        if encoder_hidden_states is None:
            encoder_hidden_states = hidden_states
        elif attn.norm_cross:
            encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)

        key = attn.to_k(encoder_hidden_states)
        value = attn.to_v(encoder_hidden_states)

        query = attn.head_to_batch_dim(query)
        key = attn.head_to_batch_dim(key)
        value = attn.head_to_batch_dim(value)

        injected = self.store.get_attn(self.layer_idx)
        if injected is not None:
            query = injected.to(device=query.device, dtype=query.dtype)

        attention_probs = attn.get_attention_scores(query, key, attention_mask)
        hidden_states = torch.bmm(attention_probs, value)
        hidden_states = attn.batch_to_head_dim(hidden_states)
        hidden_states = attn.to_out[0](hidden_states)
        hidden_states = attn.to_out[1](hidden_states)

        if input_ndim == 4:
            hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)

        self.store.save_attn(self.layer_idx, query)

        if attn.residual_connection:
            hidden_states = hidden_states + residual
        hidden_states = hidden_states / attn.rescale_output_factor
        return hidden_states


@contextmanager
def spatial_transfer_hooks(unet: UNet2DConditionModel, store: SpatialFeatureStore):
    r"""
    Install native attention processors and ResNet wrappers for one denoising run.

    Args:
        unet (`UNet2DConditionModel`):
            UNet to instrument.
        store (`SpatialFeatureStore`):
            Feature store read or written by the installed hooks.
    """

    attn_modules = collect_up_self_attentions(unet)
    resnet_modules = collect_up_resnets(unet)
    original_processors = [(module, module.processor) for module in attn_modules]
    original_forwards = []

    for layer_idx, module in enumerate(attn_modules, start=1):
        module.set_processor(MMDiffAttnProcessor(layer_idx, store))

    for layer_idx, module in enumerate(resnet_modules):
        original_forward = module.forward

        def _make_forward(orig: Callable, idx: int):
            def wrapped(hidden_states: torch.Tensor, temb: torch.Tensor | None = None, *args: Any, **kwargs: Any):
                output = orig(hidden_states, temb, *args, **kwargs)
                if store.mode == "save":
                    store.save_resnet(idx, output)
                elif store.mode == "inject":
                    injected = store.get_resnet(idx)
                    if injected is not None:
                        output = injected.to(device=output.device, dtype=output.dtype)
                return output

            return wrapped

        original_forwards.append((module, original_forward))
        module.forward = _make_forward(original_forward, layer_idx)

    try:
        yield store
    finally:
        for module, processor in original_processors:
            module.set_processor(processor)
        for module, original_forward in original_forwards:
            module.forward = original_forward


def normalize_modalities(modalities: str | list[str] | tuple[str, ...]) -> list[str]:
    r"""
    Validate and normalize the modality list passed to the pipeline.

    Args:
        modalities (`str` or sequence of `str`):
            `"all"` or any subset of `opt`, `sar`, and `ir`.

    Returns:
        `list[str]`: Deduplicated modalities in OPT → SAR → IR order.
    """

    if isinstance(modalities, str):
        requested = list(SUPPORTED_MODALITIES) if modalities.lower() == "all" else [modalities.lower()]
    else:
        requested = [str(item).lower() for item in modalities]

    unknown = [item for item in requested if item not in SUPPORTED_MODALITIES]
    if unknown:
        raise ValueError(
            f"Unsupported modalities {unknown}. Expected a subset of {list(SUPPORTED_MODALITIES)} or 'all'."
        )
    if not requested:
        raise ValueError("At least one modality must be requested.")

    ordered = [item for item in SUPPORTED_MODALITIES if item in requested]
    return ordered


@dataclass
class MMDiffPipelineOutput(BaseOutput):
    r"""
    Output of [`MMDiffPipeline`].

    Args:
        images (`list`):
            Images for the first requested modality, matching the Stable Diffusion `images` convention.
        opt (`list`, *optional*):
            Optical images when that modality was generated.
        sar (`list`, *optional*):
            SAR images when that modality was generated.
        ir (`list`, *optional*):
            Infrared images when that modality was generated.
        nsfw_content_detected (`list[bool]`, *optional*):
            Safety-checker flags for the primary `images` batch, if a checker is enabled.
        spatial_features (`dict`, *optional*):
            In-memory OPT features when `return_spatial_features=True`.
    """

    images: list[Any]
    opt: list[Any] | None = None
    sar: list[Any] | None = None
    ir: list[Any] | None = None
    nsfw_content_detected: list[bool] | None = None
    spatial_features: dict[str, Any] | None = None


class MMDiffPipeline(
    StableDiffusionPipeline,
    DiffusionPipeline,
    TextualInversionLoaderMixin,
    StableDiffusionLoraLoaderMixin,
    IPAdapterMixin,
    FromSingleFileMixin,
):
    r"""
    Pipeline for jointly generating spatially consistent optical, SAR, and infrared images.

    MMDiff fine-tunes a Stable Diffusion v1 UNet on optical remote-sensing pairs, then adapts
    SAR and IR style with LoRA. During inference the OPT branch records up-block self-attention
    queries and residual features; those features are injected into the SAR and IR branches.

    Parameters:
        vae ([`AutoencoderKL`]):
            Variational Auto-Encoder used to decode latents into images.
        text_encoder ([`CLIPTextModel`]):
            Frozen CLIP text encoder.
        tokenizer ([`CLIPTokenizer`]):
            CLIP tokenizer paired with `text_encoder`.
        unet ([`UNet2DConditionModel`]):
            OPT-finetuned UNet. SAR/IR LoRA adapters are applied on top of this backbone.
        scheduler ([`KarrasDiffusionSchedulers`]):
            Denoising scheduler. The original sampling code uses [`DDPMScheduler`].
        safety_checker ([`StableDiffusionSafetyChecker`], *optional*):
            Optional safety checker. Disabled by default for remote-sensing imagery.
        feature_extractor ([`CLIPImageProcessor`], *optional*):
            Feature extractor used only when `safety_checker` is enabled.
        image_encoder ([`CLIPVisionModelWithProjection`], *optional*):
            Optional IP-Adapter image encoder.
        requires_safety_checker (`bool`, *optional*, defaults to `False`):
            Whether a missing safety checker should emit a warning.
        lora_root (`str`, *optional*, defaults to `"loras"`):
            Directory (relative to the model root) that contains `sar/<scene>` and `ir/<scene>` adapters.
    """

    model_cpu_offload_seq = "text_encoder->image_encoder->unet->vae"
    _optional_components = ["safety_checker", "feature_extractor", "image_encoder"]
    _exclude_from_cpu_offload = ["safety_checker"]
    _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]

    def __init__(
        self,
        vae: AutoencoderKL,
        text_encoder: CLIPTextModel,
        tokenizer: CLIPTokenizer,
        unet: UNet2DConditionModel,
        scheduler: KarrasDiffusionSchedulers,
        safety_checker: StableDiffusionSafetyChecker | None = None,
        feature_extractor: CLIPImageProcessor | None = None,
        image_encoder: CLIPVisionModelWithProjection | None = None,
        requires_safety_checker: bool = False,
        lora_root: str = "loras",
    ) -> None:
        super().__init__(
            vae=vae,
            text_encoder=text_encoder,
            tokenizer=tokenizer,
            unet=unet,
            scheduler=scheduler,
            safety_checker=safety_checker,
            feature_extractor=feature_extractor,
            image_encoder=image_encoder,
            requires_safety_checker=requires_safety_checker,
        )
        self.register_to_config(lora_root=lora_root, requires_safety_checker=requires_safety_checker)
        self.spatial_store = SpatialFeatureStore()
        self._loaded_scene: str | None = None

    def check_inputs(
        self,
        prompt: str | list[str] | None,
        height: int,
        width: int,
        callback_steps: int | None,
        negative_prompt: str | list[str] | None = None,
        prompt_embeds: torch.Tensor | None = None,
        negative_prompt_embeds: torch.Tensor | None = None,
        ip_adapter_image: PipelineImageInput | None = None,
        ip_adapter_image_embeds: list[torch.Tensor] | None = None,
        callback_on_step_end_tensor_inputs: list[str] | None = None,
        modalities: str | list[str] | None = None,
        scene: str | None = None,
        spatial_features: dict[str, Any] | None = None,
    ) -> None:
        r"""
        Validate standard Stable Diffusion arguments plus MMDiff modality options.

        Args:
            prompt (`str` or `list[str]`, *optional*):
                Text prompt. Required unless `prompt_embeds` is provided.
            height (`int`):
                Output height in pixels. Must be divisible by the VAE scale factor.
            width (`int`):
                Output width in pixels. Must be divisible by the VAE scale factor.
            callback_steps (`int`, *optional*):
                Deprecated callback interval forwarded to the parent checker.
            negative_prompt (`str` or `list[str]`, *optional*):
                Negative prompt used for classifier-free guidance.
            prompt_embeds (`torch.Tensor`, *optional*):
                Precomputed prompt embeddings.
            negative_prompt_embeds (`torch.Tensor`, *optional*):
                Precomputed negative prompt embeddings.
            ip_adapter_image (`PipelineImageInput`, *optional*):
                Optional IP-Adapter image.
            ip_adapter_image_embeds (`list[torch.Tensor]`, *optional*):
                Optional precomputed IP-Adapter embeddings.
            callback_on_step_end_tensor_inputs (`list[str]`, *optional*):
                Tensor names forwarded to step-end callbacks.
            modalities (`str` or `list[str]`, *optional*):
                Requested modalities; validated by `normalize_modalities`.
            scene (`str`, *optional*):
                LoRA scene name used for SAR/IR adapters.
            spatial_features (`dict`, *optional*):
                Previously captured OPT features.
        """

        super().check_inputs(
            prompt,
            height,
            width,
            callback_steps,
            negative_prompt,
            prompt_embeds,
            negative_prompt_embeds,
            ip_adapter_image,
            ip_adapter_image_embeds,
            callback_on_step_end_tensor_inputs,
        )
        if modalities is not None:
            normalize_modalities(modalities)
        if scene is not None and not isinstance(scene, str):
            raise TypeError(f"`scene` must be a string, got {type(scene)}.")
        if spatial_features is not None and not isinstance(spatial_features, dict):
            raise TypeError("`spatial_features` must be a dict produced by this pipeline.")

    def decode_latents(self, latents: torch.Tensor, single_channel: bool = False) -> torch.Tensor:
        r"""
        Decode latents with the VAE, optionally collapsing RGB to a single SAR/IR channel.

        Args:
            latents (`torch.Tensor`):
                Denoised latent tensor of shape `(batch, 4, h, w)`.
            single_channel (`bool`, *optional*, defaults to `False`):
                If `True`, average decoded RGB channels. This matches the original
                single-channel VAE decoder used for SAR and IR.

        Returns:
            `torch.Tensor`: Decoded images in `[-1, 1]`.
        """

        latents = latents / self.vae.config.scaling_factor
        image = self.vae.decode(latents, return_dict=False)[0]
        if single_channel:
            image = image.mean(dim=1, keepdim=True)
        return image

    def resolve_lora_dir(self, modality: str, scene: str, lora_path: str | Path | None = None) -> Path:
        r"""
        Resolve the directory that stores a scene-specific SAR or IR LoRA.

        Args:
            modality (`str`):
                `"sar"` or `"ir"`.
            scene (`str`):
                Scene name such as `"ship"` or `"beach"`.
            lora_path (`str` or `Path`, *optional*):
                Explicit override. When omitted, `{model_root}/{lora_root}/{modality}/{scene}` is used.

        Returns:
            `Path`: Directory expected to contain `pytorch_lora_weights.safetensors`.
        """

        if lora_path is not None:
            return Path(lora_path)
        root = Path(self.config.lora_root)
        if not root.is_absolute():
            base = getattr(self, "name_or_path", None) or "."
            root = Path(base) / root
        return root / modality / scene

    def load_scene_loras(
        self,
        scene: str,
        sar_lora_path: str | Path | None = None,
        ir_lora_path: str | Path | None = None,
    ) -> None:
        r"""
        Load SAR and IR LoRA adapters for `scene` as named PEFT adapters.

        Args:
            scene (`str`):
                Scene used to resolve default LoRA directories.
            sar_lora_path (`str` or `Path`, *optional*):
                Explicit SAR adapter directory or weight file.
            ir_lora_path (`str` or `Path`, *optional*):
                Explicit IR adapter directory or weight file.
        """

        if self._loaded_scene == scene and sar_lora_path is None and ir_lora_path is None:
            return

        if hasattr(self, "unload_lora_weights"):
            try:
                self.unload_lora_weights()
            except Exception:
                logger.debug("No previously loaded LoRA adapters to unload.")

        loaded = False
        for modality, path in (("sar", sar_lora_path), ("ir", ir_lora_path)):
            adapter_dir = self.resolve_lora_dir(modality, scene, path)
            weight_file = adapter_dir if adapter_dir.is_file() else adapter_dir / "pytorch_lora_weights.safetensors"
            if not Path(weight_file).is_file() and not adapter_dir.is_dir():
                logger.warning("Skipping %s LoRA for scene '%s'; missing path: %s", modality, scene, adapter_dir)
                continue
            load_target = adapter_dir if adapter_dir.is_dir() else adapter_dir.parent
            self.load_lora_weights(str(load_target), adapter_name=modality)
            loaded = True

        if loaded:
            self._loaded_scene = scene

    def _set_modality_adapter(self, modality: str) -> None:
        if modality == "opt":
            if hasattr(self, "disable_lora"):
                try:
                    self.disable_lora()
                except Exception:
                    logger.debug("LoRA disable skipped; no adapters are active.")
            return
        if hasattr(self, "set_adapters"):
            try:
                self.set_adapters(modality)
            except Exception as error:
                logger.warning("Could not activate the '%s' LoRA adapter: %s", modality, error)

    def _postprocess_image(
        self,
        image: torch.Tensor,
        output_type: str,
        single_channel: bool,
        dtype: torch.dtype,
        device: torch.device,
    ) -> Any:
        if output_type == "latent":
            return image
        if single_channel:
            image, has_nsfw = self.run_safety_checker(image.repeat(1, 3, 1, 1) if image.shape[1] == 1 else image, device, dtype)
            del has_nsfw
            image = image.mean(dim=1, keepdim=True)
            image = (image / 2 + 0.5).clamp(0, 1)
            image_np = image.cpu().permute(0, 2, 3, 1).float().numpy()
            if output_type == "np":
                return image_np[..., 0]
            if output_type == "pil":
                return [
                    self.numpy_to_pil(frame)[0].convert("L") if frame.ndim == 3 else self.numpy_to_pil(frame[..., None])[0]
                    for frame in image_np
                ]
            raise ValueError(f"Unknown output_type '{output_type}'. Use 'pil', 'np', or 'latent'.")

        image, has_nsfw_concept = self.run_safety_checker(image, device, dtype)
        do_denormalize = [True] * image.shape[0] if has_nsfw_concept is None else [not flag for flag in has_nsfw_concept]
        return self.image_processor.postprocess(image, output_type=output_type, do_denormalize=do_denormalize)

    def _denoise(
        self,
        prompt_embeds: torch.Tensor,
        timesteps: torch.Tensor,
        latents: torch.Tensor,
        extra_step_kwargs: dict[str, Any],
        timestep_cond: torch.Tensor | None,
        added_cond_kwargs: dict[str, Any] | None,
        store_mode: str,
        callback: Callable | None,
        callback_steps: int | None,
        callback_on_step_end: Callable | None,
        callback_on_step_end_tensor_inputs: list[str],
        num_inference_steps: int,
    ) -> torch.Tensor:
        store = self.spatial_store
        store.mode = store_mode
        num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
        self._num_timesteps = len(timesteps)

        with spatial_transfer_hooks(self.unet, store), self.progress_bar(total=num_inference_steps) as progress_bar:
            for i, t in enumerate(timesteps):
                if self.interrupt:
                    continue
                store.set_timestep(t)
                latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
                if hasattr(self.scheduler, "scale_model_input"):
                    latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)

                noise_pred = self.unet(
                    latent_model_input,
                    t,
                    encoder_hidden_states=prompt_embeds,
                    timestep_cond=timestep_cond,
                    cross_attention_kwargs=self.cross_attention_kwargs,
                    added_cond_kwargs=added_cond_kwargs,
                    return_dict=False,
                )[0]

                if self.do_classifier_free_guidance:
                    noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
                    noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond)
                    if self.guidance_rescale > 0.0:
                        noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=self.guidance_rescale)

                latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]

                if callback_on_step_end is not None:
                    callback_kwargs = {name: locals()[name] for name in callback_on_step_end_tensor_inputs if name in locals()}
                    callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
                    latents = callback_outputs.pop("latents", latents)
                    prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)

                if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
                    progress_bar.update()
                    if callback is not None and callback_steps is not None and i % callback_steps == 0:
                        step_idx = i // getattr(self.scheduler, "order", 1)
                        callback(step_idx, t, latents)

        store.mode = "off"
        return latents

    @torch.no_grad()
    @replace_example_docstring(EXAMPLE_DOC_STRING)
    def __call__(
        self,
        prompt: str | list[str] | None = None,
        height: int | None = None,
        width: int | None = None,
        num_inference_steps: int = 50,
        timesteps: list[int] | None = None,
        sigmas: list[float] | None = None,
        guidance_scale: float = 7.5,
        negative_prompt: str | list[str] | None = None,
        num_images_per_prompt: int | None = 1,
        eta: float = 0.0,
        generator: torch.Generator | list[torch.Generator] | None = None,
        latents: torch.Tensor | None = None,
        prompt_embeds: torch.Tensor | None = None,
        negative_prompt_embeds: torch.Tensor | None = None,
        ip_adapter_image: PipelineImageInput | None = None,
        ip_adapter_image_embeds: list[torch.Tensor] | None = None,
        output_type: str | None = "pil",
        return_dict: bool = True,
        cross_attention_kwargs: dict[str, Any] | None = None,
        guidance_rescale: float = 0.0,
        clip_skip: int | None = None,
        callback_on_step_end: Callable[..., Any] | None = None,
        callback_on_step_end_tensor_inputs: list[str] | None = None,
        modalities: str | list[str] = "all",
        scene: str = DEFAULT_SCENE,
        sar_lora_path: str | Path | None = None,
        ir_lora_path: str | Path | None = None,
        attn_layers: list[int] | tuple[int, ...] | None = None,
        resnet_layers: list[int] | tuple[int, ...] | None = None,
        resnet_time: float = 1.0,
        spatial_features: dict[str, Any] | None = None,
        return_spatial_features: bool = False,
        **kwargs: Any,
    ) -> MMDiffPipelineOutput | tuple:
        r"""
        Generate optical, SAR, and/or infrared images from one text prompt.

        The call follows the Stable Diffusion stage order: check inputs, define call
        parameters, encode the prompt, prepare timesteps, prepare latents, prepare extra
        step kwargs, then run the denoising loop. OPT is generated first so its spatial
        features can be transferred into the SAR and IR branches.

        Args:
            prompt (`str` or `list[str]`, *optional*):
                Text prompt that guides all requested modalities.
            height (`int`, *optional*, defaults to `256`):
                Output height in pixels. MMDiff was trained at 256×256.
            width (`int`, *optional*, defaults to `256`):
                Output width in pixels.
            num_inference_steps (`int`, *optional*, defaults to `50`):
                Number of denoising steps.
            timesteps (`list[int]`, *optional*):
                Custom descending timestep schedule.
            sigmas (`list[float]`, *optional*):
                Custom sigma schedule for compatible schedulers.
            guidance_scale (`float`, *optional*, defaults to `7.5`):
                Classifier-free guidance scale. Guidance is enabled when this value is `> 1`.
            negative_prompt (`str` or `list[str]`, *optional*):
                Prompt used for the unconditional branch. Defaults to empty strings.
            num_images_per_prompt (`int`, *optional*, defaults to `1`):
                Number of images drawn per prompt.
            eta (`float`, *optional*, defaults to `0.0`):
                DDIM eta. Ignored by schedulers that do not accept `eta`.
            generator (`torch.Generator` or `list[torch.Generator]`, *optional*):
                RNG used to sample the shared initial latents for every modality.
            latents (`torch.Tensor`, *optional*):
                Optional pre-sampled latents reused for every modality.
            prompt_embeds (`torch.Tensor`, *optional*):
                Precomputed prompt embeddings.
            negative_prompt_embeds (`torch.Tensor`, *optional*):
                Precomputed unconditional embeddings.
            ip_adapter_image (`PipelineImageInput`, *optional*):
                Optional IP-Adapter image condition.
            ip_adapter_image_embeds (`list[torch.Tensor]`, *optional*):
                Optional precomputed IP-Adapter embeddings.
            output_type (`str`, *optional*, defaults to `"pil"`):
                `"pil"`, `"np"`, or `"latent"`.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether to return [`MMDiffPipelineOutput`].
            cross_attention_kwargs (`dict`, *optional*):
                Extra kwargs forwarded to attention processors.
            guidance_rescale (`float`, *optional*, defaults to `0.0`):
                Optional guidance rescale factor.
            clip_skip (`int`, *optional*):
                Number of CLIP layers to skip when encoding prompts.
            callback_on_step_end (`Callable`, *optional*):
                Optional per-step callback.
            callback_on_step_end_tensor_inputs (`list[str]`, *optional*):
                Tensor names passed to `callback_on_step_end`.
            modalities (`str` or `list[str]`, *optional*, defaults to `"all"`):
                `"all"` or any subset of `"opt"`, `"sar"`, `"ir"`.
            scene (`str`, *optional*, defaults to `"ship"`):
                Scene used to resolve packaged SAR/IR LoRA adapters.
            sar_lora_path (`str` or `Path`, *optional*):
                Override for the SAR LoRA directory or weight file.
            ir_lora_path (`str` or `Path`, *optional*):
                Override for the IR LoRA directory or weight file.
            attn_layers (`list[int]`, *optional*):
                1-based up-block self-attention layers to transfer. Defaults to `1..9`.
            resnet_layers (`list[int]`, *optional*):
                0-based up-block ResNet layers to transfer. Defaults to `(2,)`.
            resnet_time (`float`, *optional*, defaults to `1.0`):
                Fraction of the early timestep schedule that receives ResNet injection.
            spatial_features (`dict`, *optional*):
                Features from a previous OPT run. When omitted, OPT is run first whenever
                SAR or IR generation needs transfer features.
            return_spatial_features (`bool`, *optional*, defaults to `False`):
                If `True`, include the captured OPT features in the output.

        Examples:

        Returns:
            [`MMDiffPipelineOutput`] or `tuple`:
                Generated images grouped by modality. `images` is the first requested modality.
        """

        callback = kwargs.pop("callback", None)
        callback_steps = kwargs.pop("callback_steps", None)
        callback_on_step_end_tensor_inputs = callback_on_step_end_tensor_inputs or ["latents"]
        requested = normalize_modalities(modalities)
        attn_layers = tuple(DEFAULT_ATTN_LAYERS if attn_layers is None else attn_layers)
        resnet_layers = tuple(DEFAULT_RESNET_LAYERS if resnet_layers is None else resnet_layers)

        height = DEFAULT_RESOLUTION if height is None else height
        width = DEFAULT_RESOLUTION if width is None else width

        # 1. Check inputs
        self.check_inputs(
            prompt,
            height,
            width,
            callback_steps,
            negative_prompt,
            prompt_embeds,
            negative_prompt_embeds,
            ip_adapter_image,
            ip_adapter_image_embeds,
            callback_on_step_end_tensor_inputs,
            modalities=requested,
            scene=scene,
            spatial_features=spatial_features,
        )

        self._guidance_scale = guidance_scale
        self._guidance_rescale = guidance_rescale
        self._clip_skip = clip_skip
        self._cross_attention_kwargs = cross_attention_kwargs
        self._interrupt = False

        # 2. Define call parameters
        if prompt is not None and isinstance(prompt, str):
            batch_size = 1
        elif prompt is not None and isinstance(prompt, list):
            batch_size = len(prompt)
        else:
            batch_size = prompt_embeds.shape[0]
        num_images_per_prompt = 1 if num_images_per_prompt is None else num_images_per_prompt
        device = self._execution_device

        needs_transfer = any(modality in requested for modality in ("sar", "ir"))
        run_opt = "opt" in requested or (needs_transfer and spatial_features is None)
        if needs_transfer and not run_opt and spatial_features is None:
            raise ValueError("SAR/IR generation requires OPT spatial features. Run OPT first or pass `spatial_features`.")

        if any(modality in requested for modality in ("sar", "ir")):
            self.load_scene_loras(scene, sar_lora_path=sar_lora_path, ir_lora_path=ir_lora_path)

        self.spatial_store.reset()
        self.spatial_store.attn_layers = set(attn_layers)
        self.spatial_store.resnet_layers = set(resnet_layers)
        if spatial_features is not None:
            self.spatial_store.load_state(spatial_features)

        # 3. Encode input condition
        lora_scale = self.cross_attention_kwargs.get("scale", None) if self.cross_attention_kwargs is not None else None
        prompt_embeds, negative_prompt_embeds = self.encode_prompt(
            prompt,
            device,
            num_images_per_prompt,
            self.do_classifier_free_guidance,
            negative_prompt,
            prompt_embeds=prompt_embeds,
            negative_prompt_embeds=negative_prompt_embeds,
            lora_scale=lora_scale,
            clip_skip=self.clip_skip,
        )
        if self.do_classifier_free_guidance:
            prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])

        added_cond_kwargs = None
        if ip_adapter_image is not None or ip_adapter_image_embeds is not None:
            image_embeds = self.prepare_ip_adapter_image_embeds(
                ip_adapter_image,
                ip_adapter_image_embeds,
                device,
                batch_size * num_images_per_prompt,
                self.do_classifier_free_guidance,
            )
            added_cond_kwargs = {"image_embeds": image_embeds}

        # 4. Prepare timesteps
        timesteps, num_inference_steps = retrieve_timesteps(
            self.scheduler, num_inference_steps, device, timesteps, sigmas
        )
        timestep_values = [int(step) for step in timesteps]
        self.spatial_store.inject_attn_timesteps = set(timestep_values)
        cutoff = max(1, int(len(timestep_values) * resnet_time)) if resnet_time > 0 else 0
        self.spatial_store.inject_resnet_timesteps = set(timestep_values[:cutoff])

        # 5. Prepare latent variables
        num_channels_latents = self.unet.config.in_channels
        init_latents = self.prepare_latents(
            batch_size * num_images_per_prompt,
            num_channels_latents,
            height,
            width,
            prompt_embeds.dtype,
            device,
            generator,
            latents,
        )

        # 6. Prepare extra step kwargs
        extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
        timestep_cond = None
        if getattr(self.unet.config, "time_cond_proj_dim", None) is not None:
            guidance_scale_tensor = torch.tensor(self.guidance_scale - 1).repeat(batch_size * num_images_per_prompt)
            timestep_cond = self.get_guidance_scale_embedding(
                guidance_scale_tensor, embedding_dim=self.unet.config.time_cond_proj_dim
            ).to(device=device, dtype=init_latents.dtype)

        # 7. Run denoising loop for each requested branch
        generated: dict[str, Any] = {}
        primary_nsfw = None
        for modality in (("opt",) if run_opt else ()) + tuple(item for item in requested if item != "opt"):
            self._set_modality_adapter(modality)
            store_mode = "save" if modality == "opt" else "inject"
            latents_in = init_latents.clone()
            latents_out = self._denoise(
                prompt_embeds=prompt_embeds,
                timesteps=timesteps,
                latents=latents_in,
                extra_step_kwargs=extra_step_kwargs,
                timestep_cond=timestep_cond,
                added_cond_kwargs=added_cond_kwargs,
                store_mode=store_mode,
                callback=callback,
                callback_steps=callback_steps,
                callback_on_step_end=callback_on_step_end,
                callback_on_step_end_tensor_inputs=callback_on_step_end_tensor_inputs,
                num_inference_steps=num_inference_steps,
            )
            if modality not in requested:
                continue
            if output_type == "latent":
                images = latents_out
                has_nsfw = None
            else:
                single_channel = modality in {"sar", "ir"}
                decoded = self.decode_latents(latents_out, single_channel=single_channel)
                if single_channel:
                    images = self._postprocess_image(
                        decoded, output_type=output_type, single_channel=True, dtype=prompt_embeds.dtype, device=device
                    )
                    has_nsfw = None
                else:
                    images, has_nsfw = self.run_safety_checker(decoded, device, prompt_embeds.dtype)
                    do_denormalize = [True] * images.shape[0] if has_nsfw is None else [not flag for flag in has_nsfw]
                    images = self.image_processor.postprocess(
                        images, output_type=output_type, do_denormalize=do_denormalize
                    )
            generated[modality] = images
            if primary_nsfw is None:
                primary_nsfw = has_nsfw

        self.maybe_free_model_hooks()
        feature_state = self.spatial_store.to_state() if return_spatial_features else None
        images = generated.get(requested[0])
        if not return_dict:
            return (images, primary_nsfw)

        return MMDiffPipelineOutput(
            images=images,
            opt=generated.get("opt"),
            sar=generated.get("sar"),
            ir=generated.get("ir"),
            nsfw_content_detected=primary_nsfw,
            spatial_features=feature_state,
        )


__all__ = ["MMDiffPipeline", "MMDiffPipelineOutput", "SpatialFeatureStore"]