itsjorigo commited on
Commit
65341f8
Β·
verified Β·
1 Parent(s): 256cc53

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +13 -2
handler.py CHANGED
@@ -2,6 +2,7 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
2
  import torch
3
 
4
  TOKENIZER_NAME = "polyglots/Extended-Sinhala-LLaMA"
 
5
 
6
  class EndpointHandler:
7
  def __init__(self, path=""):
@@ -15,18 +16,28 @@ class EndpointHandler:
15
  if self.tokenizer.pad_token is None:
16
  self.tokenizer.pad_token = self.tokenizer.eos_token
17
 
 
 
 
 
 
 
 
18
  print(f"Loading model from {path}...")
19
  self.model = AutoModelForCausalLM.from_pretrained(
20
  path,
 
21
  torch_dtype = torch.float16,
22
  device_map = "auto",
23
  trust_remote_code = True,
 
24
  )
25
  # Resize to match extended vocab (139,336 tokens)
26
- self.model.resize_token_embeddings(len(self.tokenizer))
 
27
  self.model.config.pad_token_id = self.tokenizer.eos_token_id
28
  self.model.eval()
29
- print("Model ready!")
30
 
31
  def __call__(self, data: dict) -> dict:
32
  # ── unpack request ───────────────────────────────────────────────────
 
2
  import torch
3
 
4
  TOKENIZER_NAME = "polyglots/Extended-Sinhala-LLaMA"
5
+ VOCAB_SIZE = 139336
6
 
7
  class EndpointHandler:
8
  def __init__(self, path=""):
 
16
  if self.tokenizer.pad_token is None:
17
  self.tokenizer.pad_token = self.tokenizer.eos_token
18
 
19
+ # ── Fix: patch vocab size in config BEFORE model is created ──────────
20
+ # Without this, model is built with 128256 vocab then fails to load
21
+ # the 139336-vocab checkpoint weights
22
+ print(f"Patching config vocab_size to {VOCAB_SIZE:,}...")
23
+ config = AutoConfig.from_pretrained(path, trust_remote_code=True)
24
+ config.vocab_size = VOCAB_SIZE
25
+
26
  print(f"Loading model from {path}...")
27
  self.model = AutoModelForCausalLM.from_pretrained(
28
  path,
29
+ config = config,
30
  torch_dtype = torch.float16,
31
  device_map = "auto",
32
  trust_remote_code = True,
33
+ ignore_mismatched_sizes = True,
34
  )
35
  # Resize to match extended vocab (139,336 tokens)
36
+ # self.model.resize_token_embeddings(len(self.tokenizer))
37
+
38
  self.model.config.pad_token_id = self.tokenizer.eos_token_id
39
  self.model.eval()
40
+ print(f"Ready! Vocab: {self.model.config.vocab_size:,}")
41
 
42
  def __call__(self, data: dict) -> dict:
43
  # ── unpack request ───────────────────────────────────────────────────