Upload energybert/hf_configuration.py with huggingface_hub
Browse files
energybert/hf_configuration.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class BertEnergyConfig(PretrainedConfig):
|
| 5 |
+
model_type = "bert_energy"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
path: str | None = None,
|
| 10 |
+
alpha: float = 1.0,
|
| 11 |
+
beta: float | None = None,
|
| 12 |
+
vocab_size: int = 30000,
|
| 13 |
+
hidden_size: int = 768,
|
| 14 |
+
embedding_dim: int | None = None,
|
| 15 |
+
num_hidden_layers: int = 12,
|
| 16 |
+
num_attention_heads: int = 12,
|
| 17 |
+
intermediate_size: int | None = None,
|
| 18 |
+
activation: str = "relu",
|
| 19 |
+
positional: bool = True,
|
| 20 |
+
share_layers: bool = False,
|
| 21 |
+
layer_norm_eps: float = 1e-12,
|
| 22 |
+
initializer_range: float = 0.02,
|
| 23 |
+
initializer_hopfield_range: float = 0.002,
|
| 24 |
+
hidden_dropout_prob: float = 0.1,
|
| 25 |
+
attention_probs_dropout_prob: float = 0.1,
|
| 26 |
+
max_position_embeddings: int = 512,
|
| 27 |
+
tie_word_embeddings: bool = True,
|
| 28 |
+
bias: bool = True,
|
| 29 |
+
compile: bool = False,
|
| 30 |
+
pad_token_id: int | None = None,
|
| 31 |
+
problem_type: str | None = None,
|
| 32 |
+
**kwargs,
|
| 33 |
+
):
|
| 34 |
+
super().__init__(pad_token_id=pad_token_id, **kwargs)
|
| 35 |
+
|
| 36 |
+
self.path = path
|
| 37 |
+
|
| 38 |
+
# Energy-specific parameters
|
| 39 |
+
self.alpha = alpha
|
| 40 |
+
self.beta = beta
|
| 41 |
+
|
| 42 |
+
# Vocabulary / dimensions
|
| 43 |
+
self.vocab_size = vocab_size
|
| 44 |
+
self.hidden_size = hidden_size
|
| 45 |
+
self.embedding_dim = embedding_dim if embedding_dim is not None else hidden_size
|
| 46 |
+
|
| 47 |
+
# Transformer architecture
|
| 48 |
+
self.num_hidden_layers = num_hidden_layers
|
| 49 |
+
self.num_attention_heads = num_attention_heads
|
| 50 |
+
self.intermediate_size = (
|
| 51 |
+
intermediate_size if intermediate_size is not None else hidden_size * 4
|
| 52 |
+
)
|
| 53 |
+
self.activation = activation
|
| 54 |
+
self.positional = positional
|
| 55 |
+
self.share_layers = share_layers
|
| 56 |
+
self.tie_word_embeddings = tie_word_embeddings
|
| 57 |
+
self.bias = bias
|
| 58 |
+
|
| 59 |
+
# Regularization / initialization
|
| 60 |
+
self.layer_norm_eps = layer_norm_eps
|
| 61 |
+
self.initializer_range = initializer_range
|
| 62 |
+
self.initializer_hopfield_range = initializer_hopfield_range
|
| 63 |
+
self.hidden_dropout_prob = hidden_dropout_prob
|
| 64 |
+
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
| 65 |
+
|
| 66 |
+
# Sequence length
|
| 67 |
+
self.max_position_embeddings = max_position_embeddings
|
| 68 |
+
|
| 69 |
+
# Misc
|
| 70 |
+
self.compile = compile
|
| 71 |
+
self.problem_type = problem_type
|
| 72 |
+
|
| 73 |
+
# ---- Validation ----
|
| 74 |
+
if self.embedding_dim % self.num_attention_heads != 0:
|
| 75 |
+
raise ValueError("embedding_dim must be divisible by num_attention_heads")
|
| 76 |
+
|
| 77 |
+
if self.hidden_size <= 0:
|
| 78 |
+
raise ValueError("hidden_size must be > 0")
|
| 79 |
+
|
| 80 |
+
if self.embedding_dim <= 0:
|
| 81 |
+
raise ValueError("embedding_dim must be > 0")
|
| 82 |
+
|
| 83 |
+
if self.num_hidden_layers <= 0:
|
| 84 |
+
raise ValueError("num_hidden_layers must be > 0")
|
| 85 |
+
|
| 86 |
+
if self.num_attention_heads <= 0:
|
| 87 |
+
raise ValueError("num_attention_heads must be > 0")
|
| 88 |
+
|
| 89 |
+
if self.max_position_embeddings <= 0:
|
| 90 |
+
raise ValueError("max_position_embeddings must be > 0")
|
| 91 |
+
|
| 92 |
+
if self.activation not in ["relu", "gelu", "softmax"]:
|
| 93 |
+
raise ValueError("activation must be one of: relu, gelu, softmax")
|