-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use per-build variant content hashes in the lockfile
This makes the lock file a fair bit shorter than per-file hashes. They can also be verified without any knowledge of Git objects, etc.
- Loading branch information
Showing
8 changed files
with
148 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from typing import Optional | ||
import os | ||
|
||
CACHE_DIR: Optional[str] = os.environ.get("HF_KERNELS_CACHE", None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import List, Tuple | ||
import hashlib | ||
import os | ||
from pathlib import Path | ||
|
||
|
||
def content_hash(dir: Path) -> str: | ||
"""Get a hash of the contents of a directory.""" | ||
|
||
# Get the file paths. The first element is a byte-encoded relative path | ||
# used for sorting. The second element is the absolute path. | ||
paths: List[Tuple[bytes, Path]] = [] | ||
# Ideally we'd use Path.walk, but it's only available in Python 3.12. | ||
for dirpath, _, filenames in os.walk(dir): | ||
for filename in filenames: | ||
file_abs = Path(dirpath) / filename | ||
|
||
# Python likes to create files when importing modules from the | ||
# cache, only hash files that are symlinked blobs. | ||
if file_abs.is_symlink(): | ||
paths.append( | ||
(file_abs.relative_to(dir).as_posix().encode("utf-8"), file_abs) | ||
) | ||
|
||
m = hashlib.sha256() | ||
for filename, path in paths: | ||
m.update(filename) | ||
with open(path, "rb") as f: | ||
m.update(f.read()) | ||
|
||
return f"sha256-{m.hexdigest()}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"repo_id": "kernels-community/activation", | ||
"sha": "6a030420d0dd33ffdc1281afc8ae8e94b4f4f9d0", | ||
"variants": { | ||
"torch25-cxx98-cu124-x86_64-linux": "sha256-1f1acbf45bb98cc71d0d082e19c40d6a7be57a5ccebf1143539c91ee38a5ec1c", | ||
"torch25-cxx11-cu121-x86_64-linux": "sha256-0cf715a909a5218e62ed33dd941a46038ebf5b444163d70eb25e2f611f67bf51", | ||
"torch26-cxx11-cu118-x86_64-linux": "sha256-55d13c115bd45757d78ed5fc44daeef82b36cd3d444d1b25c75c3d1206aef8e8", | ||
"torch25-cxx98-cu118-x86_64-linux": "sha256-4d11fb99da76a1e7371e204446e956f3b2ea54e5400035fdfe1e7492534583a7", | ||
"torch25-cxx11-cu124-x86_64-linux": "sha256-9b90d2d01d1ffc132a120fd2ea317994ee329b1b015d8c67f832f215b48a21ba", | ||
"torch26-cxx11-cu124-x86_64-linux": "sha256-dd4a2add0d574032a6953da0648345e61438c953754522d6d08ff0327124b73a", | ||
"torch26-cxx98-cu126-x86_64-linux": "sha256-4e172f5e5479fd94295e72e849cf73cdee25df4e16b0a2750e194de3002e7f48", | ||
"torch25-cxx98-cu121-x86_64-linux": "sha256-02b4c801677dcc42c0223faba1c4f30fa0677077bf20b7f01cde80442e143c16", | ||
"torch26-cxx98-cu118-x86_64-linux": "sha256-8d47863fe329cf7dc6d90c7305f9a288047b3af57a6fcd85a351ad84bd634fd7", | ||
"torch26-cxx98-cu124-x86_64-linux": "sha256-783c6f8e8b5daf434b27b68911e65ae053ac6a71e7aef7c28f94510d3422277c", | ||
"torch26-cxx11-cu126-x86_64-linux": "sha256-5314038ed6f34aafa7ab5d5c9bccd24b0ded1623fcd8ca889fb6f635ed2c4a20", | ||
"torch25-cxx11-cu118-x86_64-linux": "sha256-81266aa4b1ae2a8dba6de510a9fba67c8aa4706e1b91ca2be999934f3f91f312" | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[tool.kernels.dependencies] | ||
"kernels-community/activation" = ">=0.0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
|
||
from hf_kernels.cli import download_kernels | ||
|
||
|
||
# Mock download arguments class. | ||
@dataclass | ||
class DownloadArgs: | ||
all_variants: bool | ||
project_dir: Path | ||
|
||
|
||
def test_hash_validation(): | ||
project_dir = Path(__file__).parent / "hash_validation" | ||
|
||
download_kernels(DownloadArgs(all_variants=False, project_dir=project_dir)) | ||
download_kernels(DownloadArgs(all_variants=True, project_dir=project_dir)) |