-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Model] Add DeepSeek-OCR-2 PaddlePaddle implementation with full SFT and LoRA support #4324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
forBlank
wants to merge
9
commits into
PaddlePaddle:develop
Choose a base branch
from
forBlank:deepseek_ocr2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ebe284
[model(deepseek_v3)]: add GQA mode support alongside MLA
forBlank 3af2e1e
[model(deepseek_ocr2)]: add DeepSeek-OCR-2 model definition
forBlank f8f5b5b
[model(deepseek_ocr2)]: register DeepSeek-OCR-2 to transformers auto …
forBlank 543a5fa
[model(deepseek_ocr2)]: add DeepSeek-OCR-2 data preprocessing, collat…
forBlank 2f58654
[model(deepseek_ocr2)]: add LoRA target modules for DeepSeek-OCR-2
forBlank 1bf9233
[model(deepseek_ocr2)]: add full and LoRA SFT training config for Dee…
forBlank 7d37a4b
[model(deepseek_ocr2)]: add unit tests for DeepSeek-OCR-2 model
forBlank 63b5058
Merge remote-tracking branch 'upstream/develop' into deepseek_ocr2
forBlank 1e1db87
[model(deepseek_v3)] compatible support gready topk_method
forBlank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
examples/best_practices/DeepSeek-OCR-2/deepseek_ocr2_full_8k_config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| ### data | ||
| train_dataset_type: messages | ||
| eval_dataset_type: messages | ||
| train_dataset_path: ./ocr_vl_sft-train_Bengali.jsonl | ||
| train_dataset_prob: "1.0" | ||
| eval_dataset_path: ./ocr_vl_sft-test_Bengali.jsonl | ||
| eval_dataset_prob: "1.0" | ||
| max_seq_len: 8192 | ||
| padding_free: False | ||
| packing: False | ||
| truncate_packing: False | ||
| dataset_type: map | ||
| dataloader_num_workers: 8 | ||
| mix_strategy: concat | ||
| template_backend: custom | ||
| template: deepseek_ocr2 | ||
|
|
||
| ### model | ||
| model_name_or_path: deepseek-ai/DeepSeek-OCR-2 | ||
| _attn_implementation: flashmask | ||
| copy_custom_file_list: "configuration_deepseek_v2.py conversation.py deepencoderv2.py modeling_deepseekocr2.py modeling_deepseekv2.py" | ||
|
|
||
| ### finetuning | ||
| # base | ||
| stage: VL-SFT | ||
| fine_tuning: full | ||
| seed: 42 | ||
| do_train: true | ||
| do_eval: true | ||
| per_device_eval_batch_size: 8 | ||
| per_device_train_batch_size: 8 | ||
| num_train_epochs: 2 | ||
| max_steps: -1 | ||
| max_estimate_samples: 500 | ||
| eval_steps: 400 | ||
| evaluation_strategy: steps | ||
| save_steps: 400 | ||
| save_strategy: steps | ||
| logging_steps: 1 | ||
| gradient_accumulation_steps: 8 | ||
| logging_dir: ./Deepseek-OCR2-Bengali/visualdl_logs/ | ||
| output_dir: ./Deepseek-OCR2-SFT-Bengali | ||
| disable_tqdm: true | ||
| eval_accumulation_steps: 16 | ||
|
|
||
| # train | ||
| lr_scheduler_type: cosine | ||
| warmup_ratio: 0.01 | ||
| learning_rate: 5.0e-6 | ||
| min_lr: 5.0e-7 | ||
|
|
||
| # optimizer | ||
| weight_decay: 0.1 | ||
| adam_epsilon: 1.0e-8 | ||
| adam_beta1: 0.9 | ||
| adam_beta2: 0.95 | ||
|
|
||
| # performance | ||
| tensor_model_parallel_size: 1 | ||
| pipeline_model_parallel_size: 1 | ||
| sharding: stage1 | ||
| recompute_granularity: full | ||
| recompute_method: uniform | ||
| recompute_num_layers: 1 | ||
| bf16: true | ||
| fp16_opt_level: O2 | ||
| # pre_alloc_memory: 42 | ||
|
|
||
| # save | ||
| unified_checkpoint: False | ||
| save_checkpoint_format: "flex_checkpoint" | ||
| load_checkpoint_format: "flex_checkpoint" | ||
75 changes: 75 additions & 0 deletions
75
examples/best_practices/DeepSeek-OCR-2/deepseek_ocr2_lora_8k_config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| ### data | ||
| train_dataset_type: messages | ||
| eval_dataset_type: messages | ||
| train_dataset_path: ./ocr_vl_sft-train_Bengali.jsonl | ||
| train_dataset_prob: "1.0" | ||
| eval_dataset_path: ./ocr_vl_sft-test_Bengali.jsonl | ||
| eval_dataset_prob: "1.0" | ||
| max_seq_len: 8192 | ||
| padding_free: False | ||
| packing: False | ||
| truncate_packing: False | ||
| dataset_type: map | ||
| dataloader_num_workers: 8 | ||
| mix_strategy: concat | ||
| template_backend: custom | ||
| template: deepseek_ocr2 | ||
|
|
||
| ### model | ||
| model_name_or_path: deepseek-ai/DeepSeek-OCR-2 | ||
| _attn_implementation: flashmask | ||
| lora: true | ||
| lora_rank: 8 | ||
| lora_alpha: 32 | ||
| copy_custom_file_list: "configuration_deepseek_v2.py conversation.py deepencoderv2.py modeling_deepseekocr2.py modeling_deepseekv2.py" | ||
|
|
||
| ### finetuning | ||
| # base | ||
| stage: VL-SFT | ||
| fine_tuning: lora | ||
| seed: 42 | ||
| do_train: true | ||
| do_eval: true | ||
| per_device_eval_batch_size: 8 | ||
| per_device_train_batch_size: 8 | ||
| num_train_epochs: 1 | ||
| max_steps: -1 | ||
| max_estimate_samples: 500 | ||
| eval_steps: 400 | ||
| evaluation_strategy: steps | ||
| save_steps: 400 | ||
| save_strategy: steps | ||
| logging_steps: 1 | ||
| gradient_accumulation_steps: 8 | ||
| logging_dir: ./Deepseek-OCR2-Bengali-lora/visualdl_logs/ | ||
| output_dir: ./Deepseek-OCR2-SFT-Bengali-lora | ||
| disable_tqdm: true | ||
| eval_accumulation_steps: 16 | ||
|
|
||
| # train | ||
| lr_scheduler_type: cosine | ||
| warmup_ratio: 0.01 | ||
| learning_rate: 5.0e-4 | ||
| min_lr: 5.0e-5 | ||
|
|
||
| # optimizer | ||
| weight_decay: 0.1 | ||
| adam_epsilon: 1.0e-8 | ||
| adam_beta1: 0.9 | ||
| adam_beta2: 0.95 | ||
|
|
||
| # performance | ||
| tensor_model_parallel_size: 1 | ||
| pipeline_model_parallel_size: 1 | ||
| sharding: stage1 | ||
| recompute_granularity: full | ||
| recompute_method: uniform | ||
| recompute_num_layers: 1 | ||
| bf16: true | ||
| fp16_opt_level: O2 | ||
| # pre_alloc_memory: 45 | ||
|
|
||
| # save | ||
| unified_checkpoint: False | ||
| save_checkpoint_format: "flex_checkpoint" | ||
| load_checkpoint_format: "flex_checkpoint" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -420,6 +420,33 @@ def get_lora_target_modules(model): | |
| "model.visual.blocks.*mlp.up_proj.*", | ||
| "model.visual.blocks.*mlp.down_proj.*", | ||
| ] | ||
| elif model.config.model_type == "deepseek_ocr2": | ||
| target_modules = [ | ||
| # Language Model (DeepseekV3) | ||
| ".*model.*q_proj.*", | ||
| ".*model.*q_a_proj.*", | ||
| ".*model.*q_b_proj.*", | ||
| ".*model.*kv_a_proj_with_mqa.*", | ||
| ".*model.*kv_b_proj.*", | ||
| ".*model.*k_proj.*", | ||
| ".*model.*v_proj.*", | ||
| ".*model.*o_proj.*", | ||
| ".*model.*mlp.gate_proj.*", | ||
| ".*model.*mlp.up_proj.*", | ||
| ".*model.*mlp.down_proj.*", | ||
| # SAM Vision Encoder | ||
| "sam_model.*attn.qkv.*", | ||
| "sam_model.*attn.proj.*", | ||
| "sam_model.*mlp.lin1.*", | ||
| "sam_model.*mlp.lin2.*", | ||
| # Qwen2 Encoder-as-Decoder | ||
| "qwen2_model.*self_attn.qkv_proj.*", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lora的时候要训练VIT吗 |
||
| "qwen2_model.*self_attn.o_proj.*", | ||
| "qwen2_model.*mlp.up_gate_proj.*", | ||
| "qwen2_model.*mlp.down_proj.*", | ||
| # Projector | ||
| "projector.*", | ||
| ] | ||
| else: | ||
| raise ValueError(f"Unknown base_model_prefix: {model.config.model_type}.") | ||
| return target_modules | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
新增一个文档简单介绍一下,并提供数据集./ocr_vl_sft-train_Bengali.jsonl下载方式