modular_dots1.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # coding=utf-8
  2. # Copyright 2025 The rednote-hilab team and the HuggingFace Inc. team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from ...modeling_outputs import CausalLMOutputWithPast
  16. from ...processing_utils import Unpack
  17. from ...utils import logging
  18. from ..deepseek_v3.modeling_deepseek_v3 import (
  19. DeepseekV3DecoderLayer,
  20. DeepseekV3MLP,
  21. DeepseekV3MoE,
  22. DeepseekV3PreTrainedModel,
  23. DeepseekV3TopkRouter,
  24. )
  25. from ..qwen3.modeling_qwen3 import (
  26. Qwen3Attention,
  27. Qwen3ForCausalLM,
  28. Qwen3Model,
  29. Qwen3RMSNorm,
  30. Qwen3RotaryEmbedding,
  31. TransformersKwargs,
  32. )
  33. from .configuration_dots1 import Dots1Config
  34. logger = logging.get_logger(__name__)
  35. class Dots1RMSNorm(Qwen3RMSNorm):
  36. pass
  37. class Dots1RotaryEmbedding(Qwen3RotaryEmbedding):
  38. pass
  39. class Dots1Attention(Qwen3Attention):
  40. pass
  41. class Dots1MLP(DeepseekV3MLP):
  42. pass
  43. class Dots1MoE(DeepseekV3MoE):
  44. pass
  45. class Dots1TopkRouter(DeepseekV3TopkRouter):
  46. pass
  47. class Dots1DecoderLayer(DeepseekV3DecoderLayer):
  48. def __init__(self, config: Dots1Config, layer_idx: int):
  49. super().__init__(config, layer_idx)
  50. self.attention_type = config.layer_types[layer_idx]
  51. class Dots1PreTrainedModel(DeepseekV3PreTrainedModel):
  52. pass
  53. class Dots1Model(Qwen3Model):
  54. pass
  55. class Dots1ForCausalLM(Qwen3ForCausalLM):
  56. def forward(
  57. self,
  58. **super_kwargs: Unpack[TransformersKwargs],
  59. ) -> CausalLMOutputWithPast:
  60. r"""
  61. labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
  62. Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
  63. config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
  64. (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
  65. Example:
  66. ```python
  67. >>> from transformers import AutoTokenizer, Dots1ForCausalLM
  68. >>> model = Dots1ForCausalLM.from_pretrained("rednote-hilab/dots1.llm1.inst")
  69. >>> tokenizer = AutoTokenizer.from_pretrained("rednote-hilab/dots1.llm1.inst")
  70. >>> prompt = "Hey, are you conscious? Can you talk to me?"
  71. >>> inputs = tokenizer(prompt, return_tensors="pt")
  72. >>> # Generate
  73. >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
  74. >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
  75. "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
  76. ```"""
  77. return super().forward(**super_kwargs)
  78. __all__ = [
  79. "Dots1PreTrainedModel",
  80. "Dots1Model",
  81. "Dots1ForCausalLM",
  82. ]