Skip to content

Commit

Permalink
docs: itg-llama-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyi-Lin committed Dec 31, 2024
1 parent 53e5262 commit 22d0141
Show file tree
Hide file tree
Showing 26 changed files with 460 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vitepress/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ function sidebarGuideCloud(): DefaultTheme.SidebarItem[] {
{ text: 'HuggingFace Transformers', link: 'integration/integration-huggingface-transformers' },
{ text: 'Hydra', link: 'integration/integration-hydra' },
{ text: 'Keras', link: 'integration/integration-keras' },
{ text: 'LightGBM', link: 'integration/integration-lightgbm'},
{ text: 'LLaMA Factory', link: 'integration/integration-llama-factory'},
{ text: 'MMEngine', link: 'integration/integration-mmengine' },
{ text: 'MMPretrain', link: 'integration/integration-mmpretrain' },
{ text: 'MMDetection', link: 'integration/integration-mmdetection' },
Expand All @@ -152,6 +154,7 @@ function sidebarGuideCloud(): DefaultTheme.SidebarItem[] {
{ text: 'Tensorboard', link: 'integration/integration-tensorboard'},
{ text: 'Ultralytics', link: 'integration/integration-ultralytics' },
{ text: 'Weights & Biases', link: 'integration/integration-wandb'},
{ text: 'XGBoost', link: 'integration/integration-xgboost'},
{ text: 'Xtuner', link: 'integration/integration-xtuner'},
{ text: 'ZhipuAI', link: 'integration/integration-zhipuai'},
]
Expand Down
1 change: 1 addition & 0 deletions .vitepress/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function sidebarGuideCloud(): DefaultTheme.SidebarItem[] {
{ text: 'Hydra', link: 'integration/integration-hydra' },
{ text: 'Keras', link: 'integration/integration-keras' },
{ text: 'LightGBM', link: 'integration/integration-lightgbm'},
{ text: 'LLaMA Factory', link: 'integration/integration-llama-factory'},
{ text: 'MMEngine', link: 'integration/integration-mmengine' },
{ text: 'MMPretrain', link: 'integration/integration-mmpretrain' },
{ text: 'MMDetection', link: 'integration/integration-mmdetection' },
Expand Down
106 changes: 106 additions & 0 deletions en/guide_cloud/integration/integration-lightgbm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# LightGBM

LightGBM (Light Gradient Boosting Machine) is a distributed gradient boosting framework based on decision tree algorithms, released by Microsoft in 2017. It is renowned for its efficiency, speed, and accuracy, and is widely used in machine learning tasks such as classification, regression, and ranking.

![lightgbm](/zh/guide_cloud/integration/lightgbm/logo.png)

You can use LightGBM for rapid model training while leveraging SwanLab for experiment tracking and visualization.

## 1. Import SwanLabCallback

```python
from swanlab.integration.lightgbm import SwanLabCallback
```

`SwanLabCallback` is a logging class designed for LightGBM.

## 2. Initialize SwanLab

```python
swanlab.init(
project="lightgbm-example",
experiment_name="breast-cancer-classification"
)
```

## 3. Pass to `lgb.train`

```python
import lightgbm as lgb

gbm = lgb.train(
...
callbacks=[SwanLabCallback()]
)
```

## 4. Complete Test Code

```python
import lightgbm as lgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import swanlab
from swanlab.integration.lightgbm import SwanLabCallback

# Step 1: Initialize swanlab
swanlab.init(project="lightgbm-example", experiment_name="breast-cancer-classification")

# Step 2: Load dataset
data = load_breast_cancer()
X = data.data
y = data.target

# Step 3: Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 4: Create LightGBM dataset
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test, reference=train_data)

# Step 5: Set parameters
params = {
'objective': 'binary',
'metric': 'binary_logloss',
'boosting_type': 'gbdt',
'num_leaves': 31,
'learning_rate': 0.05,
'feature_fraction': 0.9
}

# Step 6: Train the model with swanlab callback
num_round = 100
gbm = lgb.train(
params,
train_data,
num_round,
valid_sets=[test_data],
callbacks=[SwanLabCallback()]
)

# Step 7: Predict
y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
y_pred_binary = [1 if p >= 0.5 else 0 for p in y_pred]

# Step 8: Evaluate the model
accuracy = accuracy_score(y_test, y_pred_binary)
print(f"Model Accuracy: {accuracy:.4f}")
swanlab.log({"accuracy": accuracy})

# Step 9: Save the model
gbm.save_model('lightgbm_model.txt')

# Step 10: Load the model and predict
bst_loaded = lgb.Booster(model_file='lightgbm_model.txt')
y_pred_loaded = bst_loaded.predict(X_test)
y_pred_binary_loaded = [1 if p >= 0.5 else 0 for p in y_pred_loaded]

# Step 11: Evaluate the loaded model
accuracy_loaded = accuracy_score(y_test, y_pred_binary_loaded)
print(f"Accuracy after loading the model: {accuracy_loaded:.4f}")
swanlab.log({"accuracy_loaded": accuracy_loaded})

# Step 12: Finish the swanlab experiment
swanlab.finish()
```
120 changes: 120 additions & 0 deletions en/guide_cloud/integration/integration-llama-factory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# LLaMA Factory

![](/zh/guide_cloud/integration/llama_factory/0.png)

We are thrilled to announce the partnership between **SwanLab** and **LLaMA Factory**, dedicated to providing Chinese trainers with a high-quality and efficient large model training experience.

Now, before starting training with the new version of LLaMA Factory, you can check the "Use SwanLab" option in the "SwanLab configurations" card on the WebUI. This allows you to track, record, and visualize this large model fine-tuning session through SwanLab's powerful training dashboard.

![](/zh/guide_cloud/integration/llama_factory/1.png)

LLaMA Factory is an open-source toolkit for fine-tuning large language models (LLMs). It provides a unified and efficient framework, supporting fine-tuning for over 100 LLMs (including Qwen, LLaMA, ChatGLM, Mistral, etc.), covering various training methods, datasets, and advanced algorithms.

Fine-tuning large language models is a task with a steep learning curve. LLaMA Factory significantly lowers the barrier to entry by offering a user-friendly Web UI and command-line interface, combined with its unified and efficient framework, making it easier to go from fine-tuning to testing and evaluation.

To provide users with a better experience in monitoring and logging the fine-tuning process of large models, we have collaborated with the LLaMA Factory team on two initiatives: enhancing LLaMA Factory's experiment monitoring capabilities with SwanLab, and recording LLaMA Factory-specific hyperparameters in SwanLab.

> LLaMA Factory: https://github.com/hiyouga/LLaMA-Factory
> SwanLab: https://swanlab.cn
> SwanLab Open Source Repository: https://github.com/SwanHubX/SwanLab
> Experiment Process: https://swanlab.cn/@ZeyiLin/llamafactory/runs/y79f9ri9jr1mkoh24a7g8/chart
<br>

## Use Case

We will demonstrate the process of fine-tuning Qwen2.5 using LLaMA Factory + SwanLab visualization.

### 1. Environment Setup

First, ensure you have Python 3.8 or above and Git installed, then clone the repository:

```shellscript
git clone https://github.com/hiyouga/LLaMA-Factory
```

Install the necessary dependencies:

```shellscript
cd LLaMA-Factory
pip install -e ".[torch,metrics,swanlab]"
```

> If you are an Ascend NPU user, refer to: https://github.com/hiyouga/LLaMA-Factory/blob/main/README\_zh.md#%E5%AE%89%E8%A3%85-llama-factory for the installation guide.
### 2. Start Training with LLaMA Board

LLaMA Board is a Gradio-based visual fine-tuning interface. You can start LLaMA Board with the following command:

```shellscript
llamafactory-cli webui
```

Tip: By default, LLaMA Factory downloads models/datasets from HuggingFace. If your network environment is not favorable for HuggingFace downloads, you can switch the download source to ModelScope or OpenMind before starting LLaMA Board:

```shellscript
# Switch to ModelScope
export USE_MODELSCOPE_HUB=1 # Windows use `set USE_MODELSCOPE_HUB=1`
# Switch to OpenMind
export USE_OPENMIND_HUB=1 # Windows use `set USE_OPENMIND_HUB=1`
```

After executing `llamafactory-cli webui`, you will see the following UI interface in your browser. For this case, select Qwen2-1.5B-instruct as the model and alpaca\_zh\_demo as the dataset:

![](/zh/guide_cloud/integration/llama_factory/2.png)

At the bottom of the page, you will find a "SwanLab Configurations" card. Expand it to configure SwanLab's project name, experiment name, workspace, API key, and mode.

> If you are using SwanLab for the first time, you need to register an account on swanlab.cn to get your exclusive API key.
Check the **"Use SwanLab"** option:

![](/zh/guide_cloud/integration/llama_factory/3.png)

Now, click the **"Start" button** to begin fine-tuning:

![](/zh/guide_cloud/integration/llama_factory/4.png)

After loading the model and dataset, and officially starting the fine-tuning, you can find the SwanLab section in the command-line interface:

![](/zh/guide_cloud/integration/llama_factory/5.png)

Click the experiment link indicated by the arrow to open the SwanLab experiment tracking dashboard in your **browser**:

![](/zh/guide_cloud/integration/llama_factory/6.png)

In the "Cards" section under the "Configuration" table, the first entry will be LLamaFactory, indicating the framework used for this training.

![](/zh/guide_cloud/integration/llama_factory/7.png)

### 3. Start Training via Command Line

LLaMA Factory also supports fine-tuning via YAML configuration files in the command line.

Edit the **examples/train\_lora/qwen2vl\_lora\_sft.yaml** file in the LLaMA Factory project directory, and add the following at the end of the file:

```yaml
...

### swanlab
use_swanlab: true
swanlab_project: llamafactory
swanlab_run_name: Qwen2-VL-7B-Instruct
```
Then run:
```shellscript
llamafactory-cli train examples/train_lora/qwen2vl_lora_sft.yaml
```

After loading the model and dataset, and officially starting the fine-tuning, similar to LLaMA Board, you can find the SwanLab section in the command-line interface and access the SwanLab experiment dashboard via the experiment link.

![](/zh/guide_cloud/integration/llama_factory/8.png)

![](/zh/guide_cloud/integration/llama_factory/9.png)

***

We salute the LLaMA Factory team for providing such an excellent model training tool to the open-source community. As we continue our collaboration, stay tuned for SwanLab to offer more in-depth and powerful experiment tracking features for large model trainers.
107 changes: 107 additions & 0 deletions en/guide_cloud/integration/integration-xgboost.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# XGBoost

XGBoost (eXtreme Gradient Boosting) is an efficient, flexible, and widely-used gradient boosting framework introduced by Tianqi Chen in 2014. It is based on decision tree algorithms and builds a powerful predictive model by integrating multiple weak learners (typically decision trees). XGBoost has demonstrated outstanding performance in various machine learning competitions and practical applications, especially in classification, regression, and ranking tasks.

![xgboost](/zh/guide_cloud/integration/xgboost/logo.png)

You can use XGBoost for rapid model training while leveraging SwanLab for experiment tracking and visualization.

## 1. Import SwanLabCallback

```python
from swanlab.integration.xgboost import SwanLabCallback
```

`SwanLabCallback` is a logging class designed for XGBoost.

## 2. Initialize SwanLab

```python
swanlab.init(
project="xgboost-example",
)
```

## 3. Pass to `xgb.train`

```python
import xgboost as xgb

bst = xgb.train(
...
callbacks=[SwanLabCallback()]
)
```

## 4. Complete Test Code

```python
import xgboost as xgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import swanlab
from swanlab.integration.xgboost import SwanLabCallback

# Initialize swanlab
swanlab.init(
project="xgboost-breast-cancer",
config={
"learning_rate": 0.1,
"max_depth": 3,
"subsample": 0.8,
"colsample_bytree": 0.8,
"num_round": 100
}
)

# Load dataset
data = load_breast_cancer()
X = data.data
y = data.target

# Split dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Convert to DMatrix format, which is the internal data format of XGBoost
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)

# Set parameters
params = {
'objective': 'binary:logistic', # Binary classification task
'max_depth': 3, # Maximum depth of a tree
'eta': 0.1, # Learning rate
'subsample': 0.8, # Subsample ratio
'colsample_bytree': 0.8, # Feature subsample ratio
'eval_metric': 'logloss' # Evaluation metric
}

# Train the model
num_round = 100 # Number of iterations
bst = xgb.train(
params,
dtrain,
num_round,
evals=[(dtrain, 'train'), (dtest, 'test')],
callbacks=[SwanLabCallback()]
)

# Make predictions
y_pred = bst.predict(dtest)
y_pred_binary = [round(value) for value in y_pred] # Convert probabilities to binary classification results

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred_binary)
print(f"Accuracy: {accuracy:.4f}")

# Print classification report
print("Classification Report:")
print(classification_report(y_test, y_pred_binary, target_names=data.target_names))

# Save the model
bst.save_model('xgboost_model.model')

# End the swanlab session
swanlab.finish()
```
Binary file added en/guide_cloud/integration/llama_factory/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/guide_cloud/integration/llama_factory/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 22d0141

Please sign in to comment.