Class CustomOp
java.lang.Object
ai.beestream.jitnn.CustomOp
An operator defined outside jitNN: a kernel written in plain Java with TornadoVM's
KernelContext, an optional host reference and an optional gradient. Once built, it is
recorded, fused into the step's compiled plan, cached and differentiated like any built-in
operator.
Element-wise operators on FP32 tensors take one line:
public static void swish(KernelContext kc, FloatArray x, FloatArray y, int n) {
int i = kc.globalIdx;
if (i < n) {
float v = x.get(i);
y.set(i, v / (1.0f + TornadoMath.exp(-v)));
}
}
// d/dx x s(x) = y + s(x) (1 - y)
static final CustomOp SWISH = CustomOp.unary("swish", MyKernels::swish, v -> v / (1 + Math.exp(-v)))
.withGradient((x, y, gy) -> gy.mul(y.add(x.sigmoid().mul(y.neg().add(1)))));
Tensor y = SWISH.apply(x);
Anything else goes through builder(String): the output shapes, then the tasks the
operator adds to the plan, each with an explicit launch grid. Kernels take the
KernelContext first and bound-check their thread index; they never use @Parallel.
Names are global: compiled plans are cached by the operators' names and attributes, so two
operators cannot share a name. Kernel methods must not be named like a CUDA or OpenCL built-in
(exp, hypot, max, ...) or a C++ keyword (not, and, ...):
the generated kernel takes the method's name.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceGradients with respect to each input (null where none is needed), given the gradient of the first output.static interfaceThe gradients of an element-wise binary operator y = f(a, b): [dL/da, dL/db] from a, b, y and dL/dy.static final classDefines a custom operator step by step;CustomOp.Builder.build()registers its name.static interfaceAdds the operator's device tasks fornodeto the plan being built.static interfaceThe shape of an output, from the inputs and the integer attributes of the call.static interfaceComputesnodeon the host: inputs are readable, outputs are allocated.static interfaceThe gradient of an element-wise unary operator y = f(x): dL/dx from x, y and dL/dy. -
Method Summary
Modifier and TypeMethodDescriptionApplies the operator with integer and float attributes (part of the plan's cache key); returns its first output.Applies the operator; returns its first output.Tensor[]Applies the operator; returns all its outputs.static CustomOpbinary(String name, uk.ac.manchester.tornado.api.common.TornadoFunctions.Task5<uk.ac.manchester.tornado.api.KernelContext, uk.ac.manchester.tornado.api.types.arrays.FloatArray, uk.ac.manchester.tornado.api.types.arrays.FloatArray, uk.ac.manchester.tornado.api.types.arrays.FloatArray, Integer> kernel, DoubleBinaryOperator reference) An element-wise operator on two FP32 tensors of the same shape.static CustomOp.BuilderStarts the definition of an operator namedname.name()The operator's name, unique among all operators.toString()static CustomOpunary(String name, uk.ac.manchester.tornado.api.common.TornadoFunctions.Task4<uk.ac.manchester.tornado.api.KernelContext, uk.ac.manchester.tornado.api.types.arrays.FloatArray, uk.ac.manchester.tornado.api.types.arrays.FloatArray, Integer> kernel, DoubleUnaryOperator reference) An element-wise operator on one FP32 tensor.withGradient(CustomOp.BinaryGradient gradient) This element-wise binary operator with a gradient.withGradient(CustomOp.UnaryGradient gradient) This element-wise unary operator with a gradient.
-
Method Details
-
builder
Starts the definition of an operator namedname. -
unary
public static CustomOp unary(String name, uk.ac.manchester.tornado.api.common.TornadoFunctions.Task4<uk.ac.manchester.tornado.api.KernelContext, uk.ac.manchester.tornado.api.types.arrays.FloatArray, uk.ac.manchester.tornado.api.types.arrays.FloatArray, Integer> kernel, DoubleUnaryOperator reference) An element-wise operator on one FP32 tensor.kernelis called askernel(kc, x, y, n)on a 1-D grid of at least n threads;referencecomputes one value on the host. -
binary
public static CustomOp binary(String name, uk.ac.manchester.tornado.api.common.TornadoFunctions.Task5<uk.ac.manchester.tornado.api.KernelContext, uk.ac.manchester.tornado.api.types.arrays.FloatArray, uk.ac.manchester.tornado.api.types.arrays.FloatArray, uk.ac.manchester.tornado.api.types.arrays.FloatArray, Integer> kernel, DoubleBinaryOperator reference) An element-wise operator on two FP32 tensors of the same shape.kernelis called askernel(kc, a, b, y, n)on a 1-D grid of at least n threads. -
withGradient
This element-wise unary operator with a gradient. -
withGradient
This element-wise binary operator with a gradient. -
name
The operator's name, unique among all operators. -
apply
-
apply
-
applyAll
-
toString
-