1. Introduction to Fine-tuning

Model fine-tuning is a technique that involves further training an existing pre-trained model using a specific task dataset. This allows the model to retain its general knowledge learned from large datasets while adapting to the nuances of specific tasks. The benefits of using fine-tuned models include:

  • Improved Performance: Fine-tuning can significantly enhance a model’s performance on specific tasks.
  • Reduced Training Time: Fine-tuning typically requires less training time and computational resources compared to training from scratch.
  • Adaptation to Specific Domains: Fine-tuning helps models better adapt to data and tasks specific to certain domains.

SiliconCloud platform provides efficient model fine-tuning capabilities. Currently, the following models support fine-tuning:

  • Image Generation Models:

    • black-forest-labs/FLUX.1-dev
  • Chat Models:

    • Qwen/Qwen2.5-7B-Instruct
    • Qwen/Qwen2.5-14B-Instruct
    • Qwen/Qwen2.5-32B-Instruct
    • Qwen/Qwen2.5-72B-Instruct
    • meta-llama/Meta-Llama-3.1-8B-Instruct

For the latest supported models, refer to Model Fine-Tuning.

2. Usage Flow

2.1 Data Preparation

2.2.1 Data Preparation for Image Generation Models

The dataset requirements are as follows: 7. 对于 FLUX.1 模型,图片描述信息不超过 512 tokens,过长内容会被截断

  1. The dataset should include both images and corresponding text descriptions in the same local folder.
  2. Each dataset can contain up to 100 images and their text descriptions.
  3. Supported image formats include .jpg, .jpeg, .png, .webp. It is recommended to have a resolution of 1024×1024, 1024×768, or 768×1024.
  4. Images that do not meet the above resolution will be resized and cropped to the specified resolution during training.
  5. Text descriptions should be saved as .txt files with the same name as the images.
  6. The entire dataset should not exceed 300MB, and each text description file should not exceed 128KB.
  7. For the FLUX.1 model, the text description should not exceed 512 tokens. Longer content will be truncated. Example of a local dataset folder:

2.2.2 Data Preparation for Language Models

Only .jsonl files are supported, and they must meet the following requirements:

  1. Each line is an independent JSON object.
  2. Each object must contain an array with the key messages, and the array must not be empty.
  3. Each element in the messages array must contain role and content fields.
  4. role can only be system, user, or assistant.
  5. If there are system role messages, they must be at the beginning of the array.
  6. The first non-system message must be from the user role.
  7. user and assistant role messages should alternate and appear in pairs, with at least one pair.

Example of data:

{"messages": [{"role": "system", "content": "你是客服助手"}, {"role": "user", "content": "如何修改收货地址?"}, {"role": "assistant", "content": "您可以按以下步骤修改:\n1. 进入订单详情\n2. 点击'修改地址'\n3. 输入新地址信息\n4. 保存修改"}]}
{"messages": [{"role": "system", "content": "你是客服助手"}, {"role": "user", "content": "如何指定收获时间"}, {"role": "assistant", "content": "您可以按以下步骤修改:\n1. 进入订单详情\n2. 点击'修改送货时间'\n3. 输入新收获时间\n4. 保存修改"}]}

2.2 Create and Configure Fine-Tuning Task

  • Choose Chat Model Fine-Tuning or Image Generation Model Fine-Tuning
  • Fill in the task name
  • Select the base model
  • Upload or select already uploaded training data
  • Set validation data, which can be split from the training set (default 10%) or selected separately
  • Configure training parameters

2.3 Start Training

  • Click “Start Fine-Tuning”
  • Wait for the task to complete
  • Get the model identifier

2.4 Call Fine-Tuned Model

2.4.1 Calling Fine-Tuned Image Generation Model

  • Copy the model identifier
  • Call the fine-tuned LoRA through the /image/generations API. For more details, see the API documentation. Example:
import requests

url = "https://api.siliconflow.cn/v1/images/generations"
payload = {
    "prompt": "an island near sea, with seagulls, moon shining over the sea, light house, boats int he background, fish flying over the sea",
    "image_size": "1024x1024",
    "model": "LoRA/black-forest-labs/FLUX.1-dev",
    "loras": [
        {
            "model_id": "cm04pf7az00061413w7kz5qxs:changdu:pazlgyppednebxesxqmx:epoch_2.safetensors",
            "strength": 0.5,
        },
        {
            "model_id": "cm04pf7az00061413w7kz5qxs:changdu:pazlgyppednebxesxqmx:epoch_1.safetensors",
            "strength": 0.5,
        },
        {
            "model_id": "cm04pf7az00061413w7kz5qxs:changdu:pazlgyppednebxesxqmx:epoch.safetensors",
            "strength": 0.5,
        }
    ]
}
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
print(response.text)

2.4.2 Calling Fine-Tuned Chat Model

  • Copy the model identifier

In the Fine-Tuning Page, copy the corresponding model identifier.

  • Call the fine-tuned model directly through the /chat/completions API.

Example using the OpenAI chat.completions interface:

from openai import OpenAI
client = OpenAI(
    api_key="您的 APIKEY", # 从https://cloud.siliconflow.cn/account/ak获取
    base_url="https://api.siliconflow.cn/v1"
)

messages = [
    {"role": "user", "content": "用当前语言解释微调模型流程"},
]

response = client.chat.completions.create(
    model="您的微调模型名",
    messages=messages,
    stream=True,
    max_tokens=4096
)

for chunk in response:
    print(chunk.choices[0].delta.content, end='')

3. Detailed Configuration of Parameters

  1. Basic Training Parameters
Parameter NameDescriptionValue RangeSuggested ValueUsage Recommendation
Trigger Word【[Only for Image Generation] Trigger WordAdded to the beginning of the description of each image during training
Number of Repeats[Only for Image Generation] Number of times to train a single image
Learning RateLearning Rate0-0.10.0001
Number of EpochsNumber of Training Epochs1-103
Batch SizeBatch Size1-328
Max TokensMaximum Number of Tokens0-40964096Set based on actual dialogue length needs
  1. LoRA Parameters
Parameter NameDescriptionValue RangeSuggested ValueUsage Recommendation
LoRA RankMatrix Rank1-648
LoRA AlphaScaling Factor1-12832
LoRA DropoutDropout Rate0-1.00.05
  1. Scenario Configuration Scheme

Chat Models

ScenarioLearning RateEpochsBatch SizeLoRA RankLoRA AlphaDropout
Standard Scheme0.0001388320.05
Effect Priority0.000151616640.1
Lightweight Fast0.0001284160.05

4. Optimizing Business Practice with SiliconCloud Fine-Tuning Service

Previously, SiliconCloud developed the Zhi Shuo Xin Yu application, providing a complex prompt to let the large model generate “golden sentence” style descriptions.

Now, we can use the platform’s fine-tuning function to compress the prompt and enhance the effect, making the text generation style more uniform, faster, and further optimizing costs.

4.1 Fine-tune the data from the “Zhi Shuo Xin Yu” corpus on the platform

according to the above steps. See Model Fine-Tuning Usage Flow for details. Detailed corpus and test code can be found in siliconcloud-cookbook.

4.2 Compare the effects before and after fine-tuning.

See Model Fine-Tuning Call Model for usage.

4.2.1 Model Input

  • Before Fine-Tuning: Qwen2.5-7B-Instruct System Prompt:

    Qwen2.5-7B-Instruct System Prompt
    # 角色
    你是一位新潮评论家,你年轻、批判,又深刻;
    你言辞犀利而幽默,擅长一针见血得表达隐喻,对现实的批判讽刺又不失文雅;
    你的行文风格和"Oscar Wilde" "鲁迅" "林语堂"等大师高度一致;
    从情感上要是对输入的否定。
    # 任务
    ## 金句诠释
    用特殊视角来全新得诠释给定的汉语词汇;
    敏锐得抓住给定的词汇的本质,用“辛辣的讽刺”“一针见血的评论”的风格构造包含隐喻又直达本质的「金句」
    例如:
    "合伙人""一同下海捞金时,个个都是乘风破浪的水手,待到分金之际,方知彼此是劫财的海盗。"
    "大数据""看似无所不能的数字神明,实则不过是现代社会的数字鸦片,让人沉溺于虚幻的精准,却忽略了人性的复杂与多变。"
    "股市""万人涌入的淘金场,表面上是财富的摇篮,实则多数人成了填坑的沙土。"
    "白领""西装革履,看似掌握命运的舵手,实则不过是写字楼里的高级囚徒。"
    "金融家""在金钱的海洋中遨游,表面上是操纵风浪的舵手,实则不过是随波逐流的浮萍。"
    "城市化""乡村的宁静被钢铁森林吞噬,人们在追逐繁华的幻影中,遗失了心灵的田园。"
    "逃离北上广""逃离繁华的都市牢笼,看似追逐自由的灵魂,实则不过是换个地方继续画地为牢。"
    "基金""看似为财富增值保驾护航的金融巨轮,实则多数人不过是随波逐流的浮萍,最终沦为填补市场波动的牺牲品。"
    # 输入
    用户直接输入词汇。
    # 输出
    严格输出JSON格式,包括两个字段,“prompt”为用户的输入;“output”为用户的金句内容,不额外输出额外任何其他内容,不要输出引号,严格限制用户的输入的词汇绝对不能出现在输出中,注意突出转折和矛盾,输出内容为一句话,最后以“。”结束,中间的停顿使用“,”分隔。例如 
    {
      "prompt": "合伙人",
      "output": "一同下海捞金时,个个都是乘风破浪的水手,待到分金之际,方知彼此是劫财的海盗。"
    }
    
  • Qwen2.5-7B-Instruct+Zhi Shuo Xin Yu Fine-Tuned Prompt:

    你是智说新语生成器
    

4.2.2 Model Output

InputQwen2.5-7B-Instruct ModelQwen2.5-7B-Instruct Fine-Tuned Model
五道口VLG,不过是知识的轻轨与资本的交汇站,文青与土豪在此擦肩而过,孰是求知的信徒,孰是逐利的信徒,全凭幸存者的微笑。知识的殿堂与商业的洪流交织,学人的笔记与商人的谈资齐飞,地名犹在,荣光已非。
新时代claimed to break旧 mold yet still clings to the stirring rhetoric of progress, like a fashion plate who ignores the evolution of fashion。新时代的号角已吹响,然砥砺前行时,方显初心易失之难。
创新看似凤凰涅槃的奇迹,实则多数人不过是换汤不换药的换皮游戏。标新立异本是勇气的象征,却在同质化的社会中成了猎奇的代名词。
降维打击所谓降维打击,实则是现代文明中的一种高级愚民术,用高深莫测的科技iscard,让普通人以为自己正生活在理性与进步之中,却不知已在智力与道德的低维空间中遭受碾压。。高维智慧轻而易举地碾压低维凡尘,却不知低维众生早已习于在自己的平庸中寻觅慰藉,哪里还懂得高维打击的意义。
基金看似为财富增值保驾护航的金融巨轮,实则多数人不过是随波逐流的浮萍,最终沦为填补市场波动的牺牲品。投资者的白日梦,用钱铺路,却常跌入夜的陷阱。

4.2.3 Fine-Tuning Summary

  1. The output content after fine-tuning is more uniform in style, and the output effect is more stable and controllable.
  2. The entire input length is significantly reduced after fine-tuning, from 553 tokens to 8 tokens, greatly reducing the input token length, making it faster and further optimizing costs.