File size: 1,590 Bytes
2767c41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import Any

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

from predictor import N_FEATURES, get_metadata, predict_from_features


app = FastAPI(
    title="S4-FIFO Parameter Prediction API",
    version="0.1.0",
    description="Online control-plane inference artifact for S4-FIFO parameter selection.",
)


class PredictRequest(BaseModel):
    features: list[float] = Field(
        ...,
        description="73-dimensional cache-level feature vector in the training feature order.",
        min_length=N_FEATURES,
        max_length=N_FEATURES,
    )
    top_k: int = Field(
        default=3,
        ge=1,
        le=18,
        description="Number of probability/risk-ranked candidate configurations to return.",
    )


@app.get("/")
def root() -> dict[str, Any]:
    return {
        "service": "S4-FIFO Parameter Prediction API",
        "version": app.version,
        "endpoints": {
            "health": "/health",
            "metadata": "/metadata",
            "predict": "POST /predict",
            "docs": "/docs",
        },
    }


@app.get("/health")
def health() -> dict[str, str]:
    return {"status": "ok"}


@app.get("/metadata")
def metadata() -> dict[str, Any]:
    return get_metadata()


@app.post("/predict")
def predict(req: PredictRequest) -> dict[str, Any]:
    if len(req.features) != N_FEATURES:
        raise HTTPException(
            status_code=400,
            detail=f"Expected {N_FEATURES} features, got {len(req.features)}",
        )
    return predict_from_features(req.features, top_k=req.top_k)