Skip to content

Instantly share code, notes, and snippets.

View lebrice's full-sized avatar

Fabrice Normandin lebrice

View GitHub Profile
@lebrice
lebrice / autograd.py
Last active May 22, 2024 14:33
Interview Question
from __future__ import annotations
class Value:
""" stores a single scalar value and its gradient """
def __init__(self, data, _parents: tuple[Value, ...]=(), _op=''):
self.data = data
self.grad = 0
# internal variables used for autograd graph construction
self._backward = lambda: None
@lebrice
lebrice / mt_layers.py
Last active September 1, 2023 16:02
Multi-task layers (that can be split into layers for each task)
from __future__ import annotations
import copy
import functools
import math
from collections import OrderedDict
from typing import Sequence
import torch
from torch import Tensor, nn
"""
"""
from __future__ import annotations
import datetime
import itertools
# NOTE: Need to import cv2 to prevent a loading error for GLIBCXX with ffcv.
import cv2 # noqa
@lebrice
lebrice / imagenet_ffcv.py
Last active August 15, 2022 15:52
Imagenet FFCV Datamodule. Meant for a SLURM cluster (e.g. the Mila cluster). Builds on top of https://gist.github.com/lebrice/4a67df47d9fca3e199d3e7686396240c
""" ImageNet datamodule that uses FFCV. """
from __future__ import annotations
import typing
from collections.abc import Iterable, Sequence
from pathlib import Path
from typing import Any, Callable, TypeVar
import cv2 # noqa
@lebrice
lebrice / ffcv_test.py
Created August 9, 2022 20:33
ffcv_test.py
from __future__ import annotations
import itertools
from pytorch_lightning import Trainer
import torch
import numpy as np
from pytorch_lightning import LightningModule
from torch import nn, Tensor
import pytest
from .imagenet_ffcv import ImagenetFfcvDataModule
@lebrice
lebrice / pep_idea.py
Created August 4, 2022 21:11
Idea for a new use for typing.Unpack: Annotate the signature of a callable.
from __future__ import annotations
from pytorch_lightning import Trainer
from typing_extensions import Unpack, ParamSpec
from typing import Callable, TypedDict
# Option A: TypedDict
# --> lots of code duplication!
class TrainerConfig(TypedDict, total=False):
@lebrice
lebrice / imagenet.py
Last active August 17, 2022 21:38
Imagenet DataModule adapted for the Mila Cluster
""" ImageNet datamodule adapted to the Mila cluster.
Can be used either with a PyTorch-Lightning Trainer, or by itself to easily get efficient
dataloaders for the ImageNet dataset.
Requirements (these are the versions I'm using, but this can probably be loosened a bit).
- pytorch-lightning==1.6.0
- lightning-bolts==0.5
"""
@lebrice
lebrice / setup_cache.py
Last active October 4, 2022 17:32
Consolidating the cache on the Mila cluster
"""Sets up a user cache directory for commonly used libraries, while reusing shared cache entries.
Use this to avoid having to download files to the $HOME directory, as well as to remove
duplicated downloads and free up space in your $HOME and $SCRATCH directories.
The user cache directory should be writeable, and doesn't need to be empty.
This command adds symlinks to (some of) the files contained in the *shared* cache directory to this
user cache directory.
The shared cache directory should be readable (e.g. a directory containing frequently-downloaded
from __future__ import annotations
# Context: Dataset is on GPU memory.
from typing import Iterable
import torch
from torch import Tensor
from torchvision.datasets import MNIST
from torch.utils.data import TensorDataset, DataLoader, Dataset, ConcatDataset
@lebrice
lebrice / job_array_example.py
Last active June 9, 2022 19:18
Job array example
from dataclasses import dataclass
import os
from simple_parsing import ArgumentParser
from itertools import product
@dataclass
class ProblemConfig:
dataset: int = 0 # Which dataset ID to use.
rank: int = 0 # The rank of some matrix