Skip to content

Commit

Permalink
Merge branch 'main' into text-input-change
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jul 20, 2023
2 parents f7da79a + 5fc0d48 commit 75d433c
Show file tree
Hide file tree
Showing 20 changed files with 631 additions and 684 deletions.
6 changes: 6 additions & 0 deletions backend/src/dependencies/install_server_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
"version": "23.2.0",
"from_file": "chainner_pip-23.2.0-py3-none-any.whl",
},
{
"package_name": "psutil",
"display_name": "psutil",
"version": "5.9.5",
"from_file": None,
},
]

install_dependencies_sync(deps)
4 changes: 4 additions & 0 deletions backend/src/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def __init__(self):
def __del__(self):
nv.nvmlShutdown()

@property
def num_gpus(self):
return self.__num_gpus

def list_gpus(self) -> List[str]:
return [gpu.name for gpu in self.__gpus]

Expand Down
32 changes: 29 additions & 3 deletions backend/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import sys
import traceback
from concurrent.futures import ThreadPoolExecutor
from dataclasses import asdict, dataclass
from json import dumps as stringify
from typing import Dict, List, Optional, TypedDict, Union

import psutil
from sanic import Sanic
from sanic.log import access_logger, logger
from sanic.request import Request
Expand All @@ -23,6 +25,7 @@
from custom_types import UpdateProgressFn
from dependencies.store import DependencyInfo, install_dependencies, installed_packages
from events import EventQueue, ExecutionErrorData
from gpu import get_nvidia_helper
from nodes.group import Group
from nodes.utils.exec_options import (
JsonExecutionOptions,
Expand Down Expand Up @@ -378,9 +381,6 @@ async def list_nvidia_gpus(_request: Request):
"""Lists the available GPUs for NCNN"""
await nodes_available()
try:
# pylint: disable=import-outside-toplevel
from gpu import get_nvidia_helper

nv = get_nvidia_helper()

if nv is None:
Expand All @@ -401,6 +401,32 @@ async def python_info(_request: Request):
return json({"python": sys.executable, "version": version})


@dataclass
class SystemStat:
label: str
percent: float


@app.route("/system-usage", methods=["GET"])
async def system_usage(_request: Request):
stats_list = []
cpu_usage = psutil.cpu_percent()
mem_usage = psutil.virtual_memory().percent
stats_list.append(SystemStat("CPU", cpu_usage))
stats_list.append(SystemStat("RAM", mem_usage))
nv = get_nvidia_helper()
if nv is not None:
for i in range(nv.num_gpus):
total, used, _ = nv.get_current_vram_usage(i)
stats_list.append(
SystemStat(
f"VRAM {i}" if nv.num_gpus > 1 else "VRAM",
used / total * 100,
)
)
return json([asdict(x) for x in stats_list])


@app.route("/dependencies", methods=["GET"])
async def get_dependencies(_request: Request):
await nodes_available()
Expand Down
4 changes: 4 additions & 0 deletions src/common/Backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ export class Backend {
return this.fetchJson('/python-info', 'GET');
}

systemUsage(): Promise<{ label: string; percent: number }[]> {
return this.fetchJson('/system-usage', 'GET');
}

dependencies(): Promise<Package[]> {
return this.fetchJson('/dependencies', 'GET');
}
Expand Down
5 changes: 0 additions & 5 deletions src/common/safeIpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ipcMain as unsafeIpcMain,
ipcRenderer as unsafeIpcRenderer,
} from 'electron';
import { Systeminformation } from 'systeminformation';
import { FileOpenResult, FileSaveResult, PythonInfo, Version } from './common-types';
import { ParsedSaveData, SaveData } from './SaveFile';
import { Progress } from './ui/progress';
Expand All @@ -20,15 +19,11 @@ interface ChannelInfo<ReturnType, Args extends unknown[] = []> {
type SendChannelInfo<Args extends unknown[] = []> = ChannelInfo<void, Args>;

export interface InvokeChannels {
'get-nvidia-gpu-name': ChannelInfo<string | null>;
'get-nvidia-gpus': ChannelInfo<string[] | null>;
'get-gpu-info': ChannelInfo<Systeminformation.GraphicsData>;
'get-python': ChannelInfo<PythonInfo>;
'get-port': ChannelInfo<number>;
'get-localstorage-location': ChannelInfo<string>;
'refresh-nodes': ChannelInfo<boolean>;
'get-app-version': ChannelInfo<Version>;
'get-vram-usage': ChannelInfo<number | null>;
'dir-select': ChannelInfo<Electron.OpenDialogReturnValue, [dirPath: string]>;
'file-select': ChannelInfo<
Electron.OpenDialogReturnValue,
Expand Down
2 changes: 2 additions & 0 deletions src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,5 @@ export const getInputValue = <T extends NonNullable<InputValue>>(

export const isAutoInput = (input: Input): boolean =>
input.kind === 'generic' && input.optional && !input.hasHandle;

export const escapeRegExp = (string: string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
4 changes: 1 addition & 3 deletions src/main/backend/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ const setupOwnedBackend = async (
token: ProgressToken,
useSystemPython: boolean,
systemPythonLocation: string | undefined | null,
hasNvidia: () => Promise<boolean>,
rootDir: string
): Promise<OwnedBackendProcess> => {
token.submitProgress({
Expand Down Expand Up @@ -226,15 +225,14 @@ export const setupBackend = async (
token: ProgressToken,
useSystemPython: boolean,
systemPythonLocation: string | undefined | null,
hasNvidia: () => Promise<boolean>,
rootDir: string,
noOwnedBackend: boolean
): Promise<BackendProcess> => {
token.submitProgress({ totalProgress: 0 });

const backend = noOwnedBackend
? await setupBorrowedBackend(token, 8000)
: await setupOwnedBackend(token, useSystemPython, systemPythonLocation, hasNvidia, rootDir);
: await setupOwnedBackend(token, useSystemPython, systemPythonLocation, rootDir);

token.submitProgress({ totalProgress: 1 });
return backend;
Expand Down
17 changes: 0 additions & 17 deletions src/main/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { assertNever, delay } from '../../common/util';
import { RunArguments } from '../arguments';
import { BackendProcess } from '../backend/process';
import { setupBackend } from '../backend/setup';
import { getNvidiaGpuNames, getNvidiaSmi } from '../nvidiaSmi';
import { getRootDirSync } from '../platform';
import { settingStorage } from '../setting-storage';
import { Exit } from './exit';
Expand Down Expand Up @@ -67,30 +66,14 @@ const addProgressListeners = (monitor: ProgressMonitor) => {
});
};

const getNvidiaGPUs = async () => {
const nvidiaSmi = await getNvidiaSmi();

if (nvidiaSmi) {
try {
return await getNvidiaGpuNames(nvidiaSmi);
} catch (error) {
log.error(error);
}
}
return undefined;
};

const createBackend = async (token: ProgressToken, args: RunArguments) => {
const useSystemPython = settingStorage.getItem('use-system-python') === 'true';
const systemPythonLocation = settingStorage.getItem('system-python-location');

const hasNvidia = getNvidiaGPUs().then((gpus) => gpus !== undefined);

return setupBackend(
token,
useSystemPython,
systemPythonLocation,
() => hasNvidia,
getRootDirSync(),
args.noBackend
);
Expand Down
46 changes: 0 additions & 46 deletions src/main/gui/main-window.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ChildProcessWithoutNullStreams } from 'child_process';
import { BrowserWindow, app, dialog, nativeTheme, powerSaveBlocker, shell } from 'electron';
import EventSource from 'eventsource';
import { t } from 'i18next';
Expand All @@ -12,10 +11,8 @@ import { ProgressController, ProgressToken, SubProgress } from '../../common/ui/
import { OpenArguments, parseArgs } from '../arguments';
import { BackendProcess } from '../backend/process';
import { setupBackend } from '../backend/setup';
import { createNvidiaSmiVRamChecker, getNvidiaGpuNames, getNvidiaSmi } from '../nvidiaSmi';
import { getRootDirSync } from '../platform';
import { settingStorage, settingStorageLocation } from '../setting-storage';
import { getGpuInfo } from '../systemInfo';
import { MenuData, setMainMenu } from './menu';
import { addSplashScreen } from './splash';

Expand All @@ -27,7 +24,6 @@ const registerEventHandlerPreSetup = (
) => {
ipcMain.handle('get-app-version', () => version);
ipcMain.handle('get-appdata', () => getRootDirSync());
ipcMain.handle('get-gpu-info', getGpuInfo);
ipcMain.handle('get-localstorage-location', () => settingStorageLocation);
ipcMain.handle('refresh-nodes', () => args.refresh);

Expand Down Expand Up @@ -230,56 +226,14 @@ const registerEventHandlerPostSetup = (
});
};

const checkNvidiaSmi = async () => {
const registerEmptyGpuEvents = () => {
ipcMain.handle('get-nvidia-gpu-name', () => null);
ipcMain.handle('get-nvidia-gpus', () => null);
ipcMain.handle('get-vram-usage', () => null);
};

const registerNvidiaSmiEvents = async (nvidiaSmi: string) => {
const nvidiaGpus = await getNvidiaGpuNames(nvidiaSmi);
ipcMain.handle('get-nvidia-gpu-name', () => nvidiaGpus[0].trim());
ipcMain.handle('get-nvidia-gpus', () => nvidiaGpus.map((gpu) => gpu.trim()));

let vramChecker: ChildProcessWithoutNullStreams | undefined;
let lastVRam: number | null = null;
ipcMain.handle('get-vram-usage', () => {
if (!vramChecker) {
vramChecker = createNvidiaSmiVRamChecker(nvidiaSmi, 1000, (usage) => {
lastVRam = usage;
});
}

return lastVRam;
});
};

const nvidiaSmi = await getNvidiaSmi();

if (nvidiaSmi) {
try {
await registerNvidiaSmiEvents(nvidiaSmi);
return true;
} catch (error) {
log.error(error);
}
}
registerEmptyGpuEvents();
return false;
};

const createBackend = async (token: ProgressToken, args: OpenArguments) => {
const useSystemPython = settingStorage.getItem('use-system-python') === 'true';
const systemPythonLocation = settingStorage.getItem('system-python-location');

const nvidiaSmiPromise = checkNvidiaSmi();

return setupBackend(
token,
useSystemPython,
systemPythonLocation,
() => nvidiaSmiPromise,
getRootDirSync(),
args.noBackend
);
Expand Down
125 changes: 0 additions & 125 deletions src/main/nvidiaSmi.ts

This file was deleted.

Loading

0 comments on commit 75d433c

Please sign in to comment.