Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/transformers/dynamic_module_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def get_cached_module_file(
local_files_only = True

# Download and cache module_file from the repo `pretrained_model_name_or_path` of grab it if it's a local file.
pretrained_model_name_or_path = str(pretrained_model_name_or_path)
pretrained_model_name_or_path = str(pretrained_model_name_or_path).rstrip(os.sep)
is_local = os.path.isdir(pretrained_model_name_or_path)
if is_local:
submodule = _sanitize_module_name(os.path.basename(pretrained_model_name_or_path))
Expand Down
23 changes: 23 additions & 0 deletions tests/utils/test_dynamic_module_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# limitations under the License.

import os
import warnings

import pytest

from transformers import AutoConfig
from transformers.dynamic_module_utils import get_imports


Expand Down Expand Up @@ -127,3 +129,24 @@ def test_import_parsing(tmp_path, case):

parsed_imports = get_imports(tmp_file_path)
assert parsed_imports == ["os"]


def test_local_path_with_and_without_trailing_slash(tmp_path):
model_dir = tmp_path / "my_model"
model_dir.mkdir()
config_path = model_dir / "config.json"
config_path.write_text('{"model_type": "bert"}')
path_no_slash = str(model_dir)
path_with_slash = str(model_dir) + os.sep

with warnings.catch_warnings(record=True) as w1:
warnings.simplefilter("always")
cfg1 = AutoConfig.from_pretrained(path_no_slash)

with warnings.catch_warnings(record=True) as w2:
warnings.simplefilter("always")
cfg2 = AutoConfig.from_pretrained(path_with_slash)

assert isinstance(cfg1, type(cfg2))
assert len(w1) == 0
assert len(w2) == 0