From c0794cc9e92d9db124372883d4c669ced2c87d14 Mon Sep 17 00:00:00 2001 From: wiILIL <975202246@qq.com> Date: Thu, 27 Nov 2025 16:19:29 +0800 Subject: [PATCH] revise offline_tsds,near and remove origin tsds --- docs/.vuepress/notes/en/guide.ts | 3 +- docs/.vuepress/notes/zh/guide.ts | 3 +- .../guide/selector/selector_offline_near.md | 201 ++++++++++++++++ ...ector_tsds.md => selector_offline_tsds.md} | 123 ++++------ .../guide/selector/selector_offline_near.md | 218 ++++++++++++++++++ ...ector_tsds.md => selector_offline_tsds.md} | 100 ++++---- 6 files changed, 516 insertions(+), 132 deletions(-) create mode 100644 docs/en/notes/guide/selector/selector_offline_near.md rename docs/en/notes/guide/selector/{selector_tsds.md => selector_offline_tsds.md} (58%) create mode 100644 docs/zh/notes/guide/selector/selector_offline_near.md rename docs/zh/notes/guide/selector/{selector_tsds.md => selector_offline_tsds.md} (69%) diff --git a/docs/.vuepress/notes/en/guide.ts b/docs/.vuepress/notes/en/guide.ts index c4cff1b..ce20914 100644 --- a/docs/.vuepress/notes/en/guide.ts +++ b/docs/.vuepress/notes/en/guide.ts @@ -25,7 +25,8 @@ export const Guide: ThemeNote = defineNoteConfig({ 'quickstart', 'tutorial', 'selector_less', - 'selector_tsds', + 'selector_offline_tsds', + 'selector_offline_near', 'selector_zeroth' ], }, diff --git a/docs/.vuepress/notes/zh/guide.ts b/docs/.vuepress/notes/zh/guide.ts index 0b86798..5f56c23 100644 --- a/docs/.vuepress/notes/zh/guide.ts +++ b/docs/.vuepress/notes/zh/guide.ts @@ -25,7 +25,8 @@ export const Guide: ThemeNote = defineNoteConfig({ 'quickstart', 'tutorial', 'selector_less', - 'selector_tsds', + 'selector_offline_tsds', + 'selector_offline_near', 'selector_zeroth', ], }, diff --git a/docs/en/notes/guide/selector/selector_offline_near.md b/docs/en/notes/guide/selector/selector_offline_near.md new file mode 100644 index 0000000..9180035 --- /dev/null +++ b/docs/en/notes/guide/selector/selector_offline_near.md @@ -0,0 +1,201 @@ +--- +title: Offline-Near-Selector +createTime: 2025/11/27 16:02:41 +permalink: /en/guide/7k0w3d92/ +icon: flowbite:fish-alt-outline +--- +# Offline NEAR Selector + +This document introduces how to use the **Offline NEAR Selector** for **dynamic data selection** during supervised fine-tuning (SFT) within the **DataFlex** framework, finding the most close data to the target dataset to improve generalization performance. + +--- + +## 1. Method Overview + +The core idea of **NEAR** is: + +* Further encode **already tokenized** samples into **sentence embeddings** (e.g., 512‑dim). +* Perform **nearest‑neighbor search ** in the embedding space to obtain each sample’s representativeness score. + +> Intuition: **Closest data for the target dataset** + +### Scoring Formulation + +Let the sentence embedding of a sample be $e_i$, and let its $max_K$ nearest neighbors be $\mathcal{N}_K(i)$. + + + +--- + +## 2. Environment & Dependencies + +```bash +# DataFlex (recommended: editable install) +git clone https://github.com/OpenDCAI/DataFlex.git +cd DataFlex +pip install -e . + +# Common training/inference dependencies (as needed) +pip install llamafactory + +# NEAR extras (vector search & progress bars) +pip install faiss-cpu vllm sentence-transformer +``` + +--- + +## 3. Offline Selection + +Modify training set, embedding model, and parameters inside +**DataFlex/src/dataflex/offline_selector/offline_near_selector.py**: +```python +if __name__ == "__main__": + near = offline_near_Selector( + candidate_path="OpenDCAI/DataFlex-selector-openhermes-10w", # split = train + query_path="OpenDCAI/DataFlex-selector-openhermes-10w", # split = vaildation + + # If you want to use vllm,please add "vllm:" before model's name + # Otherwise it automatically use sentence-transfromer + embed_model="vllm:Qwen/Qwen3-Embedding-0.6B", + batch_size=32, + save_indices_path="top_indices.npy", + max_K=1000, + + ) + near.selector() +``` + +Note: model_name is used to encode the already-tokenized text into sentence embeddings (e.g., 512-dim), supporting both vLLM and sentence-transformer inference. + +Output: save as the indices matrix that contain the max_K close data for each query +--- + +## 4. Key Hyperparameters & Tips + +| Parameter | Typical Range | Meaning & Tips | +| ------------- | ------------- | --------------------------------------------------------------------------------------------- | +| `max_K` | 64–10000 | Upper bound of NN retrieval. Larger = stabler but more costly; balance with data size & VRAM. | | +| `model_name` | — | Path/name of the sentence encoder (local BERT/USE/SimCSE, etc.). | +| `cache_dir` | — | Cache directory for intermediate artifacts and resume‑from‑cache. | + +--- + +## 5. Component Config (`components.yaml`) + +**Path:** `DataFlex/src/dataflex/configs/components.yaml` + +**Preset example** + +```yaml +near: + name: near + params: + indices_path: ./src/dataflex/offline_selector/top_indices.npy + cache_dir: ../dataflex_saves/near_output + +``` + +--- + +## 6. Dynamic Training Config (LoRA + NEAR) + +**Example file:** `DataFlex/examples/train_lora/selectors/near.yaml` + +```yaml +### model +model_name_or_path: +trust_remote_code: true + +### method +stage: sft +do_train: true +finetuning_type: lora +lora_target: all +lora_rank: 16 +lora_alpha: 8 + +### dataset +dataset: # training dataset +template: qwen +cutoff_len: 4096 +overwrite_cache: true +preprocessing_num_workers: 16 + +### output +output_dir: ../dataflex_saves +logging_steps: 10 +save_steps: 100 +plot_loss: true +overwrite_output_dir: true + +### train +per_device_train_batch_size: 2 +gradient_accumulation_steps: 16 +learning_rate: 1.0e-4 +num_train_epochs: 1.0 +lr_scheduler_type: cosine +warmup_ratio: 0.1 +bf16: true + +### Dataflex args +train_type: dynamic_select +components_cfg_file: src/dataflex/configs/components.yaml +component_name: near +warmup_step: 400 +update_step: 500 +update_times: 2 + +``` + +**Notes:** + +* `component_name: near` enables the NEAR component. +* `warmup_step / update_step / update_times` decide **when** and **how often** to re‑select the training subset; total steps ≈ `warmup_step + update_step × update_times`. +* total batch_size=device_number x per_device_train_batch_size x gradient_accumulation_steps + +--- + +## 7. Run Training + +```bash +FORCE_TORCHRUN=1 DISABLE_VERSION_CHECK=1 dataflex-cli train examples/train_lora/selectors/near.yaml +``` + +**Note:** the above example runs with distributed launch. + +During training, NEAR is triggered at scheduled steps: base the sample indice → select the next training subset. + +--- + +## 8. Merge & Export the Model + +Same as the Less Selector pipeline. + +**Config file:** `DataFlex/examples/merge_lora/llama3_lora_sft.yaml` + +```yaml +model_name_or_path: base model path +adapter_name_or_path: finetuned adapter path +template: qwen +trust_remote_code: true + +export_dir: ../dataflex_saves +export_size: 5 +export_device: cpu +export_legacy_format: false + +``` + +Run the export command (inside the LLaMA‑Factory directory): + +```bash +llamafactory-cli export llama3_lora_sft.yaml +``` + +--- + +## 9. Evaluation & Comparison + +We recommend using the [DataFlow](https://github.com/OpenDCAI/DataFlow) QA evaluation pipeline to compare **NEAR** against **Less** and **random sampling**. + + diff --git a/docs/en/notes/guide/selector/selector_tsds.md b/docs/en/notes/guide/selector/selector_offline_tsds.md similarity index 58% rename from docs/en/notes/guide/selector/selector_tsds.md rename to docs/en/notes/guide/selector/selector_offline_tsds.md index 807ff23..bee1be4 100644 --- a/docs/en/notes/guide/selector/selector_tsds.md +++ b/docs/en/notes/guide/selector/selector_offline_tsds.md @@ -1,14 +1,14 @@ --- -title: selector_tsds +title: Offline-Tsds-Selector createTime: 2025/11/01 21:36:21 permalink: /en/guide/im5q9cd2/ icon: tdesign:cat --- -# TSDS Selector Guide +# Offline TSDS Selector -This document explains how to use the **TSDS Selector** (Data Selection for Task‑Specific Model Finetuning) in the **DataFlex** framework to perform **dynamic training data selection** during supervised finetuning (SFT), balancing **representative density** and **topological diversity** to improve generalization. +This document introduces how to use the **Offline TSDS Selector** for **dynamic data selection** during supervised fine-tuning (SFT) within the **DataFlex** framework, achieving a balance between **density representativeness** and **diversity** to improve generalization performance. --- @@ -65,52 +65,46 @@ pip install -e . pip install llamafactory # TSDS extras (vector search & progress bars) -pip install faiss-cpu tqdm +pip install faiss-cpu vllm sentence-transformer ``` --- -## 3. Selector Registration & Initialization - -Register a custom TSDS selector component: +## 3. Offline Selection +Modify training set, embedding model, and parameters inside +**DataFlex/src/dataflex/offline_selector/offline_tsds_selector.py**: ```python -from dataflex.selectors import Selector, register_selector - -@register_selector("tsds") -class TsdsSelector(Selector): - """Topological & Statistical Density Selector""" - def __init__( - self, - dataset, - eval_dataset, - accelerator, - data_collator, - cache_dir, - seed: int = 42, - max_K: int = 128, - kde_K: int = 64, - sigma: float = 1.0, - alpha: float = 0.5, - C: float = 10.0, - sample_size: int = 1000, - model_name: str = "/home/lianghao/yry/TSDS/bert_chinese" # sentence encoder path - ): - super().__init__(dataset, accelerator, data_collator, cache_dir) - +if __name__ == "__main__": + tsds = offline_tsds_Selector( + candidate_path="OpenDCAI/DataFlex-selector-openhermes-10w", # training set + query_path="OpenDCAI/DataFlex-selector-openhermes-10w", # validation set + + # If you want to use vllm, please add "vllm:" before the model name + # Otherwise it automatically uses sentence-transformer + embed_model="vllm:Qwen/Qwen3-Embedding-0.6B", # embedding model + batch_size=32, + save_probs_path="tsds_probs.npy", + max_K=5000, + kde_K=1000, + sigma=0.75, + alpha=0.6, + C=5.0 + ) + tsds.selector() ``` -**TODO: Replace `model_name` with your local encoder path. Using the placeholder will raise an error.** -> **Note:** `model_name` is used to encode **tokenized** samples into **sentence embeddings** (e.g., 512‑dim). Common choices include BERT/USE/SimCSE‑style encoders. +Note: model_name is used to encode the already-tokenized text into sentence embeddings (e.g., 512-dim), supporting both vLLM and sentence-transformer inference. +Output: a sampling probability for each training sample. --- ## 4. Key Hyperparameters & Tips | Parameter | Typical Range | Meaning & Tips | | ------------- | ------------- | --------------------------------------------------------------------------------------------- | -| `max_K` | 64–256 | Upper bound of NN retrieval. Larger = stabler but more costly; balance with data size & VRAM. | -| `kde_K` | 16–64 | #neighbors in KDE. Smaller = more sensitive; larger = smoother. Usually `kde_K ≤ max_K`. | +| `max_K` | 64–10000 | Upper bound of NN retrieval. Larger = stabler but more costly; balance with data size & VRAM. | +| `kde_K` | 16–2000 | #neighbors in KDE. Smaller = more sensitive; larger = smoother. Usually `kde_K ≤ max_K`. | | `sigma` | 0.5–2.0 | KDE bandwidth. Too small ⇒ noisy; too large ⇒ oversmoothing. | | `alpha` | 0.3–0.7 | Trade‑off between representativeness (density) and coverage (diversity). | | `C` | 0.01–1.0 | Selection ratio/threshold or regularization strength depending on implementation. | @@ -128,15 +122,12 @@ class TsdsSelector(Selector): ```yaml tsds: - name: tsds - params: - max_K: 128 - kde_K: 64 - sigma: 0.8 - alpha: 0.5 - C: 10.0 - model_name: "/home/lianghao/yry/TSDS/bert_chinese" - cache_dir: ../dataflex_saves/tsds_output + name: tsds + params: + probs_path: ./src/dataflex/offline_selector/tsds_probs.npy + # default location of tsds_probs.npy + cache_dir: ../dataflex_saves/tsds_output + ``` --- @@ -147,7 +138,7 @@ tsds: ```yaml ### model -model_name_or_path: /home/lianghao/yry/LLaMA-Factory/Qwen2.5-0.5B-Instruct +model_name_or_path: trust_remote_code: true ### method @@ -157,37 +148,21 @@ finetuning_type: lora lora_target: all lora_rank: 16 lora_alpha: 8 -# deepspeed: examples/deepspeed/ds_z3_config.json # choices: [ds_z0_config.json, ds_z2_config.json, ds_z3_config.json] ### dataset -dataset: alpaca_en_demo +dataset: # training dataset template: qwen cutoff_len: 4096 -# max_samples: 100000000 overwrite_cache: true preprocessing_num_workers: 16 -dataloader_num_workers: 0 -# disable_shuffling: true -seed: 42 ### output -output_dir: ../dataflex_saves/qwen/tsds +output_dir: ../dataflex_saves logging_steps: 10 save_steps: 100 plot_loss: true -save_only_model: false overwrite_output_dir: true -### swanlab -report_to: none # choices: [none, wandb, tensorboard, swanlab, mlflow] -# use_swanlab: true -# swanlab_project: medical_dynamic_sft -# swanlab_run_name: qwen2_5_3b_lora_medical_50k_baseline -# swanlab_workspace: word2li -# swanlab_api_key: -# swanlab_lark_webhook_url: -# swanlab_lark_secret: - ### train per_device_train_batch_size: 2 gradient_accumulation_steps: 16 @@ -196,37 +171,34 @@ num_train_epochs: 1.0 lr_scheduler_type: cosine warmup_ratio: 0.1 bf16: true -ddp_timeout: false ### Dataflex args -train_type: dynamic_select # trainer type: - # "dynamic_select" | "dynamic_mix" | "dynamic_weight" | "static" +train_type: dynamic_select components_cfg_file: src/dataflex/configs/components.yaml -component_name: tsds # must match the name in components_cfg_file +component_name: tsds warmup_step: 400 update_step: 500 update_times: 2 -# eval_dataset: alpaca_zh_demo -eval_dataset: alpaca_zh_demo + ``` **Notes:** * `component_name: tsds` enables the TSDS component. * `warmup_step / update_step / update_times` decide **when** and **how often** to re‑select the training subset; total steps ≈ `warmup_step + update_step × update_times`. -* `eval_dataset` provides the **target distribution** reference for similarity/representativeness scoring. +* total batch_size=device_number x per_device_train_batch_size x gradient_accumulation_steps --- ## 7. Run Training ```bash -FORCE_TORCHRUN=0 DISABLE_VERSION_CHECK=1 dataflex-cli train examples/train_lora/selectors/tsds.yaml +FORCE_TORCHRUN=1 DISABLE_VERSION_CHECK=1 dataflex-cli train examples/train_lora/selectors/tsds.yaml ``` -**Note:** the above example runs without distributed launch. +**Note:** the above example runs with distributed launch. -During training, TSDS is triggered at scheduled steps: encode training samples → NN search / KDE → combine with diversity → select the next training subset. +During training, TSDS is triggered at scheduled steps: base the sample probablity → select the next training subset. --- @@ -237,15 +209,16 @@ Same as the Less Selector pipeline. **Config file:** `DataFlex/examples/merge_lora/llama3_lora_sft.yaml` ```yaml -model_name_or_path: -adapter_name_or_path: +model_name_or_path: base model path +adapter_name_or_path: finetuned adapter path template: qwen trust_remote_code: true -export_dir: ../dataflex_saves/Qwen2.5-0.5B_lora_sft +export_dir: ../dataflex_saves export_size: 5 export_device: cpu export_legacy_format: false + ``` Run the export command (inside the LLaMA‑Factory directory): diff --git a/docs/zh/notes/guide/selector/selector_offline_near.md b/docs/zh/notes/guide/selector/selector_offline_near.md new file mode 100644 index 0000000..8172975 --- /dev/null +++ b/docs/zh/notes/guide/selector/selector_offline_near.md @@ -0,0 +1,218 @@ +--- +title: Offline-Near数据选择器 +createTime: 2025/11/26 23:42:41 +permalink: /zh/guide/acgesu99/ +icon: flowbite:fish-alt-outline +--- +# Offline NEAR Selector 使用介绍 + +本文档介绍如何在 **DataFlex** 框架中使用 **Offline NEAR Selector** 实现训练数据的**动态选择**,以在监督微调(SFT)中聚焦于与目标集的相似度,进行邻近选择。 + +--- + +## 1. 方法概述 + +**NEAR** 的核心思想是: + +* 先将**已分词(tokenized)**的样本进一步编码为**句向量**(例如 512 维)。 +* 在嵌入空间中进行**近邻搜索 **,得到每个样本与目标集的“样本相似度”。 + + +> 直观理解:选择与目标集最接近的训练数据,以最优化训练目标。 + + +--- + +## 2. 环境与依赖 + +```bash +# DataFlex(建议源码安装) +git clone https://github.com/OpenDCAI/DataFlex.git +cd DataFlex +pip install -e . + +# 训练与推理的常用依赖 +pip install llamafactory==0.9.3 + +# NEAR 额外依赖(向量检索与进度条等) +pip install faiss-cpu vllm sentence-transformer +``` + +--- + +## 3. offline 数据选择 + +在DataFlex\src\dataflex\offline_selector\offline_near_selector.py文件中修改训练集、编码模型和参数 +```python +if __name__ == "__main__": + near = offline_near_Selector( + candidate_path="OpenDCAI/DataFlex-selector-openhermes-10w", # split = train + query_path="OpenDCAI/DataFlex-selector-openhermes-10w", # split = vaildation + + # If you want to use vllm,please add "vllm:" before model's name + # Otherwise it automatically use sentence-transfromer + embed_model="vllm:Qwen/Qwen3-Embedding-0.6B", + batch_size=32, + save_indices_path="top_indices.npy", + max_K=1000, + + ) + near.selector() + +``` + +> **注意**:此处的 `model_name` 用于将**tokenized**后的文本进一步编码为**句向量**(例如 512 维),支持vllm和sentence-transformer 推理。 + +**最终保存为每个query的max_K个最邻近训练数据的索引矩阵 ( N ,max_K )** + +--- + +## 4. 关键超参数与建议 + +| 参数 | 典型范围 | 含义与建议 | +| ------------- | -------- | ----------------------------------------- | +| `max_K` | 64–2000 | 近邻检索数量上限,越大越稳但开销更高;建议与数据规模/显存权衡 | | +| `model_name` | — | 句向量编码模型路径或名称(如本地embeddingm模型) | +| `cache_dir` | — | 中间结果缓存路径,便于断点续跑 | + +--- + +## 5. 组件配置(components.yaml) + +**路径:** `DataFlex/src/dataflex/configs/components.yaml` + +**预设参数** + +```yaml +near: + name: near + params: + indices_path: ./src/dataflex/offline_selector/top_indices.npy + cache_dir: ../dataflex_saves/near_output + +``` + +--- + +## 6. 动态训练配置(LoRA + NEAR) + +**示例文件:** `DataFlex/examples/train_lora/selectors/near.yaml` + +```yaml +### model +model_name_or_path: #模型地址 +trust_remote_code: true + +### method +stage: sft +do_train: true +finetuning_type: lora +lora_target: all +lora_rank: 16 +lora_alpha: 8 +# deepspeed: examples/deepspeed/ds_z3_config.json # choices: [ds_z0_config.json, ds_z2_config.json, ds_z3_config.json] + +### dataset +dataset: #训练集 +template: qwen (训练模型类型:qwen、llama...) +cutoff_len: 4096 +# max_samples: 100000000 +overwrite_cache: true +preprocessing_num_workers: 16 +dataloader_num_workers: 0 +# disable_shuffling: true +seed: 42 + +### output +output_dir: ../dataflex_saves +logging_steps: 10 +save_steps: 100 +plot_loss: true +save_only_model: false +overwrite_output_dir: true + +### swanlab +report_to: none # choices: [none, wandb, tensorboard, swanlab, mlflow] +# use_swanlab: true +# swanlab_project: medical_dynamic_sft +# swanlab_run_name: qwen2_5_3b_lora_medical_50k_baseline +# swanlab_workspace: word2li +# swanlab_api_key: AnLWTMijcbd4cyEfundi3 +# swanlab_lark_webhook_url: https://open.feishu.cn/open-apis/bot/v2/hook/ff10a391-4e51-4481-97ff-965760cae2a1 +# swanlab_lark_secret: cySzwTbCJh08349FGAhBSf + +### train +per_device_train_batch_size: 2 +gradient_accumulation_steps: 16 +learning_rate: 1.0e-4 +num_train_epochs: 1.0 +lr_scheduler_type: cosine +warmup_ratio: 0.1 +bf16: true +ddp_timeout: false + +### Dataflex args +train_type: dynamic_select # 选择训练器类型。可选值包括: + # "dynamic_select" - 动态选择训练器 + # "dynamic_mix" - 动态混合训练器 + # "dynamic_weight" - 动态加权训练器 + # "static" - 默认静态训练器 +components_cfg_file: src/dataflex/configs/components.yaml +component_name: near # 选择组件名称,对应 components_cfg_file 中定义的组件 +warmup_step: 400 +update_step: 500 +update_times: 2 +# eval_dataset: alpaca_zh_demo + + +``` + +**参数说明:** + +* `component_name: near`:启用 NEAR 组件。 +* `warmup_step / update_step / update_times`:决定**何时**与**多久**进行一次动态选择;总步数 ≈ `warmup_step + update_step × update_times`。 +* 总batch_size=device_number x per_device_train_batch_size x gradient_accumulation_steps + + +--- + +## 7. 运行训练 + +```bash +FORCE_TORCHRUN=1 DISABLE_VERSION_CHECK=1 dataflex-cli train examples/train_lora/selectors/near.yaml +``` +**采用分布式** + +训练过程中会在设定的步数触发 NEAR 动态选择:根据离线选择的样本索引,选出下一阶段训练子集。 + +--- + +## 8. 模型合并与导出 + +与 Less Selector 流程一致: + +**配置文件:** `DataFlex/examples/merge_lora/llama3_lora_sft.yaml` + +```yaml +model_name_or_path: 原模型地址 +adapter_name_or_path: 微调后adpter地址 +template: qwen +trust_remote_code: true + +export_dir: ../dataflex_saves +export_size: 5 +export_device: cpu +export_legacy_format: false +``` + +导出命令: +在llamafactory文件夹中运行 +```bash +llamafactory-cli export llama3_lora_sft.yaml +``` + +--- + +## 9. 评估与对比 + +建议使用 [DataFlow](https://github.com/OpenDCAI/DataFlow) 的模型 QA 评估流水线,对 **NEAR** 与 **Less**、**随机采样** 等策略进行并列评测 \ No newline at end of file diff --git a/docs/zh/notes/guide/selector/selector_tsds.md b/docs/zh/notes/guide/selector/selector_offline_tsds.md similarity index 69% rename from docs/zh/notes/guide/selector/selector_tsds.md rename to docs/zh/notes/guide/selector/selector_offline_tsds.md index a1e65e9..5f3ffdc 100644 --- a/docs/zh/notes/guide/selector/selector_tsds.md +++ b/docs/zh/notes/guide/selector/selector_offline_tsds.md @@ -1,5 +1,5 @@ --- -title: Tsds 数据选择器 +title: Offline-Tsds 数据选择器 createTime: 2025/11/01 21:35:45 permalink: /zh/guide/vkqfowej/ icon: tdesign:cat @@ -7,9 +7,9 @@ icon: tdesign:cat --- -# TSDS Selector 使用介绍 +# Offline TSDS Selector 使用介绍 -本文档介绍如何在 **DataFlex** 框架中使用 **TSDS Selector** Data Selection for Task-Specific Model Finetuning实现训练数据的**动态选择**,以在监督微调(SFT)中兼顾**密度代表性**与**多样性**,提升泛化效果。 +本文档介绍如何在 **DataFlex** 框架中使用 **Offline TSDS Selector** Data Selection for Task-Specific Model Finetuning实现训练数据的**动态选择**,以在监督微调(SFT)中兼顾**密度代表性**与**多样性**,提升泛化效果。 --- @@ -61,47 +61,42 @@ git clone https://github.com/OpenDCAI/DataFlex.git cd DataFlex pip install -e . -# 训练与推理的常用依赖(按需) -pip install llamafactory +# 训练与推理的常用依赖 +pip install llamafactory==0.9.3 # TSDS 额外依赖(向量检索与进度条等) -pip install faiss-cpu tqdm +pip install faiss-cpu vllm sentence-transformer ``` --- -## 3. 选择器注册与初始化示例 - -在自定义组件中注册 TSDS 选择器: +## 3. offline 数据选择 +在DataFlex\src\dataflex\offline_selector\offline_tsds_selector.py文件中修改训练集、编码模型和参数 ```python -from dataflex.selectors import Selector, register_selector - -@register_selector("tsds") -class TsdsSelector(Selector): - """Topological & Statistical Density Selector""" - def __init__( - self, - dataset, - eval_dataset, - accelerator, - data_collator, - cache_dir, - seed: int = 42, - max_K: int = 128, - kde_K: int = 64, - sigma: float = 1.0, - alpha: float = 0.5, - C: float = 10.0, - sample_size: int = 1000, - model_name: str = "/home/lianghao/yry/TSDS/bert_chinese" # 句向量编码模型 - ): - super().__init__(dataset, accelerator, data_collator, cache_dir) +if __name__ == "__main__": + tsds = offline_tsds_Selector( + candidate_path="OpenDCAI/DataFlex-selector-openhermes-10w",#训练集 + query_path="OpenDCAI/DataFlex-selector-openhermes-10w",#验证集 + + # If you want to use vllm,please add "vllm:" before model's name + # Otherwise it automatically use sentence-transfromer + embed_model="vllm:Qwen/Qwen3-Embedding-0.6B",#编码模型 + batch_size=32, + save_probs_path="tsds_probs.npy", + max_K=5000, + kde_K=1000, + sigma=0.75, + alpha=0.6, + C=5.0 + ) + tsds.selector() ``` - ** TODO: 将模型名字修改成自己本地模型,否则会默认原地址引发报错 ** -> **注意**:此处的 `model_name` 用于将**tokenized**后的文本进一步编码为**句向量**(例如 512 维),常见选择是 BERT/USE 等句向量模型。 +> **注意**:此处的 `model_name` 用于将**tokenized**后的文本进一步编码为**句向量**(例如 512 维),支持vllm和sentence-transformer 推理。 + +**最终保存为每个训练样本的采样概率** --- @@ -109,13 +104,13 @@ class TsdsSelector(Selector): | 参数 | 典型范围 | 含义与建议 | | ------------- | -------- | ----------------------------------------- | -| `max_K` | 64–256 | 近邻检索数量上限,越大越稳但开销更高;建议与数据规模/显存权衡 | -| `kde_K` | 16–64 | 用于密度估计的邻居数,越小更敏感、越大更平滑;通常 `kde_K ≤ max_K` | +| `max_K` | 64-2000 | 近邻检索数量上限,越大越稳但开销更高;建议与数据规模/显存权衡 | +| `kde_K` | 16–10000 | 用于密度估计的邻居数,越小更敏感、越大更平滑;通常 `kde_K ≤ max_K` | | `sigma` | 0.5–2.0 | KDE 的核宽度,过小噪声大,过大易过平滑 | | `alpha` | 0.3–0.7 | 密度 vs 多样性的权衡系数,靠 1 偏重代表性,靠 0 偏重覆盖度 | | `C` | 0.01–1.0 | 用作筛选比例/阈值/正则系数等控制量;与实现细节相关 | | `sample_size` | 500–5000 | 每次候选评估的样本数上限;大幅影响速度与效果 | -| `model_name` | — | 句向量编码模型路径或名称(如本地 BERT/USE) | +| `model_name` | — | 句向量编码模型路径或名称(如本地embedding模型) | | `cache_dir` | — | 中间结果缓存路径,便于断点续跑 | --- @@ -128,15 +123,11 @@ class TsdsSelector(Selector): ```yaml tsds: - name: tsds - params: - max_K: 128 - kde_K: 64 - sigma: 0.8 - alpha: 0.5 - C: 10.0 - model_name: "/home/lianghao/yry/TSDS/bert_chinese" - cache_dir: ../dataflex_saves/tsds_output + name: tsds + params: + probs_path: ./src/dataflex/offline_selector/tsds_probs.npy + #默认离线数据选择所在位置的tsds_probs.npy文件 + cache_dir: ../dataflex_saves/tsds_output ``` --- @@ -147,7 +138,7 @@ tsds: ```yaml ### model -model_name_or_path: /home/lianghao/yry/LLaMA-Factory/Qwen2.5-0.5B-Instruct +model_name_or_path: trust_remote_code: true ### method @@ -160,8 +151,8 @@ lora_alpha: 8 # deepspeed: examples/deepspeed/ds_z3_config.json # choices: [ds_z0_config.json, ds_z2_config.json, ds_z3_config.json] ### dataset -dataset: alpaca_en_demo -template: qwen +dataset: #训练集 +template: qwen (训练模型类型:qwen、llama...) cutoff_len: 4096 # max_samples: 100000000 overwrite_cache: true @@ -171,7 +162,7 @@ dataloader_num_workers: 0 seed: 42 ### output -output_dir: ../dataflex_saves/qwen/tsds +output_dir: ../dataflex_saves logging_steps: 10 save_steps: 100 plot_loss: true @@ -210,7 +201,6 @@ warmup_step: 400 update_step: 500 update_times: 2 # eval_dataset: alpaca_zh_demo -eval_dataset: alpaca_zh_demo ``` @@ -218,18 +208,18 @@ eval_dataset: alpaca_zh_demo * `component_name: tsds`:启用 TSDS 组件。 * `warmup_step / update_step / update_times`:决定**何时**与**多久**进行一次动态选择;总步数 ≈ `warmup_step + update_step × update_times`。 -* `eval_dataset`:为 TSDS 提供“目标分布”的参考(决定相似度/代表性评估的方向)。 +* 总batch_size=device_number x per_device_train_batch_size x gradient_accumulation_steps --- ## 7. 运行训练 ```bash -FORCE_TORCHRUN=0 DISABLE_VERSION_CHECK=1 dataflex-cli train examples/train_lora/selectors/tsds.yaml +FORCE_TORCHRUN=1 DISABLE_VERSION_CHECK=1 dataflex-cli train examples/train_lora/selectors/tsds.yaml ``` -**不用采用分布式** +**采用分布式** -训练过程中会在设定的步数触发 TSDS 动态选择:编码训练样本 → 近邻检索/密度估计 → 结合多样性打分 → 选出下一阶段训练子集。 +训练过程中会在设定的步数触发 TSDS 动态选择:根据离线选择的样本采样概率,选出下一阶段训练子集。 --- @@ -245,7 +235,7 @@ adapter_name_or_path: 微调后adpter地址 template: qwen trust_remote_code: true -export_dir: ../dataflex_saves/Qwen2.5-0.5B_lora_sft +export_dir: ../dataflex_saves export_size: 5 export_device: cpu export_legacy_format: false