text
stringlengths
0
1.47k
# result would otherwise keep the old clip set alive in the cond and
# leave the dropped clip's NPZ in motions/, which the training loader
# enumerates directly. Basenames only, so relocating data_dir (the
# export dirs are symlinks) does not invalidate every entry.
params['clips_digest'] = _digest(
sorted(os.path.basename(p) for p in task['object_npzs']))
# Only objects with a head trim carry the key, so adding clip_trims.txt
# re-processes exactly those object types and leaves every other cached
# result valid.
if task.get('head_trims'):
params['head_trims_digest'] = _digest(task['head_trims'])
# Same for activity_keep.txt: only objects owning a listed clip carry it.
if task.get('activity_keep'):
params['activity_keep_digest'] = _digest(sorted(task['activity_keep']))
# Root offsets are baked into the export NPZs in place (same names), so
# neither clips_digest nor any list above sees them; the quats recorded in
# the NPZs are the only trace. Adding, changing or removing one therefore
# re-processes exactly the owning object type.
if task.get('root_offsets'):
params['root_offsets_digest'] = _digest(task['root_offsets'])
# Rest orientation fixes are baked into the NPZs the same way.
if task.get('rest_orientations'):
params['rest_orientations_digest'] = _digest(task['rest_orientations'])
return params
# Written into an export NPZ by tools/patch_annotations.py (apply_root_offsets)
# when it bakes a root orientation fix in; keep the name in sync.
ROOT_OFFSET_KEY = 'root_offset_applied'
# Written by tools/patch_annotations.py (apply_rest_orientations); keep in sync.
REST_ORIENTATION_KEY = 'rest_orientation_applied'
def read_root_offsets(object_npzs, key=ROOT_OFFSET_KEY):
"""``{clip stem: [w, x, y, z]}`` for the export NPZs carrying a baked-in
fix under *key* (a root offset by default, or a rest orientation). Only the
zip directory is read for the others, so this stays cheap over a whole
dataset."""
offsets = {}
for path in object_npzs:
with zipfile.ZipFile(path) as zf:
if key + '.npy' not in zf.namelist():
continue
with np.load(path) as d:
offsets[os.path.basename(path)[:-4]] = [round(float(x), 6) for x in d[key]]
return offsets
def _params_hash(params):
"""Stable hash of the cache-invalidating settings."""
blob = json.dumps(params, sort_keys=True, default=str)
return hashlib.sha1(blob.encode('utf-8')).hexdigest()
def _export_digest(task):
"""Digest of the inputs the settings hash cannot see: each clip NPZ's name,
size and mtime (a re-export under the same clip names) and the object's
``joint_names.json`` entry (its order drives the clean-name realignment).
Kept beside ``params_hash`` rather than in it (see :func:`_load_cached_payload`)."""
files = []
for path in sorted(task['object_npzs']):
st = os.stat(path)
files.append([os.path.basename(path), st.st_size, st.st_mtime_ns])
return _digest([files, task.get('expected_names')])
def _load_cached_payload(part_path, object_type, params, params_hash, export_digest,
adopt_legacy=False):
"""Return the cache entry of an object (``result`` and what it was built
from), or None when absent / stale / unreadable.
An entry without an ``export_digest`` cannot show that it was built from
the current export, so it is a miss unless *adopt_legacy*
(``--adopt_legacy_cache``) vouches for it; it is then stamped with the
current digest."""
if not os.path.isfile(part_path):
return None
try:
payload = np.load(part_path, allow_pickle=True).item()
except Exception as e: # noqa: BLE001 — a bad cache entry is just a miss
logger.warning(f'[{object_type}] unreadable cache {part_path} ({e}); '
f're-processing')
return None
if 'params_hash' not in payload or 'result' not in payload:
logger.info(f'[{object_type}] cache invalidated (entry predates settings '
f'tracking or holds no result); re-processing')
return None
if payload['params_hash'] == params_hash:
cached_digest = payload.get('export_digest')
if cached_digest is None:
if not adopt_legacy:
logger.info(f'[{object_type}] cache entry records no export digest; '
f're-processing (--adopt_legacy_cache keeps entries known to '
f'match the current export)')
return None
payload['export_digest'] = export_digest
atomic_np_save(part_path, payload)
return payload
if cached_digest == export_digest: