MCPcopy Create free account
hub / github.com/Pints-AI/1.5-Pints / Block

Class Block

lit_gpt/adapter.py:125–164  ·  view source on GitHub ↗

The implementation is identical to `lit_gpt.model.Block` with the exception that we replace the attention layer where adaption is implemented.

Source from the content-addressed store, hash-verified

123
124
125class Block(nn.Module):
126 """The implementation is identical to `lit_gpt.model.Block` with the exception that
127 we replace the attention layer where adaption is implemented."""
128
129 def __init__(self, config: Config, block_idx: int) -> None:
130 super().__init__()
131 self.norm_1 = config.norm_class(config.n_embd, eps=config.norm_eps)
132 self.attn = CausalSelfAttention(config, block_idx)
133 if not config.shared_attention_norm:
134 self.norm_2 = config.norm_class(config.n_embd, eps=config.norm_eps)
135 self.mlp = config.mlp_class(config)
136
137 self.config = config
138
139 def forward(
140 self,
141 x: torch.Tensor,
142 rope: RoPECache,
143 max_seq_length: int,
144 mask: Optional[torch.Tensor] = None,
145 input_pos: Optional[torch.Tensor] = None,
146 kv_cache: Optional[KVCache] = None,
147 adapter_kv_cache: Optional[KVCache] = None,
148 ) -> Tuple[torch.Tensor, Optional[KVCache], Optional[KVCache]]:
149 n_1 = self.norm_1(x)
150 h, new_kv_cache, new_adapter_kv_cache = self.attn(
151 n_1, rope, max_seq_length, mask, input_pos, kv_cache, adapter_kv_cache
152 )
153 if self.config.parallel_residual:
154 n_2 = n_1 if self.config.shared_attention_norm else self.norm_2(x)
155 x = x + h + self.mlp(n_2)
156 else:
157 if self.config.shared_attention_norm:
158 raise NotImplementedError(
159 "No checkpoint amongst the ones we support uses this configuration"
160 " (non-parallel residual and shared attention norm)."
161 )
162 x = x + h
163 x = x + self.mlp(self.norm_2(x))
164 return x, new_kv_cache, new_adapter_kv_cache
165
166
167class CausalSelfAttention(BaseCausalSelfAttention):

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected