MCPcopy Create free account
hub / github.com/OpenBMB/ToolBench / __init__

Method __init__

toolbench/utils.py:74–90  ·  view source on GitHub ↗
(self, dim, ratio, max_position_embeddings=2048, base=10000, device=None)

Source from the content-addressed store, hash-verified

72# code adapted from https://huggingface.co/kaiokendev/superhot-13b-8k-no-rlhf-test/blob/main/llama_rope_scaled_monkey_patch.py
73class CondenseRotaryEmbedding(torch.nn.Module):
74 def __init__(self, dim, ratio, max_position_embeddings=2048, base=10000, device=None):
75 super().__init__()
76 inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))
77 self.register_buffer("inv_freq", inv_freq)
78
79 # Build here to make `torch.jit.trace` work.
80 self.ratio = ratio
81 max_position_embeddings *= ratio
82 print(f"Condensing Positional embeddings from {max_position_embeddings} to {max_position_embeddings // ratio}")
83 self.max_seq_len_cached = max_position_embeddings
84 t = torch.arange(self.max_seq_len_cached, device=self.inv_freq.device, dtype=self.inv_freq.dtype) / ratio
85 freqs = torch.einsum("i,j->ij", t, self.inv_freq)
86 # Different from paper, but it uses a different permutation in order to obtain the same calculation
87 emb = torch.cat((freqs, freqs), dim=-1)
88 dtype = torch.get_default_dtype()
89 self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
90 self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
91
92 def forward(self, x, seq_len=None):
93 # x: [bs, num_attention_heads, seq_len, head_size]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected