Instructions to use UmeAiRT/ComfyUI-Auto-Installer-Assets with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use UmeAiRT/ComfyUI-Auto-Installer-Assets with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("UmeAiRT/ComfyUI-Auto-Installer-Assets", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Sync upload modeling_florence2.py
Browse files
models/LLM/Florence-2-large-PromptGen-v2.0/modeling_florence2.py
CHANGED
|
@@ -1796,6 +1796,12 @@ class Florence2Decoder(Florence2LanguagePreTrainedModel):
|
|
| 1796 |
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
|
| 1797 |
|
| 1798 |
# past_key_values_length
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1799 |
past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0
|
| 1800 |
|
| 1801 |
if inputs_embeds is None:
|
|
@@ -1939,7 +1945,7 @@ class Florence2Decoder(Florence2LanguagePreTrainedModel):
|
|
| 1939 |
|
| 1940 |
|
| 1941 |
class Florence2LanguageModel(Florence2LanguagePreTrainedModel):
|
| 1942 |
-
_tied_weights_keys =
|
| 1943 |
|
| 1944 |
def __init__(self, config: Florence2LanguageConfig):
|
| 1945 |
super().__init__(config)
|
|
@@ -1955,8 +1961,8 @@ class Florence2LanguageModel(Florence2LanguagePreTrainedModel):
|
|
| 1955 |
|
| 1956 |
def _tie_weights(self):
|
| 1957 |
if self.config.tie_word_embeddings:
|
| 1958 |
-
self.
|
| 1959 |
-
self.
|
| 1960 |
|
| 1961 |
def get_input_embeddings(self):
|
| 1962 |
return self.shared
|
|
@@ -2062,10 +2068,7 @@ class Florence2LanguageModel(Florence2LanguagePreTrainedModel):
|
|
| 2062 |
|
| 2063 |
class Florence2LanguageForConditionalGeneration(Florence2LanguagePreTrainedModel, GenerationMixin):
|
| 2064 |
base_model_prefix = "model"
|
| 2065 |
-
_tied_weights_keys =
|
| 2066 |
-
"model.decoder.embed_tokens.weight": "model.encoder.embed_tokens.weight",
|
| 2067 |
-
"lm_head.weight": "model.encoder.embed_tokens.weight"
|
| 2068 |
-
}
|
| 2069 |
_keys_to_ignore_on_load_missing = ["final_logits_bias"]
|
| 2070 |
|
| 2071 |
def __init__(self, config: Florence2LanguageConfig):
|
|
@@ -2077,6 +2080,10 @@ class Florence2LanguageForConditionalGeneration(Florence2LanguagePreTrainedModel
|
|
| 2077 |
# Initialize weights and apply final processing
|
| 2078 |
self.post_init()
|
| 2079 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2080 |
def get_encoder(self):
|
| 2081 |
return self.model.get_encoder()
|
| 2082 |
|
|
@@ -2199,7 +2206,10 @@ class Florence2LanguageForConditionalGeneration(Florence2LanguagePreTrainedModel
|
|
| 2199 |
):
|
| 2200 |
# cut decoder_input_ids if past_key_values is used
|
| 2201 |
if past_key_values is not None:
|
| 2202 |
-
|
|
|
|
|
|
|
|
|
|
| 2203 |
|
| 2204 |
# Some generation methods already pass only the last input ID
|
| 2205 |
if decoder_input_ids.shape[1] > past_length:
|
|
@@ -2228,6 +2238,9 @@ class Florence2LanguageForConditionalGeneration(Florence2LanguagePreTrainedModel
|
|
| 2228 |
|
| 2229 |
@staticmethod
|
| 2230 |
def _reorder_cache(past_key_values, beam_idx):
|
|
|
|
|
|
|
|
|
|
| 2231 |
reordered_past = ()
|
| 2232 |
for layer_past in past_key_values:
|
| 2233 |
# cached cross_attention states don't have to be reordered -> they are always the same
|
|
@@ -2336,6 +2349,17 @@ class Florence2PreTrainedModel(PreTrainedModel):
|
|
| 2336 |
supports_gradient_checkpointing = True
|
| 2337 |
_skip_keys_device_placement = "past_key_values"
|
| 2338 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2339 |
@property
|
| 2340 |
def _supports_flash_attn_2(self):
|
| 2341 |
"""
|
|
@@ -2491,8 +2515,12 @@ class Florence2VisionModelWithProjection(Florence2PreTrainedModel):
|
|
| 2491 |
if self.image_pos_embed is not None:
|
| 2492 |
x = x.view(batch_size * T, -1, x.shape[-1])
|
| 2493 |
num_tokens = x.shape[-2]
|
| 2494 |
-
h, w =
|
| 2495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2496 |
x = x.view(batch_size * T, h, w, x.shape[-1])
|
| 2497 |
pos_embed = self.image_pos_embed(x)
|
| 2498 |
x = x + pos_embed
|
|
@@ -2549,10 +2577,7 @@ class Florence2ForConditionalGeneration(Florence2PreTrainedModel, GenerationMixi
|
|
| 2549 |
language_model = Florence2LanguageForConditionalGeneration(config=config.text_config)
|
| 2550 |
|
| 2551 |
if language_model._tied_weights_keys is not None:
|
| 2552 |
-
|
| 2553 |
-
self._tied_weights_keys = {f"language_model.{k}": f"language_model.{v}" for k, v in language_model._tied_weights_keys.items()}
|
| 2554 |
-
else:
|
| 2555 |
-
self._tied_weights_keys = [f"language_model.{k}" for k in language_model._tied_weights_keys]
|
| 2556 |
self.language_model = language_model
|
| 2557 |
|
| 2558 |
self.pad_token_id = self.config.pad_token_id if self.config.pad_token_id is not None else -1
|
|
@@ -2614,8 +2639,12 @@ class Florence2ForConditionalGeneration(Florence2PreTrainedModel, GenerationMixi
|
|
| 2614 |
if self.image_pos_embed is not None:
|
| 2615 |
x = x.view(batch_size * T, -1, x.shape[-1])
|
| 2616 |
num_tokens = x.shape[-2]
|
| 2617 |
-
h, w =
|
| 2618 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2619 |
x = x.view(batch_size * T, h, w, x.shape[-1])
|
| 2620 |
pos_embed = self.image_pos_embed(x)
|
| 2621 |
x = x + pos_embed
|
|
@@ -2822,7 +2851,10 @@ class Florence2ForConditionalGeneration(Florence2PreTrainedModel, GenerationMixi
|
|
| 2822 |
):
|
| 2823 |
# cut decoder_input_ids if past_key_values is used
|
| 2824 |
if past_key_values is not None:
|
| 2825 |
-
|
|
|
|
|
|
|
|
|
|
| 2826 |
|
| 2827 |
# Some generation methods already pass only the last input ID
|
| 2828 |
if decoder_input_ids.shape[1] > past_length:
|
|
|
|
| 1796 |
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
|
| 1797 |
|
| 1798 |
# past_key_values_length
|
| 1799 |
+
if past_key_values is not None:
|
| 1800 |
+
if hasattr(past_key_values, "get_seq_length") and past_key_values.get_seq_length() == 0:
|
| 1801 |
+
past_key_values = None
|
| 1802 |
+
elif not isinstance(past_key_values, tuple):
|
| 1803 |
+
past_key_values = tuple(past_key_values)
|
| 1804 |
+
|
| 1805 |
past_key_values_length = past_key_values[0][0].shape[2] if past_key_values is not None else 0
|
| 1806 |
|
| 1807 |
if inputs_embeds is None:
|
|
|
|
| 1945 |
|
| 1946 |
|
| 1947 |
class Florence2LanguageModel(Florence2LanguagePreTrainedModel):
|
| 1948 |
+
_tied_weights_keys = None
|
| 1949 |
|
| 1950 |
def __init__(self, config: Florence2LanguageConfig):
|
| 1951 |
super().__init__(config)
|
|
|
|
| 1961 |
|
| 1962 |
def _tie_weights(self):
|
| 1963 |
if self.config.tie_word_embeddings:
|
| 1964 |
+
self.encoder.embed_tokens.weight = self.shared.weight
|
| 1965 |
+
self.decoder.embed_tokens.weight = self.shared.weight
|
| 1966 |
|
| 1967 |
def get_input_embeddings(self):
|
| 1968 |
return self.shared
|
|
|
|
| 2068 |
|
| 2069 |
class Florence2LanguageForConditionalGeneration(Florence2LanguagePreTrainedModel, GenerationMixin):
|
| 2070 |
base_model_prefix = "model"
|
| 2071 |
+
_tied_weights_keys = None
|
|
|
|
|
|
|
|
|
|
| 2072 |
_keys_to_ignore_on_load_missing = ["final_logits_bias"]
|
| 2073 |
|
| 2074 |
def __init__(self, config: Florence2LanguageConfig):
|
|
|
|
| 2080 |
# Initialize weights and apply final processing
|
| 2081 |
self.post_init()
|
| 2082 |
|
| 2083 |
+
def _tie_weights(self):
|
| 2084 |
+
if self.config.tie_word_embeddings:
|
| 2085 |
+
self.lm_head.weight = self.model.shared.weight
|
| 2086 |
+
|
| 2087 |
def get_encoder(self):
|
| 2088 |
return self.model.get_encoder()
|
| 2089 |
|
|
|
|
| 2206 |
):
|
| 2207 |
# cut decoder_input_ids if past_key_values is used
|
| 2208 |
if past_key_values is not None:
|
| 2209 |
+
if hasattr(past_key_values, "get_seq_length"):
|
| 2210 |
+
past_length = past_key_values.get_seq_length()
|
| 2211 |
+
else:
|
| 2212 |
+
past_length = past_key_values[0][0].shape[2]
|
| 2213 |
|
| 2214 |
# Some generation methods already pass only the last input ID
|
| 2215 |
if decoder_input_ids.shape[1] > past_length:
|
|
|
|
| 2238 |
|
| 2239 |
@staticmethod
|
| 2240 |
def _reorder_cache(past_key_values, beam_idx):
|
| 2241 |
+
if hasattr(past_key_values, "reorder_cache"):
|
| 2242 |
+
past_key_values.reorder_cache(beam_idx)
|
| 2243 |
+
return past_key_values
|
| 2244 |
reordered_past = ()
|
| 2245 |
for layer_past in past_key_values:
|
| 2246 |
# cached cross_attention states don't have to be reordered -> they are always the same
|
|
|
|
| 2349 |
supports_gradient_checkpointing = True
|
| 2350 |
_skip_keys_device_placement = "past_key_values"
|
| 2351 |
|
| 2352 |
+
@property
|
| 2353 |
+
def device(self) -> torch.device:
|
| 2354 |
+
try:
|
| 2355 |
+
return next(self.parameters()).device
|
| 2356 |
+
except StopIteration:
|
| 2357 |
+
return torch.device("cpu")
|
| 2358 |
+
|
| 2359 |
+
@device.setter
|
| 2360 |
+
def device(self, value):
|
| 2361 |
+
pass
|
| 2362 |
+
|
| 2363 |
@property
|
| 2364 |
def _supports_flash_attn_2(self):
|
| 2365 |
"""
|
|
|
|
| 2515 |
if self.image_pos_embed is not None:
|
| 2516 |
x = x.view(batch_size * T, -1, x.shape[-1])
|
| 2517 |
num_tokens = x.shape[-2]
|
| 2518 |
+
h, w = H // 32, W // 32
|
| 2519 |
+
if h * w != num_tokens:
|
| 2520 |
+
aspect_ratio = H / W
|
| 2521 |
+
w = int(round((num_tokens / aspect_ratio) ** 0.5))
|
| 2522 |
+
h = num_tokens // w
|
| 2523 |
+
assert h * w == num_tokens, f'failed to resolve feature map shape: num_tokens={num_tokens}, H={H}, W={W}'
|
| 2524 |
x = x.view(batch_size * T, h, w, x.shape[-1])
|
| 2525 |
pos_embed = self.image_pos_embed(x)
|
| 2526 |
x = x + pos_embed
|
|
|
|
| 2577 |
language_model = Florence2LanguageForConditionalGeneration(config=config.text_config)
|
| 2578 |
|
| 2579 |
if language_model._tied_weights_keys is not None:
|
| 2580 |
+
self._tied_weights_keys = None
|
|
|
|
|
|
|
|
|
|
| 2581 |
self.language_model = language_model
|
| 2582 |
|
| 2583 |
self.pad_token_id = self.config.pad_token_id if self.config.pad_token_id is not None else -1
|
|
|
|
| 2639 |
if self.image_pos_embed is not None:
|
| 2640 |
x = x.view(batch_size * T, -1, x.shape[-1])
|
| 2641 |
num_tokens = x.shape[-2]
|
| 2642 |
+
h, w = H // 32, W // 32
|
| 2643 |
+
if h * w != num_tokens:
|
| 2644 |
+
aspect_ratio = H / W
|
| 2645 |
+
w = int(round((num_tokens / aspect_ratio) ** 0.5))
|
| 2646 |
+
h = num_tokens // w
|
| 2647 |
+
assert h * w == num_tokens, f'failed to resolve feature map shape: num_tokens={num_tokens}, H={H}, W={W}'
|
| 2648 |
x = x.view(batch_size * T, h, w, x.shape[-1])
|
| 2649 |
pos_embed = self.image_pos_embed(x)
|
| 2650 |
x = x + pos_embed
|
|
|
|
| 2851 |
):
|
| 2852 |
# cut decoder_input_ids if past_key_values is used
|
| 2853 |
if past_key_values is not None:
|
| 2854 |
+
if hasattr(past_key_values, "get_seq_length"):
|
| 2855 |
+
past_length = past_key_values.get_seq_length()
|
| 2856 |
+
else:
|
| 2857 |
+
past_length = past_key_values[0][0].shape[2]
|
| 2858 |
|
| 2859 |
# Some generation methods already pass only the last input ID
|
| 2860 |
if decoder_input_ids.shape[1] > past_length:
|