[docs] add xpu device check (#34684)
* add XPU path * use accelerate API * Update docs/source/en/tasks/semantic_segmentation.md Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com> * update more places with accelerate API --------- Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
This commit is contained in:
@@ -508,10 +508,11 @@ See the following examples for DoLa decoding with the 32-layer LLaMA-7B model.
|
|||||||
```python
|
```python
|
||||||
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
|
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
|
||||||
>>> import torch
|
>>> import torch
|
||||||
|
>>> from accelerate.test_utils.testing import get_backend
|
||||||
|
|
||||||
>>> tokenizer = AutoTokenizer.from_pretrained("huggyllama/llama-7b")
|
>>> tokenizer = AutoTokenizer.from_pretrained("huggyllama/llama-7b")
|
||||||
>>> model = AutoModelForCausalLM.from_pretrained("huggyllama/llama-7b", torch_dtype=torch.float16)
|
>>> model = AutoModelForCausalLM.from_pretrained("huggyllama/llama-7b", torch_dtype=torch.float16)
|
||||||
>>> device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
>>> device, _, _ = get_backend() # automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
>>> model.to(device)
|
>>> model.to(device)
|
||||||
>>> set_seed(42)
|
>>> set_seed(42)
|
||||||
|
|
||||||
|
|||||||
@@ -386,9 +386,9 @@ The use and prompting for the conversational use is very similar to using the ba
|
|||||||
```py
|
```py
|
||||||
>>> import torch
|
>>> import torch
|
||||||
>>> from transformers import IdeficsForVisionText2Text, AutoProcessor
|
>>> from transformers import IdeficsForVisionText2Text, AutoProcessor
|
||||||
|
>>> from accelerate.test_utils.testing import get_backend
|
||||||
|
|
||||||
>>> device = "cuda" if torch.cuda.is_available() else "cpu"
|
>>> device, _, _ = get_backend() # automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
|
|
||||||
>>> checkpoint = "HuggingFaceM4/idefics-9b-instruct"
|
>>> checkpoint = "HuggingFaceM4/idefics-9b-instruct"
|
||||||
>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16).to(device)
|
>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16).to(device)
|
||||||
>>> processor = AutoProcessor.from_pretrained(checkpoint)
|
>>> processor = AutoProcessor.from_pretrained(checkpoint)
|
||||||
|
|||||||
@@ -256,8 +256,9 @@ image
|
|||||||
Prepare image for the model.
|
Prepare image for the model.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
device = "cuda" if torch.cuda.is_available() else "cpu"
|
from accelerate.test_utils.testing import get_backend
|
||||||
|
# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
|
device, _, _ = get_backend()
|
||||||
inputs = processor(images=image, return_tensors="pt").to(device)
|
inputs = processor(images=image, return_tensors="pt").to(device)
|
||||||
pixel_values = inputs.pixel_values
|
pixel_values = inputs.pixel_values
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -43,8 +43,9 @@ Let's see the pipeline in action. First, initialize the pipeline. If you don't p
|
|||||||
```python
|
```python
|
||||||
import torch
|
import torch
|
||||||
from transformers import pipeline
|
from transformers import pipeline
|
||||||
|
from accelerate.test_utils.testing import get_backend
|
||||||
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
|
DEVICE, _, _ = get_backend()
|
||||||
pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-384", device=DEVICE, pool=True)
|
pipe = pipeline(task="image-feature-extraction", model_name="google/vit-base-patch16-384", device=DEVICE, pool=True)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -37,8 +37,9 @@ We can now initialize the pipeline with a [Swin2SR model](https://huggingface.co
|
|||||||
```python
|
```python
|
||||||
from transformers import pipeline
|
from transformers import pipeline
|
||||||
import torch
|
import torch
|
||||||
|
from accelerate.test_utils.testing import get_backend
|
||||||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
|
device, _, _ = get_backend()
|
||||||
pipe = pipeline(task="image-to-image", model="caidas/swin2SR-lightweight-x2-64", device=device)
|
pipe = pipeline(task="image-to-image", model="caidas/swin2SR-lightweight-x2-64", device=device)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ from transformers import TrainingArguments, Trainer
|
|||||||
import torch
|
import torch
|
||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
import torch.nn.functional as F
|
import torch.nn.functional as F
|
||||||
|
from accelerate.test_utils.testing import get_backend
|
||||||
|
|
||||||
class ImageDistilTrainer(Trainer):
|
class ImageDistilTrainer(Trainer):
|
||||||
def __init__(self, teacher_model=None, student_model=None, temperature=None, lambda_param=None, *args, **kwargs):
|
def __init__(self, teacher_model=None, student_model=None, temperature=None, lambda_param=None, *args, **kwargs):
|
||||||
@@ -66,7 +66,7 @@ class ImageDistilTrainer(Trainer):
|
|||||||
self.teacher = teacher_model
|
self.teacher = teacher_model
|
||||||
self.student = student_model
|
self.student = student_model
|
||||||
self.loss_function = nn.KLDivLoss(reduction="batchmean")
|
self.loss_function = nn.KLDivLoss(reduction="batchmean")
|
||||||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
device, _, _ = get_backend() # automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
self.teacher.to(device)
|
self.teacher.to(device)
|
||||||
self.teacher.eval()
|
self.teacher.eval()
|
||||||
self.temperature = temperature
|
self.temperature = temperature
|
||||||
|
|||||||
@@ -125,9 +125,9 @@ the processor.
|
|||||||
```python
|
```python
|
||||||
from transformers import SamModel, SamProcessor
|
from transformers import SamModel, SamProcessor
|
||||||
import torch
|
import torch
|
||||||
|
from accelerate.test_utils.testing import get_backend
|
||||||
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
|
device, _, _ = get_backend()
|
||||||
model = SamModel.from_pretrained("facebook/sam-vit-base").to(device)
|
model = SamModel.from_pretrained("facebook/sam-vit-base").to(device)
|
||||||
processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -53,8 +53,9 @@ Instantiate a pipeline from a [checkpoint on the Hugging Face Hub](https://huggi
|
|||||||
```py
|
```py
|
||||||
>>> from transformers import pipeline
|
>>> from transformers import pipeline
|
||||||
>>> import torch
|
>>> import torch
|
||||||
|
>>> from accelerate.test_utils.testing import get_backend
|
||||||
>>> device = "cuda" if torch.cuda.is_available() else "cpu"
|
# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
|
>>> device, _, _ = get_backend()
|
||||||
>>> checkpoint = "depth-anything/Depth-Anything-V2-base-hf"
|
>>> checkpoint = "depth-anything/Depth-Anything-V2-base-hf"
|
||||||
>>> pipe = pipeline("depth-estimation", model=checkpoint, device=device)
|
>>> pipe = pipeline("depth-estimation", model=checkpoint, device=device)
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1488,7 +1488,9 @@ Now that you have finetuned a model, evaluated it, and uploaded it to the Huggin
|
|||||||
|
|
||||||
Load model and image processor from the Hugging Face Hub (skip to use already trained in this session):
|
Load model and image processor from the Hugging Face Hub (skip to use already trained in this session):
|
||||||
```py
|
```py
|
||||||
>>> device = "cuda"
|
>>> from accelerate.test_utils.testing import get_backend
|
||||||
|
# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
|
>>> device, _, _ = get_backend()
|
||||||
>>> model_repo = "qubvel-hf/detr_finetuned_cppe5"
|
>>> model_repo = "qubvel-hf/detr_finetuned_cppe5"
|
||||||
|
|
||||||
>>> image_processor = AutoImageProcessor.from_pretrained(model_repo)
|
>>> image_processor = AutoImageProcessor.from_pretrained(model_repo)
|
||||||
|
|||||||
@@ -689,7 +689,9 @@ Reload the dataset and load an image for inference.
|
|||||||
We will now see how to infer without a pipeline. Process the image with an image processor and place the `pixel_values` on a GPU:
|
We will now see how to infer without a pipeline. Process the image with an image processor and place the `pixel_values` on a GPU:
|
||||||
|
|
||||||
```py
|
```py
|
||||||
>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # use GPU if available, otherwise use a CPU
|
>>> from accelerate.test_utils.testing import get_backend
|
||||||
|
# automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
|
>>> device, _, _ = get_backend()
|
||||||
>>> encoding = image_processor(image, return_tensors="pt")
|
>>> encoding = image_processor(image, return_tensors="pt")
|
||||||
>>> pixel_values = encoding.pixel_values.to(device)
|
>>> pixel_values = encoding.pixel_values.to(device)
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -282,10 +282,10 @@ containing the corresponding speaker embedding.
|
|||||||
>>> import os
|
>>> import os
|
||||||
>>> import torch
|
>>> import torch
|
||||||
>>> from speechbrain.inference.classifiers import EncoderClassifier
|
>>> from speechbrain.inference.classifiers import EncoderClassifier
|
||||||
|
>>> from accelerate.test_utils.testing import get_backend
|
||||||
|
|
||||||
>>> spk_model_name = "speechbrain/spkrec-xvect-voxceleb"
|
>>> spk_model_name = "speechbrain/spkrec-xvect-voxceleb"
|
||||||
|
>>> device, _, _ = get_backend() # automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
>>> device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
||||||
>>> speaker_model = EncoderClassifier.from_hparams(
|
>>> speaker_model = EncoderClassifier.from_hparams(
|
||||||
... source=spk_model_name,
|
... source=spk_model_name,
|
||||||
... run_opts={"device": device},
|
... run_opts={"device": device},
|
||||||
|
|||||||
@@ -363,10 +363,11 @@ GPU, if available, which we didn't need to do earlier when training, as [`Traine
|
|||||||
```py
|
```py
|
||||||
>>> from transformers import AutoProcessor, Blip2ForConditionalGeneration
|
>>> from transformers import AutoProcessor, Blip2ForConditionalGeneration
|
||||||
>>> import torch
|
>>> import torch
|
||||||
|
>>> from accelerate.test_utils.testing import get_backend
|
||||||
|
|
||||||
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
>>> processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
||||||
>>> model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16)
|
>>> model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16)
|
||||||
>>> device = "cuda" if torch.cuda.is_available() else "cpu"
|
>>> device, _, _ = get_backend() # automatically detects the underlying device type (CUDA, CPU, XPU, MPS, etc.)
|
||||||
>>> model.to(device)
|
>>> model.to(device)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user