bayasdev / envycontrol

Easy GPU switching for Nvidia Optimus laptops under Linux
https://bayas.dev/envycontrol
MIT License
1.14k stars 55 forks source link

[BUG] Can't use NVENC in hybrid mode, can use just in resetted mode #147

Open sam-cavalheiro opened 7 months ago

sam-cavalheiro commented 7 months ago

Describe the bug I just can use NVENC (I think it's Nvidia codec) only with resetted envycontrol mode, not hybrid (I usually use that mode).

To Reproduce Steps to reproduce the behavior:

  1. 'sudo envycontrol -s hybrid' then reboot
  2. 'ffmpeg -i input.mp4 -vcodec h264_nvenc output.mp4' to test if NVENC works
  3. See error: 'dl_fn->cuda_dl->cuInit(0) failed -> CUDA_ERROR_UNKNOWN: unknown error'
  4. 'sudo envycontrol --reset' then reboot
  5. Test if NVENC works (step 2 again)
  6. Will convert the video as expected

Expected behavior Test NVENC (with 'ffmpeg -i input.mp4 -vcodec h264_nvenc output.mp4') should just don't return error and convert the example file -- in hybrid mode, not necessarily resetted mode.

Screenshots Hybrid mode: image

Resetted mode: image

System Information:

klmcwhirter commented 1 month ago

@sam-cavalheiro , I personally do not see this as a bug. Let me explain.

The Optimus hybrid mode has a number of features- one of which is the support of the HDMI port (and audio through it).

This carries with it the effect of greatly reducing battery time.

So, envycontrol has the --rtd3 additional option for hybrid mode that helps with that. But it comes with the tradeoff of reduced dGPU functionality. Please see the link to the official documentation in the Hybrid section of the README.

If you need the full-blown Optimus hybrid mode feature set, please do use the --reset option to restore it.

sam-cavalheiro commented 1 month ago

@sam-cavalheiro , I personally do not see this as a bug. Let me explain.

The Optimus hybrid mode has a number of features- one of which is the support of the HDMI port (and audio through it).

This carries with it the effect of greatly reducing battery time.

So, envycontrol has the --rtd3 additional option for hybrid mode that helps with that. But it comes with the tradeoff of reduced dGPU functionality. Please see the link to the official documentation in the Hybrid section of the README.

If you need the full-blown Optimus hybrid mode feature set, please do use the --reset option to restore it.

I use a custom-nvidia.conf in /etc/modprobe.d/ with options nvidia "NVreg_DynamicPowerManagement=0x02" in resseted mode. This means that RTD3 is not working here? (I don't know a good way to test this).

klmcwhirter commented 1 month ago

@sam-cavalheiro ,

I use a custom-nvidia.conf in /etc/modprobe.d/ with options nvidia "NVreg_DynamicPowerManagement=0x02" in resseted mode. This means that RTD3 is not working here? (I don't know a good way to test this).

Without digging back into the details of RTD3 to refresh my memory I cannot quickly tell if that is all that is needed.

I do know that @bayasdev has spent quite a bit of time working with several people over the years making sure his code works with different hardware (Intel and AMD iGPUs), distros, WM's, etc.

I just know there are some tradeoffs in there. Hence, the cli options to switch to nvidia mode or simply --reset are also available. I, too, found it necessary to --reset when working with pytorch with the nvidia cuda drivers, for example.

When I am done with such a project I use envycontrol again to put my laptop in the mode I need.

My usage of envycontrol is not one and done - but rather situational.

Make sense?

To gain a better understanding of what happens upon switch to hybrid mode, please take a look at the few lines of code used to switch into hybrid mode to see exactly which files it is creating with what contents. The CONSTANTS used are defined near the top of the file.

BTW, nvidia-smi should show both GPU utilization and power consumption metrics. That is how I monitor GPU overhead / usage. nvidia-smi-sample

klmcwhirter commented 1 month ago

For future readers ...

I followed these steps to install the nvidia cuda drivers and the nvidia-smi utility.

Note: these instructions are for Fedora 39. As I write this drivers are not available for Fedora 40 yet.

Installing NVIDIA CUDA Drivers on Fedora 39

IMPORTANT!!! DO NOT install the proprietary NVIDIA drivers from the NVIDIA website using the run script method. This will surely break your system when kernel updates appear.

See this video segment ... Proprietary NVIDIA Drivers

Before installing, ensure your system is updated to prevent potential conflicts between graphic card drivers and kernels. To update your Fedora system, use the following command:
sudo dnf upgrade --refresh
Import Nvidia CUDA Repository for Fedora 39:
sudo dnf config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/fedora39/x86_64/cuda-fedora39.repo
Proceed to install the necessary dependencies for NVIDIA Drivers:
sudo dnf install kernel-headers kernel-devel tar bzip2 make automake gcc gcc-c++ pciutils elfutils-libelf-devel libglvnd-opengl libglvnd-glx libglvnd-devel acpid pkgconfig dkms
To view the NVIDIA RPM modules, execute:
sudo dnf module list nvidia-driver
$ sudo dnf module list nvidia-driver
Last metadata expiration check: 2:42:46 ago on Wed 17 Apr 2024 07:11:24 AM PDT.
cuda-fedora39-x86_64
Name                   Stream                      Profiles                           Summary                                       
nvidia-driver          latest                      default [d], fm, ks, src           Nvidia driver for latest branch               
nvidia-driver          latest-dkms [d][e]          default [d] [i], fm, ks            Nvidia driver for latest-dkms branch          
nvidia-driver          open-dkms                   default [d], fm, ks, src           Nvidia driver for open-dkms branch            
nvidia-driver          550                         default [d], fm, ks, src           Nvidia driver for 550 branch                  
nvidia-driver          550-dkms                    default [d], fm, ks                Nvidia driver for 550-dkms branch             
nvidia-driver          550-open                    default [d], fm, ks, src           Nvidia driver for 550-open branch             

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

Select the appropriate one corresponding to your Fedora version to integrate the CUDA repository into your Fedora system.

To install the latest NVIDIA drivers using the DKMS method, execute:
sudo dnf module install nvidia-driver:latest-dkms
sam-cavalheiro commented 1 month ago

@klmcwhirter Sorry, but now I don't have time to test :( I'd not read the Nvidia documentation about RTD3 because english is not my primary language, so it's hard to read long text. I need to dedicate a bit of time to do that. So, for now, the only thing I can do is trust you.