Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add apple silicon GPU acceleration #6151

Merged
merged 24 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions .github/workflows/tests_preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ jobs:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install Haystack
run: |
pip install .[dev,preview] langdetect transformers[torch,sentencepiece]==4.34.1 'sentence-transformers>=2.2.0' pypdf tika 'azure-ai-formrecognizer>=3.2.0b2'
pip install --no-deps llvmlite numba 'openai-whisper>=20230918' # prevent outdated version of tiktoken pinned by openai-whisper
run: pip install .[dev,preview] langdetect transformers[torch,sentencepiece]==4.32.1 'sentence-transformers>=2.2.0' pypdf openai-whisper tika 'azure-ai-formrecognizer>=3.2.0b2'

- name: Run
run: pytest -m "unit" test/preview
Expand Down Expand Up @@ -154,7 +152,6 @@ jobs:
- "branch:${{ github.ref_name }}"
- "url:https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"


integration-tests-linux:
name: Integration / ubuntu-latest
needs: unit-tests
Expand All @@ -177,9 +174,7 @@ jobs:
sudo apt install ffmpeg # for local Whisper tests

- name: Install Haystack
run: |
pip install .[dev,preview] langdetect transformers[torch,sentencepiece]==4.34.1 'sentence-transformers>=2.2.0' pypdf tika 'azure-ai-formrecognizer>=3.2.0b2'
pip install --no-deps llvmlite numba 'openai-whisper>=20230918' # prevent outdated version of tiktoken pinned by openai-whisper
run: pip install .[dev,preview] langdetect transformers[torch,sentencepiece]==4.32.1 'sentence-transformers>=2.2.0' pypdf openai-whisper tika 'azure-ai-formrecognizer>=3.2.0b2'
NripeshN marked this conversation as resolved.
Show resolved Hide resolved

- name: Run
run: pytest --maxfail=5 -m "integration" test/preview
Expand Down Expand Up @@ -215,11 +210,12 @@ jobs:
- "branch:${{ github.ref_name }}"
- "url:https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"


integration-tests-macos:
name: Integration / macos-latest
needs: unit-tests
runs-on: macos-latest
env:
PYTORCH_ENABLE_MPS_FALLBACK: 1
steps:
- uses: actions/checkout@v4

Expand All @@ -234,9 +230,7 @@ jobs:
colima start

- name: Install Haystack
run: |
pip install .[dev,preview] langdetect transformers[torch,sentencepiece]==4.34.1 'sentence-transformers>=2.2.0' pypdf tika 'azure-ai-formrecognizer>=3.2.0b2'
pip install --no-deps llvmlite numba 'openai-whisper>=20230918' # prevent outdated version of tiktoken pinned by openai-whisper
run: pip install .[dev,preview] langdetect transformers[torch,sentencepiece]==4.32.1 'sentence-transformers>=2.2.0' pypdf openai-whisper tika 'azure-ai-formrecognizer>=3.2.0b2'

- name: Run Tika
run: docker run -d -p 9998:9998 apache/tika:2.9.0.0
Expand Down Expand Up @@ -275,7 +269,6 @@ jobs:
- "branch:${{ github.ref_name }}"
- "url:https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"


integration-tests-windows:
name: Integration / windows-latest
needs: unit-tests
Expand All @@ -288,9 +281,7 @@ jobs:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install Haystack
run: |
pip install .[dev,preview] langdetect transformers[torch,sentencepiece]==4.34.1 'sentence-transformers>=2.2.0' pypdf tika 'azure-ai-formrecognizer>=3.2.0b2'
pip install --no-deps llvmlite numba 'openai-whisper>=20230918' # prevent outdated version of tiktoken pinned by openai-whisper
run: pip install .[dev,preview] langdetect transformers[torch,sentencepiece]==4.32.1 'sentence-transformers>=2.2.0' pypdf openai-whisper tika 'azure-ai-formrecognizer>=3.2.0b2'

- name: Run
run: pytest --maxfail=5 -m "integration" test/preview -k 'not tika'
Expand Down
6 changes: 6 additions & 0 deletions e2e/modeling/test_dpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ def test_dpr_processor_save_load_non_bert_tokenizer(tmp_path: Path, query_and_pa

if torch.cuda.is_available():
device = torch.device("cuda")
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
model = BiAdaptiveModel(
Expand Down Expand Up @@ -753,6 +755,8 @@ def test_dpr_processor_save_load_non_bert_tokenizer(tmp_path: Path, query_and_pa

if torch.cuda.is_available():
device = torch.device("cuda")
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
loaded_model = BiAdaptiveModel(
Expand Down Expand Up @@ -879,6 +883,8 @@ def test_dpr_processor_save_load_non_bert_tokenizer(tmp_path: Path, query_and_pa

if torch.cuda.is_available():
device = torch.device("cuda")
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
model = BiAdaptiveModel(
Expand Down
3 changes: 2 additions & 1 deletion haystack/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ def collect_static_system_specs() -> Dict[str, Any]:

try:
torch_import.check()
has_mps = hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
specs.update(
{
"libraries.torch": torch.__version__,
"libraries.cuda": torch.version.cuda if torch.cuda.is_available() else False,
"hardware.gpus": torch.cuda.device_count() if torch.cuda.is_available() else 0,
"hardware.gpus": torch.cuda.device_count() if torch.cuda.is_available() else 1 if has_mps else 0,
}
)
except ImportError:
Expand Down
4 changes: 4 additions & 0 deletions haystack/modeling/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def initialize_device_settings(
else:
devices_to_use = [torch.device("cuda:0")]
n_gpu = 1
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
devices_to_use = [torch.device("mps")]
n_gpu = 1
else:
devices_to_use = [torch.device("cpu")]
n_gpu = 0
Expand Down Expand Up @@ -180,6 +183,7 @@ def all_gather_list(data, group=None, max_size=16384):
data (Any): data from the local worker to be gathered on other workers
group (optional): group of the collective
"""
# pylint: disable=all
SIZE_STORAGE_BYTES = 4 # int32 to encode the payload size

enc = pickle.dumps(data)
Expand Down
2 changes: 2 additions & 0 deletions haystack/preview/components/readers/extractive.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def warm_up(self):
if self.model is None:
if torch.cuda.is_available():
self.device = self.device or "cuda:0"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
self.device = self.device or "mps:0"
else:
self.device = self.device or "cpu:0"
self.model = AutoModelForQuestionAnswering.from_pretrained(self.model_name_or_path, token=self.token).to(
Expand Down
3 changes: 2 additions & 1 deletion haystack/utils/experiment_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def get_or_create_env_meta_data() -> Dict[str, Any]:
from haystack.telemetry import HAYSTACK_EXECUTION_CONTEXT

global env_meta_data # pylint: disable=global-statement
has_mps = hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
if not env_meta_data:
env_meta_data = {
"os_version": platform.release(),
Expand All @@ -246,7 +247,7 @@ def get_or_create_env_meta_data() -> Dict[str, Any]:
"transformers_version": transformers.__version__,
"torch_version": torch.__version__,
"torch_cuda_version": torch.version.cuda if torch.cuda.is_available() else 0,
"n_gpu": torch.cuda.device_count() if torch.cuda.is_available() else 0,
"n_gpu": torch.cuda.device_count() if torch.cuda.is_available() else 1 if has_mps else 0,
"n_cpu": os.cpu_count(),
"context": os.environ.get(HAYSTACK_EXECUTION_CONTEXT),
"execution_env": _get_execution_environment(),
Expand Down
2 changes: 2 additions & 0 deletions haystack/utils/torch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ def get_devices(devices: Optional[List[Union[str, torch.device]]]) -> List[torch
return [torch.device(device) for device in devices]
elif torch.cuda.is_available():
return [torch.device(device) for device in range(torch.cuda.device_count())]
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return [torch.device("mps")]
return [torch.device("cpu")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
- |
Added support for Apple Silicon GPU acceleration through "mps pytorch", enabling better performance on Apple M1 hardware.
Loading