# Arenas

> Every output whose length only the call knows, the embeddings, the states and their offsets, lands in an Arena you pass, and the verb answers views over it without copying.

`import "github.com/unum-cloud/uform"` 4.0.0-rc.1 · c58749fe

Every output whose length only the call knows, the embeddings, the states and their offsets, lands in an `Arena` you pass, and the verb answers views over it without copying. `uform.NewArena(executor)` places it where that executor computes, decided once, and `uform.NewArena(nil)` makes host pages for the calling goroutine. Each call rewinds its arena, so the previous call’s views and every slice they handed out end there, and a steady loop allocates once. A view read after a later call took its arena fails as `Contended`. A slice or segment an accessor returned is valid only until the arena’s next call. To keep a result past the next call, pass that call another arena; an arena serves one call at a time, so each deferred call needs its own. An arena from a VRAM executor holds device memory, which `Describe` reports as `ResidencyVram`; where its `HostAccess` is `HostAccessRefused` no Go code may read it, so a verb given that arena fails with `StatusDevice` before it runs, while managed memory and Apple’s shared memory read like host pages. The Go binding has no pointer accessor or device export that could hand device outputs on unread, so CUDA and HIP device arenas are not usable from Go. An arena never outlives the executor that made it, so `defer executor.Close()` comes before `defer arena.Close()`, and an arena from `uform.NewArena(nil)` lives until it is closed.

```go
arena, err := uform.NewArena(executor)
for _, batch := range batches {
	encoded, err := model.On(executor).TextEncode(batch, uform.EncodeOutputsEmbeddings, uform.ScalarF32, arena, nil)
	if err != nil {
		return err
	}
	consume(encoded.Embeddings) // rewinds next iteration
}
```

`Close` waits for every deferred call naming the arena, and a call naming an arena that serves another call finishes with `StatusContended`.

## API

### Types

#### `ArenaDescription`

ArenaDescription is where an arena's outputs live, which decides whether Go code may read them.

[types.go:898 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L898)

```go
type ArenaDescription struct {
    Residency Residency
    Device int32
    Vendor GPUVendor
    VendorOrdinal int32
    HostAccess HostAccess
}
```

Mirrors uform_arena_description_t field for field, so the library writes it in place.

#### Fields

| `Residency` | `Residency` | Residency is ResidencyRam for host pages and ResidencyVram for one device's memory. |
| - | - | - |
| `Device` | `int32` | Device is the ordinal of the GPU holding the outputs, as GPUDevices lists it, and -1 for host pages. |
| `Vendor` | `GPUVendor` | Vendor is the vendor whose memory it is, as GPUDevices reports it. |
| `VendorOrdinal` | `int32` | VendorOrdinal is the vendor's own device number, which Arrow and DLPack name, and -1 for host pages. |
| `HostAccess` | `HostAccess` | HostAccess is whether Go code may read the outputs. |

#### Struct `Arena`

Arena is memory the library writes every output whose length scales with a call into, pooled embeddings, per-token states and their offsets alike, reused so a steady loop allocates once.3

[types.go:870 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L870)

```go
type Arena struct {
    // contains filtered or unexported fields
}
```

Every call given an arena rewinds and refills it, so the views of its previous call and every slice they handed out end there, as they do once it is closed; to keep one call's views past the next, give that call another arena. An arena serves one call at a time, and a second overlapping it fails with StatusContended. Deferred verbs hold it as blocking ones do, since Close waits for every deferred call naming it. A verb given an arena whose Describe refuses host access fails with StatusDevice before it runs, since no Go code may read its views.

##### `NewArena`

NewArena makes an empty arena where executor computes, host pages or the memory of its GPU, decided once; a nil executor makes host pages for calls on the calling goroutine.

[types.go:879 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L879)

```go
func NewArena(executor *Executor) (*Arena, error)
```

Nothing is allocated until a call writes into it. An arena must not outlive the executor that made it, so defer [Executor.Close](https://www.unum.cloud/docs/uform/go/training.md#Close) before [Arena.Close](https://www.unum.cloud/docs/uform/go/training.md#Close); one made with a nil executor lives until it is closed. A GPU arena serves only calls on its executor.

##### `Close`

Close releases the memory, waiting for every deferred call naming the arena, and ends every view over it; it comes before the Close of the executor that made the arena.

[types.go:1195 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L1195)

```go
func (arena *Arena) Close() error
```

While an Arrow export of its outputs is unreleased it fails with [StatusContended](https://www.unum.cloud/docs/uform/go/errors.md#StatusContended) and frees nothing, so a later Close retries. It is idempotent, and a closed arena must not be used again.

##### `Describe`

Describe reports where the arena's outputs live, as the executor that made it decided.

[types.go:925 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L925)

```go
func (arena *Arena) Describe() (ArenaDescription, error)
```

#### Enum `HostAccess`

HostAccess is whether Go code may read an arena's outputs.1

[types.go:576 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L576)

```go
type HostAccess int32
```

#### Values

| `HostAccessRefused` | | HostAccessRefused is device memory the host must not read. |
| - | - | - |
| `HostAccessReadable` | | HostAccessReadable is host, pinned and managed memory, and Apple's shared memory. |

##### `String`

String renders the access as the library spells it, "refused" or "readable".

[types.go:586 ↗](https://github.com/unum-cloud/UForm/blob/c58749fe4bb208356cc78a87357ab007041c515b/types.go#L586)

```go
func (access HostAccess) String() string
```
