Files
HuggingFace_transformer/tests/models/evolla/test_processor_evolla.py
Xibin Bayes Zhou 45c7bfb157 Add evolla rebase main (#36232)
* add evolla

* adding protein encoder part

* add initial processing test

* save processor

* add docstring

* add evolla processor

* add two test

* change vision to protein

* change resampler to sequence_compressor

* change vision to protein

* initial update for llama

* add initial update for llamaForCausalLM

* add `test_processor`, `test_saprot_output`, `test_protein_encoder_output`

* change evolla, but still working on it

* add test_single_forward

* pass test_attention_outputs

* pass test_hidden_states_output

* pass test_save_load and test_from_pretrained_no_checkpoint

* pass test_cpu_offload

* skip some tests

* update new progress

* skip test_model_is_small

* pass test_model_weights_reload_no_missing_tied_weights

* pass test_model_get_set_embeddings

* pass test_cpu_offload

* skip test_resize_embeddings

* add pipeline_model_mapping

* remote old setUp

* pass processor save_pretrained and load_pretrained

* remove pooling layer

* pass test_inputs_embeds_matches_input_ids

* pass test_model_is_small

* pass test_attention_outputs

* pass test_initialization

* pass test_model_get_set_embeddings

* pass test_single_forward

* skip test_disk_offload_bin and test_disk_offload_safetensors

* fix most tests

* pass test_protein_encoder_output

* remove useless code

* add EvollaForProteinText2Text

* pass test_saprot_output

* pass all EvollaModelTest test and remove processor test

* add processor test to its own file

* skip is_training since esm skipped it and the saprot code causes error when setting is_training True

* pass processor tests

* solve all except config

* pass most cases

* change init

* add doc to `configuration_evolla.py`

* remove image_processing test

* remove extra processor test

* remove extra modules

* remove extra modules

* change all configs into one config

* pass all evolla test

* pass `make fixup`

* update short summary

* update Evolla-10B-hf

* pass check_dummies.py and check_code_quality

* fix  `tests/models/auto/test_tokenization_auto.py::AutoTokenizerTest::test_model_name_edge_cases_in_mappings`

* remove dummy codes

* change format

* fix llava issue

* update format

* update to solve llama3 access issue

* update to make forward right

* solve processor save load problem from instructblip solution

* remove unexpected file

* skip `test_generation_tester_mixin_inheritance`

* add `test_single_forward_correct` and `test_inference_natural_language_protein_reasoning`

* add `modular_evolla.py`

* solved issue #36362

* run `make fixup`

* update modular

* solve float32 training

* add fix

* solve `utils/check_docstrings.py`

* update

* update

* update

* remove other files and replace sequential and einsum

* add use case in document

* update the models

* update model

* change some wrong code

* Update src/transformers/models/evolla/modular_evolla.py

Co-authored-by: Cyril Vallez <cyril.vallez@gmail.com>

* Update src/transformers/models/evolla/modular_evolla.py

Co-authored-by: Cyril Vallez <cyril.vallez@gmail.com>

* Update src/transformers/models/evolla/modular_evolla.py

Co-authored-by: Cyril Vallez <cyril.vallez@gmail.com>

* Update src/transformers/models/evolla/modular_evolla.py

Co-authored-by: Cyril Vallez <cyril.vallez@gmail.com>

* fix issues mentioned in PR

* update style and rearrange the placement

* fix return_dict argument issue

* solve SaProtConfig issue

* Solve EvollaSaProtRotaryEmbedding issue

* solve attention_mask issue

* solve almosst all issues

* make style

* update config

* remove unrelated pickle file

* delete pickle files

* fix config

* simplify a lot

* remove past k-v from encoder

* continue work

* style

* skip it from init

* fix init

* fix init

* simplify more

* fill in docstrings

* change test for generation

* skip test

* fix style

---------

Co-authored-by: Chenchen Han <13980209828@163.com>
Co-authored-by: Cyril Vallez <cyril.vallez@huggingface.co>
Co-authored-by: Cyril Vallez <cyril.vallez@gmail.com>
2025-07-25 19:11:57 +02:00

296 lines
9.9 KiB
Python

# Copyright 2025 The HuggingFace Team. All rights reserved.
#
# 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 random
import shutil
import tempfile
import unittest
from transformers import (
AutoProcessor,
EvollaProcessor,
)
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
EVOLLA_VALID_AA = list("ACDEFGHIKLMNPQRSTVWY#")
EVOLLA_VALID_FS = list("pynwrqhgdlvtmfsaeikc#")
@require_torch
class EvollaProcessorTest(ProcessorTesterMixin, unittest.TestCase):
processor_class = EvollaProcessor
def setUp(self):
self.tmpdirname = tempfile.mkdtemp()
processor = EvollaProcessor.from_pretrained("westlake-repl/Evolla-10B-hf")
processor.save_pretrained(self.tmpdirname)
self.input_keys = ["protein_input_ids", "protein_attention_mask", "input_ids", "attention_mask"]
def prepare_input_and_expected_output(self):
amino_acid_sequence = "AAAA"
foldseek_sequence = "dddd"
question = "What is the function of this protein?"
expected_output = {
"protein_input_ids": torch.tensor([[0, 13, 13, 13, 13, 2]]),
"protein_attention_mask": torch.tensor([[1, 1, 1, 1, 1, 1]]),
"input_ids": torch.tensor(
[
[
128000,
128006,
9125,
128007,
271,
2675,
527,
459,
15592,
6335,
430,
649,
4320,
904,
4860,
922,
13128,
13,
128009,
128006,
882,
128007,
271,
3923,
374,
279,
734,
315,
420,
13128,
30,
128009,
128006,
78191,
128007,
271,
]
]
),
"attention_mask": torch.tensor(
[
[
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
]
]
),
}
protein_dict = {"aa_seq": amino_acid_sequence, "foldseek": foldseek_sequence}
message = [
{"role": "system", "content": "You are an AI expert that can answer any questions about protein."},
{"role": "user", "content": question},
]
return protein_dict, message, expected_output
def test_processor(self):
protein_tokenizer = self.get_protein_tokenizer()
tokenizer = self.get_tokenizer()
processor = EvollaProcessor(protein_tokenizer, tokenizer)
protein_dict, message, expected_output = self.prepare_input_and_expected_output()
inputs = processor(proteins=[protein_dict], messages_list=[message])
# check if the input is correct
for key, value in expected_output.items():
self.assertTrue(
torch.equal(inputs[key], value),
f"inputs[key] is {inputs[key]} and expected_output[key] is {expected_output[key]}",
)
def get_tokenizer(self, **kwargs):
return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer
def get_protein_tokenizer(self, **kwargs):
return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).protein_tokenizer
def tearDown(self):
shutil.rmtree(self.tmpdirname)
def prepare_inputs_single(self):
proteins = {
"aa_seq": "".join(random.choices(EVOLLA_VALID_AA, k=100)),
"foldseek": "".join(random.choices(EVOLLA_VALID_FS, k=100)),
}
return proteins
def prepare_inputs_pair(self):
proteins = [
{
"aa_seq": "".join(random.choices(EVOLLA_VALID_AA, k=100)),
"foldseek": "".join(random.choices(EVOLLA_VALID_FS, k=100)),
},
{
"aa_seq": "".join(random.choices(EVOLLA_VALID_AA, k=100)),
"foldseek": "".join(random.choices(EVOLLA_VALID_FS, k=100)),
},
]
return proteins
def prepare_inputs_long(self):
proteins = [
{
"aa_seq": "".join(random.choices(EVOLLA_VALID_AA, k=100)),
"foldseek": "".join(random.choices(EVOLLA_VALID_FS, k=100)),
},
{
"aa_seq": "".join(random.choices(EVOLLA_VALID_AA, k=2000)),
"foldseek": "".join(random.choices(EVOLLA_VALID_FS, k=2000)),
},
]
return proteins
def prepare_inputs_short(self):
proteins = [
{
"aa_seq": "".join(random.choices(EVOLLA_VALID_AA, k=1)),
"foldseek": "".join(random.choices(EVOLLA_VALID_FS, k=1)),
},
{
"aa_seq": "".join(random.choices(EVOLLA_VALID_AA, k=100)),
"foldseek": "".join(random.choices(EVOLLA_VALID_FS, k=100)),
},
]
return proteins
def prepare_inputs_empty(self):
proteins = [
{
"aa_seq": "",
"foldseek": "",
},
{
"aa_seq": "".join(random.choices(EVOLLA_VALID_AA, k=100)),
"foldseek": "".join(random.choices(EVOLLA_VALID_FS, k=100)),
},
]
return proteins
def prepare_inputs(self, protein_types="pair"):
r"""
Prepare inputs for the test.
Args:
protein_types (`str`): the types of proteins to prepare.
- "single": a single correct protein.
- "pair": a pair of correct proteins.
- "long": a long sequence of correct proteins and a correct protein.
- "short": a short sequence of correct proteins (only have 1 aa) and a correct protein.
- "empty": an empty sequence of proteins and a correct protein.
"""
if protein_types == "single":
proteins = self.prepare_inputs_single()
elif protein_types == "pair":
proteins = self.prepare_inputs_pair()
elif protein_types == "long":
proteins = self.prepare_inputs_long()
elif protein_types == "short":
proteins = self.prepare_inputs_short()
elif protein_types == "empty":
proteins = self.prepare_inputs_empty()
else:
raise ValueError(
f"protein_types should be one of 'single', 'pair', 'long','short', 'empty', but got {protein_types}"
)
questions = ["What is the function of the protein?"] * len(proteins)
messages_list = []
for question in questions:
messages = [
{"role": "system", "content": "You are an AI expert that can answer any questions about protein."},
{"role": "user", "content": question},
]
messages_list.append(messages)
return proteins, messages_list
def test_tokenizer_decode(self):
protein_tokenizer = self.get_protein_tokenizer()
tokenizer = self.get_tokenizer()
processor = EvollaProcessor(tokenizer=tokenizer, protein_tokenizer=protein_tokenizer, return_tensors="pt")
predicted_ids = [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]
decoded_processor = processor.batch_decode(predicted_ids)
decoded_tok = tokenizer.batch_decode(predicted_ids)
self.assertListEqual(decoded_tok, decoded_processor)
def test_model_input_names(self):
protein_tokenizer = self.get_protein_tokenizer()
tokenizer = self.get_tokenizer()
processor = EvollaProcessor(tokenizer=tokenizer, protein_tokenizer=protein_tokenizer)
proteins, messages_list = self.prepare_inputs()
inputs = processor(messages_list=messages_list, proteins=proteins, padding="longest", return_tensors="pt")
# For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
self.assertSetEqual(set(inputs.keys()), set(self.input_keys))