diff --git a/cortex-js/cpuinfo/README.md b/cortex-js/cpuinfo/README.md deleted file mode 100644 index b25fc22c6..000000000 --- a/cortex-js/cpuinfo/README.md +++ /dev/null @@ -1,100 +0,0 @@ -# CPU Info - -This source code provides a simple C++ program to retrieve and display information about the CPU, including the vendor, brand, number of cores, number of logical processors (threads), and supported instruction sets (e.g., SSE, AVX, AVX512). - -## Files - -### CPUID.h - -This header file contains the CPUID class, which abstracts the CPUID instruction. The class provides methods to retrieve the values of the EAX, EBX, ECX, and EDX registers after calling the CPUID instruction with a given function ID. - -### cpuinfo.cpp - -This source file implements the CPUInfo class, which uses the CPUID class to gather various CPU-related information. The main function prints this information in JSON format. - -## Building - -### Windows - -To build the project on Windows, you can use Microsoft Visual Studio or the Visual Studio Developer Command Prompt. - -**Using Visual Studio Developer Command Prompt** - -1. Open the Developer Command Prompt for Visual Studio. - -2. Navigate to the project directory: - ```cmd - cd path\to\your\project\directory - ``` - -3. Compile the source code using the following command: - ```cmd - cl cpuinfo.cpp /EHsc - ``` - - This will create the executable cpuinfo.exe in the current directory. - -### Linux - -To build the project on Linux, you need g++ (the GNU C++ compiler). - -1. Open a terminal. - -2. Navigate to the project directory: - ```bash - cd path/to/your/project/directory - ``` - -3. Compile the code: - ```bash - g++ cpuinfo.cpp -o cpuinfo - ``` - - This will create the executable cpuinfo in the current directory. - -## Running the Program - -### Windows - -After building the project, you can run the executable from the command prompt: - -```cmd - cpuinfo.exe -``` - -### Linux - -After building the project, you can run the executable from the terminal: - -```bash - ./cpuinfo -``` - -## Example Output - -The program prints the CPU information in JSON format. Example output: - -```json -{ - "vendor": "GenuineIntel", - "brand": "12th Gen Intel(R) Core(TM) i5-12400F", - "cores": 6, - "threads": 12, - "is_hyperthreading": true, - "instructions": { - "SSE": true, - "SSE2": true, - "SSE3": true, - "SSE41": true, - "SSE42": true, - "AVX": true, - "AVX2": true, - "AVX512": true - } -} -``` - -## Notes -- Ensure that your environment is properly set up for compiling C++ code. On Windows, this typically involves installing Visual Studio with the C++ build tools. On Linux, you need to have g++ installed. -- The JSON output format is designed to be easy to parse and read. -- In the `bin` directory, there are pre-built outputs. For Windows, the code is signed with our certificate. You can download and use it directly or build from source. \ No newline at end of file diff --git a/cortex-js/cpuinfo/binding.gyp b/cortex-js/cpuinfo/binding.gyp deleted file mode 100644 index 652246b5e..000000000 --- a/cortex-js/cpuinfo/binding.gyp +++ /dev/null @@ -1,10 +0,0 @@ -{ - "targets": [ - { - "target_name": "cpuinfo", - "sources": ["src/cpuinfo.cpp"], - "include_dirs": [ " -export type InstructionSet = 'AVX' | 'AVX2' | 'AVX512'; // Add more as needed later -declare module "cpuinfo" { - declare const cpuInfo: { - cpuInfo: () => InstructionSet[]; - } -} \ No newline at end of file diff --git a/cortex-js/cpuinfo/index.js b/cortex-js/cpuinfo/index.js deleted file mode 100644 index 2018ed99d..000000000 --- a/cortex-js/cpuinfo/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = exports = { cpuInfo: require('./build/Release/cpuinfo.node') } diff --git a/cortex-js/cpuinfo/package.json b/cortex-js/cpuinfo/package.json deleted file mode 100644 index fc37fc89d..000000000 --- a/cortex-js/cpuinfo/package.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "name": "cpuinfo", - "version": "1.0.0", - "description": "Get CPU instructions", - "main": "index.js", - "types": "./index.d.ts", - "scripts": { - "install": "prebuild-install -r napi || node-gyp rebuild" - }, - "author": "", - "license": "ISC", - "gypfile": true, - "binary": { - "napi_versions": [ - 3, - 6 - ] - }, - "dependencies": { - "bindings": "^1.5.0", - "node-addon-api": "^7.0.0", - "prebuild-install": "^7.1.1" - }, - "devDependencies": { - "@types/node": "^20.14.9", - "typescript": "^5.5.3" - }, - "peerDependencies": { - "node-gyp": "8.x" - }, - "files": [ - "binding.gyp", - "deps/", - "*.js", - "*.d.ts", - "src/" - ] -} diff --git a/cortex-js/cpuinfo/src/CPUID.h b/cortex-js/cpuinfo/src/CPUID.h deleted file mode 100644 index e762e04da..000000000 --- a/cortex-js/cpuinfo/src/CPUID.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef CPUID_H -#define CPUID_H - -#ifdef _WIN32 -#include -#include -typedef unsigned __int32 uint32_t; -#else -#include -#endif - -class CPUID { - uint32_t regs[4]; - -public: - explicit CPUID(unsigned i) { -#ifdef _WIN32 - __cpuid((int *)regs, (int)i); -#else - asm volatile - ("cpuid" : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3]) - : "a" (i), "c" (0)); - // ECX is set to zero for CPUID function 4 -#endif - } - - const uint32_t &EAX() const {return regs[0];} - const uint32_t &EBX() const {return regs[1];} - const uint32_t &ECX() const {return regs[2];} - const uint32_t &EDX() const {return regs[3];} -}; - -#endif // CPUID_H diff --git a/cortex-js/cpuinfo/src/cpuinfo.cpp b/cortex-js/cpuinfo/src/cpuinfo.cpp deleted file mode 100644 index b0b947c13..000000000 --- a/cortex-js/cpuinfo/src/cpuinfo.cpp +++ /dev/null @@ -1,238 +0,0 @@ -#include -#include -#include -#include - -#ifdef _WIN32 -#include -#include -typedef unsigned __int32 uint32_t; -#else -#include -#endif - -using namespace std; - -#if defined(_WIN32) || defined(LINUX) -#define MAX_INTEL_TOP_LVL 4 - -class CPUID -{ - uint32_t regs[4]; - -public: - explicit CPUID(unsigned funcId, unsigned subFuncId) - { -#ifdef _WIN32 - __cpuidex((int *)regs, (int)funcId, (int)subFuncId); - -#else - asm volatile("cpuid" : "=a"(regs[0]), "=b"(regs[1]), "=c"(regs[2]), "=d"(regs[3]) - : "a"(funcId), "c"(subFuncId)); - // ECX is set to zero for CPUID function 4 -#endif - } - - const uint32_t &EAX() const { return regs[0]; } - const uint32_t &EBX() const { return regs[1]; } - const uint32_t &ECX() const { return regs[2]; } - const uint32_t &EDX() const { return regs[3]; } -}; - -class CPUInfo -{ -public: - CPUInfo(); - string vendor() const { return mVendorId; } - string model() const { return mModelName; } - int cores() const { return mNumCores; } - float cpuSpeedInMHz() const { return mCPUMHz; } - bool isSSE() const { return mIsSSE; } - bool isSSE2() const { return mIsSSE2; } - bool isSSE3() const { return mIsSSE3; } - bool isSSE41() const { return mIsSSE41; } - bool isSSE42() const { return mIsSSE42; } - bool isAVX() const { return mIsAVX; } - bool isAVX2() const { return mIsAVX2; } - bool isAVX512() const { return mIsAVX512; } - bool isHyperThreaded() const { return mIsHTT; } - int logicalCpus() const { return mNumLogCpus; } - -private: - // Bit positions for data extractions - static const uint32_t SSE_POS = 0x02000000; - static const uint32_t SSE2_POS = 0x04000000; - static const uint32_t SSE3_POS = 0x00000001; - static const uint32_t SSE41_POS = 0x00080000; - static const uint32_t SSE42_POS = 0x00100000; - static const uint32_t AVX_POS = 0x10000000; - static const uint32_t AVX2_POS = 0x00000020; - static const uint32_t AVX512_POS = 0x00010000; // AVX-512F bit in EBX from CPUID function 7 - static const uint32_t LVL_NUM = 0x000000FF; - static const uint32_t LVL_TYPE = 0x0000FF00; - static const uint32_t LVL_CORES = 0x0000FFFF; - - // Attributes - string mVendorId; - string mModelName; - int mNumSMT; - int mNumCores; - int mNumLogCpus; - float mCPUMHz; - bool mIsHTT; - bool mIsSSE; - bool mIsSSE2; - bool mIsSSE3; - bool mIsSSE41; - bool mIsSSE42; - bool mIsAVX; - bool mIsAVX2; - bool mIsAVX512; -}; - -CPUInfo::CPUInfo() -{ - // Get vendor name EAX=0 - CPUID cpuID0(0, 0); - uint32_t HFS = cpuID0.EAX(); - mVendorId += string((const char *)&cpuID0.EBX(), 4); - mVendorId += string((const char *)&cpuID0.EDX(), 4); - mVendorId += string((const char *)&cpuID0.ECX(), 4); - // Get SSE instructions availability - CPUID cpuID1(1, 0); - mIsHTT = cpuID1.EDX() & AVX_POS; - mIsSSE = cpuID1.EDX() & SSE_POS; - mIsSSE2 = cpuID1.EDX() & SSE2_POS; - mIsSSE3 = cpuID1.ECX() & SSE3_POS; - mIsSSE41 = cpuID1.ECX() & SSE41_POS; - mIsSSE42 = cpuID1.ECX() & SSE41_POS; - mIsAVX = cpuID1.ECX() & AVX_POS; - // Get AVX2 and AVX512 instructions availability - CPUID cpuID7(7, 0); - mIsAVX2 = cpuID7.EBX() & AVX2_POS; - mIsAVX512 = cpuID7.EBX() & AVX512_POS; - - string upVId = mVendorId; - for_each(upVId.begin(), upVId.end(), [](char &in) - { in = ::toupper(in); }); - // Get num of cores - if (upVId.find("INTEL") != std::string::npos) - { - if (HFS >= 11) - { - for (int lvl = 0; lvl < MAX_INTEL_TOP_LVL; ++lvl) - { - CPUID cpuID4(0x0B, lvl); - uint32_t currLevel = (LVL_TYPE & cpuID4.ECX()) >> 8; - switch (currLevel) - { - case 0x01: - mNumSMT = LVL_CORES & cpuID4.EBX(); - break; - case 0x02: - mNumLogCpus = LVL_CORES & cpuID4.EBX(); - break; - default: - break; - } - } - mNumCores = mNumLogCpus / mNumSMT; - } - else - { - if (HFS >= 1) - { - mNumLogCpus = (cpuID1.EBX() >> 16) & 0xFF; - if (HFS >= 4) - { - mNumCores = 1 + (CPUID(4, 0).EAX() >> 26) & 0x3F; - } - } - if (mIsHTT) - { - if (!(mNumCores > 1)) - { - mNumCores = 1; - mNumLogCpus = (mNumLogCpus >= 2 ? mNumLogCpus : 2); - } - } - else - { - mNumCores = mNumLogCpus = 1; - } - } - } - else if (upVId.find("AMD") != std::string::npos) - { - if (HFS >= 1) - { - mNumLogCpus = (cpuID1.EBX() >> 16) & 0xFF; - if (CPUID(0x80000000, 0).EAX() >= 8) - { - mNumCores = 1 + (CPUID(0x80000008, 0).ECX() & 0xFF); - } - } - if (mIsHTT) - { - if (!(mNumCores > 1)) - { - mNumCores = 1; - mNumLogCpus = (mNumLogCpus >= 2 ? mNumLogCpus : 2); - } - } - else - { - mNumCores = mNumLogCpus = 1; - } - } - else - { - cout << "Unexpected vendor id" << endl; - } - // Get processor brand string - // This seems to be working for both Intel & AMD vendors - for (int i = 0x80000002; i < 0x80000005; ++i) - { - CPUID cpuID(i, 0); - mModelName += string((const char *)&cpuID.EAX(), 4); - mModelName += string((const char *)&cpuID.EBX(), 4); - mModelName += string((const char *)&cpuID.ECX(), 4); - mModelName += string((const char *)&cpuID.EDX(), 4); - } -} -#endif - -Napi::Value Method(const Napi::CallbackInfo &info) -{ - Napi::Env env = info.Env(); - Napi::Array array = Napi::Array::New(env); - - uint32_t index = 0; - -#if defined(_WIN32) || defined(LINUX) - CPUInfo cinfo; - if (cinfo.isAVX512()) - { - array.Set(index++, Napi::String::New(env, "AVX512")); - } - if (cinfo.isAVX2()) - { - array.Set(index++, Napi::String::New(env, "AVX2")); - } - if (cinfo.isAVX()) - { - array.Set(index++, Napi::String::New(env, "AVX")); - } - -#endif - return array; -} - -Napi::Object Init(Napi::Env env, Napi::Object exports) -{ - exports.Set(Napi::String::New(env, "cpuInfo"), - Napi::Function::New(env, Method)); - return exports; -} - -NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) \ No newline at end of file diff --git a/cortex-js/cpuinfo/src/example.json b/cortex-js/cpuinfo/src/example.json deleted file mode 100644 index 556df78d5..000000000 --- a/cortex-js/cpuinfo/src/example.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "vendor": "GenuineIntel", - "brand": "12th Gen Intel(R) Core(TM) i5-12400F", - "cores": 6, - "threads": 12, - "is_hyperthreading": true, - "instructions": - { - "SSE": true, - "SSE2": true, - "SSE3": true, - "SSE41": true, - "SSE42": true, - "AVX": true, - "AVX2": true, - "AVX512": true - } -} \ No newline at end of file diff --git a/cortex-js/cpuinfo/tsconfig.json b/cortex-js/cpuinfo/tsconfig.json deleted file mode 100644 index 5dc97d431..000000000 --- a/cortex-js/cpuinfo/tsconfig.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "compilerOptions": { - /* Visit https://aka.ms/tsconfig to read more about this file */ - /* Language and Environment */ - "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ - - /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ - - /* JavaScript Support */ - // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ - // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ - // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ - - "outDir": "./dist", /* Specify an output folder for all emitted files. */ - /* Interop Constraints */ - - "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ - // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ - "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ - - /* Type Checking */ - "strict": false, /* Enable all strict type-checking options. */ - /* Completeness */ - // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ - } -} diff --git a/cortex-js/package.json b/cortex-js/package.json index a9f21c68a..23383a1c6 100644 --- a/cortex-js/package.json +++ b/cortex-js/package.json @@ -10,11 +10,9 @@ }, "scripts": { "dev": "nest dev", - "preinstall": "(cd cpuinfo && npm install) && (npx node-gyp rebuild -C ./cpuinfo)", "compile": "npx ncc build ./dist/src/command.js -o command", "build": "nest build", - "build:cpuInfo": "npx node-gyp rebuild -C ./cpuinfo", - "build:binary": "yarn build:cpuInfo && yarn build && yarn compile && npx -q patch-package && npx @yao-pkg/pkg .", + "build:binary": "yarn build && yarn compile && npx -q patch-package && npx @yao-pkg/pkg .", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "build:extensions": "run-script-os", "build:extensions:windows": "powershell -command \"$jobs = Get-ChildItem -Path './src/extensions' -Directory | ForEach-Object { Start-Job -Name ($_.Name) -ScriptBlock { param($_dir); try { Set-Location $_dir; yarn; yarn build; Write-Output 'Build successful in ' + $_dir } catch { Write-Error 'Error in ' + $_dir; throw } } -ArgumentList $_.FullName }; $jobs | Wait-Job; $jobs | ForEach-Object { Receive-Job -Job $_ -Keep } | ForEach-Object { Write-Host $_ }; $failed = $jobs | Where-Object { $_.State -ne 'Completed' -or $_.ChildJobs[0].JobStateInfo.State -ne 'Completed' }; if ($failed) { Exit 1 }\"", @@ -54,6 +52,7 @@ "class-validator": "^0.14.1", "cli-progress": "^3.12.0", "cortexso-node": "^0.0.4", + "cpu-instructions": "^0.0.10", "decompress": "^4.2.1", "js-yaml": "^4.1.0", "nest-commander": "^3.13.0", @@ -65,8 +64,7 @@ "typeorm": "^0.3.20", "ulid": "^2.3.0", "uuid": "^9.0.1", - "yaml": "^2.4.2", - "cpuinfo": "file:./cpuinfo" + "yaml": "^2.4.2" }, "devDependencies": { "@nestjs/cli": "^10.0.0", @@ -84,6 +82,8 @@ "@types/uuid": "^9.0.8", "@typescript-eslint/eslint-plugin": "^6.0.0", "@typescript-eslint/parser": "^6.0.0", + "@vercel/ncc": "^0.38.0", + "@yao-pkg/pkg": "^5.12.0", "bun": "^1.1.15", "cpx": "^1.5.0", "eslint": "^8.42.0", @@ -94,18 +94,16 @@ "jest": "^29.5.0", "nest-commander-testing": "^3.3.0", "node-gyp": "^10.1.0", + "patch-package": "^8.0.0", "prettier": "^3.0.0", + "resedit-cli": "^2.0.0", "run-script-os": "^1.1.6", "source-map-support": "^0.5.21", "supertest": "^6.3.3", "ts-jest": "^29.1.0", "ts-loader": "^9.4.3", "tsconfig-paths": "^4.2.0", - "typescript": "^5.1.3", - "patch-package": "^8.0.0", - "@yao-pkg/pkg": "^5.12.0", - "resedit-cli": "^2.0.0", - "@vercel/ncc": "^0.38.0" + "typescript": "^5.1.3" }, "resolutions": { "ajv": "8.15.0", @@ -137,7 +135,11 @@ }, "pkg": { "scripts": "command/**/*.js", - "assets": ["command/*.node", "**/package.json", "node_modules/axios/**/*", "cpuinfo/**/*"], + "assets": [ + "command/*.node", + "**/package.json", + "node_modules/axios/**/*" + ], "outputPath": "dist" } } diff --git a/cortex-js/src/infrastructure/commanders/usecases/init.cli.usecases.ts b/cortex-js/src/infrastructure/commanders/usecases/init.cli.usecases.ts index 0297fa0bf..819aaa566 100644 --- a/cortex-js/src/infrastructure/commanders/usecases/init.cli.usecases.ts +++ b/cortex-js/src/infrastructure/commanders/usecases/init.cli.usecases.ts @@ -17,7 +17,7 @@ import { import { checkNvidiaGPUExist, cudaVersion } from '@/utils/cuda'; import { Engines } from '../types/engine.interface'; -import { cpuInfo } from 'cpuinfo'; +import { cpuInfo } from 'cpu-instructions'; @Injectable() export class InitCliUsecases {