5

I want to add swap space on my machine running Fedora. Searching on this raised questions:

  1. I found this 2016 post showing a method to do it, but a comment says changing the swap could result in boot issues. Is it safe to follow that method ?

  2. Fedora's doc says it replaced swap with "an emulated drive that uses RAM for its storage". This got me very confused because I thought the whole point of swap was to use the disk memory when the RAM is full.

Anyway, here is my use case: I have a fairly recent laptop with 24GB RAM, and a 1TB SSD which is 5% full. I want to try running some of the new LLaMA-based LLMs, but they need easily tens of GBs of RAM to load the models. Since I have plenty of free space in my SSD I want to turn ~200GB of it into swap space and be able to load the models.

Here is the result of running lsblk (I removed 30 or so lines of snaps):

loop0    7:0    0   104M  1 loop /var/lib/snapd/snap/authy/18
loop1    7:1    0     4K  1 loop /var/lib/snapd/snap/bare/5
...
loop34   7:34   0 320.4M  1 loop /var/lib/snapd/snap/vlc/3078
zram0  252:0    0     8G  0 disk [SWAP]
nvme0n1
       259:0    0 953.9G  0 disk 
├─nvme0n1p1
│      259:1    0   260M  0 part /boot/efi
├─nvme0n1p2
│      259:2    0    16M  0 part 
├─nvme0n1p3
│      259:3    0 269.1G  0 part 
├─nvme0n1p4
│      259:4    0   959M  0 part 
├─nvme0n1p5
│      259:5    0     1G  0 part /boot
└─nvme0n1p6
       259:6    0 634.8G  0 part /home
                                 /
Seldi
  • 109

4 Answers4

5

What worked

Following Kamil's comment I followed the instructions here.

First try

I partially solved my problem. The method in (1) didn't work, seemingly because btrfs does not allow swap files. So I followed what I read here, and created a file /etc/systemd/zram-generator.conf with the following content:

[zram0]
zram-size = 100000

After a reboot I now have around 100 G of zram:

$ zramctl
NAME       ALGORITHM DISKSIZE  DATA  COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle      97.7G  3.4G 931.5M  1.2G      12 [SWAP]

However when trying to run the LLMs, the swap memory use reached up to 24GB only and then it plateaued and the program was killed by lack of memory. At this point I'm not sure whether the issues lies with zram itself or the program I try to run...

Seldi
  • 109
4

This is what I did and worked for me. here is the output of lsblk:

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:0    0   7.3T  0 disk 
├─sda1        8:1    0    16M  0 part 
└─sda2        8:2    0   7.3T  0 part /run/media/ef/Storage
zram0       252:0    0     8G  0 disk [SWAP]
nvme0n1     259:0    0   1.8T  0 disk 
├─nvme0n1p1 259:1    0    16M  0 part 
├─nvme0n1p2 259:2    0 834.7G  0 part 
├─nvme0n1p3 259:3    0   614M  0 part 
├─nvme0n1p4 259:4    0   100M  0 part /boot/efi
├─nvme0n1p5 259:5    0     1G  0 part /boot
└─nvme0n1p6 259:6    0     1T  0 part /home

and here is output of $ swapon -s

Filename                Type        Size        Used        Priority
/dev/zram0              partition   8388604     1935420     100

Here are the steps I followed:

$ sudo touch /swapfile
$ sudo chattr +C /swapfile 
$ sudo lsattr /swapfile
$ sudo fallocate -l 200G /swapfile
$ sudo lsattr /swapfile
$ sudo chmod 600 /swapfile
$ sudo lsattr /swapfile  # make sure you see the letter C in the results: ---------------C------ /swapfile
$ sudo mkswap /swapfile # output example: Setting up swapspace version 1, size = 200 GiB (214748360704 bytes)
$ sudo mkswap /swapfile # output example: Setting up swapspace version 1, size = 200 GiB (214748360704 bytes)
                                          no label, UUID=98a6c4a0-ec8d-4afb-90af-6162e83d30b3
$ sudo swapon /swapfile
$ vim /etc/fstab 

here I added one line at the end of /etc/fstab:

/swapfile swap swap defaults 0 0

That's it. Now you can see it shows up:

$ sudo swapon -s
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   8G 1.8G  100
/swapfile  file      200G   0B   -2

I tested it and it works for reading huge files. resources that helped:

0

An 8gb Video RAM graphics card is what you are looking for. The ram they are referencing is video ram. I'm sure regular RAM helps, but when you don't have an 8gb vram graphics card the LLM defaults to CPU usage.

You didn't mention how much VRAM you have which is the main issue with running LLM's on a laptop. I use the 4gb size 7b models on my laptop, such mistral or codellama, and it uses CPU exclusively as it's an old laptop with little to no VRAM. It does the job, but it's so much slower than my gaming desktop with an 8GB video card. The 16GB videos cards are what you would need to run any models larger than 7b.

As for editing zram, this is one reference I've seen that seems to have worked for people. Though I've yet to try it.

What you want to do is edit the file /etc/systemd/zram-generator.conf and right under [zram0] you'll want to add zram-size=XXXX and replace it with a multiple of 1024 (1GB) for how much you want. In my case, the archinstall script gave me 4GB so I put 8192 to increase it to 8GB.

Afterwards, I did a restart and saw my swap increased to 8GB.

increasing zram answer on reddit - reference

128
  • 101
0

This answer here solves the problem, but breaks the creation of new snapshots. I started getting this error at dmesg:

BTRFS warning (device dm-0): cannot snapshot subvolume with active swapfile

I would suggest creating another subvolume or replacing /swapfile with /home/swapfile, this worked for me, but I am not sure if it should be done in another way.