Datasets:
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
Tags:
reinforcement-learning
policy-optimization
klpo
exact-moment-replay
score-centering
stratified-sampling
License:
Download src/celestis_rl/models.py from PureOne/Celestis-RL: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/datasets/PureOne/Celestis-RL/resolve/main/src/celestis_rl/models.py
- Command line
-
hf download hf://datasets/PureOne/Celestis-RL/src/celestis_rl/models.py
-
curl -L -o models.py https://huggingface.co/datasets/PureOne/Celestis-RL/resolve/main/src/celestis_rl/models.py
2.56 kB
| """Small locally initialized models for reproducible CPU smoke experiments.""" | |
| import torch | |
| from torch import nn | |
| class TinyCausalLM(nn.Module): | |
| def __init__(self, vocab_size: int = 16, width: int = 32, max_length: int = 32): | |
| super().__init__() | |
| self.vocab_size = vocab_size | |
| self.token = nn.Embedding(vocab_size, width) | |
| self.position = nn.Embedding(max_length, width) | |
| layer = nn.TransformerEncoderLayer(width, 4, width*2, dropout=0., batch_first=True, | |
| norm_first=False) | |
| self.decoder = nn.TransformerEncoder(layer, 1, enable_nested_tensor=False) | |
| self.norm = nn.LayerNorm(width) | |
| self.lm_head = nn.Linear(width, vocab_size, bias=False) | |
| # Untied head by default. Tests also cover tied output weights. | |
| def hidden(self, input_ids: torch.Tensor) -> torch.Tensor: | |
| t = input_ids.shape[1] | |
| if t > self.position.num_embeddings: | |
| raise ValueError("sequence exceeds configured maximum length") | |
| x = self.token(input_ids) + self.position(torch.arange(t, device=input_ids.device))[None] | |
| causal = torch.triu(torch.ones(t, t, dtype=torch.bool, device=input_ids.device), diagonal=1) | |
| return self.norm(self.decoder(x, mask=causal)) | |
| def forward(self, input_ids: torch.Tensor) -> torch.Tensor: | |
| return self.lm_head(self.hidden(input_ids)) | |
| class FixedSpanCausalLM(TinyCausalLM): | |
| """Tiny demo with a fixed vocabulary basis and trainable latent head map. | |
| forward() includes the adapter, so saved model inference agrees with its | |
| collection and replay paths. The effective output head can change within | |
| the fixed basis span, not acquire arbitrary new vocabulary directions. | |
| """ | |
| def __init__(self,vocab_size: int=16,width: int=32,max_length: int=8,*,trainable_span_adapter: bool=True): | |
| super().__init__(vocab_size,width,max_length) | |
| self.lm_head.weight.requires_grad_(False) | |
| self.trainable_span_adapter=bool(trainable_span_adapter) | |
| if self.trainable_span_adapter: | |
| self.output_adapter=nn.Linear(width,width,bias=True) | |
| with torch.no_grad(): | |
| self.output_adapter.weight.copy_(torch.eye(width));self.output_adapter.bias.zero_() | |
| else:self.output_adapter=nn.Identity() | |
| def latent(self,input_ids: torch.Tensor) -> torch.Tensor: | |
| return self.output_adapter(self.hidden(input_ids)) | |
| def forward(self,input_ids: torch.Tensor) -> torch.Tensor: | |
| return self.lm_head(self.latent(input_ids)) | |