# Training Node Guide

This guide provides step-by-step instructions for the entire Training Node workflow. By the end you will have successfully staked as a Training Node, completed a training task, and claimed your staking rewards.

{% hint style="info" %}
Be sure you have completed all pre-requisite tasks.
{% endhint %}

{% hint style="warning" %}
You can only be either training node OR validator for each task.&#x20;
{% endhint %}

### 0. Navigate to [train.flock.io](https://train.flock.io/) and connect you wallet.

Follow the steps [here](https://docs.flock.io/~/changes/186/flock-products/ai-arena/delegator-guide#step-0-go-to-https-train.flock.io-stake-to-earn-and-connect-your-wallet) to connect your wallet.

### 1. Stake gmFLOCK as Training Node from [train.flock.io](https://train.flock.io/)

1. Once you have gmFLOCK tokens, Navigate to the Training Node tab on the Stake page
2. Select a training task
3. Stake gmFLOCK

<figure><img src="https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2FudTDxclTKPcoSRbUnVSE%2Fimage.png?alt=media&#x26;token=72d0ba51-82ff-489f-b267-8dd3bc2a137c" alt=""><figcaption></figcaption></figure>

Once you’ve confirmed and approved the transaction in your web3 wallet, you will see a box on the Training Node tab with your stake details.

<figure><img src="https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2FwVKuPFxsLysUTLn0j9VT%2Fimage.png?alt=media&#x26;token=1023fa6c-d8dd-4d61-8a4b-147193f8fabb" alt="" width="380"><figcaption></figcaption></figure>

### 2. Accept delegator

Note that you have the option to accept delegator. To do so, go to "Accept Delegator" tab. Select your profit-sharing ratio, then click "Create Delegation Contract":

<figure><img src="https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2Fgc02vjcfg9zeBpGENeVL%2Fimage.png?alt=media&#x26;token=3d658247-bc8f-4bd5-9d4e-a590becfe63c" alt=""><figcaption></figcaption></figure>

Once the transaction is completed. You have the option to add a profile and a profile picture, as well as to modify your profit-sharing ratio here:

<figure><img src="https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2Fdoi5pVhPtEXGLIeqYLnA%2Fimage.png?alt=media&#x26;token=d5d5e4e5-cda9-463b-bdb2-805e1ee0feb4" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
You can only modify your rewards-sharing ratio once per month.
{% endhint %}

### 3. Get API key

Your API key is required for all remaining Validator steps. You can get it from the web app.

<figure><img src="https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2F5Mi4hCF0mrwjW9Nljzip%2Fimage.png?alt=media&#x26;token=6896e7d0-759b-44ce-a6b1-7e17ed5366f4" alt=""><figcaption></figcaption></figure>

<figure><img src="https://742781353-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1RpcbvTSHzzPwOSUvgKU%2Fuploads%2FvamRJmGlMdZDHaYzD5D3%2Fval2.1_copy-api.png?alt=media&#x26;token=43ca5c44-8e5c-42c6-b83f-fbd4a8aeaa45" alt=""><figcaption></figcaption></figure>

1. Select dropdown in upper right corner of web app
2. Select API
3. On the API page, copy your API key

Once you have your API key, you can proceed to the next step.

{% hint style="info" %}
NOTE: If you have issues generating an API key try removing any ad blocker extensions and/or clearing your cookies.
{% endhint %}

### Video Tutorial

{% embed url="<https://youtu.be/1pNoyt370z0>" %}

### 4. Set up your environment

#### Windows

For Windows users, we suggest installing WSL. Follow the guidance: [wsl-installation](https://docs.flock.io/~/changes/186/flock-products/ai-arena/quickstart/pre-requisites/wsl-installation "mention")

#### Mac/Linux

You can install Anaconda via [HERE](https://www.anaconda.com/download/success)

### 5. Clone the QuickStart repo

The [quickstart repo](https://github.com/FLock-io/testnet-training-node-quickstart.git) contains everything you need to run our [`full_automation.py`](https://github.com/FLock-io/testnet-training-node-quickstart/blob/main/full_automation.py) script. To clone it run:

```
git clone https://github.com/FLock-io/testnet-training-node-quickstart.git
```

To set up all packages within the project directory

```
conda create -n training-node python==3.10
conda activate training-node
pip install -r requirements.txt
```

{% hint style="info" %}
**File Structure**

* [`dataset.py`](https://github.com/FLock-io/testnet-training-node-quickstart/blob/main/dataset.py) - Contains the logic to process the raw data from `demo_data.jsonl`.
* [`demo_data.jsonl`](https://github.com/FLock-io/testnet-training-node-quickstart/blob/main/demo_data.jsonl) - Follows the shareGPT format. The training data you receive from the `fed-ledger` is in exactly the same format.
* [`merge.py`](https://github.com/FLock-io/testnet-training-node-quickstart/blob/main/merge.py) - Contains the utility function for merging LoRA weights. If you are training with LoRA, please ensure you merge the adapter before uploading to your Hugging Face repository.
* [`demo.py`](https://github.com/FLock-io/testnet-training-node-quickstart/blob/main/demo.py) - A training script that implements LoRA fine-tuning for a Gemma-2B model.
  {% endhint %}

### 6. Get information required to run automation script

The script described in the following step automates the following:

* Gets a task
* Downloads the training data
* Finetunes Gemma-2B on training data
* Merges weights
* Uploads to your Hugging Face model repo
* Submits the task to `fed-ledger`

To run it, you will need:

* `TASK_ID` - the id of the task you are staking on as Training Node
* `FLOCK_API_KEY` - available at <https://train.flock.io/flock_api>
* `HF_TOKEN` - your Hugging Face Access Token (Hugging Face > Profile > Settings > Access Tokens)
* `HG_USERNAME` - your Hugging Face user name

{% hint style="info" %}
NOTE: If you haven't already done so, please acknowledge license to access Gemma on Hugging Face:

<https://huggingface.co/google/gemma-2b>
{% endhint %}

### 7. Run full automation script

Once you have all the information listed in the previous step, paste the script below into your terminal, update the repsective values, and run the command.

{% tabs %}
{% tab title="Unix/Linux" %}
{% code overflow="wrap" %}

```
TASK_ID=<task-id> FLOCK_API_KEY="<your-flock-api-key-stakes-as-node-for-the-task>" HF_TOKEN="<your-hf-token>" CUDA_VISIBLE_DEVICES=0 HF_USERNAME="your-hf-user-name" python full_automation.py
```

{% endcode %}
{% endtab %}

{% tab title="CMD" %}

```
set TASK_ID=<task-id> && 
set FLOCK_API_KEY="<your-flock-api-key-stakes-as-node-for-the-task>" && 
set HF_TOKEN="<your-hf-token>" && 
set CUDA_VISIBLE_DEVICES=0 && 
set HF_USERNAME="your-hf-user-name" && 
python full_automation.py
```

{% endtab %}

{% tab title="PowerShell" %}

```
$env:TASK_ID = "<task-id>"
$env:FLOCK_API_KEY = "<your-flock-api-key-stakes-as-node-for-the-task>"
$env:HF_TOKEN = "<your-hf-token>"
$env:CUDA_VISIBLE_DEVICES = "0"
$env:HF_USERNAME = "your-hf-user-name"
python full_automation.py
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
NOTE: Tasks can be completed with any [supported base LLM model](https://github.com/FLock-io/llm-loss-validator/blob/main/src/core/constant.py). The only restriction is `max_param.`You can use any technique to train or fine-tune a model, all you need to make sure is that the model can be successfully run by our [validation script](https://github.com/FLock-io/llm-loss-validator/tree/main).
{% endhint %}

### 8. Claim rewards

Reward distribution is triggered every 24 hours at midnight UTC. The final round of reward distribution is triggered at the once the task training period is complete.&#x20;

You can claim your rewards via [train.flock.io](https://train.flock.io).
