The AI Inference Server executes every component of a pipeline in an isolated Python virtual environment. For each component, you must specify which Python packages are required by the Python scripts in that component, including the Python packages required to load persistent Python objects.
Adding Python dependencies
The Python dependencies of a component can be added in several ways:
As a standard wheel file or a zip/tar.gz file that contains standard wheel filesIn both cases, the packages, which can be precompiled wheel files or pure Python source distributions, are added to the
component.dependenciesdictionary and binaries are zipped into the configuration package. AI Inference server supports only installing source distributions that contain only Python source code.
component.add_python_packages('../packages/my_module-0.0.1-py3-anyany.whl')
component.add_python_packages('../packages/MyPackages.zip')
component.add_python_packages('../packages/my_source_module-0.0.2.tar.gz')Using a standard
requirements.txtThis way, thecomponent.dependenciesdictionary contains only the dependencies defined in the file. The required wheel files are downloaded while converting the pipeline configuration package to an edge configuration package usingpip.
component.set_requirements('./requirements.txt')By name using a list that contains the names of the Python modulesIn this case, the method searches for the module in the current Python environment and adds the package with its version and all of its transitive dependencies.
component.add_dependencies(['numpy', 'scikit-learn'])Dependencies can be added by name and version using a list that contains corresponding tuples. When the component is saved, it will perform a check if all specified dependencies can be installed together. Transitive dependencies will also be downloaded.
component.add_dependencies([('pandas', '1.3.0'), ('pyarrow', '3.0.0')])Dependencies added to a component will be installed on the AI Inference Server with the defined version and can be imported into your Python code during execution.
entrypoint.py or data_processor.py
import numpy as np
import pandas as pd
[...]Download from non-public repository
Dependencies from non-public repositories can be downloaded by specifying an extra index URL at the beginning of the requirements.txt file.
--extra-index-url https://<API_KEY>@your/private/repository
simaticaiPlease be aware that if a package is also available in a public repository, pip may download it from there and not look for it in the private repository, which may pose a cybersecurity risk.
It is recommended to only use trusted private repositories, pin the version of the package, and check if a package with the same properties already exists on https://pypi.org/simple.
If you want to change the default https://pypi.org/simple package index, you can do it by using an index URL at the beginning of the requirements.txt file. This allows you to download dependencies exclusively from a private repository.
--index-url https://<API_KEY>@your/private/repository
simaticaiDependency optimization
Some frameworkds depend on multiple python packages which are useful for training, testing and debugging the AI models, but are not required during the model execution in AI Inference Server. These transitive dependencies can significantly increase the pipeline package size. Considering AI Inference server's 2.2GB size package size limitation, it is advised to include only the necessary dependencies for inferencing into the pipeline package. AI SDK can detect some of these dependencies and replace them automatically.
The pipeline can only use the GPU in the Edge Device when the pipeline component is a GPURuntimeComponent, which can only run ONNX models and can not include python dependencies. On the other hand PythonComponent can include many dependencies, but even if some GPU specific libraries are included, the model will be executed on the CPU regardless. One such dependency is PyTorch, which has GPU specific dependencies but has a CPU only version too which is smaller. AI SDK can detect if PyTorch is a direct dependency of the pipeline package, and automatically replaces it with the CPU only version. However if it is deep inside the dependency tree, AI SDK can't detect it. It is advised to manually specify the CPU only PyTorch if it is needed for the package execution.
The AI SDK automatically adjusts the repository URLs as it is needed to download the CPU version of PyTorch. Any repositories specified in the requirements.txt file with the --index-url flag will be moved to the --extra-index-url list, and https://download.pytorch.org/whl/cpu will be added as the primary repository. This behavior can be disabled on the Pipeline object with the disable_dependency_optimization() method. It can be enabled with the enable_dependency_optimization() method, or re-creating the Pipeline object since it's the default setting.
The AI SDK will print a warning message if the repository URL modification was necessary during packaging.
If the dependency optimization is disabled, AI SDK will preserve any --index-url or --extra-index-url in the requirements.txt, and it prints a warning message about the possibility of an oversized package.