aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorAUTOMATIC1111 <16777216c@gmail.com>2023-01-10 02:02:19 +0300
committerGitHub <noreply@github.com>2023-01-10 02:02:19 +0300
commitb1d976dca2d866f9c9bb5d21c110d279565ed9c7 (patch)
tree2c31bd673e786bdd50532883f5fcfa50f4a1bbb9 /modules
parent50f91c294514149ff090003775e28f9af4e03dc1 (diff)
parent95727312ca5913876aa1c74f47d1ff6d93bb6b1f (diff)
Merge pull request #6466 from vladmandic/api-get-memory
Implement API get-memory
Diffstat (limited to 'modules')
-rw-r--r--modules/api/api.py35
-rw-r--r--modules/api/models.py4
2 files changed, 39 insertions, 0 deletions
diff --git a/modules/api/api.py b/modules/api/api.py
index 5b6125f8..6c564ad8 100644
--- a/modules/api/api.py
+++ b/modules/api/api.py
@@ -135,6 +135,7 @@ class Api:
self.add_api_route("/sdapi/v1/preprocess", self.preprocess, methods=["POST"], response_model=PreprocessResponse)
self.add_api_route("/sdapi/v1/train/embedding", self.train_embedding, methods=["POST"], response_model=TrainResponse)
self.add_api_route("/sdapi/v1/train/hypernetwork", self.train_hypernetwork, methods=["POST"], response_model=TrainResponse)
+ self.add_api_route("/sdapi/v1/memory", self.get_memory, methods=["GET"], response_model=MemoryResponse)
def add_api_route(self, path: str, endpoint, **kwargs):
if shared.cmd_opts.api_auth:
@@ -501,6 +502,40 @@ class Api:
shared.state.end()
return TrainResponse(info = "train embedding error: {error}".format(error = error))
+ def get_memory(self):
+ try:
+ import os, psutil
+ process = psutil.Process(os.getpid())
+ res = process.memory_info() # only rss is cross-platform guaranteed so we dont rely on other values
+ ram_total = 100 * res.rss / process.memory_percent() # and total memory is calculated as actual value is not cross-platform safe
+ ram = { 'free': ram_total - res.rss, 'used': res.rss, 'total': ram_total }
+ except Exception as err:
+ ram = { 'error': f'{err}' }
+ try:
+ import torch
+ if torch.cuda.is_available():
+ s = torch.cuda.mem_get_info()
+ system = { 'free': s[0], 'used': s[1] - s[0], 'total': s[1] }
+ s = dict(torch.cuda.memory_stats(shared.device))
+ allocated = { 'current': s['allocated_bytes.all.current'], 'peak': s['allocated_bytes.all.peak'] }
+ reserved = { 'current': s['reserved_bytes.all.current'], 'peak': s['reserved_bytes.all.peak'] }
+ active = { 'current': s['active_bytes.all.current'], 'peak': s['active_bytes.all.peak'] }
+ inactive = { 'current': s['inactive_split_bytes.all.current'], 'peak': s['inactive_split_bytes.all.peak'] }
+ warnings = { 'retries': s['num_alloc_retries'], 'oom': s['num_ooms'] }
+ cuda = {
+ 'system': system,
+ 'active': active,
+ 'allocated': allocated,
+ 'reserved': reserved,
+ 'inactive': inactive,
+ 'events': warnings,
+ }
+ else:
+ cuda = { 'error': 'unavailable' }
+ except Exception as err:
+ cuda = { 'error': f'{err}' }
+ return MemoryResponse(ram = ram, cuda = cuda)
+
def launch(self, server_name, port):
self.app.include_router(self.router)
uvicorn.run(self.app, host=server_name, port=port)
diff --git a/modules/api/models.py b/modules/api/models.py
index ce43c858..880edde6 100644
--- a/modules/api/models.py
+++ b/modules/api/models.py
@@ -260,3 +260,7 @@ class EmbeddingItem(BaseModel):
class EmbeddingsResponse(BaseModel):
loaded: Dict[str, EmbeddingItem] = Field(title="Loaded", description="Embeddings loaded for the current model")
skipped: Dict[str, EmbeddingItem] = Field(title="Skipped", description="Embeddings skipped for the current model (likely due to architecture incompatibility)")
+
+class MemoryResponse(BaseModel):
+ ram: dict[str, str] | dict[str, float] = Field(title="RAM", description="System memory stats")
+ cuda: dict[str, str] | dict[str, dict] = Field(title="CUDA", description="nVidia CUDA memory stats")