Best Python code snippet using lisa_python
gpu.py
Source:gpu.py  
...74        # default to install by platform, unless it's specified to skip.75        if (settings.is_enabled) and settings.install_by_platform:76            cls._install_by_platform(*args, **kwargs)77    @classmethod78    def remove_virtual_gpus(cls, devices: List[PciDevice]) -> List[PciDevice]:79        return [x for x in devices if x.vendor != "Microsoft Corporation"]80    def enabled(self) -> bool:81        return True82    def is_supported(self) -> bool:83        raise NotImplementedError84    def is_module_loaded(self) -> bool:85        lsmod_tool = self._node.tools[Lsmod]86        if (len(self.gpu_vendor) > 0) and all(87            lsmod_tool.module_exists(vendor) for vendor in self.gpu_vendor88        ):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")...gpusuite.py
Source:gpusuite.py  
...109        lspci = node.tools[Lspci]110        gpu = node.features[Gpu]111        # 1. Disable GPU devices.112        gpu_devices = lspci.get_devices_by_type(device_type=constants.DEVICE_TYPE_GPU)113        gpu_devices = gpu.remove_virtual_gpus(gpu_devices)114        # stop the service which uses nvidia module115        service = node.tools[Service]116        service.stop_service("nvidia-persistenced")117        for device in gpu_devices:118            lspci.disable_device(device)119        # 2. Enable GPU devices.120        lspci.enable_devices()121    @TestCaseMetadata(122        description="""123        This test case will run PyTorch to check CUDA driver installed correctly.124        1. Install PyTorch.125        2. Check GPU count by torch.cuda.device_count()126        3. Compare with PCI result127        """,...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!!
