openfl icon indicating copy to clipboard operation
openfl copied to clipboard

Circular import issue

Open alexey-khorkin opened this issue 3 years ago • 1 comments

Circular import error when starting director. The problem occurs when we will annotate the model_interface parammeter inside NumPyAdagrad and then start director (any interactive-api example could be used)

from openfl.interface.interactive_api.experiment import ModelInterface
...
class NumPyAdagrad(Optimizer):
    """Adagrad optimizer implementation.

    Original paper: http://jmlr.org/papers/v12/duchi11a.html
    """

    def __init__(
        self,
        *,
        params: Optional[Dict[str, np.ndarray]] = None,
        model_interface: Optional[ModelInterface] = None,     # <---- NEW ANNOTATION HERE
        learning_rate: float = 0.01,
        initial_accumulator_value: float = 0.1,
        epsilon: float = 1e-10,
    ) -> None:

The point here is that this interface class must have the ability to be imported anywhere, as it is an interface and can be used for annotations.

To Reproduce

  1. Сheckout on circular_import_issue branch
  2. Install openfl:
pip install .
  1. Go to MNIST example to start director:
cd openfl-tutorials/interactive_api/Tensorflow_MNIST/director/
./start_director.sh
  1. See error:
./start_director.sh 
Traceback (most recent call last):
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/bin/fx", line 11, in <module>
    load_entry_point('openfl==1.2.1', 'console_scripts', 'fx')()
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/interface/cli.py", line 207, in entry
    command_group = import_module(module, package)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/interface/director.py", line 17, in <module>
    from openfl.component.director import Director
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/component/__init__.py", line 6, in <module>
    from .aggregator import Aggregator
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/component/aggregator/__init__.py", line 6, in <module>
    from .aggregator import Aggregator
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/component/aggregator/aggregator.py", line 8, in <module>
    from openfl.component.aggregation_functions import WeightedAverage
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/component/aggregation_functions/__init__.py", line 6, in <module>
    from .adagrad_adaptive_aggregation import AdagradAdaptiveAggregation
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/component/aggregation_functions/adagrad_adaptive_aggregation.py", line 11, in <module>
    from openfl.utilities.optimizers.numpy import NumPyAdagrad
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/utilities/optimizers/numpy/__init__.py", line 5, in <module>
    from .adagrad_optimizer import NumPyAdagrad
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/utilities/optimizers/numpy/adagrad_optimizer.py", line 8, in <module>
    from openfl.interface.interactive_api.experiment import ModelInterface
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/interface/interactive_api/experiment.py", line 16, in <module>
    from openfl.component.aggregation_functions import AggregationFunction
ImportError: cannot import name 'AggregationFunction' from partially initialized module 'openfl.component.aggregation_functions' (most likely due to a circular import) (/home/akhorkin/Repositories/alexey-khorkin-repos/test/openfl/venv/lib/python3.8/site-packages/openfl/component/aggregation_functions/__init__.py)

alexey-khorkin avatar Mar 11 '22 12:03 alexey-khorkin

A new one:

>>> from openfl.databases import TensorDB
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/openfl/openfl/databases/__init__.py", line 6, in <module>
    from .tensor_db import TensorDB
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/openfl/openfl/databases/tensor_db.py", line 14, in <module>
    from openfl.component.aggregation_functions import AggregationFunction
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/openfl/openfl/component/__init__.py", line 6, in <module>
    from .aggregator import Aggregator
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/openfl/openfl/component/aggregator/__init__.py", line 6, in <module>
    from .aggregator import Aggregator
  File "/home/akhorkin/Repositories/alexey-khorkin-repos/openfl/openfl/component/aggregator/aggregator.py", line 9, in <module>
    from openfl.databases import TensorDB
ImportError: cannot import name 'TensorDB' from partially initialized module 'openfl.databases' (most likely due to a circular import) (/home/akhorkin/Repositories/alexey-khorkin-repos/openfl/openfl/databases/__init__.py)

alexey-khorkin avatar Mar 24 '22 07:03 alexey-khorkin