Upload 9 files
Browse files- .dockerignore +5 -0
- Dockerfile +18 -0
- env.py +96 -0
- inference.py +96 -0
- models.py +16 -0
- openenv.yaml +14 -0
- pyproject.toml +22 -0
- requirements.txt +4 -0
- uv.lock +0 -0
.dockerignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
myenv/
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.pyc
|
| 4 |
+
.vscode/
|
| 5 |
+
.git/
|
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1. Use a stable Python base image
|
| 2 |
+
FROM python:3.12-slim
|
| 3 |
+
|
| 4 |
+
# 2. Set the directory where your code will live inside the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# 3. Copy only the dependency files first (this makes builds faster)
|
| 8 |
+
COPY pyproject.toml uv.lock ./
|
| 9 |
+
|
| 10 |
+
# 4. Install 'uv' and use it to install your project's dependencies
|
| 11 |
+
RUN pip install --no-cache-dir uv && \
|
| 12 |
+
uv pip install --system --requirement pyproject.toml
|
| 13 |
+
|
| 14 |
+
# 5. Copy the rest of your project files into the container
|
| 15 |
+
COPY . .
|
| 16 |
+
|
| 17 |
+
# 6. Tell Docker to run the "server" command you defined earlier
|
| 18 |
+
CMD ["python", "inference.py"]
|
env.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
from dotenv import load_dotenv # Add this
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import os
|
| 6 |
+
import random
|
| 7 |
+
from models import EdgeObservation, EdgeAction, EdgeReward
|
| 8 |
+
|
| 9 |
+
class EdgeEnv:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.task_name = "task_stable_edge"
|
| 12 |
+
self.max_steps = 15 # Increased slightly for better trajectory data
|
| 13 |
+
self.current_step = 0
|
| 14 |
+
self._state = self._get_initial_state()
|
| 15 |
+
|
| 16 |
+
def _get_initial_state(self) -> EdgeObservation:
|
| 17 |
+
if self.task_name == "task_stable_edge":
|
| 18 |
+
return EdgeObservation(ram_usage=45.0, battery_level=100.0, latency=20.0, active_processes=5)
|
| 19 |
+
elif self.task_name == "task_fluctuating_memory":
|
| 20 |
+
return EdgeObservation(ram_usage=65.0, battery_level=85.0, latency=35.0, active_processes=12)
|
| 21 |
+
elif self.task_name == "task_low_battery_spike":
|
| 22 |
+
return EdgeObservation(ram_usage=80.0, battery_level=20.0, latency=45.0, active_processes=18)
|
| 23 |
+
return EdgeObservation(ram_usage=50.0, battery_level=100.0, latency=20.0, active_processes=5)
|
| 24 |
+
|
| 25 |
+
def set_task(self, task_name: str):
|
| 26 |
+
self.task_name = task_name
|
| 27 |
+
self.reset()
|
| 28 |
+
|
| 29 |
+
def reset(self) -> EdgeObservation:
|
| 30 |
+
self.current_step = 0
|
| 31 |
+
self._state = self._get_initial_state()
|
| 32 |
+
return self._state
|
| 33 |
+
|
| 34 |
+
def state(self) -> EdgeObservation:
|
| 35 |
+
return self._state
|
| 36 |
+
|
| 37 |
+
def step(self, action: EdgeAction) -> tuple[EdgeObservation, EdgeReward, bool, dict]:
|
| 38 |
+
self.current_step += 1
|
| 39 |
+
|
| 40 |
+
# Action execution with slight stochastic variances (real-world simulation)
|
| 41 |
+
if action.kill_process and self._state.active_processes > 0:
|
| 42 |
+
self._state.active_processes -= 1
|
| 43 |
+
self._state.ram_usage -= random.uniform(10.0, 15.0)
|
| 44 |
+
|
| 45 |
+
if action.throttle_cpu:
|
| 46 |
+
self._state.battery_level += random.uniform(1.0, 3.0)
|
| 47 |
+
self._state.latency += random.uniform(10.0, 20.0)
|
| 48 |
+
|
| 49 |
+
if action.route_to_cloud:
|
| 50 |
+
self._state.ram_usage -= random.uniform(20.0, 30.0)
|
| 51 |
+
self._state.latency += random.uniform(40.0, 60.0)
|
| 52 |
+
self._state.battery_level -= random.uniform(2.0, 5.0)
|
| 53 |
+
|
| 54 |
+
# Natural environmental drift
|
| 55 |
+
self._state.battery_level -= random.uniform(1.0, 3.0)
|
| 56 |
+
self._state.ram_usage += random.uniform(3.0, 8.0)
|
| 57 |
+
# Latency naturally recovers slightly if not pushed
|
| 58 |
+
if not action.throttle_cpu and not action.route_to_cloud:
|
| 59 |
+
self._state.latency = max(20.0, self._state.latency - 5.0)
|
| 60 |
+
|
| 61 |
+
# Clamp values
|
| 62 |
+
self._state.battery_level = max(0.0, min(100.0, self._state.battery_level))
|
| 63 |
+
self._state.ram_usage = max(0.0, min(100.0, self._state.ram_usage))
|
| 64 |
+
self._state.latency = max(5.0, min(500.0, self._state.latency))
|
| 65 |
+
|
| 66 |
+
# Terminal conditions
|
| 67 |
+
done = self.current_step >= self.max_steps or self._state.battery_level <= 0.0 or self._state.ram_usage >= 100.0
|
| 68 |
+
|
| 69 |
+
# Calculate shaped reward
|
| 70 |
+
final_score = self._grade_task()
|
| 71 |
+
|
| 72 |
+
return self._state, EdgeReward(score=final_score), done, {}
|
| 73 |
+
|
| 74 |
+
def _grade_task(self) -> float:
|
| 75 |
+
# Failsafe: System crash = 0 score
|
| 76 |
+
if self._state.ram_usage >= 100.0 or self._state.battery_level <= 0.0:
|
| 77 |
+
return 0.0
|
| 78 |
+
|
| 79 |
+
# Continuous reward shaping based on task priorities
|
| 80 |
+
ram_health = 1.0 - (self._state.ram_usage / 100.0)
|
| 81 |
+
battery_health = self._state.battery_level / 100.0
|
| 82 |
+
latency_health = max(0.0, 1.0 - (self._state.latency / 200.0)) # Latency over 200ms is 0 health
|
| 83 |
+
|
| 84 |
+
if self.task_name == "task_stable_edge":
|
| 85 |
+
# 100% focus on RAM
|
| 86 |
+
score = ram_health
|
| 87 |
+
|
| 88 |
+
elif self.task_name == "task_fluctuating_memory":
|
| 89 |
+
# 70% focus on RAM, 30% focus on keeping latency down
|
| 90 |
+
score = (ram_health * 0.7) + (latency_health * 0.3)
|
| 91 |
+
|
| 92 |
+
elif self.task_name == "task_low_battery_spike":
|
| 93 |
+
# Hard: Must balance all three constraints
|
| 94 |
+
score = (battery_health * 0.5) + (ram_health * 0.3) + (latency_health * 0.2)
|
| 95 |
+
|
| 96 |
+
return max(0.0, min(1.0, score))
|
inference.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from env import EdgeEnv
|
| 5 |
+
from models import EdgeAction
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:11434/v1/")
|
| 9 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "llama3.1")
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "ollama-local")
|
| 11 |
+
|
| 12 |
+
client = OpenAI(
|
| 13 |
+
base_url=API_BASE_URL,
|
| 14 |
+
api_key=HF_TOKEN
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def run_inference():
|
| 18 |
+
env = EdgeEnv()
|
| 19 |
+
tasks = ["task_stable_edge", "task_fluctuating_memory", "task_low_battery_spike"]
|
| 20 |
+
BENCHMARK_NAME = "EdgeDeployment"
|
| 21 |
+
|
| 22 |
+
for task in tasks:
|
| 23 |
+
# [START]
|
| 24 |
+
print(f"[START] task={task} env={BENCHMARK_NAME} model={MODEL_NAME}", flush=True)
|
| 25 |
+
|
| 26 |
+
env.set_task(task)
|
| 27 |
+
obs = env.reset()
|
| 28 |
+
done = False
|
| 29 |
+
|
| 30 |
+
step_count = 0
|
| 31 |
+
rewards_list = []
|
| 32 |
+
|
| 33 |
+
while not done:
|
| 34 |
+
|
| 35 |
+
print(f"[DEBUG] Observation: {obs.model_dump_json()}")
|
| 36 |
+
|
| 37 |
+
prompt = f"""
|
| 38 |
+
System Status: RAM: {obs.ram_usage:.1f}%, Battery: {obs.battery_level:.1f}%
|
| 39 |
+
|
| 40 |
+
Rules:
|
| 41 |
+
1. If RAM is > 80.0, "kill_process" must be true.
|
| 42 |
+
2. If Battery is < 40.0, "throttle_cpu" must be true.
|
| 43 |
+
3. Otherwise, set everything to false.
|
| 44 |
+
|
| 45 |
+
Respond ONLY with a raw JSON object containing EXACTLY these three boolean keys. No markdown, no explanation.
|
| 46 |
+
Example: {{"kill_process": false, "throttle_cpu": false, "route_to_cloud": false}}
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
error_msg = "null"
|
| 50 |
+
try:
|
| 51 |
+
response = client.chat.completions.create(
|
| 52 |
+
model=MODEL_NAME,
|
| 53 |
+
messages=[{"role": "user", "content": prompt}],
|
| 54 |
+
temperature=0.0 # 0.0 forces the model to be completely robotic and follow the rules
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
raw_output = response.choices[0].message.content.strip()
|
| 58 |
+
raw_output = raw_output.replace("```json", "").replace("```", "").strip()
|
| 59 |
+
|
| 60 |
+
action_data = json.loads(raw_output)
|
| 61 |
+
|
| 62 |
+
action = EdgeAction(
|
| 63 |
+
kill_process=action_data.get("kill_process", False),
|
| 64 |
+
throttle_cpu=action_data.get("throttle_cpu", False),
|
| 65 |
+
route_to_cloud=action_data.get("route_to_cloud", False)
|
| 66 |
+
)
|
| 67 |
+
except Exception as e:
|
| 68 |
+
|
| 69 |
+
error_msg = f'"{str(e)}"'
|
| 70 |
+
print(f"[DEBUG] ⚠️ ERROR: {e}")
|
| 71 |
+
action = EdgeAction(kill_process=False, throttle_cpu=False, route_to_cloud=False)
|
| 72 |
+
|
| 73 |
+
obs, reward, done, info = env.step(action)
|
| 74 |
+
|
| 75 |
+
# Track values
|
| 76 |
+
step_count += 1
|
| 77 |
+
reward_val = reward.score
|
| 78 |
+
rewards_list.append(reward_val)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
done_str = str(done).lower()
|
| 82 |
+
action_str = action.model_dump_json().replace(" ", "")
|
| 83 |
+
print(f"[STEP] step={step_count} action={action_str} reward={reward_val:.2f} done={done_str} error={error_msg}", flush=True)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
final_average_score = sum(rewards_list) / step_count if step_count > 0 else 0.0
|
| 87 |
+
success_str = "true" if final_average_score > 0.0 else "false"
|
| 88 |
+
rewards_str = ",".join(f"{r:.2f}" for r in rewards_list)
|
| 89 |
+
|
| 90 |
+
print(f"[END] success={success_str} steps={step_count} score={final_average_score:.2f} rewards={rewards_str}", flush=True)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
run_inference()
|
| 95 |
+
|
| 96 |
+
|
models.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
|
| 3 |
+
class EdgeObservation(BaseModel):
|
| 4 |
+
ram_usage: float = Field(..., description="Current RAM usage percentage (0.0 to 100.0)")
|
| 5 |
+
battery_level: float = Field(..., description="Current battery percentage (0.0 to 100.0)")
|
| 6 |
+
latency: float = Field(..., description="Current network latency in ms")
|
| 7 |
+
active_processes: int = Field(..., description="Number of active processes")
|
| 8 |
+
|
| 9 |
+
class EdgeAction(BaseModel):
|
| 10 |
+
kill_process: bool = Field(default=False, description="Terminate a non-critical process to free RAM")
|
| 11 |
+
throttle_cpu: bool = Field(default=False, description="Throttle CPU to save battery at the cost of latency")
|
| 12 |
+
route_to_cloud: bool = Field(default=False, description="Offload processing to the cloud to free RAM, increasing latency")
|
| 13 |
+
|
| 14 |
+
class EdgeReward(BaseModel):
|
| 15 |
+
# CRITICAL FIX: Reward must be strictly typed and expected to be bounded 0.0 - 1.0 in env.py
|
| 16 |
+
score: float = Field(..., description="Reward score strictly between 0.0 and 1.0")
|
openenv.yaml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: "1.0"
|
| 2 |
+
environment:
|
| 3 |
+
name: "edge-resource-manager"
|
| 4 |
+
description: "Simulates managing system resources (RAM, battery, latency) on an edge device."
|
| 5 |
+
tasks:
|
| 6 |
+
- name: "task_stable_edge"
|
| 7 |
+
difficulty: "easy"
|
| 8 |
+
description: "Maintain RAM usage at stable levels under normal conditions."
|
| 9 |
+
- name: "task_fluctuating_memory"
|
| 10 |
+
difficulty: "medium"
|
| 11 |
+
description: "Handle sudden memory spikes without causing excessive system latency."
|
| 12 |
+
- name: "task_low_battery_spike"
|
| 13 |
+
difficulty: "hard"
|
| 14 |
+
description: "Survive critical memory limits while the battery is nearly depleted."
|
pyproject.toml
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[build-system]
|
| 2 |
+
requires = ["setuptools>=61.0", "wheel"]
|
| 3 |
+
build-backend = "setuptools.build_meta"
|
| 4 |
+
|
| 5 |
+
[project]
|
| 6 |
+
name = "edge-deployment-env"
|
| 7 |
+
version = "0.1.0"
|
| 8 |
+
description = "Edge deployment resource simulation environment"
|
| 9 |
+
readme = "README.md"
|
| 10 |
+
requires-python = ">=3.10"
|
| 11 |
+
dependencies = [
|
| 12 |
+
"openai",
|
| 13 |
+
"pydantic",
|
| 14 |
+
"openenv-core",
|
| 15 |
+
"numpy",
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
[tool.setuptools]
|
| 19 |
+
packages = ["."]
|
| 20 |
+
|
| 21 |
+
[project.scripts]
|
| 22 |
+
server = "server.app:main"
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openenv-core
|
| 2 |
+
|
| 3 |
+
pydantic>=2.0.0
|
| 4 |
+
openai>=1.0.0
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|