@@ -9,11 +9,13 @@ import { Injectable } from '@nestjs/common';
99import { firstValueFrom } from 'rxjs' ;
1010import { FileManagerService } from '@/file-manager/file-manager.service' ;
1111import { rm } from 'fs/promises' ;
12+ import { exec } from 'child_process' ;
1213
1314@Injectable ( )
1415export class InitCliUsecases {
15- CORTEX_RELEASES_URL = 'https://api.github.com/repos/janhq/cortex/releases' ;
16- CUDA_DOWNLOAD_URL =
16+ private readonly CORTEX_RELEASES_URL =
17+ 'https://api.github.com/repos/janhq/cortex/releases' ;
18+ private readonly CUDA_DOWNLOAD_URL =
1719 'https://catalog.jan.ai/dist/cuda-dependencies/<version>/<platform>/cuda.tar.gz' ;
1820
1921 constructor (
@@ -109,7 +111,7 @@ export class InitCliUsecases {
109111 await rm ( destination , { force : true } ) ;
110112 } ;
111113
112- parseEngineFileName = ( options : InitOptions ) => {
114+ parseEngineFileName = ( options ? : InitOptions ) => {
113115 const platform =
114116 process . platform === 'win32'
115117 ? 'windows'
@@ -118,12 +120,14 @@ export class InitCliUsecases {
118120 : process . platform ;
119121 const arch = process . arch === 'arm64' ? process . arch : 'amd64' ;
120122 const cudaVersion =
121- options . runMode === 'GPU'
123+ options ? .runMode === 'GPU'
122124 ? options . gpuType === 'Nvidia'
123125 ? '-cuda-' + ( options . cudaVersion === '11' ? '11-7' : '12-0' )
124126 : '-vulkan'
125127 : '' ;
126- const instructions = options . instructions ? `-${ options . instructions } ` : '' ;
128+ const instructions = options ?. instructions
129+ ? `-${ options . instructions } `
130+ : '' ;
127131 const engineName = `${ platform } -${ arch } ${ instructions . toLowerCase ( ) } ${ cudaVersion } ` ;
128132 return `${ engineName } .tar.gz` ;
129133 } ;
@@ -173,10 +177,6 @@ export class InitCliUsecases {
173177 return undefined ; // No CUDA Toolkit found
174178 } ;
175179
176- checkFileExistenceInPaths = ( file : string , paths : string [ ] ) : boolean => {
177- return paths . some ( ( p ) => existsSync ( join ( p , file ) ) ) ;
178- } ;
179-
180180 installCudaToolkitDependency = async ( options : InitOptions ) => {
181181 const platform = process . platform === 'win32' ? 'windows' : 'linux' ;
182182
@@ -232,4 +232,61 @@ export class InitCliUsecases {
232232 }
233233 await rm ( destination , { force : true } ) ;
234234 } ;
235+
236+ // Function to check for NVIDIA GPU
237+ checkNvidiaGPUExist = ( ) : Promise < boolean > => {
238+ return new Promise < boolean > ( ( resolve ) => {
239+ // Execute the nvidia-smi command
240+ exec ( 'nvidia-smi' , ( error ) => {
241+ if ( error ) {
242+ // If there's an error, it means nvidia-smi is not installed or there's no NVIDIA GPU
243+ console . log ( 'NVIDIA GPU not detected or nvidia-smi not installed.' ) ;
244+ resolve ( false ) ;
245+ } else {
246+ // If the command executes successfully, NVIDIA GPU is present
247+ console . log ( 'NVIDIA GPU detected.' ) ;
248+ resolve ( true ) ;
249+ }
250+ } ) ;
251+ } ) ;
252+ } ;
253+
254+ detectInstructions = ( ) : Promise < 'AVX' | 'AVX2' | 'AVX512' | undefined > => {
255+ return new Promise < 'AVX' | 'AVX2' | 'AVX512' | undefined > ( ( res ) => {
256+ // Execute the cpuinfo command
257+
258+ exec (
259+ join (
260+ __dirname ,
261+ `../../../../bin/cpuinfo${ process . platform !== 'linux' ? '.exe' : '' } ` ,
262+ ) ,
263+ ( error , stdout ) => {
264+ if ( error ) {
265+ // If there's an error, it means lscpu is not installed
266+ console . log ( 'CPUInfo is not installed.' ) ;
267+ res ( 'AVX' ) ;
268+ } else {
269+ // If the command executes successfully, parse the output to detect CPU instructions
270+ if ( stdout . includes ( '\"AVX512\": \"true\"' ) ) {
271+ console . log ( 'AVX-512 instructions detected.' ) ;
272+ res ( 'AVX512' ) ;
273+ } else if ( '\"AVX2\": \"true\"' ) {
274+ console . log ( 'AVX2 instructions detected.' ) ;
275+ res ( 'AVX2' ) ;
276+ } else {
277+ console . log ( 'AVXs instructions detected.' ) ;
278+ res ( 'AVX' ) ;
279+ }
280+ }
281+ } ,
282+ ) ;
283+ } ) ;
284+ } ;
285+
286+ private checkFileExistenceInPaths = (
287+ file : string ,
288+ paths : string [ ] ,
289+ ) : boolean => {
290+ return paths . some ( ( p ) => existsSync ( join ( p , file ) ) ) ;
291+ } ;
235292}
0 commit comments