fix: unable to use AiService on arm64 (#15261)

This commit is contained in:
anatawa12
2025-01-14 19:36:35 +09:00
committed by GitHub
parent b161601863
commit 6820878676
2 changed files with 19 additions and 3 deletions

View File

@@ -15,7 +15,7 @@ import { bindThis } from '@/decorators.js';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const REQUIRED_CPU_FLAGS = ['avx2', 'fma'];
const REQUIRED_CPU_FLAGS_X64 = ['avx2', 'fma'];
let isSupportedCpu: undefined | boolean = undefined;
@Injectable()
@@ -31,8 +31,7 @@ export class AiService {
public async detectSensitive(path: string): Promise<nsfw.predictionType[] | null> {
try {
if (isSupportedCpu === undefined) {
const cpuFlags = await this.getCpuFlags();
isSupportedCpu = REQUIRED_CPU_FLAGS.every(required => cpuFlags.includes(required));
isSupportedCpu = await this.computeIsSupportedCpu();
}
if (!isSupportedCpu) {
@@ -64,6 +63,22 @@ export class AiService {
}
}
private async computeIsSupportedCpu(): Promise<boolean> {
switch (process.arch) {
case 'x64': {
const cpuFlags = await this.getCpuFlags();
return REQUIRED_CPU_FLAGS_X64.every(required => cpuFlags.includes(required));
}
case 'arm64': {
// As far as I know, no required CPU flags for ARM64.
return true;
}
default: {
return false;
}
}
}
@bindThis
private async getCpuFlags(): Promise<string[]> {
const str = await si.cpuFlags();