Skip to content

Commit

Permalink
Merge pull request #91 from neuro-ml/develop
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
maxme1 authored Nov 23, 2024
2 parents 084f1fa + 75b3360 commit fcd06be
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 53 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v4
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: '3.10'

- id: get_version
name: Get the release version
Expand All @@ -30,7 +30,8 @@ jobs:
echo $MATCH
if [ "$GITHUB_BASE_REF" = "master" ] && [ "$MATCH" != "" ]; then echo "Version $VERSION already present" && exit 1; fi
if [ "$VERSION" != "$RELEASE" ]; then echo "$VERSION vs $RELEASE" && exit 1; fi
python setup.py sdist
pip install build
python -m build --sdist
- name: Publish to PyPi
uses: pypa/gh-action-pypi-publish@master
Expand Down
27 changes: 10 additions & 17 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,19 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [ '3.6', '3.7', '3.8', '3.9', '3.10', '3.11' ]
python-version: [ '3.7', '3.8', '3.9', '3.10', '3.11', '3.12', '3.13' ]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Check the version
if: "! github.event.pull_request.head.repo.fork "
run: |
VERSION=$(python -c "from pathlib import Path; import runpy; folder, = {d.parent for d in Path().resolve().glob('*/__init__.py') if d.parent.is_dir() and (d.parent / '__version__.py').exists()}; print(runpy.run_path(folder / '__version__.py')['__version__'])")
MATCH=$(pip index versions deep-pipe | grep "Available versions:" | grep $VERSION) || echo
echo $MATCH
if [ "$GITHUB_BASE_REF" = "master" ] && [ "$MATCH" != "" ]; then exit 1; fi
- name: Build the package
run: |
python setup.py sdist
pip install build
python -m build --sdist
- name: Install
run: |
Expand All @@ -52,17 +46,16 @@ jobs:
sed -i -e "s|$(echo $MODULE_PARENT/ | tr "/" .)||g" reports/coverage-${{ matrix.python-version }}.xml
- name: Upload artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: reports-${{ matrix.python-version }}
path: reports/*-${{ matrix.python-version }}.xml
path: reports/*.xml
if: ${{ always() }}

- name: Upload coverage results
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
files: reports/coverage-${{ matrix.python-version }}.xml
token: ${{ secrets.CODECOV_TOKEN }}
files: reports/coverage-*.xml
verbose: true
25 changes: 25 additions & 0 deletions .github/workflows/version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Check Version

on: [ pull_request ]

env:
MODULE_NAME: deep-pipe

jobs:
check:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: '3.10'

- name: Check the version
if: "! github.event.pull_request.head.repo.fork "
run: |
VERSION=$(python -c "from pathlib import Path; import runpy; folder, = {d.parent for d in Path().resolve().glob('*/__init__.py') if d.parent.is_dir() and (d.parent / '__version__.py').exists()}; print(runpy.run_path(folder / '__version__.py')['__version__'])")
MATCH=$(pip index versions $MODULE_NAME | grep "Available versions:" | grep $VERSION) || echo
echo $MATCH
if [ "$GITHUB_BASE_REF" = "master" ] && [ "$MATCH" != "" ]; then exit 1; fi
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017-2020 NeuroML Group
Copyright (c) 2017-2024 NeuroML Group

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion dpipe/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.4.1'
__version__ = '0.4.2'
9 changes: 7 additions & 2 deletions dpipe/checks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from functools import wraps
from typing import Callable
import numpy as np


try:
from numpy.lib.array_utils import normalize_axis_tuple
except ImportError:
from numpy.core.numeric import normalize_axis_tuple

from .itertools import extract

Expand All @@ -12,7 +17,7 @@ def join(values):
def check_shape_along_axis(*arrays, axis):
sizes = []
for array in arrays:
sizes.append(tuple(extract(array.shape, np.core.numeric.normalize_axis_tuple(axis, array.ndim))))
sizes.append(tuple(extract(array.shape, normalize_axis_tuple(axis, array.ndim))))

if any(x != sizes[0] for x in sizes):
raise ValueError(f'Arrays of equal size along axis {axis} are required: {join(sizes)}')
Expand Down
8 changes: 7 additions & 1 deletion dpipe/im/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import numpy as np


try:
from numpy.lib.array_utils import normalize_axis_tuple
except ImportError:
from numpy.core.numeric import normalize_axis_tuple

from ..itertools import lmap
from ..checks import join

Expand Down Expand Up @@ -46,7 +52,7 @@ def axis_from_dim(axis: Union[AxesLike, None], dim: int) -> tuple:
if min(axis) < left or max(axis) > right:
raise ValueError(f'For dim={dim} axis must be within ({left}, {right}): but provided {axis}.')

return np.core.numeric.normalize_axis_tuple(axis, dim, 'axis')
return normalize_axis_tuple(axis, dim, 'axis')


def check_axes(axes) -> tuple:
Expand Down
21 changes: 11 additions & 10 deletions dpipe/torch/functional.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import warnings
from typing import Union, Callable

import numpy as np
import torch
from torch.nn import functional

from dpipe.im.axes import AxesLike
from ..im.axes import AxesLike, normalize_axis_tuple


__all__ = [
'focal_loss_with_logits', 'linear_focal_loss_with_logits', 'weighted_cross_entropy_with_logits',
Expand Down Expand Up @@ -177,11 +177,12 @@ def tversky_loss(pred: torch.Tensor, target: torch.Tensor, alpha=0.5, epsilon=1e
sum_dims = list(range(1, target.dim()))
beta = 1 - alpha

intersection = pred*target
fps, fns = pred*(1-target), (1-pred)*target
intersection = pred * target
fps, fns = pred * (1 - target), (1 - pred) * target

numerator = torch.sum(intersection, dim=sum_dims)
denumenator = torch.sum(intersection, dim=sum_dims) + alpha*torch.sum(fps, dim=sum_dims) + beta*torch.sum(fns, dim=sum_dims)
denumenator = torch.sum(intersection, dim=sum_dims) + alpha * torch.sum(fps, dim=sum_dims) + beta * torch.sum(fns,
dim=sum_dims)
tversky = numerator / (denumenator + epsilon)
loss = 1 - tversky

Expand All @@ -190,7 +191,7 @@ def tversky_loss(pred: torch.Tensor, target: torch.Tensor, alpha=0.5, epsilon=1e
return loss


def focal_tversky_loss(pred: torch.Tensor, target: torch.Tensor, gamma=4/3, alpha=0.5, epsilon=1e-7):
def focal_tversky_loss(pred: torch.Tensor, target: torch.Tensor, gamma=4 / 3, alpha=0.5, epsilon=1e-7):
"""
References
----------
Expand All @@ -200,7 +201,7 @@ def focal_tversky_loss(pred: torch.Tensor, target: torch.Tensor, gamma=4/3, alph
warnings.warn("Gamma is <=1, to focus on less accurate predictions choose gamma > 1.")
tl = tversky_loss(pred, target, alpha, epsilon, reduce=None)

return torch.pow(tl, 1/gamma).mean()
return torch.pow(tl, 1 / gamma).mean()


def loss_with_logits(criterion: Callable, logit: torch.Tensor, target: torch.Tensor, **kwargs):
Expand Down Expand Up @@ -243,8 +244,8 @@ def moveaxis(x: torch.Tensor, source: AxesLike, destination: AxesLike):
Move axes of a torch.Tensor to new positions.
Other axes remain in their original order.
"""
source = np.core.numeric.normalize_axis_tuple(source, x.ndim, 'source')
destination = np.core.numeric.normalize_axis_tuple(destination, x.ndim, 'destination')
source = normalize_axis_tuple(source, x.ndim, 'source')
destination = normalize_axis_tuple(destination, x.ndim, 'destination')
if len(source) != len(destination):
raise ValueError('`source` and `destination` arguments must have '
'the same number of elements')
Expand All @@ -260,7 +261,7 @@ def softmax(x: torch.Tensor, axis: AxesLike):
"""
A multidimensional version of softmax.
"""
source = np.core.numeric.normalize_axis_tuple(axis, x.ndim, 'axis')
source = normalize_axis_tuple(axis, x.ndim, 'axis')
dim = len(source)
destination = range(-dim, 0)

Expand Down
8 changes: 4 additions & 4 deletions dpipe/torch/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def optimizer_step(

if scaler is not None:
# autocast is not recommended during backward
with torch.cuda.amp.autocast(False):
with torch.autocast("cuda", enabled=False):
scaler.scale(loss).backward()

if not accumulate:
Expand Down Expand Up @@ -134,7 +134,7 @@ def train_step(
inputs = sequence_to_var(*inputs, device=architecture)
inputs, targets = inputs[:n_inputs], inputs[n_inputs:]

with torch.cuda.amp.autocast(scaler is not None or torch.is_autocast_enabled()):
with torch.autocast("cuda", enabled=scaler is not None or torch.is_autocast_enabled()):
loss = criterion(architecture(*inputs), *targets)

if loss_key is not None:
Expand Down Expand Up @@ -177,7 +177,7 @@ def inference_step(*inputs: np.ndarray, architecture: Module, activation: Callab
amp = amp or torch.is_autocast_enabled()
in_dtype = in_dtype or (torch.float16 if amp else None)
with _inference_ctx_manager():
with torch.cuda.amp.autocast(amp, cache_enabled=amp_cache_enabled):
with torch.autocast("cuda", enabled=amp, cache_enabled=amp_cache_enabled):
return to_np(
activation(architecture(*sequence_to_var(*inputs, device=architecture, dtype=in_dtype))),
dtype=out_dtype,
Expand All @@ -204,7 +204,7 @@ def multi_inference_step(*inputs: np.ndarray, architecture: Module,
amp = amp or torch.is_autocast_enabled()
in_dtype = in_dtype or (torch.float16 if amp else None)
with _inference_ctx_manager():
with torch.cuda.amp.autocast(amp, cache_enabled=amp_cache_enabled):
with torch.autocast("cuda", enabled=amp, cache_enabled=amp_cache_enabled):
results = architecture(*sequence_to_var(*inputs, device=architecture, dtype=in_dtype))
if callable(activations):
activations = [activations] * len(results)
Expand Down
2 changes: 1 addition & 1 deletion dpipe/torch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def load_model_state(module: nn.Module, path: PathLike, modify_state_fn: Callabl
For example, it could help you to transfer weights from similar but not completely equal architecture.
strict: bool
"""
state_to_load = torch.load(path, map_location=get_device(module))
state_to_load = torch.load(path, map_location=get_device(module), weights_only=True)
if modify_state_fn is not None:
current_state = module.state_dict()
state_to_load = modify_state_fn(current_state, state_to_load)
Expand Down
41 changes: 41 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[project]
name = 'deep_pipe'
dynamic = ['version', 'dependencies']
description = 'A collection of tools for deep learning experiments'
readme = 'README.md'
requires-python = '>=3.7'
license = { file = 'LICENSE' }
keywords = []
classifiers = [
'Development Status :: 4 - Beta',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'Programming Language :: Python :: 3 :: Only',
]

[project.urls]
'Homepage' = 'https://github.com/neuro-ml/deep_pipe'
'Issues' = 'https://github.com/neuro-ml/deep_pipe/issues'
'Source' = 'https://github.com/neuro-ml/deep_pipe'

[project.scripts]
dpipe-run = 'dpipe.layout.scripts:run'
dpipe-build = 'dpipe.layout.scripts:build'

[build-system]
requires = ['setuptools>=43.0.0', 'wheel']
build-backend = 'setuptools.build_meta'

[tool.setuptools.packages.find]
include = ['dpipe']

[tool.setuptools.dynamic]
version = { attr = 'dpipe.__version__.__version__' }
dependencies = { file = 'requirements.txt' }
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pandas
scikit-learn>=0.19
tqdm
pdp==0.3.*
tensorboard-easy
lazycon>=0.1.0,<1.0.0
loky>=3.0.0,<4.0.0
torch
Expand Down
26 changes: 16 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

from setuptools import setup, find_packages

classifiers = '''Development Status :: 4 - Beta
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12'''

classifiers = [
'Development Status :: 4 - Beta',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'Programming Language :: Python :: 3 :: Only',
]

with open('README.md', encoding='utf-8') as file:
long_description = file.read()
Expand All @@ -35,9 +41,9 @@
url='https://github.com/neuro-ml/deep_pipe',
download_url='https://github.com/neuro-ml/deep_pipe/v%s.tar.gz' % __version__,
keywords=[],
classifiers=classifiers.splitlines(),
classifiers=classifiers,
install_requires=requirements,
python_requires='>=3.6',
python_requires='>=3.7',
entry_points={
'console_scripts': [
'dpipe-run = dpipe.layout.scripts:run',
Expand Down

0 comments on commit fcd06be

Please sign in to comment.