Best Python code snippet using lisa_python
gpu.py
Source:gpu.py  
...89            return True90        return False91    def install_compute_sdk(self, version: str = "") -> None:92        # install GPU dependencies before installing driver93        self._install_gpu_dep()94        try:95            # install LIS driver if required and not already installed.96            self._node.tools[LisDriver]97        except Exception as identifier:98            self._log.debug(99                f"LisDriver is not installed. It might not be required. {identifier}"100            )101        # install the driver102        supported_driver = self.get_supported_driver()103        for driver in supported_driver:104            if driver == ComputeSDK.GRID:105                if not version:106                    version = DEFAULT_GRID_DRIVER_URL107                    self._install_grid_driver(version)108                    self.gpu_vendor.add("nvidia")109            elif driver == ComputeSDK.CUDA:110                if not version:111                    version = DEFAULT_CUDA_DRIVER_VERSION112                    self._install_cuda_driver(version)113                    self.gpu_vendor.add("nvidia")114            else:115                raise LisaException(f"{driver} is not a valid value of ComputeSDK")116        if not self.gpu_vendor:117            raise LisaException("No supported gpu driver/vendor found for this node.")118    def get_gpu_count_with_lsvmbus(self) -> int:119        lsvmbus_device_count = 0120        bridge_device_count = 0121        lsvmbus_tool = self._node.tools[Lsvmbus]122        device_list = lsvmbus_tool.get_device_channels()123        for device in device_list:124            for name, id, bridge_count in NvidiaSmi.gpu_devices:125                if id in device.device_id:126                    lsvmbus_device_count += 1127                    bridge_device_count = bridge_count128                    self._log.debug(f"GPU device {name} found!")129                    break130        return lsvmbus_device_count - bridge_device_count131    def get_gpu_count_with_lspci(self) -> int:132        lspci_tool = self._node.tools[Lspci]133        device_list = lspci_tool.get_devices_by_type(134            constants.DEVICE_TYPE_GPU, force_run=True135        )136        # Remove Microsoft Virtual one. It presents with GRID driver.137        device_list = self.remove_virtual_gpus(device_list)138        return len(device_list)139    def get_gpu_count_with_vendor_cmd(self) -> int:140        nvidiasmi = self._node.tools[NvidiaSmi]141        return nvidiasmi.get_gpu_count()142    def get_supported_driver(self) -> List[ComputeSDK]:143        raise NotImplementedError()144    def _initialize(self, *args: Any, **kwargs: Any) -> None:145        self.gpu_vendor: Set[str] = set()146    @classmethod147    def _install_by_platform(cls, *args: Any, **kwargs: Any) -> None:148        raise NotImplementedError()149    # download and install NVIDIA grid driver150    def _install_grid_driver(self, driver_url: str) -> None:151        self._log.debug("Starting GRID driver installation")152        # download and install the NVIDIA GRID driver153        wget_tool = self._node.tools[Wget]154        grid_file_path = wget_tool.get(155            driver_url,156            str(self._node.working_path),157            "NVIDIA-Linux-x86_64-grid.run",158            executable=True,159        )160        result = self._node.execute(161            f"{grid_file_path} --no-nouveau-check --silent --no-cc-version-check"162        )163        result.assert_exit_code(164            0,165            "Failed to install the GRID driver! "166            f"exit-code: {result.exit_code} stderr: {result.stderr}",167        )168        self._log.debug("Successfully installed the GRID driver")169    # download and install CUDA Driver170    def _install_cuda_driver(self, version: str) -> None:171        self._log.debug("Starting CUDA driver installation")172        cuda_repo = ""173        os_information = self._node.os.information174        if isinstance(self._node.os, Redhat):175            release = os_information.release.split(".")[0]176            cuda_repo_pkg = f"cuda-repo-rhel{release}-{version}.x86_64.rpm"177            cuda_repo = (178                "http://developer.download.nvidia.com/"179                f"compute/cuda/repos/rhel{release}/x86_64/{cuda_repo_pkg}"180            )181            # download and install the cuda driver package from the repo182            self._node.os._install_package_from_url(183                f"{cuda_repo}", package_name="cuda-drivers.rpm", signed=False184            )185        elif isinstance(self._node.os, Ubuntu):186            release = re.sub("[^0-9]+", "", os_information.release)187            # there is no ubuntu2110 and ubuntu2104 folder under nvidia site188            if release in ["2110", "2104"]:189                release = "2004"190            # Public CUDA GPG key is needed to be installed for Ubuntu191            self._node.execute(192                "apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/"193                f"cuda/repos/ubuntu{release}/x86_64/7fa2af80.pub",194                sudo=True,195            )196            if "1804" == release:197                cuda_repo_pkg = f"cuda-repo-ubuntu{release}_{version}_amd64.deb"198                cuda_repo = (199                    "http://developer.download.nvidia.com/compute/"200                    f"cuda/repos/ubuntu{release}/x86_64/{cuda_repo_pkg}"201                )202                # download and install the cuda driver package from the repo203                self._node.os._install_package_from_url(204                    f"{cuda_repo}", package_name="cuda-drivers.deb", signed=False205                )206            else:207                self._node.tools[Wget].get(208                    f"https://developer.download.nvidia.com/compute/cuda/repos/"209                    f"ubuntu{release}/x86_64/cuda-ubuntu{release}.pin",210                    "/etc/apt/preferences.d",211                    "cuda-repository-pin-600",212                    sudo=True,213                    overwrite=False,214                )215                repo_entry = (216                    f"deb http://developer.download.nvidia.com/compute/cuda/repos/"217                    f"ubuntu{release}/x86_64/ /"218                )219                self._node.execute(220                    f'add-apt-repository -y "{repo_entry}"',221                    sudo=True,222                    expected_exit_code=0,223                    expected_exit_code_failure_message=(224                        f"failed to add repo {repo_entry}"225                    ),226                )227                # the latest version cuda-drivers-510 has issues228                # nvidia-smi229                # No devices were found230                # dmesg231                # NVRM: GPU 0001:00:00.0: RmInitAdapter failed! (0x63:0x55:2344)232                # NVRM: GPU 0001:00:00.0: rm_init_adapter failed, device minor number 0233                #  switch to use 495234                self._node.os.install_packages("cuda-drivers-495")235        else:236            raise LisaException(237                f"Distro {self._node.os.name}" "not supported to install CUDA driver."238            )239    def _install_gpu_dep(self) -> None:240        # install dependency libraries for distros241        if isinstance(self._node.os, Redhat):242            self._node.os.install_packages(self._redhat_gpu_dependencies, signed=False)243        elif isinstance(self._node.os, Ubuntu):244            self._node.os.install_packages(self._ubuntu_gpu_dependencies, timeout=2000)245        else:246            raise LisaException(247                f"Distro {self._node.os.name} is not supported for GPU."248            )...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
