espnet3.contract.inference.InferenceContract
About 2 min
espnet3.contract.inference.InferenceContract
class espnet3.contract.inference.InferenceContract(inputs: tuple[Field, ...], outputs: tuple[Field, ...], imports: tuple[str, ...] = ())
Bases: object
Input and output declarations shared by all inference entry points.
imports : tuple[str, ...] = ()
inputs : tuple[Field, ...]
outputs : tuple[Field, ...]
prepare_input(sample: Mapping[str, Any], model: Any) → dict[str, Any]
Validate and normalize one copy of a sample before batching.
- Parameters:
- sample – One dataset item holding the declared input fields.
- model – The backend, for what it knows (its
sample_rate).
- Returns: A copy of
samplewhose declared inputs are checked and converted to what the backend receives. - Raises:
- TypeError – If
sampleis not a mapping. - KeyError – If a required input is missing.
- TypeError – If
######### Examples
>>> contract = load_inference_contract(
... {
... "inputs": {"text": {"kind": "text"}},
... "outputs": {"text": {"kind": "text"}},
... }
... )
>>> contract.prepare_input({"text": "hi"}, model=None)
{'text': 'hi'}prepare_output(result: Any, model: Any, , idx: Any, idx_key: str, sample: Mapping) → dict[str, Any]
Validate named backend outputs and attach the sample identifier.
- Parameters:
- result – What the backend returned: a mapping of output names.
- model – The backend, for what it knows (its
sample_rate). - idx – The dataset index of the sample.
- idx_key – The name under which the sample identifier is attached.
- sample – The original dataset item, a source for
idx_key.
- Returns: The checked outputs plus the sample identifier under
idx_key. - Raises:
- TypeError – If
resultis not a mapping. - ValueError – If
resulthas an undeclared output. - KeyError – If a declared output is missing.
- TypeError – If
######### Examples
>>> contract = load_inference_contract(
... {
... "inputs": {"text": {"kind": "text"}},
... "outputs": {"text": {"kind": "text"}},
... }
... )
>>> contract.prepare_output(
... {"text": "hi"}, None, idx=0, idx_key="utt_id", sample={}
... )
{'text': 'hi', 'utt_id': '0'}validate_input_keys(input_key: Any) → None
Ensure every backend argument has one declared input field.
- Parameters:input_key – The configured
input_key: one name or a list. - Returns: None.
- Raises:ValueError – If
input_keyis missing or differs from the declared inputs.
######### Examples
>>> contract = load_inference_contract(
... {
... "inputs": {"text": {"kind": "text"}},
... "outputs": {"text": {"kind": "text"}},
... }
... )
>>> contract.validate_input_keys("text")
>>> contract.validate_input_keys("speech")
Traceback (most recent call last):
ValueError: inference input_key must match contract.inputs; ...