snoozeml.inference — Tensor Serialization & Responses#
Tensor payload validation, wire serialization, and inference response types for SnoozeML.
- class snoozeml.inference.TensorDataType(*values)[source]#
-
Supported tensor element data types.
- FP32 = 'fp32'#
- class snoozeml.inference.TensorInput(name, shape, data)[source]#
Bases:
objectValidated multi-dimensional input tensor ready for REST or gRPC serialization.
- classmethod from_array(name, array, shape=None)[source]#
Constructs a
TensorInputfrom a NumPy array, PyTorchTensor, or nested sequence.- Parameters:
- Returns:
Validated
TensorInput.- Raises:
InvalidInputError – If the shape is empty, contains non-positive dimensions, or does not match the element count.
- Return type:
- classmethod from_dict(item)[source]#
Constructs a
TensorInputfrom a dictionary{"name": ..., "shape": ..., "data": ...}.- Parameters:
- Return type:
- class snoozeml.inference.TensorOutput(name, shape, data)[source]#
Bases:
objectMulti-dimensional output tensor returned from model inference.
- to_numpy()[source]#
Returns the output tensor as a reshaped
np.ndarrayof dtypefloat32.- Return type:
- as_numpy()[source]#
Alias for
to_numpy().- Return type:
- to_torch(device=None)[source]#
Returns the output tensor as a PyTorch
torch.Tensor(dtype=torch.float32).- Parameters:
device (str | None) – Optional PyTorch device string (e.g.
"cpu","cuda","mps").- Returns:
torch.Tensormatchingshape.- Raises:
InvalidInputError – If
torchis not installed.- Return type:
- to_dict()[source]#
Returns a dictionary
{"name": ..., "shape": ..., "data": ...}matching the REST format.
- classmethod from_rest_dict(item)[source]#
Constructs a
TensorOutputfrom a REST JSON output tensor dictionary.- Parameters:
- Return type:
- classmethod from_proto(tensor_proto)[source]#
Constructs a
TensorOutputfrom a ProtobufTensorDatamessage.- Parameters:
tensor_proto (Any)
- Return type:
- class snoozeml.inference.InferenceResponse(model_id, outputs, duration_ms)[source]#
Bases:
objectStructured result returned by
snoozeml.SnoozeClient.infer().- Parameters:
model_id (str)
outputs (List[TensorOutput])
duration_ms (float)
- outputs#
Ordered list of
TensorOutputobjects (supports name indexing).- Type:
- outputs: List[TensorOutput]#
- get_tensor(name=None)[source]#
Retrieves a
TensorOutputby binding name (or the first output ifnameisNone).- Parameters:
name (str | None) – Optional name of the output tensor node (e.g.
"logits","output"). IfNone, returns the first output tensor.- Returns:
Matching
TensorOutput.- Raises:
KeyError – If no output tensors exist or
nameis not found.- Return type:
- get_numpy(name=None)[source]#
Returns the output tensor as a reshaped
np.ndarray(dtype=np.float32).
- get_torch(name=None, device=None)[source]#
Returns the output tensor as a reshaped PyTorch
torch.Tensor(dtype=torch.float32).
- classmethod from_rest_dict(data, default_model_id='')[source]#
Constructs an
InferenceResponsefrom a REST JSON payload.- Parameters:
- Return type:
- classmethod from_proto(proto_resp)[source]#
Constructs an
InferenceResponsefrom a ProtobufModelInferResponse.- Parameters:
proto_resp (ModelInferResponse)
- Return type:
- snoozeml.inference.normalize_inputs(inputs, default_input_name='input')[source]#
Normalizes flexible user input representations into a validated
List[TensorInput].- Supported formats for
inputs: Dict[str, np.ndarray | Sequence | TensorInput]— e.g.{"input_ids": arr}List[TensorInput | Dict[str, Any]]— e.g.[{"name": "input", "shape": [1, 3], "data": [1, 2, 3]}]Single
TensorInputSingle
np.ndarray(assigned binding namedefault_input_name, default"input")
- Parameters:
inputs (Mapping[str, Any | Sequence[Any] | TensorInput] | Sequence[TensorInput | Mapping[str, Any]] | TensorInput | Any) – Input tensors in any supported format.
default_input_name (str) – Binding name used when a single bare
np.ndarrayis passed.
- Returns:
Non-empty list of validated
TensorInputobjects.- Raises:
InvalidInputError – If
inputsis empty or any tensor fails validation.- Return type:
- Supported formats for
- snoozeml.inference.slice_inputs_batch(inputs, batch_size, default_input_name='input')[source]#
Splits a dataset of input tensors along
Axis(0)into chunks ofbatch_size.- Parameters:
inputs (Mapping[str, Any | Sequence[Any] | TensorInput] | Sequence[TensorInput | Mapping[str, Any]] | TensorInput | Any) – Input tensors in any supported format.
batch_size (int) – Maximum batch size along
Axis(0)per chunk.default_input_name (str) – Binding name when a bare array is passed.
- Returns:
List of
List[TensorInput]chunks ready for sequential or concurrentinfer()calls.- Raises:
InvalidInputError – If
batch_size <= 0or input tensors have mismatchedAxis(0)lengths.- Return type:
- snoozeml.inference.concat_inference_responses(responses)[source]#
Concatenates a sequence of chunked
InferenceResponseobjects alongAxis(0).- Parameters:
responses (Sequence[InferenceResponse])
- Return type: