{"id":15049544,"url":"https://github.com/narasimha1997/py_cpu","last_synced_at":"2025-06-16T12:33:58.028Z","repository":{"id":56053149,"uuid":"315524575","full_name":"Narasimha1997/py_cpu","owner":"Narasimha1997","description":"Python bindings for Google's cpu_features library. Allows python developers to enable hardware specific optimizations at runtime.","archived":false,"fork":false,"pushed_at":"2020-11-28T13:20:36.000Z","size":66,"stargazers_count":20,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-13T18:17:02.327Z","etag":null,"topics":["c99","cplusplus","cpu","cpython","optimization","pip","pybind11","python3"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Narasimha1997.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-24T05:07:53.000Z","updated_at":"2025-02-27T10:16:34.000Z","dependencies_parsed_at":"2022-08-15T12:20:10.551Z","dependency_job_id":null,"html_url":"https://github.com/Narasimha1997/py_cpu","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/Narasimha1997/py_cpu","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Narasimha1997%2Fpy_cpu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Narasimha1997%2Fpy_cpu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Narasimha1997%2Fpy_cpu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Narasimha1997%2Fpy_cpu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Narasimha1997","download_url":"https://codeload.github.com/Narasimha1997/py_cpu/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Narasimha1997%2Fpy_cpu/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260162792,"owners_count":22968126,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c99","cplusplus","cpu","cpython","optimization","pip","pybind11","python3"],"created_at":"2024-09-24T21:21:13.473Z","updated_at":"2025-06-16T12:33:57.975Z","avatar_url":"https://github.com/Narasimha1997.png","language":"C++","readme":"# py_cpu\nPython bindings for Google's [cpu_features](https://github.com/google/cpu_features) library. Using this library, Python developers can check for hardware specific features and enable the respective optimizations in their software at runtime. `py_cpu` provides bindings for multiple hardware architectures like `x86`, `ARM`, `AARCH64`, `MIPS` and `PPC`. \n\n### Quick start\nTo use py_cpu, You can directly download and install the pre-built wheel file, using the command below:\n```\npip install py-cpu\n```\n\n(For x86, the pre-built wheel will be installed automatically, on other platforms, the source distribution will be downloaded and the package will be built on the platform natively)\n\n### Building from source:\n\nRequirements:\n    1. Python3\n    2. CMake\n    3. setuptools\n    4. wheel\n    5. scikit-build\n\nTO build from source, you can just clone this repository, you need not have to clone the submodules, as they will be downloaded by the cmake build system automatically.\n```\ngit clone https://github.com/Narasimha1997/py_cpu.git\n```\n\nTo directly install the package from the source, use the command below:\n```\npip install py_cpu/   #-- the repo root directory\n```\n\nIf you are using an older version of pip, the build-system packages will not be automatically installed, in that case,\n```\npip install -r py_cpu/requirements.txt   #manually install the build requirements\npip install py_cpu/                      #then install the package\n```\n\nTo build `sdist` and `bdist_wheel` you can just run `setup.py` as follows:\n```\ncd py_cpu/\npython3 install sdist bdist_wheel\n```\n\n### Usage guide:\nTo use the package in your codebase, just import `py_cpu`.\n\n```\nimport py_cpu\n```\n\n#### 1. Get the CPU info\n```python3\nimport py_cpu\n\n#get cpu info\ncpu_info = py_cpu.CPUInfo()\n```\n\n#### 2. Check for features:\n```python3\nimport py_cpu\n\n\n#call this once during the program init, to avoid unnecessary compute unless required.\ncpu_info = py_cpu.CPUInfo()\n\n#check if the CPU supports AES instructions\nif cpu_info.features.aes :\n    print('Yes, it supports, Run the optimized code')\nelse :\n    print('No, run normal code')\n\n```\n\n#### 3. Get the list of supported features :\n```python3\nimport py_cpu\ncpu_info = py_cpu.CPUInfo()\n\n#returns a python dictionary, you can check the feature by\n# subscripting, example : features_dict['aes'] -\u003e either True or False\nfeatures_dict = cpu_info.get_features_as_dict()\nprint(features_dict['avx'])\n\n# returns a FeatureFlags object, this is simple to use because you can use . operator instead of subscripting.\n\nfeatures = cpu_info.get_features()\nprint(features.avx)\n```\n\n#### Get the general info about the hardware\nApart from features and SOCs, you can also query the general info - about architecture type, vendor etc.\nThese fields are different for different hardware. \n\n```python3\nimport py_cpu\ncpu_info = py_cpu.CPUInfo()\n\n#get list of field names\nsupported_fields = cpu_info.get_info_fields()\n\n#example, on x86\n# ['arch', 'brand', 'family', 'features', 'model', 'stepping', 'uarch', 'vendor']\n\n#query the fields: Because the cpu_info object supports subscripting\n\nbrand_name = cup_info['brand']\n\n#if you want the entire object as a dict\ninfo_dict = cpu_info.as_dict()\n\n#if you want the entire object but exclude features\ninfo_dict_without_features = cpu_info.as_dict(include_features = False)\n\n```\n\n#### 5. Print functions\nIf you just want to print the output, you can use any of these two methods.\nThese methods will be just for a fancy fun use.\n\nPretty-Print Dict - This function uses pprint internally.\n```python3\nimport py_cpu\n#obtain CPU info\ncpu_info = py_cpu.CPUInfo()\n\n#call pprint method\ncpu_info.pprint()\n```\n\nPrint as table - This function uses python formatting/spacings to display the list as a table.\n```python3\nimport py_cpu\n#obtain CPU info\ncpu_info = py_cpu.CPUInfo()\n\n#call print_as_table method\ncpu_info.print_as_table()\n```\n\n### Guide for developers:\nIf you want to add new features, this section is for you.\nThe repository depends on `pybind11` and the original `cpu_features` repository by Google. Both of these are includes as submodules under `src/`.  To get the complete codebase, you have to clone the submodules as well. Just run these commands from the project directory :\n\n```\ngit submodule init \ngit submodule update\n```\nOr you can clone the repo recursively.\n\n#### Under the hood details:\nThe binding code is written in C++ and uses `pybind11` to build a `Cpython` extension. `binding.cc` implements the binding code for all the five platform which are supported in the original repo. Since most of the C/C++ compiler implementations on any operating system expose the architecture flags as preprocessor definitions, only the target hardware binding code gets retained for compilation. The binding code also declares structures that are python friendly - Like STL maps etc to store the data. \nThe entire structure is a read-only object for python and cannot be modified, this makes the implementation much more easy and faster. During the build-phase, `CMake` first compiles Google's cpu_features library as a submodule and builds a Position independent object code (PIC), since the Cpython extension is a shared dynamic library. Then the target is compiled with Pybind11 to create the final cpython extension. `__init__.py` is just like a glue which provides caching functionality by storing it inside an object, so you can only init once and use it throughout the lifecycle of the application.\n\n**Note** : This python binding is not an offical Google release. The project respects the license and distribution terms of both `cpu_features` and `pybind11` by adding them as sub-modules - this helps us to keep the original implementation as it is.\n\n### Contributing\nIf you like to contribute code to this repo, you are always welcome. \nI would encourage newbies to take up the tasks, as it would allow them to get into the open source world.\nAlso, please do test it on variety of platforms. Please do raise issues if you have any problems.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnarasimha1997%2Fpy_cpu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnarasimha1997%2Fpy_cpu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnarasimha1997%2Fpy_cpu/lists"}