2023-08-20 22:53:35 -07:00
|
|
|
from abc import ABC, abstractmethod
|
2023-08-22 17:11:20 -07:00
|
|
|
from typing import Optional
|
|
|
|
|
import logging
|
|
|
|
|
import asyncio
|
|
|
|
|
|
2023-08-23 11:19:16 -07:00
|
|
|
from .agent import AgentPrompt
|
|
|
|
|
from .compute_node import ComputeNode
|
2023-08-22 17:11:20 -07:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2023-08-20 22:53:35 -07:00
|
|
|
|
|
|
|
|
# How to dispatch different computing tasks (some tasks may contain a large amount of state for correct execution)
|
|
|
|
|
# to suitable computing nodes, achieving a balance of speed, cost, and power consumption,
|
|
|
|
|
# is the CORE GOAL of the entire computing task schedule system (aios_kernel).
|
2023-08-23 11:19:16 -07:00
|
|
|
class ComputeTask(ABC):
|
2023-08-20 22:53:35 -07:00
|
|
|
@abstractmethod
|
|
|
|
|
def display(self) -> str:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 11:19:16 -07:00
|
|
|
class ComputeKernel:
|
2023-08-22 17:11:20 -07:00
|
|
|
_instance = None
|
|
|
|
|
def __new__(cls):
|
|
|
|
|
if cls._instance is None:
|
2023-08-23 11:19:16 -07:00
|
|
|
cls._instance = super(ComputeKernel, cls).__new__(cls)
|
2023-08-22 17:11:20 -07:00
|
|
|
return cls._instance
|
|
|
|
|
|
2023-08-20 22:53:35 -07:00
|
|
|
def __init__(self) -> None:
|
2023-08-22 17:11:20 -07:00
|
|
|
self.task_queue = []
|
|
|
|
|
self.is_start = False
|
2023-08-20 22:53:35 -07:00
|
|
|
pass
|
|
|
|
|
|
2023-08-23 11:19:16 -07:00
|
|
|
def run(self,task:ComputeTask) -> None:
|
2023-08-20 22:53:35 -07:00
|
|
|
# check there is compute node can support this task
|
2023-08-22 17:11:20 -07:00
|
|
|
if self.is_task_support(task) is False:
|
|
|
|
|
logger.error(f"task {task.display()} is not support by any compute node")
|
|
|
|
|
return
|
2023-08-20 22:53:35 -07:00
|
|
|
# add task to working_queue
|
2023-08-22 17:11:20 -07:00
|
|
|
self.task_queue.append(task)
|
|
|
|
|
|
2023-08-20 22:53:35 -07:00
|
|
|
|
|
|
|
|
def start(self):
|
2023-08-22 17:11:20 -07:00
|
|
|
if self.is_start is True:
|
|
|
|
|
logger.warn("compute_kernel is already start")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.is_start = True
|
|
|
|
|
async def _run_task_loop():
|
|
|
|
|
while True:
|
|
|
|
|
task = self.task_queue.pop(0)
|
2023-08-23 11:19:16 -07:00
|
|
|
c_node:ComputeNode= await self._schedule(task)
|
2023-08-22 17:11:20 -07:00
|
|
|
c_node.push_task(task)
|
|
|
|
|
|
|
|
|
|
asyncio.create_task(_run_task_loop())
|
|
|
|
|
|
|
|
|
|
|
2023-08-23 11:19:16 -07:00
|
|
|
async def _schedule(self,task) -> ComputeNode:
|
2023-08-20 22:53:35 -07:00
|
|
|
pass
|
|
|
|
|
|
2023-08-23 11:19:16 -07:00
|
|
|
def add_compute_node(self,node:ComputeNode):
|
2023-08-20 22:53:35 -07:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def disable_compute_node(self,):
|
2023-08-22 17:11:20 -07:00
|
|
|
pass
|
|
|
|
|
|
2023-08-23 11:19:16 -07:00
|
|
|
def is_task_support(self,task:ComputeTask) -> bool:
|
2023-08-22 17:11:20 -07:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# friendly interface for use:
|
2023-08-23 11:19:16 -07:00
|
|
|
def llm_completion(self,prompt:AgentPrompt,mode_name:Optional[str] = None,max_token:int = 0) -> ComputeTask:
|
2023-08-22 17:11:20 -07:00
|
|
|
# craete a llm_work_task ,push on queue's end
|
|
|
|
|
# then task_schedule would run this task.(might schedule some work_task to another host)
|
|
|
|
|
pass
|
|
|
|
|
|
2023-08-23 11:19:16 -07:00
|
|
|
async def do_llm_completion(self,prompt:AgentPrompt,mode_name:Optional[str] = None,max_token:int = 0) -> str:
|
2023-08-22 17:11:20 -07:00
|
|
|
pass
|
|
|
|
|
|