File size: 2,572 Bytes
80aea5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Configuration for DiffusionLM: masked (absorbing-state) discrete diffusion
language model in the LLaDA / Diffusion-LM family (~51M params at default
scale: hidden 512, 10 layers, heads 8, SwiGLU 1536, vocab 32768).

Training: sample t ~ U(0, 1), mask each token independently with prob t,
predict masked tokens with 1/t weighting, normalized by source token count.
"""

import math

from transformers import PretrainedConfig


class DiffusionLMConfig(PretrainedConfig):
    model_type = "diffusion_lm"

    def __init__(
        self,
        vocab_size: int = 32768,
        hidden_size: int = 512,
        intermediate_size: int = 1536,
        num_hidden_layers: int = 10,
        num_attention_heads: int = 8,
        head_dim: int = 64,
        max_position_embeddings: int = 2048,
        rope_theta: float = 10000.0,
        rms_norm_eps: float = 1e-5,
        attention_dropout: float = 0.0,
        tie_word_embeddings: bool = True,
        bos_token_id: int = 1,
        eos_token_id: int = 2,
        pad_token_id: int = 0,
        mask_token_id: int | None = None,
        num_diffusion_steps: int = 64,
        time_conditioning: str = "additive",
        time_conditioning_scale: float = 0.02,
        **kwargs,
    ):
        super().__init__(
            tie_word_embeddings=tie_word_embeddings,
            bos_token_id=bos_token_id,
            eos_token_id=eos_token_id,
            pad_token_id=pad_token_id,
            **kwargs,
        )
        self.vocab_size = vocab_size
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.head_dim = head_dim
        self.max_position_embeddings = max_position_embeddings
        self.rope_theta = rope_theta
        self.rms_norm_eps = rms_norm_eps
        self.attention_dropout = attention_dropout
        # [MASK] has a separate learned input vector, outside the output vocab.
        self.mask_token_id = mask_token_id if mask_token_id is not None else vocab_size
        self.num_diffusion_steps = num_diffusion_steps
        if time_conditioning not in ('additive', 'normalized', 'none'):
            raise ValueError('Unknown diffusion time conditioning')
        if not math.isfinite(time_conditioning_scale) or time_conditioning_scale < 0:
            raise ValueError('Time conditioning scale must be finite and nonnegative')
        self.time_conditioning = time_conditioning
        self.time_conditioning_scale = time_conditioning_scale