|
| 1 | +# |
| 2 | +# Utility for migrating from the old to the new model store |
| 3 | +# |
| 4 | +import os |
| 5 | +import shutil |
| 6 | + |
| 7 | +from ramalama.common import generate_sha256 |
| 8 | +from ramalama.model import MODEL_TYPES |
| 9 | +from ramalama.model_factory import ModelFactory |
| 10 | +from ramalama.model_store import GlobalModelStore, SnapshotFile, SnapshotFileType |
| 11 | + |
| 12 | + |
| 13 | +class dotdict(dict): |
| 14 | + """dot.notation access to dictionary attributes""" |
| 15 | + |
| 16 | + __getattr__ = dict.get |
| 17 | + __setattr__ = dict.__setitem__ |
| 18 | + __delattr__ = dict.__delitem__ |
| 19 | + |
| 20 | + |
| 21 | +class ModelStoreImport: |
| 22 | + |
| 23 | + def __init__(self, store_path: str): |
| 24 | + self.store_path = store_path |
| 25 | + self._old_model_path = os.path.join(store_path, "models") |
| 26 | + self._old_repo_path = os.path.join(store_path, "repos") |
| 27 | + self._global_store = GlobalModelStore(self.store_path) |
| 28 | + |
| 29 | + class LocalModelFile(SnapshotFile): |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, url, header, hash, name, should_show_progress=False, should_verify_checksum=False, required=True |
| 33 | + ): |
| 34 | + super().__init__( |
| 35 | + url, header, hash, name, SnapshotFileType.Model, should_show_progress, should_verify_checksum, required |
| 36 | + ) |
| 37 | + |
| 38 | + def download(self, blob_file_path, snapshot_dir): |
| 39 | + if not os.path.exists(self.url): |
| 40 | + raise FileNotFoundError(f"No such file: '{self.url}'") |
| 41 | + # moving from the local location to blob directory so the model store "owns" the data |
| 42 | + shutil.copy(self.url, blob_file_path) |
| 43 | + return os.path.relpath(blob_file_path, start=snapshot_dir) |
| 44 | + |
| 45 | + def import_all(self): |
| 46 | + if not os.path.exists(self._old_model_path): |
| 47 | + return |
| 48 | + |
| 49 | + print("Starting importing AI models to new store...") |
| 50 | + for root, _, files in os.walk(self._old_model_path): |
| 51 | + if not files: |
| 52 | + continue |
| 53 | + |
| 54 | + try: |
| 55 | + # reconstruct the cli model input |
| 56 | + model = root.replace(self._old_model_path, "") |
| 57 | + model = model.replace(os.sep, "", 1) |
| 58 | + if model.startswith("file/"): |
| 59 | + model = model.replace("/", ":///", 1) |
| 60 | + else: |
| 61 | + model = model.replace("/", "://", 1) |
| 62 | + |
| 63 | + if model in MODEL_TYPES: |
| 64 | + model = "" |
| 65 | + |
| 66 | + for file in files: |
| 67 | + m = ModelFactory( |
| 68 | + os.path.join(model, file), |
| 69 | + args=dotdict( |
| 70 | + { |
| 71 | + "store": self.store_path, |
| 72 | + "use_model_store": True, |
| 73 | + "engine": "podman", |
| 74 | + "container": True, |
| 75 | + } |
| 76 | + ), |
| 77 | + ).create() |
| 78 | + _, model_tag, _ = m.extract_model_identifiers() |
| 79 | + _, _, all = m.store.get_cached_files(model_tag) |
| 80 | + if all: |
| 81 | + print(f"Already imported: {root}/{file}") |
| 82 | + continue |
| 83 | + |
| 84 | + snapshot_hash = generate_sha256(file) |
| 85 | + old_model_path = os.path.join(root, file) |
| 86 | + |
| 87 | + files: list[SnapshotFile] = [] |
| 88 | + files.append( |
| 89 | + ModelStoreImport.LocalModelFile( |
| 90 | + url=old_model_path, |
| 91 | + header={}, |
| 92 | + hash=snapshot_hash, |
| 93 | + name=file, |
| 94 | + required=True, |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + m.store.new_snapshot(model_tag, snapshot_hash, files) |
| 99 | + print(f"Imported {old_model_path} -> {m.store.get_snapshot_file_path(snapshot_hash, file)}") |
| 100 | + |
| 101 | + except Exception as ex: |
| 102 | + print(f"Failed to import {root}: {ex}") |
| 103 | + |
| 104 | + if os.path.exists(self._old_model_path): |
| 105 | + try: |
| 106 | + shutil.rmtree(self._old_model_path) |
| 107 | + except Exception as ex: |
| 108 | + print(f"Failed to remove old model directory: {ex}") |
| 109 | + if os.path.exists(self._old_repo_path): |
| 110 | + try: |
| 111 | + shutil.rmtree(self._old_repo_path) |
| 112 | + except Exception as ex: |
| 113 | + print(f"Failed to remove old blob directory: {ex}") |
0 commit comments