codeShare commited on
Commit
5e4894c
·
verified ·
1 Parent(s): d0297c8

Create modeling_flux2.py

Browse files
Files changed (1) hide show
  1. transformer/modeling_flux2.py +57 -0
transformer/modeling_flux2.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from diffusers import ModelMixin, ConfigMixin
4
+
5
+
6
+ class Flux2Transformer2DModel(ModelMixin, ConfigMixin):
7
+ config_name = "config.json"
8
+
9
+ def __init__(self, **kwargs):
10
+ super().__init__()
11
+
12
+ # Store config
13
+ self.register_to_config(**kwargs)
14
+
15
+ # Internal storage (safe for arbitrary keys)
16
+ self._sd = {}
17
+
18
+ # -----------------------------
19
+ # LOAD: accept ANY state dict
20
+ # -----------------------------
21
+ def load_state_dict(self, state_dict, strict=False):
22
+ """
23
+ Store raw tensors exactly as-is.
24
+ No validation, no structure assumptions.
25
+ """
26
+ self._sd = {}
27
+
28
+ for k, v in state_dict.items():
29
+ if isinstance(v, torch.Tensor):
30
+ self._sd[k] = v.contiguous()
31
+ else:
32
+ self._sd[k] = v
33
+
34
+ # Pretend everything matched
35
+ return torch.nn.modules.module._IncompatibleKeys([], [])
36
+
37
+ # -----------------------------
38
+ # SAVE: return original weights
39
+ # -----------------------------
40
+ def state_dict(self, *args, **kwargs):
41
+ return dict(self._sd)
42
+
43
+ # -----------------------------
44
+ # Required by diffusers internals
45
+ # -----------------------------
46
+ def _convert_deprecated_attention_blocks(self, state_dict):
47
+ # Diffusers sometimes calls this
48
+ return state_dict
49
+
50
+ # -----------------------------
51
+ # Forward (dummy)
52
+ # -----------------------------
53
+ def forward(self, *args, **kwargs):
54
+ raise RuntimeError(
55
+ "Flux2Transformer2DModel is a stub loader. "
56
+ "It cannot run inference."
57
+ )