Attr
#struct AttrOne node attribute, such as alpha.
| Name | Type | Description |
|---|---|---|
name | str = "" | The name. |
kind | int = 0 | The value kind in the ONNX format: 1 float, 2 integer, 3 string, 4 tensor, 6 and 7 lists. |
f | f64 = 0 | The float value. |
i | i64 = 0 | The integer value. |
s | str = "" | The string value. |
t | tensor.Tensor | The tensor value. |
floats | Vector<f64> | The float list. |
ints | Vector<i64> | The integer list. |
import ai.onnx
import ai.tensor
import compress
import encoding.protobuf
import archive
import os
func submessage(b &: compress.ByteBuf, field: int, message: compress.ByteBuf):
protobuf.WriteTag(b, field, protobuf.WT_LEN)
protobuf.WriteLenPrefixedBytes(b, message.data, compress.BufLen(message))
end
func text(b &: compress.ByteBuf, field: int, value: str):
protobuf.WriteTag(b, field, protobuf.WT_LEN)
protobuf.WriteLenPrefixedStr(b, value)
end
func number(b &: compress.ByteBuf, field: int, value: i64):
protobuf.WriteTag(b, field, protobuf.WT_VARINT)
protobuf.WriteVarint(b, value as u64)
end
func node(b &: compress.ByteBuf, op: str, input1: str, input2: str, output: str):
text(b, 1, input1)
if input2 != "":
text(b, 1, input2)
end
text(b, 2, output)
text(b, 4, op)
end
func model(m &: compress.ByteBuf):
mut graph := compress.NewByteBuf()
mut mul := compress.NewByteBuf()
node(mul, "Mul", "x", "w", "h")
submessage(graph, 1, mul)
mut relu := compress.NewByteBuf()
node(relu, "LeakyRelu", "h", "", "y")
mut alpha := compress.NewByteBuf()
text(alpha, 1, "alpha")
number(alpha, 20, 1)
protobuf.WriteTag(alpha, 2, protobuf.WT_FIXED32)
protobuf.WriteFloat(alpha, 0.1)
submessage(relu, 5, alpha)
submessage(graph, 1, relu)
mut weight := compress.NewByteBuf()
number(weight, 1, 3)
number(weight, 2, 1)
mut payload := compress.NewByteBuf()
each factor in [2.0, 2.0, 2.0]:
protobuf.WriteFloat(payload, factor)
end
submessage(weight, 4, payload)
text(weight, 8, "w")
submessage(graph, 5, weight)
mut input_x := compress.NewByteBuf()
text(input_x, 1, "x")
submessage(graph, 11, input_x)
mut output_y := compress.NewByteBuf()
text(output_y, 1, "y")
submessage(graph, 12, output_y)
number(m, 1, 8)
submessage(m, 7, graph)
end
func main:
mut buffer := compress.NewByteBuf()
model(buffer)
mut good := false
mut m := onnx.ParseOnnx(buffer.data, compress.BufLen(buffer), good)
println good, m.ok, m.ir_version, m.nodes.Len(), m.inits.Len(), m.init_names.Get(0)
mut relu := m.nodes.Get(1)
println relu.op, relu.inputs.Get(0), relu.outputs.Get(0), relu.AttrIndex("alpha")
println (relu.AttrFloat("alpha", 0.0) * 100.0 + 0.5) as int, relu.AttrInt("axis", -1)
attr := relu.attrs.Get(0)
println attr.name, attr.kind
println m.FeedNames().Len(), m.inputs.Get(0), m.outputs.Get(0)
mut shape := tensor.Shape2(1, 3)
mut x := tensor.Full(shape, -1.5)
x.SetAt(2, 4.0)
mut inputs := Vector {} as Vector<tensor.Tensor>
inputs.Push(x)
mut outputs := onnx.Run(m, inputs, good)
mut y := outputs.Get(0)
println good, (y.At(0) * 100.0 - 0.5) as int, y.At(2)
path := os.TempDir() + "/salam_tiny_en.onnx"
_ := archive.WriteBinaryFileBuf(path, buffer)
mut m2 := onnx.LoadOnnx(path, good)
println good, m2.nodes.Len()
m2.Free()
m.Free()
compress.BufFree(buffer)
endtrue true 8 2 1 w LeakyRelu h y 0 10 -1 alpha 1 1 x y true -30 8 true 2