### Install Ludwig and Optuna Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/optuna_executor.ipynb Installs the necessary libraries for Ludwig and Optuna. Use this at the beginning of your environment setup. ```bash !pip install ludwig optuna --quiet ``` -------------------------------- ### Start KServe Local Server and Predict Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serving/kserve/README.md Installs necessary packages, starts a local KServe server for a Ludwig model, and demonstrates how to send predictions using the v2 protocol. ```bash pip install "ludwig[serve]" kserve # Start the server python -m ludwig.serve_kserve \ --model_name titanic \ --model_path ./results/experiment_run/model \ --http_port 8080 # Predict with v2 protocol curl -s -X POST http://localhost:8080/v2/models/titanic/infer \ -H "Content-Type: application/json" \ -d '{ "inputs": [ {"name": "Pclass", "shape": [2], "datatype": "INT64", "data": [1, 3]}, {"name": "Sex", "shape": [2], "datatype": "BYTES", "data": ["female", "male"]}, {"name": "Age", "shape": [2], "datatype": "FP32", "data": [28.0, 22.0]} ] }' ``` -------------------------------- ### Install Ludwig and Torchvision Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/open_set_recognition/open_set_mnist.ipynb Installs the necessary libraries for running the example. Use --quiet to suppress output. ```python !pip install ludwig torchvision --quiet ``` -------------------------------- ### Install and Install Pre-commit Hooks Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Install the pre-commit package and set up the pre-commit hooks for automatic code formatting. ```bash pip install pre-commit pre-commit install ``` -------------------------------- ### Install All Dependencies with Shell Quoting Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Install all optional dependencies, using quotes around bracketed dependencies for shells like Zsh. ```bash pip install -e ."[full]" ``` -------------------------------- ### Install Dependencies Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/alignment/README.md Install Ludwig with LLM support and the datasets library. This is a prerequisite for running the alignment examples. ```bash pip install "ludwig[llm]" datasets ``` -------------------------------- ### Install Ludwig and Scikit-learn Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/optimizers/optimizer_comparison.ipynb Installs the necessary libraries for running Ludwig and data manipulation. Use '--quiet' to suppress verbose output during installation. ```python !pip install ludwig scikit-learn --quiet ``` -------------------------------- ### Install BoTorch for GP Sampler Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/optuna_executor.ipynb Command to install the optional dependency 'botorch' required for using the 'gp' (Bayesian optimisation) sampler with the Optuna executor. ```bash pip install botorch ``` -------------------------------- ### Install All Dependencies in Developer Mode Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Install all optional dependencies, including those for full codebase access, in editable mode. ```bash pip install -e .[full] ``` -------------------------------- ### Benchmarking Configuration Example Source: https://github.com/ludwig-ai/ludwig/blob/main/ludwig/benchmarking/README.md A sample YAML configuration defining experiment parameters, export paths, and dataset-specific settings. ```yaml experiment_name: SMOTE_test hyperopt: false export: export_artifacts: true export_base_path: s3://benchmarking.us-west-2.ludwig.com/bench/ # include the slash at the end. experiments: - dataset_name: ames_housing config_path: /home/ray/configs/ames_housing_SMOTE.yaml experiment_name: SMOTE_test_with_hyperopt hyperopt: true - dataset_name: protein - ... ... - dataset_name: mercedes_benz_greener config_path: /home/ray/configs/mercedes_benz_greener_SMOTE.yaml ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/mnist/visualize_model_test_results.ipynb Imports necessary libraries and sets up the environment by ignoring warnings and removing previous visualization directories. ```python import warnings warnings.simplefilter("ignore") import os import os.path import shutil import pandas as pd from ludwig.api import LudwigModel from ludwig.datasets import mnist from ludwig.utils.data_utils import load_json from ludwig.visualize import compare_classifiers_performance_from_pred, compare_performance, confusion_matrix shutil.rmtree("./viz2", ignore_errors=True) ``` -------------------------------- ### Install Ludwig with vLLM backend Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serve/vllm_serving.ipynb Installs the Ludwig library with support for the vLLM backend. Use this command before launching the server. ```bash !pip install "ludwig[llm,vllm]" --quiet ``` -------------------------------- ### Install Dependencies Source: https://github.com/ludwig-ai/ludwig/blob/main/notebooks/advanced_peft_adapters.ipynb Installs necessary libraries for Ludwig and PEFT. Ensure you have `peft` version 0.19.0 or higher and `torch` installed. ```python # Install dependencies # !pip install ludwig peft>=0.19.0 transformers torch ``` -------------------------------- ### Install Ray and Ludwig dependencies Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/ray/job_submission/README.md Install necessary packages on the local machine and the Ray cluster. ```bash pip install "ray[default]" ``` ```bash pip install "ludwig[distributed]" ``` -------------------------------- ### Ludwig Installation Source: https://github.com/ludwig-ai/ludwig/blob/main/README.md These commands show how to install the Ludwig library. Use 'ludwig[full]' for all dependencies, 'ludwig[llm]' for LLM fine-tuning specific dependencies, or 'ludwig' for the core library. ```bash pip install ludwig # core pip install ludwig[full] # all optional dependencies pip install ludwig[llm] # LLM fine-tuning only ``` -------------------------------- ### Install Prerequisites on MacOS with Apple Silicon Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Install necessary prerequisites like cmake and libomp on MacOS with Apple Silicon using homebrew. ```bash brew install cmake libomp ``` -------------------------------- ### Start Ludwig Model Server Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serve/README.md Launches the model server using a pre-trained model path. Requires the current working directory to be set to examples/serve. ```bash ludwig serve --model_path ../titanic/results/simple_experiment_simple_model/model ``` -------------------------------- ### Install Ludwig with Ray Serve Dependencies Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serving/ray_serve/README.md Install Ludwig with the 'distributed' extra to pull in Ray and its serving components. ```bash pip install "ludwig[distributed]" # pulls in ray[serve] ``` -------------------------------- ### Setup and Data Preparation Imports Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/open_set_recognition/open_set_mnist.ipynb Imports required libraries and defines constants for data directories, known/unknown classes, and sample limits. This setup is crucial for organizing and processing the MNIST dataset. ```python import csv from pathlib import Path import torch from PIL import Image from torchvision import datasets, transforms # ── configuration ────────────────────────────────────────────────────────────── DATA_DIR = Path("mnist_data") # raw MNIST download IMG_DIR = Path("mnist_images") # saved PNG files KNOWN_CLASSES = list(range(8)) # digits 0-7 UNKNOWN_CLASSES = [8, 9] # background / unknown # Limit samples per class so training is fast on CPU MAX_TRAIN_KNOWN = 500 # per known class MAX_TRAIN_UNKNOWN = 500 # per unknown class (background) MAX_TEST_KNOWN = 200 # per known class MAX_TEST_UNKNOWN = 200 # per unknown class IMG_DIR.mkdir(parents=True, exist_ok=True) ``` -------------------------------- ### Print Configuration Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/model_hyperopt_example.ipynb Displays the current configuration dictionary. Useful for debugging and verifying the setup before proceeding with training or optimization. ```python # View the config print("config:") config ``` -------------------------------- ### Install Ludwig Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/anomaly_detection/anomaly_detection.ipynb Installs the Ludwig library quietly. Run this before using Ludwig. ```python !pip install ludwig --quiet ``` -------------------------------- ### Install entmax Source: https://github.com/ludwig-ai/ludwig/blob/main/ludwig/utils/entmax/README.md Command to install the package via pip. ```bash pip install entmax ``` -------------------------------- ### Install Dependencies Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/llm_config_generation/llm_config_generation.ipynb Installs Ludwig and necessary backend libraries (Anthropic or OpenAI). Run the appropriate command based on your chosen LLM provider. ```bash # Install dependencies # For the Anthropic backend (Claude models): !pip install "ludwig>=0.14" anthropic --quiet # For the OpenAI backend (GPT models), also run: # !pip install openai --quiet ``` -------------------------------- ### Install Ludwig with Hyperopt and Run Optuna Executor Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/README_optuna.md Installs the necessary Ludwig package with hyperopt support, which includes Optuna, and then executes the hyperparameter optimization script. ```bash pip install 'ludwig[hyperopt]' # pulls in optuna python optuna_executor.py ``` -------------------------------- ### Launch vLLM server from command line Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serve/vllm_serving.ipynb Starts the Ludwig serve command using the vLLM backend for high-throughput inference. Replace '/path/to/model' with your model's directory. ```bash ludwig serve \ --model_path=/path/to/model \ --backend vllm \ --num_gpus 1 ``` -------------------------------- ### Install Ludwig with Nash-MTL Support Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/multi_task/multi_task.ipynb Install Ludwig from the 'future-capabilities' branch to enable Nash-MTL. This is a prerequisite for using the Nash-MTL balancing strategy. ```bash pip install git+https://github.com/ludwig-ai/ludwig@future-capabilities ``` -------------------------------- ### Install Test Dependencies with Shell Quoting Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Install test dependencies, using quotes around bracketed dependencies for shells that require it. ```bash pip install -e ."[test]" ``` -------------------------------- ### Example Output of Optuna Hyperparameter Optimization Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/README_optuna.md Illustrative output showing the best trial found by the Optuna hyperparameter optimization process, including its value, parameters, and completion time. ```text [Optuna] Best trial: value: 0.6184 params: trainer.learning_rate: 0.0032 trainer.batch_size: 64 combiner.num_fc_layers: 2 combiner.output_size: 128 completed in: 412.8s ``` -------------------------------- ### Install Ludwig with LLM support Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/alignment/alignment_dpo.ipynb Installs the Ludwig library with essential dependencies for Large Language Models (LLMs) and the datasets library. Use this command to set up your environment for LLM training and alignment tasks. ```bash !pip install "ludwig[llm]" datasets --quiet ``` -------------------------------- ### Manage Ray cluster lifecycle Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/ray/kubernetes/README.md Commands to start, stop, and interact with the Ray cluster. ```bash ./utils/ray_up.sh $CLUSTER_NAME ``` ```bash ./utils/submit.sh $CLUSTER_NAME scripts/train.py ``` ```bash ./utils/attach.sh $CLUSTER_NAME ``` ```bash ./utils/dashboard.sh $CLUSTER_NAME ``` ```bash ./utils/rsync_up.sh $CLUSTER_NAME ~/repos/ludwig ``` ```bash ./utils/ray_down.sh $CLUSTER_NAME ``` -------------------------------- ### Install Ludwig with LLM support Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/llm_structured_output/structured_output.ipynb Installs the Ludwig library with the necessary dependencies for large language models. Use this to set up your environment for LLM tasks. ```python !pip install "ludwig[llm]" --quiet ``` -------------------------------- ### Manage Docker Compose Stack Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serve/prometheus_monitoring/README.md Commands to start, verify, and stop the monitoring stack services. ```bash docker compose up -d ``` ```bash docker compose ps ``` ```bash docker compose down ``` ```bash docker compose down -v ``` -------------------------------- ### Configure Optuna Executor with TPE Sampler Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/optuna_executor.ipynb Example of configuring the Optuna executor to use the 'tpe' sampler. This sampler is efficient for fewer than 100 trials and is the classic Optuna default. ```yaml "executor": { "type": "optuna", "num_samples": 50, "sampler": "tpe" # <-- change this } ``` -------------------------------- ### Configure Muon Optimizer Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/optimizers/optimizer_comparison.ipynb Example of configuring the Muon optimizer and a cosine learning rate scheduler in a Ludwig trainer configuration. ```yaml trainer: epochs: 30 optimizer: type: muon lr: 0.001 learning_rate_scheduler: type: cosine ``` -------------------------------- ### LoftQ LoRA Initialization Configuration Source: https://github.com/ludwig-ai/ludwig/blob/main/notebooks/advanced_peft_adapters.ipynb Configures and prints settings for the LoftQ initializer, which jointly quantizes base weights and initializes LoRA. This example shows how to set `loftq_bits` and `loftq_iter`. ```python from ludwig.schema.llms.peft import LoraConfig # LoftQ: jointly quantizes base weights and initializes LoRA to minimize error loftq_cfg = LoraConfig.model_validate( {"type": "lora", "r": 16, "init_lora_weights": "loftq", "loftq_config": {"loftq_bits": 4, "loftq_iter": 1}} ) peft_cfg = loftq_cfg.to_config("CAUSAL_LM") print("LoftQ init:", peft_cfg.init_lora_weights) print("LoftQ bits:", peft_cfg.loftq_config["loftq_bits"]) ``` -------------------------------- ### Configure Schedule-Free AdamW Optimizer Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/optimizers/optimizer_comparison.ipynb Example of configuring the Schedule-Free AdamW optimizer without an external learning rate scheduler. A warmup_steps can still be configured within the optimizer block. ```yaml trainer: optimizer: type: schedule_free_adamw lr: 0.001 ``` -------------------------------- ### Configure Optuna Executor with Hyperband Pruner Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/optuna_executor.ipynb Example of configuring the Optuna executor to use the 'hyperband' pruner. This pruner is recommended for neural network HPO and stops unpromising trials early. ```yaml "executor": { "type": "optuna", "num_samples": 50, "sampler": "auto", "pruner": "hyperband" # <-- add this } ``` -------------------------------- ### Run Sample Client Program Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serve/README.md Executes the client script to interact with the running model server. Ensure the server is active before running this command. ```bash python client_program.py ``` -------------------------------- ### Install Ludwig with Vision Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/semantic_segmentation/semantic_segmentation.ipynb Installs the Ludwig library with vision dependencies. Use the --quiet flag to suppress installation output. ```bash !pip install 'ludwig[vision]' --quiet ``` -------------------------------- ### Install Ludwig Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/anomaly_detection/README.md Install the Ludwig package via pip. ```bash pip install ludwig ``` -------------------------------- ### Clone Repository and Set Up Remotes Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Clone your forked repository and add the base repository as a remote for fetching updates. ```bash git clone git@github.com:/ludwig.git cd ludwig git remote add upstream https://github.com/ludwig-ai/ludwig.git ``` -------------------------------- ### Install Ludwig Vision Dependencies Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/semantic_segmentation/README.md Install the necessary dependencies for vision tasks. ```bash pip install 'ludwig[vision]' ``` -------------------------------- ### Run basic tests Source: https://github.com/ludwig-ai/ludwig/blob/main/tests/README.md Execute the standard test suite from the repository root. ```bash pytest -vs tests ``` -------------------------------- ### Install Ludwig LLM dependencies Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/llm_structured_output/README.md Install the necessary dependencies to use LLM features in Ludwig. ```bash pip install "ludwig[llm]" ``` -------------------------------- ### Install Ludwig Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hypernetwork/hypernetwork.ipynb Installs Ludwig version 0.14 or later, which is required for the HyperNetworkCombiner. Use this command in your environment. ```bash !pip install "ludwig>=0.14" --quiet ``` -------------------------------- ### Install Test Dependencies in Developer Mode Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Install dependencies required for running tests in editable mode. ```bash pip install -e .[test] ``` -------------------------------- ### Define Base Configuration and Training Parameters Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/optimizers/optimizer_comparison.ipynb Sets up input/output feature definitions and a base trainer configuration with 30 epochs. This configuration is reused across different optimizer benchmarks. ```python FEATURE_NAMES = [ "fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "free_sulfur_dioxide", "total_sulfur_dioxide", "density", "pH", "sulphates", "alcohol", ] INPUT_FEATURES = [{"name": n, "type": "number"} for n in FEATURE_NAMES] OUTPUT_FEATURES = [{"name": "quality", "type": "binary"}] BASE_TRAINER = { "epochs": 30, } # Storage for training curves and summary metrics all_results = {} summary = [] ``` -------------------------------- ### Install Core Dependencies in Developer Mode Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Install the core Ludwig dependencies in editable mode for development. ```bash pip install -e . ``` -------------------------------- ### Standard and PiSSA LoRA Initialization Source: https://github.com/ludwig-ai/ludwig/blob/main/notebooks/advanced_peft_adapters.ipynb Compares the default LoRA weight initialization with the PiSSA initializer. PiSSA initializes LoRA matrices from the SVD of pretrained weights, potentially leading to faster convergence. ```python from ludwig.schema.llms.peft import adapter_registry, LoraConfig # Standard LoRA (baseline) standard = LoraConfig(r=8, alpha=16) print("Standard LoRA init_lora_weights:", standard.to_config("CAUSAL_LM").init_lora_weights) # PiSSA: Principal Singular values and Singular vectors Adaptation # Initializes A, B from SVD of the pretrained weight. Faster convergence. pissa = LoraConfig(r=8, alpha=16, init_lora_weights="pissa") print("PiSSA init_lora_weights:", pissa.to_config("CAUSAL_LM").init_lora_weights) ``` -------------------------------- ### Compare Softmax, Sparsemax, and 1.5-Entmax Source: https://github.com/ludwig-ai/ludwig/blob/main/ludwig/utils/entmax/README.md Demonstrates basic usage of sparsemax and entmax15 compared to standard softmax. ```python import torch from torch.nn.functional import softmax from entmax import sparsemax, entmax15 x = torch.tensor([-2, 0, 0.5]) print(softmax(x, dim=0)) # tensor([0.0486, 0.3592, 0.5922]) print(sparsemax(x, dim=0)) # tensor([0.0000, 0.2500, 0.7500]) print(entmax15(x, dim=0)) # tensor([0.0000, 0.3260, 0.6740]) ``` -------------------------------- ### Train with TinyLoRA for Ultra-Low Memory Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/peft_advanced/README.md This command enables training with TinyLoRA, suitable for environments with ultra-low memory constraints. Requires Ludwig CLI and a dataset. ```bash ludwig train --config tinylora_llm.yaml --dataset ludwig://imdb ``` -------------------------------- ### Install Ludwig with GPU Support Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/uncertainty/README.md Install Ludwig with GPU support if you plan to train models on a GPU. This is an optional step. ```bash pip install ludwig[gpu] ``` -------------------------------- ### Run Benchmarks via CLI Source: https://github.com/ludwig-ai/ludwig/blob/main/ludwig/benchmarking/README.md Execute benchmarking experiments using the command line interface with a specified configuration file. ```bash ludwig benchmark --benchmarking_config path/to/benchmarking/config.yaml ``` -------------------------------- ### Install Ludwig with LLM support Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/vlm_finetuning/README.md Installs Ludwig with necessary dependencies for Large Language Models, including transformers, PEFT, and bitsandbytes. ```bash pip install "ludwig[llm]" # transformers, peft, bitsandbytes ``` -------------------------------- ### Run Optimizer Comparison Script Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/optimizers/README.md Execute the Python script to download the dataset, train models with different optimizers, and print a comparison table of final metrics and training time. ```bash python optimizer_comparison.py ``` -------------------------------- ### Run Resumable Hyperopt with SQLite Storage Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/optuna_executor.ipynb Demonstrates how to run a hyperparameter optimization with SQLite storage enabled. If the run is interrupted, it can be resumed from the last saved state. ```python # Example: run with SQLite storage for resumability import copy config_resumable = copy.deepcopy(config) config_resumable["hyperopt"]["executor"]["storage"] = "sqlite:///optuna_results.db" config_resumable["hyperopt"]["executor"]["num_samples"] = 10 # fewer trials for demo print("Executor config:") print(json.dumps(config_resumable["hyperopt"]["executor"], indent=2)) print("\nRe-running with storage enabled — existing trials will be reused.") model2 = LudwigModel(config=config_resumable, logging_level=20) results2, _, _ = model2.hyperopt( dataset=str(combined_path), output_directory="hyperopt_output_resumable", ) print(f"Done. {len(results2)} trials.") ``` -------------------------------- ### Setup API Key and Model Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/llm_config_generation/llm_config_generation.ipynb Sets up the API key for LLM access, either from Google Colab Secrets or environment variables. Also specifies the LLM model to be used for configuration generation. ```python import os # --- Colab: read from Colab Secrets --- try: from google.colab import userdata os.environ["ANTHROPIC_API_KEY"] = userdata.get("ANTHROPIC_API_KEY") print("Loaded ANTHROPIC_API_KEY from Colab Secrets.") except Exception: # Outside Colab: make sure the variable is already set in the environment, # or uncomment and fill in the line below: # os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..." if os.environ.get("ANTHROPIC_API_KEY"): print("ANTHROPIC_API_KEY found in environment.") else: print("WARNING: ANTHROPIC_API_KEY not set. Set it before running generation cells.") # Choose your model: CLAUDE_MODEL = "claude-sonnet-4-20250514" # Anthropic # GPT_MODEL = "gpt-4o" # OpenAI (set OPENAI_API_KEY instead) ``` -------------------------------- ### CorDA LoRA Initialization Source: https://github.com/ludwig-ai/ludwig/blob/main/notebooks/advanced_peft_adapters.ipynb Demonstrates the CorDA (Context-Oriented Decomposition Adaptation) initializer for LoRA weights. This method is another advanced initialization strategy. ```python from ludwig.schema.llms.peft import LoraConfig # CorDA: Context-Oriented Decomposition Adaptation corda = LoraConfig(r=8, alpha=16, init_lora_weights="corda") print("CorDA init_lora_weights:", corda.to_config("CAUSAL_LM").init_lora_weights) ``` -------------------------------- ### Install Development Branch for Optuna Executor Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hyperopt/optuna_executor.ipynb Installs the development branch of Ludwig, which is required for the native Optuna executor if you are using a version prior to 0.14. ```bash pip install git+https://github.com/ludwig-ai/ludwig.git@data-pipeline-hyperopt-modernization ``` -------------------------------- ### Install Ludwig and Train Hypernetwork Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/hypernetwork/README.md Installs the Ludwig library and runs a standalone script to train both a hypernetwork and a baseline concat model, printing a comparison. ```bash pip install "ludwig>=0.14" python train_hypernetwork.py ``` -------------------------------- ### Full Ludwig Training with PEFT Adapter Configuration Source: https://github.com/ludwig-ai/ludwig/blob/main/notebooks/advanced_peft_adapters.ipynb Provides a complete Ludwig configuration for training an LLM using a selected PEFT adapter. It includes model type, base model, input/output features, adapter configuration, trainer settings, and preprocessing options. This example shows how to integrate various adapters like LoRA, TinyLoRA, LN-Tuning, and OFT into a training pipeline. ```python import yaml # Choose your adapter: adapter_configs = { "pissa": {"type": "lora", "r": 16, "alpha": 32, "init_lora_weights": "pissa"}, "tinylora": {"type": "tinylora", "r": 2, "u": 64}, "ln_tuning": {"type": "ln_tuning"}, "oft": {"type": "oft", "oft_block_size": 32}, } selected = "pissa" config = { "model_type": "llm", "base_model": "meta-llama/Llama-3.2-1B", "input_features": [{"name": "instruction", "type": "text"}], "output_features": [{"name": "output", "type": "text"}], "adapter": adapter_configs[selected], "trainer": { "type": "finetune", "learning_rate": 1e-4, "epochs": 3, "batch_size": 4, }, "preprocessing": {"global_max_sequence_length": 512}, } print(f"Training with {selected} adapter:") print(yaml.dump(config, default_flow_style=False)) ``` ```python # Uncomment to run training: # from ludwig.api import LudwigModel # model = LudwigModel(config=config) # train_stats, _, output_dir = model.train(dataset="path/to/dataset.csv") # print("Results saved to:", output_dir) ``` -------------------------------- ### Train with PiSSA LoRA Initialization Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/peft_advanced/README.md Use this command to train a model with PiSSA initialization, which is recommended for most tasks due to faster convergence. Requires Ludwig CLI and a dataset. ```bash ludwig train --config pissa_lora.yaml --dataset ludwig://imdb ``` -------------------------------- ### Install Ludwig with Vision Support Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/image_encoders/README.md Install Ludwig with the necessary dependencies for vision tasks. This command ensures all required libraries for image processing and model training are included. ```bash pip install ludwig[vision] ``` -------------------------------- ### Adafactor Training Example Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/optimizers/optimizer_comparison.ipynb Trains a Ludwig model using the Adafactor optimizer. This example showcases the model training process with Adafactor, emphasizing its internal step-size management. ```python config_adafactor = { "model_type": "ecd", "input_features": INPUT_FEATURES, "output_features": OUTPUT_FEATURES, "trainer": { **BASE_TRAINER, "optimizer": {"type": "adafactor", "lr": 0.001}, # No learning_rate_scheduler — Adafactor handles step-size internally }, } with tempfile.TemporaryDirectory() as tmpdir: model = LudwigModel(config_adafactor, logging_level=30) t0 = time.time() train_stats, _, _ = model.train( dataset=df, output_directory=tmpdir, skip_save_model=True, skip_save_progress=True, skip_save_log=True, ) elapsed_adafactor = time.time() - t0 all_results["adafactor"] = train_stats.validation summary.append( { "optimizer": "adafactor", "final_val_loss": train_stats.validation["quality"]["loss"][-1], "final_val_accuracy": train_stats.validation["quality"]["accuracy"][-1], "training_time_s": round(elapsed_adafactor, 1), } ) print(f"Adafactor done in {elapsed_adafactor:.1f}s") ``` -------------------------------- ### Launch vLLM server with OpenAI-compatible API enabled Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serve/vllm_serving.ipynb Starts the Ludwig vLLM server with the `--enable-openai-api` flag, making it compatible with OpenAI client libraries. This allows using tools designed for OpenAI's API. ```bash ludwig serve --model_path=/path/to/model --backend vllm --num_gpus 1 --enable-openai-api ``` -------------------------------- ### Launch vLLM Server Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serve/vllm_serving.ipynb Starts the Ludwig serve command with the vLLM backend. This replaces the default FastAPI/HuggingFace inference loop with vLLM's continuous-batching engine. Key flags include `--backend vllm`, `--num_gpus N`, `--host`, and `--port`. ```APIDOC ## Launch vLLM Server Starts the Ludwig serve command with the vLLM backend. ### Command ```bash ludwig serve \ --model_path=/path/to/model \ --backend vllm \ --num_gpus 1 ``` ### Key Flags | Flag | Default | Description | |---|---|---| | `--backend vllm` | `local` | Use vLLM inference engine | | `--num_gpus N` | `1` | Number of GPUs (enables tensor parallelism when N > 1) | | `--host` | `0.0.0.0` | Bind address | | `--port` | `8000` | Port | The server starts on `http://0.0.0.0:8000` by default. You can change the host and port with `--host` and `--port`. ``` -------------------------------- ### RAdam Training Example Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/optimizers/optimizer_comparison.ipynb Trains a Ludwig model using the RAdam optimizer and a cosine learning rate scheduler. This example demonstrates the model training process and records the training time. ```python config_radam = { "model_type": "ecd", "input_features": INPUT_FEATURES, "output_features": OUTPUT_FEATURES, "trainer": { **BASE_TRAINER, "optimizer": {"type": "radam", "lr": 0.001}, "learning_rate_scheduler": {"type": "cosine"}, }, } with tempfile.TemporaryDirectory() as tmpdir: model = LudwigModel(config_radam, logging_level=30) t0 = time.time() train_stats, _, _ = model.train( dataset=df, output_directory=tmpdir, skip_save_model=True, skip_save_progress=True, skip_save_log=True, ) elapsed_radam = time.time() - t0 all_results["radam"] = train_stats.validation summary.append( { "optimizer": "radam", "final_val_loss": train_stats.validation["quality"]["loss"][-1], "final_val_accuracy": train_stats.validation["quality"]["accuracy"][-1], "training_time_s": round(elapsed_radam, 1), } ) print(f"RAdam done in {elapsed_radam:.1f}s") ``` -------------------------------- ### Run Full Unit Test Suite Source: https://github.com/ludwig-ai/ludwig/blob/main/CONTRIBUTING.md Execute all unit tests for individual modules. These tests are fast and do not require GPU resources. ```bash pytest tests/ludwig/ ``` -------------------------------- ### Schedule-Free AdamW Training Example Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/optimizers/optimizer_comparison.ipynb Trains a Ludwig model using the Schedule-Free AdamW optimizer. This example demonstrates the model training process and highlights the absence of an external learning rate scheduler. ```python config_sfa = { "model_type": "ecd", "input_features": INPUT_FEATURES, "output_features": OUTPUT_FEATURES, "trainer": { **BASE_TRAINER, "optimizer": {"type": "schedule_free_adamw", "lr": 0.001}, # No learning_rate_scheduler — Schedule-Free AdamW handles it internally }, } with tempfile.TemporaryDirectory() as tmpdir: model = LudwigModel(config_sfa, logging_level=30) t0 = time.time() train_stats, _, _ = model.train( dataset=df, output_directory=tmpdir, skip_save_model=True, skip_save_progress=True, skip_save_log=True, ) elapsed_sfa = time.time() - t0 all_results["schedule_free_adamw"] = train_stats.validation summary.append( { "optimizer": "schedule_free_adamw", "final_val_loss": train_stats.validation["quality"]["loss"][-1], "final_val_accuracy": train_stats.validation["quality"]["accuracy"][-1], "training_time_s": round(elapsed_sfa, 1), } ) print(f"Schedule-Free AdamW done in {elapsed_sfa:.1f}s") ``` -------------------------------- ### Run KTO Training Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/alignment/README.md Execute KTO training using the Ludwig CLI. KTO is useful when binary per-response feedback is available. ```bash ludwig train --config config_kto.yaml --dataset train_kto.csv ``` -------------------------------- ### Get a dataset module Source: https://github.com/ludwig-ai/ludwig/blob/main/ludwig/datasets/README.md Access a dataset module object by its name. ```python titanic_dataset = ludwig.datasets.get_dataset("titanic") ``` -------------------------------- ### Preview Prompt, Chosen, and Rejected Responses Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/alignment/alignment_dpo.ipynb Displays a sample prompt along with its corresponding 'chosen' and 'rejected' responses from the prepared training dataset. This allows for a quick inspection of the data format and quality before proceeding with alignment training. ```python # Preview: show a prompt with its chosen vs rejected response row = train_df.iloc[0] print("=== PROMPT ===") print(row["prompt"]) print("\n=== CHOSEN ===") print(row["chosen"]) print("\n=== REJECTED ===") print(row["rejected"]) ``` -------------------------------- ### Get dataset output features Source: https://github.com/ludwig-ai/ludwig/blob/main/ludwig/datasets/README.md Retrieve output features for a specific dataset or a dictionary of all datasets and their features. ```python output_features = ludwig.datasets.get_datasets_output_features(dataset="titanic") ``` -------------------------------- ### Batch Prediction Source: https://github.com/ludwig-ai/ludwig/blob/main/examples/serve/vllm_serving.ipynb The `/batch_predict` endpoint accepts multiple examples encoded as a JSON string in the Pandas `split` format. ```APIDOC ## Batch Prediction Accepts multiple examples encoded as a JSON string in the Pandas `split` format via the `/batch_predict` endpoint. ### Method POST ### Endpoint `/batch_predict` ### Parameters #### Request Body - **dataset** (string) - Required - A JSON string representing the dataset in Pandas `split` format. Example: `{"columns":["text"],"data":[[prompt1],[prompt2]]}`. ### Request Example (curl) ```bash curl http://localhost:8000/batch_predict \ -X POST \ -F 'dataset={"columns":["text"],"data":[[What is Ludwig?],[What is vLLM?],[What is PagedAttention?]]}' ``` ### Request Example (Python) ```python import requests import json import time SERVER_URL = "http://localhost:8000" BATCH_PREDICT_URL = f"{SERVER_URL}/batch_predict" prompts = [ "What is Ludwig?", "What is vLLM?", "What is PagedAttention?", ] dataset_json = json.dumps({"columns": ["text"], "data": [[p] for p in prompts]}) t0 = time.perf_counter() response = requests.post(BATCH_PREDICT_URL, data={"dataset": dataset_json}) elapsed = time.perf_counter() - t0 response.raise_for_status() result = response.json() print(f"{len(prompts)} examples in {elapsed:.2f} s ({len(prompts) / elapsed:.1f} examples/s)") print("Columns:", result["columns"]) for i, row in enumerate(result["data"]): print(f" [{i}] {row[0][:120]}") ``` ### Response #### Success Response (200) - **columns** (list) - List of column names. - **data** (list of lists) - List of prediction results, where each inner list corresponds to a row. ``` -------------------------------- ### Configure VBLoRA Adapter Source: https://github.com/ludwig-ai/ludwig/blob/main/notebooks/advanced_peft_adapters.ipynb Demonstrates the configuration of the VBLoRA (Vector Bank LoRA) adapter, which uses shared vector banks across all layers for extreme compression. This example sets parameters for rank, number of vectors, vector length, and top-k selection. ```python from ludwig.schema.llms.peft import VBLoRAAdapterConfig # VBLoRA: shared vector bank across all layers vblora = VBLoRAAdapterConfig.model_validate( { "type": "vblora", "r": 4, "num_vectors": 256, "vector_length": 4096, # set to model hidden size "topk": 2, } ) peft_cfg = vblora.to_config("CAUSAL_LM") print("VBLoRA r:", peft_cfg.r, "num_vectors:", peft_cfg.num_vectors, "topk:", peft_cfg.topk) ```