Skip to content

Commit 65a38cc

Browse files
authored
[Cherry-Pick][Fix] deepsparse – tokenizer stride size issue with original transformers (#1426)
* [Fix] Remove erronous LIB.kv_cache input when using external kv cache management (#1337) * initial commit * initial commit * cleanup * cleanup2 * initial commit * final solution
1 parent 0d5c7b7 commit 65a38cc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: src/deepsparse/transformers/pipelines/question_answering.py

+15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import json
3838
import logging
3939
import os
40+
import warnings
4041
from typing import Any, Dict, List, Optional, Tuple, Type
4142

4243
import numpy
@@ -504,6 +505,20 @@ def route_input_to_bucket(
504505
def _tokenize(self, example: SquadExample, *args):
505506
# The logic here closely matches the tokenization step performed
506507
# on evaluation dataset in the SparseML question answering training script
508+
509+
added_special_tokens = self.tokenizer.num_special_tokens_to_add()
510+
effective_max_length = self.sequence_length - added_special_tokens
511+
if self.doc_stride >= effective_max_length:
512+
new_doc_stride = effective_max_length
513+
warnings.warn(
514+
f"Tokenizer stride set to {self.doc_stride}, "
515+
f"which is greater than or equal to its effective max length "
516+
f"of {effective_max_length} (= {self.sequence_length} "
517+
f"original max length - {added_special_tokens} added special tokens). "
518+
f"Capping the doc stride to {new_doc_stride}"
519+
)
520+
self._doc_stride = new_doc_stride
521+
507522
if not self.tokenizer.is_fast:
508523
raise ValueError(
509524
"This example script only works for models that have a fast tokenizer."

0 commit comments

Comments
 (0)