Skip to content
fn

dropout_stateful_op

→std::pair<TensorImplPtr, TensorImplPtr>
int dropout_stateful_op(const int & x, const int & state_in, double p, bool training, Generator * gen)

Stateful inverted dropout for the compile path — sibling of dropout_op that explicitly threads an MPSGraph-Philox state tensor as a second input and returns (y, state_out).

In eager mode the function delegates to dropout_op for the actual masking (so the result distribution matches the standard dropout op identically) and returns a clone of state_in as the state-out tensor — Lucid's eager Generator already advances per call, so the state buffer is purely a placeholder that exists for the trace recording. Suppresses any nested tracer recording from the inner dropout_op call so the trace contains exactly one "dropout_stateful" op node (not a nested "dropout" underneath it).

In the compile path the matching MPSGraph emitter consumes the captured op node by calling randomTensorWithShape — the 2-output stateful Philox API — using state_in to seed the RNG and binding the new state to state_out. Across dispatches the state buffer is rotated by the executable (either as an in/out feed pair or promoted to an MPSGraph variable via compile_generic_fused_step_with_vars), giving genuinely per-dispatch varying masks where the stateless seed-only path produces dispatch-deterministic ones.

Parameters

xTensorImplPtr
Input activations of any shape.
state_inTensorImplPtr
Philox-4x32 state tensor (int32[7]). Initial values are derived from Lucid's Generator on the Python side (one fresh state per dropout call site at trace time); the compile path mutates the buffer in-place across dispatches.
pdouble
Drop probability in [0,1)[0, 1).
trainingbool
When false, the eager forward is a pass-through identity (the trace still records the op so the compile path can decide whether to emit the masked or identity branch).
Optional explicit RNG used by the eager fallback; nullptr selects default_generator(). Ignored by the compile path.

Returns

std::pair<TensorImplPtr, TensorImplPtr>

(y, state_out) — masked output of the same shape and dtype as x; state_out is the same shape/dtype as state_in and (in eager mode) carries a verbatim clone of its contents.