NumPy einsum optimize: was casting=safe enforced on the final out write?
Verify rejection of a forbidden final cast so optimization cannot change permission for data loss.
The final write must enforce policy
Verify rejection of a forbidden final cast so optimization cannot change permission for data loss. Supply Truthound with the rejection outcome as well as values. A truncated integer within normal range does not validate an out buffer that has already been overwritten. [1, 2, 3]
Rejection changes with output order
NumPy #33071 reports optimized einsum bypassing casting rules on final out writes, truncating floats or dropping imaginary parts. It distinguishes reshape or transpose paths from direct matmul out handling. A TypeError for one output-index order does not establish enforcement for every order; the proposed repair remains separate from its deployment. [1]
Preserve the original out
Record input, computation and out dtypes together with casting policy. Compare observed rejection of forbidden requests, withholding buffers from calls that were not rejected. Preserve the original out buffer and externally observe completion or failure, then reconcile actual values and writes for calls without an exception. [1, 3]
The counterexample writes 1.5 as 1
The illustrative policy forbids writing float64 value 1.5 into int64 out with casting=safe. Expected rejection is true. Rejecting it passes; truncating to 1 and returning success gives false and fails. Do not reconstruct the original 1.5 by guessing from the truncated 1. [1, 3]
Check a measured rejection state
Truthound Core Python SDK 3.3.2 can compare prepared required_rejection and unsafe_cast_rejected using ColumnPairValidator with relationship="==". The operator supplies rejection measurements; the check examines evidence after the call. The SDK does not intercept NumPy memory writes, and one true outcome does not establish safe casting for every input. [1, 2, 3]
An alternative using a temporary output
Calculate into an allowed temporary dtype and copy under an explicit policy, or compare a verified non-optimized path. Test optimization, output-index order and complex-input boundaries together. Approve the out-buffer handoff only after the value and policy checks each have evidence. [1, 3]
Application conditions
Preserve the requested casting rule and original out buffer, observing actual completion or rejection externally.
Alternatives
Use an allowed temporary dtype with an explicitly checked copy or compare a verified non-optimized path.
Limitations
Core compares measured rejection, does not block NumPy memory writes and cannot prove all casting safe from one true result.
Evidence
- Event checked
- Subject checked
- Modified
