Skip to content

Instantly share code, notes, and snippets.

View chiragjn's full-sized avatar
⌨️
Programming with Python mostly but sometimes Go and Typescript

Chirag Jain chiragjn

⌨️
Programming with Python mostly but sometimes Go and Typescript
View GitHub Profile
@chiragjn
chiragjn / Dockerfile
Created February 20, 2024 10:21
llama.cpp python cuda Dockerfile example
ARG CUDA_IMAGE="12.1.1-devel-ubuntu20.04"
FROM --platform=linux/amd64 nvidia/cuda:${CUDA_IMAGE}
ENV DEBIAN_FRONTEND=noninteractive
RUN useradd -m -u 1000 user
USER root
RUN apt-get update \
&& apt-get install -y software-properties-common \
&& add-apt-repository ppa:cnugteren/clblast \
@chiragjn
chiragjn / put_site_packages_cuda_on_path.py
Created July 18, 2023 12:02
Put nvidia libs in site-packages on your LD_LIBRARY_PATH for other cuda compatible libraries to find
import os
import site
import glob
lib_paths = []
for location in site.getsitepackages():
for path in glob.glob(os.path.join(location, "nvidia/**/lib*.so*"), recursive=True):
to_add = os.path.dirname(path)
if to_add not in lib_paths:
lib_paths.append(to_add)
@chiragjn
chiragjn / lrudict.py
Created December 13, 2021 06:13
Sample lru dict impl
import collections
from typing import Optional, TypeVar, ContextManager, Generic, Any, MutableMapping
class LRUDict(collections.OrderedDict, MutableMapping[_KT, _VT]):
def __init__(self, max_size: Optional[int] = None, min_size: int = 0):
super().__init__()
self.min_size = min_size
self.max_size = max_size
if self.max_size and self.max_size < self.min_size:
@chiragjn
chiragjn / tensorflow_2.6.0_cu101_instructions.md
Last active September 6, 2021 00:29
Compile tensorflow 2.6.0 against cuda 10.1

Run dev container

docker run --gpus all -itd --name tf_cu101_build tensorflow/tensorflow:latest-devel-gpu-py3

Inside it

Update deps

apt update
@chiragjn
chiragjn / download.sh
Last active September 3, 2020 12:24
Small BERT checkpoints vs tfhub modules
#!/bin/bash
mkdir -p small_bert_checkpoints
cd small_bert_checkpoints/
wget https://storage.googleapis.com/bert_models/2020_02_20/all_bert_models.zip
unzip all_bert_models.zip
find . -name 'uncased*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;
rm uncased*.zip
rm all_bert_models.zip
@chiragjn
chiragjn / input_utils.rs
Created June 6, 2020 21:47
Some Rust stdin input utils for common input formats in competitive programming. Probably not good code
// Sample Usages:
// https://codeforces.com/contest/1352/submission/82738753
// https://codeforces.com/contest/1352/submission/82737636
use std::io::{self, BufRead, Read, Stdin};
use std::iter;
use std::str::FromStr;
struct InputUtils {
stream: Stdin,
@chiragjn
chiragjn / log_time_contextdecorator.py
Created April 21, 2020 09:25
An example of single callable that works as both a contextmanager and a decorator
import logging
import sys
import contextlib
from timeit import default_timer as timer
from typing import Any
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
@chiragjn
chiragjn / blue_green_download.py
Last active April 10, 2020 04:58
Experiment where we try to download a requested asset on a separate thread but return already loaded older asset if that is available
# Locks are tricky, maybe write with Queues ?
import concurrent.futures
import contextlib
import datetime
import threading
import time
scheduled = {}
loaded = {}
@chiragjn
chiragjn / json_response_handler_interface.py
Last active April 4, 2020 15:48
Just some thoughts around a template to handle various requests errors
import abc
import json
from typing import Dict, Any, Optional
import requests
class JsonResponseRequest(abc.ABC):
@abc.abstractmethod
def response_has_errors(self, response_json: Dict[str, Any], response: requests.Response) -> bool:
success = response_json.get('success', True)
from contextlib import contextmanager
import cProfile, pstats, io
from timeit import default_timer as timer
from pyinstrument import Profiler
from pyinstrument.renderers import ConsoleRenderer
@contextmanager
def pyinst(r=None):
r = {} if r is None else r