mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-04 13:20:12 +06:00

* generate from config mvp * fix failing tests * max_time test * Load default gen config at model load time; Update docs * further documentation; add tests * adapt rag to the new structure * handle models not instantiated with from_pretained (like in tests) * better default generation config * add can_generate fn * handle legacy use case of ad hoc model config changes * initialize gen config from config in individual methods, if gen config is none * fix _get_decoder_start_token_id when called outside GenerationMixin * correct model config load order (set attr > model config > decoder config) * update rag to match latest changes * Apply suggestions from code review Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com> Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * load gen config from model config in model.from_pretrained * fix can_generate fn * handle generate calls without a previous from_pretrained (e.g. tests) * add legacy behavior (and a warning) * lower logger severity Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com> Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
77 lines
3.1 KiB
Python
77 lines
3.1 KiB
Python
# coding=utf-8
|
|
# Copyright 2022 The HuggingFace Team Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a clone of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import copy
|
|
import tempfile
|
|
import unittest
|
|
|
|
from parameterized import parameterized
|
|
from transformers import AutoConfig, GenerationConfig
|
|
|
|
|
|
class LogitsProcessorTest(unittest.TestCase):
|
|
@parameterized.expand([(None,), ("foo.json",)])
|
|
def test_save_load_config(self, config_name):
|
|
config = GenerationConfig(
|
|
do_sample=True,
|
|
temperature=0.7,
|
|
length_penalty=1.0,
|
|
bad_words_ids=[[1, 2, 3], [4, 5]],
|
|
)
|
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
config.save_pretrained(tmp_dir, config_name=config_name)
|
|
loaded_config = GenerationConfig.from_pretrained(tmp_dir, config_name=config_name)
|
|
|
|
# Checks parameters that were specified
|
|
self.assertEqual(loaded_config.do_sample, True)
|
|
self.assertEqual(loaded_config.temperature, 0.7)
|
|
self.assertEqual(loaded_config.length_penalty, 1.0)
|
|
self.assertEqual(loaded_config.bad_words_ids, [[1, 2, 3], [4, 5]])
|
|
|
|
# Checks parameters that were not specified (defaults)
|
|
self.assertEqual(loaded_config.top_k, 50)
|
|
self.assertEqual(loaded_config.max_length, 20)
|
|
self.assertEqual(loaded_config.max_time, None)
|
|
|
|
def test_from_model_config(self):
|
|
model_config = AutoConfig.from_pretrained("gpt2")
|
|
generation_config_from_model = GenerationConfig.from_model_config(model_config)
|
|
default_generation_config = GenerationConfig()
|
|
|
|
# The generation config has loaded a few non-default parameters from the model config
|
|
self.assertNotEqual(generation_config_from_model, default_generation_config)
|
|
|
|
# One of those parameters is eos_token_id -- check if it matches
|
|
self.assertNotEqual(generation_config_from_model.eos_token_id, default_generation_config.eos_token_id)
|
|
self.assertEqual(generation_config_from_model.eos_token_id, model_config.eos_token_id)
|
|
|
|
def test_update(self):
|
|
generation_config = GenerationConfig()
|
|
update_kwargs = {
|
|
"max_new_tokens": 1024,
|
|
"foo": "bar",
|
|
}
|
|
update_kwargs_copy = copy.deepcopy(update_kwargs)
|
|
unused_kwargs = generation_config.update(**update_kwargs)
|
|
|
|
# update_kwargs was not modified (no side effects)
|
|
self.assertEqual(update_kwargs, update_kwargs_copy)
|
|
|
|
# update_kwargs was used to update the config on valid attributes
|
|
self.assertEqual(generation_config.max_new_tokens, 1024)
|
|
|
|
# `.update()` returns a dictionary of unused kwargs
|
|
self.assertEqual(unused_kwargs, {"foo": "bar"})
|