(self)
| 107 | return FluxControlNetStateDictConverter() |
| 108 | |
| 109 | def quantize(self): |
| 110 | def cast_to(weight, dtype=None, device=None, copy=False): |
| 111 | if device is None or weight.device == device: |
| 112 | if not copy: |
| 113 | if dtype is None or weight.dtype == dtype: |
| 114 | return weight |
| 115 | return weight.to(dtype=dtype, copy=copy) |
| 116 | |
| 117 | r = torch.empty_like(weight, dtype=dtype, device=device) |
| 118 | r.copy_(weight) |
| 119 | return r |
| 120 | |
| 121 | def cast_weight(s, input=None, dtype=None, device=None): |
| 122 | if input is not None: |
| 123 | if dtype is None: |
| 124 | dtype = input.dtype |
| 125 | if device is None: |
| 126 | device = input.device |
| 127 | weight = cast_to(s.weight, dtype, device) |
| 128 | return weight |
| 129 | |
| 130 | def cast_bias_weight(s, input=None, dtype=None, device=None, bias_dtype=None): |
| 131 | if input is not None: |
| 132 | if dtype is None: |
| 133 | dtype = input.dtype |
| 134 | if bias_dtype is None: |
| 135 | bias_dtype = dtype |
| 136 | if device is None: |
| 137 | device = input.device |
| 138 | bias = None |
| 139 | weight = cast_to(s.weight, dtype, device) |
| 140 | bias = cast_to(s.bias, bias_dtype, device) |
| 141 | return weight, bias |
| 142 | |
| 143 | class quantized_layer: |
| 144 | class QLinear(torch.nn.Linear): |
| 145 | def __init__(self, *args, **kwargs): |
| 146 | super().__init__(*args, **kwargs) |
| 147 | |
| 148 | def forward(self,input,**kwargs): |
| 149 | weight,bias= cast_bias_weight(self,input) |
| 150 | return torch.nn.functional.linear(input,weight,bias) |
| 151 | |
| 152 | class QRMSNorm(torch.nn.Module): |
| 153 | def __init__(self, module): |
| 154 | super().__init__() |
| 155 | self.module = module |
| 156 | |
| 157 | def forward(self,hidden_states,**kwargs): |
| 158 | weight= cast_weight(self.module,hidden_states) |
| 159 | input_dtype = hidden_states.dtype |
| 160 | variance = hidden_states.to(torch.float32).square().mean(-1, keepdim=True) |
| 161 | hidden_states = hidden_states * torch.rsqrt(variance + self.module.eps) |
| 162 | hidden_states = hidden_states.to(input_dtype) * weight |
| 163 | return hidden_states |
| 164 | |
| 165 | class QEmbedding(torch.nn.Embedding): |
| 166 | def __init__(self, *args, **kwargs): |
nothing calls this directly
no outgoing calls
no test coverage detected