mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-06 22:30:09 +06:00

* draft structure * depth decoder with forward pre hook * full model forward draft * draft update * depth decoder update * ConversationalSpeechModelForCausalLM udpates * add generate * max length criteria small fix * udpate * updates * generation update * update in loss compute * conversion script * update for correct input embeddings * handle interleaved rope * update * update * update * support compile * update training * add doc * update doc * correct inits * ConversationalSpeechModel -> Csm * conf update * name update * tests CsmForCausalLMTest * convert use cached_file * conf + modeling updates * generate utils handle third dim shape * integration test * modeling + conf updates * common test handle more than 2 dims * add nested audio list utils * processing handle nested audio list * csm processing draft * mimi util * init updates * modular update * convert modular * processing update * csm tests update * generate tests handle third dim * generate utils handle third dim * propagate _get_initial_cache_position update * tied_weight_keys update + convert correctly * fix inputs_embeds * revert audio nested list * batch inference update + return audio * audio_utils update * processor update * some more integration tests * remove old test * porcessing output labels * improve * fix * update rope values with equivalent ones * conversion update * udpate tests * handle depth decoder generation config * remove default eos_token_id * make style * revert modeling_mimi * add default generation_config * remove sdpa since handled by default * make * fix conflict * fix conflicts * correct naming * correct imports * make * causal -> conditional naming * causal -> conditional naming * auto update * make * make * add doc * test update * fix weight init * audio tokens offsets as buffer * 4d mask in conditional class * make * doc update * fix causal mask * fix causal mask * doc update * doc update * add processor doc * update doc * fix 4d causal mask * update make_list_of_audio * do not default to mutable * remove duplicates * remove useless reset_parameters * use GradientCheckpointingLayer * use can_return_tuple * formatting * prepend placeholder in _sample * torch compile fix * some more fixies * convert modular * fix * default max_length in convert * handle depth decoder generation config correctly * clearer formulation * handle output_loading_info * handle softmax warning * add doc * propagate _get_initial_cache_position changes * generation in its own module * add processor tests * fix compile witu cuda graphs * fix compile with cuda graphs * add csm.md * include CSM loss * doc nit * doc nit * doc nit * Update docs/source/en/model_doc/csm.md Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> * add save_audio to processor * Update src/transformers/models/csm/modular_csm.py Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com> * doc update * simplify audio_codes_mask computation * doc update * simplify loss computation * fix static cache test * fix * remove comment * simplify encoded length computation * use hf-internal-testing * doc update * cast to float before numpy * nit * mem efficient codebook head * nit * cat input values with cutoffs --------- Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
141 lines
6.9 KiB
Python
141 lines
6.9 KiB
Python
# Copyright 2024 HuggingFace 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 copy 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 json
|
|
import shutil
|
|
import tempfile
|
|
import unittest
|
|
|
|
import jinja2
|
|
import numpy as np
|
|
|
|
from transformers import CsmProcessor
|
|
from transformers.testing_utils import require_torch
|
|
from transformers.utils import is_torch_available
|
|
|
|
from ...test_processing_common import ProcessorTesterMixin
|
|
|
|
|
|
if is_torch_available():
|
|
import torch
|
|
|
|
|
|
@require_torch
|
|
class CsmProcessorTest(ProcessorTesterMixin, unittest.TestCase):
|
|
processor_class = CsmProcessor
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
# TODO: @eustlb, change for hf-internal-testing/csm-1b
|
|
cls.checkpoint = "eustlb/csm-1b"
|
|
processor = CsmProcessor.from_pretrained(cls.checkpoint)
|
|
cls.audio_token = processor.audio_token
|
|
cls.audio_token_id = processor.audio_token_id
|
|
cls.pad_token_id = processor.tokenizer.pad_token_id
|
|
cls.bos_token_id = processor.tokenizer.bos_token_id
|
|
cls.tmpdirname = tempfile.mkdtemp()
|
|
processor.save_pretrained(cls.tmpdirname)
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
shutil.rmtree(cls.tmpdirname, ignore_errors=True)
|
|
|
|
def prepare_processor_dict(self):
|
|
return {"chat_template": "\n{%- for message in messages %}\n {#-- Validate role is a stringified integer --#}\n {%- if not message['role'] is string or not message['role'].isdigit() %}\n {{- raise_exception(\"The role must be an integer or a stringified integer (e.g. '0') designating the speaker id\") }}\n {%- endif %}\n\n {#-- Validate content is a list --#}\n {%- set content = message['content'] %}\n {%- if content is not iterable or content is string %}\n {{- raise_exception(\"The content must be a list\") }}\n {%- endif %}\n\n {#-- Collect content types --#}\n {%- set content_types = content | map(attribute='type') | list %}\n {%- set is_last = loop.last %}\n\n {#-- Last message validation --#}\n {%- if is_last %}\n {%- if 'text' not in content_types %}\n {{- raise_exception(\"The last message must include one item of type 'text'\") }}\n {%- elif (content_types | select('equalto', 'text') | list | length > 1) or (content_types | select('equalto', 'audio') | list | length > 1) %}\n {{- raise_exception(\"At most two items are allowed in the last message: one 'text' and one 'audio'\") }}\n {%- endif %}\n\n {#-- All other messages validation --#}\n {%- else %}\n {%- if content_types | select('equalto', 'text') | list | length != 1\n or content_types | select('equalto', 'audio') | list | length != 1 %}\n {{- raise_exception(\"Each message (except the last) must contain exactly one 'text' and one 'audio' item\") }}\n {%- elif content_types | reject('in', ['text', 'audio']) | list | length > 0 %}\n {{- raise_exception(\"Only 'text' and 'audio' types are allowed in content\") }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n\n{%- for message in messages %}\n {{- bos_token }}\n {{- '[' + message['role'] + ']' }}\n {{- message['content'][0]['text'] }}\n {{- eos_token }}\n {%- if message['content']|length > 1 %}\n {{- '<|AUDIO|><|audio_eos|>' }}\n {%- endif %}\n{%- endfor %}\n"} # fmt: skip
|
|
|
|
def test_chat_template_is_saved(self):
|
|
processor_loaded = self.processor_class.from_pretrained(self.tmpdirname)
|
|
processor_dict_loaded = json.loads(processor_loaded.to_json_string())
|
|
# chat templates aren't serialized to json in processors
|
|
self.assertFalse("chat_template" in processor_dict_loaded.keys())
|
|
|
|
# they have to be saved as separate file and loaded back from that file
|
|
# so we check if the same template is loaded
|
|
processor_dict = self.prepare_processor_dict()
|
|
self.assertTrue(processor_loaded.chat_template == processor_dict.get("chat_template", None))
|
|
|
|
def test_apply_chat_template(self):
|
|
# Message contains content which a mix of lists with images and image urls and string
|
|
messages = [
|
|
{
|
|
"role": "0",
|
|
"content": [
|
|
{"type": "text", "text": "This is a test sentence 0."},
|
|
{"type": "audio"},
|
|
],
|
|
},
|
|
{
|
|
"role": "1",
|
|
"content": [
|
|
{"type": "text", "text": "This is a test sentence 1."},
|
|
{"type": "audio"},
|
|
],
|
|
},
|
|
{
|
|
"role": "0",
|
|
"content": [
|
|
{"type": "text", "text": "This is a prompt."},
|
|
],
|
|
},
|
|
]
|
|
processor = CsmProcessor.from_pretrained(self.tmpdirname)
|
|
rendered = processor.apply_chat_template(messages, tokenize=False)
|
|
|
|
expected_rendered = (
|
|
"<|begin_of_text|>[0]This is a test sentence 0.<|end_of_text|>"
|
|
"<|AUDIO|><|audio_eos|>"
|
|
"<|begin_of_text|>[1]This is a test sentence 1.<|end_of_text|>"
|
|
"<|AUDIO|><|audio_eos|>"
|
|
"<|begin_of_text|>[0]This is a prompt.<|end_of_text|>"
|
|
)
|
|
self.assertEqual(rendered, expected_rendered)
|
|
|
|
messages = [
|
|
{
|
|
"role": "0",
|
|
"content": [
|
|
{"type": "text", "text": "This is a test sentence."},
|
|
],
|
|
},
|
|
{
|
|
"role": "1",
|
|
"content": [
|
|
{"type": "text", "text": "This is a test sentence."},
|
|
],
|
|
},
|
|
]
|
|
|
|
# this should raise an error because the CSM processor requires audio content in the messages expect the last one
|
|
with self.assertRaises(jinja2.exceptions.TemplateError):
|
|
input_ids = processor.apply_chat_template(messages, tokenize=False)
|
|
|
|
# now let's very that it expands audio tokens correctly
|
|
messages = [
|
|
{
|
|
"role": "0",
|
|
"content": [
|
|
{"type": "text", "text": "This is a test sentence."},
|
|
{"type": "audio", "audio": np.zeros(4096)},
|
|
],
|
|
},
|
|
]
|
|
|
|
input_ids = processor.apply_chat_template(messages, tokenize=True)
|
|
|
|
# 4096 audio input values should give 3 audio tokens
|
|
expected_ids = torch.tensor(
|
|
[[128000, 58, 15, 60, 2028, 374, 264, 1296, 11914, 13, 128001, 128002, 128002, 128002, 128003]]
|
|
)
|
|
torch.testing.assert_close(input_ids, expected_ids)
|