Skip to content

Instantly share code, notes, and snippets.

View sandeepkumar-skb's full-sized avatar
:octocat:
Get comfortable being uncomfortable

Sandeep Kumar Behera sandeepkumar-skb

:octocat:
Get comfortable being uncomfortable
View GitHub Profile
import torch
import torch.distributed as dist
from torch.distributed.elastic.multiprocessing.errors import record
import datetime
import os
@record
def main():
os.environ["NCCL_ASYNC_ERROR_HANDLING"] = "1"
dist.init_process_group(backend="nccl", timeout=datetime.timedelta(seconds=1800))
import torch
import torch.distributed as dist
from torch.distributed.elastic.multiprocessing.errors import record
import datetime
import os
@record
def main():
os.environ["NCCL_ASYNC_ERROR_HANDLING"] = "1"
dist.init_process_group(backend="nccl", timeout=datetime.timedelta(seconds=1800))
import onnxruntime as ort
import torch
from time import perf_counter
ort_session = ort.InferenceSession("resnet50.onnx", providers=["CPUExecutionProvider"])
print(ort.get_device())
inputs = {}
for inp in ort_session.get_inputs():
inputs[inp.name] = torch.rand(inp.shape).numpy()
import pickle
def save_pkl(filename_, inp):
with open(filename_, 'wb') as f:
pickle.dump(inp, f)
def load_pkl(filename_, ):
with open(filename_, 'rb') as f:
l_file = pickle.load(f)
import torch, time, gc, contextlib
# Timing utilities
start_time = None
@contextlib.contextmanager
def add_timer(local_msg):
try:
start_timer()
yield
from pynvml import *
nvmlInit()
deviceCount = nvmlDeviceGetCount()
# assume its 1 for now
handle = nvmlDeviceGetHandleByIndex(0)
util = nvmlDeviceGetUtilizationRates(handle)
from contextlib import contextmanager
# Context Manager using object
class ContextManager():
def __init__(self, name):
self.name = name
def __enter__(self, ):
print('nvtx_marker on for: {}'.format(self.name))
return self
@sandeepkumar-skb
sandeepkumar-skb / py_multiprocess_computeB.py
Last active March 31, 2021 03:49
This is a multiprocessing example speeding up a compute bound problem.
import multiprocessing as mp
import time as time
def square():
for i in range(1000000):
x = pow(i, 2)
if __name__ == "__main__":
num_iter = 10
start = time.time()
@sandeepkumar-skb
sandeepkumar-skb / py_thread_compute_bound.py
Created March 31, 2021 03:34
This python threading example demonstrates python threading in compute bound situation.
import threading
import time as time
def square():
for i in range(1000000):
x = pow(i, 2)
if __name__ == "__main__":
num_iter = 10
start = time.time()
@sandeepkumar-skb
sandeepkumar-skb / python_thread_io.py
Last active March 31, 2021 16:03
This python example demonstrates python threading being useful for IO bound operations.
import threading
import time as time
def display(i):
print("Thread id: ", i)
time.sleep(4)
if __name__ == "__main__":