Ahead-of-time tensor graph compilation made simple.
Project details
Zig Graph Compiler (ZGC) streamlines numerical computation with an ahead-of-time approach. By leveraging Zig's compile-time features, it crafts optimized, allocation-free runtimes for various numerical pipelines, from neural networks to simulations, achieving performance close to handcrafted implementations.
Zig Graph Compiler (ZGC) is an ahead-of-time tensor graph compiler designed to generate optimized and allocation-free runtimes for numerical computations. Utilizing Zig's comptime feature, ZGC defines and optimizes a graph architecture, creating a fixed execution graph that is emitted as a specialized type before the actual program execution begins. This offers significant performance benefits by eliminating the need for runtime graph interpretation and enabling compile-time validation.
ZGC provides a streamlined approach to build complex numerical pipelines, ensuring high visibility and control over execution while leveraging Zig’s cross-compilation capabilities.
Using the DefinitionBuilder, users can define tensor graphs. Here’s a basic example:
const std = @import("std");
const zgc = @import("zgc");
const Sources = enum(usize) { input, weights };
const Definition = zgc.DefinitionBuilder;
fn define(builder: *Definition) void {
const sources = builder.sources(Sources);
const input = sources.input(.input, .f32, &.{ 4, 8 });
const weights = sources.parameter(.weights, .f32, &.{ 8, 16 });
builder.output(
builder.relu(
builder.matmul(input, weights)
)
);
}
const definition = blk: {
var builder = Definition.init();
define(&builder);
break :blk builder.finish();
};
const MyExecutable = definition.executable();
pub fn main() !void {
var runtime = MyExecutable.Runtime.init();
const input_values: [4 * 8]f32 = @splat(1);
const weight_values: [8 * 16]f32 = @splat(0.25);
try runtime.copyInput(.input, &input_values);
try runtime.copySource(.weights, &weight_values);
runtime.run();
const output = runtime.outputView(0);
std.debug.print("shape={any} data={any}\n", .{ output.shape, output.storage });
}
ZGC supports multiple strategies for selecting executables at compile time. Using options like fast, memory, and small, users can tailor performance based on needs:
const FastExecutable = definition.executableWith(.{ .target = .fast });
const MemoryExecutable = definition.executableWith(.{ .target = .memory });
const SmallExecutable = definition.executableWith(.{ .target = .small });
ZGC's sophisticated design prioritizes efficient numerical computation and offers extensive documentation, benchmarks, and examples to guide users: from defining tensor graphs to executing complex models.
For further information and to explore more detailed topics, refer to the documentation index. Interested users can also check out examples like Conway's Game of Life and MNIST digit classifier.
Comments
0Start the conversation
Share the first comment.