tensors.aug
CPU tensors with PyTorch · Source and specification
Files in this project
Code
// aug-spec: "tensors.aug.md" explains this file. Read it before changes; refresh with aug spec.
import Tensor and TensorError and tensor and add and sum and values from "https://github.com/GreenPandaStudios/aug-pytorch#e2b74b1968fb11972e260ef3796a1cd849c1f702"
/** Add two CPU tensors using LibTorch and return the sum of their elements. */
calculate() returns float unless TensorError:
own Tensor left = tensor(values=[1.0, 2.0, 3.0])
own Tensor right = tensor(values=[4.0, 5.0, 6.0])
own Tensor result = add(left, right)
return sum(tensor=result)
test calculate:
when "cpu":
it "adds_and_reads_real_tensors":
own Tensor left = tensor(values=[1.0, 2.0, 3.0])
own Tensor right = tensor(values=[4.0, 5.0, 6.0])
own Tensor result = add(left, right)
List<float> output = values(tensor=result)
assert(output.length() == 3)
assert(output.get(index=0) == 5.0)
assert(output.get(index=1) == 7.0)
assert(output.get(index=2) == 9.0)
assert(sum(tensor=result) == 21.0)
assert(calculate() == 21.0)// aug-spec: "tensors.aug.md" explains this file. Read it before changes; refresh with aug spec.
import Tensor and TensorError and tensor and add and sum and values from "https://github.com/GreenPandaStudios/aug-pytorch#e2b74b1968fb11972e260ef3796a1cd849c1f702"
/** Add two CPU tensors using LibTorch and return the sum of their elements. */
calculate() returns float unless TensorError {
own Tensor left = tensor(values=[1.0, 2.0, 3.0])
own Tensor right = tensor(values=[4.0, 5.0, 6.0])
own Tensor result = add(left, right)
return sum(tensor=result)
}
test calculate {
when "cpu" {
it "adds_and_reads_real_tensors" {
own Tensor left = tensor(values=[1.0, 2.0, 3.0])
own Tensor right = tensor(values=[4.0, 5.0, 6.0])
own Tensor result = add(left, right)
List<float> output = values(tensor=result)
assert(output.length() == 3)
assert(output.get(index=0) == 5.0)
assert(output.get(index=1) == 7.0)
assert(output.get(index=2) == 9.0)
assert(sum(tensor=result) == 21.0)
assert(calculate() == 21.0)
}
}
}Compiled specification
calculate · source
Add two CPU tensors using LibTorch and return the sum of their elements. Failures can raise TensorError.
It calls tensor with values from a list containing 1.0, 2.0, 3.0 and stores the result in owned left (Tensor). It calls tensor with values from a list containing 4.0, 5.0, 6.0 and stores the result in owned right (Tensor). It calls add with left and right and stores the result in owned result (Tensor). It returns sum with tensor from result. source
test calculate · source
Tests calculate. Each case gets fresh setup and dependencies.
cpu
It calls tensor with values from a list containing 1.0, 2.0, 3.0 and stores the result in owned left (Tensor). It calls tensor with values from a list containing 4.0, 5.0, 6.0 and stores the result in owned right (Tensor). It calls add with left and right and stores the result in owned result (Tensor). It sets output of type List<float> to values with tensor from result. source
The test requires the number of elements in output equals 3. The test requires the item at index 0 in output equals 5.0. The test requires the item at index 1 in output equals 7.0. The test requires the item at index 2 in output equals 9.0. source
The test requires sum with tensor from result equals 21.0. The test requires calculate equals 21.0. source
Dependencies
It uses add, sum, tensor, values, Tensor, and TensorError from https://github.com/GreenPandaStudios/aug-pytorch#e2b74b1968fb11972e260ef3796a1cd849c1f702.
Built-in operations follow the language reference.