Function that prepares model's input and output for serialization.
(batch: Dict[str, Any],
auxiliary_output: Dict[str, Any])
| 201 | """ |
| 202 | |
| 203 | def postprocess_fn(batch: Dict[str, Any], |
| 204 | auxiliary_output: Dict[str, Any]) -> Dict[str, Any]: |
| 205 | """Function that prepares model's input and output for serialization.""" |
| 206 | |
| 207 | features = {} |
| 208 | for dict_of_features in [batch, auxiliary_output]: |
| 209 | for key, value in dict_of_features.items(): |
| 210 | convert_to_list = ( |
| 211 | isinstance(value, Array) or isinstance(value, np.ndarray)) |
| 212 | if convert_to_list and value.dtype == jnp.bfloat16: |
| 213 | # bfloat16 is not JSON serializable => convert to float32. |
| 214 | features[key] = value.astype(jnp.float32).tolist() |
| 215 | elif convert_to_list: |
| 216 | # Cannot serialize numpy / jax arrays to json => convert to list. |
| 217 | features[key] = value.tolist() |
| 218 | else: |
| 219 | features[key] = value |
| 220 | |
| 221 | return features |
| 222 | |
| 223 | return postprocess_fn |
no outgoing calls