npedrazzini commited on
Commit
bf5fa66
·
verified ·
1 Parent(s): c645fce

Update continuous_time_embedding.py

Browse files
Files changed (1) hide show
  1. continuous_time_embedding.py +19 -8
continuous_time_embedding.py CHANGED
@@ -2,10 +2,8 @@
2
  Companion module for loading the continuous-time NewsBERT model.
3
 
4
  This model is not a plain AutoModelForMaskedLM. It wraps a full
5
- fine-tuned BERT with a continuous sinusoidal time embedding injected at the
6
- input layer. You need this file (or an equivalent copy of these two classes)
7
- to load and query it; `AutoModelForMaskedLM.from_pretrained(...)` alone will
8
- not work.
9
 
10
  Usage:
11
  from continuous_time_embedding import load_continuous_time_model
@@ -20,6 +18,8 @@ import torch.nn as nn
20
  from transformers import AutoTokenizer, AutoModelForMaskedLM
21
  from huggingface_hub import snapshot_download
22
 
 
 
23
  MIN_YEAR = 1800.0
24
  MAX_YEAR = 1920.0
25
  MIN_PERIOD_YEARS = 5.0
@@ -30,8 +30,7 @@ class ContinuousTimeEmbedding(nn.Module):
30
  """Sinusoidal (Fourier) features over normalized year, projected to
31
  hidden_size. Nearby years produce nearby embeddings by construction.
32
  Frequency band: lowest = 1 cycle over the full 1800-1920 span, highest =
33
- 1 cycle per MIN_PERIOD_YEARS (5 years) -- see training script / model
34
- card for the reasoning."""
35
 
36
  def __init__(self, hidden_size, n_freqs=N_TIME_FREQS, min_year=MIN_YEAR, max_year=MAX_YEAR,
37
  min_period_years=MIN_PERIOD_YEARS):
@@ -103,13 +102,25 @@ class ContinuousTimeBertForMLM(nn.Module):
103
 
104
  def load_continuous_time_model(repo_id_or_path, device=None, **kwargs):
105
  """Convenience loader. Works with either a local checkpoint directory or
106
- a Hugging Face Hub repo id (downloads it locally first)."""
 
107
  if os.path.isdir(repo_id_or_path):
108
  local_dir = repo_id_or_path
109
  else:
110
  local_dir = snapshot_download(repo_id_or_path)
111
 
112
- tokenizer = AutoTokenizer.from_pretrained(os.path.join(local_dir, "full_model"))
 
 
 
 
 
 
 
 
 
 
 
113
  model = ContinuousTimeBertForMLM.load_pretrained(local_dir, **kwargs)
114
 
115
  device = device or ("cuda" if torch.cuda.is_available() else "cpu")
 
2
  Companion module for loading the continuous-time NewsBERT model.
3
 
4
  This model is not a plain AutoModelForMaskedLM. It wraps a full
5
+ fine-tuned BERT with a continuous sinusoidal (Fourier) time embedding
6
+ injected at the input layer. You need this file to load and query it.
 
 
7
 
8
  Usage:
9
  from continuous_time_embedding import load_continuous_time_model
 
18
  from transformers import AutoTokenizer, AutoModelForMaskedLM
19
  from huggingface_hub import snapshot_download
20
 
21
+ BASE_MODEL_ID = "TextMachineProject/NewsBERT_1800-1920"
22
+
23
  MIN_YEAR = 1800.0
24
  MAX_YEAR = 1920.0
25
  MIN_PERIOD_YEARS = 5.0
 
30
  """Sinusoidal (Fourier) features over normalized year, projected to
31
  hidden_size. Nearby years produce nearby embeddings by construction.
32
  Frequency band: lowest = 1 cycle over the full 1800-1920 span, highest =
33
+ 1 cycle per MIN_PERIOD_YEARS (5 years)."""
 
34
 
35
  def __init__(self, hidden_size, n_freqs=N_TIME_FREQS, min_year=MIN_YEAR, max_year=MAX_YEAR,
36
  min_period_years=MIN_PERIOD_YEARS):
 
102
 
103
  def load_continuous_time_model(repo_id_or_path, device=None, **kwargs):
104
  """Convenience loader. Works with either a local checkpoint directory or
105
+ a Hugging Face Hub repo id (downloads it locally first).
106
+ """
107
  if os.path.isdir(repo_id_or_path):
108
  local_dir = repo_id_or_path
109
  else:
110
  local_dir = snapshot_download(repo_id_or_path)
111
 
112
+ tokenizer = None
113
+ for candidate in (os.path.join(local_dir, "full_model"), local_dir):
114
+ try:
115
+ tokenizer = AutoTokenizer.from_pretrained(candidate)
116
+ break
117
+ except Exception:
118
+ continue
119
+ if tokenizer is None:
120
+ print(f"[load_continuous_time_model] No tokenizer found in {repo_id_or_path}, "
121
+ f"falling back to base model tokenizer: {BASE_MODEL_ID}")
122
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
123
+
124
  model = ContinuousTimeBertForMLM.load_pretrained(local_dir, **kwargs)
125
 
126
  device = device or ("cuda" if torch.cuda.is_available() else "cpu")