Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cmd/dtypes_codegen/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"github.com/gomlx/exceptions"
"github.com/gomlx/gopjrt/protos/xla_data"
"github.com/janpfeifer/must"
"github.com/pkg/errors"
"os"
"os/exec"
"regexp"
Expand All @@ -17,6 +17,14 @@ const (
DTypeEnumGoFileName = "gen_dtype_enum.go"
)

// panicf panics with formatted description.
//
// It is only used for "bugs in the code" -- when parameters don't follow the specifications.
// In principle, it should never happen -- the same way nil-pointer panics should never happen.
func panicf(format string, args ...any) {
panic(errors.Errorf(format, args...))
}

var aliases = map[string]string{
"INVALID": "InvalidDType",
"PRED": "Bool",
Expand Down Expand Up @@ -121,7 +129,7 @@ func generateEnums(contents string) {
var enumV *enumValue
matches := reEnums.FindStringSubmatch(contents)
if len(matches) == 0 {
exceptions.Panicf("failed to match PJRT_Buffer_Types enum from pjrt_c_api.h")
panicf("failed to match PJRT_Buffer_Types enum from pjrt_c_api.h")
}
for _, line := range strings.Split(matches[2], "\n") {
if line == "" {
Expand Down
12 changes: 10 additions & 2 deletions cmd/xlabuilder_codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package main
import (
"bufio"
"fmt"
"github.com/gomlx/exceptions"
"github.com/janpfeifer/must"
"github.com/pkg/errors"
"os"
"strings"
)
Expand All @@ -17,6 +17,14 @@ type OpInfo struct {
Comments []string
}

// panicf panics with formatted description.
//
// It is only used for "bugs in the code" -- when parameters don't follow the specifications.
// In principle, it should never happen -- the same way nil-pointer panics should never happen.
func panicf(format string, args ...any) {
panic(errors.Errorf(format, args...))
}

func main() {
// Read op_types.xt
opsInfo := make([]OpInfo, 0, 200)
Expand All @@ -40,7 +48,7 @@ func main() {
}
parts := strings.Split(line, ":")
if len(parts) != 2 {
exceptions.Panicf("Invalid op definition in %q:%d : %q", OpTypesFileName, lineNum, line)
panicf("Invalid op definition in %q:%d : %q", OpTypesFileName, lineNum, line)
}
if len(comments) == 0 {
comments = append(comments, fmt.Sprintf("%s returns the Op that represents the output of the corresponding operation.", parts[0]))
Expand Down
3 changes: 2 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Nexxt
# v0.3.1

* Fixed +/-Inf for bfloat16.
* Removed dependencies on "github.com/gomlx/exceptions".

# v0.3.0 Some of the API now returns errors instead of panic

Expand Down
31 changes: 16 additions & 15 deletions docs/coverage.out
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,25 @@ github.com/gomlx/gopjrt/pjrt/gen_chelper.go:62: cMallocArrayAndSet 100.0%
github.com/gomlx/gopjrt/pjrt/gen_chelper.go:73: cDataToSlice 100.0%
github.com/gomlx/gopjrt/pjrt/gen_chelper.go:79: cCharArray 100.0%
github.com/gomlx/gopjrt/pjrt/gen_chelper.go:86: cStrFree 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:37: newLoadedExecutable 64.7%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:67: Destroy 91.7%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:86: destroyOrLog 66.7%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:94: getExecutable 80.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:122: Execute 100.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:152: OnDevices 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:187: OnDevicesByNum 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:211: DonateAll 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:220: DonateNone 100.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:234: Donate 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:245: SetDonate 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:262: Done 88.6%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:319: allocatePerDeviceBufferList 88.9%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:339: freePerDeviceBufferList 100.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:348: gatherPerDeviceBufferList 100.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:36: newLoadedExecutable 64.7%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:66: Destroy 91.7%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:85: destroyOrLog 66.7%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:93: getExecutable 80.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:121: Execute 100.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:151: OnDevices 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:186: OnDevicesByNum 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:210: DonateAll 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:219: DonateNone 100.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:233: Donate 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:244: SetDonate 0.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:261: Done 88.6%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:318: allocatePerDeviceBufferList 88.9%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:338: freePerDeviceBufferList 100.0%
github.com/gomlx/gopjrt/pjrt/loadedexecutables.go:347: gatherPerDeviceBufferList 100.0%
github.com/gomlx/gopjrt/pjrt/namedvalues.go:20: pjrtNamedValuesToMap 25.0%
github.com/gomlx/gopjrt/pjrt/namedvalues.go:50: mallocArrayPJRT_NamedValue 6.7%
github.com/gomlx/gopjrt/pjrt/namedvalues.go:101: destroyPJRT_NamedValue 0.0%
github.com/gomlx/gopjrt/pjrt/pjrt.go:17: panicf 0.0%
github.com/gomlx/gopjrt/pjrt/plugins.go:32: pjrtPluginInitialize 100.0%
github.com/gomlx/gopjrt/pjrt/plugins.go:40: pjrtPluginAttributes 87.5%
github.com/gomlx/gopjrt/pjrt/plugins.go:54: newPlugin 75.0%
Expand Down
40 changes: 29 additions & 11 deletions dtypes/dtypes.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
package dtypes

import (
"github.com/gomlx/exceptions"
"github.com/gomlx/gopjrt/dtypes/bfloat16"
"github.com/pkg/errors"
"github.com/x448/float16"
"math"
"reflect"
"strconv"
)

// panicf panics with formatted description.
//
// It is only used for "bugs in the code" -- when parameters don't follow the specifications.
// In principle, it should never happen -- the same way nil-pointer panics should never happen.
func panicf(format string, args ...any) {
panic(errors.Errorf(format, args...))
}

func init() {
// Only works for 32 and 64 bits platforms.
// TODO: find some compile-time check.
if strconv.IntSize != 32 && strconv.IntSize != 64 {
panicf("cannot use int of %d bits with gopjrt -- only platforms with int32 or int64 are supported", strconv.IntSize)
}
}

// Generate automatic C-to-Go boilerplate code for pjrt_c_api.h.
//go:generate go run ../cmd/dtypes_codegen

Expand All @@ -31,7 +47,7 @@ func FromGenericsType[T Supported]() DType {
case 64:
return Int64
default:
exceptions.Panicf("Cannot use int of %d bits with gopjrt -- try using int32 or int64", strconv.IntSize)
panicf("Cannot use int of %d bits with gopjrt -- try using int32 or int64", strconv.IntSize)
}
case int64:
return Int64
Expand Down Expand Up @@ -75,7 +91,7 @@ func FromGoType(t reflect.Type) DType {
case 64:
return Int64
default:
exceptions.Panicf("cannot use int of %d bits with GoMLX -- try using int32 or int64", strconv.IntSize)
panicf("cannot use int of %d bits with GoMLX -- try using int32 or int64", strconv.IntSize)
}
case reflect.Int64:
return Int64
Expand Down Expand Up @@ -177,8 +193,10 @@ func (dtype DType) GoType() reflect.Type {
return reflect.TypeOf(complex128(0))

default:
exceptions.Panicf("unknown dtype %q (%d) in DType.GoType", dtype, dtype)
panic(nil) // Quiet lint warning.
// This should never happen, except if someone entered an invalid DType number beyond the values
// defined.
panicf("unknown dtype %q (%d) in DType.GoType", dtype, dtype)
panic(nil)
}
}

Expand Down Expand Up @@ -224,9 +242,9 @@ func (dtype DType) LowestValue() any {
return bfloat16.Inf(-1)

default:
exceptions.Panicf("LowestValue for dtype %s not defined", dtype)
// For invalid dtypes (like complex numbers), return zero.
return reflect.New(dtype.GoType()).Elem().Interface()
}
return 0 // Never reaches here.
}

// HighestValue for dtype converted to the corresponding Go type.
Expand Down Expand Up @@ -265,9 +283,9 @@ func (dtype DType) HighestValue() any {
return bfloat16.Inf(1)

default:
exceptions.Panicf("LowestValue for dtype %s not defined", dtype)
// For invalid dtypes (like complex numbers), return zero.
return reflect.New(dtype.GoType()).Elem().Interface()
}
return 0 // Never reaches here.
}

// SmallestNonZeroValueForDType is the smallest non-zero value dtypes.
Expand Down Expand Up @@ -307,8 +325,8 @@ func (dtype DType) SmallestNonZeroValueForDType() any {
return bfloat16.SmallestNonzero // 1p-24, see discussion in https://github.com/x448/float16/pull/46

default:
exceptions.Panicf("SmallestNonZeroValueForDType not defined for dtype %s", dtype)
panic(nil) // Removes lint warning.
// For invalid dtypes (like complex numbers), return zero.
return reflect.New(dtype.GoType()).Elem().Interface()
}
}

Expand Down
23 changes: 23 additions & 0 deletions dtypes/dtypes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dtypes

import (
"github.com/gomlx/gopjrt/dtypes/bfloat16"
"github.com/stretchr/testify/require"
"github.com/x448/float16"
"math"
"testing"
)

func TestDType_HighestLowestSmallestValues(t *testing.T) {
require.True(t, math.IsInf(Float64.HighestValue().(float64), 1))
require.True(t, math.IsInf(float64(Float32.LowestValue().(float32)), -1))
_, ok := Float16.SmallestNonZeroValueForDType().(float16.Float16)
require.True(t, ok)
_, ok = BFloat16.SmallestNonZeroValueForDType().(bfloat16.BFloat16)
require.True(t, ok)

// Complex numbers don't define Highest of Lowest, and instead return 0
require.Equal(t, complex64(0), Complex64.HighestValue().(complex64))
require.Equal(t, complex128(0), Complex128.LowestValue().(complex128))
require.Equal(t, complex64(0), Complex64.SmallestNonZeroValueForDType().(complex64))
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ module github.com/gomlx/gopjrt
go 1.23

require (
github.com/chenxingqiang/go-floatx v0.0.0-20240103165049-2f5e300cb3c3
github.com/chewxy/math32 v1.10.1
github.com/gomlx/exceptions v0.0.3
github.com/janpfeifer/gonb v0.10.1
github.com/janpfeifer/must v0.0.2
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/chenxingqiang/go-floatx v0.0.0-20240103165049-2f5e300cb3c3 h1:2m11dpwbI3ccPgDbAbJ8X6fonuzsi54n70duaraoLn8=
github.com/chenxingqiang/go-floatx v0.0.0-20240103165049-2f5e300cb3c3/go.mod h1:7v935pWqAb/hfUDQyVXq8eXUoXWa7Mub1xEuRbtTFWE=
github.com/chewxy/math32 v1.10.1 h1:LFpeY0SLJXeaiej/eIp2L40VYfscTvKh/FSEZ68uMkU=
github.com/chewxy/math32 v1.10.1/go.mod h1:dOB2rcuFrCn6UHrze36WSLVPKtzPMRAQvBvUwkSsLqs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -8,8 +6,6 @@ github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gomlx/exceptions v0.0.3 h1:HKnTgEjj4jlmhr8zVFkTP9qmV1ey7ypYYosQ8GzXWuM=
github.com/gomlx/exceptions v0.0.3/go.mod h1:uHL0TQwJ0xaV2/snJOJV6hSE4yRmhhfymuYgNredGxU=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/janpfeifer/gonb v0.10.1 h1:yk29LAN5EF0OMKdnXd6xWTmlI/Tedajk7ssVu/1eBis=
Expand Down
3 changes: 1 addition & 2 deletions pjrt/loadedexecutables.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package pjrt
*/
import "C"
import (
"github.com/gomlx/exceptions"
"github.com/pkg/errors"
"k8s.io/klog/v2"
"runtime"
Expand Down Expand Up @@ -327,7 +326,7 @@ func allocatePerDeviceBufferList(numDevices int, buffers []*Buffer) ***C.PJRT_Bu
}
if buffers[idxBuffer].cBuffer == nil {
// Buffer given, but it's cBuffer is nil -> probably it has already been destroyed.
exceptions.Panicf("buffers[%d].cBuffer is nil, has it already been destroyed!?", idxBuffer)
panicf("buffers[%d].cBuffer is nil, has it already been destroyed!?", idxBuffer)
}
return buffers[idxBuffer].cBuffer
})
Expand Down
10 changes: 10 additions & 0 deletions pjrt/pjrt.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
// Package pjrt implements a Go wrapper for the PJRT_C_API.
package pjrt

import "github.com/pkg/errors"

// Generate automatic C-to-Go boilerplate code for pjrt_c_api.h.
//go:generate go run ../cmd/pjrt_codegen

// Since CGO C types cannot cross boundaries of a package (see issue https://github.com/golang/go/issues/13467)
// We make a copy of chelper.go for every sub-directory that needs it.
//go:generate go run ../cmd/copy_go_code --original=chelper.go

// panicf panics with formatted description.
//
// It is only used for "bugs in the code" -- when parameters don't follow the specifications.
// In principle, it should never happen -- the same way nil-pointer panics should never happen.
func panicf(format string, args ...any) {
panic(errors.Errorf(format, args...))
}
9 changes: 2 additions & 7 deletions xlabuilder/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package xlabuilder
*/
import "C"
import (
"github.com/gomlx/exceptions"
"github.com/gomlx/gopjrt/dtypes"
"github.com/gomlx/gopjrt/dtypes/bfloat16"
"github.com/pkg/errors"
Expand Down Expand Up @@ -98,12 +97,8 @@ func NewScalarLiteralFromFloat64(value float64, dtype dtypes.DType) (*Literal, e
return NewScalarLiteral(bfloat16.FromFloat32(float32(value))), nil
default:
var convertedValue reflect.Value
panicMsg := exceptions.Try(func() {
convertedValue = reflect.ValueOf(value).Convert(dtype.GoType())
})
if panicMsg != nil {
return nil, errors.Errorf("failed to convert %g to dtype %s: %v", value, dtype, panicMsg)
}
convertedValue = reflect.ValueOf(value)
convertedValue = convertedValue.Convert(dtype.GoType())
l, err := NewLiteralFromShape(MakeShape(dtype))
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions xlabuilder/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package xlabuilder
import "C"
import (
"fmt"
"github.com/gomlx/exceptions"
"github.com/gomlx/gopjrt/dtypes"
"github.com/gomlx/gopjrt/protos/xla_data"
"github.com/pkg/errors"
Expand All @@ -33,12 +32,13 @@ type Shape struct {
}

// MakeShape filled with the values given.
// It doesn't work for tuple shapes.
//
// The dimensions must be >= 1, and it doesn't work for tuple shapes.
func MakeShape(dtype dtypes.DType, dimensions ...int) Shape {
s := Shape{Dimensions: slices.Clone(dimensions), DType: dtype}
for _, dim := range dimensions {
if dim <= 0 {
exceptions.Panicf("shapes.Make(%+v): cannot create a shape with an axis with dimension <= 0", s)
panicf("shapes.Make(%+v): cannot create a shape with an axis with dimension <= 0", s)
}
}
return s
Expand Down
5 changes: 2 additions & 3 deletions xlabuilder/special_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package xlabuilder

import (
"fmt"
"github.com/gomlx/exceptions"
"github.com/gomlx/gopjrt/dtypes"
"github.com/gomlx/gopjrt/protos/xla_data"
"github.com/pkg/errors"
Expand Down Expand Up @@ -119,7 +118,7 @@ func DecodeIota(op *Op) (shape Shape, iotaAxis int) {
func Identity(input *Op) *Op {
builder := input.builder
if builder.IsNil() {
exceptions.Panicf("trying to access XlaBuilder that is nil or already destroyed")
panicf("trying to access XlaBuilder that is nil or already destroyed")
}
op := newOp(IdentityOp)
op.OpInputs = []*Op{input}
Expand Down Expand Up @@ -455,7 +454,7 @@ func Slice(x *Op, starts, limits, strides []int) (*Op, error) {
func DecodeSlice(op *Op) (starts, limits, strides []int) {
rank := op.OpInputs[0].Shape.Rank()
if len(op.IntsArg) != 3*rank {
exceptions.Panicf("DecodeSlice() has input of rank %d, but arguments don't have 3*%d elements, instead got %d",
panicf("DecodeSlice() has input of rank %d, but arguments don't have 3*%d elements, instead got %d -- probably the op as not a SliceOp?",
rank, 3*rank, len(op.IntsArg))
}
starts = op.IntsArg[0:rank]
Expand Down
Loading