.pt2 checkpoints and run inference. Weights are loaded from /opt/ml/model (SageMaker convention) via torch._inductor.aoti_load_package.
For models with no custom forward logic (e.g. embedding backbones), BaseServerModel is used directly. Models that need custom input handling (e.g. bulk RNA, segmentation masks) subclass it and override prepare_sample and forward_batch.
Two-stage failure isolation
Stage 1 — Deserialization (per-request, parallel):prepare_sample decodes, transforms, and validates each
request in a thread pool. Failures are returned immediately as
ValueError (HTTP 400) to the individual caller.
Stage 2 — GPU forward (shared batch):
forward_batch receives only validated tensors, runs the
model, synchronizes CUDA, and transfers results to CPU. Any
failure at this stage raises _ForwardError
(RuntimeError subclass → HTTP 503) shared by all
requests in the batch.
PreparedSample
BaseServerModel.prepare_sample during the per-request deserialization stage. Only samples that pass all validation enter the batch queue.
Attributes:
tile- Preprocessed image tensor of shape(C, H, W).extra_tensors- Additional model inputs (e.g. bulk RNA tensor) keyed by input name. Empty for standard embedding models.
BaseServerModel
.pt2 model and runs single-tile inference.
For embedding backbones (h0-mini, h0, h1, …) this class is used directly — no subclass needed. Override prepare_sample and forward_batch for models that require custom input handling (e.g. bulk RNA).
The model specification (tile size, normalization, etc.).
Filename of the
.pt2 checkpoint.Torch device string (
"cuda" or "cpu").Directory containing exported checkpoints.
model_spec
model_name
preprocess_bulk_rna
Raw bulk RNA counts as a flat list of floats.
prepare_sample
Raw model request with base64 image data.
PreparedSample with the preprocessed tile tensor.
Raises:
ValueError- If the image cannot be decoded, transformed, or has the wrong shape.
forward_batch
prepare_sample. The entire GPU execution path (forward + synchronize + CPU transfer) is wrapped in error handling so that CUDA errors cannot escape.
Pre-validated samples with preprocessed tensors.
Original requests (for response metadata).
"prediction", "embedding", or "prediction_with_embedding". The last returns both the prediction and the embedding from the single forward pass.ModelResponse per sample, positionally aligned.
Raises:
_ForwardError- If the GPU forward pass, CUDA synchronization, or CPU transfer fails.
get_metadata
forward
List of tile requests.
(N, D).
