Skip to content

Instantly share code, notes, and snippets.

View N8python's full-sized avatar

n8programs N8python

View GitHub Profile
def random_so(batch, n, *, generator, device):
matrix = torch.randn((batch, n, n), generator=generator, device=device)
q, r = torch.linalg.qr(matrix)
# Sign-correct QR to obtain Haar-random orthogonal matrices.
diagonal = torch.diagonal(r, dim1=-2, dim2=-1)
signs = torch.where(diagonal >= 0, 1.0, -1.0)
q = q * signs.unsqueeze(-2)
# Force determinant +1, giving SO(N) rather than O(N).
@N8python
N8python / full-neural-poission.glsl
Created July 12, 2026 00:38
Full model used by N8AO's new neural denoiser.
// N8AO 2.0.0 generated neural Poisson-blur fragment shader
// Variant: Neural-High, 16 denoise taps, perspective camera, standard depth
// Network: 552 inline int8 coefficients, 517 nonzero terms after zero pruning
// This is the exact ShaderMaterial fragment source; Three.js prepends its GLSL3 program prefix.
#define NEURAL_DENOISE
layout(location = 0) out highp vec4 neuralFragColor;
#define gl_FragColor neuralFragColor
uniform sampler2D sceneDiffuse;
import argparse
import copy
import glob
import shutil
import time
import types
from pathlib import Path
import mlx.core as mx
import mlx.nn as nn
from mlx.utils import tree_flatten, tree_map
from mlx_lm import load, generate
import mlx.core as mx
from mlx_lm.utils import (
dequantize_model,
fetch_from_hub,
get_model_path,
quantize_model,
save_config,
save_weights,
@N8python
N8python / matrix_exp.py
Created January 19, 2025 22:18
MLX matrix_exp.
import mlx.core as mx
@mx.compile
def _compute_T1(A):
"""I + A"""
return mx.eye(A.shape[-1]) + A
@mx.compile
def _compute_T2(A):
"""I + A + A^2/2"""
A2 = A @ A
return mx.eye(A.shape[-1]) + A + A2/2
@N8python
N8python / orthomatrix.py
Created January 19, 2025 05:32
OPTIMIZE with me
import torch
import torch.nn as nn
import torch.optim as optim
# Define the differentiable orthonormal linear layer
class OrthonormalLayer(nn.Module):
def __init__(self, n):
"""
Initializes a learnable layer with an orthonormal weight matrix.
:param n: Dimension of the square weight matrix.
@N8python
N8python / pretrain.py
Created January 15, 2025 23:41
Simple character-level pretraining in MLX. Gets a roughly billion tokens/day for an 18M parameter model on one M3 Max.
import json
import random
import mlx.optimizers as optim
import mlx.core as mx
import mlx.nn as nn
import numpy as np
from tqdm import tqdm
import time
from datetime import datetime
@N8python
N8python / batch.py
Created December 31, 2024 00:39
Prototypes.
def generate_batched(
model: nn.Module,
tokenizer: Union[PreTrainedTokenizer, TokenizerWrapper],
prompt: str,
batch_size: int,
*,
verbose: bool = False,
formatter: Optional[Callable] = None,
max_tokens: int = 256,
@N8python
N8python / MORE.py
Last active December 7, 2024 22:01
faster every day
from mlx_lm import load
import mlx.core as mx
from mlx.utils import tree_flatten, tree_map, tree_unflatten
import numpy as np
# Copyright © 2023-2024 Apple Inc.
import contextlib
import copy
import glob
import importlib
from mlx_lm import load
import mlx.core as mx
from mlx.utils import tree_flatten, tree_map, tree_unflatten
import numpy as np
# Copyright © 2023-2024 Apple Inc.
import contextlib
import copy
import glob
import importlib