Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
145 lines
5.0 KiB
Python
145 lines
5.0 KiB
Python
# Copyright 2025 The HuggingFace Inc. 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.
|
|
"""Testing suite for the PyTorch Jais2 model."""
|
|
|
|
import unittest
|
|
|
|
from transformers import AutoTokenizer, is_torch_available
|
|
from transformers.testing_utils import (
|
|
Expectations,
|
|
cleanup,
|
|
require_deterministic_for_xpu,
|
|
require_torch,
|
|
require_torch_accelerator,
|
|
slow,
|
|
torch_device,
|
|
)
|
|
|
|
from ...causal_lm_tester import CausalLMModelTest, CausalLMModelTester
|
|
|
|
|
|
if is_torch_available():
|
|
import torch
|
|
|
|
from transformers import (
|
|
Jais2Config,
|
|
Jais2ForCausalLM,
|
|
Jais2Model,
|
|
)
|
|
|
|
|
|
class Jais2ModelTester(CausalLMModelTester):
|
|
if is_torch_available():
|
|
config_class = Jais2Config
|
|
base_model_class = Jais2Model
|
|
causal_lm_class = Jais2ForCausalLM
|
|
|
|
config_overrides = {
|
|
"hidden_act": "relu2",
|
|
}
|
|
|
|
|
|
@require_torch
|
|
class Jais2ModelTest(CausalLMModelTest, unittest.TestCase):
|
|
model_tester_class = Jais2ModelTester
|
|
|
|
@unittest.skip("Float8 quantization + TP numerical noise exceeds match threshold")
|
|
def test_tp_generation_quantized(self):
|
|
pass
|
|
|
|
all_model_classes = (
|
|
(
|
|
Jais2Model,
|
|
Jais2ForCausalLM,
|
|
)
|
|
if is_torch_available()
|
|
else ()
|
|
)
|
|
|
|
all_generative_model_classes = (Jais2ForCausalLM,) if is_torch_available() else ()
|
|
|
|
pipeline_model_mapping = (
|
|
{
|
|
"feature-extraction": Jais2Model,
|
|
"text-generation": Jais2ForCausalLM,
|
|
}
|
|
if is_torch_available()
|
|
else {}
|
|
)
|
|
|
|
|
|
@slow
|
|
@require_torch_accelerator
|
|
class Jais2IntegrationTest(unittest.TestCase):
|
|
def setUp(self):
|
|
cleanup(torch_device, gc_collect=True)
|
|
|
|
def tearDown(self):
|
|
cleanup(torch_device, gc_collect=True)
|
|
|
|
@require_deterministic_for_xpu
|
|
def test_model_logits(self):
|
|
model_id = "inceptionai/Jais-2-8B-Chat"
|
|
dummy_input = torch.LongTensor([[0, 0, 0, 0, 0, 0, 1, 2, 3], [1, 1, 2, 3, 4, 5, 6, 7, 8]]).to(torch_device)
|
|
attention_mask = dummy_input.ne(0).to(torch.long)
|
|
|
|
model = Jais2ForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
|
|
|
|
with torch.no_grad():
|
|
logits = model(dummy_input, attention_mask=attention_mask).logits
|
|
logits = logits.float()
|
|
|
|
# fmt: off
|
|
EXPECTED_LOGITS_BATCH0 = Expectations(
|
|
{
|
|
("cuda", None): [-0.9751, -1.0918, -0.9600, -0.9526, -0.9600, -0.9551, -0.9624, -0.9644, -0.9644, -0.9600, -0.9561, -0.9658, -0.9585, -0.9688, -0.9663],
|
|
("xpu", 3): [-0.9692, -1.0859, -0.9541, -0.9468, -0.9546, -0.9492, -0.9570, -0.9585, -0.9585, -0.9541, -0.9507, -0.9604, -0.9526, -0.9634, -0.9609],
|
|
}
|
|
).get_expectation()
|
|
EXPECTED_LOGITS_BATCH1 = Expectations(
|
|
{
|
|
("cuda", None): [-1.5361, -1.6328, -1.5283, -1.5225, -1.5293, -1.5244, -1.5322, -1.5332, -1.5332, -1.5293, -1.5254, -1.5352, -1.5273, -1.5381, -1.5361],
|
|
("xpu", 3): [-1.5342, -1.6318, -1.5264, -1.5205, -1.5273, -1.5225, -1.5303, -1.5312, -1.5312, -1.5273, -1.5234, -1.5332, -1.5254, -1.5361, -1.5342],
|
|
}
|
|
).get_expectation()
|
|
# fmt: on
|
|
|
|
torch.testing.assert_close(
|
|
logits[0, -1, :15],
|
|
torch.tensor(EXPECTED_LOGITS_BATCH0, device=torch_device),
|
|
rtol=1e-3,
|
|
atol=1e-3,
|
|
)
|
|
torch.testing.assert_close(
|
|
logits[1, -1, :15],
|
|
torch.tensor(EXPECTED_LOGITS_BATCH1, device=torch_device),
|
|
rtol=1e-3,
|
|
atol=1e-3,
|
|
)
|
|
|
|
def test_model_generation(self):
|
|
tokenizer = AutoTokenizer.from_pretrained("inceptionai/Jais-2-8B-Chat")
|
|
model = Jais2ForCausalLM.from_pretrained(
|
|
"inceptionai/Jais-2-8B-Chat", torch_dtype=torch.float16, device_map="auto"
|
|
)
|
|
input_text = "Simply put, the theory of relativity states that"
|
|
model_inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
|
model_inputs.pop("token_type_ids", None)
|
|
|
|
generated_ids = model.generate(**model_inputs, max_new_tokens=32, do_sample=False)
|
|
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
|
|
|
EXPECTED_TEXT = "Simply put, the theory of relativity states that the laws of physics are the same for all non-accelerating observers, and that the speed of light in a vacuum is the same for all observers," # fmt: skip
|
|
self.assertEqual(generated_text, EXPECTED_TEXT)
|