The DeepSeek-V4 report merges more than ten domain specialists into one model with multi-teacher on-policy distillation. Given expert policies with weights , the report defines
evaluated on trajectories sampled from the student, with the full-vocabulary distribution at every position rather than a single-token estimate.
Implement multi_teacher_opd(student_logp, teacher_logp, weights):
student_logp has shape (T, V): the student's log-probabilities over the vocabulary at each of T positions of a student-sampled trajectory (each row is a normalised log-distribution).teacher_logp has shape (N, T, V): the same for each of N teachers.weights has shape (N,).per_teacher of shape (N, T) (the unweighted ), per_position of shape (T,) (), mean (Python float, the mean of per_position, which is the loss) and grad of shape (T, V): the gradient of per_position[t] with respect to the student logits that produced student_logp[t] through a softmax, which issummed over teachers with their weights.
student_logp[None] - teacher_logp gives all the per-teacher log-ratios at once.grad sums to zero, because the softmax is invariant to shifting the logits.