Skip to content

Commit eaa48c8

Browse files
yao-matrixstevhliu
andauthored
make model docs device agnostic (2) (#40256)
* doc cont. Signed-off-by: Yao, Matrix <[email protected]> * more models Signed-off-by: Yao, Matrix <[email protected]> * Update docs/source/en/quicktour.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/quicktour.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/quicktour.md Co-authored-by: Steven Liu <[email protected]> * Update docs/source/en/quicktour.md Co-authored-by: Steven Liu <[email protected]> * Update mixtral.md --------- Signed-off-by: Yao, Matrix <[email protected]> Co-authored-by: Steven Liu <[email protected]>
1 parent 42fe769 commit eaa48c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+157
-159
lines changed

docs/source/en/model_doc/bart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ model = AutoModelForMaskedLM.from_pretrained(
6565
device_map="auto",
6666
attn_implementation="sdpa"
6767
)
68-
inputs = tokenizer("Plants create <mask> through a process known as photosynthesis.", return_tensors="pt").to("cuda")
68+
inputs = tokenizer("Plants create <mask> through a process known as photosynthesis.", return_tensors="pt").to(model.device)
6969

7070
with torch.no_grad():
7171
outputs = model(**inputs)

docs/source/en/model_doc/barthez.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ model = AutoModelForMaskedLM.from_pretrained(
6868
torch_dtype=torch.float16,
6969
device_map="auto",
7070
)
71-
inputs = tokenizer("Les plantes produisent <mask> grâce à un processus appelé photosynthèse.", return_tensors="pt").to("cuda")
71+
inputs = tokenizer("Les plantes produisent <mask> grâce à un processus appelé photosynthèse.", return_tensors="pt").to(model.device)
7272

7373
with torch.no_grad():
7474
outputs = model(**inputs)

docs/source/en/model_doc/bartpho.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Quang tổng hợp hay gọi tắt là quang hợp là quá trình thu nhận v
8282
tảo và một số vi khuẩn để tạo ra hợp chất hữu cơ phục vụ bản thân cũng như làm nguồn thức ăn cho hầu hết các sinh vật
8383
trên Trái Đất. Quang hợp trong thực vật thường liên quan đến chất tố diệp lục màu xanh lá cây và tạo ra oxy như một sản phẩm phụ
8484
"""
85-
inputs = tokenizer(text, return_tensors="pt").to("cuda")
85+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
8686

8787
outputs = model.generate(inputs["input_ids"], num_beams=2, min_length=0, max_length=20)
8888
tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]

docs/source/en/model_doc/bertweet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ model = AutoModelForMaskedLM.from_pretrained(
6767
torch_dtype=torch.float16,
6868
device_map="auto"
6969
)
70-
inputs = tokenizer("Plants create <mask> through a process known as photosynthesis.", return_tensors="pt").to("cuda")
70+
inputs = tokenizer("Plants create <mask> through a process known as photosynthesis.", return_tensors="pt").to(model.device)
7171

7272
with torch.no_grad():
7373
outputs = model(**inputs)

docs/source/en/model_doc/big_bird.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ model = AutoModelForMaskedLM.from_pretrained(
6464
torch_dtype=torch.float16,
6565
device_map="auto",
6666
)
67-
inputs = tokenizer("Plants create [MASK] through a process known as photosynthesis.", return_tensors="pt").to("cuda")
67+
inputs = tokenizer("Plants create [MASK] through a process known as photosynthesis.", return_tensors="pt").to(model.device)
6868

6969
with torch.no_grad():
7070
outputs = model(**inputs)

docs/source/en/model_doc/bigbird_pegasus.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ input_text = """Plants are among the most remarkable and essential life forms on
7272
Through photosynthesis, plants capture energy from sunlight using a green pigment called chlorophyll, which is located in specialized cell structures called chloroplasts. In the presence of light, plants absorb carbon dioxide from the atmosphere through small pores in their leaves called stomata, and take in water from the soil through their root systems.
7373
These ingredients are then transformed into glucose, a type of sugar that serves as a source of chemical energy, and oxygen, which is released as a byproduct into the atmosphere. The glucose produced during photosynthesis is not just used immediately; plants also store it as starch or convert it into other organic compounds like cellulose, which is essential for building their cellular structure.
7474
This energy reserve allows them to grow, develop leaves, produce flowers, bear fruit, and carry out various physiological processes throughout their lifecycle."""
75-
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
75+
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)
7676

7777
output = model.generate(**input_ids, cache_implementation="static")
7878
print(tokenizer.decode(output[0], skip_special_tokens=True))
@@ -115,7 +115,7 @@ input_text = """Plants are among the most remarkable and essential life forms on
115115
Through photosynthesis, plants capture energy from sunlight using a green pigment called chlorophyll, which is located in specialized cell structures called chloroplasts. In the presence of light, plants absorb carbon dioxide from the atmosphere through small pores in their leaves called stomata, and take in water from the soil through their root systems.
116116
These ingredients are then transformed into glucose, a type of sugar that serves as a source of chemical energy, and oxygen, which is released as a byproduct into the atmosphere. The glucose produced during photosynthesis is not just used immediately; plants also store it as starch or convert it into other organic compounds like cellulose, which is essential for building their cellular structure.
117117
This energy reserve allows them to grow, develop leaves, produce flowers, bear fruit, and carry out various physiological processes throughout their lifecycle."""
118-
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
118+
input_ids = tokenizer(input_text, return_tensors="pt").to(model.device)
119119

120120
output = model.generate(**input_ids, cache_implementation="static")
121121
print(tokenizer.decode(output[0], skip_special_tokens=True))

docs/source/en/model_doc/blip.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/
7373
image = Image.open(requests.get(url, stream=True).raw)
7474

7575
question = "What is the weather in this image?"
76-
inputs = processor(images=image, text=question, return_tensors="pt").to("cuda", torch.float16)
76+
inputs = processor(images=image, text=question, return_tensors="pt").to(model.device, torch.float16)
7777

7878
output = model.generate(**inputs)
7979
processor.batch_decode(output, skip_special_tokens=True)[0]

docs/source/en/model_doc/clap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ tokenizer = AutoTokenizer.from_pretrained("laion/clap-htsat-unfused")
4848

4949
texts = ["the sound of a cat", "the sound of a dog", "music playing"]
5050

51-
inputs = tokenizer(texts, padding=True, return_tensors="pt").to("cuda")
51+
inputs = tokenizer(texts, padding=True, return_tensors="pt").to(model.device)
5252

5353
with torch.no_grad():
5454
text_features = model.get_text_features(**inputs)

docs/source/en/model_doc/code_llama.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ model = AutoModelForCausalLM.from_pretrained(
7474

7575
# basic code generation
7676
prompt = "# Function to calculate the factorial of a number\ndef factorial(n):"
77-
input_ids = tokenizer(prompt, return_tensors="pt").to("cuda")
77+
input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)
7878

7979
output = model.generate(
8080
**input_ids,
@@ -121,7 +121,7 @@ model = AutoModelForCausalLM.from_pretrained(
121121
)
122122

123123
prompt = "# Write a Python function to check if a string is a palindrome\ndef is_palindrome(s):"
124-
input_ids = tokenizer(prompt, return_tensors="pt").to("cuda")
124+
input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)
125125

126126
output = model.generate(**input_ids, max_new_tokens=200, cache_implementation="static")
127127
print(tokenizer.decode(output[0], skip_special_tokens=True))

docs/source/en/model_doc/csm.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ CSM can be used to simply generate speech from a text prompt:
3838

3939
```python
4040
import torch
41-
from transformers import CsmForConditionalGeneration, AutoProcessor
41+
from transformers import CsmForConditionalGeneration, AutoProcessor, infer_device
4242

4343
model_id = "sesame/csm-1b"
44-
device = "cuda" if torch.cuda.is_available() else "cpu"
44+
device = infer_device()
4545

4646
# load the model and the processor
4747
processor = AutoProcessor.from_pretrained(model_id)
@@ -72,11 +72,11 @@ CSM can be used to generate speech given a conversation, allowing consistency in
7272

7373
```python
7474
import torch
75-
from transformers import CsmForConditionalGeneration, AutoProcessor
75+
from transformers import CsmForConditionalGeneration, AutoProcessor, infer_device
7676
from datasets import load_dataset, Audio
7777

7878
model_id = "sesame/csm-1b"
79-
device = "cuda" if torch.cuda.is_available() else "cpu"
79+
device = infer_device()
8080

8181
# load the model and the processor
8282
processor = AutoProcessor.from_pretrained(model_id)
@@ -117,11 +117,11 @@ CSM supports batched inference!
117117

118118
```python
119119
import torch
120-
from transformers import CsmForConditionalGeneration, AutoProcessor
120+
from transformers import CsmForConditionalGeneration, AutoProcessor, infer_device
121121
from datasets import load_dataset, Audio
122122

123123
model_id = "sesame/csm-1b"
124-
device = "cuda" if torch.cuda.is_available() else "cpu"
124+
device = infer_device()
125125

126126
# load the model and the processor
127127
processor = AutoProcessor.from_pretrained(model_id)
@@ -306,11 +306,11 @@ print("="*50)
306306
CSM Transformers integration supports training!
307307

308308
```python
309-
from transformers import CsmForConditionalGeneration, AutoProcessor
309+
from transformers import CsmForConditionalGeneration, AutoProcessor, infer_device
310310
from datasets import load_dataset, Audio
311311

312312
model_id = "sesame/csm-1b"
313-
device = "cuda"
313+
device = infer_device()
314314

315315
# load the model and the processor
316316
processor = AutoProcessor.from_pretrained(model_id)

0 commit comments

Comments
 (0)