Maybe casts the inputs to the compute dtype. If self._compute_dtype is floating-point, and self_autocast is True, floating-point inputs are casted to self._compute_dtype. Args: inputs: Input tensor, or structure of input tensors. Returns: `inputs`, but tensors may have
(self, inputs)
| 1783 | return self._dtype_policy.compute_dtype |
| 1784 | |
| 1785 | def _maybe_cast_inputs(self, inputs): |
| 1786 | """Maybe casts the inputs to the compute dtype. |
| 1787 | |
| 1788 | If self._compute_dtype is floating-point, and self_autocast is True, |
| 1789 | floating-point inputs are casted to self._compute_dtype. |
| 1790 | |
| 1791 | Args: |
| 1792 | inputs: Input tensor, or structure of input tensors. |
| 1793 | |
| 1794 | Returns: |
| 1795 | `inputs`, but tensors may have been casted to self._compute_dtype |
| 1796 | """ |
| 1797 | compute_dtype = self._compute_dtype |
| 1798 | if (self._autocast and compute_dtype and |
| 1799 | dtypes.as_dtype(compute_dtype).is_floating): |
| 1800 | def f(x): |
| 1801 | cast_types = (ops.Tensor, sparse_tensor.SparseTensor, |
| 1802 | ragged_tensor.RaggedTensor) |
| 1803 | if (isinstance(x, cast_types) and x.dtype.is_floating and |
| 1804 | x.dtype.base_dtype.name != compute_dtype): |
| 1805 | if self._dtype_defaulted_to_floatx: |
| 1806 | self._warn_about_input_casting(x.dtype.base_dtype) |
| 1807 | return math_ops.cast(x, compute_dtype) |
| 1808 | else: |
| 1809 | return x |
| 1810 | return nest.map_structure(f, inputs) |
| 1811 | else: |
| 1812 | return inputs |
| 1813 | |
| 1814 | def _warn_about_input_casting(self, input_dtype): |
| 1815 | # self._already_warned_about_input_casting is only retrieved or set in this |