OPD icon indicating copy to clipboard operation
OPD copied to clipboard

关于reverse kl和forward kl

Open QUTGXX opened this issue 5 months ago • 4 comments

您好,我是一名菜鸟, 希望您能解答我的困惑。

看到您提供了sample opd的shell脚本,有些问题想和您探讨一下。

export DISTILLATION_LOSS_MODE=forward_kl_topk export USE_POLICY_GRADIENT=False 那这边我看了下verl的源码,应该对应的是forward kl而不是OPD中推荐的reverse kl。 是因为使用了topk嘛?

还想请教一个问题,什么时候使用sampled-token OPD和 top-k KL OPD呢?

以下是我使用一台4卡a800训练的配置文件,目前loss水平欠佳,烦请您给点指导,谢谢。

# ===== 训练参数 =====
ARGS=(
    data.train_files="$TRAIN_DATA"
    data.val_files="$VAL_DATA"
    actor_rollout_ref.model.path="$STUDENT_MODEL"
    algorithm.adv_estimator=grpo
#    critic.model.path="$STUDENT_MODEL"
#    critic.ppo_micro_batch_size_per_gpu=1
#    critic.fsdp.param_offload=True
#    critic.fsdp.optimizer_offload=True
#    critic.model.enable_gradient_checkpointing=True
    trainer.n_gpus_per_node=2
    distillation.n_gpus_per_node=2

    actor_rollout_ref.actor.fsdp_config.param_offload=False
    actor_rollout_ref.actor.fsdp_config.optimizer_offload=False
    actor_rollout_ref.actor.fsdp_config.model_dtype=bfloat16 

    actor_rollout_ref.actor.ppo_mini_batch_size=32
    actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1
    actor_rollout_ref.rollout.name=vllm
    actor_rollout_ref.rollout.tensor_model_parallel_size=2
    actor_rollout_ref.rollout.gpu_memory_utilization=0.25
    actor_rollout_ref.rollout.free_cache_engine=True
    actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=1
    actor_rollout_ref.model.enable_activation_offload=False
    actor_rollout_ref.actor.optim.lr=5e-6
    actor_rollout_ref.actor.use_dynamic_bsz=True
    actor_rollout_ref.actor.ppo_max_token_len_per_gpu=2048
    actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=2048
    actor_rollout_ref.ref.log_prob_use_dynamic_bsz=True
    actor_rollout_ref.actor.fsdp_config.use_torch_compile=False
    actor_rollout_ref.model.enable_gradient_checkpointing=True
    actor_rollout_ref.rollout.max_model_len=4096
    # 5. 教师原生蒸馏配置
    distillation.enabled=True
    distillation.nnodes=1
    distillation.teacher_models.teacher_model.model_path="$TEACHER_MODEL"
    distillation.teacher_models.teacher_model.inference.name=vllm

    # 教师推理使用 TP=2(占用另外两张卡,实现物理隔绝)
    distillation.teacher_models.teacher_model.inference.tensor_model_parallel_size=2
    distillation.teacher_models.teacher_model.inference.gpu_memory_utilization=0.85 
    distillation.teacher_models.teacher_model.inference.max_model_len=4096
    distillation.teacher_models.teacher_model.inference.enforce_eager=True

    # 蒸馏损失配置
    distillation.distillation_loss.loss_mode=forward_kl_topk
    distillation.distillation_loss.topk=32
    distillation.distillation_loss.use_task_rewards=True
    distillation.distillation_loss.distillation_loss_coef=0.5
    distillation.distillation_loss.use_policy_gradient=False
    distillation.distillation_loss.loss_max_clamp=10.0
    distillation.distillation_loss.log_prob_min_clamp=-10.0    

    # 其他训练设置
    trainer.rollout_data_dir="$CKPT_DIR/rollout_data"
    trainer.validation_data_dir="$CKPT_DIR/validation_data"
    trainer.log_val_generations=10
    trainer.test_freq=5 
    trainer.val_before_train=True
    trainer.total_epochs=10
    trainer.save_freq=10
    trainer.default_local_dir="$CKPT_DIR"
    trainer.project_name="qwen3.5_opd_grpo"
    trainer.experiment_name="9b_distill_from_27b_grpo+5e-6"
    trainer.logger='["console","wandb"]'
)
Image Image

QUTGXX avatar Jun 11 '26 02:06 QUTGXX

您好,感谢提问。

对于第一个问题“verl的源码,应该对应的是forward kl而不是OPD中推荐的reverse kl。 是因为使用了topk嘛?”,并不是,verl 实现的是 GKD (https://arxiv.org/abs/2306.06629) 里面的 forward KL, 而reverse kl并没有实现,这个实现需要大量改动verl的代码因此我们没有考虑将reverse KL的实验merge进去,另外值得一提的是forward KL使用的是teacher top-k,这样可以利用vllm拿到teacher top-k 的prob实现起来比较方便,效率也更高。

对于第二个问题“什么时候使用sampled-token OPD和 top-k KL OPD呢?”,这个仍然是一个亟待解决的问题,我们在rethinking-OPD最后做了一个简单的ablation结论是sampled-token OPD就足够了,top-k KL OPD没有明显的优势。不过从数学角度看,sampled-token是unbiased but high variance的,top-k OPD是biased but low variance的,最理想的应该是deepseek-v4里面使用的全词表OPD,是unbiased and low variance的,不过对显存和infra有非常高的要求,top-k OPD更像是一种trade off. 但是由于学术界选取的往往都是比较小的模型,这种high variance可能在工业界训练非常大的模型的时候带来很大的instability,这是工业界不想看到的。当然这仅仅是我的个人猜测,如果有误还请指正。

最后,“loss水平欠佳” 您好像没有把loss的曲线放出来,不过从overlap ratio上看这个明显已经是OPD成功了,由于这个max model len比较短所以这个score没有参考意义,你可以直接把存的ckpt 在validation set上测一下看看是不是涨点了。

Turingzero0 avatar Jun 11 '26 08:06 Turingzero0

感谢您的细心答复,我贴上我loss的曲线。 目前测试overlap ratio并没有达到期望的90%以上。然后我已经跑完10轮了,用的是GSM8数据集。 我使用的topk OPD+GRPO的方式训练的。如果想提高overlap ratio,您推荐有哪些技巧可以提升呢? Image

Image

QUTGXX avatar Jun 11 '26 09:06 QUTGXX

Overlap ratio并不一定能涨到90%以上,和模型的选择很有关系,只有RL teacher -> student比较好蒸,因为thinking pattern比较近,不同size之间会比较难蒸一点,对齐有上限

Turingzero0 avatar Jun 11 '26 09:06 Turingzero0

Overlap ratio并不一定能涨到90%以上,和模型的选择很有关系,只有RL teacher -> student比较好蒸,因为thinking pattern比较近,不同size之间会比较难蒸一点,对齐有上限

明白了,感谢您的回复!!

QUTGXX avatar Jun 11 '26 10:06 QUTGXX