{"id":23415745,"url":"https://github.com/shivendrra/shredword","last_synced_at":"2025-04-09T06:23:29.064Z","repository":{"id":268017761,"uuid":"849173165","full_name":"shivendrra/shredword","owner":"shivendrra","description":"Fast \u0026 efficient BPE tokenizer written in C \u0026 python for LLM tranining","archived":false,"fork":false,"pushed_at":"2025-03-22T18:12:30.000Z","size":14877,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T19:22:10.384Z","etag":null,"topics":["c","c-tokenizer","cpp","open-source","tiktoken","tokenizer"],"latest_commit_sha":null,"homepage":"","language":"C++","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/shivendrra.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-08-29T05:34:00.000Z","updated_at":"2025-03-12T19:03:02.000Z","dependencies_parsed_at":"2025-01-26T17:23:10.581Z","dependency_job_id":"ae87cf56-ee4a-4568-9fdf-36ac43130caf","html_url":"https://github.com/shivendrra/shredword","commit_stats":null,"previous_names":["shivendrra/shredword"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivendrra%2Fshredword","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivendrra%2Fshredword/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivendrra%2Fshredword/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivendrra%2Fshredword/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shivendrra","download_url":"https://codeload.github.com/shivendrra/shredword/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247988648,"owners_count":21029128,"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":["c","c-tokenizer","cpp","open-source","tiktoken","tokenizer"],"created_at":"2024-12-22T21:27:57.876Z","updated_at":"2025-04-09T06:23:29.050Z","avatar_url":"https://github.com/shivendrra.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ShredWord\nShredWord is a byte-pair encoding (BPE) based tokenizer designed for efficient and flexible text processing. It offers training, encoding, and decoding functionalities and is backed by a C/C++ core with a Python interface for easy integration into machine learning workflows.\n\n## Features\n\n1. **Efficient Tokenization**: Utilizes BPE for compressing text data and reducing the vocabulary size, making it well-suited for NLP tasks.\n2. **Customizable Vocabulary**: Allows users to define the target vocabulary size during training.\n3. **Save and Load Models**: Supports saving and loading trained tokenizers for reuse.\n4. **Python Integration**: Provides a Python interface for seamless integration and usability.\n\n\n## How It Works\n\n### Byte-Pair Encoding (BPE)\nBPE is a subword tokenization algorithm that compresses a dataset by merging the most frequent pairs of characters or subwords into new tokens. This process continues until a predefined vocabulary size is reached.\n\nKey steps:\n1. Initialize the vocabulary with all unique characters in the dataset.\n2. Count the frequency of character pairs.\n3. Merge the most frequent pair into a new token.\n4. Repeat until the target vocabulary size is achieved.\n\nShredWord implements this process efficiently in C/C++, exposing training, encoding, and decoding methods through Python.\n\n## Installation\n\n### Prerequisites\n- Python 3.7+\n- GCC or a compatible compiler (for compiling the C/C++ code)\n\n### Steps\n1. Clone the repository:\n   ```bash\n   git clone https://github.com/shivendrra/shredword.git\n   cd shredword\n   ```\n\n2. Compile the shared library:\n   ```bash\n   g++ -shared -fPIC -o build/libtoken.dll main.cpp base.cpp\n   ```\n\n3. Install the Python package:\n   ```bash\n   pip install .\n   ```\n\n\n## Usage\n\nBelow is a simple example demonstrating how to use ShredWord for training, encoding, and decoding text.\n\n### Example\n```python\nfrom shredword import Shred\n\ntokenizer = Shred()\ninput_file = \"test data/training_data.txt\"\n\n# Load training data\nwith open(input_file, \"r\", encoding=\"utf-8\") as f:\n  text = f.read()\n\n# Uncomment to train a new tokenizer\n# VOCAB_SIZE = 556\n# tokenizer.train(text, VOCAB_SIZE)\n# tokenizer.save(\"vocab/trained_vocab\")\n\n# Load a pre-trained tokenizer\ntokenizer.load(\"vocab/trained_vocab.model\")\n\n# Encode text\nencoded = tokenizer.encode(text)\nprint(\"Encoded:\", encoded)\n\n# Decode text\ndecoded = tokenizer.decode(encoded)\nprint(\"Decoded:\", decoded)\n```\n\n### Output\n- **Encoded**: A list of token IDs representing the input text.\n- **Decoded**: The original text reconstructed from the token IDs.\n\n\n## API Overview\n\n### Core Methods\n- `train(text, vocab_size)`: Train a tokenizer on the input text to a specified vocabulary size.\n- `encode(text)`: Convert input text into a list of token IDs.\n- `decode(ids)`: Reconstruct text from token IDs.\n- `save(file_path)`: Save the trained tokenizer to a file.\n- `load(file_path)`: Load a pre-trained tokenizer from a file.\n\n### Properties\n- `merges`: View or set the merge rules for tokenization.\n- `vocab`: Access the vocabulary as a dictionary of token IDs to strings.\n- `pattern`: View or set the regular expression pattern used for token splitting.\n- `special_tokens`: View or set special tokens used by the tokenizer.\n\n## Advanced Features\n\n### Saving and Loading\nTrained tokenizers can be saved to a file and reloaded for use in future tasks. The saved model includes merge rules and any special tokens or patterns defined during training.\n\n```python\n# Save the trained model\ntokenizer.save(\"vocab/trained_vocab.model\")\n\n# Load the model\ntokenizer.load(\"vocab/trained_vocab.model\")\n```\n\n### Customization\nUsers can define special tokens or modify the merge rules and pattern directly using the provided properties.\n\n```python\n# Set special tokens\nspecial_tokens = [(\"\u003cPAD\u003e\", 0), (\"\u003cUNK\u003e\", 1)]\ntokenizer.special_tokens = special_tokens\n\n# Update merge rules\nmerges = [(101, 32, 256), (32, 116, 257)]\ntokenizer.merges = merges\n```\n\n## Contributing\n\nWe welcome contributions! Feel free to open an issue or submit a pull request if you have ideas for improvement.\n\n## License\n\nThis project is licensed under the MIT License. See the `LICENSE` file for details.\n\n## Acknowledgments\n\nShredWord was inspired by the need for efficient and flexible tokenization in modern NLP pipelines. Special thanks to contributors and the open-source community for their support.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshivendrra%2Fshredword","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshivendrra%2Fshredword","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshivendrra%2Fshredword/lists"}