{"id":17132063,"url":"https://github.com/dayyass/muse_tf2pt","last_synced_at":"2025-04-13T06:32:15.806Z","repository":{"id":241078358,"uuid":"804073470","full_name":"dayyass/muse_tf2pt","owner":"dayyass","description":"Convert MUSE from TensorFlow to PyTorch and ONNX","archived":false,"fork":false,"pushed_at":"2024-05-22T07:38:23.000Z","size":1825,"stargazers_count":11,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-11T02:14:17.307Z","etag":null,"topics":["embedder","embeddings","encoder","machine-learning","multilingual","natural-language-processing","sentence-embeddings","sentence-transformers","transformer","universal-sentence-encoder"],"latest_commit_sha":null,"homepage":"https://huggingface.co/dayyass/universal-sentence-encoder-multilingual-large-3-pytorch","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dayyass.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-21T22:52:08.000Z","updated_at":"2024-08-12T16:26:43.000Z","dependencies_parsed_at":"2024-05-22T09:46:24.873Z","dependency_job_id":"5c9011f6-a313-4884-9370-05f7f0ecf382","html_url":"https://github.com/dayyass/muse_tf2pt","commit_stats":null,"previous_names":["dayyass/muse_tf2pt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dayyass%2Fmuse_tf2pt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dayyass%2Fmuse_tf2pt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dayyass%2Fmuse_tf2pt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dayyass%2Fmuse_tf2pt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dayyass","download_url":"https://codeload.github.com/dayyass/muse_tf2pt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248674674,"owners_count":21143760,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["embedder","embeddings","encoder","machine-learning","multilingual","natural-language-processing","sentence-embeddings","sentence-transformers","transformer","universal-sentence-encoder"],"created_at":"2024-10-14T19:25:53.660Z","updated_at":"2025-04-13T06:32:14.397Z","avatar_url":"https://github.com/dayyass.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Convert MUSE from TensorFlow to PyTorch and ONNX\n\nThis repository contains code to:\n1. Convert the mUSE (Multilingual Universal Sentence Encoder) transformer model from [TF Hub](https://www.kaggle.com/models/google/universal-sentence-encoder/tensorFlow2/multilingual-large) format to **PyTorch** and **ONNX** formats.\n1. Use these models using **PyTorch** and **ONNX**.\n\n\u003e [!IMPORTANT]\n\u003e **The PyTorch model can be used not only for inference, but also for additional training and fine-tuning!**\n\n# Usage\n\n## ONNX\n\nThe model was transferred from TF Hub to **ONNX** using the [tensorflow-onnx](https://github.com/onnx/tensorflow-onnx) library:\n```bash\npython -m tf2onnx.convert --saved-model models/universal-sentence-encoder-multilingual-large-3 --output models/model.onnx --extra_opset ai.onnx.contrib:1\n```\n\nThe model is available for download via the [link](https://huggingface.co/dayyass/universal-sentence-encoder-multilingual-large-3-pytorch/tree/main), the inference code is available [here](tests/test_inference_torch.py).\n\n## PyTorch\n\nThe transfer of the model from TF Hub to **PyTorch** was carried out through manual work of direct translation of the calculation graph (*you can visualize the ONNX version of the model via [Netron](https://netron.app/)*) to PyTorch. Notebooks [convert.ipynb](convert.ipynb) and [onnx_inference_and_debug.ipynb](onnx_inference_and_debug.ipynb) were used for conversion and debugging.\n\nThe model is available in [HF Models](https://huggingface.co/dayyass/universal-sentence-encoder-multilingual-large-3-pytorch/tree/main) directly through `torch` (*currently, without native support from the `transformers` library*).\n\nModel initialization and usage code:\n```python\nimport torch\nfrom functools import partial\nfrom src.architecture import MUSE\nfrom src.tokenizer import get_tokenizer, tokenize\n\nPATH_TO_PT_MODEL = \"models/model.pt\"\nPATH_TO_TF_MODEL = \"models/universal-sentence-encoder-multilingual-large-3\"\n\ntokenizer = get_tokenizer(PATH_TO_TF_MODEL)\ntokenize = partial(tokenize, tokenizer=tokenizer)\n\nmodel_torch = MUSE(\n    num_embeddings=128010,\n    embedding_dim=512,\n    d_model=512,\n    num_heads=8,\n)\nmodel_torch.load_state_dict(\n    torch.load(PATH_TO_PT_MODEL)\n)\n\nsentence = \"Hello, world!\"\nres = model_torch(tokenize(sentence))\n```\n\u003e [!NOTE]\n\u003e Currently, the checkpoint of the original TF Hub model is used for tokenization, so it is loaded in the code above.\n\n## Notes\nFor the TF Hub to work, the model needs the `tensorflow-text` library, which is available in PyPI except for Apple Silicon, so it can be downloaded [here](https://github.com/sun1638650145/Libraries-and-Extensions-for-TensorFlow-for-Apple-Silicon/releases).\n\nPython \u003e= 3.8.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdayyass%2Fmuse_tf2pt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdayyass%2Fmuse_tf2pt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdayyass%2Fmuse_tf2pt/lists"}