MCPcopy
hub / github.com/tensorflow/tfjs / cloneModelInternal

Function cloneModelInternal

tfjs-core/src/io/model_management.ts:107–161  ·  view source on GitHub ↗
(
    sourceURL: string, destURL: string,
    deleteSource = false)

Source from the content-addressed store, hash-verified

105}
106
107async function cloneModelInternal(
108 sourceURL: string, destURL: string,
109 deleteSource = false): Promise<ModelArtifactsInfo> {
110 assert(
111 sourceURL !== destURL,
112 () => `Old path and new path are the same: '${sourceURL}'`);
113
114 const loadHandlers = IORouterRegistry.getLoadHandlers(sourceURL);
115 assert(
116 loadHandlers.length > 0,
117 () => `Copying failed because no load handler is found for source URL ${
118 sourceURL}.`);
119 assert(
120 loadHandlers.length < 2,
121 () => `Copying failed because more than one (${loadHandlers.length}) ` +
122 `load handlers for source URL ${sourceURL}.`);
123 const loadHandler = loadHandlers[0];
124
125 const saveHandlers = IORouterRegistry.getSaveHandlers(destURL);
126 assert(
127 saveHandlers.length > 0,
128 () => `Copying failed because no save handler is found for destination ` +
129 `URL ${destURL}.`);
130 assert(
131 saveHandlers.length < 2,
132 () => `Copying failed because more than one (${loadHandlers.length}) ` +
133 `save handlers for destination URL ${destURL}.`);
134 const saveHandler = saveHandlers[0];
135
136 const sourceScheme = parseURL(sourceURL).scheme;
137 const sourcePath = parseURL(sourceURL).path;
138 const sameMedium = sourceScheme === parseURL(sourceURL).scheme;
139
140 const modelArtifacts = await loadHandler.load();
141
142 // If moving within the same storage medium, remove the old model as soon as
143 // the loading is done. Without doing this, it is possible that the combined
144 // size of the two models will cause the cloning to fail.
145 if (deleteSource && sameMedium) {
146 await ModelStoreManagerRegistry.getManager(sourceScheme)
147 .removeModel(sourcePath);
148 }
149
150 const saveResult = await saveHandler.save(modelArtifacts);
151
152 // If moving between mediums, the deletion is done after the save succeeds.
153 // This guards against the case in which saving to the destination medium
154 // fails.
155 if (deleteSource && !sameMedium) {
156 await ModelStoreManagerRegistry.getManager(sourceScheme)
157 .removeModel(sourcePath);
158 }
159
160 return saveResult.modelArtifactsInfo;
161}
162
163/**
164 * List all models stored in registered storage mediums.

Callers 2

copyModelFunction · 0.85
moveModelFunction · 0.85

Calls 8

assertFunction · 0.90
parseURLFunction · 0.85
getLoadHandlersMethod · 0.80
getSaveHandlersMethod · 0.80
getManagerMethod · 0.80
removeModelMethod · 0.65
loadMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…