lib/accy/src/tensor/interpret/dispatch.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 pub fn primitive(comptime Value: type, impl: anytype, ctx: anytype) !?Value {
 2     const Impl = @TypeOf(impl.*);
 3     return switch (ctx.op.kind) {
 4         .parameter => if (comptime @hasDecl(Impl, "parameter")) try impl.parameter(ctx) else null,
 5         .constant => if (comptime @hasDecl(Impl, "constant")) try impl.constant(ctx) else null,
 6         .iota => if (comptime @hasDecl(Impl, "iota")) try impl.iota(ctx) else null,
 7         .broadcast => if (comptime @hasDecl(Impl, "broadcast")) try impl.broadcast(ctx) else null,
 8         .broadcast_in_dim => if (comptime @hasDecl(Impl, "broadcastInDim")) try impl.broadcastInDim(ctx) else null,
 9         .reshape => if (comptime @hasDecl(Impl, "reshape")) try impl.reshape(ctx) else null,
10         .transpose => if (comptime @hasDecl(Impl, "transpose")) try impl.transpose(ctx) else null,
11         .reduce => if (comptime @hasDecl(Impl, "reduce")) try impl.reduce(ctx) else null,
12         .gather => if (comptime @hasDecl(Impl, "gather")) try impl.gather(ctx) else null,
13         .scatter_add => if (comptime @hasDecl(Impl, "scatterAdd")) try impl.scatterAdd(ctx) else null,
14         .sparse_cross_entropy => if (comptime @hasDecl(Impl, "sparseCrossEntropy")) try impl.sparseCrossEntropy(ctx) else null,
15         .dot_general => if (comptime @hasDecl(Impl, "dotGeneral")) try impl.dotGeneral(ctx) else null,
16         .compare => if (comptime @hasDecl(Impl, "compare")) try impl.compare(ctx) else null,
17         .select => if (comptime @hasDecl(Impl, "select")) try impl.select(ctx) else null,
18         .custom_call => if (comptime @hasDecl(Impl, "customCall")) try impl.customCall(ctx) else null,
19         .scan => if (comptime @hasDecl(Impl, "scan")) try impl.scan(ctx) else null,
20         .projection => if (comptime @hasDecl(Impl, "projection")) try impl.projection(ctx) else null,
21         .unary => |unary| switch (unary.op) {
22             .neg => if (comptime @hasDecl(Impl, "neg")) try impl.neg(ctx) else null,
23             .abs => if (comptime @hasDecl(Impl, "abs")) try impl.abs(ctx) else null,
24             .exp => if (comptime @hasDecl(Impl, "exp")) try impl.exp(ctx) else null,
25             .log => if (comptime @hasDecl(Impl, "log")) try impl.log(ctx) else null,
26             .sqrt => if (comptime @hasDecl(Impl, "sqrt")) try impl.sqrt(ctx) else null,
27             .tanh => if (comptime @hasDecl(Impl, "tanh")) try impl.tanh(ctx) else null,
28             .sin => if (comptime @hasDecl(Impl, "sin")) try impl.sin(ctx) else null,
29             .cos => if (comptime @hasDecl(Impl, "cos")) try impl.cos(ctx) else null,
30             .tan => if (comptime @hasDecl(Impl, "tan")) try impl.tan(ctx) else null,
31         },
32         .binary => |binary| switch (binary.op) {
33             .add => if (comptime @hasDecl(Impl, "add")) try impl.add(ctx) else null,
34             .sub => if (comptime @hasDecl(Impl, "sub")) try impl.sub(ctx) else null,
35             .mul => if (comptime @hasDecl(Impl, "mul")) try impl.mul(ctx) else null,
36             .div => if (comptime @hasDecl(Impl, "div")) try impl.div(ctx) else null,
37             .max => if (comptime @hasDecl(Impl, "max")) try impl.max(ctx) else null,
38             .min => if (comptime @hasDecl(Impl, "min")) try impl.min(ctx) else null,
39             .pow => if (comptime @hasDecl(Impl, "pow")) try impl.pow(ctx) else null,
40         },
41     };
42 }