Upload rl_code/verl/utils/tokenizer.py with huggingface_hub
Browse files
rl_code/verl/utils/tokenizer.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Bytedance Ltd. and/or its affiliates
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Utils for tokenization."""
|
| 15 |
+
|
| 16 |
+
from typing import Optional
|
| 17 |
+
|
| 18 |
+
from transformers import AutoProcessor, AutoTokenizer, PreTrainedTokenizer, ProcessorMixin
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_tokenizer(model_path: str, override_chat_template: Optional[str] = None, **kwargs) -> PreTrainedTokenizer:
|
| 22 |
+
"""Create a huggingface pretrained tokenizer."""
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, **kwargs)
|
| 24 |
+
if override_chat_template is not None:
|
| 25 |
+
tokenizer.chat_template = override_chat_template
|
| 26 |
+
|
| 27 |
+
if tokenizer.bos_token == "<bos>" and tokenizer.eos_token == "<eos>":
|
| 28 |
+
# the EOS token in gemma2 & gemma3 is ambiguious, which may worsen RL performance.
|
| 29 |
+
# https://huggingface.co/google/gemma-2-2b-it/commit/17a01657f5c87135bcdd0ec7abb4b2dece04408a
|
| 30 |
+
print("Found gemma model. Set eos_token and eos_token_id to <end_of_turn> and 107.")
|
| 31 |
+
tokenizer.eos_token = "<end_of_turn>"
|
| 32 |
+
|
| 33 |
+
if tokenizer.pad_token_id is None:
|
| 34 |
+
print("Pad token is None. Set it to eos_token.")
|
| 35 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 36 |
+
|
| 37 |
+
return tokenizer
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_processor(model_path: str, override_chat_template: Optional[str] = None, **kwargs) -> Optional[ProcessorMixin]:
|
| 41 |
+
"""Create a huggingface pretrained processor."""
|
| 42 |
+
processor = AutoProcessor.from_pretrained(model_path, **kwargs)
|
| 43 |
+
if override_chat_template is not None:
|
| 44 |
+
processor.chat_template = override_chat_template
|
| 45 |
+
|
| 46 |
+
# Avoid load tokenizer, see:
|
| 47 |
+
# https://github.com/huggingface/transformers/blob/v4.52.4/src/transformers/models/auto/processing_auto.py#L386
|
| 48 |
+
if processor is not None and "Processor" not in processor.__class__.__name__:
|
| 49 |
+
processor = None
|
| 50 |
+
|
| 51 |
+
return processor
|