base.py (2163B)
1 # File: base.py 2 # Created Date: Saturday June 4th 2022 3 # Author: Steven Atkinson ([email protected]) 4 5 import abc as _abc 6 from pathlib import Path as _Path 7 from tempfile import TemporaryDirectory as _TemporaryDirectory 8 9 import pytest as _pytest 10 import torch as _torch 11 12 13 class Base(_abc.ABC): 14 @classmethod 15 def setup_class(cls, C, args=None, kwargs=None): 16 cls._C = C 17 cls._args = () if args is None else args 18 cls._kwargs = {} if kwargs is None else kwargs 19 20 def test_init(self, args=None, kwargs=None): 21 obj = self._construct(args=args, kwargs=kwargs) 22 assert isinstance(obj, self._C) 23 24 def test_export(self, args=None, kwargs=None): 25 model = self._construct(args=args, kwargs=kwargs) 26 with _TemporaryDirectory() as tmpdir: 27 model.export(_Path(tmpdir)) 28 29 @_pytest.mark.parametrize( 30 "device", 31 ( 32 _pytest.param( 33 "cuda", 34 marks=_pytest.mark.skipif( 35 not _torch.cuda.is_available(), reason="CUDA-specific test" 36 ), 37 ), 38 _pytest.param( 39 "mps", 40 marks=_pytest.mark.skipif( 41 not _torch.backends.mps.is_available(), reason="MPS-specific test" 42 ), 43 ), 44 ), 45 ) 46 def test_process_input_longer_than_65536_on(self, device: str): 47 """ 48 Processing inputs longer than 65,536 samples using various accelerator 49 backends can cause problems. 50 51 See: 52 * https://github.com/sdatkinson/neural-amp-modeler/issues/505 53 * https://github.com/sdatkinson/neural-amp-modeler/issues/512 54 55 (Funny that both have the same length limit--65,536...) 56 57 Assert that precautions are taken. 58 """ 59 x = _torch.zeros((65_536 + 1,)).to(device) 60 model = self._construct().to(device) 61 model(x) 62 63 def _construct(self, C=None, args=None, kwargs=None): 64 C = self._C if C is None else C 65 args = args if args is not None else self._args 66 kwargs = kwargs if kwargs is not None else self._kwargs 67 return C(*args, **kwargs)