> For the complete documentation index, see [llms.txt](https://docs.tensorwave.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorwave.com/welcome-to-tensorwave/hugging-face-quickstart.md).

# Hugging Face Quickstart

Estimated time: 7 minutes, 9 minutes with buffer.

***

Hugging Face is an AI/ML platform for the entire model pipeline. For this quickstart, we'll walk you through accelerated inference using a pretrained model. &#x20;

{% hint style="info" %}
Learn more about Hugging Face [here](https://huggingface.co/).
{% endhint %}

***

### Installing Dependencies

Because PyTorch with ROCm comes preloaded on your device, you will not need to install this dependency. However, you will still need a couple of libraries in order to run our quickstart script. Begin by installing [transformers](https://huggingface.co/docs/transformers/en/index) using the following command:

```bash
pip install transformers
```

This should take no more than a few minutes.

***

### Creating and Running Inference Script

Next, go ahead and create and navigate to a new directory to create your script in:

```bash
mkdir hf-hello-world
cd hf-hello-world
```

Then, create a new script:

```bash
nano hello-world.py
```

Within this script, paste the following code and exit:

```bash
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import time

# Load model without quantization
tokenizer = AutoTokenizer.from_pretrained("facebook/opt-350m")
model = AutoModelForCausalLM.from_pretrained("facebook/opt-350m")

# Move model to GPU
model = model.to("cuda")

# Input text
print("Warming up model...")
input_text = "Hello, my name is"
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
warmup = model.generate(**inputs, max_new_tokens=20)

print("Preparing text...")
input_text = "According to all known laws of aviation, there is no way that a bee should be able to fly. Its wings are too small to get its fat little body off the ground. The bee, of course, flies anyway because"
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")

print("Starting inference...")
start = time.time()
outputs = model.generate(
    **inputs,
    max_new_tokens=50,
    do_sample=True,
    temperature=0.7,
    top_k=50,
    top_p=0.95,
    no_repeat_ngram_size=2
)
t = time.time()-start
print(f"inference time: {t}")

print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```

After doing so, you may run the script using the following:

```bash
python3 hello-world.py
```

This runs a small model on one GPU, but feel free to swap out your model and prompts to your liking, then map to the proper devices. The output should be similar to:

```
Warming up model...
Preparing text...
Starting inference...
inference time: 0.4770219326019287
According to all known laws of aviation, there is no way that a bee should be able to fly. Its wings are too small to get its fat little body off the ground. The bee, of course, flies anyway because it can.
Well, if you fly with a little fat of your body, you can fly pretty damn well.  You just have to be careful.
```

***

### Teardown

Navigate back to your base directory and remove your `hf-hello-world` folder:

```bash
cd ~
rm -rf hf-hello-world/
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tensorwave.com/welcome-to-tensorwave/hugging-face-quickstart.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
