Skip to content

Conversation

@mergennachin
Copy link
Contributor

No description provided.

Copilot AI review requested due to automatic review settings January 7, 2026 20:58
@mergennachin mergennachin requested a review from lucylq as a code owner January 7, 2026 20:58
@pytorch-bot
Copy link

pytorch-bot bot commented Jan 7, 2026

🔗 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 Pending

As of commit 12949ee with merge base 09b5bdb (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 7, 2026
@github-actions
Copy link

github-actions bot commented Jan 7, 2026

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Copy link
Contributor

Copilot AI left a 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.

Comment on lines +216 to +233
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)
Copy link

Copilot AI Jan 7, 2026

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
os.makedirs(args.output_dir, exist_ok=True)

print("Extracting tokenizer...")
extract_tokenizer(args.output_dir)
Copy link

Copilot AI Jan 7, 2026

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.

Suggested change
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}"
)

Copilot uses AI. Check for mistakes.
Comment on lines +215 to +233
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)
Copy link

Copilot AI Jan 7, 2026

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.

Suggested change
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)

Copilot uses AI. Check for mistakes.
Returns:
Path to the extracted tokenizer.model, or None if extraction failed.
"""
from huggingface_hub import hf_hub_download
Copy link

Copilot AI Jan 7, 2026

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.

Copilot uses AI. Check for mistakes.
@mergennachin mergennachin merged commit 3090486 into main Jan 7, 2026
152 of 156 checks passed
@mergennachin mergennachin deleted the parakeet_tokenizer branch January 7, 2026 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants