configuration_rwkv.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # coding=utf-8
  2. # Copyright 2023 The OpenAI Team Authors and HuggingFace Inc. team.
  3. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """RWKV configuration"""
  17. from ...configuration_utils import PretrainedConfig
  18. from ...utils import logging
  19. logger = logging.get_logger(__name__)
  20. class RwkvConfig(PretrainedConfig):
  21. """
  22. This is the configuration class to store the configuration of a [`RwkvModel`]. It is used to instantiate a RWKV
  23. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  24. defaults will yield a similar configuration to that of the RWVK-4
  25. [RWKV/rwkv-4-169m-pile](https://huggingface.co/RWKV/rwkv-4-169m-pile) architecture.
  26. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  27. documentation from [`PretrainedConfig`] for more information.
  28. Args:
  29. vocab_size (`int`, *optional*, defaults to 50277):
  30. Vocabulary size of the RWKV model. Defines the number of different tokens that can be represented by the
  31. `inputs_ids` passed when calling [`RwkvModel`].
  32. context_length (`int`, *optional*, defaults to 1024):
  33. The maximum sequence length that this model can be used with in a single forward (using it in RNN mode
  34. lets use any sequence length).
  35. hidden_size (`int`, *optional*, defaults to 4096):
  36. Dimensionality of the embeddings and hidden states.
  37. num_hidden_layers (`int`, *optional*, defaults to 32):
  38. Number of hidden layers in the model.
  39. attention_hidden_size (`int`, *optional*):
  40. Dimensionality of the attention hidden states. Will default to `hidden_size` if unset.
  41. intermediate_size (`int`, *optional*):
  42. Dimensionality of the inner feed-forward layers. Will default to 4 times `hidden_size` if unset.
  43. layer_norm_epsilon (`float`, *optional*, defaults to 1e-05):
  44. The epsilon to use in the layer normalization layers.
  45. bos_token_id (`int`, *optional*, defaults to 0):
  46. The id of the beginning of sentence token in the vocabulary. Defaults to 0 as RWKV uses the same tokenizer
  47. as GPTNeoX.
  48. eos_token_id (`int`, *optional*, defaults to 0):
  49. The id of the end of sentence token in the vocabulary. Defaults to 0 as RWKV uses the same tokenizer as
  50. GPTNeoX.
  51. rescale_every (`int`, *optional*, defaults to 6):
  52. At inference, the hidden states (and weights of the corresponding output layers) are divided by 2 every
  53. `rescale_every` layer. If set to 0 or a negative number, no rescale is done.
  54. tie_word_embeddings (`bool`, *optional*, defaults to `False`):
  55. Whether or not to tie the word embeddings with the input token embeddings.
  56. use_cache (`bool`, *optional*, defaults to `True`):
  57. Whether or not the model should return the last state.
  58. Example:
  59. ```python
  60. >>> from transformers import RwkvConfig, RwkvModel
  61. >>> # Initializing a Rwkv configuration
  62. >>> configuration = RwkvConfig()
  63. >>> # Initializing a model (with random weights) from the configuration
  64. >>> model = RwkvModel(configuration)
  65. >>> # Accessing the model configuration
  66. >>> configuration = model.config
  67. ```"""
  68. model_type = "rwkv"
  69. attribute_map = {"max_position_embeddings": "context_length"}
  70. def __init__(
  71. self,
  72. vocab_size=50277,
  73. context_length=1024,
  74. hidden_size=4096,
  75. num_hidden_layers=32,
  76. attention_hidden_size=None,
  77. intermediate_size=None,
  78. layer_norm_epsilon=1e-5,
  79. bos_token_id=0,
  80. eos_token_id=0,
  81. rescale_every=6,
  82. tie_word_embeddings=False,
  83. use_cache=True,
  84. **kwargs,
  85. ):
  86. self.vocab_size = vocab_size
  87. self.context_length = context_length
  88. self.hidden_size = hidden_size
  89. self.num_hidden_layers = num_hidden_layers
  90. self.attention_hidden_size = attention_hidden_size if attention_hidden_size is not None else hidden_size
  91. self.intermediate_size = intermediate_size if intermediate_size is not None else 4 * hidden_size
  92. self.layer_norm_epsilon = layer_norm_epsilon
  93. self.rescale_every = rescale_every
  94. self.use_cache = use_cache
  95. self.bos_token_id = bos_token_id
  96. self.eos_token_id = eos_token_id
  97. super().__init__(
  98. tie_word_embeddings=tie_word_embeddings, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs
  99. )
  100. __all__ = ["RwkvConfig"]