1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
package main
import (
"io"
"text/template"
)
const importsArrowRaw = `import (
arrowArray "github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/bitutil"
arrowTensor "github.com/apache/arrow-go/v18/arrow/tensor"
arrow "github.com/apache/arrow-go/v18/arrow"
)
`
const conversionsRaw = `func convFromFloat64s(to Dtype, data []float64) interface{} {
switch to {
{{range .Kinds -}}
{{if isNumber . -}}
case {{reflectKind .}}:
{{if eq .String "float64" -}}
retVal := make([]float64, len(data))
copy(retVal, data)
return retVal
{{else if eq .String "float32" -}}
retVal := make([]float32, len(data))
for i, v := range data {
switch {
case math.IsNaN(v):
retVal[i] = math32.NaN()
case math.IsInf(v, 1):
retVal[i] = math32.Inf(1)
case math.IsInf(v, -1):
retVal[i] = math32.Inf(-1)
default:
retVal[i] = float32(v)
}
}
return retVal
{{else if eq .String "complex64" -}}
retVal := make([]complex64, len(data))
for i, v := range data {
switch {
case math.IsNaN(v):
retVal[i] = complex64(cmplx.NaN())
case math.IsInf(v, 0):
retVal[i] = complex64(cmplx.Inf())
default:
retVal[i] = complex(float32(v), float32(0))
}
}
return retVal
{{else if eq .String "complex128" -}}
retVal := make([]complex128, len(data))
for i, v := range data {
switch {
case math.IsNaN(v):
retVal[i] = cmplx.NaN()
case math.IsInf(v, 0):
retVal[i] = cmplx.Inf()
default:
retVal[i] = complex(v, float64(0))
}
}
return retVal
{{else -}}
retVal := make([]{{asType .}}, len(data))
for i, v :=range data{
switch {
case math.IsNaN(v), math.IsInf(v, 0):
retVal[i] = 0
default:
retVal[i] = {{asType .}}(v)
}
}
return retVal
{{end -}}
{{end -}}
{{end -}}
default:
panic("Unsupported Dtype")
}
}
func convToFloat64s(t *Dense) (retVal []float64){
retVal = make([]float64, t.len())
switch t.t{
{{range .Kinds -}}
{{if isNumber . -}}
case {{reflectKind .}}:
{{if eq .String "float64" -}}
return t.{{sliceOf .}}
{{else if eq .String "float32" -}}
for i, v := range t.{{sliceOf .}} {
switch {
case math32.IsNaN(v):
retVal[i] = math.NaN()
case math32.IsInf(v, 1):
retVal[i] = math.Inf(1)
case math32.IsInf(v, -1):
retVal[i] = math.Inf(-1)
default:
retVal[i] = float64(v)
}
}
{{else if eq .String "complex64" -}}
for i, v := range t.{{sliceOf .}} {
switch {
case cmplx.IsNaN(complex128(v)):
retVal[i] = math.NaN()
case cmplx.IsInf(complex128(v)):
retVal[i] = math.Inf(1)
default:
retVal[i] = float64(real(v))
}
}
{{else if eq .String "complex128" -}}
for i, v := range t.{{sliceOf .}} {
switch {
case cmplx.IsNaN(v):
retVal[i] = math.NaN()
case cmplx.IsInf(v):
retVal[i] = math.Inf(1)
default:
retVal[i] = real(v)
}
}
{{else -}}
for i, v := range t.{{sliceOf .}} {
retVal[i]= float64(v)
}
{{end -}}
return retVal
{{end -}}
{{end -}}
default:
panic(fmt.Sprintf("Cannot convert *Dense of %v to []float64", t.t))
}
}
func convToFloat64(x interface{}) float64 {
switch xt := x.(type) {
{{range .Kinds -}}
{{if isNumber . -}}
case {{asType .}}:
{{if eq .String "float64 -"}}
return xt
{{else if eq .String "complex64" -}}
return float64(real(xt))
{{else if eq .String "complex128" -}}
return real(xt)
{{else -}}
return float64(xt)
{{end -}}
{{end -}}
{{end -}}
default:
panic("Cannot convert to float64")
}
}
`
const compatRaw = `// FromMat64 converts a *"gonum/matrix/mat64".Dense into a *tensorf64.Tensor.
func FromMat64(m *mat.Dense, opts ...FuncOpt) *Dense {
r, c := m.Dims()
fo := ParseFuncOpts(opts...)
defer returnOpOpt(fo)
toCopy := fo.Safe()
as := fo.As()
if as.Type == nil {
as = Float64
}
switch as.Kind() {
{{range .Kinds -}}
{{if isNumber . -}}
case reflect.{{reflectKind .}}:
{{if eq .String "float64" -}}
var backing []float64
if toCopy {
backing = make([]float64, len(m.RawMatrix().Data))
copy(backing, m.RawMatrix().Data)
} else {
backing = m.RawMatrix().Data
}
{{else -}}
backing := convFromFloat64s({{asType . | title}}, m.RawMatrix().Data).([]{{asType .}})
{{end -}}
retVal := New(WithBacking(backing), WithShape(r, c))
return retVal
{{end -}}
{{end -}}
default:
panic(fmt.Sprintf("Unsupported Dtype - cannot convert float64 to %v", as))
}
panic("Unreachable")
}
// ToMat64 converts a *Dense to a *mat.Dense. All the values are converted into float64s.
// This function will only convert matrices. Anything *Dense with dimensions larger than 2 will cause an error.
func ToMat64(t *Dense, opts ...FuncOpt) (retVal *mat.Dense, err error) {
// checks:
if !t.IsNativelyAccessible() {
return nil, errors.Errorf("Cannot convert *Dense to *mat.Dense. Data is inaccessible")
}
if !t.IsMatrix() {
// error
return nil, errors.Errorf("Cannot convert *Dense to *mat.Dense. Expected number of dimensions: <=2, T has got %d dimensions (Shape: %v)", t.Dims(), t.Shape())
}
fo := ParseFuncOpts(opts...)
defer returnOpOpt(fo)
toCopy := fo.Safe()
// fix dims
r := t.Shape()[0]
c := t.Shape()[1]
var data []float64
switch {
case t.t == Float64 && toCopy && !t.IsMaterializable():
data = make([]float64, t.len())
copy(data, t.Float64s())
case !t.IsMaterializable():
data = convToFloat64s(t)
default:
it := newFlatIterator(&t.AP)
var next int
for next, err = it.Next(); err == nil; next, err = it.Next() {
if err = handleNoOp(err); err != nil {
return
}
data = append(data, convToFloat64(t.Get(next)))
}
err = nil
}
retVal = mat.NewDense(r, c, data)
return
}
`
type ArrowData struct {
BinaryTypes []string
FixedWidthTypes []string
PrimitiveTypes []string
}
const compatArrowArrayRaw = `// FromArrowArray converts an "arrow/array".Interface into a Tensor of matching DataType.
func FromArrowArray(a arrowArray.Interface) *Dense {
a.Retain()
defer a.Release()
r := a.Len()
// TODO(poopoothegorilla): instead of creating bool ValidMask maybe
// bitmapBytes can be used from arrow API
mask := make([]bool, r)
for i := 0; i < r; i++ {
mask[i] = a.IsNull(i)
}
switch a.DataType() {
{{range .BinaryTypes -}}
case arrow.BinaryTypes.{{.}}:
{{if eq . "String" -}}
backing := make([]string, r)
for i := 0; i < r; i++ {
backing[i] = a.(*arrowArray.{{.}}).Value(i)
}
{{else -}}
backing := a.(*arrowArray.{{.}}).{{.}}Values()
{{end -}}
retVal := New(WithBacking(backing, mask), WithShape(r, 1))
return retVal
{{end -}}
{{range .FixedWidthTypes -}}
case arrow.FixedWidthTypes.{{.}}:
{{if eq . "Boolean" -}}
backing := make([]bool, r)
for i := 0; i < r; i++ {
backing[i] = a.(*arrowArray.{{.}}).Value(i)
}
{{else -}}
backing := a.(*arrowArray.{{.}}).{{.}}Values()
{{end -}}
retVal := New(WithBacking(backing, mask), WithShape(r, 1))
return retVal
{{end -}}
{{range .PrimitiveTypes -}}
case arrow.PrimitiveTypes.{{.}}:
backing := a.(*arrowArray.{{.}}).{{.}}Values()
retVal := New(WithBacking(backing, mask), WithShape(r, 1))
return retVal
{{end -}}
default:
panic(fmt.Sprintf("Unsupported Arrow DataType - %v", a.DataType()))
}
panic("Unreachable")
}
`
const compatArrowTensorRaw = `// FromArrowTensor converts an "arrow/tensor".Interface into a Tensor of matching DataType.
func FromArrowTensor(a arrowTensor.Interface) *Dense {
a.Retain()
defer a.Release()
if !a.IsContiguous() {
panic("Non-contiguous data is Unsupported")
}
var shape []int
for _, val := range a.Shape() {
shape = append(shape, int(val))
}
l := a.Len()
validMask := a.Data().Buffers()[0].Bytes()
dataOffset := a.Data().Offset()
mask := make([]bool, l)
for i := 0; i < l; i++ {
mask[i] = len(validMask) != 0 && bitutil.BitIsNotSet(validMask, dataOffset+i)
}
switch a.DataType() {
{{range .PrimitiveTypes -}}
case arrow.PrimitiveTypes.{{.}}:
backing := a.(*arrowTensor.{{.}}).{{.}}Values()
if a.IsColMajor() {
return New(WithShape(shape...), AsFortran(backing, mask))
}
return New(WithShape(shape...), WithBacking(backing, mask))
{{end -}}
default:
panic(fmt.Sprintf("Unsupported Arrow DataType - %v", a.DataType()))
}
panic("Unreachable")
}
`
var (
importsArrow *template.Template
conversions *template.Template
compats *template.Template
compatsArrowArray *template.Template
compatsArrowTensor *template.Template
)
func init() {
importsArrow = template.Must(template.New("imports_arrow").Funcs(funcs).Parse(importsArrowRaw))
conversions = template.Must(template.New("conversions").Funcs(funcs).Parse(conversionsRaw))
compats = template.Must(template.New("compat").Funcs(funcs).Parse(compatRaw))
compatsArrowArray = template.Must(template.New("compat_arrow_array").Funcs(funcs).Parse(compatArrowArrayRaw))
compatsArrowTensor = template.Must(template.New("compat_arrow_tensor").Funcs(funcs).Parse(compatArrowTensorRaw))
}
func generateDenseCompat(f io.Writer, generic Kinds) {
// NOTE(poopoothegorilla): an alias is needed for the Arrow Array pkg to prevent naming
// collisions
importsArrow.Execute(f, generic)
conversions.Execute(f, generic)
compats.Execute(f, generic)
arrowData := ArrowData{
BinaryTypes: arrowBinaryTypes,
FixedWidthTypes: arrowFixedWidthTypes,
PrimitiveTypes: arrowPrimitiveTypes,
}
compatsArrowArray.Execute(f, arrowData)
compatsArrowTensor.Execute(f, arrowData)
}
|