Skip to content

Instantly share code, notes, and snippets.

# BSD 3-Clause License
#
# Copyright (c) 2023, maharjun
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
@maharjun
maharjun / scooputils.py
Created May 4, 2023 05:11
SCOOP Utilities
"""A bunch of utilities that prove useful with the SCOOP Parallelization framework"""
###############################################################################
# BSD 3-Clause License
#
# Copyright (c) 2023, maharjun
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
@maharjun
maharjun / looputils.py
Last active May 4, 2023 05:59
Loop Interval counter
"""
This file contains the code for some very helpful utilities to store data and
perform auxiliary actions periodically in training loops and the like.
"""
###############################################################################
# BSD 3-Clause License
#
# Copyright (c) 2023, maharjun
#
@maharjun
maharjun / diskcacheutils.py
Created May 4, 2023 06:20
Function Disk Cache
"""
This module provides utilities for caching the results of time-consuming functions
to disk for faster retrieval in future runs. The main classes and functions are:
- hash_det: A function that generates a deterministic hash value for an object
(immune to changes in its id).
- function_takes_no_arguments: A helper function that checks if a given function
takes no arguments.
@maharjun
maharjun / torchdillshim.py
Created May 4, 2023 06:44
Torch Dill Shim
"""
This is a shim for dill to be used with torch (namely that when used in a project
that pickles torch objects, dill should be imported from this module).
for example::
from utils.dillshim import dill
The purpose of this shim is register the pickling and unpickling logic
for certain native pytorch types such as torch random generators that
otherwise cannot be pickled by dill, as well as to be able to unpickle
@maharjun
maharjun / pytorch-utils.py
Created May 4, 2023 08:34
Pytorch Utilities
"""
Utility functions for PyTorch data manipulation and device handling.
This module provides various utility functions for handling PyTorch datasets, tensors, and devices. Functions include:
- Splitting datasets into train and test sets
- Concatenating multiple datasets
- Generating random batches from a dataset
- Converting data to a specific device
- Retrieving the default device
- Getting GPU device names
@maharjun
maharjun / cached_class_method.py
Created May 4, 2023 11:26
Cached class method decorator
"""
The cached_class_method utility is a Python function decorator that caches the
results of a class method in order to avoid recomputing the same values when the
method is called multiple times with the same arguments. It does so by storing the
results in a cache dictionary as an attribute of the class instance, and looking
them up whenever the method is called with the same arguments.
"""
###############################################################################
# BSD 3-Clause License