-
Notifications
You must be signed in to change notification settings - Fork 794
Export tokenizer.model for parakeet export #16494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16494
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 43 PendingAs of commit 12949ee with merge base 09b5bdb ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds functionality to extract and export the tokenizer.model file from the Parakeet model's .nemo archive during the export process.
- Adds a new
extract_tokenizer()function that downloads and extracts tokenizer.model from the cached .nemo file - Integrates tokenizer extraction into the main export workflow before model loading
- Adds necessary imports (shutil, tarfile, tempfile) to support tar archive extraction
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| with tarfile.open(nemo_path, "r") as tar: | ||
| # Find tokenizer.model in the archive (may be in root or subdirectory) | ||
| tokenizer_member = None | ||
| for member in tar.getmembers(): | ||
| if member.name.endswith(tokenizer_filename): | ||
| tokenizer_member = member | ||
| break | ||
|
|
||
| if tokenizer_member is None: | ||
| print(f"Warning: {tokenizer_filename} not found in .nemo archive") | ||
| return None | ||
|
|
||
| # Extract to temp directory | ||
| tar.extract(tokenizer_member, tmpdir) | ||
| extracted_path = os.path.join(tmpdir, tokenizer_member.name) | ||
|
|
||
| # Copy to output directory | ||
| shutil.copy2(extracted_path, output_path) |
Copilot
AI
Jan 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function opens a tar file without specifying error handling for potential exceptions. If the .nemo file is corrupted or not a valid tar archive, this will raise an exception that is not handled. Consider adding a try-except block to handle tarfile.ReadError and other potential exceptions, and return None with an appropriate error message.
| with tarfile.open(nemo_path, "r") as tar: | |
| # Find tokenizer.model in the archive (may be in root or subdirectory) | |
| tokenizer_member = None | |
| for member in tar.getmembers(): | |
| if member.name.endswith(tokenizer_filename): | |
| tokenizer_member = member | |
| break | |
| if tokenizer_member is None: | |
| print(f"Warning: {tokenizer_filename} not found in .nemo archive") | |
| return None | |
| # Extract to temp directory | |
| tar.extract(tokenizer_member, tmpdir) | |
| extracted_path = os.path.join(tmpdir, tokenizer_member.name) | |
| # Copy to output directory | |
| shutil.copy2(extracted_path, output_path) | |
| try: | |
| with tarfile.open(nemo_path, "r") as tar: | |
| # Find tokenizer.model in the archive (may be in root or subdirectory) | |
| tokenizer_member = None | |
| for member in tar.getmembers(): | |
| if member.name.endswith(tokenizer_filename): | |
| tokenizer_member = member | |
| break | |
| if tokenizer_member is None: | |
| print(f"Warning: {tokenizer_filename} not found in .nemo archive") | |
| return None | |
| # Extract to temp directory | |
| tar.extract(tokenizer_member, tmpdir) | |
| extracted_path = os.path.join(tmpdir, tokenizer_member.name) | |
| # Copy to output directory | |
| shutil.copy2(extracted_path, output_path) | |
| except (tarfile.ReadError, tarfile.TarError, OSError) as e: | |
| print(f"Error: failed to open or extract from .nemo archive '{nemo_path}': {e}") | |
| return None |
| os.makedirs(args.output_dir, exist_ok=True) | ||
|
|
||
| print("Extracting tokenizer...") | ||
| extract_tokenizer(args.output_dir) |
Copilot
AI
Jan 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value from extract_tokenizer is not checked. If the tokenizer extraction fails (returns None), the script will continue execution without the tokenizer file. Consider checking the return value and handling failure appropriately, such as by logging a warning or raising an error if the tokenizer is required for the export.
| extract_tokenizer(args.output_dir) | |
| tokenizer_path = extract_tokenizer(args.output_dir) | |
| if tokenizer_path is None: | |
| raise RuntimeError( | |
| f"Failed to extract tokenizer into output directory: {args.output_dir}" | |
| ) |
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| with tarfile.open(nemo_path, "r") as tar: | ||
| # Find tokenizer.model in the archive (may be in root or subdirectory) | ||
| tokenizer_member = None | ||
| for member in tar.getmembers(): | ||
| if member.name.endswith(tokenizer_filename): | ||
| tokenizer_member = member | ||
| break | ||
|
|
||
| if tokenizer_member is None: | ||
| print(f"Warning: {tokenizer_filename} not found in .nemo archive") | ||
| return None | ||
|
|
||
| # Extract to temp directory | ||
| tar.extract(tokenizer_member, tmpdir) | ||
| extracted_path = os.path.join(tmpdir, tokenizer_member.name) | ||
|
|
||
| # Copy to output directory | ||
| shutil.copy2(extracted_path, output_path) |
Copilot
AI
Jan 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential path traversal vulnerability when extracting from tar archive. A malicious .nemo file could contain members with absolute paths or paths with ".." that could write files outside the intended output directory. Consider validating that the extracted member path is safe before extraction, or use a safer extraction method that prevents path traversal attacks.
| with tempfile.TemporaryDirectory() as tmpdir: | |
| with tarfile.open(nemo_path, "r") as tar: | |
| # Find tokenizer.model in the archive (may be in root or subdirectory) | |
| tokenizer_member = None | |
| for member in tar.getmembers(): | |
| if member.name.endswith(tokenizer_filename): | |
| tokenizer_member = member | |
| break | |
| if tokenizer_member is None: | |
| print(f"Warning: {tokenizer_filename} not found in .nemo archive") | |
| return None | |
| # Extract to temp directory | |
| tar.extract(tokenizer_member, tmpdir) | |
| extracted_path = os.path.join(tmpdir, tokenizer_member.name) | |
| # Copy to output directory | |
| shutil.copy2(extracted_path, output_path) | |
| with tarfile.open(nemo_path, "r") as tar: | |
| # Find tokenizer.model in the archive (may be in root or subdirectory) | |
| tokenizer_member = None | |
| for member in tar.getmembers(): | |
| if member.name.endswith(tokenizer_filename): | |
| tokenizer_member = member | |
| break | |
| if tokenizer_member is None: | |
| print(f"Warning: {tokenizer_filename} not found in .nemo archive") | |
| return None | |
| if not tokenizer_member.isfile(): | |
| print(f"Warning: {tokenizer_filename} in .nemo archive is not a regular file") | |
| return None | |
| # Safely extract the tokenizer file contents directly to the output path | |
| with tar.extractfile(tokenizer_member) as src, open(output_path, "wb") as dst: | |
| shutil.copyfileobj(src, dst) |
| Returns: | ||
| Path to the extracted tokenizer.model, or None if extraction failed. | ||
| """ | ||
| from huggingface_hub import hf_hub_download |
Copilot
AI
Jan 7, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The huggingface_hub import is placed inside the function rather than at the module level. While this can avoid import errors if the package is not installed, it's inconsistent with the module-level imports used for other dependencies like torch and torchaudio. Consider either importing at the module level for consistency, or documenting why this particular import needs to be local.
No description provided.