Jamie@TitanML commited on
Commit
b7e7960
·
verified ·
1 Parent(s): 3971371

Create modelling_config.py

Browse files
Files changed (1) hide show
  1. modelling_config.py +118 -0
modelling_config.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.modeling_utils import PretrainedConfig
2
+
3
+ class JinaBertConfig(PretrainedConfig):
4
+ r"""
5
+ This is the configuration class to store the configuration of a [`JinaBertModel`]. It is used to
6
+ instantiate a BERT model according to the specified arguments, defining the model architecture. Instantiating a
7
+ configuration with the defaults will yield a similar configuration to that of the BERT
8
+ [bert-base-uncased](https://huggingface.co/bert-base-uncased) architecture.
9
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
10
+ documentation from [`PretrainedConfig`] for more information.
11
+ Args:
12
+ vocab_size (`int`, *optional*, defaults to 30522):
13
+ Vocabulary size of the BERT model. Defines the number of different tokens that can be represented by the
14
+ `inputs_ids` passed when calling [`BertModel`] or [`TFBertModel`].
15
+ hidden_size (`int`, *optional*, defaults to 768):
16
+ Dimensionality of the encoder layers and the pooler layer.
17
+ num_hidden_layers (`int`, *optional*, defaults to 12):
18
+ Number of hidden layers in the Transformer encoder.
19
+ num_attention_heads (`int`, *optional*, defaults to 12):
20
+ Number of attention heads for each attention layer in the Transformer encoder.
21
+ intermediate_size (`int`, *optional*, defaults to 3072):
22
+ Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
23
+ hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`):
24
+ The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
25
+ `"relu"`, `"silu"` and `"gelu_new"` are supported.
26
+ hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
27
+ The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
28
+ attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
29
+ The dropout ratio for the attention probabilities.
30
+ max_position_embeddings (`int`, *optional*, defaults to 512):
31
+ The maximum sequence length that this model might ever be used with. Typically set this to something large
32
+ just in case (e.g., 512 or 1024 or 2048).
33
+ type_vocab_size (`int`, *optional*, defaults to 2):
34
+ The vocabulary size of the `token_type_ids` passed when calling [`BertModel`] or [`TFBertModel`].
35
+ initializer_range (`float`, *optional*, defaults to 0.02):
36
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
37
+ layer_norm_eps (`float`, *optional*, defaults to 1e-12):
38
+ The epsilon used by the layer normalization layers.
39
+ position_embedding_type (`str`, *optional*, defaults to `"absolute"`):
40
+ Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For
41
+ positional embeddings use `"absolute"`. For more information on `"relative_key"`, please refer to
42
+ [Self-Attention with Relative Position Representations (Shaw et al.)](https://arxiv.org/abs/1803.02155).
43
+ For more information on `"relative_key_query"`, please refer to *Method 4* in [Improve Transformer Models
44
+ with Better Relative Position Embeddings (Huang et al.)](https://arxiv.org/abs/2009.13658).
45
+ is_decoder (`bool`, *optional*, defaults to `False`):
46
+ Whether the model is used as a decoder or not. If `False`, the model is used as an encoder.
47
+ use_cache (`bool`, *optional*, defaults to `True`):
48
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
49
+ relevant if `config.is_decoder=True`.
50
+ classifier_dropout (`float`, *optional*):
51
+ The dropout ratio for the classification head.
52
+ feed_forward_type (`str`, *optional*, defaults to `"original"`):
53
+ The type of feed forward layer to use in the bert layers.
54
+ Can be one of GLU variants, e.g. `"reglu"`, `"geglu"`
55
+ emb_pooler (`str`, *optional*, defaults to `None`):
56
+ The function to use for pooling the last layer embeddings to get the sentence embeddings.
57
+ Should be one of `None`, `"mean"`.
58
+ attn_implementation (`str`, *optional*, defaults to `"torch"`):
59
+ The implementation of the self-attention layer. Can be one of:
60
+ - `None` for the original implementation,
61
+ - `torch` for the PyTorch SDPA implementation,
62
+ Examples:
63
+ ```python
64
+ >>> from transformers import JinaBertConfig, JinaBertModel
65
+ >>> # Initializing a JinaBert configuration
66
+ >>> configuration = JinaBertConfig()
67
+ >>> # Initializing a model (with random weights) from the configuration
68
+ >>> model = JinaBertModel(configuration)
69
+ >>> # Accessing the model configuration
70
+ >>> configuration = model.config
71
+ >>> # Encode text inputs
72
+ >>> embeddings = model.encode(text_inputs)
73
+ ```"""
74
+ model_type = "bert"
75
+
76
+ def __init__(
77
+ self,
78
+ vocab_size=30522,
79
+ hidden_size=768,
80
+ num_hidden_layers=12,
81
+ num_attention_heads=12,
82
+ intermediate_size=3072,
83
+ hidden_act="gelu",
84
+ hidden_dropout_prob=0.1,
85
+ attention_probs_dropout_prob=0.1,
86
+ max_position_embeddings=512,
87
+ type_vocab_size=2,
88
+ initializer_range=0.02,
89
+ layer_norm_eps=1e-12,
90
+ pad_token_id=0,
91
+ position_embedding_type="alibi",
92
+ use_cache=True,
93
+ classifier_dropout=None,
94
+ feed_forward_type="original",
95
+ emb_pooler=None,
96
+ attn_implementation=None,
97
+ **kwargs,
98
+ ):
99
+ super().__init__(pad_token_id=pad_token_id, **kwargs)
100
+
101
+ self.vocab_size = vocab_size
102
+ self.hidden_size = hidden_size
103
+ self.num_hidden_layers = num_hidden_layers
104
+ self.num_attention_heads = num_attention_heads
105
+ self.hidden_act = hidden_act
106
+ self.intermediate_size = intermediate_size
107
+ self.hidden_dropout_prob = hidden_dropout_prob
108
+ self.attention_probs_dropout_prob = attention_probs_dropout_prob
109
+ self.max_position_embeddings = max_position_embeddings
110
+ self.type_vocab_size = type_vocab_size
111
+ self.initializer_range = initializer_range
112
+ self.layer_norm_eps = layer_norm_eps
113
+ self.position_embedding_type = position_embedding_type
114
+ self.use_cache = use_cache
115
+ self.classifier_dropout = classifier_dropout
116
+ self.feed_forward_type = feed_forward_type
117
+ self.emb_pooler = emb_pooler
118
+ self.attn_implementation = attn_implementation