Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Ashland, Oregon
  • 23:46 (UTC -08:00)
View GitHub Profile
@havenwood
havenwood / map.rb
Created February 16, 2025 00:31
A Ractor-backed `Map` riffing on https://bugs.ruby-lang.org/issues/21121#note-1
class Ractor
module Channel
def self.new(&block)
channel = Ractor.new do
loop do
Ractor.yield Ractor.receive
end
end
return block.call(channel) if block_given?
@havenwood
havenwood / wired_limits.zsh
Created January 26, 2025 22:52
Adjust wired limits to allocate more memory to the GPU with Apple Silicon
#!/usr/bin/env zsh -e
memory_minus_gb() {
local -i gb=${1:?Specify GB to subtract}
local -i total_mb=$(( $(sysctl -n hw.memsize) / 1024 / 1024 ))
print -- $(( total_mb - gb * 1024 ))
}
memory_minus_percent() {
@havenwood
havenwood / models.md
Last active January 24, 2025 10:10
LLM models high level overview

Models

In order to offload to GPU a model must fit in VRAM.

The following table lists models assuming Q4_K_M quantization. You can always use a model that fits in a smaller VRAM size.

VRAM Models
384GB - DeepSeek V3 671
128GB - Mistral Large 2411 123b
64GB - Qwen2.5 72b
@havenwood
havenwood / data_literal.rb
Created January 9, 2025 16:41
A `~{meaning: 42}`-style Data literal implemented in Ruby
module DataLiteral
refine Hash do
def to_data(name = nil)
if name
unless Object.const_defined?(name)
Object.const_set(name, Data.define(*keys))
end
Object.const_get(name).new(*values)
else
@havenwood
havenwood / install-ruby-3.4.1-mmtk.sh
Last active December 25, 2024 09:48
A little script to install Ruby 3.4.0 with the experimental MMTk garbage collector
#!/usr/bin/env zsh -e
version=3.4.1
brew install gmp jemalloc libffi libyaml openssl@3 readline zlib
command -v rustc >/dev/null 2>&1 || brew install rust
mkdir -p "$HOME/src"
cd "$HOME/src"
curl "https://cache.ruby-lang.org/pub/ruby/3.4/ruby-$version.tar.xz" | tar -x
cd "ruby-$version"
@havenwood
havenwood / integer.rb
Created December 2, 2024 16:52
A Ruby implementation of Integer#each_digit
class Integer
def each_digit(base = 10, &block)
raise Math::DomainError, 'out of domain' if negative?
raise TypeError, "wrong argument type #{base.class} (expected Integer)" unless base.respond_to?(:to_int)
radix = base.to_int
raise ArgumentError, 'negative radix' if radix.negative?
raise ArgumentError, "invalid radix #{radix}" if radix < 2
return enum_for(__method__, base) unless block_given?
@havenwood
havenwood / str.rb
Created November 13, 2024 21:55
Example for @isene on #ruby IRC
without = %w[b c]
without_regexp = Regexp.union(without)
split_index = 7
dropped = split_index.pred
str = 'abcdaba123abcd'
pos = str
.each_grapheme_cluster
.with_index
.lazy
@havenwood
havenwood / waffle.rb
Created November 8, 2024 16:01
An example of implementing #to_json for Ruby IRC
require 'json'
class Waffle
def initialize(toppings:, syrup:)
@toppings = toppings
@syrup = syrup
end
class << self
def json_create(object)
@havenwood
havenwood / enumerator.rb
Created September 28, 2024 09:54
Enumerator#feed examples
class Integer
def stream(by: 1)
Enumerator.new Float::INFINITY do |yielder|
int = self
loop do
fed = yielder.yield(int)
int += fed if fed
int += by
end
@havenwood
havenwood / immutable.rb
Created July 1, 2024 18:46
An example immutable (Dry) and mutable (Active Model) Args interface
require 'dry-struct'
require 'dry-types'
require 'dry-validation'
class WordTally
class Args < Dry::Struct
module Types
include Dry.Types()
end