STBack23 commited on
Commit
0344903
·
verified ·
1 Parent(s): 94b2eb1

L4 24GB: max_memory CPU offload + sdpa default for model load

Browse files
src/omnisub/backends/transformers_qwen.py CHANGED
@@ -21,13 +21,15 @@ class TransformersQwenOmniBackend(LLMBackend):
21
  quant: str = "4bit",
22
  device_map: str = "auto",
23
  cache_dir: Optional[str] = None,
24
- attn_implementation: Optional[str] = "flash_attention_2",
 
25
  ):
26
  self.model_name = model_name
27
  self.quant = quant
28
  self.device_map = device_map
29
  self.cache_dir = cache_dir
30
  self.attn_implementation = attn_implementation
 
31
  self._model = None
32
  self._processor = None
33
 
@@ -51,14 +53,21 @@ class TransformersQwenOmniBackend(LLMBackend):
51
  bnb_4bit_compute_dtype=torch.bfloat16,
52
  bnb_4bit_quant_type="nf4",
53
  bnb_4bit_use_double_quant=True,
 
 
54
  )
55
 
56
  self._processor = Qwen3OmniMoeProcessor.from_pretrained(
57
  self.model_name, cache_dir=self.cache_dir
58
  )
59
 
60
- # Ưu tiên FlashAttention 2 (ít VRAM nhất); nếu không build được thì lùi
61
- # về sdpa rồi eager để vẫn chạy.
 
 
 
 
 
62
  seen: set = set()
63
  unique_attn: List[str] = []
64
  for a in (self.attn_implementation, "sdpa", "eager"):
@@ -74,17 +83,22 @@ class TransformersQwenOmniBackend(LLMBackend):
74
  self.model_name,
75
  torch_dtype=torch.bfloat16,
76
  device_map=self.device_map,
 
77
  quantization_config=quant_config,
78
  attn_implementation=attn,
79
  cache_dir=self.cache_dir,
 
80
  )
81
  self.attn_implementation = attn
82
  break
83
- except (ImportError, ValueError) as e:
84
  last_err = e
85
  continue
86
  if self._model is None:
87
- raise RuntimeError(f"Không nạp được model với mọi attn_implementation: {last_err}")
 
 
 
88
  # Chỉ cần text → tắt talker (sinh audio) để tiết kiệm ~10GB VRAM.
89
  disable = getattr(self._model, "disable_talker", None)
90
  if callable(disable):
 
21
  quant: str = "4bit",
22
  device_map: str = "auto",
23
  cache_dir: Optional[str] = None,
24
+ attn_implementation: Optional[str] = "sdpa",
25
+ gpu_memory_gib: float = 22.0,
26
  ):
27
  self.model_name = model_name
28
  self.quant = quant
29
  self.device_map = device_map
30
  self.cache_dir = cache_dir
31
  self.attn_implementation = attn_implementation
32
+ self.gpu_memory_gib = gpu_memory_gib
33
  self._model = None
34
  self._processor = None
35
 
 
53
  bnb_4bit_compute_dtype=torch.bfloat16,
54
  bnb_4bit_quant_type="nf4",
55
  bnb_4bit_use_double_quant=True,
56
+ # Cho phép một phần module tràn sang CPU khi nạp (cần trên L4 24GB).
57
+ llm_int8_enable_fp32_cpu_offload=True,
58
  )
59
 
60
  self._processor = Qwen3OmniMoeProcessor.from_pretrained(
61
  self.model_name, cache_dir=self.cache_dir
62
  )
63
 
64
+ # L4 24GB: model 4-bit + encoder audio/vision + talker (trước khi tắt)
65
+ # thường vượt VRAM → dùng max_memory để offload phần dư sang CPU.
66
+ max_memory = {
67
+ 0: f"{self.gpu_memory_gib:.0f}GiB",
68
+ "cpu": "100GiB",
69
+ }
70
+
71
  seen: set = set()
72
  unique_attn: List[str] = []
73
  for a in (self.attn_implementation, "sdpa", "eager"):
 
83
  self.model_name,
84
  torch_dtype=torch.bfloat16,
85
  device_map=self.device_map,
86
+ max_memory=max_memory,
87
  quantization_config=quant_config,
88
  attn_implementation=attn,
89
  cache_dir=self.cache_dir,
90
+ low_cpu_mem_usage=True,
91
  )
92
  self.attn_implementation = attn
93
  break
94
+ except (ImportError, ValueError, RuntimeError, OSError) as e:
95
  last_err = e
96
  continue
97
  if self._model is None:
98
+ raise RuntimeError(
99
+ f"Không nạp được model trên L4 {self.gpu_memory_gib:.0f}GB: {last_err}\n"
100
+ "Gợi ý: thử GPU lớn hơn (A100 40GB) hoặc checkpoint AWQ Int4 sẵn có."
101
+ ) from last_err
102
  # Chỉ cần text → tắt talker (sinh audio) để tiết kiệm ~10GB VRAM.
103
  disable = getattr(self._model, "disable_talker", None)
104
  if callable(disable):