Akahsizrr commited on
Commit
16c8abd
·
verified ·
1 Parent(s): 6207807

Upload alea/model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. alea/model.py +9 -3
alea/model.py CHANGED
@@ -11,7 +11,7 @@ import torch.nn as nn
11
  from transformers import AutoModel
12
 
13
  from .heads import SetChoiceHead, NoulHead, RegressHead
14
- from .ngram import AuxEmbedding, NGramEncoder
15
  from .packing import build_additive_mask, SEG_OPT, SEG_ANS, SEG_QCTX, SEG_STATE
16
 
17
 
@@ -20,7 +20,7 @@ class AleaModel(nn.Module):
20
  use_set_attn=True, attn_impl="eager",
21
  use_aux_embed=False, use_ngram=False,
22
  ngram_orders=(2, 3, 4), ngram_buckets=262144, d_ng=128,
23
- use_ngram2=False):
24
  super().__init__()
25
  self.backbone = AutoModel.from_pretrained(
26
  model_name, torch_dtype=torch.bfloat16,
@@ -38,6 +38,9 @@ class AleaModel(nn.Module):
38
  # second bank: longer orders + bigger hash space, separate gate
39
  self.ngram2 = (NGramEncoder(d, (5, 6, 7, 8), 524288, d_ng)
40
  if use_ngram2 else None)
 
 
 
41
 
42
  def _input_embeds(self, input_ids):
43
  emb = self.backbone.embed_tokens(input_ids)
@@ -50,6 +53,9 @@ class AleaModel(nn.Module):
50
  if self.ngram2 is not None:
51
  ng2 = self.ngram2(input_ids)
52
  add = ng2 if add is None else add + ng2
 
 
 
53
  if add is not None:
54
  emb = emb + add.to(emb.dtype)
55
  return emb
@@ -60,7 +66,7 @@ class AleaModel(nn.Module):
60
  Returns list[list[dict]] — per sequence, per question:
61
  {'qtype','logits' (choice/score) or 'p_yes' (noul)}"""
62
  if (self.aux_embed is not None or self.ngram is not None
63
- or self.ngram2 is not None):
64
  out = self.backbone(inputs_embeds=self._input_embeds(input_ids),
65
  attention_mask=attention_mask,
66
  position_ids=position_ids)
 
11
  from transformers import AutoModel
12
 
13
  from .heads import SetChoiceHead, NoulHead, RegressHead
14
+ from .ngram import AuxEmbedding, NGramEncoder, NGramEncoderMH
15
  from .packing import build_additive_mask, SEG_OPT, SEG_ANS, SEG_QCTX, SEG_STATE
16
 
17
 
 
20
  use_set_attn=True, attn_impl="eager",
21
  use_aux_embed=False, use_ngram=False,
22
  ngram_orders=(2, 3, 4), ngram_buckets=262144, d_ng=128,
23
+ use_ngram2=False, use_ngram3=False):
24
  super().__init__()
25
  self.backbone = AutoModel.from_pretrained(
26
  model_name, torch_dtype=torch.bfloat16,
 
38
  # second bank: longer orders + bigger hash space, separate gate
39
  self.ngram2 = (NGramEncoder(d, (5, 6, 7, 8), 524288, d_ng)
40
  if use_ngram2 else None)
41
+ # third bank: multi-hash compositional tables (thin rows, K=2)
42
+ self.ngram3 = (NGramEncoderMH(d, (3, 4, 5, 6), 262144, 262144, 64)
43
+ if use_ngram3 else None)
44
 
45
  def _input_embeds(self, input_ids):
46
  emb = self.backbone.embed_tokens(input_ids)
 
53
  if self.ngram2 is not None:
54
  ng2 = self.ngram2(input_ids)
55
  add = ng2 if add is None else add + ng2
56
+ if self.ngram3 is not None:
57
+ ng3 = self.ngram3(input_ids)
58
+ add = ng3 if add is None else add + ng3
59
  if add is not None:
60
  emb = emb + add.to(emb.dtype)
61
  return emb
 
66
  Returns list[list[dict]] — per sequence, per question:
67
  {'qtype','logits' (choice/score) or 'p_yes' (noul)}"""
68
  if (self.aux_embed is not None or self.ngram is not None
69
+ or self.ngram2 is not None or self.ngram3 is not None):
70
  out = self.backbone(inputs_embeds=self._input_embeds(input_ids),
71
  attention_mask=attention_mask,
72
  position_ids=position_ids)