You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
The issue related to <1565>, where the User encountered a problem while importing user-defined modules in Jupyter notebooks while running an experiment based on Workflow API & FederatedRuntime
In the current implementation of Workflow API, jupyter notebook is expected to define the Federated Learning experiment in it's entirety. If the user attempts to import a user defined module from a different python script it will fail due to following reasons
When the notebook is exported, the script inside the generated_workspace does not contain the user defined code
This will lead to ImportError/ModuleNotFoundError failure during execution on participants in a distributed infrastructure
Describe the solution you'd like Use case: Enable user's to import the helper functions from a separate python script. For e.g.
To support this use case, existing export process in notebook_tools needs to be enhanced to
Identify user-defined imports
Use the Python ast module to parse the exported script (i.e experiment.py)
Extract all import statements:
import module
from module import ...
Classify imports and filter only user-defined modules present in the workspace
Copy user-defined modules into the generated_workpace
Locate its corresponding .py file or folder within the workspace, the same directory where the Jupyter notebook is located.
Copy the module into the generated_workspace as follows
Directly copy, if it's a single file (e.g. validation.py)
If it's a folder (e.g. helper/validation.py), the entire folder is copied
This shall ensure that the FL experiment and workspace includes all user-defined module code
To enable the user defined imports from a different python file (workspace/validation.py), the generated_workspace shall also contain the additional python scrips. For e.g. existing workspace
workspace
├── crowd_guard.ipynb
└── validation.py
generated_workspace after the workspace is exported
Current implementation assumes that the Jupyter notebook contains all the user-defined logic required for the experiment Additional context: This enhancement is based on following Requirements & Guidelines
Export Directives
User defined utility / helper functions should be present in a notebook cell that is annotated by #| export directive as the first line
This is required to enable infrastructure to export the logic to exported script and analyze the exported script for any user-defined imports
No installations in User defined imports:
User defined utility should not install any packages as this will not be analyzed by the infrastructure to build the requirements.txt for the FL experiment
Location of User defined python scripts:
User-defined modules must be present inside the same directory as the Jupyter Notebook to enable infrastructure to copy and package in generate_workspace
Rationale: Path of user-defined module may not exist on distributed nodes and it may not be possible to copy the user-defined script to the given path
For e.g. If user creates scripts in openfl directory and uses them in the jupyter notebook (for e.g. from openfl.experimental.workflow.external.helper.validation import CrowdGuardClientValidation)
During AST module analysis of the exported script, the import statements could not be resolved.
It requires system path modification and rewriting of imports in the exported script to align with the generated_workspace
For e.g. from openfl.experimental.workflow.external.helper.validation import CrowdGuardClientValidation would need to be modified to from helper.validation import CrowdGuardClientValidation
Modification of imports is not recommended as it would introduce complexity
Restrictions on User-defined imports
User defined code should not modify the sys.path to enable python to find the scripts to import
For e.g.
Analysing whether ALL imports in user-defined code are user-defined could be quite complex
In above example notebook_tools shall be able to identify that from helper.validation import CrowdGuardClientValidation is a user defined import. But it would be quite complex to analyze and keep track if CrowdGuardClientValidation has other user defined imports, such as from utils import calculate_accuracy, compute_precision and whether they are in same python script or different
User defined module imports:
Used defined imports for the helper module (e.g.helper/validation) with or without an __init__.py are supported, because entire user-defined helper folders are copied to the generated_workspace, which would resolve the imports correctly in the exported script (e.g. experiment.py)
For e.g., Import statements like from helper.validation import CrowdGuardClientValidation and from helper import CrowdGuardClientValidation
If an __init__.py file is present inside the same directory (for e.g. workspace) as the Jupyter Notebook along with user-defined module validation.py. For e.g.
Using import statement in Jupyter notebook (for e.g. from validation import CrowdGuardClientValidation) remains valid in the exported script (experiment.py), as validation.py is copied to the generated_workspace.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is your feature request related to a problem? Please describe.
The issue related to <1565>, where the User encountered a problem while importing user-defined modules in Jupyter notebooks while running an experiment based on Workflow API &
FederatedRuntimeIn the current implementation of Workflow API, jupyter notebook is expected to define the Federated Learning experiment in it's entirety. If the user attempts to import a user defined module from a different python script it will fail due to following reasons
generated_workspacedoes not contain the user defined codeImportError/ModuleNotFoundErrorfailure during execution on participants in a distributed infrastructureDescribe the solution you'd like
Use case: Enable user's to import the helper functions from a separate python script. For e.g.
Filename: validation.py (contains helper functions)
Filename: CrowdGuard.ipynb (Jupyter notebook for Workflow API experiment)
To support this use case, existing export process in
notebook_toolsneeds to be enhanced toastmodule to parse the exported script (i.eexperiment.py)Classify imports and filter only user-defined modules present in the workspace
Copy user-defined modules into the
generated_workpace.pyfile or folder within theworkspace, the same directory where the Jupyter notebook is located.generated_workspaceas followsvalidation.py)helper/validation.py), the entire folder is copiedThis shall ensure that the FL experiment and workspace includes all user-defined module code
To enable the user defined imports from a different python file (workspace/validation.py), the generated_workspace shall also contain the additional python scrips. For e.g. existing workspace
generated_workspace after the workspace is exported
Describe alternatives you've considered
Additional context: This enhancement is based on following Requirements & Guidelines
Export Directives
#| exportdirective as the first lineNo installations in User defined imports:
requirements.txtfor the FL experimentLocation of User defined python scripts:
generate_workspaceFor e.g. If user creates scripts in openfl directory and uses them in the jupyter notebook (for e.g.
from openfl.experimental.workflow.external.helper.validation import CrowdGuardClientValidation)During AST module analysis of the exported script, the import statements could not be resolved.
It requires system path modification and rewriting of imports in the exported script to align with the
generated_workspaceFor e.g.
from openfl.experimental.workflow.external.helper.validation import CrowdGuardClientValidationwould need to be modified tofrom helper.validation import CrowdGuardClientValidationModification of imports is not recommended as it would introduce complexity
Restrictions on User-defined imports
User defined code should not modify the
sys.pathto enable python to find the scripts to importFor e.g.
Filename:CrowdGuard.ipynb (Jupyter notebook for Workflow API experiment)
Correct_Usage
User-defined imports should be self-contained:
Filename: utils.py (contains additional helper functions)
Filename: validation.py (contains helper functions)
from helper.validation import CrowdGuardClientValidationis a user defined import. But it would be quite complex to analyze and keep track ifCrowdGuardClientValidationhas other user defined imports, such asfrom utils import calculate_accuracy, compute_precisionand whether they are in same python script or differentUser defined module imports:
Used defined imports for the helper module (e.g.
helper/validation) with or without an__init__.pyare supported, because entire user-defined helper folders are copied to thegenerated_workspace, which would resolve the imports correctly in the exported script (e.g.experiment.py)from helper.validation import CrowdGuardClientValidationandfrom helper import CrowdGuardClientValidationIf an
__init__.pyfile is present inside the same directory (for e.g.workspace) as the Jupyter Notebook along with user-defined modulevalidation.py. For e.g.from validation import CrowdGuardClientValidation) remains valid in the exported script (experiment.py), asvalidation.pyis copied to thegenerated_workspace.Beta Was this translation helpful? Give feedback.
All reactions