Advertisement
Guest User

Untitled

a guest
Dec 4th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. const std = @import("std");
  2.  
  3. pub fn Tensor(comptime T: type) type {
  4. return struct {
  5. shape: []const u64,
  6. };
  7. }
  8.  
  9. pub fn Constant(comptime T: type) type {
  10. return struct {
  11. value: T,
  12. };
  13. }
  14.  
  15. pub fn Operation(comptime T: type) type {
  16. return struct {
  17. left: Tensor(T),
  18. right: Tensor(T),
  19. };
  20. }
  21.  
  22. pub fn Graph(comptime T: type) type {
  23. return struct {
  24. elementType: type = T,
  25. constants: std.ArrayList(Constant(T)),
  26. operations: std.ArrayList(Operation(T)),
  27.  
  28. pub fn init(allocator: *std.mem.Allocator) Graph(T) {
  29. return .{
  30. .constants = std.ArrayList(Constant(T)).init(allocator),
  31. .operations = std.ArrayList(Operation(T)).init(allocator),
  32. };
  33. }
  34. };
  35. }
  36.  
  37. pub fn constant(comptime T: type, graph: *Graph(T), value: T) !Tensor(T) {
  38. try graph.constants.append(.{ .value = value });
  39. return Tensor(T){ .shape = &[_]u64{} };
  40. }
  41.  
  42. pub fn add(comptime T: type, graph: *Graph(T), x: Tensor(T), y: Tensor(T)) !Tensor(T) {
  43. try graph.operations.append(.{ .left = x, .right = y });
  44. return Tensor(T){ .shape = &[_]u64{} };
  45. }
  46.  
  47. test "create graph" {
  48. var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
  49. defer arena.deinit();
  50. const allocator = &arena.allocator;
  51.  
  52. const T = f64;
  53. var graph = Graph(T).init(allocator);
  54. const x = try constant(T, &graph, 5);
  55. const y = try constant(T, &graph, 10);
  56. const z = try add(T, &graph, x, y);
  57. }
  58.  
  59. pub fn main() !void {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement