kernel_image_resize / publish.sh
Molbap's picture
Molbap HF Staff
Upload folder using huggingface_hub
3f4e938 verified
Raw
History Blame Contribute Delete
2.4 kB
#!/usr/bin/env bash
# Publish the universal kernel to the Hub as a `kernel` repo type.
#
# IMPORTANT: kernel repos are repo_type="kernel" (served under /api/kernels/), NOT model repos.
# get_kernel() queries repo_type="kernel", so uploading with --repo-type model gives a 404.
#
# A universal kernel needs no compilation: the build is just a copy of the source package into the
# variant directory get_kernel resolves (build/torch-universal/<name>/).
set -euo pipefail
REPO_ID="${1:-Molbap/kernel_image_resize}"
NAME="kernel_image_resize"
HERE="$(cd "$(dirname "$0")" && pwd)"
rm -rf "$HERE/build"
mkdir -p "$HERE/build/torch-universal"
cp -r "$HERE/torch-ext/$NAME" "$HERE/build/torch-universal/$NAME"
find "$HERE/build" -name __pycache__ -type d -exec rm -rf {} + 2>/dev/null || true
# metadata.json at the variant root: get_kernel reads it to find the package (name.python_name) and
# its deps. A Triton universal kernel runs on CUDA, so backend=cuda; the variant is already chosen by
# the torch-universal directory name. Field names are dash-cased (python-depends), version is an int.
python - "$HERE/build/torch-universal/metadata.json" <<'PY'
import json, sys
json.dump(
{"id": "kernel-image-resize", "name": "kernel-image-resize", "version": 1, "license": "Apache-2.0",
"upstream": None, "python-depends": [], "backend": {"type": "cuda"}},
open(sys.argv[1], "w"),
)
PY
echo "built build/torch-universal/$NAME + metadata.json"
# Create the kernel repo and upload. Uses the Python API because it reliably accepts
# repo_type="kernel" (the hf CLI's repo-type choices can be stricter).
python - "$REPO_ID" "$HERE" <<'PY'
import sys
# Some huggingface_hub versions know the `kernel` repo type (constant + create_repo) but still
# validate upload_folder against the old REPO_TYPES list. Register it so the upload validator passes.
import huggingface_hub.constants as hfc
if "kernel" not in hfc.REPO_TYPES:
hfc.REPO_TYPES = list(hfc.REPO_TYPES) + ["kernel"]
from huggingface_hub import create_repo, upload_folder
repo_id, folder = sys.argv[1], sys.argv[2]
create_repo(repo_id, repo_type="kernel", exist_ok=True)
upload_folder(
repo_id=repo_id,
repo_type="kernel",
folder_path=folder,
ignore_patterns=["__pycache__/*", "*.pyc", "result", ".git/*", "build/torch-universal/*/__pycache__/*"],
)
print(f"uploaded {folder} -> {repo_id} (repo_type=kernel)")
PY