Skip to content
fn

promote_for_schema

→TensorImplPtr
int promote_for_schema(const OpSchema & schema, const int & a)

Cast an input to the dtype its schema asks for.

UnaryKernel, BinaryKernel and ReduceKernel each open with a SchemaGuard and cast to effective_dtype(), so every op built on them inherits both the autocast policy and the integer promotion that goes with real_valued / ForceFP32. An op that assembles its own forward — softmax, variance, the activations that need an extra term — has to ask for the same thing, and the ones that did not computed in the input's own dtype.

For an integer input that is not an approximation of the answer, it is a different answer: softmax of an int32 tensor came back [0, 0, 0] on Metal and var came back 0, both integer-typed and both silently wrong, while the CPU raised NotImplementedError for the same call. Every one of those schemas already said the result was float; nothing was reading it.

astype_op rather than maybe_cast_for_kernel, for the reason BatchNorm gives: the cast tensor carries an AstypeBackward, so a float input cast under an autocast scope keeps its place in the graph instead of silently detaching.

Parameters

schemaconst OpSchema&
The op's schema — supplies the AMP policy and real_valued.
aconst TensorImplPtr&
The input.

Returns

TensorImplPtr

a unchanged when it is already at the effective dtype, otherwise a cast copy.

Notes

The SchemaGuard here dies when this returns, so the op's body runs with autocast still on — unlike a kernel-template op, whose guard lives as long as its forward. Harmless while the body calls the backend directly; a body built from other *_op calls has them apply their own rows (softplus). Holding a guard for the body is LCD-500-E2.