sitammeur commited on
Commit
40cf531
·
verified ·
1 Parent(s): 38637d5

Update src/florence/model.py

Browse files
Files changed (1) hide show
  1. src/florence/model.py +25 -28
src/florence/model.py CHANGED
@@ -1,12 +1,11 @@
1
  # Importing necessary libraries
2
- import os
3
  import sys
4
- import importlib
5
- import subprocess
6
  from typing import Optional
 
7
  from PIL import Image
8
  import torch
9
  from transformers import AutoProcessor, AutoModelForCausalLM
 
10
  import spaces
11
  import gradio as gr
12
 
@@ -15,36 +14,34 @@ from src.logger import logging
15
  from src.exception import CustomExceptionHandling
16
 
17
 
18
- # Install the required dependencies
19
- # NOTE: passing env={...} to subprocess.run REPLACES the entire environment
20
- # instead of extending it, which strips PATH/HOME/etc. Copy the current
21
- # environment first and only add/override the flag we need.
22
- env = os.environ.copy()
23
- env["FLASH_ATTENTION_SKIP_CUDA_BUILD"] = "TRUE"
24
- subprocess.run(
25
- "pip install flash-attn --no-build-isolation",
26
- env=env,
27
- shell=True,
28
- check=True,
29
- )
 
 
 
 
30
 
31
- # The pip install above happens in a subprocess AFTER this Python process
32
- # has already started, so the interpreter's import machinery has a stale
33
- # view of what's on disk (sys.path_importer_cache). Without invalidating
34
- # it, transformers' internal check_imports()/find_spec() lookup for
35
- # flash_attn reports "not found" even though it just installed
36
- # successfully -- this is what was causing the ImportError at model load.
37
- importlib.invalidate_caches()
38
 
39
  # Load model and processor from Hugging Face
40
  model_id = "microsoft/Florence-2-large-ft"
41
  try:
42
- model = (
43
- AutoModelForCausalLM.from_pretrained(model_id, attn_implementation="eager", torch_dtype=torch.float16, trust_remote_code=True)
44
- .to("cuda")
45
- .eval()
46
- )
47
- processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
 
48
  logging.info("Model and processor loaded successfully.")
49
 
50
  # Handle exceptions that may occur during the process
 
1
  # Importing necessary libraries
 
2
  import sys
 
 
3
  from typing import Optional
4
+ from unittest.mock import patch
5
  from PIL import Image
6
  import torch
7
  from transformers import AutoProcessor, AutoModelForCausalLM
8
+ from transformers.dynamic_module_utils import get_imports
9
  import spaces
10
  import gradio as gr
11
 
 
14
  from src.exception import CustomExceptionHandling
15
 
16
 
17
+ # Florence-2's custom modeling file (modeling_florence2.py) unconditionally
18
+ # lists "flash_attn" as a required import, even though we load the model
19
+ # with attn_implementation="eager" (which never touches flash_attn).
20
+ # Actually installing a working flash-attn build inside a Space is slow
21
+ # and often fails outright, since it needs a prebuilt wheel that exactly
22
+ # matches the image's torch/CUDA/Python ABI -- if no match exists, pip
23
+ # silently produces a non-functional stub package, which still leaves
24
+ # the model unable to load. Since flash_attn isn't actually needed here,
25
+ # the standard fix is to patch transformers' import-checker to drop
26
+ # "flash_attn" from the required-imports list, only for this one file.
27
+ # Reference: https://huggingface.co/microsoft/Florence-2-large-ft/discussions/4
28
+ def _fixed_get_imports(filename):
29
+ imports = get_imports(filename)
30
+ if str(filename).endswith("/modeling_florence2.py") and "flash_attn" in imports:
31
+ imports.remove("flash_attn")
32
+ return imports
33
 
 
 
 
 
 
 
 
34
 
35
  # Load model and processor from Hugging Face
36
  model_id = "microsoft/Florence-2-large-ft"
37
  try:
38
+ with patch("transformers.dynamic_module_utils.get_imports", _fixed_get_imports):
39
+ model = (
40
+ AutoModelForCausalLM.from_pretrained(model_id, attn_implementation="eager", torch_dtype=torch.float16, trust_remote_code=True)
41
+ .to("cuda")
42
+ .eval()
43
+ )
44
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
45
  logging.info("Model and processor loaded successfully.")
46
 
47
  # Handle exceptions that may occur during the process