|
|
| import torch |
| import os |
| import json |
| from modeling_omegacode import HolographicMasterCodeTransformer |
|
|
| class InferenceHandler: |
| def __init__(self): |
| self.model = None |
| self.initialized = False |
|
|
| def initialize(self, context): |
| |
| self.model_repo_id = os.getenv("HF_MODEL_ID") |
| if not self.model_repo_id: |
| raise ValueError("HF_MODEL_ID environment variable not set. Cannot determine model repository.") |
|
|
| |
| |
| model_dir = context.properties.get("model_dir", "/tmp/model") |
|
|
| |
| |
| import sys |
| if model_dir not in sys.path: |
| sys.path.insert(0, model_dir) |
|
|
| |
| config_path = os.path.join(model_dir, "config.json") |
| if not os.path.exists(config_path): |
| raise FileNotFoundError(f"config.json not found at {config_path}") |
| with open(config_path, 'r') as f: |
| model_config = json.load(f) |
|
|
| |
| self.model = HolographicMasterCodeTransformer( |
| input_dim=model_config.get('input_dim', 8), |
| d_model=model_config.get('d_model', 128), |
| nhead=model_config.get('nhead', 8), |
| num_layers=model_config.get('num_layers', 6), |
| output_dim=model_config.get('output_dim', 1), |
| n_harmonics=model_config.get('n_harmonics', 4) |
| ) |
|
|
| |
| model_weights_path = os.path.join(model_dir, "pytorch_model.bin") |
| if not os.path.exists(model_weights_path): |
| raise FileNotFoundError(f"pytorch_model.bin not found at {model_weights_path}") |
| self.model.load_state_dict(torch.load(model_weights_path, map_location='cpu')) |
| self.model.eval() |
|
|
| self.initialized = True |
|
|
| def preprocess(self, inputs): |
| |
| |
| |
| try: |
| input_data = inputs[0].get('input_data') |
| if not input_data: |
| raise ValueError("Missing 'input_data' in input payload") |
| |
| tensor_input = torch.tensor(input_data, dtype=torch.float32) |
| return tensor_input |
| except Exception as e: |
| raise ValueError(f"Input preprocessing failed: {e}. Expected format: [{{'input_data': [[...]]}}]") |
|
|
| def inference(self, input_batch): |
| |
| if not self.initialized: |
| raise RuntimeError("Model not initialized.") |
| |
| |
| |
| |
| alpha_value = 1/137.035 |
|
|
| with torch.no_grad(): |
| output = self.model(input_batch, alpha=alpha_value) |
| return output |
|
|
| def postprocess(self, outputs): |
| |
| |
| return [{'output': outputs.tolist()}] |
|
|
| def handle(self, inputs, context): |
| |
| model_input = self.preprocess(inputs) |
| model_output = self.inference(model_input) |
| return self.postprocess(model_output) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|