2024-12-12 20:21:09 +03:00

403 lines
14 KiB
Python

import os
import asyncio
import logging
from typing import Dict, List, Optional, Any
from datetime import datetime
from dataclasses import dataclass
from ocean_lib.ocean.ocean import Ocean
from ocean_lib.config import Config
from ocean_lib.services.service import Service
# Placeholder for DistributedComputeOrchestrator class definition
class DistributedComputeOrchestrator:
def __init__(self):
pass
async def prepare_partitions(self, dataset_did, partition_config):
# Placeholder implementation
return [{'did': dataset_did, 'partition_id': i} for i in range(partition_config.get('num_partitions', 1))]
# Placeholder for ComputeMetricsTracker class definition
class ComputeMetricsTracker:
def __init__(self):
pass
async def register_job_group(self, compute_jobs):
# Placeholder implementation
return "job_group_id"
async def get_jobs_in_group(self, job_group_id):
# Placeholder implementation
return []
async def update_job_metrics(self, job_id, status):
# Placeholder implementation
pass
async def get_metrics(self, job_group_id):
# Placeholder implementation
return {}
async def get_job_metrics(self, job_id):
# Placeholder implementation
return {}
@dataclass
class ComputeEnvironment:
name: str
resources: Dict[str, Any]
docker_image: str
runtime_config: Dict[str, Any]
supported_frameworks: List[str]
class DistributedOceanCompute:
def __init__(self, config_path: str = 'config.ini'):
"""Initialize enhanced Ocean compute manager."""
# Initialize Ocean Protocol
self.config = Config(config_path)
self.ocean = Ocean(self.config)
self.wallet = self.ocean.create_wallet(os.getenv("OCEAN_PRIVATE_KEY"))
# Initialize compute environments
self.environments = self._initialize_environments()
# Initialize metrics tracker
self.metrics_tracker = ComputeMetricsTracker()
# Initialize compute orchestrator
self.orchestrator = DistributedComputeOrchestrator()
# Setup logging
self._setup_logging()
def _initialize_environments(self) -> Dict[str, ComputeEnvironment]:
"""Initialize supported compute environments."""
return {
'pytorch-gpu': ComputeEnvironment(
name='pytorch-gpu',
resources={
'cpu_cores': 8,
'memory_gb': 32,
'gpu_count': 1,
'gpu_type': 'NVIDIA-T4'
},
docker_image='oceanprotocol/pytorch:latest',
runtime_config={
'cuda_version': '11.4',
'pytorch_version': '1.9',
'allow_network': False
},
supported_frameworks=['pytorch', 'torchvision']
),
'tensorflow-gpu': ComputeEnvironment(
name='tensorflow-gpu',
resources={
'cpu_cores': 8,
'memory_gb': 32,
'gpu_count': 1,
'gpu_type': 'NVIDIA-T4'
},
docker_image='oceanprotocol/tensorflow:latest',
runtime_config={
'cuda_version': '11.4',
'tensorflow_version': '2.6',
'allow_network': False
},
supported_frameworks=['tensorflow', 'keras']
),
'sklearn-cpu': ComputeEnvironment(
name='sklearn-cpu',
resources={
'cpu_cores': 16,
'memory_gb': 64,
'gpu_count': 0
},
docker_image='oceanprotocol/sklearn:latest',
runtime_config={
'sklearn_version': '0.24',
'allow_network': False
},
supported_frameworks=['sklearn', 'pandas', 'numpy']
),
'r-analytics': ComputeEnvironment(
name='r-analytics',
resources={
'cpu_cores': 8,
'memory_gb': 32,
'gpu_count': 0
},
docker_image='oceanprotocol/r-analytics:latest',
runtime_config={
'r_version': '4.1',
'allow_network': False
},
supported_frameworks=['r-base', 'tidyverse', 'caret']
)
}
async def start_distributed_compute(
self,
dataset_did: str,
algorithm_did: Optional[str] = None,
compute_config: Dict[str, Any] = None,
environment_name: str = 'pytorch-gpu',
partition_config: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""Start distributed compute job with enhanced configuration."""
try:
# Validate environment
environment = self._validate_environment(environment_name, compute_config)
# Prepare compute partitions
partitions = await self.orchestrator.prepare_partitions(
dataset_did,
partition_config or {}
)
# Start distributed computation
compute_jobs = []
for partition in partitions:
job = await self._start_partition_compute(
partition,
environment,
algorithm_did,
compute_config
)
compute_jobs.append(job)
# Register with metrics tracker
job_group_id = await self.metrics_tracker.register_job_group(compute_jobs)
return {
'job_group_id': job_group_id,
'jobs': compute_jobs,
'environment': environment.name,
'start_time': datetime.utcnow().isoformat()
}
except Exception as e:
self.logger.error(f"Error starting distributed compute: {str(e)}")
raise
async def monitor_distributed_compute(
self,
job_group_id: str,
check_interval: int = 30
) -> Dict[str, Any]:
"""Monitor distributed compute jobs with enhanced metrics."""
try:
jobs = await self.metrics_tracker.get_jobs_in_group(job_group_id)
completed = False
while not completed:
# Update job statuses
statuses = []
for job in jobs:
status = await self._get_job_status(job['job_id'])
statuses.append(status)
# Update metrics
await self.metrics_tracker.update_job_metrics(
job['job_id'],
status
)
# Check if all jobs are complete
completed = all(s['status'] in ['completed', 'failed'] for s in statuses)
if not completed:
await asyncio.sleep(check_interval)
# Aggregate results
return await self._aggregate_results(job_group_id)
except Exception as e:
self.logger.error(f"Error monitoring compute: {str(e)}")
raise
async def get_compute_metrics(
self,
job_group_id: str
) -> Dict[str, Any]:
"""Get comprehensive compute metrics."""
return await self.metrics_tracker.get_metrics(job_group_id)
async def _start_partition_compute(
self,
partition: Dict[str, Any],
environment: ComputeEnvironment,
algorithm_did: Optional[str],
compute_config: Dict[str, Any]
) -> Dict[str, Any]:
"""Start compute job for a single partition."""
try:
# Configure environment
env_config = self._prepare_environment_config(
environment,
compute_config
)
# Get assets
dataset_asset = await self.ocean.assets.resolve(partition['did'])
algorithm_asset = None
if algorithm_did:
algorithm_asset = await self.ocean.assets.resolve(algorithm_did)
# Get compute service
compute_service = dataset_asset.get_service('compute')
# Start compute job
job = await self.ocean.compute.start(
consumer_wallet=self.wallet,
dataset=dataset_asset,
compute_environment=env_config,
algorithm=algorithm_asset,
payment_amount=self._calculate_payment(compute_service)
)
return {
'job_id': job['jobId'],
'partition': partition,
'environment': environment.name,
'start_time': datetime.utcnow().isoformat()
}
except Exception as e:
self.logger.error(f"Error starting partition compute: {str(e)}")
raise
def _validate_environment(
self,
environment_name: str,
compute_config: Dict[str, Any]
) -> ComputeEnvironment:
"""Validate and configure compute environment."""
if environment_name not in self.environments:
raise ValueError(f"Unsupported environment: {environment_name}")
environment = self.environments[environment_name]
# Validate resource requirements
if compute_config and 'resources' in compute_config:
required_resources = compute_config['resources']
for resource, required in required_resources.items():
if required > environment.resources.get(resource, 0):
raise ValueError(
f"Insufficient {resource} in environment {environment_name}"
)
return environment
async def _aggregate_results(
self,
job_group_id: str
) -> Dict[str, Any]:
"""Aggregate results from distributed compute jobs."""
try:
jobs = await self.metrics_tracker.get_jobs_in_group(job_group_id)
aggregated_results = {
'success_count': 0,
'failed_count': 0,
'total_compute_time': 0,
'results': [],
'metrics': {}
}
for job in jobs:
job_results = await self._get_job_results(job['job_id'])
if job_results['status'] == 'completed':
aggregated_results['success_count'] += 1
aggregated_results['results'].append(job_results['data'])
else:
aggregated_results['failed_count'] += 1
aggregated_results['total_compute_time'] += job_results['compute_time']
# Aggregate metrics
metrics = await self.metrics_tracker.get_job_metrics(job['job_id'])
for metric, value in metrics.items():
if metric not in aggregated_results['metrics']:
aggregated_results['metrics'][metric] = []
aggregated_results['metrics'][metric].append(value)
# Calculate average metrics
for metric in aggregated_results['metrics']:
values = aggregated_results['metrics'][metric]
aggregated_results['metrics'][metric] = sum(values) / len(values)
return aggregated_results
except Exception as e:
self.logger.error(f"Error aggregating results: {str(e)}")
raise
def _calculate_payment(self, compute_service: Service) -> int:
"""Calculate required payment for compute service."""
base_fee = compute_service.get_price()
# Add premium for distributed compute
premium = base_fee * 0.2
return int(base_fee + premium)
def _setup_logging(self):
"""Setup logging configuration."""
self.logger = logging.getLogger('OceanCompute')
self.logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
)
self.logger.addHandler(handler)
# Example usage
async def main():
# Initialize compute manager
compute_manager = DistributedOceanCompute()
# Configure compute job
compute_config = {
'resources': {
'cpu_cores': 4,
'memory_gb': 16,
'gpu_count': 1
},
'batch_size': 32,
'max_runtime': 3600
}
# Configure partitioning
partition_config = {
'strategy': 'equal_size',
'num_partitions': 4
}
try:
# Start distributed compute
job_group = await compute_manager.start_distributed_compute(
dataset_did="did:op:123",
algorithm_did="did:op:456",
compute_config=compute_config,
environment_name='pytorch-gpu',
partition_config=partition_config
)
# Monitor computation
results = await compute_manager.monitor_distributed_compute(
job_group['job_group_id']
)
# Get metrics
metrics = await compute_manager.get_compute_metrics(
job_group['job_group_id']
)
print(f"Compute results: {results}")
print(f"Compute metrics: {metrics}")
except Exception as e:
print(f"Error in compute workflow: {str(e)}")
if __name__ == "__main__":
asyncio.run(main())