Changed update_render_threads to use SetAppConfigRequest to set which devices are active.

Keep ImageRequest.render_device for affinity only. (Send a task to an already active device.)
This commit is contained in:
Marc-Andre Ferland 2022-11-14 21:47:45 -05:00
parent 0ebfbca93e
commit ffe0eb1544
3 changed files with 36 additions and 31 deletions

View File

@ -803,7 +803,6 @@ function getCurrentUserRequest() {
height: heightField.value,
// allow_nsfw: allowNSFWField.checked,
turbo: turboField.checked,
render_device: getCurrentRenderDeviceSelection(),
use_full_precision: useFullPrecisionField.checked,
use_stable_diffusion_model: stableDiffusionModelField.value,
use_vae_model: vaeModelField.value,
@ -839,19 +838,6 @@ function getCurrentUserRequest() {
return newTask
}
function getCurrentRenderDeviceSelection() {
let selectedGPUs = $('#use_gpus').val()
if (useCPUField.checked && !autoPickGPUsField.checked) {
return 'cpu'
}
if (autoPickGPUsField.checked || selectedGPUs.length == 0) {
return 'auto'
}
return selectedGPUs.join(',')
}
function makeImage() {
if (!isServerAvailable()) {
alert('The server is not available.')
@ -1165,6 +1151,19 @@ promptStrengthSlider.addEventListener('input', updatePromptStrength)
promptStrengthField.addEventListener('input', updatePromptStrengthSlider)
updatePromptStrength()
function getCurrentRenderDeviceSelection() {
let selectedGPUs = $('#use_gpus').val()
if (useCPUField.checked && !autoPickGPUsField.checked) {
return 'cpu'
}
if (autoPickGPUsField.checked || selectedGPUs.length == 0) {
return 'auto'
}
return selectedGPUs.join(',')
}
useCPUField.addEventListener('click', function() {
let gpuSettingEntry = getParameterSettingsEntry('use_gpus')
let autoPickGPUSettingEntry = getParameterSettingsEntry('auto_pick_gpus')
@ -1184,11 +1183,19 @@ useCPUField.addEventListener('click', function() {
}
gpuSettingEntry.style.display = (autoPickGPUsField.checked ? 'none' : '')
}
changeAppConfig({
'render_devices': getCurrentRenderDeviceSelection()
})
})
useGPUsField.addEventListener('click', function() {
let selectedGPUs = $('#use_gpus').val()
autoPickGPUsField.checked = (selectedGPUs.length === 0)
changeAppConfig({
'render_devices': getCurrentRenderDeviceSelection()
})
})
autoPickGPUsField.addEventListener('click', function() {
@ -1198,6 +1205,10 @@ autoPickGPUsField.addEventListener('click', function() {
let gpuSettingEntry = getParameterSettingsEntry('use_gpus')
gpuSettingEntry.style.display = (this.checked ? 'none' : '')
changeAppConfig({
'render_devices': getCurrentRenderDeviceSelection()
})
})
async function changeAppConfig(configDelta) {

View File

@ -40,7 +40,7 @@ class RenderTask(): # Task with output queue and completion lock.
def __init__(self, req: Request):
self.request: Request = req # Initial Request
self.response: Any = None # Copy of the last reponse
self.render_device = None
self.render_device = None # Select the task afinity. (Not used to change active devices).
self.temp_images:list = [None] * req.num_outputs * (1 if req.show_only_filtered_image else 2)
self.error: Exception = None
self.lock: threading.Lock = threading.Lock() # Locks at task start and unlocks when task is completed
@ -72,7 +72,7 @@ class ImageRequest(BaseModel):
save_to_disk_path: str = None
turbo: bool = True
use_cpu: bool = False ##TODO Remove after UI and plugins transition.
render_device: str = 'auto'
render_device: str = None
use_full_precision: bool = False
use_face_correction: str = None # or "GFPGANv1.3"
use_upscale: str = None # or "RealESRGAN_x4plus" or "RealESRGAN_x4plus_anime_6B"

View File

@ -161,6 +161,9 @@ async def setAppConfig(req : SetAppConfigRequest):
config = getConfig()
if req.update_branch:
config['update_branch'] = req.update_branch
if req.render_devices:
config['render_devices'] = req.render_devices
update_render_threads_from_request(req.render_devices)
try:
setConfig(config)
return JSONResponse({'status': 'OK'}, headers=NOCACHE_HEADERS)
@ -287,27 +290,18 @@ def save_render_devices_to_config(render_devices):
setConfig(config)
def update_render_threads_on_request(req : task_manager.ImageRequest):
if req.use_cpu: # TODO Remove after transition.
print('WARNING Replace {use_cpu: true} by {render_device: "cpu"}')
req.render_device = 'cpu'
del req.use_cpu
def update_render_threads_from_request(render_device):
if render_device not in ('cpu', 'auto') and not render_device.startswith('cuda:'):
raise HTTPException(status_code=400, detail=f'Invalid render device requested: {render_device}')
if req.render_device not in ('cpu', 'auto') and not req.render_device.startswith('cuda:'):
raise HTTPException(status_code=400, detail=f'Invalid render device requested: {req.render_device}')
if req.render_device.startswith('cuda:'):
req.render_device = req.render_device.split(',')
save_render_devices_to_config(req.render_device)
del req.render_device
if render_device.startswith('cuda:'):
render_device = render_device.split(',')
save_render_devices_to_config(render_device)
update_render_threads()
@app.post('/render')
def render(req : task_manager.ImageRequest):
update_render_threads_on_request(req)
try:
save_model_to_config(req.use_stable_diffusion_model, req.use_vae_model)
req.use_stable_diffusion_model = resolve_ckpt_to_use(req.use_stable_diffusion_model)