Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #1047: LLM caching: Bug when sharing prefix in target #1048

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion skorch/llm/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def generate_logits(self, *, label_id, **kwargs):
recorded_logits = []
logits_cached = self.get_cache(kwargs)
while logits_cached is not None:
if label_id[0] == self.tokenizer.eos_token_id:
if not label_id or label_id[0] == self.tokenizer.eos_token_id:
# don't extend with eos_token -- it is already there at the end,
# we don't need it twice
break
Expand Down
17 changes: 17 additions & 0 deletions skorch/tests/llm/test_llm_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,23 @@ def test_caching_is_faster(self, classifier_cls):
# at least 1/3 faster
assert cached_time < 0.1 * uncached_time

def test_caching_works_shared_label_prefix_without_eos(self, classifier_cls):
clf = classifier_cls('gpt2')

# carefully chosen class labels so that one label has the other label as
# its prefix. '11111' = '11' + '111'. For models that tokenize single
# digits indepdentenly this is far more relevant.
X = np.array(["Hey there", "No thank you"])
y = ['11', '11111']

clf.fit(X, y)

y_pred_1 = clf.predict(X)
y_pred_2 = clf.predict(X)

# does not raise and gives the same results
np.testing.assert_array_equal(y_pred_1, y_pred_2)

def test_custom_prompt(self, model, tokenizer, classifier_cls, X):
prompt = "Please classify my text:\n{text}\n\nLabels: {labels}\n\n"
clf = classifier_cls(
Expand Down
Loading