UniBuild / unibuild_data.py
WeiHuang95's picture
Prepare all published UniBuild components as compatible LMDB datasets
5316050 verified
Raw History Blame Contribute Delete
1.94 kB
# SPDX-License-Identifier: MIT
"""Apply prepared Hugging Face data paths without changing training parameters."""
import json
import os
from pathlib import Path
def apply_release_config(config, config_file):
"""Return False for an ordinary local install; apply a prepared release otherwise."""
code_root = Path(config_file).resolve().parent
explicit = os.environ.get('UNIBUILD_RELEASE_CONFIG')
registry = Path(explicit).expanduser() if explicit else code_root / 'data/unibuild_release.json'
if not registry.is_file():
if explicit:
raise FileNotFoundError(f'UNIBUILD_RELEASE_CONFIG does not exist: {registry}')
return False
state = json.loads(registry.read_text(encoding='utf-8'))
if state.get('format_version') != 1:
raise ValueError(f'Unsupported prepared UniBuild data registry: {registry}')
prepared = {}
for name, original in state['datasets_dict'].items():
entry = dict(original)
entry['class_dict'] = {int(k): tuple(v) for k, v in entry['class_dict'].items()}
for key in ['data_dir', 'split_dir']:
path = Path(entry[key])
entry[key] = str(path if path.is_absolute() else code_root / path)
# Older prepared releases default to ordinary files; new ones record LMDB mode.
entry['use_lmdb'] = bool(entry.get('use_lmdb', False))
if not Path(entry['data_dir']).is_dir() or not Path(entry['split_dir']).is_dir():
raise FileNotFoundError(f'Prepared dataset paths are missing for {name}; rerun prepare_for_unibuild.py')
prepared[name] = entry
selected = state['datasets']
if not selected or any(name not in prepared for name in selected):
raise ValueError('Prepared dataset selection is empty or invalid')
config.datasets_dict.update(prepared)
config.datasets = list(selected)
config.unibuild_release_registry = str(registry.resolve())
return True