public static (Tile tile, Tensor <int> input, Tensor <float> lookup, Tensor <float> res) LookupTile() { var input = new Symbol("input", 0); input.Shape = new Shape(ElementKind.Int32, new[] { 16 }); var lookup = new Symbol("lookup", 1); lookup.Shape = new Shape(ElementKind.Float32, new[] { 4 }); var res = new Symbol("res", 2); res.Shape = new Shape(ElementKind.Float32, new[] { 16 }); var i = new Index("i"); i.Ranges = new[] { new Range(0, 16) }; var statement = new Statement( StatementKind.Assign, // left new Element(res, new[] { new IndexExpression(i) }), // right new ElementExpression( new Element(lookup, new[] { new IndexExpression( new Element(input, new[] { new IndexExpression(i), }) ) }))); var tile = new Tile("lookup", new[] { statement }); var allocator = new TensorAllocator(); var tinput = (Tensor <int>)allocator.Create(input.Shape, "input"); var si = tinput.Buffer.Span; for (int n = 0; n < 16; n++) { si[n] = n % 4; } var tlookup = (Tensor <float>)allocator.Create(lookup.Shape, "lookup"); var sl = tlookup.Buffer.Span; for (int m = 0; m < 4; m++) { sl[m] = -m; } var tres = (Tensor <float>)allocator.Create(res.Shape, "res"); return(tile, tinput, tlookup, tres); }
public static (Tile tile, Tensor <int> input, Tensor <int> res) TransposeTile() { var input = new Symbol("input", 0); input.Shape = new Shape(ElementKind.Int32, new[] { 16, 16 }); var res = new Symbol("res", 1); res.Shape = new Shape(ElementKind.Int32, new[] { 16, 16 }); var i = new Index("i"); i.Ranges = new[] { new Range(0, 16) }; var j = new Index("j"); j.Ranges = new[] { new Range(0, 16) }; var statement = new Statement( StatementKind.Assign, // left new Element(res, new[] { new IndexExpression(j), new IndexExpression(i) }), // right new ElementExpression( new Element(input, new[] { new IndexExpression(i), new IndexExpression(j), }))); var tile = new Tile("transpose", new[] { statement }); var allocator = new TensorAllocator(); var tinput = (Tensor <int>)allocator.Create(input.Shape, "input"); var s = tinput.Buffer.Span; for (int m = 0; m < 16; m++) { for (int n = 0; n < 16; n++) { s[m * 16 + n] = n - m; } } var tres = (Tensor <int>)allocator.Create(input.Shape, "res"); return(tile, tinput, tres); }
public static (Tile tile, Tensor <int> input, Tensor <int> res) SimpleMaxTile() { var input = new Symbol("input", 0); input.Shape = new Shape(ElementKind.Int32, new[] { 16 }); var res = new Symbol("res", 1); res.Shape = new Shape(ElementKind.Int32, new[] { 1 }); var i = new Index("i"); i.Ranges = new[] { new Range(0, 16) }; var statement = new Statement( StatementKind.Max, // left new Element(res, new[] { new IndexExpression(0) }), // right new ElementExpression( new Element(input, new[] { new IndexExpression(i) }))); var tile = new Tile("max", new[] { statement }); var allocator = new TensorAllocator(); var tinput = (Tensor <int>)allocator.Create(input.Shape, "input"); var s = tinput.Buffer.Span; for (int n = 0; n < 16; n++) { s[n] = n + 2; } var tres = (Tensor <int>)allocator.Create(res.Shape, "res"); return(tile, tinput, tres); }
public static (Tile, Tensor <float> a, Tensor <float> x, Tensor <float> b, Tensor <float> res) LinearTile() { // linear 2D layer: res[i] = sum(a[i,j] * x[j] + b[i]) var a = new Symbol("A", 0); a.Shape = new Shape(ElementKind.Float32, new[] { 16, 32 }); var x = new Symbol("x", 1); x.Shape = new Shape(ElementKind.Float32, new[] { 32 }); var b = new Symbol("b", 2); b.Shape = new Shape(ElementKind.Float32, new[] { 16 }); var res = new Symbol("res", 3); res.Shape = new Shape(ElementKind.Float32, new[] { 16 }); var i = new Index("i"); i.Ranges = new[] { new Range(0, 16) }; var j = new Index("j"); j.Ranges = new[] { new Range(0, 32) }; var statement = new Statement(StatementKind.AddSum, // left new Element(res, new[] { new IndexExpression(i) }), // right new ElementExpression(BinaryExpressionKind.Add, new ElementExpression(BinaryExpressionKind.Multiply, new ElementExpression(new Element(a, new[] { new IndexExpression(i), new IndexExpression(j) })), new ElementExpression(new Element(x, new[] { new IndexExpression(j) }))), new ElementExpression(new Element(b, new[] { new IndexExpression(i) })))); var tile = new Tile("linear", new[] { statement }); var allocator = new TensorAllocator(); var ta = (Tensor <float>)allocator.Create(a.Shape, "a"); var sa = ta.Buffer.Span; for (int m = 0; m < 16; m++) { for (int n = 0; n < 32; n++) { sa[m * 32 + n] = (n + m) % 2; } } var tx = (Tensor <float>)allocator.Create(x.Shape, "x"); var sx = tx.Buffer.Span; for (int n = 0; n < 32; n++) { sx[n] = n % 3; } var tb = (Tensor <float>)allocator.Create(b.Shape, "b"); var sb = tb.Buffer.Span; for (int m = 0; m < 16; m++) { sb[m] = m % 5; } var tres = (Tensor <float>)allocator.Create(res.Shape, "res"); return(tile, ta, tx, tb, tres); }