Best Python code snippet using lisa_python
gpu.py
Source:gpu.py  
...127                    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()...gpusuite.py
Source:gpusuite.py  
...80        assert_that(81            lsvmbus_device_count,82            "Expected device count didn't match Actual device count from lsvmbus",83        ).is_equal_to(expected_count)84        lspci_device_count = gpu_feature.get_gpu_count_with_lspci()85        assert_that(86            lspci_device_count,87            "Expected device count didn't match Actual device count from lspci",88        ).is_equal_to(expected_count)89        _check_driver_installed(node)90        vendor_cmd_device_count = gpu_feature.get_gpu_count_with_vendor_cmd()91        assert_that(92            vendor_cmd_device_count,93            "Expected device count didn't match Actual device count"94            " from vendor command",95        ).is_equal_to(expected_count)96    @TestCaseMetadata(97        description="""98        This test case will99        1. Validate disabling GPU devices.100        2. Validate enable back the disabled GPU devices.101        """,102        priority=2,103        requirement=simple_requirement(104            supported_features=[GpuEnabled()],105        ),106    )107    def verify_gpu_rescind_validation(self, node: Node) -> None:108        _check_driver_installed(node)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        """,128        priority=3,129        requirement=simple_requirement(130            supported_features=[GpuEnabled()],131        ),132    )133    def verify_gpu_cuda_with_pytorch(self, node: Node) -> None:134        _check_driver_installed(node)135        _install_cudnn(node)136        gpu = node.features[Gpu]137        pip = node.tools[Pip]138        if not pip.exists_package("torch"):139            pip.install_packages("torch")140        gpu_script = "import torch;print(f'gpu count: {torch.cuda.device_count()}')"141        python = node.tools[Python]142        expected_count = gpu.get_gpu_count_with_lspci()143        script_result = python.run(144            f'-c "{gpu_script}"',145            force_run=True,146        )147        gpu_count_str = get_matched_str(script_result.stdout, self._pytorch_pattern)148        script_result.assert_exit_code(149            message=f"failed on run gpu script: {gpu_script}, "150            f"output: {script_result.stdout}"151        )152        assert_that(gpu_count_str).described_as(153            f"gpu count is not in result: {script_result.stdout}"154        ).is_not_empty()155        gpu_count = int(gpu_count_str)156        assert_that(gpu_count).described_as(...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!!
