TIL

Today I Learned by OOZOU

Run a command on your GNU/Linux dedicated Nvidia GPU (e.g. ollama)

To make sure a command (CLI) is using your NVIDIA GPU in GNU/Linux, you can use the environment variables __NV_PRIME_RENDER_OFFLOAD=1 and __GLX_VENDOR_LIBRARY_NAME=nvidia before your command.

Example:

__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia ollama run orca-mini

You can also verify that it works by running:

__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia glxinfo | grep vendor

Which should output something like:

server glx vendor string: NVIDIA Corporation
client glx vendor string: NVIDIA Corporation
OpenGL vendor string: NVIDIA Corporation

Getting Started


To get started with Ollama, you’ll need to ensure that your system meets the necessary requirements. This includes having a compatible operating system, such as Linux, Windows, or macOS, and a compatible graphics card, such as an NVIDIA GPU. You can check the system requirements on the Ollama website.

Once you’ve confirmed that your system meets the requirements, you can proceed to install Ollama. You can use the official installation script, which can be run in a terminal using the following command: curl -s https://raw.githubusercontent.com/ollama/ollama/master/install.sh | bash

Running Ollama on Nvidia GPU


To run Ollama on an NVIDIA GPU, you’ll need to ensure that you have the necessary drivers installed. You can check for NVIDIA drivers using the command: nvidia-smi or ubuntu-drivers list --gpgpu If you receive an error, you can install the required drivers using the command: sudo ubuntu-drivers install Once you’ve installed the drivers, you can run Ollama on your NVIDIA GPU using the following command: docker run -d --name ollama -p 11434:11434 -v /home/ollama:/home/ollama:z --gpus all ollama/ollama:gpu

Deploying a Web UI (Optional)


If you want to interact with Ollama using a web interface, you can deploy a web UI using Open WebUI. You can install Open WebUI using Docker by running the following command: docker run -d -p 8080:8080 open-webui/open-webui Once Open WebUI is installed, you can access it by opening a web browser and entering http://localhost:8080. You can create an account and select a model to interact with the LLM.

Optimizing Performance


To optimize the performance of Ollama, you can use several techniques. One way is to use a GPU-accelerated version of Ollama, which can significantly improve performance. You can also optimize the performance of Ollama by adjusting the batch size and sequence length.

Additionally, you can use tools like nvidia-smi to monitor the performance of your GPU and adjust the settings accordingly. You can also use docker stats to monitor the performance of your Docker containers and adjust the settings accordingly.

By following these tips, you can optimize the performance of Ollama and get the most out of your system.
6
Hicham

Use Pundit Policies to manage permitted parameters, defaults and per action

To manage permitted params you can add permitted_attributes and permitted_attributes_for_#{action} in your policies e.g.

class CommentPolicy < ApplicationPolicy
  def permitted_attributes
    %i[body]
  end

  def permitted_attributes_for_create
    %i[body application_id]
  end
end

Then in your controller

class CommentController
  def create
    @comment = Comment.create(comment_attributes) # { body: 'body', application_id: 1 }
  end
 
  def update
    @comment = Comment.find(params[:id])
    @comment.update(comment_attributes) # { body: 'new body' }
  end
 
  private def comment_attributes
    permitted_attributes(Comment)
  end
end
11
Joe

Use `requestSubmit` for form submits with Turbo

- `HTMLFormElement.submit()`: Does NOT trigger `submit` event
- `HTMLFormElement.requestSubmit()`: Triggers `submit` event

`Turbo` listens to submit events on form. 
Hence, always use `requestSubmit` to submit  a form using JS.
10
Abhi

Ignore bundled gems for Rubocop and ESLint in GitHub Actions

Rails projects will often install gems into the vendor directory in GitHub actions so we can cache them for the consecutive runs.

If you use code linters in the action you need to exclude the `vendor/bundle/*` to prevent false positives

For ESLint this can be configured through ignorePatterns
// ./.eslintrc.json
{
  "ignorePatterns": [
    "vendor/bundle/*"
  ]
}

For Rubocop you can exclude through the AllCops config
# ./.rubocop.yml
AllCops:
  Exclude:
    - 'vendor/**/*'

Importance of Ignoring Bundled Gems in Code Linters


In software development, maintaining high code quality is essential. Code linters are a tool to help achieve this by performing static code analysis, which can detect potential errors and enforce a consistent coding style. However, when using linters in a continuous integration environment, such as GitHub Actions, it's crucial to configure them correctly to avoid false positives.

Bundled gems, often stored in the vendor directory, are external code dependencies that can be flagged by code linters if not properly excluded. By ignoring these directories, you can ensure that the linter focuses on your source code rather than external libraries, thus maintaining the integrity of your code analysis.

Configuring Linters to Exclude Bundled Gems


For JavaScript projects using ESLint, you can configure the linter to ignore the vendor/bundle/* directory through the ignorePatterns setting in your .eslintrc.json file. This ensures that only your code is checked, improving code quality without unnecessary distractions.

// ./.eslintrc.json
{
  "ignorePatterns": [
    "vendor/bundle/*"
  ]
}

Similarly, for Ruby projects using Rubocop, you can exclude the vendor directory by adding an Exclude rule under AllCops in your .rubocop.yml configuration file. This helps focus the static code analysis on your actual source code.

# ./.rubocop.yml
AllCops:
  Exclude:
    - 'vendor/**/*'

By configuring your code linters to exclude bundled gems, you can maintain a high standard of code quality while avoiding false positives in your static code analysis.

#RUBY-ON-RAILS
15
Joe

Add a macOS-style drop shadow to your "partial" screen captures or plain-bordered images


By default, and like it or hate it, the macOS keystrokes to capture full app screen (⇧- ⌘- 5) will add a subtle drop shadow around your saved image.  This can make it more visually appealing when adding a screen shot or image to a document.  The convert app, part of the ImageMagick distribution, has a nice command line function to add this same drop shadow effect to any of your images (e.g., a partial screen capture using .⇧- ⌘- 4)

Once installed, you can use the following command line to update your image with a drop-shadow:
convert "Screen Shot 2021-07-07 at 09.47.13.png" \( +clone -background black -shadow 80x20+0+15 \) +swap -background transparent -layers merge +repage "Screen Shot 2021-07-07 at 09.47.13.png"


which will turn this:
sample_clean.png 143 KB



into this:
sample_shadow.png 132.01 KB


Pro-tip: You can create a pseudo-function in your BASH or ZSH environment - e.g., in your ~/.bashrc file, add this line:
shadow () { convert "$@" \( +clone -background black -shadow 80x20+0+15 \) +swap -background transparent -layers merge +repage "$@"; }
and then source your ~/.bashrc file (first time) to activate it:
source ~/.bashrc

Now, from your command line, you can add a drop shadow to any image file by simply typing:
shadow "Screen Shot 2021-07-07 at 09.47.13.png"
(remember to add double-quotes around your file if it contains spaces)

Enhancing PNG Images with Drop Shadows


Adding a drop shadow to a PNG image can significantly enhance its visual appeal, making it stand out against any background. This shadow effect creates a sense of depth, giving the image a more three-dimensional look. For designers and content creators, incorporating shadow png images into their projects can elevate the overall aesthetic, making designs more engaging and professional.

How to Add a Drop Shadow Using CSS


For web developers looking to add a drop shadow to PNG images via CSS, the box-shadow property is a versatile tool. Here's a quick example:

css img { box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.5); }

This code snippet adds a subtle shadow effect to images, using a black shadow with 50% opacity. Adjust the values to achieve the desired shadow projection and intensity.

Creative Uses of Shadow PNG Images


Shadow png images are not just for aesthetic purposes; they can also serve functional roles in design. For instance, using shadows can help guide the viewer's attention to specific elements, create a sense of hierarchy, or add texture to flat designs. Whether you're designing a website, creating digital art, or preparing a presentation, shadows can be a powerful tool in your creative arsenal.

By leveraging tools like ImageMagick or CSS, you can easily apply a drop shadow effect to your PNG images, enhancing their impact and ensuring they capture the viewer's attention.

Done.
1.09 Thousand
Jeff

git check-ignore

This command lets you debug if a certain file/path is ignored and, with the -v flag, it also shows you the matching exclude pattern (Docs).
16
Federico

Allow pasting multiline ruby blocks in Pry when using leading dot notation

We use leading dot notation a lot now e.g.

Foo
  .bar
  .baz

If you copy and paste this into a Pry REPL it throws an error because it doesn't know how to parse it. I thought this was because Pry was parsing differently to IRB but it's actually because Pry has a feature to call system methods by prefixing them with a dot. To be able to copy paste code like this you can simply remove the Pry commands 

Pry.commands.delete /\.(.*)/
6
Joe

Rails 6, Ruby 3, Bootstrap 5 and Hotwire

Extending the blogpost from Bootrails with Hotwire:
All the fancy new things (as of when I'm writing this TIL):

$ ruby --version
3.0.1
$ rails --version
Rails 6.1.3.1
$ rails new MyApp --skip-bundle --skip-coffee --database=postgresql
$ cd MyApp
# Add Hotwire-rails to Gemfile
$ echo "\ngem 'hotwire-rails'" >> Gemfile
$ bundle
$ ./bin/rails webpacker:install
# lets keep javascript and stylesheets in a app/frontend folder
$ mv app/javascript app/frontend
# add bootstrap dependencies
$ yarn add bootstrap@next
# when prompted, choose the latest version here:
$ yarn add @popperjs/core@next

Edit `./webpacker.yml` and modify source_path:
# Inside webpacker.yml
default: &default
  source_path: app/frontend # Change here

Add files to load bootstrap:
$ mkdir app/frontend/js && touch app/frontend/js/bootstrap_js_files.js
$ touch app/frontend/packs/application.scss
# Warning, this removes application.css (which we replace with application.scss)
$ touch app/assets/stylesheets/.keep && rm app/assets/stylesheets/application.css

// inside app/frontend/js/bootstrap_js_files.js
// enable plugins as you require them  

// import 'bootstrap/js/src/alert'  
// import 'bootstrap/js/src/button'  
// import 'bootstrap/js/src/carousel'  
import 'bootstrap/js/src/collapse'  
import 'bootstrap/js/src/dropdown'  
// import 'bootstrap/js/src/modal'  
// import 'bootstrap/js/src/popover'  
import 'bootstrap/js/src/scrollspy'  
// import 'bootstrap/js/src/tab'  
// import 'bootstrap/js/src/toast'  
// import 'bootstrap/js/src/tooltip'

// inside app/frontend/packs/application.js  

// Add this line before import Rails …
import '../js/bootstrap_js_files.js'  

// inside app/frontend/packs/application.scss  

// Import Bootstrap v5  
@import "~bootstrap/scss/bootstrap";

Setup Hotwire
# create ./bin/bundle; currently required for the Remove Turbolinks task by Stimulus
# see: https://github.com/hotwired/stimulus-rails/issues/55
$ bundle binstubs bundler
$ ./bin/rails hotwire:install

Modify `app/views/layouts/application.html.erb`:
<!DOCTYPE html>
<html>
  <head>
    <title>MyApp</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <!-- Warning !! ensure that "stylesheet_pack_tag" is used, line below; also change turbolinks to turbo for hotwire -->
    <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbo-track': 'reload' %>
    <%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

Check if everything compiles:
$ ./bin/rails assets:clobber
# You can probably ignore the warnings :)
$ ./bin/rails webpacker:compile
13
Const