ninjals commited on
Commit
80b6a6c
·
verified ·
1 Parent(s): b55773f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -1
app.py CHANGED
@@ -47,12 +47,51 @@ RERANK_COMMIT_HASH = "47e5a355d1a050c3e5f69d53f14964b1d34bcd9d"
47
  GENERATION_MODEL_ID = "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-FP8"
48
  FALLBACK_GEN_MODEL_ID = os.getenv("FALLBACK_GEN_MODEL_ID", "Qwen/Qwen2.5-7B-Instruct")
49
 
50
- ATTN_IMPL = "flash_attention_2" if os.getenv("USE_FA2", "0") == "1" else "sdpa"
51
 
52
  modality_to_tokens = {"image": 2048, "image_text": 10240, "text": 8192}
53
 
54
  PATH_TO_EMBEDDING_FILE = os.getenv("EMBEDDINGS_FILE", "image_text_embeddings_10k.safetensors")
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  # -----------------------------------------------------------------------------
57
  # Load dataset + embeddings (CPU only)
58
  # -----------------------------------------------------------------------------
 
47
  GENERATION_MODEL_ID = "nvidia/NVIDIA-Nemotron-3-Nano-30B-A3B-FP8"
48
  FALLBACK_GEN_MODEL_ID = os.getenv("FALLBACK_GEN_MODEL_ID", "Qwen/Qwen2.5-7B-Instruct")
49
 
50
+ # ATTN_IMPL = "flash_attention_2" if os.getenv("USE_FA2", "0") == "1" else "sdpa"
51
 
52
  modality_to_tokens = {"image": 2048, "image_text": 10240, "text": 8192}
53
 
54
  PATH_TO_EMBEDDING_FILE = os.getenv("EMBEDDINGS_FILE", "image_text_embeddings_10k.safetensors")
55
 
56
+
57
+ def check_flash_attention():
58
+ import torch
59
+ from transformers.utils import is_flash_attn_2_available
60
+
61
+ print(f"--- Flash Attention Check ---")
62
+ print(f"PyTorch version: {torch.__version__}")
63
+ print(f"CUDA available: {torch.cuda.is_available()}")
64
+
65
+ # Transformers helper check
66
+ fa2_available = is_flash_attn_2_available()
67
+ print(f"Transformers reports FA2 available: {fa2_available}")
68
+
69
+ if torch.cuda.is_available():
70
+ capability = torch.cuda.get_device_capability()
71
+ print(f"GPU Compute Capability: {capability}")
72
+ if capability[0] < 8:
73
+ print("Note: FA2 requires Compute Capability 8.0+ (Ampere or newer).")
74
+
75
+ return fa2_available
76
+
77
+ # Determine best implementation
78
+ if check_flash_attention():
79
+ ATTN_IMPL = "flash_attention_2"
80
+ else:
81
+ ATTN_IMPL = "sdpa" # Fallback to Scaled Dot Product Attention
82
+
83
+ print(f"[INFO] Using {ATTN_IMPL} for model loading.")
84
+
85
+ # model = AutoModelForCausalLM.from_pretrained(
86
+ # model_id,
87
+ # torch_dtype=torch.float16, # FA2 requires fp16 or bf16
88
+ # attn_implementation=best_attn,
89
+ # trust_remote_code=True
90
+ # ).to("cuda")
91
+
92
+ # Call it inside your setup or first GPU call
93
+ # FLASH_AVAILABLE = check_flash_attention()
94
+
95
  # -----------------------------------------------------------------------------
96
  # Load dataset + embeddings (CPU only)
97
  # -----------------------------------------------------------------------------