Skip to content
Merged
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
15 changes: 15 additions & 0 deletions .github/scripts/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def _build_inputs(
head_sha: str,
target_branch: str,
upload: bool,
bot_comment_id: str,
) -> dict:
inputs = {
"kernel_name": kernel_name,
Expand All @@ -184,6 +185,8 @@ def _build_inputs(
inputs["target_branch"] = target_branch
if not upload:
inputs["upload"] = "false"
if upload and bot_comment_id:
inputs["bot_comment_id"] = bot_comment_id
return inputs


Expand Down Expand Up @@ -247,6 +250,7 @@ def _plan_build_actions(
head_sha: str,
target_branch: str,
upload: bool,
bot_comment_id: str,
requested_backends: list[str] | None = None,
) -> None:
backends = read_backends(kernel_name)
Expand Down Expand Up @@ -299,6 +303,7 @@ def _plan_build_actions(
head_sha=head_sha,
target_branch=target_branch,
upload=upload,
bot_comment_id=bot_comment_id,
),
},
description=f"for kernel `{kernel_name}` on ref `{ref}`",
Expand All @@ -319,6 +324,7 @@ def plan_dispatch(
head_sha: str = "",
target_branch: str = "",
upload: bool = True,
bot_comment_id: str = "",
run_security: bool = False,
security_only: bool = False,
requested_backends: list[str] | None = None,
Expand All @@ -339,6 +345,7 @@ def plan_dispatch(
head_sha=head_sha,
target_branch=target_branch,
upload=upload,
bot_comment_id=bot_comment_id,
requested_backends=requested_backends,
)

Expand Down Expand Up @@ -487,6 +494,7 @@ def dispatch(
head_sha: str = "",
target_branch: str = "",
upload: bool = True,
bot_comment_id: str = "",
run_security: bool = False,
security_only: bool = False,
requested_backends: list[str] | None = None,
Expand All @@ -509,6 +517,7 @@ def dispatch(
head_sha=head_sha,
target_branch=target_branch,
upload=upload,
bot_comment_id=bot_comment_id,
run_security=run_security,
security_only=security_only,
requested_backends=requested_backends,
Expand Down Expand Up @@ -607,6 +616,11 @@ def main() -> int:
default="",
help="PR head SHA for commit status reporting",
)
parser.add_argument(
"--bot-comment-id",
default="",
help="Issue comment ID to update with Hub upload links",
)
parser.add_argument(
"--target-branch",
default="",
Expand Down Expand Up @@ -675,6 +689,7 @@ def main() -> int:
skip_build=args.skip_build,
pr_number=args.pr_number,
head_sha=args.head_sha,
bot_comment_id=args.bot_comment_id,
target_branch=args.target_branch,
upload=not args.no_upload,
run_security=args.security,
Expand Down
77 changes: 77 additions & 0 deletions .github/scripts/format_hub_upload_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python3
# Input:
# [--base <comment-body>] <kernel> <repo-prefix> <upload-json-or-path>...
#
# Output:
# A Hub upload section, or <comment-body> with that section updated.
Comment on lines +2 to +6

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(nit): Possible to include an actual representative example?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

just below this section are some copy pastable commands to test the script and see the output. for example in the PR description above there is an example running the script and its output

I avoided including the string output in the comment in the case we tweak the formatting - however it should be very easy to run the command to see what the output would look like using on of the examples in the header comments

#
# Example:
# python3 .github/scripts/format_hub_upload_comment.py msa kernels-community "$RUNNER_TEMP"/upload*.json
# python3 .github/scripts/format_hub_upload_comment.py --base "$BODY" msa kernels-community "$RUNNER_TEMP"/upload*.json
# python3 .github/scripts/format_hub_upload_comment.py msa kernels-community '{"pull_requests":[{"url":"https://hf.co/kernels/MiniMaxAI/msa/discussions/1"}]}'
import argparse
import json
from pathlib import Path

from hub_pr_upload_args import repo_id

SECTION = "Hub uploads:"


def load_json(value):
if not value.lstrip().startswith(("{", "[")):
path = Path(value)
if path.is_file():
with open(path) as f:
return json.load(f)
return json.loads(value)


def dedupe(lines):
return list(dict.fromkeys(lines))


def upload_lines(kernel, repo_prefix, uploads):
urls = [
pr["url"]
for upload in uploads
for pr in load_json(upload).get("pull_requests", [])
]
lines = [
f"- Hub repo: https://huggingface.co/kernels/{repo_id(kernel, repo_prefix)}"
]
lines.extend(f"- Hub pull request: {url}" for url in urls)
return dedupe(lines)


def update_comment(base, lines):
body_lines = base.rstrip().splitlines()
if SECTION in body_lines:
section = body_lines.index(SECTION)
prefix = body_lines[:section]
existing = [line for line in body_lines[section + 1 :] if line.startswith("- ")]
else:
prefix = body_lines
existing = []
body = "\n".join(prefix).rstrip()
section = "\n".join([SECTION, *dedupe(existing + lines)])
return f"{body}\n\n{section}".strip()


def main():
parser = argparse.ArgumentParser()
parser.add_argument("--base")
parser.add_argument("kernel")
parser.add_argument("repo_prefix")
parser.add_argument("uploads", nargs="+")
args = parser.parse_args()

lines = upload_lines(args.kernel, args.repo_prefix, args.uploads)
if args.base is not None:
print(update_comment(args.base, lines))
else:
print("\n".join([SECTION, *lines]))


if __name__ == "__main__":
raise SystemExit(main())
35 changes: 35 additions & 0 deletions .github/scripts/hub_pr_upload_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
# Input:
# <repo-id|upload-args> <kernel> <repo-prefix>
#
# Output:
# The effective Hub repo-id, or upload args using --create-pr for external repos.
#
# Example:
# python3 .github/scripts/hub_pr_upload_args.py upload-args msa kernels-community
import sys
import tomllib
from pathlib import Path

COMMUNITY = "kernels-community"
ROOT = Path(__file__).resolve().parents[2]


def external_repo_id(kernel):
with open(ROOT / kernel / "build.toml", "rb") as f:
repo_id = tomllib.load(f).get("general", {}).get("hub", {}).get("repo-id")
return repo_id if isinstance(repo_id, str) and repo_id and not repo_id.startswith(f"{COMMUNITY}/") else ""


def repo_id(kernel, repo_prefix):
return external_repo_id(kernel) or f"{repo_prefix}/{kernel}"


if __name__ == "__main__":
mode, kernel, repo_prefix = sys.argv[1:]
if mode == "repo-id":
print(repo_id(kernel, repo_prefix))
elif mode == "upload-args":
print("--create-pr" if external_repo_id(kernel) else f"--repo-id {repo_prefix}/{kernel}")
else:
sys.exit(f"unknown mode: {mode}")
1 change: 1 addition & 0 deletions .github/scripts/pr_comment_kernel_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ def main(*, dry_run: bool = False):
head_sha=pr_head_sha or "",
target_branch=target_branch,
upload=dispatch_upload,
bot_comment_id=str(status_comment_id or "") if dispatch_upload else "",
requested_backends=kernel_backends.get(kernel_name),
# The audit is per-PR, so request it only once (on the first kernel).
run_security=run_security and index == 0,
Expand Down
13 changes: 13 additions & 0 deletions .github/scripts/test_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ def test_requested_backends_thread_through_dispatch_dry_run():
assert any("xpu" in c.split(",") for c in csvs)


def test_bot_comment_id_threads_to_build_inputs_only_when_set():
without = _build_actions(_plan(backends=["cuda"]))
assert all("bot_comment_id" not in a.body["inputs"] for a in without)

with_id = _build_actions(_plan(backends=["cuda"], bot_comment_id="12345"))
assert all(a.body["inputs"]["bot_comment_id"] == "12345" for a in with_id)

no_upload = _build_actions(
_plan(backends=["cuda"], upload=False, bot_comment_id="12345")
)
assert all("bot_comment_id" not in a.body["inputs"] for a in no_upload)


# select_workflows: backend-union and Windows-gate cases (single-backend rows omitted).
ROUTING_TRUTH_TABLE = [
(["cuda"], False, {"build.yaml"}),
Expand Down
57 changes: 54 additions & 3 deletions .github/workflows/build-mac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ on:
required: false
type: string
default: ""
bot_comment_id:
description: "Issue comment ID to update with Hub upload links"
required: false
type: string
default: ""

permissions:
contents: read
Expand Down Expand Up @@ -206,12 +211,15 @@ jobs:
REPO_PREFIX: ${{ inputs.repo_prefix }}
TARGET_BRANCH: ${{ inputs.target_branch }}
run: |
cd "$KERNEL"
BRANCH_FLAG=""
if [ -n "$TARGET_BRANCH" ]; then
BRANCH_FLAG="--branch $TARGET_BRANCH"
fi
nix run -L github:huggingface/kernels#kernel-builder -- upload --repo-type kernel --repo-id "$REPO_PREFIX/$KERNEL" $BRANCH_FLAG
TARGET_FLAG=$(python3 .github/scripts/hub_pr_upload_args.py upload-args "$KERNEL" "$REPO_PREFIX")
cd "$KERNEL"
nix run -L github:huggingface/kernels#kernel-builder -- upload \
--repo-type kernel $TARGET_FLAG $BRANCH_FLAG \
--output-json "$RUNNER_TEMP/upload.json"

# v1 kernels without an explicit branch override also get uploaded to main.
- name: Upload v1 kernels to main
Expand All @@ -221,21 +229,39 @@ jobs:
KERNEL: ${{ steps.validate.outputs.kernel }}
REPO_PREFIX: ${{ inputs.repo_prefix }}
run: |
TARGET_FLAG=$(python3 .github/scripts/hub_pr_upload_args.py upload-args "$KERNEL" "$REPO_PREFIX")
cd "$KERNEL"

if [ -f "build.toml" ]; then
VERSION=$(grep -E '^\s*version\s*=\s*1\s*$' build.toml || true)
BRANCH=$(grep -E '^\s*branch\s*=' build.toml || true)
if [ -n "$VERSION" ] && [ -z "$BRANCH" ]; then
nix run -L github:huggingface/kernels#kernel-builder -- upload --repo-type kernel --repo-id "$REPO_PREFIX/$KERNEL" --branch main
nix run -L github:huggingface/kernels#kernel-builder -- upload \
--repo-type kernel $TARGET_FLAG --branch main \
--output-json "$RUNNER_TEMP/upload-main.json"
fi
fi

- name: Upload Hub upload summary
if: steps.validate.outputs.skip == 'false' && inputs.skip_build != true && inputs.mode != 'pr' && inputs.upload != false
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: hub-upload-macos
path: ${{ runner.temp }}/upload*.json
retention-days: 1

# Report the final build outcome as a commit status on the PR head SHA.
report-status:
if: always() && inputs.head_sha != ''
needs: [build-kernel]
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
issues: write
statuses: write
env:
REPORT_HUB_UPLOAD: ${{ needs.build-kernel.result == 'success' && inputs.bot_comment_id != '' && inputs.mode != 'pr' && inputs.upload != false }}
steps:
- name: Set commit status
env:
Expand Down Expand Up @@ -263,3 +289,28 @@ jobs:
-f target_url="$RUN_URL" \
-f description="$DESC" \
-f context="$STATUS_CONTEXT"

- name: Checkout comment helpers
if: env.REPORT_HUB_UPLOAD == 'true'
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
ref: ${{ inputs.pr_number != '' && format('refs/pull/{0}/head', inputs.pr_number) || github.ref }}

- name: Download Hub upload summaries
if: env.REPORT_HUB_UPLOAD == 'true'
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
pattern: hub-upload-*
path: hub-uploads

- name: Update bot comment with Hub URLs
if: env.REPORT_HUB_UPLOAD == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BOT_COMMENT_ID: ${{ inputs.bot_comment_id }}
KERNEL: ${{ inputs.kernel_name }}
REPO_PREFIX: ${{ inputs.repo_prefix }}
run: |
BODY=$(gh api "repos/$GITHUB_REPOSITORY/issues/comments/$BOT_COMMENT_ID" --jq .body)
BODY=$(python3 .github/scripts/format_hub_upload_comment.py --base "$BODY" "$KERNEL" "$REPO_PREFIX" hub-uploads/**/*.json)
gh api -X PATCH "repos/$GITHUB_REPOSITORY/issues/comments/$BOT_COMMENT_ID" -f body="$BODY"
5 changes: 5 additions & 0 deletions .github/workflows/build-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ on:
required: false
type: string
default: ""
bot_comment_id:
description: "Issue comment ID to update with Hub upload links"
required: false
type: string
default: ""

permissions:
contents: read
Expand Down
Loading