Kafka-ML

repository·master·Indexed 42 days ago

https://github.com/ertis-research/kafka-ml

A framework for managing TensorFlow/Keras and PyTorch machine learning pipelines on Kubernetes, designed to connect ML models to real-time data streams via Apache Kafka. It includes a Django 3.0.7 back-end for model management and various data source clients supporting Avro and RAW formats for training and inference workflows.

Tokens
23.9K
Snippets
79
Records
113
Agent score
52%

What's inside Kafka-ML

  1. Overview of Kafka-ML

    master

    Kafka-ML is a framework designed to manage machine learning (ML) pipelines on Kubernetes, specifically for TensorFlow/Keras and PyTorch (Ignite) models. It enables the design, training, and inference of ML models by connecting them to data streams via Apache Kafka. This allows for direct integration with real-time data sources like IoT streams.

    Key features include:

    • Web UI: Allows users to define ML models without needing external libraries or manual execution.
    • Stream Integration: Uses Apache Kafka to feed training and inference datasets.
    • Pipeline Management: Supports the full lifecycle of ML: design, training, and inference.
  2. Available Kafka data source clients

    master

    Kafka-ML provides several client implementations in the datasources/ directory for streaming data to Apache Kafka. These clients support both training and inference workflows using Avro or RAW formats.

    Training Data Clients

    • avro_sink.py: Sends training data using the Avro format. Requirement: You must provide definitions for two Avro schemas: one for the label and one for the data.
    • raw_sink.py: Sends training data using the RAW format.
    • federated_raw_sink.py: Sends training data using the RAW format specifically to topics prepared for federated learning.

    Inference Data Clients

    • avro_inference.py: Sends inference data using the Avro format. Requirement: You must provide an Avro schema definition for the data.

    Base Implementation

    • sink.py: The base file that provides the underlying logic used by both the Avro and RAW sink clients.
  3. Overview of Federated Backend core files

    master

    The Federated Backend is built with Django 3.2.13 and Python 3.5-3.8. Key files include:

    • automl/models.py: Defines database tables and attributes.
    • automl/views.py: Implements the RESTful API via Views (handling HTTP methods like GET, POST).
    • automl/serializers.py: Handles encoding/decoding models to/from JSON.
    • automl/urls.py: Maps RESTful Views to URL paths.
    • autoweb/settings.py: The main configuration file.
  4. Overview of Back-end core files

    master

    The Back-end is implemented using Django 3.0.7. Key files include:

    • automl/models.py: Defines database tables and attributes.
    • automl/views.py: Implements the RESTful API via Views (handling HTTP methods like GET, POST).
    • automl/serializers.py: Handles encoding/decoding models to and from JSON.
    • automl/urls.py: Maps RESTful Views to URL paths.
    • autoweb/settings.py: The main configuration file for the project.
  5. How TensorFlow model inference works in Kafka-ML

    master

    The TensorFlow inference module is designed to run as a Kubernetes Job within Kafka-ML. Once deployed, the module performs a continuous loop: it waits for data from a configured Kafka INPUT_TOPIC, processes it using a model downloaded via MODEL_URL, and sends the resulting predictions to a configured OUTPUT_TOPIC.

    Key components of the module include:

    • inference.py: The main entry point executed during the inference Job.
    • decoders.py: Handles data stream decoding for formats including RAW, Avro, JSON, and TELEGRAF_STR_JSON.
    • config.py: Manages debug configurations.
    • utils.py: Contains shared utility functions.
  6. How PyTorch model training works in Kafka-ML

    master

    The PyTorch training module is a task executed within a Kubernetes Job. It operates using a control-message-driven lifecycle:

    1. Wait for Control: The task waits for a control message on the configured Kafka CONTROL_TOPIC that matches the local DEPLOYMENT_ID.
    2. Data & Model Acquisition: Once a match is found, the task identifies the corresponding data stream and downloads the untrained PyTorch model from the Back-end using the RESULT_URL.
    3. Training: The model is trained using parameters provided via environment variables (BATCH, KWARGS_FIT, etc.).
    4. Result Submission: After training, the trained model and training/validation results are sent back to the Back-end via a POST request to the RESULT_URL.

    Key files in this module:

    • training.py: The main entry point executed by the training Job.
    • config.py: Configuration for debugging.
    • utils.py: Common utility functions.
  7. How the TensorFlow training module works

    master

    The TensorFlow training module is a task designed to run within a Kubernetes-orchestrated TensorFlow training Job in Kafka-ML.

    Lifecycle:

    1. Wait: The task starts and waits for a control message on the configured Kafka CONTROL_TOPIC that matches the DEPLOYMENT_ID.
    2. Download: Once a matching control message and data stream are identified, the task downloads the untrained TensorFlow model from the Back-end using the RESULT_URL.
    3. Train: The model is trained using the data stream and parameters provided via environment variables.
    4. Upload: The trained model, along with training (and optionally validation) results, is sent back to the Back-end via a POST request to the RESULT_URL.

    Core Files:

    • training.py: The main entry point executed during the training Job.
    • decoders.py: Contains decoders for RAW, Avro, JSON, and TELEGRAF_STR_JSON data streams.
    • config.py: Configuration for debugging.
    • utils.py: Shared utility functions.
  8. How PyTorch model inference works in Kafka-ML

    master

    The PyTorch model inference module is designed to run as a Kubernetes Job. Once deployed, it performs a continuous loop: it waits for data arriving on a configured Kafka input topic, processes it using a PyTorch model, and sends the resulting predictions to a configured Kafka output topic.

    Key components of the module include:

    • inference.py: The main entry point executed during the inference Job.
    • config.py: Used for debug configuration.
    • utils.py: Contains common utility functions.
  9. Concept: Federated Learning in Kafka-ML

    master

    The Kafka-ML Federated Module implements federated learning, a privacy-preserving approach where model training occurs on decentralized devices. Instead of transferring raw sensitive data to a central server, local devices perform training and only share model updates or gradients. These updates are aggregated by a central server to improve a global model, which is then redistributed to devices.

    Current Support: Currently, federated learning support is limited to TensorFlow/Keras models. The usage pattern is similar to single-model deployment, but the configuration deployment form includes additional fields specific to federated orchestration.

  10. How the Federated TensorFlow training task works

    master

    The federated training module is designed to run as a TensorFlow training Job within a Kubernetes environment.

    Lifecycle:

    1. Wait for Configuration: The task waits until it receives a valid message containing the model architecture.
    2. Model Download: Once configured, it downloads the corresponding TensorFlow model from Kafka-ML Cloud.
    3. Training: It consumes the specified data stream and trains the downloaded model.
    4. Results Upload: After training, the module sends the trained model, training results, and optionally validation results back to the backend via Apache Kafka in the cloud.

    Core Components:

    • federated_training.py: The main entry point executed during the training Job.
    • KafkaModelEngine.py: Handles reading and loading TensorFlow models from Apache Kafka in the cloud.
    • FederatedKafkaMLAggregationSink.py: Handles sending trained models and metrics to Apache Kafka in the cloud.
    • decoders.py: Provides decoders for various data formats: RAW, Avro, JSON, and TELEGRAF_STR_JSON.