Skip to content

Instantly share code, notes, and snippets.

View andreaskoepf's full-sized avatar

Andreas Köpf andreaskoepf

View GitHub Profile
local function pca(X)
-- PCA -------------------------------------------------------------------------
-- X is m x n
local mean = torch.mean(X, 1) -- 1 x n
local m = X:size(1)
local Xm = X - torch.ones(m, 1) * mean
Xm:div(math.sqrt(m - 1))
local v,s,_ = torch.svd(Xm:t())
s:cmul(s) -- n
function YUV422toYUV(input, w, h)
local yuv_result = torch.ByteTensor(3, h, w)
local x = input:view(h, w, 2)
yuv_result[1] = x[{{},{},1}] -- copy Y part
local u = yuv_result[2]
local v = yuv_result[3]
local uv = x[{{},{},2}]:reshape(h,w/2,2)
local u_ = uv[{{},{},1}]
local v_ = uv[{{},{},2}]
u:view(h,w/2, 2)[{{},{},1}] = u_
@andreaskoepf
andreaskoepf / torch_chunk_alternative.lua
Last active April 17, 2016 14:33
Alternative implementation of torch.chunk() that always returns exactly nChunks.
--[[
torch.chunk2()
returns a table with nChunk entries, even in the case that the tensor has < nChunk entries in the specified dimension.
Behaviour of the originial torch.chunk() function:
th> torch.rand(11):chunk(5)
{
1 : DoubleTensor - size: 3
2 : DoubleTensor - size: 3
using Microsoft.Extensions.DependencyModel;
using System;
using System.Linq;
using System.Runtime.Loader;
using System.Reflection;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
# adapted from: https://github.com/lucidrains/vit-pytorch/blob/main/vit_pytorch/vit.py
import torch
import torch.nn.functional as F
from torch import nn
from torch.utils import checkpoint
from einops import rearrange, repeat
# adapted from: https://github.com/lucidrains/vit-pytorch/blob/main/vit_pytorch/vit.py
import torch
import torch.nn.functional as F
from torch import nn
from torch.utils import checkpoint
from einops import rearrange, repeat
import triton
@andreaskoepf
andreaskoepf / install_tgi.sh
Last active August 29, 2023 08:51
Install Huggingface TGI v1.0 without docker
# sent to me by tju01, thx
# install base tools
apt update
apt install protobuf-compiler libssl-dev gcc pkg-config g++ make
# install rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
from typing import Optional
import torch
def precompute_freqs_cis(
dim: int, end: int, theta: float = 10000.0, scaling_factor: float = 1.0
) -> torch.Tensor:
freqs = 1.0 / (theta ** (torch.arange(0, dim, 2).float() / dim))
t = torch.arange(end, device=freqs.device).float() / scaling_factor # type: ignore
freqs = torch.outer(t, freqs).float() # type: ignore
@andreaskoepf
andreaskoepf / export_model.py
Last active August 17, 2023 22:08
Simplified export model script
import argparse
import sys
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Push checkpoints in HF transformers format to the Huggingface Hub.",
@andreaskoepf
andreaskoepf / llama2_system_prompt.txt
Created August 21, 2023 01:37
llama2 system prompt
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct.
If you don't know the answer to a question, please don't share false information."