# Linux Installation Instructions

Ryzen AI for Linux supports running AI models on the AMD Neural Processing Unit (NPU).
The current release supports STX and KRK platforms.

With this release, users can now compile and run AI models using the following formats:

- CNN models in INT8
- CNN models in BF16
- NLP models (e.g., BERT, encoder-based) in BF16
- LLMs (NPU-only flow)

## Prerequisites

Use the commands below to install Python 3.12.x along with certain dependencies

```bash
sudo apt update
sudo apt install python3.12
sudo apt install python3.12-venv
sudo apt install libboost-filesystem1.74.0
sudo apt install dkms
```

After installing required Ubuntu distribution and Python version, proceed with NPU drivers installation

<a id="install-driver"></a>

## Install NPU Drivers

- Download the NPU driver package from Downloads section of [Ryzen AI Software Drivers](https://download.amd.com/opendownload/RyzenAI/Driver/RAI_1.8_Linux_NPU_XRT.zip).
- RyzenAI linux driver package contains
  : - XRT Package
      : - xrt_202620.2.25.37_24.04-amd64-base.deb
        - xrt_202620.2.25.37_24.04-amd64-base-dev.deb
        - xrt_202620.2.25.37_24.04-amd64-npu.deb
    - NPU driver package
      : - xrt_plugin.2.25.260102.56.release_24.04-amd64-amdxdna.deb
- Install NPU driver package on your machine

```bash
sudo apt install --fix-broken -y ./xrt_202620.2.25.37_24.04-amd64-base.deb
sudo apt install --fix-broken -y ./xrt_202620.2.25.37_24.04-amd64-base-dev.deb
sudo apt install --fix-broken -y ./xrt_202620.2.25.37_24.04-amd64-npu.deb
sudo apt install --fix-broken -y ./xrt_plugin.2.25.260102.56.release_24.04-amd64-amdxdna.deb
```

- Verify your Driver installation

```bash
 source /opt/xilinx/xrt/setup.sh
 xrt-smi examine

Device(s) Present
|BDF             |Name                |Architecture  |Topology  |
|----------------|--------------------|--------------|----------|
|[0000:c5:00.1]  |NPU Strix           |aie2p         |6x8       |

 # NPU name and Device code may differ based on your machine
```

<a id="install-bundled"></a>

## Install Ryzen AI Software

- Download the RyzenAI for Linux package ryzen_ai-1.8.0.tgz from Downloads section of [Ryzen AI Software Installer](https://account.amd.com/en/forms/downloads/xef.html?filename=ryzen_ai-1.8.0.tgz).
- Navigate to the downloaded path and follow the below steps

```bash
mkdir ryzen_ai-1.8.0
cp ryzen_ai-1.8.0.tgz ryzen_ai-1.8.0

cd ryzen_ai-1.8.0
tar -xvzf ryzen_ai-1.8.0.tgz
```

- Install RyzenAI package at your desired target path

```bash
./install_ryzen_ai.sh -a yes -p <TARGET-PATH>/venv
source <TARGET-PATH>/venv/bin/activate
```

- This will successfully install RyzenAI and activate the Virtual environment at your target location

```bash
# Validate your installation path
echo $RYZEN_AI_INSTALLATION_PATH
```

- Set essential Environment variables

```bash
export LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:${RYZEN_AI_INSTALLATION_PATH}/onnxruntime/lib/:$LD_LIBRARY_PATH
```

## Test the Installation

The RyzenAI software package contains a test script that verifies your correct installation of NPU Drivers.

- Navigate to your targeted Virtual Environment created in the previous step
- You will observe a subfolder named “quicktest”

```bash
cd <TARGET-PATH>/venv/quicktest
python quicktest.py
```

- The quicktest.py script picks up a simple CNN model, compiles it and runs on AMD’s Neural Processing Unit (NPU).
- On successful run, you can observe output as shown below.

```bash
Setting environment for STX/KRK

Test Finished
```

## Examples, Demos, Tutorials

- RyzenAI-SW demonstrates various demos and examples for Model compilation and deployment on NPUs
- Here are a few examples from our [RyzenAI Software Repository](https://github.com/amd/RyzenAI-SW/tree/main)
  : - [Getting started Resnet with BF16 Model](https://github.com/amd/RyzenAI-SW/tree/main/CNN-examples/getting_started_resnet/bf16)
    - [Getting started Resnet with INT8 Model](https://github.com/amd/RyzenAI-SW/tree/main/CNN-examples/getting_started_resnet/int8)
    - [Yolov8m Model for Object Detection](https://github.com/amd/RyzenAI-SW/tree/main/CNN-examples/object_detection)

## Note

Before running the above examples -
: - RyzenAI-SW repo hosts a diverse set of examples with both NPU and iGPU Execution Provider. However, Linux currently supports NPU only flow.
  - RyzenAI creates its own Python Virtual Environment to run the examples. Skip conda environment instruction as they are Windows specific only
  - Ensure to activate Linux based Python Virtual Environment
    ```bash
    source <TARGET-PATH>/venv/bin/activate
    ```

### Get NPU Info for your Machine

[Getting started Resnet with INT8 Model](https://github.com/amd/RyzenAI-SW/tree/main/CNN-examples/getting_started_resnet/int8)

Getting started Resnet with INT8 Model contains Resnet_util.py script that has a function “get_npu_info” to detect correct “NPU type” in your machine. This NPU lookup logic is based for Windows system.

For Linux, NPU lookup logic is shown below:

```python
import subprocess

def get_npu_info():
    # Run below command as subprocess to enumerate PCI devices
    command = r'lspci -nn'
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()

    # Check for supported Hardware IDs
    npu_type = ''
    if '1022:17f0' in stdout.decode(): npu_type = 'STX/KRK'
    return npu_type
```

## Running LLM

Follow this page to run LLM models on Linux: [Running LLM on Linux](https://ryzenai.docs.amd.com/en/latest/llm_linux.html)