| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| from __future__ import annotations |
|
|
| import json |
| import os |
| import random |
| import tempfile |
| import unittest |
| import unittest.mock as mock |
|
|
| from huggingface_hub import HfFolder, snapshot_download |
| from requests.exceptions import HTTPError |
|
|
| from transformers import is_tf_available |
| from transformers.configuration_utils import PretrainedConfig |
| from transformers.testing_utils import ( |
| TOKEN, |
| USER, |
| CaptureLogger, |
| TemporaryHubRepo, |
| is_staging_test, |
| require_safetensors, |
| require_tf, |
| slow, |
| ) |
| from transformers.utils import ( |
| SAFE_WEIGHTS_INDEX_NAME, |
| SAFE_WEIGHTS_NAME, |
| TF2_WEIGHTS_INDEX_NAME, |
| TF2_WEIGHTS_NAME, |
| logging, |
| ) |
|
|
|
|
| logger = logging.get_logger(__name__) |
|
|
|
|
| if is_tf_available(): |
| import h5py |
| import numpy as np |
| import tensorflow as tf |
|
|
| from transformers import ( |
| BertConfig, |
| RagRetriever, |
| TFBertForSequenceClassification, |
| TFBertModel, |
| TFRagModel, |
| ) |
| from transformers.modeling_tf_utils import keras, tf_shard_checkpoint, unpack_inputs |
| from transformers.tf_utils import stable_softmax |
|
|
| tf.config.experimental.enable_tensor_float_32_execution(False) |
|
|
|
|
| @require_tf |
| class TFModelUtilsTest(unittest.TestCase): |
| def test_cached_files_are_used_when_internet_is_down(self): |
| |
| response_mock = mock.Mock() |
| response_mock.status_code = 500 |
| response_mock.headers = {} |
| response_mock.raise_for_status.side_effect = HTTPError |
| response_mock.json.return_value = {} |
|
|
| |
| _ = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
|
|
| |
| with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: |
| _ = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
| |
| mock_head.assert_called() |
|
|
| |
| def test_unpack_inputs(self): |
| class DummyModel: |
| def __init__(self): |
| config_kwargs = {"output_attentions": False, "output_hidden_states": False, "return_dict": False} |
| self.config = PretrainedConfig(**config_kwargs) |
| self.main_input_name = "input_ids" |
|
|
| @unpack_inputs |
| def call( |
| self, |
| input_ids=None, |
| past_key_values=None, |
| output_attentions=None, |
| output_hidden_states=None, |
| return_dict=None, |
| ): |
| return input_ids, past_key_values, output_attentions, output_hidden_states, return_dict |
|
|
| @unpack_inputs |
| def foo(self, pixel_values, output_attentions=None, output_hidden_states=None, return_dict=None): |
| return pixel_values, output_attentions, output_hidden_states, return_dict |
|
|
| dummy_model = DummyModel() |
| input_ids = tf.constant([0, 1, 2, 3], dtype=tf.int32) |
| past_key_values = tf.constant([4, 5, 6, 7], dtype=tf.int32) |
| pixel_values = tf.constant([8, 9, 10, 11], dtype=tf.int32) |
|
|
| |
| output = dummy_model.call(input_ids=input_ids, past_key_values=past_key_values) |
| tf.debugging.assert_equal(output[0], input_ids) |
| tf.debugging.assert_equal(output[1], past_key_values) |
| self.assertFalse(output[2]) |
| self.assertFalse(output[3]) |
| self.assertFalse(output[4]) |
|
|
| |
| output = dummy_model.call(input_ids, past_key_values) |
| tf.debugging.assert_equal(output[0], input_ids) |
| tf.debugging.assert_equal(output[1], past_key_values) |
| self.assertFalse(output[2]) |
| self.assertFalse(output[3]) |
| self.assertFalse(output[4]) |
|
|
| |
| output = dummy_model.call(input_ids={"input_ids": input_ids, "past_key_values": past_key_values}) |
| tf.debugging.assert_equal(output[0], input_ids) |
| tf.debugging.assert_equal(output[1], past_key_values) |
| self.assertFalse(output[2]) |
| self.assertFalse(output[3]) |
| self.assertFalse(output[4]) |
|
|
| |
| output = dummy_model.call( |
| input_ids=input_ids, past_key_values=past_key_values, output_attentions=False, return_dict=True |
| ) |
| tf.debugging.assert_equal(output[0], input_ids) |
| tf.debugging.assert_equal(output[1], past_key_values) |
| self.assertFalse(output[2]) |
| self.assertFalse(output[3]) |
| self.assertTrue(output[4]) |
|
|
| |
| with self.assertRaises(ValueError): |
| output = dummy_model.call(input_ids=input_ids, past_key_values=past_key_values, foo="bar") |
|
|
| |
| |
| output = dummy_model.foo(pixel_values=pixel_values) |
| tf.debugging.assert_equal(output[0], pixel_values) |
| self.assertFalse(output[1]) |
| self.assertFalse(output[2]) |
| self.assertFalse(output[3]) |
|
|
| |
| def test_xla_stable_softmax(self): |
| large_penalty = -1e9 |
| n_tokens = 10 |
| batch_size = 8 |
|
|
| def masked_softmax(x, boolean_mask): |
| numerical_mask = (1.0 - tf.cast(boolean_mask, dtype=tf.float32)) * large_penalty |
| masked_x = x + numerical_mask |
| return stable_softmax(masked_x) |
|
|
| xla_masked_softmax = tf.function(masked_softmax, jit_compile=True) |
| xla_stable_softmax = tf.function(stable_softmax, jit_compile=True) |
| x = tf.random.normal((batch_size, n_tokens)) |
|
|
| |
| masked_tokens = random.randint(0, n_tokens) |
| boolean_mask = tf.convert_to_tensor([[1] * (n_tokens - masked_tokens) + [0] * masked_tokens], dtype=tf.int32) |
|
|
| |
| numerical_mask = (1.0 - tf.cast(boolean_mask, dtype=tf.float32)) * large_penalty |
| masked_x = x + numerical_mask |
| xla_out = xla_stable_softmax(masked_x) |
| out = stable_softmax(masked_x) |
| assert tf.experimental.numpy.allclose(xla_out, out) |
|
|
| |
| unstable_out = tf.nn.softmax(masked_x) |
| assert tf.experimental.numpy.allclose(unstable_out, out) |
|
|
| |
| xla_out = xla_masked_softmax(x, boolean_mask) |
| out = masked_softmax(x, boolean_mask) |
| assert tf.experimental.numpy.allclose(xla_out, out) |
|
|
| def test_checkpoint_sharding_from_hub(self): |
| model = TFBertModel.from_pretrained("ArthurZ/tiny-random-bert-sharded") |
| |
| ref_model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
| for p1, p2 in zip(model.weights, ref_model.weights): |
| assert np.allclose(p1.numpy(), p2.numpy()) |
|
|
| def test_sharded_checkpoint_with_prefix(self): |
| model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert", load_weight_prefix="a/b") |
| sharded_model = TFBertModel.from_pretrained("ArthurZ/tiny-random-bert-sharded", load_weight_prefix="a/b") |
| for p1, p2 in zip(model.weights, sharded_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
| self.assertTrue(p1.name.startswith("a/b/")) |
| self.assertTrue(p2.name.startswith("a/b/")) |
|
|
| def test_sharded_checkpoint_transfer(self): |
| |
| TFBertForSequenceClassification.from_pretrained("ArthurZ/tiny-random-bert-sharded") |
|
|
| def test_shard_checkpoint(self): |
| |
| model = keras.Sequential( |
| [ |
| keras.layers.Dense(200, use_bias=False), |
| keras.layers.Dense(200, use_bias=False), |
| keras.layers.Dense(100, use_bias=False), |
| keras.layers.Dense(50, use_bias=False), |
| ] |
| ) |
| inputs = tf.zeros((1, 100), dtype=tf.float32) |
| model(inputs) |
| weights = model.weights |
| weights_dict = {w.name: w for w in weights} |
| with self.subTest("No shard when max size is bigger than model size"): |
| shards, index = tf_shard_checkpoint(weights) |
| self.assertIsNone(index) |
| self.assertDictEqual(shards, {TF2_WEIGHTS_NAME: weights}) |
|
|
| with self.subTest("Test sharding, no weights bigger than max size"): |
| shards, index = tf_shard_checkpoint(weights, max_shard_size="300kB") |
| |
| self.assertDictEqual( |
| index, |
| { |
| "metadata": {"total_size": 340000}, |
| "weight_map": { |
| "dense/kernel:0": "tf_model-00001-of-00002.h5", |
| "dense_1/kernel:0": "tf_model-00001-of-00002.h5", |
| "dense_2/kernel:0": "tf_model-00002-of-00002.h5", |
| "dense_3/kernel:0": "tf_model-00002-of-00002.h5", |
| }, |
| }, |
| ) |
|
|
| shard1 = [weights_dict["dense/kernel:0"], weights_dict["dense_1/kernel:0"]] |
| shard2 = [weights_dict["dense_2/kernel:0"], weights_dict["dense_3/kernel:0"]] |
| self.assertDictEqual(shards, {"tf_model-00001-of-00002.h5": shard1, "tf_model-00002-of-00002.h5": shard2}) |
|
|
| with self.subTest("Test sharding with weights bigger than max size"): |
| shards, index = tf_shard_checkpoint(weights, max_shard_size="100kB") |
| |
| self.assertDictEqual( |
| index, |
| { |
| "metadata": {"total_size": 340000}, |
| "weight_map": { |
| "dense/kernel:0": "tf_model-00001-of-00003.h5", |
| "dense_1/kernel:0": "tf_model-00002-of-00003.h5", |
| "dense_2/kernel:0": "tf_model-00003-of-00003.h5", |
| "dense_3/kernel:0": "tf_model-00003-of-00003.h5", |
| }, |
| }, |
| ) |
|
|
| shard1 = [weights_dict["dense/kernel:0"]] |
| shard2 = [weights_dict["dense_1/kernel:0"]] |
| shard3 = [weights_dict["dense_2/kernel:0"], weights_dict["dense_3/kernel:0"]] |
| self.assertDictEqual( |
| shards, |
| { |
| "tf_model-00001-of-00003.h5": shard1, |
| "tf_model-00002-of-00003.h5": shard2, |
| "tf_model-00003-of-00003.h5": shard3, |
| }, |
| ) |
|
|
| @slow |
| def test_special_layer_name_sharding(self): |
| retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True) |
| model = TFRagModel.from_pretrained("facebook/rag-token-nq", retriever=retriever) |
|
|
| with tempfile.TemporaryDirectory() as tmp_dir: |
| for max_size in ["150kB", "150kiB", "200kB", "200kiB"]: |
| model.save_pretrained(tmp_dir, max_shard_size=max_size) |
| ref_model = TFRagModel.from_pretrained(tmp_dir, retriever=retriever) |
| for p1, p2 in zip(model.weights, ref_model.weights): |
| assert np.allclose(p1.numpy(), p2.numpy()) |
|
|
| @require_safetensors |
| def test_checkpoint_sharding_local(self): |
| model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
|
|
| with tempfile.TemporaryDirectory() as tmp_dir: |
| |
| for max_size in ["150kB", "150kiB", "200kB", "200kiB"]: |
| model.save_pretrained(tmp_dir, max_shard_size=max_size) |
|
|
| |
| shard_to_size = {} |
| for shard in os.listdir(tmp_dir): |
| if shard.endswith(".h5"): |
| shard_file = os.path.join(tmp_dir, shard) |
| shard_to_size[shard_file] = os.path.getsize(shard_file) |
|
|
| index_file = os.path.join(tmp_dir, TF2_WEIGHTS_INDEX_NAME) |
| |
| self.assertTrue(os.path.isfile(index_file)) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, TF2_WEIGHTS_NAME))) |
|
|
| |
| for shard_file, size in shard_to_size.items(): |
| if max_size.endswith("kiB"): |
| max_size_int = int(max_size[:-3]) * 2**10 |
| else: |
| max_size_int = int(max_size[:-2]) * 10**3 |
| |
| |
| if size >= max_size_int + 50000: |
| with h5py.File(shard_file, "r") as state_file: |
| self.assertEqual(len(state_file), 1) |
|
|
| |
| with open(index_file, encoding="utf-8") as f: |
| index = json.loads(f.read()) |
|
|
| all_shards = set(index["weight_map"].values()) |
| shards_found = {f for f in os.listdir(tmp_dir) if f.endswith(".h5")} |
| self.assertSetEqual(all_shards, shards_found) |
|
|
| |
| new_model = TFBertModel.from_pretrained(tmp_dir) |
|
|
| model.build_in_name_scope() |
| new_model.build_in_name_scope() |
|
|
| for p1, p2 in zip(model.weights, new_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| def test_safetensors_checkpoint_sharding_local(self): |
| model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
|
|
| with tempfile.TemporaryDirectory() as tmp_dir: |
| |
| for max_size in ["150kB", "150kiB", "200kB", "200kiB"]: |
| model.save_pretrained(tmp_dir, max_shard_size=max_size, safe_serialization=True) |
|
|
| |
| shard_to_size = {} |
| for shard in os.listdir(tmp_dir): |
| if shard.endswith(".h5"): |
| shard_file = os.path.join(tmp_dir, shard) |
| shard_to_size[shard_file] = os.path.getsize(shard_file) |
|
|
| index_file = os.path.join(tmp_dir, SAFE_WEIGHTS_INDEX_NAME) |
| |
| self.assertTrue(os.path.isfile(index_file)) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, SAFE_WEIGHTS_NAME))) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, TF2_WEIGHTS_NAME))) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, TF2_WEIGHTS_INDEX_NAME))) |
|
|
| |
| with open(index_file, encoding="utf-8") as f: |
| index = json.loads(f.read()) |
|
|
| all_shards = set(index["weight_map"].values()) |
| shards_found = {f for f in os.listdir(tmp_dir) if f.endswith(".safetensors")} |
| self.assertSetEqual(all_shards, shards_found) |
|
|
| |
| new_model = TFBertModel.from_pretrained(tmp_dir) |
|
|
| model.build_in_name_scope() |
| new_model.build_in_name_scope() |
|
|
| for p1, p2 in zip(model.weights, new_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| @slow |
| def test_save_pretrained_signatures(self): |
| model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
|
|
| |
| |
| @tf.function( |
| input_signature=[ |
| [ |
| tf.TensorSpec([None, None], tf.int32, name="input_ids"), |
| tf.TensorSpec([None, None], tf.int32, name="token_type_ids"), |
| tf.TensorSpec([None, None], tf.int32, name="attention_mask"), |
| ] |
| ] |
| ) |
| def serving_fn(input): |
| return model(input) |
|
|
| |
| with tempfile.TemporaryDirectory() as tmp_dir: |
| model.save_pretrained(tmp_dir, saved_model=True, signatures=None) |
| model_loaded = keras.models.load_model(f"{tmp_dir}/saved_model/1") |
| self.assertTrue("serving_default" in list(model_loaded.signatures.keys())) |
|
|
| |
| with tempfile.TemporaryDirectory() as tmp_dir: |
| model.save_pretrained(tmp_dir, saved_model=True, signatures={"custom_signature": serving_fn}) |
| model_loaded = keras.models.load_model(f"{tmp_dir}/saved_model/1") |
| self.assertTrue("custom_signature" in list(model_loaded.signatures.keys())) |
|
|
| |
| with tempfile.TemporaryDirectory() as tmp_dir: |
| model.save_pretrained( |
| tmp_dir, |
| saved_model=True, |
| signatures={"custom_signature_1": serving_fn, "custom_signature_2": serving_fn}, |
| ) |
| model_loaded = keras.models.load_model(f"{tmp_dir}/saved_model/1") |
| self.assertTrue("custom_signature_1" in list(model_loaded.signatures.keys())) |
| self.assertTrue("custom_signature_2" in list(model_loaded.signatures.keys())) |
|
|
| @require_safetensors |
| def test_safetensors_save_and_load(self): |
| model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
| with tempfile.TemporaryDirectory() as tmp_dir: |
| model.save_pretrained(tmp_dir, safe_serialization=True) |
| |
| self.assertTrue(os.path.isfile(os.path.join(tmp_dir, SAFE_WEIGHTS_NAME))) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, SAFE_WEIGHTS_INDEX_NAME))) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, TF2_WEIGHTS_NAME))) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, TF2_WEIGHTS_INDEX_NAME))) |
|
|
| new_model = TFBertModel.from_pretrained(tmp_dir) |
|
|
| |
| for p1, p2 in zip(model.weights, new_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| @require_safetensors |
| def test_safetensors_sharded_save_and_load(self): |
| model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
| with tempfile.TemporaryDirectory() as tmp_dir: |
| model.save_pretrained(tmp_dir, safe_serialization=True, max_shard_size="150kB") |
| |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, SAFE_WEIGHTS_NAME))) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, TF2_WEIGHTS_NAME))) |
| self.assertTrue(os.path.isfile(os.path.join(tmp_dir, SAFE_WEIGHTS_INDEX_NAME))) |
| self.assertFalse(os.path.isfile(os.path.join(tmp_dir, TF2_WEIGHTS_INDEX_NAME))) |
|
|
| new_model = TFBertModel.from_pretrained(tmp_dir) |
|
|
| |
| for p1, p2 in zip(model.weights, new_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| @require_safetensors |
| def test_safetensors_load_from_hub(self): |
| tf_model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert") |
|
|
| |
| safetensors_model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert-safetensors-tf") |
|
|
| |
| for p1, p2 in zip(safetensors_model.weights, tf_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| |
| safetensors_model = TFBertModel.from_pretrained("hf-internal-testing/tiny-random-bert-safetensors") |
|
|
| |
| for p1, p2 in zip(safetensors_model.weights, tf_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| @require_safetensors |
| def test_safetensors_tf_from_tf(self): |
| model = TFBertModel.from_pretrained("hf-internal-testing/tiny-bert-tf-only") |
|
|
| with tempfile.TemporaryDirectory() as tmp_dir: |
| model.save_pretrained(tmp_dir, safe_serialization=True) |
| new_model = TFBertModel.from_pretrained(tmp_dir) |
|
|
| for p1, p2 in zip(model.weights, new_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| @require_safetensors |
| def test_safetensors_tf_from_sharded_h5_with_sharded_safetensors_local(self): |
| with tempfile.TemporaryDirectory() as tmp_dir: |
| path = snapshot_download("hf-internal-testing/tiny-bert-tf-safetensors-h5-sharded", cache_dir=tmp_dir) |
|
|
| |
| TFBertModel.from_pretrained(path) |
|
|
| @require_safetensors |
| def test_safetensors_tf_from_sharded_h5_with_sharded_safetensors_hub(self): |
| |
| TFBertModel.from_pretrained("hf-internal-testing/tiny-bert-tf-safetensors-h5-sharded", use_safetensors=True) |
| |
| TFBertModel.from_pretrained("hf-internal-testing/tiny-bert-tf-safetensors-h5-sharded", use_safetensors=False) |
|
|
| @require_safetensors |
| def test_safetensors_load_from_local(self): |
| """ |
| This test checks that we can load safetensors from a checkpoint that only has those on the Hub |
| """ |
| with tempfile.TemporaryDirectory() as tmp: |
| location = snapshot_download("hf-internal-testing/tiny-bert-tf-only", cache_dir=tmp) |
| tf_model = TFBertModel.from_pretrained(location) |
|
|
| with tempfile.TemporaryDirectory() as tmp: |
| location = snapshot_download("hf-internal-testing/tiny-bert-tf-safetensors-only", cache_dir=tmp) |
| safetensors_model = TFBertModel.from_pretrained(location) |
|
|
| for p1, p2 in zip(tf_model.weights, safetensors_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| @require_safetensors |
| def test_safetensors_load_from_hub_from_safetensors_pt(self): |
| """ |
| This test checks that we can load safetensors from a checkpoint that only has those on the Hub. |
| saved in the "pt" format. |
| """ |
| tf_model = TFBertModel.from_pretrained("hf-internal-testing/tiny-bert-h5") |
|
|
| |
| safetensors_model = TFBertModel.from_pretrained("hf-internal-testing/tiny-bert-pt-safetensors") |
| for p1, p2 in zip(tf_model.weights, safetensors_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| @require_safetensors |
| def test_safetensors_load_from_local_from_safetensors_pt(self): |
| """ |
| This test checks that we can load safetensors from a local checkpoint that only has those |
| saved in the "pt" format. |
| """ |
| with tempfile.TemporaryDirectory() as tmp: |
| location = snapshot_download("hf-internal-testing/tiny-bert-h5", cache_dir=tmp) |
| tf_model = TFBertModel.from_pretrained(location) |
|
|
| |
| with tempfile.TemporaryDirectory() as tmp: |
| location = snapshot_download("hf-internal-testing/tiny-bert-pt-safetensors", cache_dir=tmp) |
| safetensors_model = TFBertModel.from_pretrained(location) |
|
|
| for p1, p2 in zip(tf_model.weights, safetensors_model.weights): |
| self.assertTrue(np.allclose(p1.numpy(), p2.numpy())) |
|
|
| @require_safetensors |
| def test_safetensors_load_from_hub_h5_before_safetensors(self): |
| """ |
| This test checks that we'll first download h5 weights before safetensors |
| The safetensors file on that repo is a pt safetensors and therefore cannot be loaded without PyTorch |
| """ |
| TFBertModel.from_pretrained("hf-internal-testing/tiny-bert-pt-safetensors-msgpack") |
|
|
| @require_safetensors |
| def test_safetensors_load_from_local_h5_before_safetensors(self): |
| """ |
| This test checks that we'll first download h5 weights before safetensors |
| The safetensors file on that repo is a pt safetensors and therefore cannot be loaded without PyTorch |
| """ |
| with tempfile.TemporaryDirectory() as tmp: |
| location = snapshot_download("hf-internal-testing/tiny-bert-pt-safetensors-msgpack", cache_dir=tmp) |
| TFBertModel.from_pretrained(location) |
|
|
|
|
| @require_tf |
| @is_staging_test |
| class TFModelPushToHubTester(unittest.TestCase): |
| @classmethod |
| def setUpClass(cls): |
| cls._token = TOKEN |
| HfFolder.save_token(TOKEN) |
|
|
| def test_push_to_hub(self): |
| with TemporaryHubRepo(token=self._token) as tmp_repo: |
| config = BertConfig( |
| vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37 |
| ) |
| model = TFBertModel(config) |
| |
| model.build_in_name_scope() |
|
|
| logging.set_verbosity_info() |
| logger = logging.get_logger("transformers.utils.hub") |
| with CaptureLogger(logger) as cl: |
| model.push_to_hub(tmp_repo.repo_id, token=self._token) |
| logging.set_verbosity_warning() |
| |
| self.assertIn("Uploading the following files to __DUMMY_TRANSFORMERS_USER__/test-model-tf", cl.out) |
|
|
| new_model = TFBertModel.from_pretrained(tmp_repo.repo_id) |
| models_equal = True |
| for p1, p2 in zip(model.weights, new_model.weights): |
| if not tf.math.reduce_all(p1 == p2): |
| models_equal = False |
| break |
| self.assertTrue(models_equal) |
|
|
| def test_push_to_hub_via_save_pretrained(self): |
| with TemporaryHubRepo(token=self._token) as tmp_repo: |
| config = BertConfig( |
| vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37 |
| ) |
| model = TFBertModel(config) |
| |
| model.build_in_name_scope() |
|
|
| |
| with tempfile.TemporaryDirectory() as tmp_dir: |
| model.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token) |
|
|
| new_model = TFBertModel.from_pretrained(tmp_repo.repo_id) |
| models_equal = True |
| for p1, p2 in zip(model.weights, new_model.weights): |
| if not tf.math.reduce_all(p1 == p2): |
| models_equal = False |
| break |
| self.assertTrue(models_equal) |
|
|
| def test_push_to_hub_in_organization(self): |
| with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo: |
| config = BertConfig( |
| vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37 |
| ) |
| model = TFBertModel(config) |
| |
| model.build_in_name_scope() |
|
|
| model.push_to_hub(tmp_repo.repo_id, token=self._token) |
|
|
| new_model = TFBertModel.from_pretrained(tmp_repo.repo_id) |
| models_equal = True |
| for p1, p2 in zip(model.weights, new_model.weights): |
| if not tf.math.reduce_all(p1 == p2): |
| models_equal = False |
| break |
| self.assertTrue(models_equal) |
|
|
| def test_push_to_hub_in_organization_via_save_pretrained(self): |
| with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo: |
| config = BertConfig( |
| vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37 |
| ) |
| model = TFBertModel(config) |
| |
| model.build_in_name_scope() |
|
|
| |
| with tempfile.TemporaryDirectory() as tmp_dir: |
| model.save_pretrained(tmp_dir, push_to_hub=True, token=self._token, repo_id=tmp_repo.repo_id) |
|
|
| new_model = TFBertModel.from_pretrained(tmp_repo.repo_id) |
| models_equal = True |
| for p1, p2 in zip(model.weights, new_model.weights): |
| if not tf.math.reduce_all(p1 == p2): |
| models_equal = False |
| break |
| self.assertTrue(models_equal) |
|
|