0
0

perf(backend): use mutex for nsfw model loading (#11109)

Co-authored-by: tamaina <tamaina@hotmail.co.jp>
This commit is contained in:
popkirby 2023-07-08 07:27:26 +09:00 committed by GitHub
parent d84796588c
commit 8daca59ca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 1 deletions

View File

@ -33,6 +33,7 @@
### Server ### Server
- JSON.parse の回数を削減することで、ストリーミングのパフォーマンスを向上しました - JSON.parse の回数を削減することで、ストリーミングのパフォーマンスを向上しました
- nsfwjs のモデルロードを排他することで、重複ロードによってメモリ使用量が増加しないように
- 連合の配送ジョブのパフォーマンスを向上ロック機構の見直し、Redisキャッシュの活用 - 連合の配送ジョブのパフォーマンスを向上ロック機構の見直し、Redisキャッシュの活用
## 13.13.2 ## 13.13.2

View File

@ -75,6 +75,7 @@
"accepts": "1.3.8", "accepts": "1.3.8",
"ajv": "8.12.0", "ajv": "8.12.0",
"archiver": "5.3.1", "archiver": "5.3.1",
"async-mutex": "^0.4.0",
"autwh": "0.1.0", "autwh": "0.1.0",
"bcryptjs": "2.4.3", "bcryptjs": "2.4.3",
"blurhash": "2.0.5", "blurhash": "2.0.5",

View File

@ -4,6 +4,7 @@ import { dirname } from 'node:path';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import * as nsfw from 'nsfwjs'; import * as nsfw from 'nsfwjs';
import si from 'systeminformation'; import si from 'systeminformation';
import { Mutex } from 'async-mutex';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
@ -17,6 +18,7 @@ let isSupportedCpu: undefined | boolean = undefined;
@Injectable() @Injectable()
export class AiService { export class AiService {
private model: nsfw.NSFWJS; private model: nsfw.NSFWJS;
private modelLoadMutex: Mutex = new Mutex();
constructor( constructor(
@Inject(DI.config) @Inject(DI.config)
@ -39,7 +41,13 @@ export class AiService {
const tf = await import('@tensorflow/tfjs-node'); const tf = await import('@tensorflow/tfjs-node');
if (this.model == null) this.model = await nsfw.load(`file://${_dirname}/../../nsfw-model/`, { size: 299 }); if (this.model == null) {
await this.modelLoadMutex.runExclusive(async () => {
if (this.model == null) {
this.model = await nsfw.load(`file://${_dirname}/../../nsfw-model/`, { size: 299 });
}
});
}
const buffer = await fs.promises.readFile(path); const buffer = await fs.promises.readFile(path);
const image = await tf.node.decodeImage(buffer, 3) as any; const image = await tf.node.decodeImage(buffer, 3) as any;

View File

@ -140,6 +140,9 @@ importers:
archiver: archiver:
specifier: 5.3.1 specifier: 5.3.1
version: 5.3.1 version: 5.3.1
async-mutex:
specifier: ^0.4.0
version: 0.4.0
autwh: autwh:
specifier: 0.1.0 specifier: 0.1.0
version: 0.1.0 version: 0.1.0
@ -9468,6 +9471,12 @@ packages:
resolution: {integrity: sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==} resolution: {integrity: sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==}
dev: true dev: true
/async-mutex@0.4.0:
resolution: {integrity: sha512-eJFZ1YhRR8UN8eBLoNzcDPcy/jqjsg6I1AP+KvWQX80BqOSW1oJPJXDylPUEeMr2ZQvHgnQ//Lp6f3RQ1zI7HA==}
dependencies:
tslib: 2.6.0
dev: false
/async-settle@1.0.0: /async-settle@1.0.0:
resolution: {integrity: sha512-VPXfB4Vk49z1LHHodrEQ6Xf7W4gg1w0dAPROHngx7qgDjqmIQ+fXmwgGXTW/ITLai0YLSvWepJOP9EVpMnEAcw==} resolution: {integrity: sha512-VPXfB4Vk49z1LHHodrEQ6Xf7W4gg1w0dAPROHngx7qgDjqmIQ+fXmwgGXTW/ITLai0YLSvWepJOP9EVpMnEAcw==}
engines: {node: '>= 0.10'} engines: {node: '>= 0.10'}