Skip to content
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

Not raising error when valid tokenizer not found #11

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/docs/sdk/resource/checkpoint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -636,4 +636,3 @@ Convert Hugging Face model checkpoint to PeriFlow format.
|------|-------------|
| `NotFoundError` | Raised when `model_name_or_path` is not found. Also raised when `tokenizer_output_dir` is not found. |
| `CheckpointConversionError` | Raised when given model architecture from checkpoint is not supported to convert. |
| `TokenizerNotFoundError` | Raised when `tokenizer_output_dir` is not `None` and the model does not have the PeriFlow-compatible tokenizer implementation, which is equivalent to Hugging Face 'fast' tokenizer. Refer to [this link](https://huggingface.co/docs/transformers/main_classes/tokenizer#tokenizer) to get more info. |
3 changes: 1 addition & 2 deletions periflow/cli/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
InvalidPathError,
NotFoundError,
NotSupportedError,
TokenizerNotFoundError,
)
from periflow.formatter import (
JSONFormatter,
Expand Down Expand Up @@ -692,7 +691,7 @@ def convert(
cache_dir=cache_dir,
dry_run=dry_run,
)
except (NotFoundError, CheckpointConversionError, TokenizerNotFoundError) as exc:
except (NotFoundError, CheckpointConversionError) as exc:
secho_error_and_exit(str(exc))

msg = (
Expand Down
1 change: 1 addition & 0 deletions periflow/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
context_settings={"help_option_names": ["-h", "--help"]},
add_completion=False,
callback=validate_cli_version,
pretty_exceptions_enable=False,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you disable pretty exceptions?

Copy link
Collaborator Author

@kooyunmo kooyunmo Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pretty exception is too verbose and it displays too much info to users. By applying this change, it will show the normal Python traceback. Refer to https://typer.tiangolo.com/tutorial/exceptions/#disable-pretty-exceptions for more info.

)

app.add_typer(credential.app, name="credential", help="Manage credentials")
Expand Down
19 changes: 13 additions & 6 deletions periflow/converter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,22 @@ def save_tokenizer(
if not os.path.isdir(save_dir):
raise NotFoundError(f"Directory '{save_dir}' is not found.")

tokenizer = AutoTokenizer.from_pretrained(
model_name_or_path,
cache_dir=cache_dir,
trust_remote_code=True,
)
try:
tokenizer = AutoTokenizer.from_pretrained(
model_name_or_path,
cache_dir=cache_dir,
trust_remote_code=True,
)
except OSError as exc:
raise TokenizerNotFoundError(str(exc)) from exc

if not tokenizer.is_fast:
raise TokenizerNotFoundError
raise TokenizerNotFoundError(
"This model does not support PeriFlow-compatible tokenizer"
)

saved_file_paths = tokenizer.save_pretrained(save_directory=save_dir)

tokenizer_json_path = None
for path in saved_file_paths:
if "tokenizer.json" == os.path.basename(path):
Expand Down
15 changes: 9 additions & 6 deletions periflow/sdk/resource/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
NotFoundError,
NotSupportedCheckpointError,
PeriFlowInternalError,
TokenizerNotFoundError,
)
from periflow.logging import logger
from periflow.schema.resource.v1.checkpoint import V1Checkpoint
Expand Down Expand Up @@ -818,7 +819,6 @@ def convert(
Raises:
NotFoundError: Raised when `model_name_or_path` is not found. Also raised when `tokenizer_output_dir` is not found.
CheckpointConversionError: Raised when given model architecture from checkpoint is not supported to convert.
TokenizerNotFoundError: Raised when `tokenizer_output_dir` is not `None` and the model does not have the PeriFlow-compatible tokenizer implementation, which is equivalent to Hugging Face 'fast' tokenizer. Refer to [this link](https://huggingface.co/docs/transformers/main_classes/tokenizer#tokenizer) to get more info.

"""
try:
Expand Down Expand Up @@ -923,8 +923,11 @@ def convert(
yaml.dump(attr, file, sort_keys=False)

if tokenizer_output_dir is not None:
save_tokenizer(
model_name_or_path=model_name_or_path,
cache_dir=cache_dir,
save_dir=tokenizer_output_dir,
)
try:
save_tokenizer(
model_name_or_path=model_name_or_path,
cache_dir=cache_dir,
save_dir=tokenizer_output_dir,
)
except TokenizerNotFoundError as exc:
logger.warn(str(exc))