Skip to content

packages/@greenpandastudios/aug-pytorch/0.2.0/api.aug ​

CPU tensors with PyTorch · Dependency source and specification

This is the exact dependency version used by this example.

Code ​

aug
// Generated by aug spec. This is a copy of the installed dependency source.
// aug-spec: "api.aug.md" explains this file. Read it before changes; refresh with aug spec.
import Tensor from bindings
import TensorError from contracts
extern C _tensor(List<float> values) returns own Tensor unless TensorError
extern C _add(Tensor left, Tensor right) returns own Tensor unless TensorError
extern C _sum(Tensor tensor) returns float unless TensorError
extern C _values(Tensor tensor) returns List<float> unless TensorError
/** Copy a list of float64 values into a CPU tensor. */
tensor(List<float> values) returns own Tensor:
    unsafe:
        return _tensor(values)
/** Add tensors without changing either input. */
add(Tensor left, Tensor right) returns own Tensor:
    unsafe:
        return _add(left, right)
/** Sum every element. */
sum(Tensor tensor) returns float:
    unsafe:
        return _sum(tensor)
/** Copy tensor values into an August list. */
values(Tensor tensor) returns List<float>:
    unsafe:
        return _values(tensor)
extern C _liveTensors() returns int
extern C _liveBuffers() returns int
_consumeAndFail(own Tensor value) unless TensorError:
    throw TensorError(code=99, message="expected cleanup test")
interface _TensorContainer:
    total() returns float unless TensorError
_TensorHolder(mutable own Tensor item) implements _TensorContainer:
    total() returns float:
        return sum(tensor=item)
_replace(borrow _TensorHolder holder, own Tensor replacement):
    holder.item = replacement
test tensor:
    when "cpu":
        it "adds_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)
            assert(sum(tensor=result) == 21.0)
            List<float> output = values(tensor=result)
            assert(output.length() == 3)
    when "ownership":
        it "transfers_into_a_field_and_releases_the_old_tensor":
            int before = 0
            unsafe:
                before = _liveTensors()
            scope:
                own Tensor initial = tensor(values=[1.0])
                own _TensorHolder holder = _TensorHolder(item=initial)
                own Tensor replacement = tensor(values=[7.0])
                borrow holder:
                    _replace(holder, replacement)
                assert(holder.total() == 7.0)
                unsafe:
                    assert(_liveTensors() == before + 1)
            unsafe:
                assert(_liveTensors() == before)
        it "preserves_native_error_methods":
            bool caught = false
            own Tensor left = tensor(values=[1.0, 2.0])
            own Tensor right = tensor(values=[1.0, 2.0, 3.0])
            try:
                add(left, right)
            catch TensorError error:
                caught = error.explain().length() > 0
            assert(caught)
        it "releases_a_transferred_tensor_when_the_callee_fails":
            int before = 0
            unsafe:
                before = _liveTensors()
            bool caught = false
            try:
                own Tensor value = tensor(values=[1.0])
                _consumeAndFail(value)
            catch TensorError error:
                caught = error.code == 99
            assert(caught)
            unsafe:
                assert(_liveTensors() == before)
                assert(_liveBuffers() == 0)
        it "releases_scoped_and_unused_results":
            int before = 0
            unsafe:
                before = _liveTensors()
            scope:
                own Tensor value = tensor(values=[2.0])
                List<float> output = values(tensor=value)
                assert(output.get(index=0) == 2.0)
            tensor(values=[3.0])
            unsafe:
                assert(_liveTensors() == before)
                assert(_liveBuffers() == 0)
aug
// Generated by aug spec. This is a copy of the installed dependency source.
// aug-spec: "api.aug.md" explains this file. Read it before changes; refresh with aug spec.
import Tensor from bindings
import TensorError from contracts
extern C _tensor(List<float> values) returns own Tensor unless TensorError
extern C _add(Tensor left, Tensor right) returns own Tensor unless TensorError
extern C _sum(Tensor tensor) returns float unless TensorError
extern C _values(Tensor tensor) returns List<float> unless TensorError
/** Copy a list of float64 values into a CPU tensor. */
tensor(List<float> values) returns own Tensor {
    unsafe {
        return _tensor(values)
    }
}
/** Add tensors without changing either input. */
add(Tensor left, Tensor right) returns own Tensor {
    unsafe {
        return _add(left, right)
    }
}
/** Sum every element. */
sum(Tensor tensor) returns float {
    unsafe {
        return _sum(tensor)
    }
}
/** Copy tensor values into an August list. */
values(Tensor tensor) returns List<float> {
    unsafe {
        return _values(tensor)
    }
}
extern C _liveTensors() returns int
extern C _liveBuffers() returns int
_consumeAndFail(own Tensor value) unless TensorError {
    throw TensorError(code=99, message="expected cleanup test")
}
interface _TensorContainer {
    total() returns float unless TensorError
}
_TensorHolder(mutable own Tensor item) implements _TensorContainer {
    total() returns float {
        return sum(tensor=item)
    }
}
_replace(borrow _TensorHolder holder, own Tensor replacement) {
    holder.item = replacement
}
test tensor {
    when "cpu" {
        it "adds_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)
            assert(sum(tensor=result) == 21.0)
            List<float> output = values(tensor=result)
            assert(output.length() == 3)
        }
    }
    when "ownership" {
        it "transfers_into_a_field_and_releases_the_old_tensor" {
            int before = 0
            unsafe {
                before = _liveTensors()
            }
            scope {
                own Tensor initial = tensor(values=[1.0])
                own _TensorHolder holder = _TensorHolder(item=initial)
                own Tensor replacement = tensor(values=[7.0])
                borrow holder {
                    _replace(holder, replacement)
                }
                assert(holder.total() == 7.0)
                unsafe {
                    assert(_liveTensors() == before + 1)
                }
            }
            unsafe {
                assert(_liveTensors() == before)
            }
        }
        it "preserves_native_error_methods" {
            bool caught = false
            own Tensor left = tensor(values=[1.0, 2.0])
            own Tensor right = tensor(values=[1.0, 2.0, 3.0])
            try {
                add(left, right)
            }
            catch TensorError error {
                caught = error.explain().length() > 0
            }
            assert(caught)
        }
        it "releases_a_transferred_tensor_when_the_callee_fails" {
            int before = 0
            unsafe {
                before = _liveTensors()
            }
            bool caught = false
            try {
                own Tensor value = tensor(values=[1.0])
                _consumeAndFail(value)
            }
            catch TensorError error {
                caught = error.code == 99
            }
            assert(caught)
            unsafe {
                assert(_liveTensors() == before)
                assert(_liveBuffers() == 0)
            }
        }
        it "releases_scoped_and_unused_results" {
            int before = 0
            unsafe {
                before = _liveTensors()
            }
            scope {
                own Tensor value = tensor(values=[2.0])
                List<float> output = values(tensor=value)
                assert(output.get(index=0) == 2.0)
            }
            tensor(values=[3.0])
            unsafe {
                assert(_liveTensors() == before)
                assert(_liveBuffers() == 0)
            }
        }
    }
}

Compiled specification ​

Interactions and sequences

tensor · source ​

Copy a list of float64 values into a CPU tensor. It takes values as List<float>. It returns ownership of Tensor. Failures can raise TensorError.

Within an unsafe block, it returns _tensor with values. Native operations must satisfy their declared C contracts. source

Checked interface
text
tensor(List<float> values) returns own Tensor unless TensorError

It takes values as List<float>. It returns ownership of Tensor. Failures can raise TensorError.

add · source ​

Add tensors without changing either input. It takes left and right as Tensor. It returns ownership of Tensor. Failures can raise TensorError.

Within an unsafe block, it returns _add with left and right. Native operations must satisfy their declared C contracts. source

Checked interface
text
add(Tensor left, Tensor right) returns own Tensor unless TensorError

It takes left and right as Tensor. It returns ownership of Tensor. Failures can raise TensorError.

sum · source ​

Sum every element. It takes tensor as Tensor. Failures can raise TensorError.

Within an unsafe block, it returns _sum with tensor. Native operations must satisfy their declared C contracts. source

Checked interface
text
sum(Tensor tensor) returns float unless TensorError

It takes tensor as Tensor. Failures can raise TensorError.

values · source ​

Copy tensor values into an August list. It takes tensor as Tensor. Failures can raise TensorError.

Within an unsafe block, it returns _values with tensor. Native operations must satisfy their declared C contracts. source

Checked interface
text
values(Tensor tensor) returns List<float> unless TensorError

It takes tensor as Tensor. Failures can raise TensorError.

_tensor · source ​

It is private to its defining scope. It takes values as List<float>. It returns ownership of Tensor. Failures can raise TensorError.

Native implementation: @greenpandastudios/aug-pytorch@0.2.0, 2.14.1. Supported targets: linux arm64 glibc 2.36+ itanium-cxx11, linux x64 glibc 2.36+ itanium-cxx11, macos arm64 14.0+ apple-libc++. Binding contract: native.abi.json (SHA-256 f07b8cab89ad7cfe368edcd9daf87ecc810adb6a2eed1f446d7b71fb664e91f5). It calls aug_torch_tensor_from_f64_v1 through the C ABI on the caller thread; a blocking native call blocks that thread. The caller owns the returned handle. Its contract does not permit worker entry. The compiler checks the provider, descriptor digest, signature and ownership at August call sites. The native author promises not to retain inputs, enter August from foreign threads, or unwind across the C boundary; internal native workers may run. The compiler does not prove those promises.

_add · source ​

It is private to its defining scope. It takes left and right as Tensor. It returns ownership of Tensor. Failures can raise TensorError.

Native implementation: @greenpandastudios/aug-pytorch@0.2.0, 2.14.1. Supported targets: linux arm64 glibc 2.36+ itanium-cxx11, linux x64 glibc 2.36+ itanium-cxx11, macos arm64 14.0+ apple-libc++. Binding contract: native.abi.json (SHA-256 f07b8cab89ad7cfe368edcd9daf87ecc810adb6a2eed1f446d7b71fb664e91f5). It calls aug_torch_tensor_add_v1 through the C ABI on the caller thread; a blocking native call blocks that thread. left lends read access for this call; right lends read access for this call. The caller owns the returned handle. Its contract does not permit worker entry. The compiler checks the provider, descriptor digest, signature and ownership at August call sites. The native author promises not to retain inputs, enter August from foreign threads, or unwind across the C boundary; internal native workers may run. The compiler does not prove those promises.

_sum · source ​

It is private to its defining scope. It takes tensor as Tensor. It returns float. Failures can raise TensorError.

Native implementation: @greenpandastudios/aug-pytorch@0.2.0, 2.14.1. Supported targets: linux arm64 glibc 2.36+ itanium-cxx11, linux x64 glibc 2.36+ itanium-cxx11, macos arm64 14.0+ apple-libc++. Binding contract: native.abi.json (SHA-256 f07b8cab89ad7cfe368edcd9daf87ecc810adb6a2eed1f446d7b71fb664e91f5). It calls aug_torch_tensor_sum_v1 through the C ABI on the caller thread; a blocking native call blocks that thread. tensor lends read access for this call. Its contract does not permit worker entry. The compiler checks the provider, descriptor digest, signature and ownership at August call sites. The native author promises not to retain inputs, enter August from foreign threads, or unwind across the C boundary; internal native workers may run. The compiler does not prove those promises.

_values · source ​

It is private to its defining scope. It takes tensor as Tensor. It returns List<float>. Failures can raise TensorError.

Native implementation: @greenpandastudios/aug-pytorch@0.2.0, 2.14.1. Supported targets: linux arm64 glibc 2.36+ itanium-cxx11, linux x64 glibc 2.36+ itanium-cxx11, macos arm64 14.0+ apple-libc++. Binding contract: native.abi.json (SHA-256 f07b8cab89ad7cfe368edcd9daf87ecc810adb6a2eed1f446d7b71fb664e91f5). It calls aug_torch_tensor_values_v1 through the C ABI on the caller thread; a blocking native call blocks that thread. tensor lends read access for this call. August copies the returned buffer, then calls aug_torch_values_release_v1 to release it. Its contract does not permit worker entry. The compiler checks the provider, descriptor digest, signature and ownership at August call sites. The native author promises not to retain inputs, enter August from foreign threads, or unwind across the C boundary; internal native workers may run. The compiler does not prove those promises.

_liveTensors · source ​

It is private to its defining scope. It returns int.

Native implementation: @greenpandastudios/aug-pytorch@0.2.0, 2.14.1. Supported targets: linux arm64 glibc 2.36+ itanium-cxx11, linux x64 glibc 2.36+ itanium-cxx11, macos arm64 14.0+ apple-libc++. Binding contract: native.abi.json (SHA-256 f07b8cab89ad7cfe368edcd9daf87ecc810adb6a2eed1f446d7b71fb664e91f5). It calls aug_probe_live_tensors_v1 through the C ABI on the caller thread; a blocking native call blocks that thread. Its contract does not permit worker entry. The compiler checks the provider, descriptor digest, signature and ownership at August call sites. The native author promises not to retain inputs, enter August from foreign threads, or unwind across the C boundary; internal native workers may run. The compiler does not prove those promises.

_liveBuffers · source ​

It is private to its defining scope. It returns int.

Native implementation: @greenpandastudios/aug-pytorch@0.2.0, 2.14.1. Supported targets: linux arm64 glibc 2.36+ itanium-cxx11, linux x64 glibc 2.36+ itanium-cxx11, macos arm64 14.0+ apple-libc++. Binding contract: native.abi.json (SHA-256 f07b8cab89ad7cfe368edcd9daf87ecc810adb6a2eed1f446d7b71fb664e91f5). It calls aug_probe_live_buffers_v1 through the C ABI on the caller thread; a blocking native call blocks that thread. Its contract does not permit worker entry. The compiler checks the provider, descriptor digest, signature and ownership at August call sites. The native author promises not to retain inputs, enter August from foreign threads, or unwind across the C boundary; internal native workers may run. The compiler does not prove those promises.

_consumeAndFail · source ​

It is private to its defining scope. It takes value as Tensor with ownership transferred. Failures can raise TensorError. It raises a TensorError with code 99 and message "expected cleanup test". source

Checked interface
text
_consumeAndFail(own Tensor value) returns void unless TensorError

It takes value as Tensor with ownership transferred. Failures can raise TensorError.

_TensorContainer · interface · source ​

It is private to this file.

_TensorContainer.total · source ​

It returns float. Failures can raise TensorError.

_TensorHolder · class · source ​

It implements _TensorContainer. It is private to this file. It takes item as Tensor with ownership transferred, kept mutable.

_TensorHolder.total · source ​

Failures can raise TensorError. It returns sum with tensor from item. source

Checked interface
text
total() returns float unless TensorError

Failures can raise TensorError.

_replace · source ​

It is private to its defining scope. It takes holder as _TensorHolder with permission to mutate it during the call and replacement as Tensor with ownership transferred. It may change holder. It sets holder.item to replacement. source

Checked interface
text
_replace(borrow _TensorHolder holder, own Tensor replacement) returns void changes holder

It takes holder as _TensorHolder with permission to mutate it during the call and replacement as Tensor with ownership transferred. It may change holder.

test tensor · source ​

Tests tensor. Each case gets fresh setup and dependencies.

cpu ​

adds_real_tensors · source ​

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). The test requires sum with tensor from result equals 21.0. source

It sets output of type List<float> to values with tensor from result. The test requires output.length equals 3. source

ownership ​

transfers_into_a_field_and_releases_the_old_tensor · source ​

It sets before to 0. Within an unsafe block, it gets before from _liveTensors. Native operations must satisfy their declared C contracts. Within a task and ownership scope, it calls tensor with values from a list containing 1.0 and stores the result in owned initial (Tensor). source

It creates _TensorHolder with item from initial and stores the result in owned holder (_TensorHolder). It calls tensor with values from a list containing 7.0 and stores the result in owned replacement (Tensor). With temporary permission to change holder, it calls _replace with holder and replacement. The test requires holder.total equals 7.0. source

Within an unsafe block, the test requires _liveTensors equals (before plus 1). Native operations must satisfy their declared C contracts. On leaving this scope, join its child tasks and release its local values. Within an unsafe block, the test requires _liveTensors equals before. source

Native operations must satisfy their declared C contracts. source

preserves_native_error_methods · source ​

It sets caught to false. It calls tensor with values from a list containing 1.0, 2.0 and stores the result in owned left (Tensor). It calls tensor with values from a list containing 1.0, 2.0, 3.0 and stores the result in owned right (Tensor). source

It tries to call add with left and right. If this work raises TensorError as error, it sets caught to length on error.explain is positive. The test requires caught is true. source

releases_a_transferred_tensor_when_the_callee_fails · source ​

It sets before to 0. Within an unsafe block, it gets before from _liveTensors. Native operations must satisfy their declared C contracts. It sets caught to false. source

It calls tensor with values from a list containing 1.0 and stores the result in owned value (Tensor). It calls _consumeAndFail with value. If this work raises TensorError as error, it sets caught to error.code equals 99. The test requires caught is true. source

Within an unsafe block, the test requires _liveTensors equals before; then the test requires _liveBuffers equals 0. Native operations must satisfy their declared C contracts. source

releases_scoped_and_unused_results · source ​

It sets before to 0. Within an unsafe block, it gets before from _liveTensors. Native operations must satisfy their declared C contracts. Within a task and ownership scope, it calls tensor with values from a list containing 2.0 and stores the result in owned value (Tensor); then it sets output of type List<float> to values with tensor from value; then the test requires output.get with index 0 equals 2.0. source

On leaving this scope, join its child tasks and release its local values. It calls tensor with values from a list containing 3.0. Within an unsafe block, the test requires _liveTensors equals before; then the test requires _liveBuffers equals 0. Native operations must satisfy their declared C contracts. source

Dependencies ​

It uses Tensor from bindings. It uses TensorError from contracts.

Built-in operations follow the language reference.

Last updated:

The world runs on language