Skip to content
Merged
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
22 changes: 22 additions & 0 deletions tests/generation/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tempfile
import unittest
import warnings
from pathlib import Path

import numpy as np
import pytest
Expand Down Expand Up @@ -4985,6 +4986,27 @@ def test_custom_generate_requires_trust_remote_code(self):
with self.assertRaises(ValueError):
model.generate(**model_inputs, custom_generate="transformers-community/custom_generate_example")

def test_custom_generate_local_directory(self):
"""Tests that custom_generate works with local directories containing importable relative modules"""
with tempfile.TemporaryDirectory() as tmp_dir:
custom_generate_dir = Path(tmp_dir) / "custom_generate"
custom_generate_dir.mkdir()
with open(custom_generate_dir / "generate.py", "w") as f:
f.write("from .helper import ret_success\ndef generate(*args, **kwargs):\n return ret_success()\n")
with open(custom_generate_dir / "helper.py", "w") as f:
f.write('def ret_success():\n return "success"\n')
model = AutoModelForCausalLM.from_pretrained(
"hf-internal-testing/tiny-random-MistralForCausalLM", device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-MistralForCausalLM")
model_inputs = tokenizer("Hello, world!", return_tensors="pt").to(model.device)
value = model.generate(
**model_inputs,
custom_generate=str(tmp_dir),
trust_remote_code=True,
)
assert value == "success"


@require_torch
class TokenHealingTestCase(unittest.TestCase):
Expand Down