@@ -397,26 +397,25 @@ In the event you wish to create a new installer, you may use the
397
397
following initialization pattern:
398
398
399
399
```
400
- from invokeai.app.services.config import InvokeAIAppConfig
400
+ from invokeai.app.services.config import get_config
401
401
from invokeai.app.services.model_records import ModelRecordServiceSQL
402
402
from invokeai.app.services.model_install import ModelInstallService
403
403
from invokeai.app.services.download import DownloadQueueService
404
- from invokeai.app.services.shared.sqlite import SqliteDatabase
404
+ from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase
405
405
from invokeai.backend.util.logging import InvokeAILogger
406
406
407
- config = InvokeAIAppConfig.get_config()
408
- config.parse_args()
407
+ config = get_config()
409
408
410
409
logger = InvokeAILogger.get_logger(config=config)
411
- db = SqliteDatabase(config, logger)
410
+ db = SqliteDatabase(config.db_path , logger)
412
411
record_store = ModelRecordServiceSQL(db)
413
412
queue = DownloadQueueService()
414
413
queue.start()
415
414
416
- installer = ModelInstallService(app_config=config,
415
+ installer = ModelInstallService(app_config=config,
417
416
record_store=record_store,
418
- download_queue=queue
419
- )
417
+ download_queue=queue
418
+ )
420
419
installer.start()
421
420
```
422
421
@@ -1367,30 +1366,54 @@ the in-memory loaded model:
1367
1366
| ` model ` | AnyModel | The instantiated model (details below) |
1368
1367
| ` locker ` | ModelLockerBase | A context manager that mediates the movement of the model into VRAM |
1369
1368
1370
- Because the loader can return multiple model types, it is typed to
1371
- return ` AnyModel ` , a Union ` ModelMixin ` , ` torch.nn.Module ` ,
1372
- ` IAIOnnxRuntimeModel ` , ` IPAdapter ` , ` IPAdapterPlus ` , and
1373
- ` EmbeddingModelRaw ` . ` ModelMixin ` is the base class of all diffusers
1374
- models, ` EmbeddingModelRaw ` is used for LoRA and TextualInversion
1375
- models. The others are obvious.
1369
+ ### get_model_by_key(key, [ submodel] ) -> LoadedModel
1370
+
1371
+ The ` get_model_by_key() ` method will retrieve the model using its
1372
+ unique database key. For example:
1373
+
1374
+ loaded_model = loader.get_model_by_key('f13dd932c0c35c22dcb8d6cda4203764', SubModelType('vae'))
1375
+
1376
+ ` get_model_by_key() ` may raise any of the following exceptions:
1377
+
1378
+ * ` UnknownModelException ` -- key not in database
1379
+ * ` ModelNotFoundException ` -- key in database but model not found at path
1380
+ * ` NotImplementedException ` -- the loader doesn't know how to load this type of model
1381
+
1382
+ ### Using the Loaded Model in Inference
1376
1383
1377
1384
` LoadedModel ` acts as a context manager. The context loads the model
1378
1385
into the execution device (e.g. VRAM on CUDA systems), locks the model
1379
1386
in the execution device for the duration of the context, and returns
1380
1387
the model. Use it like this:
1381
1388
1382
1389
```
1383
- model_info = loader.get_model_by_key('f13dd932c0c35c22dcb8d6cda4203764', SubModelType('vae'))
1384
- with model_info as vae:
1390
+ loaded_model_ = loader.get_model_by_key('f13dd932c0c35c22dcb8d6cda4203764', SubModelType('vae'))
1391
+ with loaded_model as vae:
1385
1392
image = vae.decode(latents)[0]
1386
1393
```
1387
1394
1388
- ` get_model_by_key() ` may raise any of the following exceptions:
1395
+ The object returned by the LoadedModel context manager is an
1396
+ ` AnyModel ` , which is a Union of ` ModelMixin ` , ` torch.nn.Module ` ,
1397
+ ` IAIOnnxRuntimeModel ` , ` IPAdapter ` , ` IPAdapterPlus ` , and
1398
+ ` EmbeddingModelRaw ` . ` ModelMixin ` is the base class of all diffusers
1399
+ models, ` EmbeddingModelRaw ` is used for LoRA and TextualInversion
1400
+ models. The others are obvious.
1401
+
1402
+ In addition, you may call ` LoadedModel.model_on_device() ` , a context
1403
+ manager that returns a tuple of the model's state dict in CPU and the
1404
+ model itself in VRAM. It is used to optimize the LoRA patching and
1405
+ unpatching process:
1406
+
1407
+ ```
1408
+ loaded_model_= loader.get_model_by_key('f13dd932c0c35c22dcb8d6cda4203764', SubModelType('vae'))
1409
+ with loaded_model.model_on_device() as (state_dict, vae):
1410
+ image = vae.decode(latents)[0]
1411
+ ```
1412
+
1413
+ Since not all models have state dicts, the ` state_dict ` return value
1414
+ can be None.
1415
+
1389
1416
1390
- * ` UnknownModelException ` -- key not in database
1391
- * ` ModelNotFoundException ` -- key in database but model not found at path
1392
- * ` NotImplementedException ` -- the loader doesn't know how to load this type of model
1393
-
1394
1417
### Emitting model loading events
1395
1418
1396
1419
When the ` context ` argument is passed to ` load_model_*() ` , it will
@@ -1578,3 +1601,59 @@ This method takes a model key, looks it up using the
1578
1601
` ModelRecordServiceBase ` object in ` mm.store ` , and passes the returned
1579
1602
model configuration to ` load_model_by_config() ` . It may raise a
1580
1603
` NotImplementedException ` .
1604
+
1605
+ ## Invocation Context Model Manager API
1606
+
1607
+ Within invocations, the following methods are available from the
1608
+ ` InvocationContext ` object:
1609
+
1610
+ ### context.download_and_cache_model(source) -> Path
1611
+
1612
+ This method accepts a ` source ` of a remote model, downloads and caches
1613
+ it locally, and then returns a Path to the local model. The source can
1614
+ be a direct download URL or a HuggingFace repo_id.
1615
+
1616
+ In the case of HuggingFace repo_id, the following variants are
1617
+ recognized:
1618
+
1619
+ * stabilityai/stable-diffusion-v4 -- default model
1620
+ * stabilityai/stable-diffusion-v4: fp16 -- fp16 variant
1621
+ * stabilityai/stable-diffusion-v4:fp16: vae -- the fp16 vae subfolder
1622
+ * stabilityai/stable-diffusion-v4:onnx: vae -- the onnx variant vae subfolder
1623
+
1624
+ You can also point at an arbitrary individual file within a repo_id
1625
+ directory using this syntax:
1626
+
1627
+ * stabilityai/stable-diffusion-v4::/checkpoints/sd4.safetensors
1628
+
1629
+ ### context.load_local_model(model_path, [ loader] ) -> LoadedModel
1630
+
1631
+ This method loads a local model from the indicated path, returning a
1632
+ ` LoadedModel ` . The optional loader is a Callable that accepts a Path
1633
+ to the object, and returns a ` AnyModel ` object. If no loader is
1634
+ provided, then the method will use ` torch.load() ` for a .ckpt or .bin
1635
+ checkpoint file, ` safetensors.torch.load_file() ` for a safetensors
1636
+ checkpoint file, or ` cls.from_pretrained() ` for a directory that looks
1637
+ like a diffusers directory.
1638
+
1639
+ ### context.load_remote_model(source, [ loader] ) -> LoadedModel
1640
+
1641
+ This method accepts a ` source ` of a remote model, downloads and caches
1642
+ it locally, loads it, and returns a ` LoadedModel ` . The source can be a
1643
+ direct download URL or a HuggingFace repo_id.
1644
+
1645
+ In the case of HuggingFace repo_id, the following variants are
1646
+ recognized:
1647
+
1648
+ * stabilityai/stable-diffusion-v4 -- default model
1649
+ * stabilityai/stable-diffusion-v4: fp16 -- fp16 variant
1650
+ * stabilityai/stable-diffusion-v4:fp16: vae -- the fp16 vae subfolder
1651
+ * stabilityai/stable-diffusion-v4:onnx: vae -- the onnx variant vae subfolder
1652
+
1653
+ You can also point at an arbitrary individual file within a repo_id
1654
+ directory using this syntax:
1655
+
1656
+ * stabilityai/stable-diffusion-v4::/checkpoints/sd4.safetensors
1657
+
1658
+
1659
+
0 commit comments