Doron Adler
commited on
Commit
•
d9f299b
1
Parent(s):
1300df7
Use MPS device (via Pytorch for Apple silicon) when available instead of cpu
Browse files
app.py
CHANGED
@@ -30,8 +30,14 @@ def fixed_get_imports(filename: str | os.PathLike) -> list[str]:
|
|
30 |
@spaces.GPU
|
31 |
def get_device_type():
|
32 |
import torch
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
model_id = 'microsoft/Florence-2-base-ft'
|
36 |
|
37 |
import subprocess
|
@@ -40,11 +46,13 @@ if (device == "cuda"):
|
|
40 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
41 |
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
42 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
|
|
43 |
else:
|
44 |
#https://huggingface.co/microsoft/Florence-2-base-ft/discussions/4
|
45 |
with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports):
|
46 |
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
47 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
|
|
48 |
|
49 |
DESCRIPTION = "# [Florence-2 base-ft Demo with CPU inference support](https://huggingface.co/microsoft/Florence-2-base-ft)"
|
50 |
|
|
|
30 |
@spaces.GPU
|
31 |
def get_device_type():
|
32 |
import torch
|
33 |
+
if torch.cuda.is_available():
|
34 |
+
return "cuda"
|
35 |
+
else:
|
36 |
+
if (torch.backends.mps.is_available() and torch.backends.mps.is_built()):
|
37 |
+
return "mps"
|
38 |
+
else:
|
39 |
+
return "cpu"
|
40 |
+
|
41 |
model_id = 'microsoft/Florence-2-base-ft'
|
42 |
|
43 |
import subprocess
|
|
|
46 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
47 |
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
48 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
49 |
+
model.to(device)
|
50 |
else:
|
51 |
#https://huggingface.co/microsoft/Florence-2-base-ft/discussions/4
|
52 |
with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports):
|
53 |
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
54 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
55 |
+
model.to(device)
|
56 |
|
57 |
DESCRIPTION = "# [Florence-2 base-ft Demo with CPU inference support](https://huggingface.co/microsoft/Florence-2-base-ft)"
|
58 |
|