|
| 1 | +--- |
| 2 | +title: Tensorflow Dependency Switching |
| 3 | +doc_type: demo |
| 4 | +status: draft |
| 5 | +generated: true |
| 6 | +created: '2026-01-07' |
| 7 | +builder: omnipkg-docbuilder |
| 8 | +builder_version: 2.1.0 |
| 9 | +section: demos |
| 10 | +--- |
| 11 | + |
| 12 | +# Tensorflow Dependency Switching |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +This log demonstrates **Complex Dependency Tree Management**. |
| 17 | + |
| 18 | +While the previous demo showed swapping a single binary, this one shows swapping **TensorFlow** (a massive package) along with its sub-dependencies (`typing_extensions`, `keras`) in a nested context. It proves OmniPkg handles the "Butterfly Effect" of dependencies—changing one package updates the entire graph instantly. |
| 19 | + |
| 20 | +# TensorFlow Test |
| 21 | + |
| 22 | +!!! note "Demo Prerequisite" |
| 23 | + This demo requires **Python 3.11**. |
| 24 | + |
| 25 | +This demo validates OmniPkg's ability to handle **massive, complex dependency graphs**. Unlike simple packages, TensorFlow brings along a huge tree of dependencies (Keras, TensorBoard, Protobuf, etc.). OmniPkg must ensure *all* of them are swapped correctly to prevent ABI mismatches. |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```bash |
| 30 | +# Run Demo #4: Complex Dependency Graph Switching |
| 31 | +omnipkg demo 4 |
| 32 | +``` |
| 33 | + |
| 34 | +## What You'll Learn |
| 35 | + |
| 36 | +1. **Deep Graph Swapping**: How activating `tensorflow==2.13.0` automatically pulls in the correct `keras`, `typing_extensions`, and `numpy` versions. |
| 37 | +2. **Nested Contexts**: How to use `omnipkgLoader` inside another `omnipkgLoader` block (Inception-style environment management). |
| 38 | +3. **Strict Mode Verification**: Verifying that no "cloaked" modules (old versions) leak into the new context. |
| 39 | + |
| 40 | +## The Code |
| 41 | + |
| 42 | +This script performs a nested activation: it first activates a specific version of `typing_extensions`, and then *inside that block*, activates an old version of `tensorflow` that requires a *different* `typing_extensions`. OmniPkg handles this conflict resolution in real-time. |
| 43 | + |
| 44 | +```python title="demo_tensorflow.py" |
| 45 | +from omnipkg.loader import omnipkgLoader |
| 46 | + |
| 47 | +# 1. Outer Context: Activate older typing_extensions |
| 48 | +with omnipkgLoader("typing_extensions==4.5.0"): |
| 49 | + import typing_extensions as te |
| 50 | + print(f"Outer Version: {te.__version__}") |
| 51 | + # Output: 4.5.0 |
| 52 | + |
| 53 | + # 2. Inner Context: Activate TensorFlow 2.13.0 |
| 54 | + # TensorFlow 2.13.0 might require a different typing_extensions. |
| 55 | + # OmniPkg creates a NEW nested bubble for this block. |
| 56 | + with omnipkgLoader("tensorflow==2.13.0"): |
| 57 | + import tensorflow as tf |
| 58 | + import typing_extensions as te_inner |
| 59 | + |
| 60 | + print(f"Inner TF Version: {tf.__version__}") |
| 61 | + # Output: 2.13.0 |
| 62 | + |
| 63 | + print(f"Inner TE Version: {te_inner.__version__}") |
| 64 | + # Output: Matches TF's requirement (automatically resolved) |
| 65 | + |
| 66 | + # Verify the complex graph works by creating a model |
| 67 | + model = tf.keras.Sequential([tf.keras.layers.Dense(1)]) |
| 68 | + print("Model created successfully") |
| 69 | + |
| 70 | + # 3. Back to Outer Context |
| 71 | + # TensorFlow is unloaded; typing_extensions reverts to 4.5.0 |
| 72 | + import typing_extensions as te_back |
| 73 | + print(f"Restored Version: {te_back.__version__}") |
| 74 | + # Output: 4.5.0 |
| 75 | +``` |
| 76 | + |
| 77 | +## Live Execution Log |
| 78 | + |
| 79 | +Notice the **Nested activation (depth=2)** line in the logs. This confirms that OmniPkg is maintaining a stack of environments and correctly popping them off as the code exits the `with` blocks. |
| 80 | + |
| 81 | +```text title="omnipkg demo 4" |
| 82 | +(evocoder_env) minds3t@aiminingrig:~/omnipkg$ omnipkg demo 4 |
| 83 | +
|
| 84 | +================================================================================ |
| 85 | + 🚀 🚨 OMNIPKG TENSORFLOW DEPENDENCY SWITCHING TEST 🚨 |
| 86 | +================================================================================ |
| 87 | +
|
| 88 | +--- Nested Loader Test --- |
| 89 | +🌀 Testing nested loader usage... |
| 90 | + ✅ Outer context - Typing Extensions: 4.5.0 |
| 91 | +
|
| 92 | +🚀 Fast-activating tensorflow==2.13.0 ... |
| 93 | + 📂 Searching for bubble: .../tensorflow-2.13.0 |
| 94 | + 📊 Bubble: 40 packages, 0 conflicts |
| 95 | + 🧹 Purging 40 module(s) from memory... |
| 96 | + - 🔒 STRICT mode |
| 97 | + ⚡ HEALED in 25,743.3 μs |
| 98 | + ✅ Inner context - TensorFlow: 2.13.0 |
| 99 | + ✅ Inner context - Typing Extensions: 4.5.0 |
| 100 | + ✅ Nested loader test: Model created successfully |
| 101 | +
|
| 102 | +🌀 omnipkg loader: Deactivating tensorflow==2.13.0 [depth=2]... |
| 103 | + ✅ Environment restored. |
| 104 | + ⏱️ Swap Time: 26,032.537 μs |
| 105 | +
|
| 106 | +🌀 omnipkg loader: Deactivating typing_extensions==4.5.0... |
| 107 | + ✅ Environment restored. |
| 108 | + ⏱️ Swap Time: 125,331.240 μs |
| 109 | +
|
| 110 | +================================================================================ |
| 111 | + 🚀 STEP 3: Test Results Summary |
| 112 | +================================================================================ |
| 113 | +Test 1 (TensorFlow 2.13.0 Bubble): ✅ PASSED |
| 114 | +Test 2 (Dependency Switching): ✅ PASSED |
| 115 | +Test 3 (Nested Loaders): ✅ PASSED |
| 116 | +
|
| 117 | +Overall: 3/3 tests passed |
| 118 | +🎉 DEMO PASSED! 🎉 |
| 119 | +``` |
| 120 | + |
| 121 | +## How It Works |
| 122 | + |
| 123 | +1. **Graph Resolution**: When `tensorflow==2.13.0` is requested, the LibResolver walks its dependency tree. It identifies 40+ sub-dependencies. |
| 124 | +2. **Memory Purge**: It unloads all 40 modules from `sys.modules` to ensure no stale code remains. |
| 125 | +3. **Stack Management**: The `omnipkgLoader` tracks the "depth" of the context. When the inner block exits, it doesn't just clear `sys.path`—it restores the `sys.path` of the *outer* block (restoring `typing_extensions==4.5.0`). |
0 commit comments