Skip to content

Commit 2d0b9d6

Browse files
committed
fix: add test and improve logic
1 parent f0523e1 commit 2d0b9d6

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

kernels/src/kernels/cli/upload.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
from kernels.variants import BUILD_VARIANT_REGEX
66

77

8+
def get_file_count_in_build(build_dir: Path) -> int:
9+
return sum(
10+
1
11+
for p in build_dir.rglob("*")
12+
if p.is_file() and p.relative_to(build_dir).as_posix().startswith("torch")
13+
)
14+
815
def upload_kernels_dir(
916
kernel_dir: Path,
1017
*,
@@ -70,15 +77,11 @@ def upload_kernels_dir(
7077
allow_patterns=["benchmark*.py"],
7178
)
7279

73-
file_count = sum(
74-
1
75-
for p in build_dir.rglob("*")
76-
if p.is_file() and p.relative_to(build_dir).as_posix().startswith("torch")
77-
)
80+
file_count = get_file_count_in_build(build_dir)
7881

79-
if file_count > 200:
82+
if file_count > 1_000:
8083
print(
81-
f"⚠️ Found {file_count} files to upload, which exceeds the 200 file limit for a single commit. Deleting old build files and re-uploading the whole build folder to avoid hitting file limits."
84+
f"⚠️ Found {file_count} files to upload, which exceeds the 1,000 file limit for a single commit."
8285
)
8386
kernel_root_dir = build_dir.parent
8487
api.upload_large_folder(

kernels/tests/test_kernel_upload.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import tempfile
55
from dataclasses import dataclass
66
from pathlib import Path
7+
from types import SimpleNamespace
8+
from unittest.mock import Mock
79

810
import pytest
911

@@ -120,3 +122,27 @@ def test_kernel_upload_deletes_as_expected():
120122
str(filename_to_change) in k for k in repo_filenames
121123
), f"{repo_filenames=}"
122124
_get_hf_api().delete_repo(repo_id=REPO_ID)
125+
126+
127+
def test_large_kernel_upload_uses_kernel_root_path(monkeypatch, tmp_path):
128+
kernel_root = tmp_path / "kernel"
129+
build_variant = kernel_root / "build" / "torch-cpu"
130+
build_variant.mkdir(parents=True, exist_ok=True)
131+
(build_variant / "metadata.json").write_text("{}")
132+
for i in range(1001):
133+
(build_variant / f"file_{i}.py").touch()
134+
135+
api = Mock()
136+
api.create_repo.return_value = SimpleNamespace(repo_id=REPO_ID)
137+
monkeypatch.setattr("kernels.cli.upload._get_hf_api", lambda: api)
138+
139+
upload_kernels(UploadArgs(kernel_root, REPO_ID, False, "main"))
140+
141+
api.upload_large_folder.assert_called_once()
142+
kwargs = api.upload_large_folder.call_args.kwargs
143+
assert kwargs["repo_id"] == REPO_ID
144+
assert kwargs["folder_path"] == kernel_root.resolve()
145+
assert kwargs["revision"] == "main"
146+
assert kwargs["repo_type"] == "model"
147+
assert kwargs["allow_patterns"] == ["build/torch*"]
148+
api.upload_folder.assert_not_called()

0 commit comments

Comments
 (0)