Skip to content

Instantly share code, notes, and snippets.

@gg2001
gg2001 / gpt2_tensor.py
Last active September 1, 2024 02:23
GPT-2 inference implemented in pure PyTorch tensors - no torch.nn/modules. Only imports gelu, layer_norm and softmax from torch.nn.functional + model weights & tokenizer from Hugging Face. Run with -> python gpt2_tensor.py --prompt "hello"
import argparse
import torch
from torch.nn.functional import gelu, layer_norm, softmax
from transformers import GPT2Model, AutoTokenizer
from typing import TypedDict
MODEL_NAME = "gpt2"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = GPT2Model.from_pretrained(MODEL_NAME).to(device)
@gg2001
gg2001 / CToken.py
Last active October 6, 2021 10:11
Python implementation of Compound's CToken & InterestRateModel contract
import abc
def truncate(n: int, decimals: int = 18) -> float:
return n / (10 ** decimals)
def apy(ratePerBlock: int) -> float:
return ((((((ratePerBlock / 1e18) * 6570) + 1) ** 365)) - 1) * 100
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.7.6;
pragma abicoder v2;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IUniswapV3FlashCallback } from "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3FlashCallback.sol";
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import { ISwapRouter } from "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import { LowGasSafeMath } from "@uniswap/v3-core/contracts/libraries/LowGasSafeMath.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
@gg2001
gg2001 / Global.gitignore
Last active May 21, 2020 13:09
Global .gitignore for macOS, Linux, Windows, Emacs, Vim, VS Code, Sublime Text
### Begin Global.gitignore (https://github.com/github/gitignore)
### https://gist.github.com/gg2001/6a838a9bbae3138e0a2f7c1712058708
## macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride