Skip to content

Commit d1b0934

Browse files
committed
Updated to use new gotype package.
1 parent 321e6d4 commit d1b0934

4 files changed

Lines changed: 16 additions & 13 deletions

File tree

pjrt/buffers.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"unsafe"
1515

1616
"github.com/gomlx/compute/dtypes"
17+
"github.com/gomlx/compute/dtypes/gotype"
1718
"github.com/gomlx/go-xla/internal/shapeinference"
1819
"github.com/gomlx/go-xla/types/shapes"
1920
"github.com/pkg/errors"
@@ -222,7 +223,7 @@ func (b *Buffer) Client() *Client {
222223
}
223224

224225
// ScalarToRaw generates the raw values needed by BufferFromHostConfig.FromRawData to feed a simple scalar value.
225-
func ScalarToRaw[T dtypes.Supported](value T) ([]byte, dtypes.DType, []int) {
226+
func ScalarToRaw[T gotype.Supported](value T) ([]byte, dtypes.DType, []int) {
226227
dtype := dtypes.FromGenericsType[T]()
227228
rawSlice := unsafe.Slice((*byte)(unsafe.Pointer(&value)), int(unsafe.Sizeof(value)))
228229
return rawSlice, dtype, nil // empty dimensions for scalar
@@ -252,7 +253,7 @@ func (b *Buffer) Size() (int, error) {
252253
}
253254

254255
// BufferToScalar is a generic function that transfer a Buffer back to host as a scalar of the given type.
255-
func BufferToScalar[T dtypes.Supported](b *Buffer) (value T, err error) {
256+
func BufferToScalar[T gotype.Supported](b *Buffer) (value T, err error) {
256257
dst := unsafe.Slice((*byte)(unsafe.Pointer(&value)), unsafe.Sizeof(value))
257258
err = b.ToHost(dst)
258259
return
@@ -262,7 +263,7 @@ func BufferToScalar[T dtypes.Supported](b *Buffer) (value T, err error) {
262263
//
263264
// It is a shortcut to Client.BufferFromHost call with default parameters.
264265
// If you need more control where the value will be used you'll have to use Client.BufferFromHost instead.
265-
func ScalarToBuffer[T dtypes.Supported](client *Client, value T) (b *Buffer, err error) {
266+
func ScalarToBuffer[T gotype.Supported](client *Client, value T) (b *Buffer, err error) {
266267
dtype := dtypes.FromGenericsType[T]()
267268
src := unsafe.Slice((*byte)(unsafe.Pointer(&value)), unsafe.Sizeof(value))
268269
return client.BufferFromHost().FromRawData(src, dtype, nil).Done()
@@ -272,7 +273,7 @@ func ScalarToBuffer[T dtypes.Supported](client *Client, value T) (b *Buffer, err
272273
//
273274
// It is a shortcut to Client.BufferFromHost call with default parameters.
274275
// If you need more control where the value will be used you'll have to use Client.BufferFromHost instead.
275-
func ScalarToBufferOnDeviceNum[T dtypes.Supported](client *Client, deviceNum int, value T) (b *Buffer, err error) {
276+
func ScalarToBufferOnDeviceNum[T gotype.Supported](client *Client, deviceNum int, value T) (b *Buffer, err error) {
276277
dtype := dtypes.FromGenericsType[T]()
277278
src := unsafe.Slice((*byte)(unsafe.Pointer(&value)), unsafe.Sizeof(value))
278279
return client.BufferFromHost().FromRawData(src, dtype, nil).ToDeviceNum(deviceNum).Done()
@@ -283,15 +284,15 @@ func ScalarToBufferOnDeviceNum[T dtypes.Supported](client *Client, deviceNum int
283284
//
284285
// It is a shortcut to Client.BufferFromHost call with default parameters.
285286
// If you need more control where the value will be used you'll have to use Client.BufferFromHost instead.
286-
func ArrayToBuffer[T dtypes.Supported](client *Client, flatValues []T, dimensions ...int) (b *Buffer, err error) {
287+
func ArrayToBuffer[T gotype.Supported](client *Client, flatValues []T, dimensions ...int) (b *Buffer, err error) {
287288
if len(dimensions) == 0 && len(flatValues) != 1 {
288289
return nil, errors.Errorf("ArrayToBuffer not given any dimensions (indicating a scalar), but len(flatValues) == %d", len(flatValues))
289290
}
290291
return client.BufferFromHost().FromFlatDataWithDimensions(flatValues, dimensions).Done()
291292
}
292293

293294
// BufferToArray transfers the buffer to an array defined by a slice with its flat values, and returns also its underlying dimensions.
294-
func BufferToArray[T dtypes.Supported](buffer *Buffer) (flatValues []T, dimensions []int, err error) {
295+
func BufferToArray[T gotype.Supported](buffer *Buffer) (flatValues []T, dimensions []int, err error) {
295296
if err = buffer.Check(); err != nil {
296297
return
297298
}

pjrt/pjrt_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"testing"
1212

13-
"github.com/gomlx/compute/dtypes"
13+
"github.com/gomlx/compute/dtypes/gotype"
1414
"github.com/pkg/errors"
1515
"k8s.io/klog/v2"
1616
)
@@ -88,7 +88,7 @@ func compile(t *testing.T, client *Client, comp XlaComputation) (exec *LoadedExe
8888
// execWithScalars executes the program on the input value given, and return the output.
8989
// Both input and output expected to be a scalar.
9090
// Any errors fail the test.
91-
func execWithScalars[T dtypes.Supported](t *testing.T, client *Client, exec *LoadedExecutable, input T) T {
91+
func execWithScalars[T gotype.Supported](t *testing.T, client *Client, exec *LoadedExecutable, input T) T {
9292
inputBuffer, err := ScalarToBuffer(client, input)
9393
requireNoError(t, err, "Failed to create on-device buffer for input %v", input)
9494
defer func() { requireNoError(t, inputBuffer.Destroy()) }()
@@ -105,7 +105,7 @@ func execWithScalars[T dtypes.Supported](t *testing.T, client *Client, exec *Loa
105105
return output
106106
}
107107

108-
func execWithSlices[T dtypes.Supported](t *testing.T, client *Client, exec *LoadedExecutable, input []T) (flat []T, dims []int) {
108+
func execWithSlices[T gotype.Supported](t *testing.T, client *Client, exec *LoadedExecutable, input []T) (flat []T, dims []int) {
109109
inputBuffer, err := ArrayToBuffer(client, input, len(input))
110110
requireNoError(t, err, "Failed to create on-device buffer for input %v", input)
111111
defer func() { requireNoError(t, inputBuffer.Destroy()) }()
@@ -122,7 +122,7 @@ func execWithSlices[T dtypes.Supported](t *testing.T, client *Client, exec *Load
122122
return
123123
}
124124

125-
func execArrayOutput[T dtypes.Supported](t *testing.T, client *Client, exec *LoadedExecutable) (flat []T, dims []int) {
125+
func execArrayOutput[T gotype.Supported](t *testing.T, client *Client, exec *LoadedExecutable) (flat []T, dims []int) {
126126
outputBuffers := capture(exec.Execute().Done()).Test(t)
127127
assertLen(t, outputBuffers, 1, "Expected only one output")
128128
defer func() { requireNoError(t, outputBuffers[0].Destroy()) }()
@@ -137,7 +137,7 @@ func execArrayOutput[T dtypes.Supported](t *testing.T, client *Client, exec *Loa
137137
}
138138

139139
// execScalarOutput executes the LoadedExecutable with no inputs, and a scalar output of the given type.
140-
func execScalarOutput[T dtypes.Supported](t *testing.T, client *Client, exec *LoadedExecutable) (value T) {
140+
func execScalarOutput[T gotype.Supported](t *testing.T, client *Client, exec *LoadedExecutable) (value T) {
141141
outputBuffers := capture(exec.Execute().Done()).Test(t)
142142
assertLen(t, outputBuffers, 1, "Expected only one output")
143143
defer func() { requireNoError(t, outputBuffers[0].Destroy()) }()

types/shapes/dtype.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/gomlx/compute/dtypes"
2424
"github.com/gomlx/compute/dtypes/bfloat16"
2525
"github.com/gomlx/compute/dtypes/float16"
26+
"github.com/gomlx/compute/dtypes/gotype"
2627
"github.com/pkg/errors"
2728
)
2829

@@ -32,7 +33,7 @@ import (
3233
// It doesn't work for if T (the output type) is a complex number.
3334
// If value is a complex number, it converts by taking the real part of the number and
3435
// discarding the imaginary part.
35-
func ConvertTo[T dtypes.NumberNotComplex](value any) T {
36+
func ConvertTo[T gotype.NumericNotComplex](value any) T {
3637
t, ok := value.(T)
3738
if ok {
3839
return t

types/shapes/shape.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import (
8282
"strings"
8383

8484
"github.com/gomlx/compute/dtypes"
85+
"github.com/gomlx/compute/dtypes/gotype"
8586
"github.com/pkg/errors"
8687
)
8788

@@ -147,7 +148,7 @@ func Make(dtype dtypes.DType, dimensions ...int) Shape {
147148
}
148149

149150
// Scalar returns a scalar Shape for the given type.
150-
func Scalar[T dtypes.Number]() Shape {
151+
func Scalar[T gotype.Numeric]() Shape {
151152
return Shape{DType: dtypes.FromGenericsType[T]()}
152153
}
153154

0 commit comments

Comments
 (0)