the-only-ashutosh commited on
Commit
de7bd61
·
1 Parent(s): d23fe11

feed zeros for required token_type_ids input

Browse files
Files changed (3) hide show
  1. Dockerfile +2 -1
  2. __pycache__/app.cpython-312.pyc +0 -0
  3. app.py +5 -2
Dockerfile CHANGED
@@ -24,7 +24,8 @@ tok=AutoTokenizer.from_pretrained(m); \
24
  sess=ort.InferenceSession(hf_hub_download(m,'model.onnx'),providers=['CPUExecutionProvider']); \
25
  names={i.name for i in sess.get_inputs()}; \
26
  e=tok(['ભાવ સમાચાર','mandi prices'],padding=True,truncation=True,max_length=512,return_tensors='np'); \
27
- h=sess.run(None,{k:v for k,v in e.items() if k in names})[0]; \
 
28
  c=h[:,0]; c=c/np.linalg.norm(c,axis=1,keepdims=True); \
29
  assert c.shape==(2,1024), c.shape; \
30
  assert abs(float(np.linalg.norm(c[0]))-1.0)<1e-3; \
 
24
  sess=ort.InferenceSession(hf_hub_download(m,'model.onnx'),providers=['CPUExecutionProvider']); \
25
  names={i.name for i in sess.get_inputs()}; \
26
  e=tok(['ભાવ સમાચાર','mandi prices'],padding=True,truncation=True,max_length=512,return_tensors='np'); \
27
+ f={n:(e[n] if n in e else np.zeros_like(e['input_ids'])) for n in names}; \
28
+ h=sess.run(None,f)[0]; \
29
  c=h[:,0]; c=c/np.linalg.norm(c,axis=1,keepdims=True); \
30
  assert c.shape==(2,1024), c.shape; \
31
  assert abs(float(np.linalg.norm(c[0]))-1.0)<1e-3; \
__pycache__/app.cpython-312.pyc CHANGED
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
 
app.py CHANGED
@@ -57,8 +57,11 @@ def _encode(texts: list[str]) -> list[list[float]]:
57
  chunk, padding=True, truncation=True,
58
  max_length=MAX_TOKENS, return_tensors="np",
59
  )
60
- # bge-m3 (xlm-roberta) has no token_type_ids; feed only what the graph wants.
61
- feed = {k: v for k, v in enc.items() if k in _input_names}
 
 
 
62
  hidden = _session.run(None, feed)[0] # (B, T, 1024) last_hidden_state
63
  # dense embedding = CLS token (position 0), then L2-normalize so cosine == dot.
64
  cls = hidden[:, 0]
 
57
  chunk, padding=True, truncation=True,
58
  max_length=MAX_TOKENS, return_tensors="np",
59
  )
60
+ # The ONNX graph requires token_type_ids but the xlm-roberta tokenizer
61
+ # doesn't emit them (bge-m3 ignores them) feed zeros for any required
62
+ # input the tokenizer didn't produce.
63
+ ids = enc["input_ids"]
64
+ feed = {n: enc[n] if n in enc else np.zeros_like(ids) for n in _input_names}
65
  hidden = _session.run(None, feed)[0] # (B, T, 1024) last_hidden_state
66
  # dense embedding = CLS token (position 0), then L2-normalize so cosine == dot.
67
  cls = hidden[:, 0]