poulpy/poulpy-hal/docs/backend_safety_contract.md
poulpy-hal/docs/backend_safety_contract.mdRaw
1Implementors must uphold all of the following for **every** call:
2
3* **Memory domains**: Pointers produced by to_ref() / to_mut() must be valid
4 in the target execution domain for Self (e.g., CPU host memory for CPU,
5 device memory for a specific GPU). If host↔device transfers are required,
6 perform them inside the implementation; do not assume the caller synchronized.
7
8* **Alignment & layout**: All data must match the layout, stride, and element
9 size expected by the kernel. size(), rows(), cols_in(), cols_out(),
10 n(), etc... must be interpreted identically to the reference CPU implementation.
11
12* **Scratch lifetime**: Any scratch obtained from scratch.tmp_slice(...) (or a
13 backend-specific variant) must remain valid for the duration of the call; it
14 may be reused by the caller afterwards. Do not retain pointers past return.
15
16* **Synchronization**: The call must appear **logically synchronous** to the
17 caller. If you enqueue asynchronous work (e.g., CUDA streams), you must
18 ensure completion before returning or clearly document and implement a
19 synchronization contract used by all backends consistently.
20
21* **Aliasing & overlaps**: If res, a, b, etc... alias or overlap in ways
22 that violate your kernel’s requirements, you must either handle safely or reject
23 with a defined error path (e.g., debug assert). Never trigger UB.
24
25* **Numerical contract**: For modular/integer arithmetic, results must be
26 bit-exact to the specification. For floating-point, any permitted tolerance
27 must be documented and consistent with the crate’s guarantees.