ginipick commited on
Commit
cad5d49
ยท
verified ยท
1 Parent(s): dff9eba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -28,13 +28,15 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # ๋ช…
28
 
29
  ###--------------ZERO GPU ํ•„์ˆ˜/ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๊ณตํ†ต --------------------###
30
  def clear_memory():
 
31
  gc.collect()
32
  if torch.cuda.is_available():
33
  try:
34
- torch.cuda.empty_cache()
35
- torch.cuda.synchronize()
36
- except:
37
- pass
 
38
 
39
  ###---------------------------------------------------------------
40
 
@@ -92,21 +94,33 @@ gd_model = GroundingDinoForObjectDetection.from_pretrained(gd_model_path, torch_
92
  gd_model = gd_model.to(device=device)
93
  assert isinstance(gd_model, GroundingDinoForObjectDetection)
94
 
95
- # ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™” ๋ถ€๋ถ„๋„ ์ˆ˜์ •
96
  pipe = FluxPipeline.from_pretrained(
97
  "black-forest-labs/FLUX.1-dev",
98
  torch_dtype=torch.float16,
99
  use_auth_token=HF_TOKEN
100
  )
101
 
102
- # ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์„ฑ๋Šฅ ์ตœ์ ํ™” ์„ค์ •
103
- pipe.enable_attention_slicing()
104
- pipe.enable_vae_slicing()
105
- pipe.enable_xformers_memory_efficient_attention() # xformers๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋‹ค๋ฉด
 
 
 
 
 
106
 
 
107
  if torch.cuda.is_available():
108
- pipe = pipe.to("cuda")
109
- pipe.enable_model_cpu_offload() # ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ ์œ„ํ•œ CPU ์˜คํ”„๋กœ๋“œ
 
 
 
 
 
 
110
 
111
 
112
  # LoRA ๊ฐ€์ค‘์น˜ ๋กœ๋“œ
 
28
 
29
  ###--------------ZERO GPU ํ•„์ˆ˜/ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๊ณตํ†ต --------------------###
30
  def clear_memory():
31
+ """๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ ํ•จ์ˆ˜"""
32
  gc.collect()
33
  if torch.cuda.is_available():
34
  try:
35
+ with torch.cuda.device('cuda:0'):
36
+ torch.cuda.empty_cache()
37
+ torch.cuda.synchronize()
38
+ except Exception as e:
39
+ print(f"Warning: Could not clear CUDA memory: {e}")
40
 
41
  ###---------------------------------------------------------------
42
 
 
94
  gd_model = gd_model.to(device=device)
95
  assert isinstance(gd_model, GroundingDinoForObjectDetection)
96
 
97
+ # ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™” ๋ฐ ์ตœ์ ํ™” ์„ค์ •
98
  pipe = FluxPipeline.from_pretrained(
99
  "black-forest-labs/FLUX.1-dev",
100
  torch_dtype=torch.float16,
101
  use_auth_token=HF_TOKEN
102
  )
103
 
104
+ # ๋ฉ”๋ชจ๋ฆฌ ์ตœ์ ํ™” ์„ค์ • - FluxPipeline์—์„œ ์ง€์›ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋งŒ ์‚ฌ์šฉ
105
+ pipe.enable_attention_slicing(slice_size="auto")
106
+
107
+ # xformers ์ตœ์ ํ™” (์„ค์น˜๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ)
108
+ try:
109
+ import xformers
110
+ pipe.enable_xformers_memory_efficient_attention()
111
+ except ImportError:
112
+ print("xformers is not installed. Skipping memory efficient attention.")
113
 
114
+ # GPU ์„ค์ •
115
  if torch.cuda.is_available():
116
+ try:
117
+ pipe = pipe.to("cuda:0")
118
+ # CPU ์˜คํ”„๋กœ๋”ฉ์ด ์ง€์›๋˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ํ™œ์„ฑํ™”
119
+ if hasattr(pipe, 'enable_model_cpu_offload'):
120
+ pipe.enable_model_cpu_offload()
121
+ except Exception as e:
122
+ print(f"Warning: Could not move pipeline to CUDA: {str(e)}")
123
+
124
 
125
 
126
  # LoRA ๊ฐ€์ค‘์น˜ ๋กœ๋“œ