configuration_dia.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright 2025 The Nari Labs and HuggingFace Inc. team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Dia model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...modeling_rope_utils import RopeParameters
  18. from ...utils import auto_docstring, logging
  19. logger = logging.get_logger(__name__)
  20. @auto_docstring(checkpoint="nari-labs/Dia-1.6B")
  21. @strict
  22. class DiaEncoderConfig(PreTrainedConfig):
  23. model_type = "dia_encoder"
  24. max_position_embeddings: int = 1024
  25. num_hidden_layers: int = 12
  26. hidden_size: int = 1024
  27. num_attention_heads: int = 16
  28. num_key_value_heads: int = 16
  29. head_dim: int = 128
  30. intermediate_size: int = 4096
  31. norm_eps: float = 1e-5
  32. vocab_size: int = 256
  33. hidden_act: str = "silu"
  34. rope_parameters: dict | None = None
  35. initializer_range: float = 0.02
  36. @auto_docstring(checkpoint="nari-labs/Dia-1.6B")
  37. @strict
  38. class DiaDecoderConfig(PreTrainedConfig):
  39. r"""
  40. cross_num_attention_heads (`int`, *optional*, defaults to 16):
  41. Number of attention heads for each cross-attention layer in the Transformer decoder.
  42. cross_head_dim (`int`, *optional*, defaults to 128):
  43. Dimensionality of the cross-attention head.
  44. cross_num_key_value_heads (`int`, *optional*, defaults to 16):
  45. Number of key and value heads for each cross-attention layer in the Transformer decoder.
  46. cross_hidden_size (`int`, *optional*, defaults to 1024):
  47. Dimensionality of the cross-attention layers.
  48. """
  49. model_type = "dia_decoder"
  50. max_position_embeddings: int = 3072
  51. num_hidden_layers: int = 18
  52. hidden_size: int = 2048
  53. intermediate_size: int = 8192
  54. num_attention_heads: int = 16
  55. num_key_value_heads: int = 4
  56. head_dim: int = 128
  57. cross_num_attention_heads: int = 16
  58. cross_head_dim: int = 128
  59. cross_num_key_value_heads: int = 16
  60. cross_hidden_size: int = 1024
  61. norm_eps: float = 1e-5
  62. vocab_size: int = 1028
  63. hidden_act: str = "silu"
  64. num_channels: int = 9
  65. rope_parameters: RopeParameters | dict | None = None
  66. initializer_range: float = 0.02
  67. use_cache: bool = True
  68. is_encoder_decoder: bool = True
  69. pad_token_id: int | None = 1025
  70. eos_token_id: int | list[int] | None = 1024
  71. bos_token_id: int | None = 1026
  72. @auto_docstring(checkpoint="nari-labs/Dia-1.6B")
  73. @strict
  74. class DiaConfig(PreTrainedConfig):
  75. r"""
  76. delay_pattern (`list[int]`, *optional*, defaults to `[0, 8, 9, 10, 11, 12, 13, 14, 15]`):
  77. The delay pattern for the decoder. The length of this list must match `decoder_config.num_channels`.
  78. Example:
  79. ```python
  80. >>> from transformers import DiaConfig, DiaModel
  81. >>> # Initializing a DiaConfig with default values
  82. >>> configuration = DiaConfig()
  83. >>> # Initializing a DiaModel (with random weights) from the configuration
  84. >>> model = DiaModel(configuration)
  85. >>> # Accessing the model configuration
  86. >>> configuration = model.config
  87. ```
  88. """
  89. model_type = "dia"
  90. keys_to_ignore_at_inference = ["past_key_values"]
  91. sub_configs = {"encoder_config": DiaEncoderConfig, "decoder_config": DiaDecoderConfig}
  92. encoder_config: DiaEncoderConfig | dict | None = None
  93. decoder_config: DiaDecoderConfig | dict | None = None
  94. norm_eps: float = 1e-5
  95. is_encoder_decoder: bool = True
  96. pad_token_id: int | None = None
  97. eos_token_id: int | list[int] | None = None
  98. bos_token_id: int | None = None
  99. delay_pattern: list[int] | None = None
  100. initializer_range: float = 0.02
  101. use_cache: bool = True
  102. def __post_init__(self, **kwargs):
  103. if isinstance(self.encoder_config, dict):
  104. self.encoder_config = DiaEncoderConfig(**self.encoder_config)
  105. if isinstance(self.decoder_config, dict):
  106. self.decoder_config = DiaDecoderConfig(**self.decoder_config)
  107. self.encoder_config = self.encoder_config if self.encoder_config is not None else DiaEncoderConfig()
  108. self.decoder_config = self.decoder_config if self.decoder_config is not None else DiaDecoderConfig()
  109. self.delay_pattern = (
  110. self.delay_pattern if self.delay_pattern is not None else [0, 8, 9, 10, 11, 12, 13, 14, 15]
  111. )
  112. # TODO: Remove token ID forwarding once the `nari-labs/Dia-1.6B` checkpoint is updated
  113. if self.pad_token_id is not None:
  114. logger.warning_once(
  115. "Passing `pad_token_id` to `DiaConfig` is deprecated. "
  116. "Please set it directly on `DiaDecoderConfig` instead."
  117. )
  118. self.decoder_config.pad_token_id = self.pad_token_id
  119. if self.eos_token_id is not None:
  120. logger.warning_once(
  121. "Passing `eos_token_id` to `DiaConfig` is deprecated. "
  122. "Please set it directly on `DiaDecoderConfig` instead."
  123. )
  124. self.decoder_config.eos_token_id = self.eos_token_id
  125. if self.bos_token_id is not None:
  126. logger.warning_once(
  127. "Passing `bos_token_id` to `DiaConfig` is deprecated. "
  128. "Please set it directly on `DiaDecoderConfig` instead."
  129. )
  130. self.decoder_config.bos_token_id = self.bos_token_id
  131. super().__post_init__(**kwargs)
  132. def validate_architecture(self):
  133. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  134. if self.decoder_config.num_channels != len(self.delay_pattern):
  135. raise ValueError("Number of channels must match delay pattern length.")
  136. def get_text_config(self, *args, **kwargs):
  137. """Defaulting to audio config as it's the decoder in this case which is usually the text backbone"""
  138. return self.decoder_config
  139. __all__ = ["DiaConfig", "DiaEncoderConfig", "DiaDecoderConfig"]