Cion-lab commited on
Commit
5282366
·
verified ·
1 Parent(s): 41ec03f

hubckpt: bounded Range read-back in verify_checkpoint (probe v5 measured a 797s full-object download; the run cannot afford eleven of those)

Browse files
Files changed (1) hide show
  1. train/hubckpt.py +38 -18
train/hubckpt.py CHANGED
@@ -144,30 +144,50 @@ def verify_checkpoint(repo, dirpath, path_in_repo, token=None, repo_type="datase
144
  rb = None
145
  rb_ok = False
146
  if readback_one and have:
147
- # Read back the LARGEST file and re-hash it. In a Trainer checkpoint that is the fp16
148
- # model.safetensors -- an LFS object -- and it is the only file whose retrievability the Hub's
149
- # listing cannot establish, because the listing reports the oid the commit *claims*. Picking the
150
- # cheapest file (cursor.json, a few dozen git-stored bytes) proved the git tree while leaving
151
- # 1.7 GB of weights untested, and `ok=True` then authorised deleting the only local copy (E-031).
152
- # The token is sent when there is one, so this also works if the repo is ever made private.
 
 
 
 
 
 
 
 
 
153
  pick = max(have, key=lambda k: (have[k][0] or 0))
154
- tmp = os.path.join("/tmp", "hubverify_" + os.path.basename(pick))
155
  url = f"{HUB}/{repo_type}s/{repo}/resolve/main/{prefix}{pick}"
 
 
 
156
  try:
157
- rq = urllib.request.Request(url)
158
- if token:
159
- rq.add_header("Authorization", f"Bearer {token}")
160
- with urllib.request.urlopen(rq, timeout=300) as fh, open(tmp, "wb") as out:
161
- shutil.copyfileobj(fh, out, 1 << 20)
162
- got = sha256_file(tmp)
163
- rb_ok = bool(pick in want and got == want[pick][1])
164
- rb = {"path": pick, "bytes": os.path.getsize(tmp), "sha_matches_manifest": rb_ok}
 
 
 
 
 
 
 
 
 
 
 
165
  except Exception as e:
166
  rb = {"path": pick, "url": url,
167
  "error": f"{type(e).__name__}: {str(e)[:160]}"}
168
- finally:
169
- if os.path.exists(tmp):
170
- os.remove(tmp)
171
  # ok must include the read-back: a repo whose files are listed but not downloadable is not a
172
  # checkpoint, and pruning into that state loses the run. `extra` is gated too: a file on the Hub that
173
  # is not in this directory means the path being pushed to already held something else, which is a
 
144
  rb = None
145
  rb_ok = False
146
  if readback_one and have:
147
+ # Read back a bounded slice of the LARGEST file, from two offsets, and compare it byte for byte.
148
+ # The whole-file content proof is already above: for every LFS object the Hub's `lfs.oid` *is* the
149
+ # sha256 of the bytes it stores, and that is compared against the local sha256. What the listing
150
+ # cannot establish is the retrieval path -- that `resolve/` really serves this object, because the
151
+ # oid is what the commit *claims*. Picking the cheapest file (cursor.json, a few dozen git-stored
152
+ # bytes) proved the git tree while leaving 1.7 GB of weights untested, and `ok=True` then
153
+ # authorised deleting the only local copy (E-031).
154
+ #
155
+ # It used to download the entire largest file to prove that, and probe v5 measured what the tail
156
+ # of that costs on this link: `CKPT 10 ... push+verify=797.7s` for an 850 MB optimizer.pt, against
157
+ # 26 s for the identical cycle in the next leg when the CDN was warm. The run has eleven of those
158
+ # cycles and the weekly quota has ~4 hours of slack, so a cold cache must not be able to eat two
159
+ # of them. Two 4 MiB ranges -- head and mid-file, since a truncated object still has a good head --
160
+ # answer the same question, and a response that is not 206 with exactly the bytes asked for is a
161
+ # failure rather than a silent full download.
162
  pick = max(have, key=lambda k: (have[k][0] or 0))
163
+ size = have[pick][0] or 0
164
  url = f"{HUB}/{repo_type}s/{repo}/resolve/main/{prefix}{pick}"
165
+ chunk = 4 << 20
166
+ spans = [(0, size - 1)] if size <= chunk else [(0, chunk - 1),
167
+ (size // 2, min(size, size // 2 + chunk) - 1)]
168
  try:
169
+ with open(os.path.join(dirpath, pick), "rb") as loc:
170
+ for start, end in spans:
171
+ rq = urllib.request.Request(url)
172
+ if token:
173
+ rq.add_header("Authorization", f"Bearer {token}")
174
+ rq.add_header("Range", f"bytes={start}-{end}")
175
+ with urllib.request.urlopen(rq, timeout=180) as fh:
176
+ status = getattr(fh, "status", 200)
177
+ data = fh.read((end - start + 1) + 1)
178
+ want_n = end - start + 1
179
+ if status != 206 or len(data) != want_n:
180
+ raise RuntimeError(f"range {start}-{end} returned HTTP {status} with "
181
+ f"{len(data)} bytes; expected 206 with exactly {want_n}")
182
+ loc.seek(start)
183
+ if loc.read(want_n) != data:
184
+ raise RuntimeError(f"bytes {start}-{end} of {pick} differ from the local file")
185
+ rb_ok = True
186
+ rb = {"path": pick, "ranges": [[s, e] for s, e in spans],
187
+ "bytes": sum(e - s + 1 for s, e in spans), "file_bytes": size}
188
  except Exception as e:
189
  rb = {"path": pick, "url": url,
190
  "error": f"{type(e).__name__}: {str(e)[:160]}"}
 
 
 
191
  # ok must include the read-back: a repo whose files are listed but not downloadable is not a
192
  # checkpoint, and pruning into that state loses the run. `extra` is gated too: a file on the Hub that
193
  # is not in this directory means the path being pushed to already held something else, which is a