pytorch / cpuinfo

CPU INFOrmation library (x86/x86-64/ARM/ARM64, Linux/Windows/Android/macOS/iOS)
BSD 2-Clause "Simplified" License
996 stars 314 forks source link

Crash Seen with unspecified micro arch for Windows On Arm devices when getting the cpu micro arch info to check the core type #235

Open yganup opened 6 months ago

yganup commented 6 months ago

For WOA(windows on ARM) devices where the CPU string does not match any string in woa_chips, cpuinfo_get_uarch returns NULL (index = 0 and cpuinfo_uarchs_count = 0 for unspecified CPU).

const struct cpuinfo_uarch_info* cpuinfo_get_uarch(uint32_t index) {
    if (!cpuinfo_is_initialized) {
        cpuinfo_log_fatal("cpuinfo_get_%s called before cpuinfo is initialized", "uarch");
    }
#if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64 || CPUINFO_ARCH_RISCV32 || CPUINFO_ARCH_RISCV64
    if CPUINFO_UNLIKELY (index >= cpuinfo_uarchs_count) {
         return NULL; 
    }
    return &cpuinfo_uarchs[index];
#else
    if CPUINFO_UNLIKELY (index != 0) {
        return NULL;
    }
    return &cpuinfo_global_uarch;
#endif

To gracefully handle return of cpuinfo_uarch_info below are the 2 methods can be used

  1. Return a dummy cpuinfo_uarch_info struct with uarch = cpuinfo_uarch_unknown (0) instead of NULL.
  2. As part of usage guidelines for _cpuinfo_getuarch, Include NULL checking for return value
// check for ARM cortex a-53 CPU 
cpuinfo_uarch_info *cpu_data = cpuinfo_get_uarch(cpuinfo_get_current_uarch_index())

if( cpu_data!= NULL)
{
 switch (cpu_data->uarch){
   case cpuinfo_uarch_cortex_a53:
   case cpuinfo_uarch_cortex_a55r0:
   case cpuinfo_uarch_cortex_a55:
     return true;
   default:
     return false;
 }
return false

This would help stability of libraries/binaries using cpuinfo on devices with unknown micro arch for Windows on ARM devices . Also I would assume the same issue would be seen for other platforms as well.