-
Notifications
You must be signed in to change notification settings - Fork 950
Expand file tree
/
Copy pathupdate_pytorch_pin.py
More file actions
275 lines (209 loc) · 7.87 KB
/
update_pytorch_pin.py
File metadata and controls
275 lines (209 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
import base64
import hashlib
import json
import re
import sys
import urllib.request
from pathlib import Path
def parse_nightly_version(nightly_version):
"""
Parse NIGHTLY_VERSION (e.g., 'dev20251004') to date string (e.g., '2025-10-04').
Args:
nightly_version: String in format 'devYYYYMMDD'
Returns:
Date string in format 'YYYY-MM-DD'
"""
match = re.match(r"dev(\d{4})(\d{2})(\d{2})", nightly_version)
if not match:
raise ValueError(f"Invalid NIGHTLY_VERSION format: {nightly_version}")
year, month, day = match.groups()
return f"{year}-{month}-{day}"
def get_torch_nightly_version():
"""
Read NIGHTLY_VERSION from torch_pin.py.
Returns:
NIGHTLY_VERSION string
"""
with open("torch_pin.py", "r") as f:
content = f.read()
match = re.search(r'NIGHTLY_VERSION\s*=\s*["\']([^"\']+)["\']', content)
if not match:
raise ValueError("Could not find NIGHTLY_VERSION in torch_pin.py")
return match.group(1)
def get_commit_hash_for_nightly(date_str):
"""
Fetch commit hash from PyTorch nightly branch for a given date.
Args:
date_str: Date string in format 'YYYY-MM-DD'
Returns:
Commit hash string
"""
api_url = "https://api.github.com/repos/pytorch/pytorch/commits"
params = f"?sha=nightly&per_page=50"
url = api_url + params
req = urllib.request.Request(url)
req.add_header("Accept", "application/vnd.github.v3+json")
req.add_header("User-Agent", "ExecuTorch-Bot")
try:
with urllib.request.urlopen(req) as response:
commits = json.loads(response.read().decode())
except Exception as e:
print(f"Error fetching commits: {e}", file=sys.stderr)
sys.exit(1)
# Look for commit with title matching "{date_str} nightly release"
target_title = f"{date_str} nightly release"
for commit in commits:
commit_msg = commit.get("commit", {}).get("message", "")
# Check if the first line of commit message matches
first_line = commit_msg.split("\n")[0].strip()
if first_line.startswith(f"{date_str} nightly"):
return extract_hash_from_title(first_line)
raise ValueError(
f"Could not find commit with title matching '{target_title}' in nightly branch"
)
def extract_hash_from_title(title):
match = re.search(r"\(([0-9a-fA-F]{7,40})\)", title)
if not match:
raise ValueError(f"Could not extract commit hash from title '{title}'")
return match.group(1)
def update_pytorch_pin(commit_hash):
"""
Update .ci/docker/ci_commit_pins/pytorch.txt with the new commit hash.
Args:
commit_hash: Commit hash to write
"""
pin_file = ".ci/docker/ci_commit_pins/pytorch.txt"
with open(pin_file, "w") as f:
f.write(f"{commit_hash}\n")
print(f"Updated {pin_file} with commit hash: {commit_hash}")
def should_skip_file(filename):
"""
Check if a file should be skipped during sync (build files).
Args:
filename: Base filename to check
Returns:
True if file should be skipped
"""
skip_files = {"BUCK", "CMakeLists.txt", "TARGETS", "targets.bzl"}
return filename in skip_files
def fetch_file_content(commit_hash, file_path):
"""
Fetch file content from GitHub API.
Args:
commit_hash: Commit hash to fetch from
file_path: File path in the repository
Returns:
File content as bytes
"""
api_url = f"https://api.github.com/repos/pytorch/pytorch/contents/{file_path}?ref={commit_hash}"
req = urllib.request.Request(api_url)
req.add_header("Accept", "application/vnd.github.v3+json")
req.add_header("User-Agent", "ExecuTorch-Bot")
try:
with urllib.request.urlopen(req) as response:
data = json.loads(response.read().decode())
# Content is base64 encoded
content = base64.b64decode(data["content"])
return content
except urllib.request.HTTPError as e:
print(f"Error fetching file {file_path}: {e}", file=sys.stderr)
raise
def sync_directory(et_dir, pt_path, commit_hash):
"""
Sync files from PyTorch to ExecuTorch using GitHub API.
Only syncs files that already exist in ExecuTorch - does not add new files.
Args:
et_dir: ExecuTorch directory path
pt_path: PyTorch directory path in the repository (e.g., "c10")
commit_hash: Commit hash to fetch from
Returns:
Number of files grafted
"""
files_grafted = 0
print(f"Checking {et_dir} vs pytorch/{pt_path}...")
if not et_dir.exists():
print(f"Warning: ExecuTorch directory {et_dir} does not exist, skipping")
return 0
# Loop through files in ExecuTorch directory
for et_file in et_dir.rglob("*"):
if not et_file.is_file():
continue
# Skip build files
if should_skip_file(et_file.name):
continue
# Construct corresponding path in PyTorch
rel_path = et_file.relative_to(et_dir)
pt_file_path = f"{pt_path}/{rel_path}".replace("\\", "/")
# Fetch content from PyTorch and compare
try:
pt_content = fetch_file_content(commit_hash, pt_file_path)
et_content = et_file.read_bytes()
if pt_content != et_content:
print(f"⚠️ Difference detected in {rel_path}")
print(f"📋 Grafting from PyTorch commit {commit_hash}...")
et_file.write_bytes(pt_content)
print(f"✅ Grafted {et_file}")
files_grafted += 1
except urllib.request.HTTPError as e:
if e.code != 404: # It's ok to have more files in ET than pytorch/pytorch.
print(f"Error fetching {rel_path} from PyTorch: {e}")
except Exception as e:
print(f"Error syncing {rel_path}: {e}")
continue
return files_grafted
def sync_c10_directories(commit_hash):
"""
Sync c10 and torch/headeronly directories from PyTorch to ExecuTorch using GitHub API.
Args:
commit_hash: PyTorch commit hash to sync from
Returns:
Total number of files grafted
"""
print("\n🔄 Syncing c10 directories from PyTorch via GitHub API...")
# Get repository root
repo_root = Path.cwd()
# Define directory pairs to sync (from check_c10_sync.sh)
# Format: (executorch_dir, pytorch_path_in_repo)
dir_pairs = [
(
repo_root / "runtime/core/portable_type/c10/c10",
"c10",
),
(
repo_root / "runtime/core/portable_type/c10/torch/headeronly",
"torch/headeronly",
),
]
total_grafted = 0
for et_dir, pt_path in dir_pairs:
files_grafted = sync_directory(et_dir, pt_path, commit_hash)
total_grafted += files_grafted
if total_grafted > 0:
print(f"\n✅ Successfully grafted {total_grafted} file(s) from PyTorch")
else:
print("\n✅ No differences found - c10 is in sync")
return total_grafted
def main():
try:
# Read NIGHTLY_VERSION from torch_pin.py
nightly_version = get_torch_nightly_version()
print(f"Found NIGHTLY_VERSION: {nightly_version}")
# Parse to date string
date_str = parse_nightly_version(nightly_version)
print(f"Parsed date: {date_str}")
# Fetch commit hash from PyTorch nightly branch
commit_hash = get_commit_hash_for_nightly(date_str)
print(f"Found commit hash: {commit_hash}")
# Update the pin file
update_pytorch_pin(commit_hash)
# Sync c10 directories from PyTorch
sync_c10_directories(commit_hash)
print(
"\n✅ Successfully updated PyTorch commit pin and synced c10 directories!"
)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()