|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +""" |
| 9 | +This script checks if the PyTorch commit hash in pytorch.txt matches |
| 10 | +the commit hash for the NIGHTLY_VERSION specified in torch_pin.py. |
| 11 | +
|
| 12 | +It uses functions from update_pytorch_pin.py to fetch the expected commit |
| 13 | +hash and compares it with the current pin. |
| 14 | +""" |
| 15 | + |
| 16 | +import os |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +# Add the parent directory to path to import update_pytorch_pin module |
| 21 | +repo_root = Path(__file__).resolve().parent.parent.parent |
| 22 | +sys.path.insert(0, str(repo_root / ".github" / "scripts")) |
| 23 | + |
| 24 | +from update_pytorch_pin import ( |
| 25 | + get_commit_hash_for_nightly, |
| 26 | + get_torch_nightly_version, |
| 27 | + parse_nightly_version, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def get_current_pytorch_commit(): |
| 32 | + """ |
| 33 | + Read the current commit hash from .ci/docker/ci_commit_pins/pytorch.txt. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + Current commit hash string |
| 37 | + """ |
| 38 | + pin_file = repo_root / ".ci" / "docker" / "ci_commit_pins" / "pytorch.txt" |
| 39 | + if not pin_file.exists(): |
| 40 | + raise FileNotFoundError(f"Could not find {pin_file}") |
| 41 | + |
| 42 | + with open(pin_file, "r") as f: |
| 43 | + commit_hash = f.read().strip() |
| 44 | + |
| 45 | + if not commit_hash: |
| 46 | + raise ValueError(f"{pin_file} is empty") |
| 47 | + |
| 48 | + return commit_hash |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + print("=" * 80) |
| 53 | + print("Checking PyTorch commit pin consistency") |
| 54 | + print("=" * 80) |
| 55 | + print() |
| 56 | + |
| 57 | + try: |
| 58 | + # Get NIGHTLY_VERSION from torch_pin.py |
| 59 | + os.chdir(repo_root) |
| 60 | + nightly_version = get_torch_nightly_version() |
| 61 | + print(f"Nightly version: {nightly_version}") |
| 62 | + |
| 63 | + # Parse to date string |
| 64 | + date_str = parse_nightly_version(nightly_version) |
| 65 | + print(f"Expected date: {date_str}") |
| 66 | + |
| 67 | + # Get expected commit hash from PyTorch nightly branch |
| 68 | + print(f"Fetching commit hash for {date_str} from PyTorch nightly branch...") |
| 69 | + expected_commit = get_commit_hash_for_nightly(date_str) |
| 70 | + print(f"Expected commit hash: {expected_commit}") |
| 71 | + print() |
| 72 | + |
| 73 | + # Get current commit hash from pytorch.txt |
| 74 | + current_commit = get_current_pytorch_commit() |
| 75 | + print(f"Current commit hash: {current_commit}") |
| 76 | + print() |
| 77 | + |
| 78 | + # Compare commits |
| 79 | + print("=" * 80) |
| 80 | + print("Verification Result") |
| 81 | + print("=" * 80) |
| 82 | + print() |
| 83 | + |
| 84 | + if expected_commit == current_commit: |
| 85 | + print("✓ SUCCESS: PyTorch commit pin matches the nightly version!") |
| 86 | + print() |
| 87 | + print(f"Commit {current_commit} corresponds to {nightly_version}") |
| 88 | + print() |
| 89 | + print( |
| 90 | + f"Reference: https://hud.pytorch.org/pytorch/pytorch/commit/{current_commit}" |
| 91 | + ) |
| 92 | + return 0 |
| 93 | + else: |
| 94 | + print("✗ ERROR: PyTorch commit pin does NOT match the nightly version!") |
| 95 | + print() |
| 96 | + print(f" Expected commit: {expected_commit}") |
| 97 | + print(f" Current commit: {current_commit}") |
| 98 | + print() |
| 99 | + print(f"The commit in .ci/docker/ci_commit_pins/pytorch.txt") |
| 100 | + print(f"does not correspond to NIGHTLY_VERSION={nightly_version}") |
| 101 | + print() |
| 102 | + print("To fix this, you can run:") |
| 103 | + print(f" python .github/scripts/update_pytorch_pin.py") |
| 104 | + print( |
| 105 | + "or manually update the commit hash in .ci/docker/ci_commit_pins/pytorch.txt" |
| 106 | + ) |
| 107 | + print(f"with the expected commit hash {expected_commit}") |
| 108 | + return 1 |
| 109 | + |
| 110 | + except Exception as e: |
| 111 | + print(f"✗ ERROR: {e}", file=sys.stderr) |
| 112 | + return 1 |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + sys.exit(main()) |
0 commit comments