Skip to content

Instantly share code, notes, and snippets.

View wassname's full-sized avatar
🙃

wassname (Michael J Clark) wassname

🙃
View GitHub Profile
@wassname
wassname / peft_adapter_is_active.py
Last active August 28, 2024 01:30
Given a peft model work out is adapters are enabled or disabled"
from peft.peft_model import BaseTunerLayer, PeftModel
from peft.utils.other import ModulesToSaveWrapper
def adapter_is_disabled(model: PeftModel) -> bool:
"""Given a peft model work out is adapters are enabled or disabled"""
for module in model.model.modules():
if isinstance(module, (BaseTunerLayer, ModulesToSaveWrapper)):
# print(help(module.enable_adapters))
return module._disable_adapters
@wassname
wassname / symphypothesis.py
Last active August 2, 2024 02:55
fhypothesis: a easy way to display hypothesis in python, kind of like assert
import sympy as sp
from typing import Dict, Any
from IPython.display import display
from sympy import init_printing
init_printing()
def shypothesis(hypothesis: str, variables: Dict[str, Any] = None, round=3, verbose=False):
"""
Evaluate a hypothesis using SymPy, showing simplified equation and result.
@wassname
wassname / craftax_render_symbolic.py
Last active May 24, 2024 23:44
Craftax symbolic to env.state to pixel
"""
craftax = "1.4.1"
jax = "^0.4.28"
jax =
https://gist.github.com/wassname/9f410d11f33cec75393b64d62286dd41
"""
import numpy as np
import numpy as np
from craftax.craftax.craftax_state import EnvState, Inventory, Mobs
from craftax.craftax.constants import MAX_OBS_DIM, OBS_DIM, BlockType, ItemType, MONSTERS_KILLED_TO_CLEAR_LEVEL
@wassname
wassname / launch.json
Created May 19, 2024 02:19
my typical vscode debugger options
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "test",
"type": "debugpy",
"request": "launch",
@wassname
wassname / choice_tree.py
Last active June 3, 2024 13:44
for huggingface transformers sometime you want to constrain output to json schema and record the probabilities on choices/enums. I use it when rating, judging. It's much more efficient than sampling multiple times.
from jaxtyping import Float, Int
import torch
from torch.nn import functional as F
from torch import Tensor
from typing import List, Callable, Tuple, Dict, Optional
import pandas as pd
from transformers import AutoModelForCausalLM, AutoTokenizer
def get_valid_next_choices(choices_tokens, current_tokens):
@wassname
wassname / hf_perplexity.py
Last active March 31, 2025 23:08
simple perplexity for huggingface models similar to llam..cpp
# Directly taken from https://huggingface.co/spaces/evaluate-measurement/perplexity/blob/main/perplexity.py
# TODO replace with a strided version https://github.com/huggingface/transformers/issues/9648#issuecomment-812981524
import numpy as np
import torch
import itertools
from torch.nn import CrossEntropyLoss
from tqdm.auto import tqdm
import torch.nn.functional as F
from datasets import load_dataset, Dataset
from transformers import AutoTokenizer, AutoModelForCausalLM
@wassname
wassname / twohot.md
Last active July 22, 2025 00:38
two-hot encoding notes

What is two-hot encoding?

Description

Two hot encoding was introduced in 2017 in "Marc G Bellemare et all "A distributional perspective on reinforcement learning" but the clearest description is in the 2020 paper "Dreamer-v3" by Danijar Hafner et al.) where it is used for reward and value distributions.

two-hot encoding is a generalization of onehot encoding to continuous values. It produces a vector of length |B| where all elements are 0 except for the two entries closest to the encoded continuous number, at positions k and k + 1. These two entries sum up to 1, with more weight given to the entry that is closer to the encoded number

Code samples

@wassname
wassname / torch_scalar.py
Created December 27, 2023 01:06
wrap sklearn scalars for torch
"""
how to wrap a scikit-learn scalar like RobustScaler for pytorch
"""
import torch
import numpy as np
from einops import rearrange
from sklearn.preprocessing import StandardScaler, RobustScaler
class TorchRobustScaler(RobustScaler):
@wassname
wassname / style_df.py
Created December 23, 2023 22:57
How to style dataframes in vscode
"""
you cannot display, you need to specify html
- see also https://pandas.pydata.org/docs/user_guide/style.html#Builtin-Styles
"""
import pandas as pd
from IPython.display import display, HTML
df = pd.DataFrame({
"strings": ["Adam", "Mike"],
"ints": [1, 3],
@wassname
wassname / justfile
Last active January 3, 2026 11:22
justfile cheatsheet
# see https://cheatography.com/linux-china/cheat-sheets/justfile/
# we can set sehll
set shell := ["zsh", "-cu"]
# settings
set dotenv-load
# Export all just variables as environment variables.
set export