Skip to content

Commit

Permalink
setting up ci-cd
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorgedavyd committed May 28, 2024
1 parent 732a52a commit 3781af8
Show file tree
Hide file tree
Showing 22 changed files with 288 additions and 44 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Default

on:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: [3.7, 3.8, 3.9, 3.11, 3.12]

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Set up the dependencies
run: |
python -m pip install --upgrade pip
pip install pipreqs pip-tools
chmod +x requirements.sh
sh ./requirements.sh
- name: Install dependencies
run: |
pip install pytest black
pip install -r requirements.txt
- name: Run black
id: black
run: |
black .
git config --global user.name 'Jorgedavyd'
git config --global user.email 'jorged.encyso@gmail.com'
git diff --exit-code || (git add . && git commit -m "Automatically formatted with black" && git push)
- name: Run tests
run: |
pytest tests/
46 changes: 46 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: To PyPI

on:
push:
tags:
- '*.*.*' # Trigger on all tag pushes

jobs:
build-and-publish:
name: Build and publish Python 🐍 distributions 📦 to PyPI
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel setuptools
- name: Extract tag name
id: extract_tag
run: echo "TAG_NAME=$(echo $GITHUB_REF | cut -d/ -f3)" >> $GITHUB_ENV

- name: Update version in setup.py and lightorch/_version.py
run: |
sed -i "s/{{VERSION_PLACEHOLDER}}/${{ env.TAG_NAME }}/g" setup.py
sed -i "s/{{VERSION_PLACEHOLDER}}/${{ env.TAG_NAME }}/g" lightorch/_version.py
- name: Build the package
run: |
python setup.py sdist bdist_wheel
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@v1.8.14
with:
password: ${{ secrets.PYPI_API_TOKEN }}

- name: Clean up
run: rm -rf build dist *.egg-info
2 changes: 2 additions & 0 deletions docs/RELEASES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 0.0.1
First Release
3 changes: 1 addition & 2 deletions lightorch/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
__version_info__ = ("0", "0", "1")
__version__ = ".".join(__version_info__)
__version__ = '{{VERSION_PLACEHOLDER}}'
1 change: 1 addition & 0 deletions lightorch/nn/criterions.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,5 @@ def forward(self, out: Tensor, target: Tensor) -> Tensor:
"PeakNoiseSignalRatio",
"StyleLoss",
"PerceptualLoss",
"Loss"
]
37 changes: 30 additions & 7 deletions lightorch/nn/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from torch.nn import init
from math import sqrt
import torch.nn.functional as f
from typing import Tuple

class _FourierConvNd(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
*kernel_size,
padding: Tuple[int],
bias: bool = True,
eps: float = 1e-5,
pre_fft: bool = True,
Expand All @@ -22,7 +24,7 @@ def __init__(
super().__init__()

self.factory_kwargs = {"device": device, "dtype": dtype}

self.padding = padding
if pre_fft:
self.fft = lambda x: fftn(x, dim=(-i for i in range(1, len(kernel_size))))
else:
Expand Down Expand Up @@ -73,7 +75,12 @@ def __init__(self, *args, **kwargs) -> None:
def forward(self, input: Tensor) -> Tensor:
if self.fft:
input = self.fft(input)
out = F.fourierconv1d(input, self.one, self.weight, self.bias)
if self.padding is not None:
out = F.fourierconv1d(input, self.one, self.weight, self.bias)
else:
out = F.fourierconv1d(f.pad(
input, self.padding, mode = 'constant', value = 0
), self.one, self.weight, self.bias)
if self.ifft:
return self.ifft(out)
return out
Expand All @@ -86,7 +93,11 @@ def __init__(self, *args, **kwargs) -> None:
def forward(self, input: Tensor) -> Tensor:
if self.fft:
input = self.fft(input)
out = F.fourierconv2d(input, self.one, self.weight, self.bias)
if self.padding is not None:
out = F.fourierconv2d(input, self.one, self.weight, self.bias)
else:
out = F.fourierconv2d(f.pad(input, self.padding, 'constant', value = 0), self.one, self.weight, self.bias)

if self.ifft:
return self.ifft(out)
return out
Expand All @@ -99,7 +110,10 @@ def __init__(self, *args, **kwargs) -> None:
def forward(self, input: Tensor) -> Tensor:
if self.fft:
input = self.fft(input)
out = F.fourierconv3d(input, self.one, self.weight, self.bias)
if self.padding is not None:
out = F.fourierconv3d(input, self.one, self.weight, self.bias)
else:
out = F.fourierconv3d(f.pad(input, self.padding, 'constant', value = 0), self.one, self.weight, self.bias)
if self.ifft:
return self.ifft(out)
return out
Expand All @@ -111,7 +125,10 @@ def __init__(self, *args, **kwargs) -> None:
def forward(self, input: Tensor) -> Tensor:
if self.fft:
input = self.fft(input)
out = F.fourierdeconv1d(input, self.one, self.weight, self.bias, self.eps)
if self.padding is not None:
out = F.fourierdeconv1d(input, self.one, self.weight, self.bias)
else:
out = F.fourierdeconv1d(f.pad(input, self.padding, 'constant', value = 0), self.one, self.weight, self.bias)
if self.ifft:
return self.ifft(out)
return out
Expand All @@ -124,7 +141,10 @@ def __init__(self, *args, **kwargs) -> None:
def forward(self, input: Tensor) -> Tensor:
if self.fft:
input = self.fft(input)
out = F.fourierdeconv2d(input, self.one, self.weight, self.bias, self.eps)
if self.padding is not None:
out = F.fourierdeconv2d(input, self.one, self.weight, self.bias)
else:
out = F.fourierdeconv2d(f.pad(input, self.padding, 'constant', value = 0), self.one, self.weight, self.bias)
if self.ifft:
return self.ifft(out)
return out
Expand All @@ -137,7 +157,10 @@ def __init__(self, *args, **kwargs) -> None:
def forward(self, input: Tensor) -> Tensor:
if self.fft:
input = self.fft(input)
out = F.fourierdeconv3d(input, self.one, self.weight, self.bias, self.eps)
if self.padding is not None:
out = F.fourierdeconv3d(input, self.one, self.weight, self.bias)
else:
out = F.fourierdeconv3d(f.pad(input, self.padding, 'constant', value = 0), self.one, self.weight, self.bias)
if self.ifft:
return self.ifft(out)
return out
Expand Down
4 changes: 2 additions & 2 deletions lightorch/nn/transformer/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def forward(self, **kwargs) -> Tensor:
hist.append(out)

for cross, decoder in zip(hist, self.decoder):
out = decoder(**kwargs, cross)
out = decoder(**kwargs, cross = cross)

out = self.fc(out)

Expand All @@ -102,7 +102,7 @@ def __init__(self, *cells, n_layers: int, fc: nn.Module) -> None:
self.fc = fc
self.n_layers = n_layers

def _single_forward(self, cells: Sequence[TransformerCell], layer: int, first_args: Sequence, second_args: Sequence) -> Tensor:
def _single_forward(self, cells: Sequence[TransformerCell], first_args: Sequence, second_args: Sequence) -> Tensor:
out0 = cells[0].self_attention(*first_args)
out1 = cells[1].self_attention(*second_args)

Expand Down
2 changes: 0 additions & 2 deletions lightorch/training/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
from .adversarial import *
from .supervised import *
4 changes: 4 additions & 0 deletions requirements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rm requirements.txt
pipreqs lightorch/ --savepath=requirements.in
pip-compile
rm requirements.in
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from setuptools import setup, find_packages
from lightorch import __version__, __author__, __email__
from lightorch import __author__, __email__
from pathlib import Path

this_directory = Path(__file__).parent
Expand All @@ -8,7 +8,7 @@
if __name__ == "__main__":
setup(
name="lightorch",
version=__version__,
version='{{VERSION_PLACEHOLDER}}',
packages=find_packages(),
author=__author__,
long_description=long_description,
Expand Down
7 changes: 7 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Tests
1. Unit Tests:
- All functions/methods covered.
- Edge cases considered.
2. Integration Tests:
- Interactions with other modules tested.
- API calls verified.
Empty file removed tests/htuning.py
Empty file.
8 changes: 0 additions & 8 deletions tests/nn/criterions.py

This file was deleted.

11 changes: 0 additions & 11 deletions tests/nn/functional.py

This file was deleted.

Empty file removed tests/nn/modules.py
Empty file.
7 changes: 0 additions & 7 deletions tests/nn/transformer.py

This file was deleted.

File renamed without changes.
File renamed without changes.
63 changes: 63 additions & 0 deletions tests/test_nn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from ..lightorch.nn.criterions import *
from torch import Tensor, nn
from .utils import *
import random

## Test automation for criterions

# Unit tests
input: Tensor = create_inputs(1, 3, 256, 256)
target: Tensor = create_inputs(1, 3, 256, 256)

mu: Tensor = create_inputs(1, 32)
logvar: Tensor = create_inputs(1, 32)

model: nn.Module = Model(32)
randint: int = random.randint(-100, 100)


def test_tv() -> None:
loss = TV(randint)
loss(input = input)


def test_style() -> None:
loss = StyleLoss(model, input, randint)
loss(input = input, target = target, feature_extractor = False)


def test_perc() -> None:
loss = PerceptualLoss(model, input, randint)
loss(input = input, target = target, feature_extractor = False)

def test_psnr() -> None:
loss = PeakNoiseSignalRatio(1, randint)
loss(input = input, target = target)

# Integration tests
def test_lagrange() -> None:
loss = LagrangianFunctional(
nn.MSELoss(), nn.MSELoss(), nn.MSELoss(), lambd = Tensor([randint for _ in range(2)])
)
loss(input=input, target=target)

def test_loss() -> None:
loss = Loss(
TV(randint), PeakNoiseSignalRatio(1, randint)
)
loss(input = input, target = target)

def test_elbo() -> None:
loss = ELBO(
randint, PeakNoiseSignalRatio(1, randint)
)
loss(input = input, target = target, mu = mu, logvar = logvar)

def test_fourier() -> None:
input: Tensor = create_inputs(1, 3, 256, 256)

def test_partial() -> None:
raise NotImplementedError

def test_kan() -> None:
raise NotImplementedError
40 changes: 40 additions & 0 deletions tests/test_supervised.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from ..lightorch.training.supervised import Module
from ..lightorch.nn.criterions import PeakNoiseSignalRatio
from ..lightorch.htuning.optuna import htuning
from .utils import Model, create_inputs, DataModule

import random
from torch import Tensor, nn
in_size: int = 32
input: Tensor = create_inputs(1, in_size)
randint: int = random.randint(-100, 100)
#Integrated test

class SupModel(Module):
def __init__(self, **hparams) -> None:
super().__init__(**hparams)
self.criterion = PeakNoiseSignalRatio(1, randint)
self.model = Model(in_size)

def forward(self, input: Tensor) -> Tensor:
return self.model(input)

def objective():
pass

def test_supervised() -> None:
htuning(
model_class = SupModel,
hparam_objective=objective,
datamodule = DataModule,
valid_metrics = 'MSE',
datamodule_kwargs= dict(
pin_memory = False,
num_workers = 1,
batch_size = 1
),
directions = 'minimize',
precision = 'high',
n_trials = 10,
trianer_kwargs = dict(fast_dev_run = True)
)
Empty file removed tests/training.py
Empty file.
Loading

0 comments on commit 3781af8

Please sign in to comment.