Documentation
¶
Overview ¶
Package testutil provides utilities for testing a compute.Backend. It is used by the gobackend package to implement the "go" backend, but shared here as it could be used by other backends as well.
It includes helpers for:
- Comparison: IsEqual, IsInDelta and IsInRelativeDelta, implemented for various types, including the half-precision float16 and bfloat16.
- Buffer manipulation: ToBuffer and FromBuffer are not efficient, but convenient conversion of values to Go for testing.
Index ¶
- Constants
- func Exec(backend compute.Backend, inputs []any, ...) ([]any, error)
- func Exec1(backend compute.Backend, inputs []any, ...) (any, error)
- func Exec2(backend compute.Backend, inputs []any, ...) (any, any, error)
- func Exec3(backend compute.Backend, inputs []any, ...) (any, any, any, error)
- func FlattenSlice(v any) any
- func FromBuffer(backend compute.Backend, buf compute.Buffer) (any, error)
- func IsEqual(want, got any) (ok bool, diff string)
- func IsInDelta(want, got any, delta float64) (ok bool, diff string)
- func IsInRelativeDelta(want, got any, relDelta float64) (ok bool, diff string)
- func Must(err error)
- func Must1[T any](value T, err error) T
- func SkipIfMissing(t *testing.T, b compute.Backend, op compute.OpType)
- func SkipIfMissingDType(t *testing.T, b compute.Backend, dtype dtypes.DType)
- func SkipIfMissingDynamicShapes(t *testing.T, b compute.Backend)
- func SkipIfMissingFunctions(t *testing.T, b compute.Backend)
- func ToBuffer(backend compute.Backend, v any) (compute.Buffer, error)
- func ToFlatAndShape(v any) (any, shapes.Shape)
- func Try(f func()) (panicked bool, reason any)
- type DurationSampler
- func (s *DurationSampler) Count() int
- func (s *DurationSampler) Max() time.Duration
- func (s *DurationSampler) Mean() time.Duration
- func (s *DurationSampler) Median() time.Duration
- func (s *DurationSampler) Min() time.Duration
- func (s *DurationSampler) Percentile(p float64) time.Duration
- func (s *DurationSampler) ReservoirCount() int
- func (s *DurationSampler) Reset()
- func (s *DurationSampler) Sample(d time.Duration)
Constants ¶
const DefaultReservoirSize = 16 * 1024
DefaultReservoirSize is the default capacity for the duration sampler (16K samples).
Variables ¶
This section is empty.
Functions ¶
func Exec ¶
func Exec(backend compute.Backend, inputs []any, buildFn func(f compute.Function, params []compute.Value) ([]compute.Value, error), ) ([]any, error)
Exec builds, compiles, and executes graph with multiple inputs and outputs.
Each of the inputs are converted using ToBuffer, and the results are converted back to Go using FromBuffer.
func Exec1 ¶
func Exec1(backend compute.Backend, inputs []any, buildFn func(f compute.Function, params []compute.Value) (compute.Value, error), ) (any, error)
Exec1 is like Exec, but it has only 1 output.
func Exec2 ¶
func Exec2(backend compute.Backend, inputs []any, buildFn func(f compute.Function, params []compute.Value) (compute.Value, compute.Value, error), ) (any, any, error)
Exec2 is like Exec, but it has only 2 outputs.
func Exec3 ¶
func Exec3(backend compute.Backend, inputs []any, buildFn func(f compute.Function, params []compute.Value) (compute.Value, compute.Value, compute.Value, error), ) (any, any, any, error)
Exec3 is like Exec, but it has only 3 outputs.
func FlattenSlice ¶
FlattenSlice is a helper to convert a nested slice of any into a flat slice.
func FromBuffer ¶
FromBuffer converts a compute.Buffer into a nested slice of the corresponding Go type.
func IsEqual ¶
IsEqual returns whether want and got are equal using go-cmp. If they are not equal, it returns the diff using the format "-want +got").
func IsInDelta ¶
IsInDelta reports if want and got are equal within a given absolute delta. If they are not equal, it returns the diff using the format "-want +got").
func IsInRelativeDelta ¶
IsInRelativeDelta reports if want and got are equal within a given relative delta. If they are not equal, it returns the diff using the format "-want +got").
func SkipIfMissing ¶
SkipIfMissing skips the current test if the backend doesn't support the given operation.
func SkipIfMissingDType ¶
SkipIfMissingDType skips the current test if the backend doesn't support the given data type.
func SkipIfMissingDynamicShapes ¶ added in v0.1.5
SkipIfMissingDynamicShapes skips the current test if the backend doesn't support dynamic shapes.
func SkipIfMissingFunctions ¶
SkipIfMissingFunctions skips the current test if the backend doesn't support functions (closures).
func ToFlatAndShape ¶
ToFlatAndShape converts a nested slice of any into a flat slice and a shape that can be fed into testBackend (or a BufferFromFlatData).
Types ¶
type DurationSampler ¶ added in v0.1.10
type DurationSampler struct {
// contains filtered or unexported fields
}
DurationSampler collects time.Duration observations using Algorithm R (Reservoir Sampling) with a fixed capacity to compute percentiles (like median) in constant space and O(1) sample addition.
func NewDurationSampler ¶ added in v0.1.10
func NewDurationSampler(capacity int) *DurationSampler
NewDurationSampler creates a sampler with the given maximum reservoir capacity. If capacity <= 0, DefaultReservoirSize (16384) is used.
func (*DurationSampler) Count ¶ added in v0.1.10
func (s *DurationSampler) Count() int
Count returns the total number of samples observed.
func (*DurationSampler) Max ¶ added in v0.1.10
func (s *DurationSampler) Max() time.Duration
Max returns the maximum duration recorded, or 0 if empty.
func (*DurationSampler) Mean ¶ added in v0.1.10
func (s *DurationSampler) Mean() time.Duration
Mean returns the mean duration of the retained samples, or 0 if empty.
func (*DurationSampler) Median ¶ added in v0.1.10
func (s *DurationSampler) Median() time.Duration
Median returns the median duration of the collected samples. If no samples were recorded, it returns 0.
func (*DurationSampler) Min ¶ added in v0.1.10
func (s *DurationSampler) Min() time.Duration
Min returns the minimum duration recorded, or 0 if empty.
func (*DurationSampler) Percentile ¶ added in v0.1.10
func (s *DurationSampler) Percentile(p float64) time.Duration
Percentile returns the p-th percentile duration (0.0 <= p <= 1.0). If no samples were recorded, it returns 0.
func (*DurationSampler) ReservoirCount ¶ added in v0.1.10
func (s *DurationSampler) ReservoirCount() int
ReservoirCount returns the number of samples currently retained in the reservoir.
func (*DurationSampler) Reset ¶ added in v0.1.10
func (s *DurationSampler) Reset()
Reset clears the samples and count, retaining the allocated reservoir buffer.
func (*DurationSampler) Sample ¶ added in v0.1.10
func (s *DurationSampler) Sample(d time.Duration)
Sample adds a duration observation. The first capacity samples are stored sequentially. Subsequent samples are retained with probability (capacity / count), replacing a randomly chosen sample in the reservoir.