Mishamq commited on
Commit
3fdaf71
·
verified ·
1 Parent(s): d5109fc

Upload hybridna_tokenizer.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. hybridna_tokenizer.py +22 -1
hybridna_tokenizer.py CHANGED
@@ -160,6 +160,8 @@ class HybriDNATokenizer(PreTrainedTokenizer):
160
  truncation: bool = True,
161
  max_length: Optional[int] = None,
162
  add_special_tokens: bool = True,
 
 
163
  ):
164
  # ---------- detect batch vs single ----------
165
  is_batch = not isinstance(text, str)
@@ -220,7 +222,26 @@ class HybriDNATokenizer(PreTrainedTokenizer):
220
  batch_input_ids = batch_input_ids[0]
221
  batch_attention = batch_attention[0]
222
 
223
- return {
224
  "input_ids": batch_input_ids,
225
  "attention_mask": batch_attention,
226
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  truncation: bool = True,
161
  max_length: Optional[int] = None,
162
  add_special_tokens: bool = True,
163
+ return_tensors: Optional[str] = None,
164
+ **kwargs,
165
  ):
166
  # ---------- detect batch vs single ----------
167
  is_batch = not isinstance(text, str)
 
222
  batch_input_ids = batch_input_ids[0]
223
  batch_attention = batch_attention[0]
224
 
225
+ result = {
226
  "input_ids": batch_input_ids,
227
  "attention_mask": batch_attention,
228
  }
229
+
230
+ # ---------- convert to tensors if requested ----------
231
+ if return_tensors == "pt":
232
+ import torch
233
+ if is_batch:
234
+ result["input_ids"] = torch.tensor(np.stack(result["input_ids"]), dtype=torch.long)
235
+ result["attention_mask"] = torch.tensor(np.stack(result["attention_mask"]), dtype=torch.long)
236
+ else:
237
+ result["input_ids"] = torch.tensor(result["input_ids"], dtype=torch.long).unsqueeze(0)
238
+ result["attention_mask"] = torch.tensor(result["attention_mask"], dtype=torch.long).unsqueeze(0)
239
+ elif return_tensors == "np":
240
+ if is_batch:
241
+ result["input_ids"] = np.stack(result["input_ids"])
242
+ result["attention_mask"] = np.stack(result["attention_mask"])
243
+ else:
244
+ result["input_ids"] = np.expand_dims(result["input_ids"], 0)
245
+ result["attention_mask"] = np.expand_dims(result["attention_mask"], 0)
246
+
247
+ return result