├── tools ├── __init__.py ├── checkpoint_conversion │ ├── __init__.py │ ├── requirements.txt │ └── checkpoint_conversion_utils.py ├── sentencepiece_testing │ ├── __init__.py │ ├── create_sentence_piece_tokenizer_proto.py │ ├── create_no_special_token_proto.py │ ├── create_llama_test_proto.py │ ├── create_mistral_test_proto.py │ ├── create_gemma_test_proto.py │ ├── create_t5_test_proto.py │ ├── utils.py │ ├── create_f_net_test_proto.py │ ├── create_albert_test_proto.py │ ├── create_deberta_v3_test_proto.py │ ├── create_xlm_roberta_test_proto.py │ └── create_phi3_test_proto.py ├── pretrained_tokenizers │ ├── word_piece_cleaning_script.py │ ├── README.md │ └── word_piece_training_script.py ├── count_preset_params.py └── quantize_checkpoints.py ├── keras_hub ├── src │ ├── __init__.py │ ├── layers │ │ ├── __init__.py │ │ ├── modeling │ │ │ ├── __init__.py │ │ │ ├── token_and_position_embedding_test.py │ │ │ └── transformer_layer_utils_test.py │ │ └── preprocessing │ │ │ ├── __init__.py │ │ │ └── preprocessing_layer.py │ ├── metrics │ │ └── __init__.py │ ├── models │ │ ├── __init__.py │ │ ├── clip │ │ │ └── __init__.py │ │ ├── xlnet │ │ │ ├── __init__.py │ │ │ └── xlnet_backbone_test.py │ │ ├── gpt_neo_x │ │ │ ├── __init__.py │ │ │ ├── gpt_neo_x_backbone_test.py │ │ │ └── gpt_neo_x_tokenizer_test.py │ │ ├── mobilenet │ │ │ ├── __init__.py │ │ │ ├── mobilenet_image_classifier.py │ │ │ ├── mobilenet_backbone_test.py │ │ │ └── mobilenet_image_classifier_test.py │ │ ├── retinanet │ │ │ ├── __init__.py │ │ │ └── non_max_supression_test.py │ │ ├── vit_det │ │ │ ├── __init__.py │ │ │ └── vit_det_backbone_test.py │ │ ├── csp_darknet │ │ │ ├── __init__.py │ │ │ ├── csp_darknet_image_classifier.py │ │ │ ├── csp_darknet_backbone_test.py │ │ │ └── csp_darknet_image_classifier_test.py │ │ ├── efficientnet │ │ │ ├── __init__.py │ │ │ ├── mbconv_test.py │ │ │ └── fusedmbconv_test.py │ │ ├── vae │ │ │ ├── __init__.py │ │ │ └── vae_backbone_test.py │ │ ├── t5 │ │ │ ├── __init__.py │ │ │ ├── t5_layer_norm.py │ │ │ └── t5_tokenizer_test.py │ │ ├── opt │ │ │ ├── __init__.py │ │ │ ├── opt_tokenizer_test.py │ │ │ └── opt_backbone_test.py │ │ ├── sam │ │ │ ├── __init__.py │ │ │ ├── sam_image_converter.py │ │ │ ├── sam_image_segmenter_preprocessor.py │ │ │ ├── sam_presets.py │ │ │ └── sam_mask_decoder_test.py │ │ ├── vgg │ │ │ ├── __init__.py │ │ │ ├── vgg_image_converter.py │ │ │ ├── vgg_image_classifier_preprocessor.py │ │ │ ├── vgg_backbone_test.py │ │ │ ├── vgg_image_classifier_test.py │ │ │ └── vgg_presets.py │ │ ├── bart │ │ │ ├── __init__.py │ │ │ └── bart_tokenizer_test.py │ │ ├── bert │ │ │ └── __init__.py │ │ ├── gpt2 │ │ │ ├── __init__.py │ │ │ └── gpt2_tokenizer_test.py │ │ ├── phi3 │ │ │ ├── __init__.py │ │ │ ├── phi3_layernorm.py │ │ │ └── phi3_presets.py │ │ ├── f_net │ │ │ ├── __init__.py │ │ │ ├── f_net_presets.py │ │ │ └── f_net_tokenizer_test.py │ │ ├── bloom │ │ │ ├── __init__.py │ │ │ └── bloom_tokenizer_test.py │ │ ├── gemma │ │ │ ├── __init__.py │ │ │ ├── rms_normalization.py │ │ │ └── gemma_tokenizer_test.py │ │ ├── llama │ │ │ ├── __init__.py │ │ │ ├── llama_layernorm.py │ │ │ └── llama_tokenizer_test.py │ │ ├── albert │ │ │ ├── __init__.py │ │ │ └── albert_tokenizer_test.py │ │ ├── falcon │ │ │ ├── __init__.py │ │ │ ├── falcon_presets.py │ │ │ ├── falcon_backbone_test.py │ │ │ └── falcon_tokenizer_test.py │ │ ├── llama3 │ │ │ ├── __init__.py │ │ │ ├── llama3_causal_lm.py │ │ │ └── llama3_tokenizer_test.py │ │ ├── resnet │ │ │ ├── __init__.py │ │ │ ├── resnet_image_converter.py │ │ │ ├── resnet_image_classifier.py │ │ │ └── resnet_image_classifier_preprocessor.py │ │ ├── electra │ │ │ └── __init__.py │ │ ├── mistral │ │ │ ├── __init__.py │ │ │ ├── mistral_layer_norm.py │ │ │ ├── mistral_presets.py │ │ │ └── mistral_tokenizer_test.py │ │ ├── roberta │ │ │ ├── __init__.py │ │ │ └── roberta_presets.py │ │ ├── whisper │ │ │ ├── __init__.py │ │ │ └── whisper_audio_converter_test.py │ │ ├── densenet │ │ │ ├── __init__.py │ │ │ ├── densenet_image_converter.py │ │ │ ├── densenet_image_classifier.py │ │ │ ├── densenet_image_classifier_preprocessor.py │ │ │ ├── densenet_backbone_test.py │ │ │ └── densenet_presets.py │ │ ├── deberta_v3 │ │ │ └── __init__.py │ │ ├── deeplab_v3 │ │ │ ├── __init__.py │ │ │ ├── deeplab_v3_image_converter.py │ │ │ ├── deeplab_v3_image_segmeter_preprocessor.py │ │ │ └── deeplab_v3_presets.py │ │ ├── pali_gemma │ │ │ ├── __init__.py │ │ │ ├── pali_gemma_image_converter.py │ │ │ └── pali_gemma_vit_test.py │ │ ├── mit │ │ │ ├── __init__.py │ │ │ ├── mit_image_converter.py │ │ │ ├── mit_image_classifier.py │ │ │ ├── mit_image_classifier_preprocessor.py │ │ │ ├── mit_backbone_test.py │ │ │ ├── mix_transformer_backbone_test.py │ │ │ └── mit_image_classifier_test.py │ │ ├── distil_bert │ │ │ ├── __init__.py │ │ │ └── distil_bert_presets.py │ │ ├── xlm_roberta │ │ │ ├── __init__.py │ │ │ ├── xlm_roberta_presets.py │ │ │ └── xlm_roberta_tokenizer_test.py │ │ ├── segformer │ │ │ ├── __init__.py │ │ │ ├── segformer_image_converter.py │ │ │ └── segformer_image_segmenter_preprocessor.py │ │ ├── stable_diffusion_3 │ │ │ ├── __init__.py │ │ │ └── stable_diffusion_3_presets.py │ │ ├── masked_lm_preprocessor_test.py │ │ ├── seq_2_seq_lm_preprocessor_test.py │ │ ├── causal_lm_preprocessor_test.py │ │ ├── seq_2_seq_lm.py │ │ └── text_classifier_preprocessor_test.py │ ├── tests │ │ ├── __init__.py │ │ ├── doc_tests │ │ │ └── __init__.py │ │ └── test_data │ │ │ ├── test_image.jpg │ │ │ ├── t5_test_vocab.spm │ │ │ ├── albert_test_vocab.spm │ │ │ ├── f_net_test_vocab.spm │ │ │ ├── gemma_test_vocab.spm │ │ │ ├── llama_test_vocab.spm │ │ │ ├── phi3_test_vocab.spm │ │ │ ├── mistral_test_vocab.spm │ │ │ ├── deberta_v3_test_vocab.spm │ │ │ ├── no_special_token_vocab.spm │ │ │ ├── tokenizer_test_vocab.spm │ │ │ └── xlm_roberta_test_vocab.spm │ ├── utils │ │ ├── __init__.py │ │ ├── timm │ │ │ ├── __init__.py │ │ │ ├── convert_densenet_test.py │ │ │ └── convert_resnet_test.py │ │ ├── imagenet │ │ │ ├── __init__.py │ │ │ └── imagenet_utils_test.py │ │ ├── transformers │ │ │ ├── __init__.py │ │ │ ├── convert_gpt2_test.py │ │ │ ├── convert_bart_test.py │ │ │ ├── convert_llama3_test.py │ │ │ ├── convert_mistral_test.py │ │ │ ├── convert_bert_test.py │ │ │ ├── convert_albert_test.py │ │ │ ├── convert_gemma_test.py │ │ │ ├── convert_distilbert_test.py │ │ │ └── convert_pali_gemma_test.py │ │ ├── python_utils.py │ │ ├── python_utils_test.py │ │ └── keras_utils_test.py │ ├── bounding_box │ │ ├── __init__.py │ │ ├── to_dense_test.py │ │ └── validate_format_test.py │ ├── samplers │ │ ├── __init__.py │ │ ├── greedy_sampler.py │ │ ├── serialization_test.py │ │ └── random_sampler.py │ ├── tokenizers │ │ └── __init__.py │ ├── version_utils.py │ └── api_export.py ├── api │ ├── utils │ │ └── __init__.py │ ├── metrics │ │ └── __init__.py │ ├── __init__.py │ ├── samplers │ │ └── __init__.py │ └── bounding_box │ │ └── __init__.py └── __init__.py ├── integration_tests ├── __init__.py ├── import_test.py ├── no_tensorflow_test.py └── basic_usage_test.py ├── .kokoro ├── README.md └── github │ └── ubuntu │ └── gpu │ ├── jax │ ├── continuous.cfg │ └── presubmit.cfg │ ├── torch │ ├── presubmit.cfg │ └── continuous.cfg │ ├── tensorflow │ ├── presubmit.cfg │ └── continuous.cfg │ └── build.sh ├── .devcontainer ├── Dockerfile ├── setup.sh └── devcontainer.json ├── .github ├── assets │ └── release_screenshot.png ├── workflows │ ├── auto-assignment.yml │ ├── labeler.yaml │ ├── scripts │ │ └── auto-assignment.js │ ├── publish-to-pypi.yml │ └── nightly.yml ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── dependabot.yml ├── CODE_OF_CONDUCT.md ├── .vscode └── settings.json ├── pyproject.toml ├── shell ├── format.sh ├── api_gen.sh └── lint.sh ├── requirements.txt ├── .gitignore ├── keras_nlp ├── README.md ├── keras_nlp │ └── __init__.py └── setup.py ├── requirements-common.txt ├── requirements-tensorflow-cuda.txt ├── requirements-torch-cuda.txt ├── requirements-jax-cuda.txt ├── SECURITY.md ├── setup.cfg └── benchmarks └── README.md /tools/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /integration_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/layers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/metrics/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/bounding_box/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/clip/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/xlnet/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/samplers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/tokenizers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/utils/timm/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/layers/modeling/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/gpt_neo_x/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/mobilenet/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/retinanet/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/vit_det/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/tests/doc_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/utils/imagenet/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tools/checkpoint_conversion/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/layers/preprocessing/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/csp_darknet/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/models/efficientnet/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /keras_hub/src/utils/transformers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tools/checkpoint_conversion/requirements.txt: -------------------------------------------------------------------------------- 1 | torch 2 | transformers 3 | -------------------------------------------------------------------------------- /.kokoro/README.md: -------------------------------------------------------------------------------- 1 | CI to run on PR and merge to Master and for continous build. -------------------------------------------------------------------------------- /keras_hub/src/models/vae/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.vae.vae_backbone import VAEBackbone 2 | -------------------------------------------------------------------------------- /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/vscode/devcontainers/python:3.9 2 | COPY setup.sh /setup.sh 3 | -------------------------------------------------------------------------------- /.github/assets/release_screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/.github/assets/release_screenshot.png -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/test_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/test_image.jpg -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/t5_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/t5_test_vocab.spm -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | This project follows 4 | [Google's Open Source Community Guidelines](https://opensource.google/conduct/). 5 | -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/albert_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/albert_test_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/f_net_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/f_net_test_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/gemma_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/gemma_test_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/llama_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/llama_test_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/phi3_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/phi3_test_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/mistral_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/mistral_test_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/deberta_v3_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/deberta_v3_test_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/no_special_token_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/no_special_token_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/tokenizer_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/tokenizer_test_vocab.spm -------------------------------------------------------------------------------- /keras_hub/src/tests/test_data/xlm_roberta_test_vocab.spm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ushareng/keras-nlp/master/keras_hub/src/tests/test_data/xlm_roberta_test_vocab.spm -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.testing.pytestEnabled": true, 3 | "python.formatting.provider": "black", 4 | "editor.rulers": [ 5 | 80 6 | ] 7 | } -------------------------------------------------------------------------------- /.devcontainer/setup.sh: -------------------------------------------------------------------------------- 1 | pip install --upgrade pip 2 | pip install -r requirements.txt 3 | pip install -e . 4 | echo "sh shell/lint.sh" > .git/hooks/pre-commit 5 | chmod a+x .git/hooks/pre-commit 6 | -------------------------------------------------------------------------------- /integration_tests/import_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import keras_hub 4 | 5 | 6 | class ImportTest(unittest.TestCase): 7 | def test_version(self): 8 | self.assertIsNotNone(keras_hub.__version__) 9 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.black] 2 | line-length = 80 3 | 4 | [tool.isort] 5 | profile = "black" 6 | force_single_line = "True" 7 | known_first_party = ["keras_hub", "tests"] 8 | default_section = "THIRDPARTY" 9 | line_length = 80 10 | -------------------------------------------------------------------------------- /keras_hub/src/utils/python_utils.py: -------------------------------------------------------------------------------- 1 | """Utilities with miscellaneous python extensions.""" 2 | 3 | 4 | class classproperty(property): 5 | """Define a class level property.""" 6 | 7 | def __get__(self, _, owner_cls): 8 | return self.fget(owner_cls) 9 | -------------------------------------------------------------------------------- /shell/format.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | base_dir=$(dirname $(dirname $0)) 4 | targets="${base_dir}" 5 | 6 | isort --sp "${base_dir}/pyproject.toml" ${targets} 7 | black --config "${base_dir}/pyproject.toml" ${targets} 8 | flake8 --config "${base_dir}/setup.cfg" ${targets} 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # Tensorflow. 2 | tensorflow-cpu~=2.17 3 | tensorflow-text~=2.17 4 | 5 | # Torch. 6 | --extra-index-url https://download.pytorch.org/whl/cpu 7 | torch>=2.1.0 8 | torchvision>=0.16.0 9 | 10 | # Jax. 11 | jax[cpu] 12 | 13 | -r requirements-common.txt 14 | -------------------------------------------------------------------------------- /keras_hub/src/models/t5/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.t5.t5_backbone import T5Backbone 2 | from keras_hub.src.models.t5.t5_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, T5Backbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/version_utils.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | 3 | # Unique source of truth for the version number. 4 | __version__ = "0.17.0.dev0" 5 | 6 | 7 | @keras_hub_export("keras_hub.version") 8 | def version(): 9 | return __version__ 10 | -------------------------------------------------------------------------------- /keras_hub/api/utils/__init__.py: -------------------------------------------------------------------------------- 1 | """DO NOT EDIT. 2 | 3 | This file was autogenerated. Do not edit it by hand, 4 | since your modifications would be overwritten. 5 | """ 6 | 7 | from keras_hub.src.utils.imagenet.imagenet_utils import ( 8 | decode_imagenet_predictions, 9 | ) 10 | -------------------------------------------------------------------------------- /keras_hub/src/models/opt/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.opt.opt_backbone import OPTBackbone 2 | from keras_hub.src.models.opt.opt_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, OPTBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/sam/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.sam.sam_backbone import SAMBackbone 2 | from keras_hub.src.models.sam.sam_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, SAMBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/vgg/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone 2 | from keras_hub.src.models.vgg.vgg_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, VGGBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/bart/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.bart.bart_backbone import BartBackbone 2 | from keras_hub.src.models.bart.bart_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, BartBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/bert/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.bert.bert_backbone import BertBackbone 2 | from keras_hub.src.models.bert.bert_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, BertBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/gpt2/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.gpt2.gpt2_backbone import GPT2Backbone 2 | from keras_hub.src.models.gpt2.gpt2_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, GPT2Backbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/phi3/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.phi3.phi3_backbone import Phi3Backbone 2 | from keras_hub.src.models.phi3.phi3_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, Phi3Backbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/f_net/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.f_net.f_net_backbone import FNetBackbone 2 | from keras_hub.src.models.f_net.f_net_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, FNetBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/bloom/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.bloom.bloom_backbone import BloomBackbone 2 | from keras_hub.src.models.bloom.bloom_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, BloomBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/gemma/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.gemma.gemma_backbone import GemmaBackbone 2 | from keras_hub.src.models.gemma.gemma_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, GemmaBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/llama/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.llama.llama_backbone import LlamaBackbone 2 | from keras_hub.src.models.llama.llama_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, LlamaBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/albert/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.albert.albert_backbone import AlbertBackbone 2 | from keras_hub.src.models.albert.albert_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, AlbertBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/falcon/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.falcon.falcon_backbone import FalconBackbone 2 | from keras_hub.src.models.falcon.falcon_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, FalconBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/llama3/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.llama3.llama3_backbone import Llama3Backbone 2 | from keras_hub.src.models.llama3.llama3_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, Llama3Backbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/resnet/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.resnet.resnet_backbone import ResNetBackbone 2 | from keras_hub.src.models.resnet.resnet_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, ResNetBackbone) 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Python temp files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # Vim temp files 7 | *.swp 8 | *.swo 9 | 10 | *.egg-info/ 11 | dist/ 12 | 13 | .coverage 14 | .coverage.* 15 | 16 | # Windows-specific build files 17 | build/ 18 | 19 | # Pycharm files 20 | .idea/ 21 | 22 | venv/ 23 | 24 | -------------------------------------------------------------------------------- /keras_hub/src/models/electra/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.electra.electra_backbone import ElectraBackbone 2 | from keras_hub.src.models.electra.electra_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, ElectraBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/mistral/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.mistral.mistral_backbone import MistralBackbone 2 | from keras_hub.src.models.mistral.mistral_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, MistralBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/roberta/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.roberta.roberta_backbone import RobertaBackbone 2 | from keras_hub.src.models.roberta.roberta_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, RobertaBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/whisper/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.whisper.whisper_backbone import WhisperBackbone 2 | from keras_hub.src.models.whisper.whisper_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, WhisperBackbone) 6 | -------------------------------------------------------------------------------- /keras_hub/src/models/densenet/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone 2 | from keras_hub.src.models.densenet.densenet_presets import backbone_presets 3 | from keras_hub.src.utils.preset_utils import register_presets 4 | 5 | register_presets(backbone_presets, DenseNetBackbone) 6 | -------------------------------------------------------------------------------- /shell/api_gen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -Eeuo pipefail 3 | 4 | base_dir=$(dirname $(dirname $0)) 5 | 6 | echo "Generating api directory with public APIs..." 7 | # Generate API Files 8 | python3 "${base_dir}"/api_gen.py 9 | 10 | echo "Formatting api directory..." 11 | # Format API Files 12 | bash "${base_dir}"/shell/format.sh 13 | -------------------------------------------------------------------------------- /keras_nlp/README.md: -------------------------------------------------------------------------------- 1 | # KerasNLP: Multi-framework NLP Models 2 | 3 | KerasNLP has renamed to KerasHub! Read the announcement 4 | [here](https://github.com/keras-team/keras-nlp/issues/1831). 5 | 6 | This directory contains a shim package for `keras-nlp` so that the old style 7 | `pip install keras-nlp` and `import keras_nlp` continue to work. 8 | -------------------------------------------------------------------------------- /requirements-common.txt: -------------------------------------------------------------------------------- 1 | # Library deps. 2 | dm-tree 3 | regex 4 | rich 5 | kagglehub 6 | # Tooling deps. 7 | astor 8 | packaging 9 | black[jupyter]>=22 10 | flake8 11 | isort 12 | pytest 13 | pytest-cov 14 | build 15 | namex 16 | # Optional deps. 17 | rouge-score 18 | sentencepiece 19 | tensorflow-datasets 20 | safetensors 21 | pillow 22 | -------------------------------------------------------------------------------- /requirements-tensorflow-cuda.txt: -------------------------------------------------------------------------------- 1 | # Tensorflow with cuda support. 2 | tensorflow[and-cuda]~=2.17 3 | tensorflow-text~=2.17 4 | 5 | # Torch cpu-only version. 6 | --extra-index-url https://download.pytorch.org/whl/cpu 7 | torch>=2.1.0 8 | torchvision>=0.16.0 9 | 10 | # Jax cpu-only version. 11 | jax[cpu] 12 | 13 | -r requirements-common.txt 14 | -------------------------------------------------------------------------------- /keras_hub/src/models/deberta_v3/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.deberta_v3.deberta_v3_backbone import ( 2 | DebertaV3Backbone, 3 | ) 4 | from keras_hub.src.models.deberta_v3.deberta_v3_presets import backbone_presets 5 | from keras_hub.src.utils.preset_utils import register_presets 6 | 7 | register_presets(backbone_presets, DebertaV3Backbone) 8 | -------------------------------------------------------------------------------- /keras_hub/src/models/deeplab_v3/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.deeplab_v3.deeplab_v3_backbone import ( 2 | DeepLabV3Backbone, 3 | ) 4 | from keras_hub.src.models.deeplab_v3.deeplab_v3_presets import backbone_presets 5 | from keras_hub.src.utils.preset_utils import register_presets 6 | 7 | register_presets(backbone_presets, DeepLabV3Backbone) 8 | -------------------------------------------------------------------------------- /keras_hub/src/models/pali_gemma/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.pali_gemma.pali_gemma_backbone import ( 2 | PaliGemmaBackbone, 3 | ) 4 | from keras_hub.src.models.pali_gemma.pali_gemma_presets import backbone_presets 5 | from keras_hub.src.utils.preset_utils import register_presets 6 | 7 | register_presets(backbone_presets, PaliGemmaBackbone) 8 | -------------------------------------------------------------------------------- /requirements-torch-cuda.txt: -------------------------------------------------------------------------------- 1 | # Tensorflow cpu-only version. 2 | tensorflow-cpu~=2.17 3 | tensorflow-text~=2.17 4 | 5 | # Torch with cuda support. 6 | --extra-index-url https://download.pytorch.org/whl/cu121 7 | torch==2.4.1+cu121 8 | torchvision==0.19.1+cu121 9 | 10 | # Jax cpu-only version. 11 | jax[cpu] 12 | 13 | -r requirements-common.txt 14 | -------------------------------------------------------------------------------- /keras_hub/src/models/mit/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.mit.mit_backbone import MiTBackbone 2 | from keras_hub.src.models.mit.mit_image_classifier import MiTImageClassifier 3 | from keras_hub.src.models.mit.mit_presets import backbone_presets 4 | from keras_hub.src.utils.preset_utils import register_presets 5 | 6 | register_presets(backbone_presets, MiTBackbone) 7 | -------------------------------------------------------------------------------- /keras_hub/src/models/distil_bert/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.distil_bert.distil_bert_backbone import ( 2 | DistilBertBackbone, 3 | ) 4 | from keras_hub.src.models.distil_bert.distil_bert_presets import ( 5 | backbone_presets, 6 | ) 7 | from keras_hub.src.utils.preset_utils import register_presets 8 | 9 | register_presets(backbone_presets, DistilBertBackbone) 10 | -------------------------------------------------------------------------------- /keras_hub/src/models/mit/mit_image_converter.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.layers.preprocessing.image_converter import ImageConverter 3 | from keras_hub.src.models.mit import MiTBackbone 4 | 5 | 6 | @keras_hub_export("keras_hub.layers.MiTImageConverter") 7 | class MiTImageConverter(ImageConverter): 8 | backbone_cls = MiTBackbone 9 | -------------------------------------------------------------------------------- /keras_hub/src/models/xlm_roberta/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.xlm_roberta.xlm_roberta_backbone import ( 2 | XLMRobertaBackbone, 3 | ) 4 | from keras_hub.src.models.xlm_roberta.xlm_roberta_presets import ( 5 | backbone_presets, 6 | ) 7 | from keras_hub.src.utils.preset_utils import register_presets 8 | 9 | register_presets(backbone_presets, XLMRobertaBackbone) 10 | -------------------------------------------------------------------------------- /keras_hub/src/models/sam/sam_image_converter.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.layers.preprocessing.image_converter import ImageConverter 3 | from keras_hub.src.models.sam.sam_backbone import SAMBackbone 4 | 5 | 6 | @keras_hub_export("keras_hub.layers.SAMImageConverter") 7 | class SAMImageConverter(ImageConverter): 8 | backbone_cls = SAMBackbone 9 | -------------------------------------------------------------------------------- /keras_hub/src/models/vgg/vgg_image_converter.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.layers.preprocessing.image_converter import ImageConverter 3 | from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone 4 | 5 | 6 | @keras_hub_export("keras_hub.layers.VGGImageConverter") 7 | class VGGImageConverter(ImageConverter): 8 | backbone_cls = VGGBackbone 9 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_sentence_piece_tokenizer_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox."], 7 | "tokenizer_test_vocab.spm", 8 | vocab_size=7, 9 | model_type="WORD", 10 | ) 11 | 12 | 13 | if __name__ == "__main__": 14 | main() 15 | -------------------------------------------------------------------------------- /keras_hub/src/models/resnet/resnet_image_converter.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.layers.preprocessing.image_converter import ImageConverter 3 | from keras_hub.src.models.resnet.resnet_backbone import ResNetBackbone 4 | 5 | 6 | @keras_hub_export("keras_hub.layers.ResNetImageConverter") 7 | class ResNetImageConverter(ImageConverter): 8 | backbone_cls = ResNetBackbone 9 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_no_special_token_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["abc"], 7 | "no_special_token_vocab.spm", 8 | vocab_size=5, 9 | pad_id=-1, 10 | eos_id=-1, 11 | bos_id=-1, 12 | ) 13 | 14 | 15 | if __name__ == "__main__": 16 | main() 17 | -------------------------------------------------------------------------------- /keras_hub/src/models/densenet/densenet_image_converter.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.layers.preprocessing.image_converter import ImageConverter 3 | from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone 4 | 5 | 6 | @keras_hub_export("keras_hub.layers.DenseNetImageConverter") 7 | class DenseNetImageConverter(ImageConverter): 8 | backbone_cls = DenseNetBackbone 9 | -------------------------------------------------------------------------------- /keras_hub/src/models/mobilenet/mobilenet_image_classifier.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.image_classifier import ImageClassifier 3 | from keras_hub.src.models.mobilenet.mobilenet_backbone import MobileNetBackbone 4 | 5 | 6 | @keras_hub_export("keras_hub.models.MobileNetImageClassifier") 7 | class MobileNetImageClassifier(ImageClassifier): 8 | backbone_cls = MobileNetBackbone 9 | -------------------------------------------------------------------------------- /keras_hub/src/models/segformer/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.segformer.segformer_backbone import SegFormerBackbone 2 | from keras_hub.src.models.segformer.segformer_image_segmenter import ( 3 | SegFormerImageSegmenter, 4 | ) 5 | from keras_hub.src.models.segformer.segformer_presets import presets 6 | from keras_hub.src.utils.preset_utils import register_presets 7 | 8 | register_presets(presets, SegFormerImageSegmenter) 9 | -------------------------------------------------------------------------------- /keras_hub/src/models/stable_diffusion_3/__init__.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.models.stable_diffusion_3.stable_diffusion_3_backbone import ( 2 | StableDiffusion3Backbone, 3 | ) 4 | from keras_hub.src.models.stable_diffusion_3.stable_diffusion_3_presets import ( 5 | backbone_presets, 6 | ) 7 | from keras_hub.src.utils.preset_utils import register_presets 8 | 9 | register_presets(backbone_presets, StableDiffusion3Backbone) 10 | -------------------------------------------------------------------------------- /keras_hub/src/models/segformer/segformer_image_converter.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.layers.preprocessing.image_converter import ImageConverter 3 | from keras_hub.src.models.segformer.segformer_backbone import SegFormerBackbone 4 | 5 | 6 | @keras_hub_export("keras_hub.layers.SegFormerImageConverter") 7 | class SegFormerImageConverter(ImageConverter): 8 | backbone_cls = SegFormerBackbone 9 | -------------------------------------------------------------------------------- /keras_hub/src/utils/python_utils_test.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.tests.test_case import TestCase 2 | from keras_hub.src.utils.python_utils import classproperty 3 | 4 | 5 | class ClassPropertyTest(TestCase): 6 | def test_class_property(self): 7 | class Foo: 8 | @classproperty 9 | def bar(cls): 10 | return "class property" 11 | 12 | self.assertAllEqual(Foo.bar, "class property") 13 | -------------------------------------------------------------------------------- /keras_hub/api/metrics/__init__.py: -------------------------------------------------------------------------------- 1 | """DO NOT EDIT. 2 | 3 | This file was autogenerated. Do not edit it by hand, 4 | since your modifications would be overwritten. 5 | """ 6 | 7 | from keras_hub.src.metrics.bleu import Bleu 8 | from keras_hub.src.metrics.edit_distance import EditDistance 9 | from keras_hub.src.metrics.perplexity import Perplexity 10 | from keras_hub.src.metrics.rouge_l import RougeL 11 | from keras_hub.src.metrics.rouge_n import RougeN 12 | -------------------------------------------------------------------------------- /keras_hub/src/models/csp_darknet/csp_darknet_image_classifier.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.csp_darknet.csp_darknet_backbone import ( 3 | CSPDarkNetBackbone, 4 | ) 5 | from keras_hub.src.models.image_classifier import ImageClassifier 6 | 7 | 8 | @keras_hub_export("keras_hub.models.CSPDarkNetImageClassifier") 9 | class CSPDarkNetImageClassifier(ImageClassifier): 10 | backbone_cls = CSPDarkNetBackbone 11 | -------------------------------------------------------------------------------- /keras_hub/src/models/deeplab_v3/deeplab_v3_image_converter.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.layers.preprocessing.image_converter import ImageConverter 3 | from keras_hub.src.models.deeplab_v3.deeplab_v3_backbone import ( 4 | DeepLabV3Backbone, 5 | ) 6 | 7 | 8 | @keras_hub_export("keras_hub.layers.DeepLabV3ImageConverter") 9 | class DeepLabV3ImageConverter(ImageConverter): 10 | backbone_cls = DeepLabV3Backbone 11 | -------------------------------------------------------------------------------- /keras_hub/src/models/pali_gemma/pali_gemma_image_converter.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.layers.preprocessing.image_converter import ImageConverter 3 | from keras_hub.src.models.pali_gemma.pali_gemma_backbone import ( 4 | PaliGemmaBackbone, 5 | ) 6 | 7 | 8 | @keras_hub_export("keras_hub.layers.PaliGemmaImageConverter") 9 | class PaliGemmaImageConverter(ImageConverter): 10 | backbone_cls = PaliGemmaBackbone 11 | -------------------------------------------------------------------------------- /requirements-jax-cuda.txt: -------------------------------------------------------------------------------- 1 | # Tensorflow cpu-only version. 2 | tensorflow-cpu~=2.17 3 | tensorflow-text~=2.17 4 | 5 | # Torch cpu-only version. 6 | --extra-index-url https://download.pytorch.org/whl/cpu 7 | torch>=2.1.0 8 | torchvision>=0.16.0 9 | 10 | # Jax with cuda support. 11 | # Keep same version as Keras repo. 12 | --find-links https://storage.googleapis.com/jax-releases/jax_cuda_releases.html 13 | jax[cuda12_pip]==0.4.28 14 | 15 | -r requirements-common.txt 16 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_llama_test_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox", "the earth is round"], 7 | "llama_test_vocab.spm", 8 | vocab_size=10, 9 | model_type="WORD", 10 | pad_id=-1, 11 | unk_id=0, 12 | bos_id=1, 13 | eos_id=2, 14 | ) 15 | 16 | 17 | if __name__ == "__main__": 18 | main() 19 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_mistral_test_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox", "the earth is round"], 7 | "mistral_test_vocab.spm", 8 | vocab_size=10, 9 | model_type="WORD", 10 | pad_id=-1, 11 | unk_id=0, 12 | bos_id=1, 13 | eos_id=2, 14 | ) 15 | 16 | 17 | if __name__ == "__main__": 18 | main() 19 | -------------------------------------------------------------------------------- /.github/workflows/auto-assignment.yml: -------------------------------------------------------------------------------- 1 | name: auto-assignment 2 | on: 3 | issues: 4 | types: 5 | - opened 6 | 7 | permissions: 8 | contents: read 9 | issues: write 10 | pull-requests: write 11 | 12 | jobs: 13 | welcome: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/github-script@v7 18 | with: 19 | script: | 20 | const script = require('./\.github/workflows/scripts/auto-assignment.js') 21 | script({github, context}) 22 | -------------------------------------------------------------------------------- /keras_hub/src/models/mit/mit_image_classifier.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.image_classifier import ImageClassifier 3 | from keras_hub.src.models.mit.mit_backbone import MiTBackbone 4 | from keras_hub.src.models.mit.mit_image_classifier_preprocessor import ( 5 | MiTImageClassifierPreprocessor, 6 | ) 7 | 8 | 9 | @keras_hub_export("keras_hub.models.MiTImageClassifier") 10 | class MiTImageClassifier(ImageClassifier): 11 | backbone_cls = MiTBackbone 12 | preprocessor_cls = MiTImageClassifierPreprocessor 13 | -------------------------------------------------------------------------------- /keras_hub/src/models/mit/mit_image_classifier_preprocessor.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.image_classifier_preprocessor import ( 3 | ImageClassifierPreprocessor, 4 | ) 5 | from keras_hub.src.models.mit.mit_backbone import MiTBackbone 6 | from keras_hub.src.models.mit.mit_image_converter import MiTImageConverter 7 | 8 | 9 | @keras_hub_export("keras_hub.models.MiTImageClassifierPreprocessor") 10 | class MiTImageClassifierPreprocessor(ImageClassifierPreprocessor): 11 | backbone_cls = MiTBackbone 12 | image_converter_cls = MiTImageConverter 13 | -------------------------------------------------------------------------------- /keras_hub/src/models/resnet/resnet_image_classifier.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.image_classifier import ImageClassifier 3 | from keras_hub.src.models.resnet.resnet_backbone import ResNetBackbone 4 | from keras_hub.src.models.resnet.resnet_image_classifier_preprocessor import ( 5 | ResNetImageClassifierPreprocessor, 6 | ) 7 | 8 | 9 | @keras_hub_export("keras_hub.models.ResNetImageClassifier") 10 | class ResNetImageClassifier(ImageClassifier): 11 | backbone_cls = ResNetBackbone 12 | preprocessor_cls = ResNetImageClassifierPreprocessor 13 | -------------------------------------------------------------------------------- /keras_hub/src/models/vgg/vgg_image_classifier_preprocessor.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.image_classifier_preprocessor import ( 3 | ImageClassifierPreprocessor, 4 | ) 5 | from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone 6 | from keras_hub.src.models.vgg.vgg_image_converter import VGGImageConverter 7 | 8 | 9 | @keras_hub_export("keras_hub.models.VGGImageClassifierPreprocessor") 10 | class VGGImageClassifierPreprocessor(ImageClassifierPreprocessor): 11 | backbone_cls = VGGBackbone 12 | image_converter_cls = VGGImageConverter 13 | -------------------------------------------------------------------------------- /keras_hub/api/__init__.py: -------------------------------------------------------------------------------- 1 | """DO NOT EDIT. 2 | 3 | This file was autogenerated. Do not edit it by hand, 4 | since your modifications would be overwritten. 5 | """ 6 | 7 | from keras_hub.api import bounding_box 8 | from keras_hub.api import layers 9 | from keras_hub.api import metrics 10 | from keras_hub.api import models 11 | from keras_hub.api import samplers 12 | from keras_hub.api import tokenizers 13 | from keras_hub.api import utils 14 | from keras_hub.src.utils.preset_utils import upload_preset 15 | from keras_hub.src.version_utils import __version__ 16 | from keras_hub.src.version_utils import version 17 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_gemma_test_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox", "the earth is round"], 7 | "gemma_test_vocab.spm", 8 | vocab_size=11, 9 | model_type="WORD", 10 | pad_id=0, 11 | bos_id=1, 12 | eos_id=2, 13 | unk_id=3, 14 | pad_piece="", 15 | bos_piece="", 16 | eos_piece="", 17 | unk_piece="", 18 | ) 19 | 20 | 21 | if __name__ == "__main__": 22 | main() 23 | -------------------------------------------------------------------------------- /shell/lint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | base_dir=$(dirname $(dirname $0)) 4 | targets="${base_dir}" 5 | 6 | isort --sp "${base_dir}/pyproject.toml" -c ${targets} 7 | if ! [ $? -eq 0 ]; then 8 | echo "Please run \"./shell/format.sh\" to format the code." 9 | exit 1 10 | fi 11 | 12 | flake8 --config "${base_dir}/setup.cfg" ${targets} 13 | if ! [ $? -eq 0 ]; then 14 | echo "Please fix the code style issue." 15 | exit 1 16 | fi 17 | 18 | black --config "${base_dir}/pyproject.toml" --check ${targets} 19 | if ! [ $? -eq 0 ]; then 20 | echo "Please run \"./shell/format.sh\" to format the code." 21 | exit 1 22 | fi 23 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_t5_test_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox", "the earth is round"], 7 | "t5_test_vocab.spm", 8 | vocab_size=11, 9 | model_type="WORD", 10 | bos_id=-1, 11 | pad_id=0, 12 | eos_id=1, 13 | unk_id=2, 14 | pad_piece="", 15 | eos_piece="", 16 | unk_piece="", 17 | user_defined_symbols="[MASK]", 18 | ) 19 | 20 | 21 | if __name__ == "__main__": 22 | main() 23 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/utils.py: -------------------------------------------------------------------------------- 1 | import io 2 | import pathlib 3 | 4 | import sentencepiece 5 | 6 | 7 | def train_sentencepiece(data, filename, *args, **kwargs): 8 | bytes_io = io.BytesIO() 9 | sentencepiece.SentencePieceTrainer.train( 10 | sentence_iterator=iter(data), model_writer=bytes_io, *args, **kwargs 11 | ) 12 | with open( 13 | pathlib.Path(__file__).parent.parent.parent 14 | / "keras_hub" 15 | / "src" 16 | / "tests" 17 | / "test_data" 18 | / filename, 19 | mode="wb", 20 | ) as f: 21 | f.write(bytes_io.getbuffer()) 22 | -------------------------------------------------------------------------------- /keras_hub/src/models/densenet/densenet_image_classifier.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone 3 | from keras_hub.src.models.densenet.densenet_image_classifier_preprocessor import ( 4 | DenseNetImageClassifierPreprocessor, 5 | ) 6 | from keras_hub.src.models.image_classifier import ImageClassifier 7 | 8 | 9 | @keras_hub_export("keras_hub.models.DenseNetImageClassifier") 10 | class DenseNetImageClassifier(ImageClassifier): 11 | backbone_cls = DenseNetBackbone 12 | preprocessor_cls = DenseNetImageClassifierPreprocessor 13 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_f_net_test_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox", "the earth is round"], 7 | "f_net_test_vocab.spm", 8 | vocab_size=12, 9 | model_type="WORD", 10 | pad_id=0, 11 | unk_id=1, 12 | bos_id=2, 13 | eos_id=3, 14 | pad_piece="", 15 | unk_piece="", 16 | bos_piece="[CLS]", 17 | eos_piece="[SEP]", 18 | user_defined_symbols="[MASK]", 19 | ) 20 | 21 | 22 | if __name__ == "__main__": 23 | main() 24 | -------------------------------------------------------------------------------- /keras_hub/src/models/resnet/resnet_image_classifier_preprocessor.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.image_classifier_preprocessor import ( 3 | ImageClassifierPreprocessor, 4 | ) 5 | from keras_hub.src.models.resnet.resnet_backbone import ResNetBackbone 6 | from keras_hub.src.models.resnet.resnet_image_converter import ( 7 | ResNetImageConverter, 8 | ) 9 | 10 | 11 | @keras_hub_export("keras_hub.models.ResNetImageClassifierPreprocessor") 12 | class ResNetImageClassifierPreprocessor(ImageClassifierPreprocessor): 13 | backbone_cls = ResNetBackbone 14 | image_converter_cls = ResNetImageConverter 15 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_albert_test_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox", "the earth is round"], 7 | "albert_test_vocab.spm", 8 | vocab_size=12, 9 | model_type="WORD", 10 | pad_id=0, 11 | unk_id=1, 12 | bos_id=2, 13 | eos_id=3, 14 | pad_piece="", 15 | unk_piece="", 16 | bos_piece="[CLS]", 17 | eos_piece="[SEP]", 18 | user_defined_symbols="[MASK]", 19 | ) 20 | 21 | 22 | if __name__ == "__main__": 23 | main() 24 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_deberta_v3_test_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox", "the earth is round"], 7 | "deberta_v3_test_vocab.spm", 8 | vocab_size=12, 9 | model_type="WORD", 10 | pad_id=0, 11 | bos_id=1, 12 | eos_id=2, 13 | unk_id=3, 14 | pad_piece="[PAD]", 15 | bos_piece="[CLS]", 16 | eos_piece="[SEP]", 17 | unk_piece="[UNK]", 18 | user_defined_symbols="[MASK]", 19 | ) 20 | 21 | 22 | if __name__ == "__main__": 23 | main() 24 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_xlm_roberta_test_proto.py: -------------------------------------------------------------------------------- 1 | from tools.sentencepiece_testing.utils import train_sentencepiece 2 | 3 | 4 | def main(): 5 | train_sentencepiece( 6 | ["the quick brown fox", "the earth is round"], 7 | "xlm_roberta_test_vocab.spm", 8 | vocab_size=12, 9 | model_type="WORD", 10 | pad_id=0, 11 | unk_id=1, 12 | bos_id=2, 13 | eos_id=3, 14 | pad_piece="", 15 | unk_piece="", 16 | bos_piece="[CLS]", 17 | eos_piece="[SEP]", 18 | user_defined_symbols="[MASK]", 19 | ) 20 | 21 | 22 | if __name__ == "__main__": 23 | main() 24 | -------------------------------------------------------------------------------- /keras_hub/src/models/densenet/densenet_image_classifier_preprocessor.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone 3 | from keras_hub.src.models.densenet.densenet_image_converter import ( 4 | DenseNetImageConverter, 5 | ) 6 | from keras_hub.src.models.image_classifier_preprocessor import ( 7 | ImageClassifierPreprocessor, 8 | ) 9 | 10 | 11 | @keras_hub_export("keras_hub.models.DenseNetImageClassifierPreprocessor") 12 | class DenseNetImageClassifierPreprocessor(ImageClassifierPreprocessor): 13 | backbone_cls = DenseNetBackbone 14 | image_converter_cls = DenseNetImageConverter 15 | -------------------------------------------------------------------------------- /keras_hub/src/models/falcon/falcon_presets.py: -------------------------------------------------------------------------------- 1 | """Falcon model preset configurations.""" 2 | 3 | backbone_presets = { 4 | "falcon_refinedweb_1b_en": { 5 | "metadata": { 6 | "description": ( 7 | "24-layer Falcon model (Falcon with 1B parameters), trained on " 8 | "350B tokens of RefinedWeb dataset." 9 | ), 10 | "params": 1311625216, 11 | "official_name": "Falcon", 12 | "path": "falcon", 13 | "model_card": "https://huggingface.co/tiiuae/falcon-rw-1b", 14 | }, 15 | "kaggle_handle": "kaggle://keras/falcon/keras/falcon_refinedweb_1b_en/1", 16 | }, 17 | } 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | 14 | 15 | **To Reproduce** 16 | 19 | 20 | **Expected behavior** 21 | 24 | 25 | **Additional context** 26 | 29 | 30 | **Would you like to help us fix it?** 31 | -------------------------------------------------------------------------------- /keras_hub/src/models/deeplab_v3/deeplab_v3_image_segmeter_preprocessor.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.deeplab_v3.deeplab_v3_backbone import ( 3 | DeepLabV3Backbone, 4 | ) 5 | from keras_hub.src.models.deeplab_v3.deeplab_v3_image_converter import ( 6 | DeepLabV3ImageConverter, 7 | ) 8 | from keras_hub.src.models.image_segmenter_preprocessor import ( 9 | ImageSegmenterPreprocessor, 10 | ) 11 | 12 | 13 | @keras_hub_export("keras_hub.models.DeepLabV3ImageSegmenterPreprocessor") 14 | class DeepLabV3ImageSegmenterPreprocessor(ImageSegmenterPreprocessor): 15 | backbone_cls = DeepLabV3Backbone 16 | image_converter_cls = DeepLabV3ImageConverter 17 | -------------------------------------------------------------------------------- /.kokoro/github/ubuntu/gpu/jax/continuous.cfg: -------------------------------------------------------------------------------- 1 | build_file: "keras-hub/.kokoro/github/ubuntu/gpu/build.sh" 2 | 3 | action { 4 | define_artifacts { 5 | regex: "**/sponge_log.log" 6 | regex: "**/sponge_log.xml" 7 | } 8 | } 9 | 10 | env_vars: { 11 | key: "KERAS_BACKEND" 12 | value: "jax" 13 | } 14 | 15 | before_action { 16 | fetch_keystore { 17 | keystore_resource { 18 | keystore_config_id: 73361 19 | keyname: "keras_kaggle_username" 20 | } 21 | } 22 | } 23 | 24 | before_action { 25 | fetch_keystore { 26 | keystore_resource { 27 | keystore_config_id: 73361 28 | keyname: "keras_kaggle_secret_key" 29 | } 30 | } 31 | } 32 | 33 | # Set timeout to 60 mins from default 180 mins 34 | timeout_mins: 60 -------------------------------------------------------------------------------- /.kokoro/github/ubuntu/gpu/jax/presubmit.cfg: -------------------------------------------------------------------------------- 1 | build_file: "keras-hub/.kokoro/github/ubuntu/gpu/build.sh" 2 | 3 | action { 4 | define_artifacts { 5 | regex: "**/sponge_log.log" 6 | regex: "**/sponge_log.xml" 7 | } 8 | } 9 | 10 | env_vars: { 11 | key: "KERAS_BACKEND" 12 | value: "jax" 13 | } 14 | 15 | before_action { 16 | fetch_keystore { 17 | keystore_resource { 18 | keystore_config_id: 73361 19 | keyname: "keras_kaggle_username" 20 | } 21 | } 22 | } 23 | 24 | before_action { 25 | fetch_keystore { 26 | keystore_resource { 27 | keystore_config_id: 73361 28 | keyname: "keras_kaggle_secret_key" 29 | } 30 | } 31 | } 32 | 33 | # Set timeout to 60 mins from default 180 mins 34 | timeout_mins: 60 -------------------------------------------------------------------------------- /.kokoro/github/ubuntu/gpu/torch/presubmit.cfg: -------------------------------------------------------------------------------- 1 | build_file: "keras-hub/.kokoro/github/ubuntu/gpu/build.sh" 2 | 3 | action { 4 | define_artifacts { 5 | regex: "**/sponge_log.log" 6 | regex: "**/sponge_log.xml" 7 | } 8 | } 9 | 10 | env_vars: { 11 | key: "KERAS_BACKEND" 12 | value: "torch" 13 | } 14 | 15 | before_action { 16 | fetch_keystore { 17 | keystore_resource { 18 | keystore_config_id: 73361 19 | keyname: "keras_kaggle_username" 20 | } 21 | } 22 | } 23 | 24 | before_action { 25 | fetch_keystore { 26 | keystore_resource { 27 | keystore_config_id: 73361 28 | keyname: "keras_kaggle_secret_key" 29 | } 30 | } 31 | } 32 | 33 | # Set timeout to 60 mins from default 180 mins 34 | timeout_mins: 60 -------------------------------------------------------------------------------- /keras_hub/src/models/t5/t5_layer_norm.py: -------------------------------------------------------------------------------- 1 | import keras 2 | from keras import ops 3 | 4 | 5 | class T5LayerNorm(keras.layers.Layer): 6 | def __init__(self, epsilon=1e-6, **kwargs): 7 | super().__init__(**kwargs) 8 | self.epsilon = epsilon 9 | 10 | def build(self, input_shape): 11 | self.weight = self.add_weight( 12 | name="weight", 13 | shape=(input_shape[-1],), 14 | initializer="ones", 15 | ) 16 | self.built = True 17 | 18 | def call(self, hidden_states): 19 | variance = ops.mean(ops.square(hidden_states), axis=-1, keepdims=True) 20 | hidden_states = hidden_states * ops.rsqrt(variance + self.epsilon) 21 | return self.weight * hidden_states 22 | -------------------------------------------------------------------------------- /.kokoro/github/ubuntu/gpu/torch/continuous.cfg: -------------------------------------------------------------------------------- 1 | build_file: "keras-hub/.kokoro/github/ubuntu/gpu/build.sh" 2 | 3 | action { 4 | define_artifacts { 5 | regex: "**/sponge_log.log" 6 | regex: "**/sponge_log.xml" 7 | } 8 | } 9 | 10 | env_vars: { 11 | key: "KERAS_BACKEND" 12 | value: "torch" 13 | } 14 | 15 | before_action { 16 | fetch_keystore { 17 | keystore_resource { 18 | keystore_config_id: 73361 19 | keyname: "keras_kaggle_username" 20 | } 21 | } 22 | } 23 | 24 | before_action { 25 | fetch_keystore { 26 | keystore_resource { 27 | keystore_config_id: 73361 28 | keyname: "keras_kaggle_secret_key" 29 | } 30 | } 31 | } 32 | 33 | # Set timeout to 60 mins from default 180 mins 34 | timeout_mins: 60 -------------------------------------------------------------------------------- /tools/checkpoint_conversion/checkpoint_conversion_utils.py: -------------------------------------------------------------------------------- 1 | import hashlib 2 | import tarfile 3 | import zipfile 4 | 5 | 6 | def get_md5_checksum(file_path): 7 | md5_hash = hashlib.md5() 8 | with open(file_path, "rb") as f: 9 | for byte_block in iter(lambda: f.read(4096), b""): 10 | md5_hash.update(byte_block) 11 | return md5_hash.hexdigest() 12 | 13 | 14 | def extract_files_from_archive(archive_file_path): 15 | if archive_file_path.endswith(".tar.gz"): 16 | with tarfile.open(archive_file_path, "r:gz") as tar: 17 | return tar.extractall() 18 | elif archive_file_path.endswith(".zip"): 19 | with zipfile.ZipFile(archive_file_path, "r") as zip_ref: 20 | return zip_ref.extractall() 21 | -------------------------------------------------------------------------------- /.kokoro/github/ubuntu/gpu/tensorflow/presubmit.cfg: -------------------------------------------------------------------------------- 1 | build_file: "keras-hub/.kokoro/github/ubuntu/gpu/build.sh" 2 | 3 | action { 4 | define_artifacts { 5 | regex: "**/sponge_log.log" 6 | regex: "**/sponge_log.xml" 7 | } 8 | } 9 | 10 | env_vars: { 11 | key: "KERAS_BACKEND" 12 | value: "tensorflow" 13 | } 14 | 15 | before_action { 16 | fetch_keystore { 17 | keystore_resource { 18 | keystore_config_id: 73361 19 | keyname: "keras_kaggle_username" 20 | } 21 | } 22 | } 23 | 24 | before_action { 25 | fetch_keystore { 26 | keystore_resource { 27 | keystore_config_id: 73361 28 | keyname: "keras_kaggle_secret_key" 29 | } 30 | } 31 | } 32 | 33 | # Set timeout to 60 mins from default 180 mins 34 | timeout_mins: 60 -------------------------------------------------------------------------------- /.kokoro/github/ubuntu/gpu/tensorflow/continuous.cfg: -------------------------------------------------------------------------------- 1 | build_file: "keras-hub/.kokoro/github/ubuntu/gpu/build.sh" 2 | 3 | action { 4 | define_artifacts { 5 | regex: "**/sponge_log.log" 6 | regex: "**/sponge_log.xml" 7 | } 8 | } 9 | 10 | env_vars: { 11 | key: "KERAS_BACKEND" 12 | value: "tensorflow" 13 | } 14 | 15 | before_action { 16 | fetch_keystore { 17 | keystore_resource { 18 | keystore_config_id: 73361 19 | keyname: "keras_kaggle_username" 20 | } 21 | } 22 | } 23 | 24 | before_action { 25 | fetch_keystore { 26 | keystore_resource { 27 | keystore_config_id: 73361 28 | keyname: "keras_kaggle_secret_key" 29 | } 30 | } 31 | } 32 | 33 | # Set timeout to 60 mins from default 180 mins 34 | timeout_mins: 60 -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | 14 | 15 | **Describe the solution you'd like** 16 | 19 | 20 | **Describe alternatives you've considered** 21 | 24 | 25 | **Additional context** 26 | 29 | -------------------------------------------------------------------------------- /keras_hub/src/models/stable_diffusion_3/stable_diffusion_3_presets.py: -------------------------------------------------------------------------------- 1 | """StableDiffusion3 preset configurations.""" 2 | 3 | backbone_presets = { 4 | "stable_diffusion_3_medium": { 5 | "metadata": { 6 | "description": ( 7 | "3 billion parameter, including CLIP L and CLIP G text " 8 | "encoders, MMDiT generative model, and VAE autoencoder. " 9 | "Developed by Stability AI." 10 | ), 11 | "params": 2987080931, 12 | "official_name": "StableDiffusion3", 13 | "path": "stable_diffusion_3", 14 | "model_card": "https://arxiv.org/abs/2110.00476", 15 | }, 16 | "kaggle_handle": "kaggle://keras/stablediffusion3/keras/stable_diffusion_3_medium/3", 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /keras_hub/api/samplers/__init__.py: -------------------------------------------------------------------------------- 1 | """DO NOT EDIT. 2 | 3 | This file was autogenerated. Do not edit it by hand, 4 | since your modifications would be overwritten. 5 | """ 6 | 7 | from keras_hub.src.samplers.beam_sampler import BeamSampler 8 | from keras_hub.src.samplers.contrastive_sampler import ContrastiveSampler 9 | from keras_hub.src.samplers.greedy_sampler import GreedySampler 10 | from keras_hub.src.samplers.random_sampler import RandomSampler 11 | from keras_hub.src.samplers.sampler import Sampler 12 | from keras_hub.src.samplers.serialization import deserialize 13 | from keras_hub.src.samplers.serialization import get 14 | from keras_hub.src.samplers.serialization import serialize 15 | from keras_hub.src.samplers.top_k_sampler import TopKSampler 16 | from keras_hub.src.samplers.top_p_sampler import TopPSampler 17 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you have discovered a security vulnerability in this project, please report it 4 | privately. **Do not disclose it as a public issue.** This gives us time to work with you 5 | to fix the issue before public exposure, reducing the chance that the exploit will be 6 | used before a patch is released. 7 | 8 | You may submit the report in the following ways: 9 | 10 | - send a [private vulnerability report](https://github.com/keras-team/keras-hub/security/advisories/new) 11 | 12 | Please provide the following information in your report: 13 | 14 | - A description of the vulnerability and its impact 15 | - How to reproduce the issue 16 | 17 | This project is maintained by volunteers on a reasonable-effort basis. As such, 18 | please give us 90 days to work on a fix before public exposure. 19 | -------------------------------------------------------------------------------- /keras_hub/src/layers/preprocessing/preprocessing_layer.py: -------------------------------------------------------------------------------- 1 | import keras 2 | 3 | from keras_hub.src.utils.tensor_utils import assert_tf_libs_installed 4 | 5 | 6 | class PreprocessingLayer(keras.layers.Layer): 7 | """Preprocessing layer base class.""" 8 | 9 | def __init__(self, **kwargs): 10 | assert_tf_libs_installed(self.__class__.__name__) 11 | super().__init__(**kwargs) 12 | # Don't convert inputs (we want tf tensors not backend tensors). 13 | self._convert_input_args = False 14 | # Allow raw inputs like python strings. 15 | self._allow_non_tensor_positional_args = True 16 | # Most pre-preprocessing has no build. 17 | if not hasattr(self, "build"): 18 | self.built = True 19 | 20 | def get_build_config(self): 21 | return None 22 | -------------------------------------------------------------------------------- /keras_hub/src/models/deeplab_v3/deeplab_v3_presets.py: -------------------------------------------------------------------------------- 1 | """DeepLabV3 preset configurations.""" 2 | 3 | backbone_presets = { 4 | "deeplab_v3_plus_resnet50_pascalvoc": { 5 | "metadata": { 6 | "description": ( 7 | "DeepLabV3+ model with ResNet50 as image encoder and trained on " 8 | "augmented Pascal VOC dataset by Semantic Boundaries Dataset(SBD)" 9 | "which is having categorical accuracy of 90.01 and 0.63 Mean IoU." 10 | ), 11 | "params": 39190656, 12 | "official_name": "DeepLabV3", 13 | "path": "deeplab_v3", 14 | "model_card": "https://arxiv.org/abs/1802.02611", 15 | }, 16 | "kaggle_handle": "kaggle://keras/deeplabv3plus/keras/deeplab_v3_plus_resnet50_pascalvoc/3", 17 | }, 18 | } 19 | -------------------------------------------------------------------------------- /integration_tests/no_tensorflow_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | import keras_hub 6 | 7 | 8 | class NoTensorflow(unittest.TestCase): 9 | def test_backbone_works(self): 10 | backbone = keras_hub.models.BertBackbone.from_preset( 11 | "bert_tiny_en_uncased", 12 | ) 13 | backbone.predict( 14 | { 15 | "token_ids": np.ones((4, 128)), 16 | "padding_mask": np.ones((4, 128)), 17 | "segment_ids": np.ones((4, 128)), 18 | } 19 | ) 20 | 21 | def test_tokenizer_errors(self): 22 | with self.assertRaises(Exception) as e: 23 | keras_hub.models.BertTokenizer.from_preset( 24 | "bert_tiny_en_uncased", 25 | ) 26 | self.assertTrue("pip install tensorflow-text" in e.exception) 27 | -------------------------------------------------------------------------------- /keras_nlp/keras_nlp/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # Add everything in /api/ to the module search path. 4 | import keras_hub 5 | 6 | # Import everything from /api/ into keras. 7 | from keras_hub.api import * # noqa: F403 8 | from keras_hub.api import __version__ # Import * ignores names start with "_". 9 | 10 | __path__.extend(keras_hub.__path__) # noqa: F405 11 | # Don't pollute namespace. 12 | del keras_hub 13 | del os 14 | 15 | 16 | # Never autocomplete `.src` or `.api` on an imported keras object. 17 | def __dir__(): 18 | keys = dict.fromkeys((globals().keys())) 19 | keys.pop("src") 20 | keys.pop("api") 21 | return list(keys) 22 | 23 | 24 | # Don't import `.src` or `.api` during `from keras import *`. 25 | __all__ = [ 26 | name 27 | for name in globals().keys() 28 | if not (name.startswith("_") or name in ("src", "api")) 29 | ] 30 | -------------------------------------------------------------------------------- /keras_hub/src/utils/timm/convert_densenet_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras import ops 3 | 4 | from keras_hub.src.models.backbone import Backbone 5 | from keras_hub.src.models.image_classifier import ImageClassifier 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class TimmDenseNetBackboneTest(TestCase): 10 | @pytest.mark.large 11 | def test_convert_densenet_backbone(self): 12 | model = Backbone.from_preset("hf://timm/densenet121.tv_in1k") 13 | outputs = model.predict(ops.ones((1, 224, 224, 3))) 14 | self.assertEqual(outputs.shape, (1, 7, 7, 1024)) 15 | 16 | @pytest.mark.large 17 | def test_convert_densenet_classifier(self): 18 | model = ImageClassifier.from_preset("hf://timm/densenet121.tv_in1k") 19 | outputs = model.predict(ops.ones((1, 512, 512, 3))) 20 | self.assertEqual(outputs.shape, (1, 1000)) 21 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "github-actions" 9 | directory: "/" 10 | schedule: 11 | interval: "monthly" 12 | groups: 13 | github-actions: 14 | patterns: 15 | - "*" 16 | - package-ecosystem: "pip" 17 | directory: "/" 18 | schedule: 19 | interval: "monthly" 20 | groups: 21 | python: 22 | patterns: 23 | - "*" 24 | ignore: 25 | # ignore all updates for JAX GPU due to cuda version issue 26 | - dependency-name: "jax[cuda12_pip]" 27 | -------------------------------------------------------------------------------- /keras_hub/src/bounding_box/to_dense_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import tensorflow as tf 3 | from keras import backend 4 | 5 | from keras_hub.src.bounding_box import to_dense 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class ToDenseTest(TestCase): 10 | @pytest.mark.skipif( 11 | backend.backend() != "tensorflow", 12 | reason="Only applies to backends which support raggeds", 13 | ) 14 | def test_converts_to_dense(self): 15 | bounding_boxes = { 16 | "boxes": tf.ragged.constant( 17 | [[[0, 0, 1, 1]], [[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 1, 1]]] 18 | ), 19 | "classes": tf.ragged.constant([[0], [1, 2, 3]]), 20 | } 21 | bounding_boxes = to_dense.to_dense(bounding_boxes) 22 | self.assertEqual(bounding_boxes["boxes"].shape, [2, 3, 4]) 23 | self.assertEqual(bounding_boxes["classes"].shape, [2, 3]) 24 | -------------------------------------------------------------------------------- /keras_hub/src/utils/timm/convert_resnet_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras import ops 3 | 4 | from keras_hub.src.models.backbone import Backbone 5 | from keras_hub.src.models.image_classifier import ImageClassifier 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class TimmResNetBackboneTest(TestCase): 10 | @pytest.mark.large 11 | def test_convert_resnet_backbone(self): 12 | model = Backbone.from_preset("hf://timm/resnet18.a1_in1k") 13 | outputs = model.predict(ops.ones((1, 224, 224, 3))) 14 | self.assertEqual(outputs.shape, (1, 7, 7, 512)) 15 | 16 | @pytest.mark.large 17 | def test_convert_resnet_classifier(self): 18 | model = ImageClassifier.from_preset("hf://timm/resnet18.a1_in1k") 19 | outputs = model.predict(ops.ones((1, 512, 512, 3))) 20 | self.assertEqual(outputs.shape, (1, 1000)) 21 | 22 | # TODO: compare numerics with timm model 23 | -------------------------------------------------------------------------------- /keras_hub/src/models/gemma/rms_normalization.py: -------------------------------------------------------------------------------- 1 | import keras 2 | from keras import ops 3 | 4 | 5 | class RMSNormalization(keras.layers.Layer): 6 | def __init__(self, epsilon=1e-6, **kwargs): 7 | super().__init__(**kwargs) 8 | self.epsilon = epsilon 9 | 10 | def build(self, input_shape): 11 | self.scale = self.add_weight( 12 | name="scale", 13 | trainable=True, 14 | shape=(input_shape[-1],), 15 | initializer="zeros", 16 | ) 17 | self.built = True 18 | 19 | def call(self, x): 20 | # Always compute normalization in float32. 21 | x = ops.cast(x, "float32") 22 | scale = ops.cast(self.scale, "float32") 23 | var = ops.mean(ops.square(x), axis=-1, keepdims=True) 24 | normed_inputs = x * ops.reciprocal(ops.sqrt(var + self.epsilon)) 25 | normed_inputs = normed_inputs * (1 + scale) 26 | return ops.cast(normed_inputs, self.compute_dtype) 27 | -------------------------------------------------------------------------------- /keras_hub/src/models/sam/sam_image_segmenter_preprocessor.py: -------------------------------------------------------------------------------- 1 | import keras 2 | 3 | from keras_hub.src.api_export import keras_hub_export 4 | from keras_hub.src.models.image_segmenter_preprocessor import ( 5 | ImageSegmenterPreprocessor, 6 | ) 7 | from keras_hub.src.models.sam.sam_backbone import SAMBackbone 8 | from keras_hub.src.models.sam.sam_image_converter import SAMImageConverter 9 | from keras_hub.src.utils.tensor_utils import preprocessing_function 10 | 11 | 12 | @keras_hub_export("keras_hub.models.SAMImageSegmenterPreprocessor") 13 | class SAMImageSegmenterPreprocessor(ImageSegmenterPreprocessor): 14 | backbone_cls = SAMBackbone 15 | image_converter_cls = SAMImageConverter 16 | 17 | @preprocessing_function 18 | def call(self, x, y=None, sample_weight=None): 19 | images = x["images"] 20 | if self.image_converter: 21 | x["images"] = self.image_converter(images) 22 | return keras.utils.pack_x_y_sample_weight(x, y, sample_weight) 23 | -------------------------------------------------------------------------------- /keras_hub/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # sentencepiece segfaults on some version of tensorflow if tf is imported first. 4 | try: 5 | import sentencepiece 6 | except ImportError: 7 | pass 8 | 9 | # Import everything from /api/ into keras. 10 | from keras_hub.api import * # noqa: F403 11 | from keras_hub.api import __version__ # Import * ignores names start with "_". 12 | 13 | # Add everything in /api/ to the module search path. 14 | __path__.append(os.path.join(os.path.dirname(__file__), "api")) # noqa: F405 15 | 16 | # Don't pollute namespace. 17 | del os 18 | 19 | 20 | # Never autocomplete `.src` or `.api` on an imported keras object. 21 | def __dir__(): 22 | keys = dict.fromkeys((globals().keys())) 23 | keys.pop("src") 24 | keys.pop("api") 25 | return list(keys) 26 | 27 | 28 | # Don't import `.src` or `.api` during `from keras import *`. 29 | __all__ = [ 30 | name 31 | for name in globals().keys() 32 | if not (name.startswith("_") or name in ("src", "api")) 33 | ] 34 | -------------------------------------------------------------------------------- /keras_hub/src/utils/keras_utils_test.py: -------------------------------------------------------------------------------- 1 | import keras 2 | 3 | from keras_hub.src.tests.test_case import TestCase 4 | from keras_hub.src.utils.keras_utils import clone_initializer 5 | 6 | 7 | class CloneInitializerTest(TestCase): 8 | def test_config_equality(self): 9 | initializer = keras.initializers.VarianceScaling( 10 | scale=2.0, 11 | mode="fan_out", 12 | ) 13 | clone = clone_initializer(initializer) 14 | self.assertAllEqual(initializer.get_config(), clone.get_config()) 15 | 16 | def test_random_output(self): 17 | initializer = keras.initializers.VarianceScaling( 18 | scale=2.0, 19 | mode="fan_out", 20 | ) 21 | clone = clone_initializer(initializer) 22 | self.assertNotAllEqual(initializer(shape=(2, 2)), clone(shape=(2, 2))) 23 | 24 | def test_strings(self): 25 | initializer = "glorot_uniform" 26 | clone = clone_initializer(initializer) 27 | self.assertAllEqual(initializer, clone) 28 | -------------------------------------------------------------------------------- /tools/pretrained_tokenizers/word_piece_cleaning_script.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import tqdm 4 | 5 | # Clean these folders and saved parsed version of them. 6 | clean_folders = ["bnwiki", "arwiki", "ruwiki", "ptwiki", "idwiki"] 7 | 8 | for i in range(len(clean_folders)): 9 | clean_folder = clean_folders[i] 10 | output_folder = clean_folders[i] + "_parsed" 11 | os.mkdir(output_folder) 12 | for folder in tqdm.tqdm(os.listdir(clean_folder)): 13 | path = os.path.join(clean_folder, folder) 14 | os.mkdir(os.path.join(output_folder, folder)) 15 | for file in os.listdir(path): 16 | article = [] 17 | with open(os.path.join(path, file)) as f: 18 | for line in f: 19 | if line.startswith("") or line.startswith(""] 9 | self.vocab = dict([(token, i) for i, token in enumerate(self.vocab)]) 10 | self.merges = ["Ġ a", "Ġ t", "Ġ i", "Ġ b", "a i", "p l", "n e"] 11 | self.merges += ["Ġa t", "p o", "r t", "Ġt h", "ai r", "pl a", "po rt"] 12 | self.merges += ["Ġai r", "Ġa i", "pla ne"] 13 | self.init_kwargs = {"vocabulary": self.vocab, "merges": self.merges} 14 | self.input_data = [ 15 | " airplane at airport<|endoftext|>", 16 | " airplane airport", 17 | ] 18 | 19 | def test_tokenizer_basics(self): 20 | self.run_preprocessing_layer_test( 21 | cls=GPTNeoXTokenizer, 22 | init_kwargs=self.init_kwargs, 23 | input_data=self.input_data, 24 | expected_output=[[2, 3, 4, 2, 5, 6], [2, 3, 2, 5]], 25 | ) 26 | 27 | def test_errors_missing_special_tokens(self): 28 | with self.assertRaises(ValueError): 29 | GPTNeoXTokenizer(vocabulary=["a", "b", "c"], merges=[]) 30 | -------------------------------------------------------------------------------- /keras_hub/src/models/sam/sam_presets.py: -------------------------------------------------------------------------------- 1 | """SAM preset configurations.""" 2 | 3 | backbone_presets = { 4 | "sam_base_sa1b": { 5 | "metadata": { 6 | "description": ("The base SAM model trained on the SA1B dataset."), 7 | "params": 93735728, 8 | "official_name": "SAMImageSegmenter", 9 | "path": "sam", 10 | "model_card": "https://arxiv.org/abs/2304.02643", 11 | }, 12 | "kaggle_handle": "kaggle://keras/sam/keras/sam_base_sa1b/4", 13 | }, 14 | "sam_large_sa1b": { 15 | "metadata": { 16 | "description": ("The large SAM model trained on the SA1B dataset."), 17 | "params": 641090864, 18 | "official_name": "SAMImageSegmenter", 19 | "path": "sam", 20 | "model_card": "https://arxiv.org/abs/2304.02643", 21 | }, 22 | "kaggle_handle": "kaggle://keras/sam/keras/sam_large_sa1b/4", 23 | }, 24 | "sam_huge_sa1b": { 25 | "metadata": { 26 | "description": ("The huge SAM model trained on the SA1B dataset."), 27 | "params": 312343088, 28 | "official_name": "SAMImageSegmenter", 29 | "path": "sam", 30 | "model_card": "https://arxiv.org/abs/2304.02643", 31 | }, 32 | "kaggle_handle": "kaggle://keras/sam/keras/sam_huge_sa1b/4", 33 | }, 34 | } 35 | -------------------------------------------------------------------------------- /keras_hub/src/models/vit_det/vit_det_backbone_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.vit_det.vit_det_backbone import ViTDetBackbone 5 | from keras_hub.src.tests.test_case import TestCase 6 | 7 | 8 | class ViTDetBackboneTest(TestCase): 9 | def setUp(self): 10 | self.init_kwargs = { 11 | "image_shape": (16, 16, 3), 12 | "patch_size": 2, 13 | "hidden_size": 4, 14 | "num_layers": 2, 15 | "global_attention_layer_indices": [2, 5, 8, 11], 16 | "intermediate_dim": 4 * 4, 17 | "num_heads": 2, 18 | "num_output_channels": 2, 19 | "window_size": 2, 20 | } 21 | self.input_data = np.ones((1, 16, 16, 3), dtype="float32") 22 | 23 | def test_backbone_basics(self): 24 | self.run_backbone_test( 25 | cls=ViTDetBackbone, 26 | init_kwargs=self.init_kwargs, 27 | input_data=self.input_data, 28 | expected_output_shape=(1, 8, 8, 2), 29 | run_mixed_precision_check=False, 30 | run_quantization_check=False, 31 | ) 32 | 33 | @pytest.mark.large 34 | def test_saved_model(self): 35 | self.run_model_saving_test( 36 | cls=ViTDetBackbone, 37 | init_kwargs=self.init_kwargs, 38 | input_data=self.input_data, 39 | ) 40 | -------------------------------------------------------------------------------- /keras_hub/src/utils/transformers/convert_gemma_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.backbone import Backbone 4 | from keras_hub.src.models.causal_lm import CausalLM 5 | from keras_hub.src.models.gemma.gemma_backbone import GemmaBackbone 6 | from keras_hub.src.models.gemma.gemma_causal_lm import GemmaCausalLM 7 | from keras_hub.src.tests.test_case import TestCase 8 | 9 | 10 | class TestTask(TestCase): 11 | @pytest.mark.large 12 | def test_convert_tiny_preset(self): 13 | model = GemmaCausalLM.from_preset("hf://ariG23498/tiny-gemma-test") 14 | prompt = "What is your favorite condiment?" 15 | model.generate([prompt], max_length=15) 16 | 17 | model = GemmaCausalLM.from_preset("hf://ariG23498/tiny-gemma-2-test") 18 | prompt = "What is your favorite condiment?" 19 | model.generate([prompt], max_length=15) 20 | 21 | @pytest.mark.large 22 | def test_class_detection(self): 23 | model = CausalLM.from_preset( 24 | "hf://ariG23498/tiny-gemma-test", 25 | load_weights=False, 26 | ) 27 | self.assertIsInstance(model, GemmaCausalLM) 28 | model = Backbone.from_preset( 29 | "hf://ariG23498/tiny-gemma-test", 30 | load_weights=False, 31 | ) 32 | self.assertIsInstance(model, GemmaBackbone) 33 | 34 | # TODO: compare numerics with huggingface model 35 | -------------------------------------------------------------------------------- /keras_hub/src/layers/modeling/token_and_position_embedding_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from keras import ops 3 | from keras import random 4 | 5 | from keras_hub.src.layers.modeling.token_and_position_embedding import ( 6 | TokenAndPositionEmbedding, 7 | ) 8 | from keras_hub.src.tests.test_case import TestCase 9 | 10 | 11 | class TokenAndPositionEmbeddingTest(TestCase): 12 | def test_layer_behaviors(self): 13 | self.run_layer_test( 14 | cls=TokenAndPositionEmbedding, 15 | init_kwargs={ 16 | "vocabulary_size": 5, 17 | "sequence_length": 4, 18 | "embedding_dim": 3, 19 | "embeddings_initializer": "ones", 20 | }, 21 | input_data=random.randint(minval=0, maxval=5, shape=(2, 4)), 22 | expected_output_shape=(2, 4, 3), 23 | expected_output_data=ops.ones((2, 4, 3)) * 2, 24 | expected_num_trainable_weights=2, 25 | ) 26 | 27 | def test_mask_propagation(self): 28 | test_layer = TokenAndPositionEmbedding( 29 | vocabulary_size=5, 30 | sequence_length=4, 31 | embedding_dim=3, 32 | mask_zero=True, 33 | ) 34 | input_data = np.array([[1, 0], [1, 0]]) 35 | mask = input_data != 0 36 | outputs = test_layer(input_data) 37 | self.assertAllEqual(outputs._keras_mask, mask) 38 | -------------------------------------------------------------------------------- /tools/count_preset_params.py: -------------------------------------------------------------------------------- 1 | """ 2 | Small utility script to count parameters in our preset checkpoints. 3 | 4 | Usage: 5 | python tools/count_preset_params.py 6 | python tools/count_preset_params.py --model BertBackbone 7 | python tools/count_preset_params.py --preset bert_base_multi 8 | """ 9 | 10 | import inspect 11 | 12 | from absl import app 13 | from absl import flags 14 | from keras.utils.layer_utils import count_params 15 | from tensorflow import keras 16 | 17 | import keras_hub 18 | 19 | FLAGS = flags.FLAGS 20 | flags.DEFINE_string("model", None, "The name of a model, e.g. BertBackbone.") 21 | flags.DEFINE_string( 22 | "preset", None, "The name of a preset, e.g. bert_base_multi." 23 | ) 24 | 25 | 26 | def main(_): 27 | for name, symbol in keras_hub.models.__dict__.items(): 28 | if inspect.isclass(symbol) and issubclass(symbol, keras.Model): 29 | if FLAGS.model and name != FLAGS.model: 30 | continue 31 | if not hasattr(symbol, "from_preset"): 32 | continue 33 | for preset in symbol.presets: 34 | if FLAGS.preset and preset != FLAGS.preset: 35 | continue 36 | model = symbol.from_preset(preset) 37 | params = count_params(model.weights) 38 | print(f"{name} {preset} {params}") 39 | 40 | 41 | if __name__ == "__main__": 42 | app.run(main) 43 | -------------------------------------------------------------------------------- /keras_hub/src/utils/transformers/convert_distilbert_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.backbone import Backbone 4 | from keras_hub.src.models.distil_bert.distil_bert_backbone import ( 5 | DistilBertBackbone, 6 | ) 7 | from keras_hub.src.models.distil_bert.distil_bert_text_classifier import ( 8 | DistilBertTextClassifier, 9 | ) 10 | from keras_hub.src.models.text_classifier import TextClassifier 11 | from keras_hub.src.tests.test_case import TestCase 12 | 13 | 14 | class TestTask(TestCase): 15 | @pytest.mark.large 16 | def test_convert_tiny_preset(self): 17 | model = DistilBertTextClassifier.from_preset( 18 | "hf://distilbert/distilbert-base-uncased", num_classes=2 19 | ) 20 | prompt = "That movies was terrible." 21 | model.predict([prompt]) 22 | 23 | @pytest.mark.large 24 | def test_class_detection(self): 25 | model = TextClassifier.from_preset( 26 | "hf://distilbert/distilbert-base-uncased", 27 | num_classes=2, 28 | load_weights=False, 29 | ) 30 | self.assertIsInstance(model, DistilBertTextClassifier) 31 | model = Backbone.from_preset( 32 | "hf://distilbert/distilbert-base-uncased", 33 | load_weights=False, 34 | ) 35 | self.assertIsInstance(model, DistilBertBackbone) 36 | 37 | # TODO: compare numerics with huggingface model 38 | -------------------------------------------------------------------------------- /keras_hub/src/utils/transformers/convert_pali_gemma_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.backbone import Backbone 5 | from keras_hub.src.models.causal_lm import CausalLM 6 | from keras_hub.src.models.pali_gemma.pali_gemma_backbone import ( 7 | PaliGemmaBackbone, 8 | ) 9 | from keras_hub.src.models.pali_gemma.pali_gemma_causal_lm import ( 10 | PaliGemmaCausalLM, 11 | ) 12 | from keras_hub.src.tests.test_case import TestCase 13 | 14 | 15 | class TestTask(TestCase): 16 | @pytest.mark.large 17 | def test_convert_tiny_preset(self): 18 | model = PaliGemmaCausalLM.from_preset( 19 | "hf://ariG23498/tiny-pali-gemma-test" 20 | ) 21 | image = np.random.rand(224, 224, 3) 22 | prompt = "describe the image " 23 | model.generate({"images": image, "prompts": prompt}, max_length=15) 24 | 25 | @pytest.mark.large 26 | def test_class_detection(self): 27 | model = CausalLM.from_preset( 28 | "hf://ariG23498/tiny-pali-gemma-test", 29 | load_weights=False, 30 | ) 31 | self.assertIsInstance(model, PaliGemmaCausalLM) 32 | model = Backbone.from_preset( 33 | "hf://ariG23498/tiny-pali-gemma-test", 34 | load_weights=False, 35 | ) 36 | self.assertIsInstance(model, PaliGemmaBackbone) 37 | 38 | # TODO: compare numerics with huggingface model 39 | -------------------------------------------------------------------------------- /keras_hub/src/models/densenet/densenet_backbone_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.densenet.densenet_backbone import DenseNetBackbone 5 | from keras_hub.src.tests.test_case import TestCase 6 | 7 | 8 | class DenseNetBackboneTest(TestCase): 9 | def setUp(self): 10 | self.init_kwargs = { 11 | "stackwise_num_repeats": [2, 4, 6, 4], 12 | "compression_ratio": 0.5, 13 | "growth_rate": 2, 14 | "image_shape": (32, 32, 3), 15 | } 16 | self.input_size = 32 17 | self.input_data = np.ones( 18 | (2, self.input_size, self.input_size, 3), dtype="float32" 19 | ) 20 | 21 | def test_backbone_basics(self): 22 | self.run_vision_backbone_test( 23 | cls=DenseNetBackbone, 24 | init_kwargs=self.init_kwargs, 25 | input_data=self.input_data, 26 | expected_output_shape=(2, 1, 1, 24), 27 | expected_pyramid_output_keys=["P2", "P3", "P4", "P5"], 28 | expected_pyramid_image_sizes=[(8, 8), (4, 4), (2, 2), (1, 1)], 29 | run_mixed_precision_check=False, 30 | run_data_format_check=False, 31 | ) 32 | 33 | @pytest.mark.large 34 | def test_saved_model(self): 35 | self.run_model_saving_test( 36 | cls=DenseNetBackbone, 37 | init_kwargs=self.init_kwargs, 38 | input_data=self.input_data, 39 | ) 40 | -------------------------------------------------------------------------------- /tools/pretrained_tokenizers/word_piece_training_script.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import keras_hub 5 | 6 | # List directories of parsed Wikipedia articles and vocab sizes 7 | directories = [ 8 | "eswiki_parsed", 9 | "frwiki_parsed", 10 | "hiwiki_parsed", 11 | "arwiki_parsed", 12 | "ruwiki_parsed", 13 | "bnwiki_parsed", 14 | "idwiki_parsed", 15 | "ptwiki_parsed", 16 | ] 17 | vocab_sizes = [20000, 50000] 18 | identifier = "v1" 19 | 20 | # Runs the computation 21 | for directory in directories: 22 | for vocab_size in vocab_sizes: 23 | print(f"Running directory {directory} with vocab size {vocab_size}") 24 | files = [] 25 | for folder in os.listdir(directory): 26 | path = os.path.join(directory, folder) 27 | for file in os.listdir(path): 28 | if file[0] != ".": 29 | files.append(os.path.join(path, file)) 30 | 31 | if os.path.exists(f"{directory}_{vocab_size}_{identifier}.txt"): 32 | raise ValueError("already done.") 33 | 34 | start = time.time() 35 | keras_hub.tokenizers.compute_word_piece_vocabulary( 36 | files, 37 | vocabulary_size=vocab_size, 38 | lowercase=False, 39 | strip_accents=False, 40 | vocabulary_output_file=f"{directory}_{vocab_size}_{identifier}.txt", 41 | ) 42 | end = time.time() 43 | print("Time taken:", end - start) 44 | -------------------------------------------------------------------------------- /keras_hub/src/utils/imagenet/imagenet_utils_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from keras_hub.src.tests.test_case import TestCase 4 | from keras_hub.src.utils.imagenet.imagenet_utils import ( 5 | decode_imagenet_predictions, 6 | ) 7 | 8 | 9 | class ImageNetUtilsTest(TestCase): 10 | def test_decode_imagenet_predictions(self): 11 | preds = np.array( 12 | [ 13 | [0.0] * 997 + [0.5, 0.3, 0.2], 14 | [0.0] * 997 + [0.2, 0.3, 0.5], 15 | ] 16 | ) 17 | labels = decode_imagenet_predictions(preds, top=3) 18 | self.assertEqual( 19 | labels, 20 | [ 21 | [("bolete", 0.5), ("ear", 0.3), ("toilet_tissue", 0.2)], 22 | [("toilet_tissue", 0.5), ("ear", 0.3), ("bolete", 0.2)], 23 | ], 24 | ) 25 | labels = decode_imagenet_predictions( 26 | preds, top=3, include_synset_ids=True 27 | ) 28 | self.assertEqual( 29 | labels, 30 | [ 31 | [ 32 | ("n13054560", "bolete", 0.5), 33 | ("n13133613", "ear", 0.3), 34 | ("n15075141", "toilet_tissue", 0.2), 35 | ], 36 | [ 37 | ("n15075141", "toilet_tissue", 0.5), 38 | ("n13133613", "ear", 0.3), 39 | ("n13054560", "bolete", 0.2), 40 | ], 41 | ], 42 | ) 43 | -------------------------------------------------------------------------------- /keras_hub/src/models/whisper/whisper_audio_converter_test.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | from keras_hub.src.models.whisper.whisper_audio_converter import ( 4 | WhisperAudioConverter, 5 | ) 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class WhisperAudioConverterTest(TestCase): 10 | def setUp(self): 11 | self.init_kwargs = { 12 | "num_mels": 80, 13 | "num_fft_bins": 400, 14 | "stride": 100, 15 | "sampling_rate": 100, 16 | "max_audio_length": 5, 17 | } 18 | audio_tensor_1 = tf.ones((2,), dtype="float32") 19 | audio_tensor_2 = tf.ones((25,), dtype="float32") 20 | self.input_data = tf.ragged.stack( 21 | [audio_tensor_1, audio_tensor_2], 22 | axis=0, 23 | ) 24 | 25 | def test_feature_extractor_basics(self): 26 | self.run_preprocessing_layer_test( 27 | cls=WhisperAudioConverter, 28 | init_kwargs=self.init_kwargs, 29 | input_data=self.input_data, 30 | ) 31 | 32 | def test_correctness(self): 33 | audio_tensor = tf.ones((2,), dtype="float32") 34 | outputs = WhisperAudioConverter(**self.init_kwargs)(audio_tensor) 35 | 36 | # Verify shape. 37 | self.assertEqual(outputs.shape, (5, 80)) 38 | # Verify output. 39 | expected = [1.1656, 1.0151, -0.8343, -0.8343, -0.8343] 40 | self.assertAllClose(outputs[:, 0], expected, atol=0.01, rtol=0.01) 41 | -------------------------------------------------------------------------------- /keras_hub/src/models/mistral/mistral_presets.py: -------------------------------------------------------------------------------- 1 | """Mistral model preset configurations.""" 2 | 3 | # Metadata for loading pretrained model weights. 4 | backbone_presets = { 5 | "mistral_7b_en": { 6 | "metadata": { 7 | "description": "Mistral 7B base model", 8 | "params": 7241732096, 9 | "official_name": "Mistral", 10 | "path": "mistral", 11 | "model_card": "https://github.com/mistralai/mistral-src/blob/main/README.md", 12 | }, 13 | "kaggle_handle": "kaggle://keras/mistral/keras/mistral_7b_en/6", 14 | }, 15 | "mistral_instruct_7b_en": { 16 | "metadata": { 17 | "description": "Mistral 7B instruct model", 18 | "params": 7241732096, 19 | "official_name": "Mistral", 20 | "path": "mistral", 21 | "model_card": "https://github.com/mistralai/mistral-src/blob/main/README.md", 22 | }, 23 | "kaggle_handle": "kaggle://keras/mistral/keras/mistral_instruct_7b_en/6", 24 | }, 25 | "mistral_0.2_instruct_7b_en": { 26 | "metadata": { 27 | "description": "Mistral 7B instruct Version 0.2 model", 28 | "params": 7241732096, 29 | "official_name": "Mistral", 30 | "path": "mistral", 31 | "model_card": "https://github.com/mistralai/mistral-src/blob/main/README.md", 32 | }, 33 | "kaggle_handle": "kaggle://keras/mistral/keras/mistral_0.2_instruct_7b_en/1", 34 | }, 35 | } 36 | -------------------------------------------------------------------------------- /keras_hub/src/models/csp_darknet/csp_darknet_backbone_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.csp_darknet.csp_darknet_backbone import ( 5 | CSPDarkNetBackbone, 6 | ) 7 | from keras_hub.src.tests.test_case import TestCase 8 | 9 | 10 | class CSPDarkNetBackboneTest(TestCase): 11 | def setUp(self): 12 | self.init_kwargs = { 13 | "stackwise_num_filters": [2, 4, 6, 8], 14 | "stackwise_depth": [1, 3, 3, 1], 15 | "block_type": "basic_block", 16 | "image_shape": (32, 32, 3), 17 | } 18 | self.input_size = 32 19 | self.input_data = np.ones( 20 | (2, self.input_size, self.input_size, 3), dtype="float32" 21 | ) 22 | 23 | def test_backbone_basics(self): 24 | self.run_vision_backbone_test( 25 | cls=CSPDarkNetBackbone, 26 | init_kwargs=self.init_kwargs, 27 | input_data=self.input_data, 28 | expected_output_shape=(2, 1, 1, 8), 29 | expected_pyramid_output_keys=["P2", "P3", "P4", "P5"], 30 | expected_pyramid_image_sizes=[(8, 8), (4, 4), (2, 2), (1, 1)], 31 | run_mixed_precision_check=False, 32 | run_data_format_check=False, 33 | ) 34 | 35 | @pytest.mark.large 36 | def test_saved_model(self): 37 | self.run_model_saving_test( 38 | cls=CSPDarkNetBackbone, 39 | init_kwargs=self.init_kwargs, 40 | input_data=self.input_data, 41 | ) 42 | -------------------------------------------------------------------------------- /.github/workflows/labeler.yaml: -------------------------------------------------------------------------------- 1 | # Copyright 2024 Google LLC. All Rights Reserved. 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 | 15 | 16 | # This workflow automatically identifies issues and pull requests (PRs) 17 | # related to Gemma. It searches for the keyword "Gemma" (case-insensitive) 18 | # in both the title and description of the issue/PR. If a match is found, 19 | # the workflow adds the label 'Gemma' to the issue/PR. 20 | 21 | name: 'Labeler' 22 | on: 23 | issues: 24 | types: [edited, opened] 25 | pull_request_target: 26 | types: [opened, edited] 27 | 28 | permissions: 29 | contents: read 30 | issues: write 31 | pull-requests: write 32 | 33 | jobs: 34 | welcome: 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v4 38 | - uses: actions/github-script@v7 39 | with: 40 | script: | 41 | const script = require('./\.github/workflows/scripts/labeler.js') 42 | script({github, context}) 43 | -------------------------------------------------------------------------------- /.github/workflows/scripts/auto-assignment.js: -------------------------------------------------------------------------------- 1 | /** Automatically assign issues and PRs to users in the `assigneesList` 2 | * on a rotating basis. 3 | 4 | @param {!object} 5 | GitHub objects can call GitHub APIs using their built-in library functions. 6 | The context object contains issue and PR details. 7 | */ 8 | 9 | module.exports = async ({ github, context }) => { 10 | let issueNumber; 11 | let assigneesList; 12 | // Is this an issue? If so, assign the issue number. Otherwise, assign the PR number. 13 | if (context.payload.issue) { 14 | //assignee List for issues. 15 | assigneesList = ["SuryanarayanaY", "sachinprasadhs"]; 16 | issueNumber = context.payload.issue.number; 17 | } else { 18 | //assignee List for PRs. 19 | assigneesList = [mattdangerw]; 20 | issueNumber = context.payload.number; 21 | } 22 | console.log("assignee list", assigneesList); 23 | console.log("entered auto assignment for this issue: ", issueNumber); 24 | if (!assigneesList.length) { 25 | console.log("No assignees found for this repo."); 26 | return; 27 | } 28 | let noOfAssignees = assigneesList.length; 29 | let selection = issueNumber % noOfAssignees; 30 | let assigneeForIssue = assigneesList[selection]; 31 | 32 | console.log( 33 | "issue Number = ", 34 | issueNumber + " , assigning to: ", 35 | assigneeForIssue 36 | ); 37 | return github.rest.issues.addAssignees({ 38 | issue_number: context.issue.number, 39 | owner: context.repo.owner, 40 | repo: context.repo.repo, 41 | assignees: [assigneeForIssue], 42 | }); 43 | }; 44 | -------------------------------------------------------------------------------- /keras_hub/src/models/xlm_roberta/xlm_roberta_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import pytest 4 | 5 | from keras_hub.src.models.xlm_roberta.xlm_roberta_tokenizer import ( 6 | XLMRobertaTokenizer, 7 | ) 8 | from keras_hub.src.tests.test_case import TestCase 9 | 10 | 11 | class XLMRobertaTokenizerTest(TestCase): 12 | def setUp(self): 13 | self.init_kwargs = { 14 | # Generated using create_xlm_roberta_test_proto.py 15 | "proto": os.path.join( 16 | self.get_test_data_dir(), "xlm_roberta_test_vocab.spm" 17 | ) 18 | } 19 | self.input_data = ["the quick brown fox", "the earth is round"] 20 | 21 | def test_tokenizer_basics(self): 22 | self.run_preprocessing_layer_test( 23 | cls=XLMRobertaTokenizer, 24 | init_kwargs=self.init_kwargs, 25 | input_data=self.input_data, 26 | expected_output=[[6, 11, 7, 9], [6, 8, 10, 12]], 27 | ) 28 | 29 | @pytest.mark.large 30 | def test_smallest_preset(self): 31 | self.run_preset_test( 32 | cls=XLMRobertaTokenizer, 33 | preset="xlm_roberta_base_multi", 34 | input_data=["The quick brown fox."], 35 | expected_output=[[581, 63773, 119455, 6, 147797, 5]], 36 | ) 37 | 38 | @pytest.mark.extra_large 39 | def test_all_presets(self): 40 | for preset in XLMRobertaTokenizer.presets: 41 | self.run_preset_test( 42 | cls=XLMRobertaTokenizer, 43 | preset=preset, 44 | input_data=self.input_data, 45 | ) 46 | -------------------------------------------------------------------------------- /keras_hub/src/models/mit/mit_backbone_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.mit.mit_backbone import MiTBackbone 5 | from keras_hub.src.tests.test_case import TestCase 6 | 7 | 8 | class MiTBackboneTest(TestCase): 9 | def setUp(self): 10 | self.init_kwargs = { 11 | "depths": [2, 2], 12 | "image_shape": (32, 32, 3), 13 | "hidden_dims": [4, 8], 14 | "num_layers": 2, 15 | "blockwise_num_heads": [1, 2], 16 | "blockwise_sr_ratios": [8, 4], 17 | "max_drop_path_rate": 0.1, 18 | "patch_sizes": [7, 3], 19 | "strides": [4, 2], 20 | } 21 | self.input_size = 32 22 | self.input_data = np.ones( 23 | (2, self.input_size, self.input_size, 3), dtype="float32" 24 | ) 25 | 26 | def test_backbone_basics(self): 27 | self.run_vision_backbone_test( 28 | cls=MiTBackbone, 29 | init_kwargs=self.init_kwargs, 30 | input_data=self.input_data, 31 | expected_output_shape=(2, 4, 4, 8), 32 | expected_pyramid_output_keys=["P1", "P2"], 33 | expected_pyramid_image_sizes=[(8, 8), (4, 4)], 34 | run_quantization_check=False, 35 | run_mixed_precision_check=False, 36 | run_data_format_check=False, 37 | ) 38 | 39 | @pytest.mark.large 40 | def test_saved_model(self): 41 | self.run_model_saving_test( 42 | cls=MiTBackbone, 43 | init_kwargs=self.init_kwargs, 44 | input_data=self.input_data, 45 | ) 46 | -------------------------------------------------------------------------------- /keras_hub/src/models/mit/mix_transformer_backbone_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.mit.mit_backbone import MiTBackbone 5 | from keras_hub.src.tests.test_case import TestCase 6 | 7 | 8 | class MiTBackboneTest(TestCase): 9 | def setUp(self): 10 | self.init_kwargs = { 11 | "depths": [2, 2], 12 | "image_shape": (32, 32, 3), 13 | "hidden_dims": [4, 8], 14 | "num_layers": 2, 15 | "blockwise_num_heads": [1, 2], 16 | "blockwise_sr_ratios": [8, 4], 17 | "max_drop_path_rate": 0.1, 18 | "patch_sizes": [7, 3], 19 | "strides": [4, 2], 20 | } 21 | self.input_size = 32 22 | self.input_data = np.ones( 23 | (2, self.input_size, self.input_size, 3), dtype="float32" 24 | ) 25 | 26 | def test_backbone_basics(self): 27 | self.run_vision_backbone_test( 28 | cls=MiTBackbone, 29 | init_kwargs=self.init_kwargs, 30 | input_data=self.input_data, 31 | expected_output_shape=(2, 4, 4, 8), 32 | expected_pyramid_output_keys=["P1", "P2"], 33 | expected_pyramid_image_sizes=[(8, 8), (4, 4)], 34 | run_quantization_check=False, 35 | run_mixed_precision_check=False, 36 | run_data_format_check=False, 37 | ) 38 | 39 | @pytest.mark.large 40 | def test_saved_model(self): 41 | self.run_model_saving_test( 42 | cls=MiTBackbone, 43 | init_kwargs=self.init_kwargs, 44 | input_data=self.input_data, 45 | ) 46 | -------------------------------------------------------------------------------- /.github/workflows/publish-to-pypi.yml: -------------------------------------------------------------------------------- 1 | name: Publish to PyPI 2 | 3 | on: push 4 | 5 | permissions: 6 | contents: read 7 | 8 | jobs: 9 | build-and-publish: 10 | name: Build and publish to PyPI 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v4 14 | - name: Set up Python 15 | uses: actions/setup-python@v5 16 | with: 17 | python-version: 3.9 18 | - name: Get pip cache dir 19 | id: pip-cache 20 | run: | 21 | python -m pip install --upgrade pip setuptools 22 | echo "::set-output name=dir::$(pip cache dir)" 23 | - name: pip cache 24 | uses: actions/cache@v4 25 | with: 26 | path: ${{ steps.pip-cache.outputs.dir }} 27 | key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }} 28 | restore-keys: | 29 | ${{ runner.os }}-pip- 30 | - name: Install dependencies 31 | run: | 32 | pip install -r requirements.txt --progress-bar off 33 | - name: Build a binary wheel and a source tarball 34 | run: >- 35 | python pip_build.py 36 | - name: Publish KerasHub to PyPI 37 | if: startsWith(github.ref, 'refs/tags') 38 | uses: pypa/gh-action-pypi-publish@release/v1 39 | with: 40 | password: ${{ secrets.PYPI_API_TOKEN_HUB }} 41 | verbose: true 42 | - name: Publish KerasNLP to PyPI 43 | if: startsWith(github.ref, 'refs/tags') 44 | uses: pypa/gh-action-pypi-publish@release/v1 45 | with: 46 | packages-dir: keras_nlp/dist/ 47 | password: ${{ secrets.PYPI_API_TOKEN }} 48 | verbose: true 49 | -------------------------------------------------------------------------------- /keras_hub/src/api_export.py: -------------------------------------------------------------------------------- 1 | import types 2 | 3 | from keras.saving import register_keras_serializable 4 | 5 | try: 6 | import namex 7 | except ImportError: 8 | namex = None 9 | 10 | 11 | def maybe_register_serializable(path, symbol): 12 | if isinstance(path, (list, tuple)): 13 | # If we have multiple export names, actually make sure to register these 14 | # first. This makes sure we have a backward compat mapping of old 15 | # serialized names to new class. 16 | for name in path: 17 | name = name.split(".")[-1] 18 | register_keras_serializable(package="keras_nlp", name=name)(symbol) 19 | register_keras_serializable(package="keras_hub", name=name)(symbol) 20 | if isinstance(symbol, types.FunctionType) or hasattr(symbol, "get_config"): 21 | # We register twice, first with keras_nlp, second with keras_hub, 22 | # so loading still works for classes saved as "keras_nlp". 23 | register_keras_serializable(package="keras_nlp")(symbol) 24 | register_keras_serializable(package="keras_hub")(symbol) 25 | 26 | 27 | if namex: 28 | 29 | class keras_hub_export(namex.export): 30 | def __init__(self, path): 31 | super().__init__(package="keras_hub", path=path) 32 | 33 | def __call__(self, symbol): 34 | maybe_register_serializable(self.path, symbol) 35 | return super().__call__(symbol) 36 | 37 | else: 38 | 39 | class keras_hub_export: 40 | def __init__(self, path): 41 | self.path = path 42 | 43 | def __call__(self, symbol): 44 | maybe_register_serializable(self.path, symbol) 45 | return symbol 46 | -------------------------------------------------------------------------------- /keras_hub/src/models/llama3/llama3_causal_lm.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.llama3.llama3_backbone import Llama3Backbone 3 | from keras_hub.src.models.llama3.llama3_causal_lm_preprocessor import ( 4 | Llama3CausalLMPreprocessor, 5 | ) 6 | from keras_hub.src.models.llama.llama_causal_lm import LlamaCausalLM 7 | 8 | 9 | @keras_hub_export("keras_hub.models.Llama3CausalLM") 10 | class Llama3CausalLM(LlamaCausalLM): 11 | """An end-to-end Llama 3 model for causal language modeling. 12 | 13 | A causal language model (LM) predicts the next token based on previous 14 | tokens. This task setup can be used to train the model unsupervised on 15 | plain text input, or to autoregressively generate plain text similar to 16 | the data used for training. This task can be used for pre-training or 17 | fine-tuning a LLaMA 3 model, simply by calling `fit()`. 18 | 19 | This model has a `generate()` method, which generates text based on a 20 | prompt. The generation strategy used is controlled by an additional 21 | `sampler` argument on `compile()`. You can recompile the model with 22 | different `keras_hub.samplers` objects to control the generation. By 23 | default, `"top_k"` sampling will be used. 24 | 25 | Args: 26 | backbone: A `keras_hub.models.Llama3Backbone` instance. 27 | preprocessor: A `keras_hub.models.Llama3CausalLMPreprocessor` or `None`. 28 | If `None`, this model will not apply preprocessing, and inputs 29 | should be preprocessed before calling the model. 30 | """ 31 | 32 | backbone_cls = Llama3Backbone 33 | preprocessor_cls = Llama3CausalLMPreprocessor 34 | -------------------------------------------------------------------------------- /keras_hub/src/models/mobilenet/mobilenet_backbone_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.mobilenet.mobilenet_backbone import MobileNetBackbone 5 | from keras_hub.src.tests.test_case import TestCase 6 | 7 | 8 | class MobileNetBackboneTest(TestCase): 9 | def setUp(self): 10 | self.init_kwargs = { 11 | "stackwise_expansion": [1, 4, 6], 12 | "stackwise_num_filters": [4, 8, 16], 13 | "stackwise_kernel_size": [3, 3, 5], 14 | "stackwise_num_strides": [2, 2, 1], 15 | "stackwise_se_ratio": [0.25, None, 0.25], 16 | "stackwise_activation": ["relu", "relu", "hard_swish"], 17 | "output_num_filters": 1280, 18 | "input_activation": "hard_swish", 19 | "output_activation": "hard_swish", 20 | "inverted_res_block": True, 21 | "input_num_filters": 16, 22 | "image_shape": (224, 224, 3), 23 | "depth_multiplier": 1, 24 | } 25 | self.input_data = np.ones((2, 224, 224, 3), dtype="float32") 26 | 27 | def test_backbone_basics(self): 28 | self.run_vision_backbone_test( 29 | cls=MobileNetBackbone, 30 | init_kwargs=self.init_kwargs, 31 | input_data=self.input_data, 32 | expected_output_shape=(2, 28, 28, 96), 33 | run_mixed_precision_check=False, 34 | run_data_format_check=False, 35 | ) 36 | 37 | @pytest.mark.large 38 | def test_saved_model(self): 39 | self.run_model_saving_test( 40 | cls=MobileNetBackbone, 41 | init_kwargs=self.init_kwargs, 42 | input_data=self.input_data, 43 | ) 44 | -------------------------------------------------------------------------------- /tools/sentencepiece_testing/create_phi3_test_proto.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | 3 | import sentencepiece.sentencepiece_model_pb2 as sp_pb2 4 | 5 | from tools.sentencepiece_testing.utils import train_sentencepiece 6 | 7 | ADDED_TOKENS = [ 8 | "<|endoftext|>", 9 | "<|assistant|>", 10 | "<|system|>", 11 | "<|end|>", 12 | "<|user|>", 13 | ] 14 | 15 | 16 | def add_added_tokens(filename): 17 | with open( 18 | pathlib.Path(__file__).parent.parent.parent 19 | / "keras_hub" 20 | / "src" 21 | / "tests" 22 | / "test_data" 23 | / filename, 24 | mode="rb", 25 | ) as sp_model_file: 26 | model_proto = sp_pb2.ModelProto() 27 | model_proto.ParseFromString(sp_model_file.read()) 28 | for token in ADDED_TOKENS: 29 | new_token = sp_pb2.ModelProto().SentencePiece() 30 | new_token.piece = token 31 | new_token.score = 0.0 32 | new_token.type = 4 # user defined symbols. 33 | model_proto.pieces.append(new_token) 34 | with open( 35 | pathlib.Path(__file__).parent.parent.parent 36 | / "keras_hub" 37 | / "src" 38 | / "tests" 39 | / "test_data" 40 | / filename, 41 | mode="wb", 42 | ) as f: 43 | f.write(model_proto.SerializeToString()) 44 | 45 | 46 | def main(): 47 | train_sentencepiece( 48 | ["the fox on the table", "the fox on the earth"], 49 | "phi3_test_vocab.spm", 50 | vocab_size=15, 51 | model_type="bpe", # BPE 52 | pad_id=-1, 53 | unk_id=0, 54 | bos_id=1, 55 | eos_id=2, 56 | ) 57 | add_added_tokens("phi3_test_vocab.spm") 58 | 59 | 60 | if __name__ == "__main__": 61 | main() 62 | -------------------------------------------------------------------------------- /keras_hub/src/models/sam/sam_mask_decoder_test.py: -------------------------------------------------------------------------------- 1 | from keras import random 2 | 3 | from keras_hub.src.models.sam.sam_mask_decoder import SAMMaskDecoder 4 | from keras_hub.src.tests.test_case import TestCase 5 | 6 | 7 | class SAMMaskDecoderTest(TestCase): 8 | def setUp(self): 9 | self.batch_size = 2 10 | self.image_size = 128 11 | self.init_kwargs = { 12 | "num_layers": 2, 13 | "hidden_size": 8, 14 | "intermediate_dim": 32, 15 | "num_heads": 8, 16 | "embedding_dim": 8, 17 | "num_multimask_outputs": 3, 18 | "iou_head_depth": 3, 19 | "iou_head_hidden_dim": 8, 20 | } 21 | self.inputs = { 22 | "image_embeddings": random.uniform( 23 | minval=0, maxval=1, shape=(1, 8, 8, 8) 24 | ), 25 | "prompt_sparse_embeddings": random.uniform( 26 | minval=0, maxval=1, shape=(1, 12, 8) 27 | ), 28 | "prompt_dense_embeddings": random.uniform( 29 | minval=0, maxval=1, shape=(1, 8, 8, 8) 30 | ), 31 | "prompt_dense_positional_embeddings": random.uniform( 32 | minval=0, maxval=1, shape=(1, 8, 8, 8) 33 | ), 34 | } 35 | 36 | def test_layer_basics(self): 37 | self.run_layer_test( 38 | cls=SAMMaskDecoder, 39 | init_kwargs=self.init_kwargs, 40 | input_data=self.inputs, 41 | expected_output_shape={ 42 | "masks": (1, 4, 32, 32), 43 | "iou_pred": (1, 4), 44 | }, 45 | expected_num_trainable_weights=120, 46 | run_precision_checks=False, 47 | ) 48 | -------------------------------------------------------------------------------- /keras_hub/src/models/densenet/densenet_presets.py: -------------------------------------------------------------------------------- 1 | """DenseNet preset configurations.""" 2 | 3 | backbone_presets = { 4 | "densenet_121_imagenet": { 5 | "metadata": { 6 | "description": ( 7 | "121-layer DenseNet model pre-trained on the ImageNet 1k dataset " 8 | "at a 224x224 resolution." 9 | ), 10 | "params": 7037504, 11 | "official_name": "DenseNet", 12 | "path": "densenet", 13 | "model_card": "https://arxiv.org/abs/1608.06993", 14 | }, 15 | "kaggle_handle": "kaggle://keras/densenet/keras/densenet_121_imagenet/2", 16 | }, 17 | "densenet_169_imagenet": { 18 | "metadata": { 19 | "description": ( 20 | "169-layer DenseNet model pre-trained on the ImageNet 1k dataset " 21 | "at a 224x224 resolution." 22 | ), 23 | "params": 12642880, 24 | "official_name": "DenseNet", 25 | "path": "densenet", 26 | "model_card": "https://arxiv.org/abs/1608.06993", 27 | }, 28 | "kaggle_handle": "kaggle://keras/densenet/keras/densenet_169_imagenet/2", 29 | }, 30 | "densenet_201_imagenet": { 31 | "metadata": { 32 | "description": ( 33 | "201-layer DenseNet model pre-trained on the ImageNet 1k dataset " 34 | "at a 224x224 resolution." 35 | ), 36 | "params": 18321984, 37 | "official_name": "DenseNet", 38 | "path": "densenet", 39 | "model_card": "https://arxiv.org/abs/1608.06993", 40 | }, 41 | "kaggle_handle": "kaggle://keras/densenet/keras/densenet_201_imagenet/2", 42 | }, 43 | } 44 | -------------------------------------------------------------------------------- /keras_hub/src/models/csp_darknet/csp_darknet_image_classifier_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.csp_darknet.csp_darknet_backbone import ( 5 | CSPDarkNetBackbone, 6 | ) 7 | from keras_hub.src.models.csp_darknet.csp_darknet_image_classifier import ( 8 | CSPDarkNetImageClassifier, 9 | ) 10 | from keras_hub.src.tests.test_case import TestCase 11 | 12 | 13 | class CSPDarkNetImageClassifierTest(TestCase): 14 | def setUp(self): 15 | # Setup model. 16 | self.images = np.ones((2, 16, 16, 3), dtype="float32") 17 | self.labels = [0, 3] 18 | self.backbone = CSPDarkNetBackbone( 19 | stackwise_num_filters=[2, 16, 16], 20 | stackwise_depth=[1, 3, 3, 1], 21 | block_type="basic_block", 22 | image_shape=(16, 16, 3), 23 | ) 24 | self.init_kwargs = { 25 | "backbone": self.backbone, 26 | "num_classes": 2, 27 | "activation": "softmax", 28 | } 29 | self.train_data = ( 30 | self.images, 31 | self.labels, 32 | ) 33 | 34 | def test_classifier_basics(self): 35 | pytest.skip( 36 | reason="TODO: enable after preprocessor flow is figured out" 37 | ) 38 | self.run_task_test( 39 | cls=CSPDarkNetImageClassifier, 40 | init_kwargs=self.init_kwargs, 41 | train_data=self.train_data, 42 | expected_output_shape=(2, 2), 43 | ) 44 | 45 | @pytest.mark.large 46 | def test_saved_model(self): 47 | self.run_model_saving_test( 48 | cls=CSPDarkNetImageClassifier, 49 | init_kwargs=self.init_kwargs, 50 | input_data=self.images, 51 | ) 52 | -------------------------------------------------------------------------------- /keras_hub/src/models/phi3/phi3_presets.py: -------------------------------------------------------------------------------- 1 | """Phi-3 model preset configurations.""" 2 | 3 | # Metadata for loading pretrained model weights. 4 | backbone_presets = { 5 | "phi3_mini_4k_instruct_en": { 6 | "metadata": { 7 | "description": ( 8 | "3.8 billion parameters, 32 layers, 4k context length, Phi-3 " 9 | "model. The model was trained using the Phi-3 datasets. This " 10 | "dataset includes both synthetic data and filtered publicly " 11 | "available website data, with an emphasis on high-quality and " 12 | "reasoning-dense properties." 13 | ), 14 | "params": 3821079552, 15 | "official_name": "Phi-3", 16 | "path": "phi3", 17 | "model_card": "https://huggingface.co/microsoft/Phi-3-mini-4k-instruct", 18 | }, 19 | "kaggle_handle": "kaggle://keras/phi3/keras/phi3_mini_4k_instruct_en", 20 | }, 21 | "phi3_mini_128k_instruct_en": { 22 | "metadata": { 23 | "description": ( 24 | "3.8 billion parameters, 32 layers, 128k context length, Phi-3 " 25 | "model. The model was trained using the Phi-3 datasets. This " 26 | "dataset includes both synthetic data and filtered publicly " 27 | "available website data, with an emphasis on high-quality and " 28 | "reasoning-dense properties." 29 | ), 30 | "params": 3821079552, 31 | "official_name": "Phi-3", 32 | "path": "phi3", 33 | "model_card": "https://huggingface.co/microsoft/Phi-3-mini-128k-instruct", 34 | }, 35 | "kaggle_handle": "kaggle://keras/phi3/keras/phi3_mini_128k_instruct_en", 36 | }, 37 | } 38 | -------------------------------------------------------------------------------- /keras_hub/src/models/mit/mit_image_classifier_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.mit.mit_backbone import MiTBackbone 5 | from keras_hub.src.models.mit.mit_image_classifier import MiTImageClassifier 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class MiTImageClassifierTest(TestCase): 10 | def setUp(self): 11 | # Setup model. 12 | self.images = np.ones((2, 32, 32, 3), dtype="float32") 13 | self.labels = [0, 3] 14 | self.backbone = MiTBackbone( 15 | depths=[2, 2, 2, 2], 16 | image_shape=(32, 32, 3), 17 | hidden_dims=[4, 8], 18 | num_layers=2, 19 | blockwise_num_heads=[1, 2], 20 | blockwise_sr_ratios=[8, 4], 21 | max_drop_path_rate=0.1, 22 | patch_sizes=[7, 3], 23 | strides=[4, 2], 24 | ) 25 | self.init_kwargs = { 26 | "backbone": self.backbone, 27 | "num_classes": 2, 28 | "activation": "softmax", 29 | } 30 | self.train_data = ( 31 | self.images, 32 | self.labels, 33 | ) 34 | 35 | def test_classifier_basics(self): 36 | pytest.skip( 37 | reason="TODO: enable after preprocessor flow is figured out" 38 | ) 39 | self.run_task_test( 40 | cls=MiTImageClassifier, 41 | init_kwargs=self.init_kwargs, 42 | train_data=self.train_data, 43 | expected_output_shape=(4, 4), 44 | ) 45 | 46 | @pytest.mark.large 47 | def test_saved_model(self): 48 | self.run_model_saving_test( 49 | cls=MiTImageClassifier, 50 | init_kwargs=self.init_kwargs, 51 | input_data=self.images, 52 | ) 53 | -------------------------------------------------------------------------------- /keras_hub/src/layers/modeling/transformer_layer_utils_test.py: -------------------------------------------------------------------------------- 1 | from keras import ops 2 | from keras import random 3 | 4 | import keras_hub.src.layers.modeling.transformer_layer_utils as utils 5 | from keras_hub.src.tests.test_case import TestCase 6 | 7 | 8 | class TransformerLayerUtilsTest(TestCase): 9 | def test_compute_causal_mask(self): 10 | mask = utils.compute_causal_mask(1, 2, 2) 11 | self.assertAllEqual(mask, [[[1, 0], [1, 1]]]) 12 | 13 | def test_merge_padding_and_attention_mask(self): 14 | padding_mask = ops.array([[1, 1, 0]]) 15 | attention_mask = ops.array([[[0, 0, 1], [0, 1, 0], [1, 0, 0]]]) 16 | inputs = random.uniform(shape=[1, 3, 2]) 17 | merged_mask = utils.merge_padding_and_attention_mask( 18 | inputs, 19 | padding_mask, 20 | attention_mask, 21 | ) 22 | self.assertAllEqual(merged_mask, [[[0, 0, 0], [0, 1, 0], [1, 0, 0]]]) 23 | 24 | def test_bad_mask_shapes(self): 25 | with self.assertRaises(ValueError): 26 | padding_mask = ops.array([[[1, 1, 0], [1, 0, 0]]]) 27 | attention_mask = ops.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]]) 28 | inputs = random.uniform(shape=[1, 3, 2]) 29 | utils.merge_padding_and_attention_mask( 30 | inputs, 31 | padding_mask, 32 | attention_mask, 33 | ) 34 | 35 | with self.assertRaises(ValueError): 36 | padding_mask = ops.array([[1, 1, 0]]) 37 | attention_mask = ops.array([[0, 0, 1], [1, 0, 0]]) 38 | inputs = random.uniform(shape=[1, 3, 2]) 39 | utils.merge_padding_and_attention_mask( 40 | inputs, 41 | padding_mask, 42 | attention_mask, 43 | ) 44 | -------------------------------------------------------------------------------- /.kokoro/github/ubuntu/gpu/build.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | 3 | export KAGGLE_KEY="$(cat ${KOKORO_KEYSTORE_DIR}/73361_keras_kaggle_secret_key)" 4 | export KAGGLE_USERNAME="$(cat ${KOKORO_KEYSTORE_DIR}/73361_keras_kaggle_username)" 5 | 6 | if [[ -z "${KAGGLE_KEY}" ]]; then 7 | echo "KAGGLE_KEY is NOT set" 8 | exit 1 9 | fi 10 | 11 | if [[ -z "${KAGGLE_USERNAME}" ]]; then 12 | echo "KAGGLE_USERNAME is NOT set" 13 | exit 1 14 | fi 15 | 16 | set -x 17 | cd "${KOKORO_ROOT}/" 18 | 19 | PYTHON_BINARY="/usr/bin/python3.9" 20 | 21 | "${PYTHON_BINARY}" -m venv venv 22 | source venv/bin/activate 23 | # Check the python version 24 | python --version 25 | python3 --version 26 | 27 | export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:" 28 | # Check cuda 29 | nvidia-smi 30 | nvcc --version 31 | 32 | cd "src/github/keras-hub" 33 | pip install -U pip setuptools psutil 34 | 35 | if [ "$KERAS_BACKEND" == "tensorflow" ] 36 | then 37 | echo "TensorFlow backend detected." 38 | pip install -r requirements-tensorflow-cuda.txt --progress-bar off \ 39 | --timeout 1000 40 | 41 | elif [ "$KERAS_BACKEND" == "jax" ] 42 | then 43 | echo "JAX backend detected." 44 | pip install -r requirements-jax-cuda.txt --progress-bar off --timeout 1000 45 | 46 | elif [ "$KERAS_BACKEND" == "torch" ] 47 | then 48 | echo "PyTorch backend detected." 49 | pip install -r requirements-torch-cuda.txt --progress-bar off --timeout 1000 50 | fi 51 | 52 | pip install --no-deps -e "." --progress-bar off 53 | pip install huggingface_hub 54 | 55 | # Run Extra Large Tests for Continuous builds 56 | if [ "${RUN_XLARGE:-0}" == "1" ] 57 | then 58 | pytest keras_hub --check_gpu --run_large --run_extra_large \ 59 | --cov=keras-hub 60 | else 61 | pytest keras_hub --check_gpu --run_large \ 62 | --cov=keras-hub 63 | fi 64 | -------------------------------------------------------------------------------- /keras_hub/src/models/t5/t5_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import pytest 4 | 5 | from keras_hub.src.models.t5.t5_tokenizer import T5Tokenizer 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class T5TokenizerTest(TestCase): 10 | def setUp(self): 11 | self.init_kwargs = { 12 | # Generated using create_t5_test_proto.py 13 | "proto": os.path.join(self.get_test_data_dir(), "t5_test_vocab.spm") 14 | } 15 | self.input_data = ["the quick brown fox", "the earth is round"] 16 | 17 | def test_tokenizer_basics(self): 18 | self.run_preprocessing_layer_test( 19 | cls=T5Tokenizer, 20 | init_kwargs=self.init_kwargs, 21 | input_data=self.input_data, 22 | expected_output=[[4, 9, 5, 7], [4, 6, 8, 10]], 23 | ) 24 | 25 | def test_errors_missing_special_tokens(self): 26 | with self.assertRaises(ValueError): 27 | T5Tokenizer( 28 | # Generated using create_no_special_token_proto.py 29 | proto=os.path.join( 30 | self.get_test_data_dir(), "no_special_token_vocab.spm" 31 | ) 32 | ) 33 | 34 | @pytest.mark.large 35 | def test_smallest_preset(self): 36 | for preset in T5Tokenizer.presets: 37 | self.run_preset_test( 38 | cls=T5Tokenizer, 39 | preset=preset, 40 | input_data=["The quick brown fox."], 41 | expected_output=[[37, 1704, 4216, 3, 20400, 5]], 42 | ) 43 | 44 | @pytest.mark.extra_large 45 | def test_all_presets(self): 46 | for preset in T5Tokenizer.presets: 47 | self.run_preset_test( 48 | cls=T5Tokenizer, 49 | preset=preset, 50 | input_data=self.input_data, 51 | ) 52 | -------------------------------------------------------------------------------- /keras_hub/src/models/f_net/f_net_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import pytest 4 | 5 | from keras_hub.src.models.f_net.f_net_tokenizer import FNetTokenizer 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class FNetTokenizerTest(TestCase): 10 | def setUp(self): 11 | self.init_kwargs = { 12 | # Generated using create_f_net_test_proto.py 13 | "proto": os.path.join( 14 | self.get_test_data_dir(), "f_net_test_vocab.spm" 15 | ) 16 | } 17 | self.input_data = ["the quick brown fox", "the earth is round"] 18 | 19 | def test_tokenizer_basics(self): 20 | self.run_preprocessing_layer_test( 21 | cls=FNetTokenizer, 22 | init_kwargs=self.init_kwargs, 23 | input_data=self.input_data, 24 | expected_output=[[5, 10, 6, 8], [5, 7, 9, 11]], 25 | ) 26 | 27 | def test_errors_missing_special_tokens(self): 28 | with self.assertRaises(ValueError): 29 | FNetTokenizer( 30 | # Generated using create_no_special_token_proto.py 31 | proto=os.path.join( 32 | self.get_test_data_dir(), "no_special_token_vocab.spm" 33 | ) 34 | ) 35 | 36 | @pytest.mark.large 37 | def test_smallest_preset(self): 38 | self.run_preset_test( 39 | cls=FNetTokenizer, 40 | preset="f_net_base_en", 41 | input_data=["The quick brown fox."], 42 | expected_output=[[97, 1467, 5187, 26, 2521, 16678]], 43 | ) 44 | 45 | @pytest.mark.extra_large 46 | def test_all_presets(self): 47 | for preset in FNetTokenizer.presets: 48 | self.run_preset_test( 49 | cls=FNetTokenizer, 50 | preset=preset, 51 | input_data=self.input_data, 52 | ) 53 | -------------------------------------------------------------------------------- /keras_hub/src/models/llama/llama_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import pytest 4 | 5 | from keras_hub.src.models.llama.llama_tokenizer import LlamaTokenizer 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class LlamaTokenizerTest(TestCase): 10 | def setUp(self): 11 | self.init_kwargs = { 12 | # Generated using create_llama_test_proto.py 13 | "proto": os.path.join( 14 | self.get_test_data_dir(), "llama_test_vocab.spm" 15 | ) 16 | } 17 | self.input_data = ["the quick brown fox", "the earth is round"] 18 | 19 | def test_tokenizer_basics(self): 20 | self.run_preprocessing_layer_test( 21 | cls=LlamaTokenizer, 22 | init_kwargs=self.init_kwargs, 23 | input_data=self.input_data, 24 | expected_output=[[3, 8, 4, 6], [3, 5, 7, 9]], 25 | ) 26 | 27 | def test_errors_missing_special_tokens(self): 28 | with self.assertRaises(ValueError): 29 | LlamaTokenizer( 30 | # Generated using create_no_special_token_proto.py 31 | proto=os.path.join( 32 | self.get_test_data_dir(), "no_special_token_vocab.spm" 33 | ) 34 | ) 35 | 36 | @pytest.mark.large 37 | def test_smallest_preset(self): 38 | self.run_preset_test( 39 | cls=LlamaTokenizer, 40 | preset="llama2_7b_en", 41 | input_data=["The quick brown fox."], 42 | expected_output=[[450, 4996, 17354, 1701, 29916, 29889]], 43 | ) 44 | 45 | @pytest.mark.extra_large 46 | def test_all_presets(self): 47 | for preset in LlamaTokenizer.presets: 48 | self.run_preset_test( 49 | cls=LlamaTokenizer, 50 | preset=preset, 51 | input_data=self.input_data, 52 | ) 53 | -------------------------------------------------------------------------------- /keras_hub/src/models/albert/albert_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import pytest 4 | 5 | from keras_hub.src.models.albert.albert_tokenizer import AlbertTokenizer 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class AlbertTokenizerTest(TestCase): 10 | def setUp(self): 11 | self.init_kwargs = { 12 | # Generated using create_albert_test_proto.py 13 | "proto": os.path.join( 14 | self.get_test_data_dir(), "albert_test_vocab.spm" 15 | ) 16 | } 17 | self.input_data = ["the quick brown fox", "the earth is round"] 18 | 19 | def test_tokenizer_basics(self): 20 | self.run_preprocessing_layer_test( 21 | cls=AlbertTokenizer, 22 | init_kwargs=self.init_kwargs, 23 | input_data=self.input_data, 24 | expected_output=[[5, 10, 6, 8], [5, 7, 9, 11]], 25 | ) 26 | 27 | def test_errors_missing_special_tokens(self): 28 | with self.assertRaises(ValueError): 29 | AlbertTokenizer( 30 | # Generated using create_no_special_token_proto.py 31 | proto=os.path.join( 32 | self.get_test_data_dir(), "no_special_token_vocab.spm" 33 | ) 34 | ) 35 | 36 | @pytest.mark.large 37 | def test_smallest_preset(self): 38 | self.run_preset_test( 39 | cls=AlbertTokenizer, 40 | preset="albert_base_en_uncased", 41 | input_data=["The quick brown fox."], 42 | expected_output=[[13, 1, 438, 2231, 886, 2385, 9]], 43 | ) 44 | 45 | @pytest.mark.extra_large 46 | def test_all_presets(self): 47 | for preset in AlbertTokenizer.presets: 48 | self.run_preset_test( 49 | cls=AlbertTokenizer, 50 | preset=preset, 51 | input_data=self.input_data, 52 | ) 53 | -------------------------------------------------------------------------------- /.github/workflows/nightly.yml: -------------------------------------------------------------------------------- 1 | name: Nightly 2 | 3 | on: 4 | workflow_dispatch: # To Generate wheels on demand outside of schedule. 5 | schedule: 6 | - cron: '0 3 * * *' # run at 3 AM UTC / 8 PM PDT 7 | 8 | permissions: 9 | contents: read 10 | 11 | jobs: 12 | run-test-for-nightly: 13 | uses: ./.github/workflows/actions.yml 14 | nightly: 15 | name: Build Wheel file and upload 16 | needs: [run-test-for-nightly] 17 | runs-on: ubuntu-latest 18 | steps: 19 | - uses: actions/checkout@v4 20 | - name: Set up Python 21 | uses: actions/setup-python@v5 22 | with: 23 | python-version: 3.9 24 | - name: Get pip cache dir 25 | id: pip-cache 26 | run: | 27 | python -m pip install --upgrade pip setuptools 28 | echo "::set-output name=dir::$(pip cache dir)" 29 | - name: pip cache 30 | uses: actions/cache@v4 31 | with: 32 | path: ${{ steps.pip-cache.outputs.dir }} 33 | key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }} 34 | restore-keys: | 35 | ${{ runner.os }}-pip- 36 | - name: Install dependencies 37 | run: | 38 | python -m pip install --upgrade pip setuptools 39 | pip install twine 40 | pip install -r requirements.txt --progress-bar off 41 | - name: Build wheel file 42 | run: | 43 | python pip_build.py --nightly 44 | - name: Publish KerasHub Nightly to PyPI 45 | uses: pypa/gh-action-pypi-publish@release/v1 46 | with: 47 | password: ${{ secrets.PYPI_NIGHTLY_API_TOKEN_HUB }} 48 | verbose: true 49 | - name: Publish KerasNLP Nightly to PyPI 50 | uses: pypa/gh-action-pypi-publish@release/v1 51 | with: 52 | packages-dir: keras_nlp/dist/ 53 | password: ${{ secrets.PYPI_NIGHTLY_API_TOKEN }} 54 | verbose: true 55 | -------------------------------------------------------------------------------- /keras_hub/src/models/mistral/mistral_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import pytest 4 | 5 | from keras_hub.src.models.mistral.mistral_tokenizer import MistralTokenizer 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class MistralTokenizerTest(TestCase): 10 | def setUp(self): 11 | self.init_kwargs = { 12 | # Generated using create_mistral_test_proto.py 13 | "proto": os.path.join( 14 | self.get_test_data_dir(), "mistral_test_vocab.spm" 15 | ) 16 | } 17 | self.input_data = ["the quick brown fox", "the earth is round"] 18 | 19 | def test_tokenizer_basics(self): 20 | self.run_preprocessing_layer_test( 21 | cls=MistralTokenizer, 22 | init_kwargs=self.init_kwargs, 23 | input_data=self.input_data, 24 | expected_output=[[3, 8, 4, 6], [3, 5, 7, 9]], 25 | ) 26 | 27 | def test_errors_missing_special_tokens(self): 28 | with self.assertRaises(ValueError): 29 | MistralTokenizer( 30 | # Generated using create_no_special_token_proto.py 31 | proto=os.path.join( 32 | self.get_test_data_dir(), "no_special_token_vocab.spm" 33 | ) 34 | ) 35 | 36 | @pytest.mark.large 37 | def test_smallest_preset(self): 38 | self.run_preset_test( 39 | cls=MistralTokenizer, 40 | preset="mistral_7b_en", 41 | input_data=["The quick brown fox."], 42 | expected_output=[[415, 2936, 9060, 285, 1142, 28723]], 43 | ) 44 | 45 | @pytest.mark.extra_large 46 | def test_all_presets(self): 47 | for preset in MistralTokenizer.presets: 48 | self.run_preset_test( 49 | cls=MistralTokenizer, 50 | preset=preset, 51 | input_data=self.input_data, 52 | ) 53 | -------------------------------------------------------------------------------- /keras_hub/src/models/masked_lm_preprocessor_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.bert.bert_masked_lm_preprocessor import ( 4 | BertMaskedLMPreprocessor, 5 | ) 6 | from keras_hub.src.models.gpt2.gpt2_tokenizer import GPT2Tokenizer 7 | from keras_hub.src.models.masked_lm_preprocessor import MaskedLMPreprocessor 8 | from keras_hub.src.tests.test_case import TestCase 9 | 10 | 11 | class TestMaskedLMPreprocessor(TestCase): 12 | def test_preset_accessors(self): 13 | bert_presets = set(BertMaskedLMPreprocessor.presets.keys()) 14 | gpt2_presets = set(GPT2Tokenizer.presets.keys()) 15 | all_presets = set(MaskedLMPreprocessor.presets.keys()) 16 | self.assertTrue(bert_presets.issubset(all_presets)) 17 | self.assertTrue(gpt2_presets.isdisjoint(all_presets)) 18 | 19 | @pytest.mark.large 20 | def test_from_preset(self): 21 | self.assertIsInstance( 22 | MaskedLMPreprocessor.from_preset("bert_tiny_en_uncased"), 23 | BertMaskedLMPreprocessor, 24 | ) 25 | self.assertIsInstance( 26 | BertMaskedLMPreprocessor.from_preset("bert_tiny_en_uncased"), 27 | BertMaskedLMPreprocessor, 28 | ) 29 | 30 | @pytest.mark.large 31 | def test_from_preset_with_sequence_length(self): 32 | preprocessor = MaskedLMPreprocessor.from_preset( 33 | "bert_tiny_en_uncased", sequence_length=16 34 | ) 35 | self.assertEqual(preprocessor.sequence_length, 16) 36 | 37 | @pytest.mark.large 38 | def test_from_preset_errors(self): 39 | with self.assertRaises(ValueError): 40 | # No loading on an incorrect class. 41 | BertMaskedLMPreprocessor.from_preset("gpt2_base_en") 42 | with self.assertRaises(ValueError): 43 | # No loading on a non-keras model. 44 | BertMaskedLMPreprocessor.from_preset("hf://spacy/en_core_web_sm") 45 | -------------------------------------------------------------------------------- /keras_hub/src/models/opt/opt_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.opt.opt_tokenizer import OPTTokenizer 4 | from keras_hub.src.tests.test_case import TestCase 5 | 6 | 7 | class OPTTokenizerTest(TestCase): 8 | def setUp(self): 9 | self.vocab = ["", "", "air", "Ġair", "plane", "Ġat", "port"] 10 | self.vocab = dict([(token, i) for i, token in enumerate(self.vocab)]) 11 | self.merges = ["Ġ a", "Ġ t", "Ġ i", "Ġ b", "a i", "p l", "n e"] 12 | self.merges += ["Ġa t", "p o", "r t", "Ġt h", "ai r", "pl a", "po rt"] 13 | self.merges += ["Ġai r", "Ġa i", "pla ne"] 14 | self.init_kwargs = {"vocabulary": self.vocab, "merges": self.merges} 15 | self.input_data = [ 16 | " airplane at airport", 17 | " airplane airport", 18 | ] 19 | 20 | def test_tokenizer_basics(self): 21 | self.run_preprocessing_layer_test( 22 | cls=OPTTokenizer, 23 | init_kwargs=self.init_kwargs, 24 | input_data=self.input_data, 25 | expected_output=[[3, 4, 5, 3, 6, 1], [3, 4, 3, 6]], 26 | ) 27 | 28 | def test_errors_missing_special_tokens(self): 29 | with self.assertRaises(ValueError): 30 | OPTTokenizer(vocabulary=["a", "b", "c"], merges=[]) 31 | 32 | @pytest.mark.large 33 | def test_smallest_preset(self): 34 | self.run_preset_test( 35 | cls=OPTTokenizer, 36 | preset="opt_125m_en", 37 | input_data=["The quick brown fox."], 38 | expected_output=[[133, 2119, 6219, 23602, 4]], 39 | ) 40 | 41 | @pytest.mark.extra_large 42 | def test_all_presets(self): 43 | for preset in OPTTokenizer.presets: 44 | self.run_preset_test( 45 | cls=OPTTokenizer, 46 | preset=preset, 47 | input_data=self.input_data, 48 | ) 49 | -------------------------------------------------------------------------------- /keras_hub/src/models/distil_bert/distil_bert_presets.py: -------------------------------------------------------------------------------- 1 | """DistilBERT model preset configurations.""" 2 | 3 | backbone_presets = { 4 | "distil_bert_base_en_uncased": { 5 | "metadata": { 6 | "description": ( 7 | "6-layer DistilBERT model where all input is lowercased. " 8 | "Trained on English Wikipedia + BooksCorpus using BERT as the " 9 | "teacher model." 10 | ), 11 | "params": 66362880, 12 | "official_name": "DistilBERT", 13 | "path": "distil_bert", 14 | "model_card": "https://huggingface.co/distilbert-base-uncased", 15 | }, 16 | "kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_en_uncased/2", 17 | }, 18 | "distil_bert_base_en": { 19 | "metadata": { 20 | "description": ( 21 | "6-layer DistilBERT model where case is maintained. " 22 | "Trained on English Wikipedia + BooksCorpus using BERT as the " 23 | "teacher model." 24 | ), 25 | "params": 65190912, 26 | "official_name": "DistilBERT", 27 | "path": "distil_bert", 28 | "model_card": "https://huggingface.co/distilbert-base-cased", 29 | }, 30 | "kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_en/2", 31 | }, 32 | "distil_bert_base_multi": { 33 | "metadata": { 34 | "description": ( 35 | "6-layer DistilBERT model where case is maintained. Trained on Wikipedias of 104 languages" 36 | ), 37 | "params": 134734080, 38 | "official_name": "DistilBERT", 39 | "path": "distil_bert", 40 | "model_card": "https://huggingface.co/distilbert-base-multilingual-cased", 41 | }, 42 | "kaggle_handle": "kaggle://keras/distil_bert/keras/distil_bert_base_multi/2", 43 | }, 44 | } 45 | -------------------------------------------------------------------------------- /keras_hub/src/models/seq_2_seq_lm_preprocessor_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.bart.bart_seq_2_seq_lm_preprocessor import ( 4 | BartSeq2SeqLMPreprocessor, 5 | ) 6 | from keras_hub.src.models.bert.bert_tokenizer import BertTokenizer 7 | from keras_hub.src.models.seq_2_seq_lm_preprocessor import Seq2SeqLMPreprocessor 8 | from keras_hub.src.tests.test_case import TestCase 9 | 10 | 11 | class TestSeq2SeqLMPreprocessor(TestCase): 12 | def test_preset_accessors(self): 13 | bert_presets = set(BertTokenizer.presets.keys()) 14 | bart_presets = set(BartSeq2SeqLMPreprocessor.presets.keys()) 15 | all_presets = set(Seq2SeqLMPreprocessor.presets.keys()) 16 | self.assertTrue(bert_presets.isdisjoint(all_presets)) 17 | self.assertTrue(bart_presets.issubset(all_presets)) 18 | 19 | @pytest.mark.large 20 | def test_from_preset(self): 21 | self.assertIsInstance( 22 | Seq2SeqLMPreprocessor.from_preset("bart_base_en"), 23 | BartSeq2SeqLMPreprocessor, 24 | ) 25 | self.assertIsInstance( 26 | BartSeq2SeqLMPreprocessor.from_preset("bart_base_en"), 27 | BartSeq2SeqLMPreprocessor, 28 | ) 29 | 30 | @pytest.mark.large 31 | def test_from_preset_with_sequence_length(self): 32 | preprocessor = Seq2SeqLMPreprocessor.from_preset( 33 | "bart_base_en", decoder_sequence_length=16 34 | ) 35 | self.assertEqual(preprocessor.decoder_sequence_length, 16) 36 | 37 | @pytest.mark.large 38 | def test_from_preset_errors(self): 39 | with self.assertRaises(ValueError): 40 | # No loading on an incorrect class. 41 | BartSeq2SeqLMPreprocessor.from_preset("bert_tiny_en_uncased") 42 | with self.assertRaises(ValueError): 43 | # No loading on a non-keras model. 44 | BartSeq2SeqLMPreprocessor.from_preset("hf://spacy/en_core_web_sm") 45 | -------------------------------------------------------------------------------- /keras_hub/src/models/gemma/gemma_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import pytest 4 | 5 | from keras_hub.src.models.gemma.gemma_tokenizer import GemmaTokenizer 6 | from keras_hub.src.tests.test_case import TestCase 7 | 8 | 9 | class GemmaTokenizerTest(TestCase): 10 | def setUp(self): 11 | self.init_kwargs = { 12 | # Generated using create_gemma_test_proto.py 13 | "proto": os.path.join( 14 | self.get_test_data_dir(), "gemma_test_vocab.spm" 15 | ) 16 | } 17 | self.input_data = ["the quick brown fox", "the earth is round"] 18 | 19 | def test_tokenizer_basics(self): 20 | self.run_preprocessing_layer_test( 21 | cls=GemmaTokenizer, 22 | init_kwargs=self.init_kwargs, 23 | input_data=self.input_data, 24 | expected_output=[[4, 9, 5, 7], [4, 6, 8, 10]], 25 | ) 26 | 27 | def test_errors_missing_special_tokens(self): 28 | with self.assertRaises(ValueError): 29 | GemmaTokenizer( 30 | # Generated using create_no_special_token_proto.py 31 | proto=os.path.join( 32 | self.get_test_data_dir(), "no_special_token_vocab.spm" 33 | ) 34 | ) 35 | 36 | @pytest.mark.kaggle_key_required 37 | @pytest.mark.large 38 | def test_smallest_preset(self): 39 | self.run_preset_test( 40 | cls=GemmaTokenizer, 41 | preset="gemma_2b_en", 42 | input_data=["The quick brown fox."], 43 | expected_output=[[651, 4320, 8426, 25341, 235265]], 44 | ) 45 | 46 | @pytest.mark.kaggle_key_required 47 | @pytest.mark.extra_large 48 | def test_all_presets(self): 49 | for preset in GemmaTokenizer.presets: 50 | self.run_preset_test( 51 | cls=GemmaTokenizer, 52 | preset=preset, 53 | input_data=self.input_data, 54 | ) 55 | -------------------------------------------------------------------------------- /keras_hub/src/models/vgg/vgg_image_classifier_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.vgg.vgg_backbone import VGGBackbone 5 | from keras_hub.src.models.vgg.vgg_image_classifier import VGGImageClassifier 6 | from keras_hub.src.models.vgg.vgg_image_classifier_preprocessor import ( 7 | VGGImageClassifierPreprocessor, 8 | ) 9 | from keras_hub.src.models.vgg.vgg_image_converter import VGGImageConverter 10 | from keras_hub.src.tests.test_case import TestCase 11 | 12 | 13 | class VGGImageClassifierTest(TestCase): 14 | def setUp(self): 15 | # Setup model. 16 | self.images = np.ones((2, 8, 8, 3), dtype="float32") 17 | self.labels = [0, 1] 18 | self.backbone = VGGBackbone( 19 | stackwise_num_repeats=[2, 4, 4], 20 | stackwise_num_filters=[2, 16, 16], 21 | image_shape=(8, 8, 3), 22 | ) 23 | image_converter = VGGImageConverter(image_size=(8, 8)) 24 | self.preprocessor = VGGImageClassifierPreprocessor( 25 | image_converter=image_converter, 26 | ) 27 | self.init_kwargs = { 28 | "backbone": self.backbone, 29 | "num_classes": 2, 30 | "activation": "softmax", 31 | "pooling": "flatten", 32 | "preprocessor": self.preprocessor, 33 | } 34 | self.train_data = ( 35 | self.images, 36 | self.labels, 37 | ) 38 | 39 | def test_classifier_basics(self): 40 | self.run_task_test( 41 | cls=VGGImageClassifier, 42 | init_kwargs=self.init_kwargs, 43 | train_data=self.train_data, 44 | expected_output_shape=(2, 2), 45 | ) 46 | 47 | @pytest.mark.large 48 | def test_saved_model(self): 49 | self.run_model_saving_test( 50 | cls=VGGImageClassifier, 51 | init_kwargs=self.init_kwargs, 52 | input_data=self.images, 53 | ) 54 | -------------------------------------------------------------------------------- /keras_hub/src/models/causal_lm_preprocessor_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.bert.bert_tokenizer import BertTokenizer 4 | from keras_hub.src.models.causal_lm_preprocessor import CausalLMPreprocessor 5 | from keras_hub.src.models.gpt2.gpt2_causal_lm_preprocessor import ( 6 | GPT2CausalLMPreprocessor, 7 | ) 8 | from keras_hub.src.models.gpt2.gpt2_preprocessor import GPT2Preprocessor 9 | from keras_hub.src.tests.test_case import TestCase 10 | 11 | 12 | class TestCausalLMPreprocessor(TestCase): 13 | def test_preset_accessors(self): 14 | bert_presets = set(BertTokenizer.presets.keys()) 15 | gpt2_presets = set(GPT2Preprocessor.presets.keys()) 16 | all_presets = set(CausalLMPreprocessor.presets.keys()) 17 | self.assertTrue(bert_presets.isdisjoint(all_presets)) 18 | self.assertTrue(gpt2_presets.issubset(all_presets)) 19 | 20 | @pytest.mark.large 21 | def test_from_preset(self): 22 | self.assertIsInstance( 23 | CausalLMPreprocessor.from_preset("gpt2_base_en"), 24 | GPT2CausalLMPreprocessor, 25 | ) 26 | self.assertIsInstance( 27 | GPT2CausalLMPreprocessor.from_preset("gpt2_base_en"), 28 | GPT2CausalLMPreprocessor, 29 | ) 30 | 31 | @pytest.mark.large 32 | def test_from_preset_with_sequence_length(self): 33 | preprocessor = CausalLMPreprocessor.from_preset( 34 | "gpt2_base_en", sequence_length=16 35 | ) 36 | self.assertEqual(preprocessor.sequence_length, 16) 37 | 38 | @pytest.mark.large 39 | def test_from_preset_errors(self): 40 | with self.assertRaises(ValueError): 41 | # No loading on an incorrect class. 42 | GPT2CausalLMPreprocessor.from_preset("bert_tiny_en_uncased") 43 | with self.assertRaises(ValueError): 44 | # No loading on a non-keras model. 45 | GPT2CausalLMPreprocessor.from_preset("hf://spacy/en_core_web_sm") 46 | -------------------------------------------------------------------------------- /keras_hub/src/models/gpt2/gpt2_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.gpt2.gpt2_tokenizer import GPT2Tokenizer 4 | from keras_hub.src.tests.test_case import TestCase 5 | 6 | 7 | class GPT2TokenizerTest(TestCase): 8 | def setUp(self): 9 | self.vocab = ["!", "air", "Ġair", "plane", "Ġat", "port"] 10 | self.vocab += ["<|endoftext|>"] 11 | self.vocab = dict([(token, i) for i, token in enumerate(self.vocab)]) 12 | self.merges = ["Ġ a", "Ġ t", "Ġ i", "Ġ b", "a i", "p l", "n e"] 13 | self.merges += ["Ġa t", "p o", "r t", "Ġt h", "ai r", "pl a", "po rt"] 14 | self.merges += ["Ġai r", "Ġa i", "pla ne"] 15 | self.init_kwargs = {"vocabulary": self.vocab, "merges": self.merges} 16 | self.input_data = [ 17 | " airplane at airport<|endoftext|>", 18 | " airplane airport", 19 | ] 20 | 21 | def test_tokenizer_basics(self): 22 | self.run_preprocessing_layer_test( 23 | cls=GPT2Tokenizer, 24 | init_kwargs=self.init_kwargs, 25 | input_data=self.input_data, 26 | expected_output=[[2, 3, 4, 2, 5, 6], [2, 3, 2, 5]], 27 | ) 28 | 29 | def test_errors_missing_special_tokens(self): 30 | with self.assertRaises(ValueError): 31 | GPT2Tokenizer(vocabulary=["a", "b", "c"], merges=[]) 32 | 33 | @pytest.mark.large 34 | def test_smallest_preset(self): 35 | self.run_preset_test( 36 | cls=GPT2Tokenizer, 37 | preset="gpt2_base_en", 38 | input_data=["The quick brown fox."], 39 | expected_output=[[464, 2068, 7586, 21831, 13]], 40 | ) 41 | 42 | @pytest.mark.extra_large 43 | def test_all_presets(self): 44 | for preset in GPT2Tokenizer.presets: 45 | self.run_preset_test( 46 | cls=GPT2Tokenizer, 47 | preset=preset, 48 | input_data=self.input_data, 49 | ) 50 | -------------------------------------------------------------------------------- /keras_hub/src/samplers/random_sampler.py: -------------------------------------------------------------------------------- 1 | from keras import ops 2 | from keras import random 3 | 4 | from keras_hub.src.api_export import keras_hub_export 5 | from keras_hub.src.samplers.sampler import Sampler 6 | 7 | 8 | @keras_hub_export("keras_hub.samplers.RandomSampler") 9 | class RandomSampler(Sampler): 10 | """Random Sampler class. 11 | 12 | This sampler implements random sampling. Briefly, random sampler randomly 13 | selects a token from the entire distribution of the tokens, with selection 14 | chance determined by the probability of each token. 15 | 16 | Args: 17 | seed: int. The random seed. Defaults to `None`. 18 | 19 | Call arguments: 20 | {{call_args}} 21 | 22 | Examples: 23 | ```python 24 | causal_lm = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en") 25 | 26 | # Pass by name to compile. 27 | causal_lm.compile(sampler="random") 28 | causal_lm.generate(["Keras is a"]) 29 | 30 | # Pass by object to compile. 31 | sampler = keras_hub.samplers.RandomSampler(temperature=0.7) 32 | causal_lm.compile(sampler=sampler) 33 | causal_lm.generate(["Keras is a"]) 34 | ``` 35 | """ 36 | 37 | def __init__( 38 | self, 39 | seed=None, 40 | **kwargs, 41 | ): 42 | super().__init__(**kwargs) 43 | self.seed = seed 44 | self.seed_generator = random.SeedGenerator(seed) 45 | 46 | def get_next_token(self, probabilities): 47 | # Sample the next token from the probability distribution. 48 | next_token_id = random.categorical( 49 | ops.log(probabilities), 50 | 1, 51 | seed=self.seed_generator, 52 | dtype="int32", 53 | ) 54 | return ops.squeeze(next_token_id, axis=-1) 55 | 56 | def get_config(self): 57 | config = super().get_config() 58 | config.update( 59 | { 60 | "seed": self.seed, 61 | } 62 | ) 63 | return config 64 | -------------------------------------------------------------------------------- /keras_hub/src/models/bloom/bloom_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.bloom.bloom_tokenizer import BloomTokenizer 4 | from keras_hub.src.tests.test_case import TestCase 5 | 6 | 7 | class BloomTokenizerTest(TestCase): 8 | def setUp(self): 9 | self.vocab = ["!", "air", "Ġair", "plane", "Ġat", "port"] 10 | self.vocab += ["", "", ""] 11 | self.vocab = dict([(token, i) for i, token in enumerate(self.vocab)]) 12 | self.merges = ["Ġ a", "Ġ t", "Ġ i", "Ġ b", "a i", "p l", "n e"] 13 | self.merges += ["Ġa t", "p o", "r t", "Ġt h", "ai r", "pl a", "po rt"] 14 | self.merges += ["Ġai r", "Ġa i", "pla ne"] 15 | self.init_kwargs = {"vocabulary": self.vocab, "merges": self.merges} 16 | self.input_data = [ 17 | "airplane at airport", 18 | " airplane airport", 19 | ] 20 | 21 | def test_tokenizer_basics(self): 22 | self.run_preprocessing_layer_test( 23 | cls=BloomTokenizer, 24 | init_kwargs=self.init_kwargs, 25 | input_data=self.input_data, 26 | expected_output=[[6, 1, 3, 4, 2, 5, 8], [6, 2, 3, 2, 5, 8]], 27 | ) 28 | 29 | def test_errors_missing_special_tokens(self): 30 | with self.assertRaises(ValueError): 31 | BloomTokenizer(vocabulary=["a", "b", "c"], merges=[]) 32 | 33 | @pytest.mark.large 34 | def test_smallest_preset(self): 35 | self.run_preset_test( 36 | cls=BloomTokenizer, 37 | preset="bloom_560m_multi", 38 | input_data=["The quick brown fox."], 39 | expected_output=[[2175, 23714, 73173, 144252, 17]], 40 | ) 41 | 42 | @pytest.mark.extra_large 43 | def test_all_presets(self): 44 | for preset in BloomTokenizer.presets: 45 | self.run_preset_test( 46 | cls=BloomTokenizer, 47 | preset=preset, 48 | input_data=self.input_data, 49 | ) 50 | -------------------------------------------------------------------------------- /keras_hub/src/models/falcon/falcon_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.falcon.falcon_tokenizer import FalconTokenizer 4 | from keras_hub.src.tests.test_case import TestCase 5 | 6 | 7 | class FalconTokenizerTest(TestCase): 8 | def setUp(self): 9 | self.vocab = ["!", "air", "Ġair", "plane", "Ġat", "port"] 10 | self.vocab += ["<|endoftext|>"] 11 | self.vocab = dict([(token, i) for i, token in enumerate(self.vocab)]) 12 | self.merges = ["Ġ a", "Ġ t", "Ġ i", "Ġ b", "a i", "p l", "n e"] 13 | self.merges += ["Ġa t", "p o", "r t", "Ġt h", "ai r", "pl a", "po rt"] 14 | self.merges += ["Ġai r", "Ġa i", "pla ne"] 15 | self.init_kwargs = {"vocabulary": self.vocab, "merges": self.merges} 16 | self.input_data = [ 17 | " airplane at airport<|endoftext|>", 18 | " airplane airport", 19 | ] 20 | 21 | def test_tokenizer_basics(self): 22 | self.run_preprocessing_layer_test( 23 | cls=FalconTokenizer, 24 | init_kwargs=self.init_kwargs, 25 | input_data=self.input_data, 26 | expected_output=[[2, 3, 4, 2, 5, 6], [2, 3, 2, 5]], 27 | ) 28 | 29 | def test_errors_missing_special_tokens(self): 30 | with self.assertRaises(ValueError): 31 | FalconTokenizer(vocabulary=["a", "b", "c"], merges=[]) 32 | 33 | @pytest.mark.large 34 | def test_smallest_preset(self): 35 | self.run_preset_test( 36 | cls=FalconTokenizer, 37 | preset="falcon_refinedweb_1b_en", 38 | input_data=["The quick brown fox."], 39 | expected_output=[[464, 2068, 7586, 21831, 13]], 40 | ) 41 | 42 | @pytest.mark.extra_large 43 | def test_all_presets(self): 44 | for preset in FalconTokenizer.presets: 45 | self.run_preset_test( 46 | cls=FalconTokenizer, 47 | preset=preset, 48 | input_data=self.input_data, 49 | ) 50 | -------------------------------------------------------------------------------- /keras_hub/src/models/seq_2_seq_lm.py: -------------------------------------------------------------------------------- 1 | from keras_hub.src.api_export import keras_hub_export 2 | from keras_hub.src.models.causal_lm import CausalLM 3 | 4 | 5 | @keras_hub_export("keras_hub.models.Seq2SeqLM") 6 | class Seq2SeqLM(CausalLM): 7 | """Base class for sequence to sequence language modeling tasks. 8 | 9 | `Seq2SeqLM` tasks wrap a `keras_hub.models.Backbone` and 10 | a `keras_hub.models.Preprocessor` to create a model that can be used for 11 | generation and generative fine-tuning, when generation is conditioned on 12 | additional input sequence in a sequence-to-sequence setting. 13 | 14 | `Seq2SeqLM` tasks provide an additional, high-level `generate()` function 15 | which can be used to auto-regressively sample an output sequence token by 16 | token. The `compile()` method of `Seq2SeqLM` classes contains an additional 17 | `sampler` argument, which can be used to pass a `keras_hub.samplers.Sampler` 18 | to control how the predicted distribution will be sampled. 19 | 20 | When calling `fit()`, each input should contain an input and output 21 | sequence. The model will be trained to predict the output sequence 22 | token-by-token using a causal mask, similar to a `keras_hub.models.CausalLM` 23 | task. Unlike the `CausalLM` task, an input sequence must be passed, and 24 | can be attended to in full by all tokens in the output sequence. 25 | 26 | All `Seq2SeqLM` tasks include a `from_preset()` constructor which can be 27 | used to load a pre-trained config and weights. 28 | 29 | Example: 30 | ```python 31 | # Load a Bart backbone with pre-trained weights. 32 | seq_2_seq_lm = keras_hub.models.Seq2SeqLM.from_preset( 33 | "bart_base_en", 34 | ) 35 | seq_2_seq_lm.compile(sampler="top_k") 36 | # Generate conditioned on the `"The quick brown fox."` as an input sequence. 37 | seq_2_seq_lm.generate("The quick brown fox.", max_length=30) 38 | ``` 39 | """ 40 | 41 | # TODO: fill in during https://github.com/keras-team/keras-hub/pull/1425 42 | -------------------------------------------------------------------------------- /benchmarks/README.md: -------------------------------------------------------------------------------- 1 | # KerasHub Benchmarks 2 | 3 | This directory houses a collection of scripts for benchmarking APIs and utility 4 | functions which KerasHub provides. 5 | 6 | ## Text Generation 7 | For benchmarking text generation functions, the following command can be run 8 | from the root of the repository: 9 | 10 | ```sh 11 | python3 ./keras_hub/benchmarks/text_generation.py 12 | ``` 13 | 14 | On running this script on Google Colab (with 3090 GPU, and TensorFlow 2.11.0), 15 | the following results were obtained: 16 | 17 | | **Decoding Strategy** | **Graph Mode (sec)** | **Graph Mode with XLA (sec)** | 18 | |:---------------------: |:--------------------: |:-----------------------------: | 19 | | Greedy Search | 470.23 | 61.79 | 20 | | Beam Search | 530.13 | 189.61 | 21 | | Top-k Search | 374.05 | 62.87 | 22 | | Top-p Search | 401.97 | 260.31 | 23 | 24 | To change the configuration, say, for example, number of layers in the transformer 25 | model used for inference, the user can modify the config dictionaries given at 26 | the top of the script. 27 | 28 | ## Sentiment Analysis 29 | 30 | For benchmarking classification models, the following command can be run 31 | from the root of the repository: 32 | 33 | ```sh 34 | python3 keras_hub/benchmarks/sentiment_analysis.py \ 35 | --model="BertTextClassifier" \ 36 | --preset="bert_small_en_uncased" \ 37 | --learning_rate=5e-5 \ 38 | --num_epochs=5 \ 39 | --batch_size=32 40 | --mixed_precision_policy="mixed_float16" 41 | ``` 42 | 43 | flag `--model` specifies the model name, and `--preset` specifies the preset under testing. `--preset` could be None, 44 | while `--model` is required. Other flags are common training flags. 45 | 46 | This script outputs: 47 | 48 | - validation accuracy for each epoch. 49 | - testing accuracy after training is done. 50 | - total elapsed time (in seconds). -------------------------------------------------------------------------------- /keras_hub/src/models/text_classifier_preprocessor_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.bert.bert_text_classifier_preprocessor import ( 4 | BertTextClassifierPreprocessor, 5 | ) 6 | from keras_hub.src.models.gpt2.gpt2_tokenizer import GPT2Tokenizer 7 | from keras_hub.src.models.text_classifier_preprocessor import ( 8 | TextClassifierPreprocessor, 9 | ) 10 | from keras_hub.src.tests.test_case import TestCase 11 | 12 | 13 | class TestTextClassifierPreprocessor(TestCase): 14 | def test_preset_accessors(self): 15 | bert_presets = set(BertTextClassifierPreprocessor.presets.keys()) 16 | gpt2_presets = set(GPT2Tokenizer.presets.keys()) 17 | all_presets = set(TextClassifierPreprocessor.presets.keys()) 18 | self.assertTrue(bert_presets.issubset(all_presets)) 19 | self.assertTrue(gpt2_presets.isdisjoint(all_presets)) 20 | 21 | @pytest.mark.large 22 | def test_from_preset(self): 23 | self.assertIsInstance( 24 | TextClassifierPreprocessor.from_preset("bert_tiny_en_uncased"), 25 | BertTextClassifierPreprocessor, 26 | ) 27 | self.assertIsInstance( 28 | BertTextClassifierPreprocessor.from_preset("bert_tiny_en_uncased"), 29 | BertTextClassifierPreprocessor, 30 | ) 31 | 32 | @pytest.mark.large 33 | def test_from_preset_with_sequence_length(self): 34 | preprocessor = TextClassifierPreprocessor.from_preset( 35 | "bert_tiny_en_uncased", sequence_length=16 36 | ) 37 | self.assertEqual(preprocessor.sequence_length, 16) 38 | 39 | @pytest.mark.large 40 | def test_from_preset_errors(self): 41 | with self.assertRaises(ValueError): 42 | # No loading on an incorrect class. 43 | BertTextClassifierPreprocessor.from_preset("gpt2_base_en") 44 | with self.assertRaises(ValueError): 45 | # No loading on a non-keras model. 46 | BertTextClassifierPreprocessor.from_preset( 47 | "hf://spacy/en_core_web_sm" 48 | ) 49 | -------------------------------------------------------------------------------- /keras_nlp/setup.py: -------------------------------------------------------------------------------- 1 | """Setup script.""" 2 | 3 | import os 4 | import pathlib 5 | 6 | from setuptools import find_packages 7 | from setuptools import setup 8 | 9 | 10 | def read(rel_path): 11 | here = os.path.abspath(os.path.dirname(__file__)) 12 | with open(os.path.join(here, rel_path)) as fp: 13 | return fp.read() 14 | 15 | 16 | def get_version(rel_path): 17 | for line in read(rel_path).splitlines(): 18 | if line.startswith("__version__"): 19 | delim = '"' if '"' in line else "'" 20 | return line.split(delim)[1] 21 | raise RuntimeError("Unable to find version string.") 22 | 23 | 24 | HERE = pathlib.Path(__file__).parent 25 | README = (HERE / "README.md").read_text() 26 | PARENT = HERE.parent 27 | VERSION = get_version(PARENT / "keras_hub" / "src" / "version_utils.py") 28 | 29 | setup( 30 | name="keras-nlp", 31 | description=( 32 | "Industry-strength Natural Language Processing extensions for Keras." 33 | ), 34 | long_description=README, 35 | long_description_content_type="text/markdown", 36 | version=VERSION, 37 | url="https://github.com/keras-team/keras-nlp", 38 | author="Keras team", 39 | author_email="keras-nlp@google.com", 40 | license="Apache License 2.0", 41 | install_requires=[ 42 | f"keras-hub=={VERSION}", 43 | ], 44 | # Supported Python versions 45 | python_requires=">=3.9", 46 | classifiers=[ 47 | "Development Status :: 3 - Alpha", 48 | "Programming Language :: Python :: 3", 49 | "Programming Language :: Python :: 3.9", 50 | "Programming Language :: Python :: 3.10", 51 | "Programming Language :: Python :: 3.11", 52 | "Programming Language :: Python :: 3 :: Only", 53 | "Operating System :: Unix", 54 | "Operating System :: Microsoft :: Windows", 55 | "Operating System :: MacOS", 56 | "Intended Audience :: Science/Research", 57 | "Topic :: Scientific/Engineering", 58 | "Topic :: Software Development", 59 | ], 60 | packages=find_packages(exclude=("*_test.py",)), 61 | ) 62 | -------------------------------------------------------------------------------- /tools/quantize_checkpoints.py: -------------------------------------------------------------------------------- 1 | """ 2 | Quantize preset checkpoints with dynamic int8 and optionally upload the quantized preset. 3 | 4 | Usage: 5 | export KERAS_BACKEND=jax CUDA_VISIBLE_DEVICES= 6 | python tools/quantize_checkpoints.py --preset llama3_8b_en 7 | python tools/quantize_checkpoints.py --preset llama3_8b_en --upload_uri kaggle://keras/llama3/keras/llama3_8b_en_int8 8 | """ 9 | 10 | import keras 11 | from absl import app 12 | from absl import flags 13 | 14 | import keras_hub 15 | 16 | FLAGS = flags.FLAGS 17 | 18 | 19 | flags.DEFINE_string( 20 | "preset", 21 | None, 22 | "Must be a valid `CausalLM` preset from KerasHub", 23 | required=True, 24 | ) 25 | flags.DEFINE_string( 26 | "upload_uri", 27 | None, 28 | 'Could be "kaggle://keras/{variant}/keras/{preset}_int8"', 29 | required=False, 30 | ) 31 | 32 | 33 | def validate_output(causal_lm): 34 | input_str = "What is Keras?" 35 | length = 32 36 | 37 | keras_output = causal_lm.generate([input_str], max_length=length) 38 | keras_output = keras_output[0] 39 | print("🔶 KerasHub output:", keras_output) 40 | 41 | 42 | def main(_): 43 | preset = FLAGS.preset 44 | upload_uri = FLAGS.upload_uri 45 | print(f"🏃 Quantizing {preset}") 46 | 47 | keras.config.set_floatx("bfloat16") 48 | 49 | causal_lm = keras_hub.models.CausalLM.from_preset(preset, dtype="bfloat16") 50 | backbone = causal_lm.backbone 51 | tokenizer = causal_lm.preprocessor.tokenizer 52 | 53 | backbone.quantize("int8") 54 | print("✅ Weights quantized") 55 | 56 | causal_lm.backbone = backbone 57 | validate_output(causal_lm) 58 | print("✅ Output validated") 59 | 60 | quantized_preset = f"{preset}_int8" 61 | backbone.save_to_preset(quantized_preset) 62 | tokenizer.save_to_preset(quantized_preset) 63 | print(f"🏁 Preset saved to ./{quantized_preset}") 64 | 65 | if upload_uri: 66 | keras_hub.upload_preset(uri=upload_uri, preset=quantized_preset) 67 | print(f"🏁 Preset uploaded to {upload_uri}") 68 | 69 | 70 | if __name__ == "__main__": 71 | app.run(main) 72 | -------------------------------------------------------------------------------- /keras_hub/src/models/pali_gemma/pali_gemma_vit_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from keras_hub.src.models.pali_gemma.pali_gemma_vit import PaliGemmaVit 4 | from keras_hub.src.models.pali_gemma.pali_gemma_vit import ( 5 | PaliGemmaVitEmbeddings, 6 | ) 7 | from keras_hub.src.models.pali_gemma.pali_gemma_vit import PaliGemmaVitEncoder 8 | from keras_hub.src.tests.test_case import TestCase 9 | 10 | 11 | class PaliGemmaVitTest(TestCase): 12 | def test_vit_encoder(self): 13 | # encoder calls Attention and CLIPLayer, both of which gets 14 | # verified with this test 15 | batch_size = 2 16 | image_size = 16 17 | hidden_dim = 8 18 | intermediate_dim = 16 19 | vit_encoder = PaliGemmaVitEncoder( 20 | image_size=image_size, 21 | hidden_dim=hidden_dim, 22 | intermediate_dim=intermediate_dim, 23 | num_layers=2, 24 | num_heads=2, 25 | patch_size=4, 26 | ) 27 | dummy_input = np.random.rand(batch_size, image_size, image_size, 3) 28 | output = vit_encoder(dummy_input) 29 | self.assertEqual( 30 | output.shape, (batch_size, intermediate_dim, hidden_dim) 31 | ) 32 | 33 | def test_vision_embeddings(self): 34 | embeddings_layer = PaliGemmaVitEmbeddings( 35 | image_size=16, 36 | patch_size=4, 37 | hidden_dim=8, 38 | ) 39 | dummy_input = np.ones([1, 16, 16, 3]) 40 | vision_embeddings = embeddings_layer(dummy_input) 41 | self.assertEqual(vision_embeddings.shape, (1, 16, 8)) 42 | 43 | def test_vit_output_shape(self): 44 | embeddings_layer = PaliGemmaVit( 45 | image_size=16, 46 | patch_size=4, 47 | hidden_dim=8, 48 | num_layers=2, 49 | num_heads=2, 50 | intermediate_dim=16, 51 | num_classes=32, 52 | ) 53 | dummy_input = np.ones([1, 16, 16, 3]) 54 | image_embeddings = embeddings_layer(dummy_input) 55 | self.assertEqual(image_embeddings.shape, (1, 16, 32)) 56 | -------------------------------------------------------------------------------- /keras_hub/src/models/retinanet/non_max_supression_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from keras import ops 3 | 4 | from keras_hub.src.models.retinanet.non_max_supression import NonMaxSuppression 5 | from keras_hub.src.tests.test_case import TestCase 6 | 7 | 8 | class NonMaxSupressionTest(TestCase): 9 | def test_confidence_threshold(self): 10 | boxes = np.random.uniform(low=0, high=1, size=(2, 5, 4)) 11 | classes = ops.expand_dims( 12 | np.array( 13 | [[0.1, 0.1, 0.4, 0.9, 0.5], [0.7, 0.5, 0.3, 0.0, 0.0]], 14 | "float32", 15 | ), 16 | axis=-1, 17 | ) 18 | 19 | nms = NonMaxSuppression( 20 | bounding_box_format="yxyx", 21 | from_logits=False, 22 | iou_threshold=1.0, 23 | confidence_threshold=0.45, 24 | max_detections=2, 25 | ) 26 | 27 | outputs = nms(boxes, classes) 28 | 29 | self.assertAllClose( 30 | outputs["boxes"], [boxes[0][-2:, ...], boxes[1][:2, ...]] 31 | ) 32 | self.assertAllClose(outputs["classes"], [[0.0, 0.0], [0.0, 0.0]]) 33 | self.assertAllClose(outputs["confidence"], [[0.9, 0.5], [0.7, 0.5]]) 34 | 35 | def test_max_detections(self): 36 | boxes = np.random.uniform(low=0, high=1, size=(2, 5, 4)) 37 | classes = ops.expand_dims( 38 | np.array( 39 | [[0.1, 0.1, 0.4, 0.5, 0.9], [0.7, 0.5, 0.3, 0.0, 0.0]], 40 | "float32", 41 | ), 42 | axis=-1, 43 | ) 44 | 45 | nms = NonMaxSuppression( 46 | bounding_box_format="yxyx", 47 | from_logits=False, 48 | iou_threshold=1.0, 49 | confidence_threshold=0.1, 50 | max_detections=1, 51 | ) 52 | 53 | outputs = nms(boxes, classes) 54 | 55 | self.assertAllClose( 56 | outputs["boxes"], [boxes[0][-1:, ...], boxes[1][:1, ...]] 57 | ) 58 | self.assertAllClose(outputs["classes"], [[0.0], [0.0]]) 59 | self.assertAllClose(outputs["confidence"], [[0.9], [0.7]]) 60 | -------------------------------------------------------------------------------- /keras_hub/src/models/mobilenet/mobilenet_image_classifier_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras_hub.src.models.mobilenet.mobilenet_backbone import MobileNetBackbone 5 | from keras_hub.src.models.mobilenet.mobilenet_image_classifier import ( 6 | MobileNetImageClassifier, 7 | ) 8 | from keras_hub.src.tests.test_case import TestCase 9 | 10 | 11 | class MobileNetImageClassifierTest(TestCase): 12 | def setUp(self): 13 | # Setup model. 14 | self.images = np.ones((2, 224, 224, 3), dtype="float32") 15 | self.labels = [0, 3] 16 | self.backbone = MobileNetBackbone( 17 | stackwise_expansion=[1, 4, 6], 18 | stackwise_num_filters=[4, 8, 16], 19 | stackwise_kernel_size=[3, 3, 5], 20 | stackwise_num_strides=[2, 2, 1], 21 | stackwise_se_ratio=[0.25, None, 0.25], 22 | stackwise_activation=["relu", "relu", "hard_swish"], 23 | output_num_filters=1280, 24 | input_activation="hard_swish", 25 | output_activation="hard_swish", 26 | inverted_res_block=True, 27 | input_num_filters=16, 28 | image_shape=(224, 224, 3), 29 | ) 30 | self.init_kwargs = { 31 | "backbone": self.backbone, 32 | "num_classes": 2, 33 | "activation": "softmax", 34 | } 35 | self.train_data = ( 36 | self.images, 37 | self.labels, 38 | ) 39 | 40 | def test_classifier_basics(self): 41 | pytest.skip( 42 | reason="TODO: enable after preprocessor flow is figured out" 43 | ) 44 | self.run_task_test( 45 | cls=MobileNetImageClassifier, 46 | init_kwargs=self.init_kwargs, 47 | train_data=self.train_data, 48 | expected_output_shape=(2, 2), 49 | ) 50 | 51 | @pytest.mark.large 52 | def test_saved_model(self): 53 | self.run_model_saving_test( 54 | cls=MobileNetImageClassifier, 55 | init_kwargs=self.init_kwargs, 56 | input_data=self.images, 57 | ) 58 | -------------------------------------------------------------------------------- /keras_hub/src/models/vgg/vgg_presets.py: -------------------------------------------------------------------------------- 1 | """vgg preset configurations.""" 2 | 3 | backbone_presets = { 4 | "vgg_11_imagenet": { 5 | "metadata": { 6 | "description": ( 7 | "11-layer vgg model pre-trained on the ImageNet 1k dataset " 8 | "at a 224x224 resolution." 9 | ), 10 | "params": 9220480, 11 | "official_name": "vgg", 12 | "path": "vgg", 13 | "model_card": "https://arxiv.org/abs/1409.1556", 14 | }, 15 | "kaggle_handle": "kaggle://keras/vgg/keras/vgg_11_imagenet/1", 16 | }, 17 | "vgg_13_imagenet": { 18 | "metadata": { 19 | "description": ( 20 | "13-layer vgg model pre-trained on the ImageNet 1k dataset " 21 | "at a 224x224 resolution." 22 | ), 23 | "params": 9404992, 24 | "official_name": "vgg", 25 | "path": "vgg", 26 | "model_card": "https://arxiv.org/abs/1409.1556", 27 | }, 28 | "kaggle_handle": "kaggle://keras/vgg/keras/vgg_13_imagenet/1", 29 | }, 30 | "vgg_16_imagenet": { 31 | "metadata": { 32 | "description": ( 33 | "16-layer vgg model pre-trained on the ImageNet 1k dataset " 34 | "at a 224x224 resolution." 35 | ), 36 | "params": 14714688, 37 | "official_name": "vgg", 38 | "path": "vgg", 39 | "model_card": "https://arxiv.org/abs/1409.1556", 40 | }, 41 | "kaggle_handle": "kaggle://keras/vgg/keras/vgg_16_imagenet/1", 42 | }, 43 | "vgg_19_imagenet": { 44 | "metadata": { 45 | "description": ( 46 | "19-layer vgg model pre-trained on the ImageNet 1k dataset " 47 | "at a 224x224 resolution." 48 | ), 49 | "params": 20024384, 50 | "official_name": "vgg", 51 | "path": "vgg", 52 | "model_card": "https://arxiv.org/abs/1409.1556", 53 | }, 54 | "kaggle_handle": "kaggle://keras/vgg/keras/vgg_19_imagenet/1", 55 | }, 56 | } 57 | -------------------------------------------------------------------------------- /integration_tests/basic_usage_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import keras 4 | import numpy as np 5 | 6 | import keras_hub 7 | 8 | 9 | class BasicUsageTest(unittest.TestCase): 10 | def test_transformer(self): 11 | # Tokenize some inputs with a binary label. 12 | vocab = ["[UNK]", "the", "qu", "##ick", "br", "##own", "fox", "."] 13 | sentences = ["The quick brown fox jumped.", "The fox slept."] 14 | tokenizer = keras_hub.tokenizers.WordPieceTokenizer( 15 | vocabulary=vocab, 16 | sequence_length=10, 17 | ) 18 | x, y = tokenizer(sentences), np.array([1, 0]) 19 | 20 | # Create a tiny transformer. 21 | inputs = keras.Input(shape=(None,), dtype="int32") 22 | outputs = keras_hub.layers.TokenAndPositionEmbedding( 23 | vocabulary_size=len(vocab), 24 | sequence_length=10, 25 | embedding_dim=16, 26 | )(inputs) 27 | outputs = keras_hub.layers.TransformerEncoder( 28 | num_heads=4, 29 | intermediate_dim=32, 30 | )(outputs) 31 | outputs = keras.layers.GlobalAveragePooling1D()(outputs) 32 | outputs = keras.layers.Dense(1, activation="sigmoid")(outputs) 33 | model = keras.Model(inputs, outputs) 34 | 35 | # Run a single batch of gradient descent. 36 | model.compile(loss="binary_crossentropy") 37 | loss = model.train_on_batch(x, y) 38 | 39 | # Make sure we have a valid loss. 40 | self.assertGreater(loss, 0) 41 | 42 | def test_quickstart(self): 43 | """This roughly matches the quick start example in our base README.""" 44 | # Load a BERT model. 45 | classifier = keras_hub.models.TextClassifier.from_preset( 46 | "bert_tiny_en_uncased", 47 | num_classes=2, 48 | activation="softmax", 49 | ) 50 | # Fine-tune. 51 | classifier.fit(x=["foo", "bar", "baz"], y=[1, 0, 1], batch_size=2) 52 | # Predict two new examples. 53 | classifier.predict( 54 | ["What an amazing movie!", "A total waste of my time."] 55 | ) 56 | -------------------------------------------------------------------------------- /keras_hub/src/models/bart/bart_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.bart.bart_tokenizer import BartTokenizer 4 | from keras_hub.src.tests.test_case import TestCase 5 | 6 | 7 | class BartTokenizerTest(TestCase): 8 | def setUp(self): 9 | self.vocab = ["", "", "", "air", "Ġair", "plane", "Ġat"] 10 | self.vocab += ["port", ""] 11 | self.vocab = dict([(token, i) for i, token in enumerate(self.vocab)]) 12 | self.merges = ["Ġ a", "Ġ t", "Ġ i", "Ġ b", "a i", "p l", "n e"] 13 | self.merges += ["Ġa t", "p o", "r t", "Ġt h", "ai r", "pl a", "po rt"] 14 | self.merges += ["Ġai r", "Ġa i", "pla ne"] 15 | self.init_kwargs = {"vocabulary": self.vocab, "merges": self.merges} 16 | self.input_data = [ 17 | " airplane at airport", 18 | " airplane airport", 19 | ] 20 | 21 | def test_tokenizer_basics(self): 22 | self.run_preprocessing_layer_test( 23 | cls=BartTokenizer, 24 | init_kwargs=self.init_kwargs, 25 | input_data=self.input_data, 26 | expected_output=[[0, 4, 5, 6, 4, 7, 2, 1], [4, 5, 4, 7]], 27 | expected_detokenize_output=[ 28 | " airplane at airport", 29 | " airplane airport", 30 | ], 31 | ) 32 | 33 | def test_errors_missing_special_tokens(self): 34 | with self.assertRaises(ValueError): 35 | BartTokenizer(vocabulary=["a", "b", "c"], merges=[]) 36 | 37 | @pytest.mark.large 38 | def test_smallest_preset(self): 39 | self.run_preset_test( 40 | cls=BartTokenizer, 41 | preset="bart_base_en", 42 | input_data=["The quick brown fox."], 43 | expected_output=[[133, 2119, 6219, 23602, 4]], 44 | ) 45 | 46 | @pytest.mark.extra_large 47 | def test_all_presets(self): 48 | for preset in BartTokenizer.presets: 49 | self.run_preset_test( 50 | cls=BartTokenizer, 51 | preset=preset, 52 | input_data=self.input_data, 53 | ) 54 | -------------------------------------------------------------------------------- /keras_hub/src/models/llama3/llama3_tokenizer_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras_hub.src.models.llama3.llama3_tokenizer import Llama3Tokenizer 4 | from keras_hub.src.tests.test_case import TestCase 5 | 6 | 7 | class Llama3TokenizerTest(TestCase): 8 | def setUp(self): 9 | self.vocab = ["!", "air", "Ġair", "plane", "Ġat", "port"] 10 | self.vocab += ["<|end_of_text|>", "<|begin_of_text|>"] 11 | self.vocab += ["<|start_header_id|>", "<|end_header_id|>"] 12 | self.vocab += ["<|eot_id|>"] 13 | self.vocab = dict([(token, i) for i, token in enumerate(self.vocab)]) 14 | self.merges = ["Ġ a", "Ġ t", "Ġ i", "Ġ b", "a i", "p l", "n e"] 15 | self.merges += ["Ġa t", "p o", "r t", "Ġt h", "ai r", "pl a", "po rt"] 16 | self.merges += ["Ġai r", "Ġa i", "pla ne"] 17 | self.init_kwargs = {"vocabulary": self.vocab, "merges": self.merges} 18 | self.input_data = [ 19 | "<|begin_of_text|>airplane at airport<|end_of_text|>", 20 | " airplane airport", 21 | ] 22 | 23 | def test_tokenizer_basics(self): 24 | self.run_preprocessing_layer_test( 25 | cls=Llama3Tokenizer, 26 | init_kwargs=self.init_kwargs, 27 | input_data=self.input_data, 28 | expected_output=[[7, 1, 3, 4, 2, 5, 6], [2, 3, 2, 5]], 29 | ) 30 | 31 | def test_errors_missing_special_tokens(self): 32 | with self.assertRaises(ValueError): 33 | Llama3Tokenizer(vocabulary={"foo": 0, "bar": 1}, merges=["fo o"]) 34 | 35 | @pytest.mark.large 36 | def test_smallest_preset(self): 37 | self.run_preset_test( 38 | cls=Llama3Tokenizer, 39 | preset="llama3_8b_en", 40 | input_data=["The quick brown fox."], 41 | expected_output=[[791, 4062, 14198, 39935, 13]], 42 | ) 43 | 44 | @pytest.mark.extra_large 45 | def test_all_presets(self): 46 | for preset in Llama3Tokenizer.presets: 47 | self.run_preset_test( 48 | cls=Llama3Tokenizer, 49 | preset=preset, 50 | input_data=self.input_data, 51 | ) 52 | -------------------------------------------------------------------------------- /keras_hub/src/models/opt/opt_backbone_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras import ops 3 | 4 | from keras_hub.src.models.opt.opt_backbone import OPTBackbone 5 | from keras_hub.src.tests.test_case import TestCase 6 | 7 | 8 | class OPTBackboneTest(TestCase): 9 | def setUp(self): 10 | self.init_kwargs = { 11 | "vocabulary_size": 10, 12 | "num_layers": 2, 13 | "num_heads": 2, 14 | "hidden_dim": 2, 15 | "intermediate_dim": 4, 16 | "max_sequence_length": 5, 17 | } 18 | self.input_data = { 19 | "token_ids": ops.ones((2, 5), dtype="int32"), 20 | "padding_mask": ops.ones((2, 5), dtype="int32"), 21 | } 22 | 23 | def test_backbone_basics(self): 24 | self.run_backbone_test( 25 | cls=OPTBackbone, 26 | init_kwargs=self.init_kwargs, 27 | input_data=self.input_data, 28 | expected_output_shape=(2, 5, 2), 29 | ) 30 | 31 | @pytest.mark.large 32 | def test_saved_model(self): 33 | self.run_model_saving_test( 34 | cls=OPTBackbone, 35 | init_kwargs=self.init_kwargs, 36 | input_data=self.input_data, 37 | ) 38 | 39 | @pytest.mark.large 40 | def test_smallest_preset(self): 41 | self.run_preset_test( 42 | cls=OPTBackbone, 43 | preset="opt_125m_en", 44 | input_data={ 45 | "token_ids": ops.array([[133, 2119, 6219, 23602, 4]]), 46 | "padding_mask": ops.ones((1, 5), dtype="int32"), 47 | }, 48 | expected_output_shape=(1, 5, 768), 49 | # The forward pass from a preset should be stable! 50 | expected_partial_output=ops.array( 51 | [-0.246, -1.004, -0.072, 0.097, 0.533] 52 | ), 53 | ) 54 | 55 | @pytest.mark.extra_large 56 | def test_all_presets(self): 57 | for preset in OPTBackbone.presets: 58 | self.run_preset_test( 59 | cls=OPTBackbone, 60 | preset=preset, 61 | input_data=self.input_data, 62 | ) 63 | --------------------------------------------------------------------------------