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
|
package tensor
import (
"reflect"
"gorgonia.org/tensor/internal/storage"
)
// ConsOpt is a tensor construction option.
type ConsOpt func(Tensor)
// Of is a construction option for a Tensor.
func Of(a Dtype) ConsOpt {
Register(a)
f := func(t Tensor) {
switch tt := t.(type) {
case *Dense:
tt.t = a
case *CS:
tt.t = a
default:
panic("Unsupported Tensor type")
}
}
return f
}
// WithBacking is a construction option for a Tensor
// Use it as such:
// backing := []float64{1,2,3,4}
// t := New(WithBacking(backing))
// It can be used with other construction options like WithShape
func WithBacking(x interface{}, argMask ...[]bool) ConsOpt {
var mask []bool
if len(argMask) > 0 {
mask = argMask[0]
}
f := func(t Tensor) {
if x == nil {
return
}
switch tt := t.(type) {
case *Dense:
tt.fromSlice(x)
if len(argMask) > 0 {
tt.addMask(mask)
}
default:
panic("Unsupported Tensor type")
}
}
return f
}
// WithMask is a construction option for a Tensor
// Use it as such:
// mask := []bool{true,true,false,false}
// t := New(WithBacking(backing), WithMask(mask))
// It can be used with other construction options like WithShape
// The supplied mask can be any type. If non-boolean, then tensor mask is set to true
// wherever non-zero value is obtained
func WithMask(x interface{}) ConsOpt {
f := func(t Tensor) {
if x == nil {
return
}
switch tt := t.(type) {
case *Dense:
tt.MaskFromSlice(x)
default:
panic("Unsupported Tensor type")
}
}
return f
}
// WithShape is a construction option for a Tensor. It creates the ndarray in the required shape.
func WithShape(dims ...int) ConsOpt {
f := func(t Tensor) {
switch tt := t.(type) {
case *Dense:
throw := BorrowInts(len(dims))
copy(throw, dims)
tt.setShape(throw...)
case *CS:
if len(dims) != 2 {
panic("Only sparse matrices are supported")
}
throw := BorrowInts(len(dims))
copy(throw, dims)
tt.s = throw
default:
panic("Unsupported Tensor type")
}
}
return f
}
// FromScalar is a construction option for representing a scalar value as a Tensor
func FromScalar(x interface{}, argMask ...[]bool) ConsOpt {
var mask []bool
if len(argMask) > 0 {
mask = argMask[0]
}
f := func(t Tensor) {
switch tt := t.(type) {
case *Dense:
xT := reflect.TypeOf(x)
sxT := reflect.SliceOf(xT)
xv := reflect.MakeSlice(sxT, 1, 1) // []T
xv0 := xv.Index(0) // xv[0]
xv0.Set(reflect.ValueOf(x))
tt.array.Header.Raw = storage.AsByteSlice(xv.Interface())
tt.t = Dtype{xT}
tt.mask = mask
default:
panic("Unsupported Tensor Type")
}
}
return f
}
// FromMemory is a construction option for creating a *Dense (for now) from memory location. This is a useful
// option for super large tensors that don't fit into memory - the user may need to `mmap` a file the tensor.
//
// Bear in mind that at the current stage of the ConsOpt design, the order of the ConsOpt is important.
// FromMemory requires the *Dense's Dtype be set already.
// This would fail (and panic):
// New(FromMemory(ptr, size), Of(Float64))
// This would not:
// New(Of(Float64), FromMemory(ptr, size))
// This behaviour of requiring the ConsOpts to be in order might be changed in the future.
//
// Memory must be manually managed by the caller.
// Tensors called with this construction option will not be returned to any pool - rather, all references to the pointers will be null'd.
// Use with caution.
//go:nocheckptr
func FromMemory(ptr uintptr, memsize uintptr) ConsOpt {
f := func(t Tensor) {
switch tt := t.(type) {
case *Dense:
tt.Header.Raw = nil // GC anything if needed
tt.Header.Raw = storage.FromMemory(ptr, memsize)
tt.flag = MakeMemoryFlag(tt.flag, ManuallyManaged)
default:
panic("Unsupported Tensor type")
}
}
return f
}
// WithEngine is a construction option that would cause a Tensor to be linked with an execution engine.
func WithEngine(e Engine) ConsOpt {
f := func(t Tensor) {
switch tt := t.(type) {
case *Dense:
tt.e = e
if e != nil && !e.AllocAccessible() {
tt.flag = MakeMemoryFlag(tt.flag, NativelyInaccessible)
}
tt.oe = nil
if oe, ok := e.(standardEngine); ok {
tt.oe = oe
}
case *CS:
tt.e = e
if e != nil && !e.AllocAccessible() {
tt.f = MakeMemoryFlag(tt.f, NativelyInaccessible)
}
}
}
return f
}
// AsFortran creates a *Dense with a col-major layout.
// If the optional backing argument is passed, the backing is assumed to be C-order (row major), and
// it will be transposed before being used.
func AsFortran(backing interface{}, argMask ...[]bool) ConsOpt {
var mask []bool
if len(argMask) > 0 {
mask = argMask[0]
}
f := func(t Tensor) {
switch tt := t.(type) {
case *Dense:
if backing != nil {
// put the data into the tensor, then make a clone tensor to transpose
tt.fromSliceOrArrayer(backing)
// create a temporary tensor, to which the transpose will be done
tmp := NewDense(tt.Dtype(), tt.shape.Clone())
copyArray(tmp.arrPtr(), tt.arrPtr())
tmp.SetMask(mask)
tmp.T()
tmp.Transpose()
// copy the data back to the current tensor
copyArray(tt.arrPtr(), tmp.arrPtr())
tt.SetMask(tmp.Mask())
// cleanup: return the temporary tensor back to the pool
ReturnTensor(tmp)
}
tt.AP.o = MakeDataOrder(tt.AP.o, ColMajor)
if tt.AP.shape != nil {
ReturnInts(tt.AP.strides)
tt.AP.strides = nil
tt.AP.strides = tt.AP.calcStrides()
}
case *CS:
panic("AsFortran is not an available option for Compressed Sparse layouts")
}
}
return f
}
func AsDenseDiag(backing interface{}) ConsOpt {
f := func(t Tensor) {
switch tt := t.(type) {
case *Dense:
if bt, ok := backing.(Tensor); ok {
backing = bt.Data()
}
xT := reflect.TypeOf(backing)
if xT.Kind() != reflect.Slice {
panic("Expected a slice")
}
xV := reflect.ValueOf(backing)
l := xV.Len()
// elT := xT.Elem()
sli := reflect.MakeSlice(xT, l*l, l*l)
shape := Shape{l, l}
strides := shape.CalcStrides()
for i := 0; i < l; i++ {
idx, err := Ltoi(shape, strides, i, i)
if err != nil {
panic(err)
}
at := sli.Index(idx)
xi := xV.Index(i)
at.Set(xi)
}
tt.fromSliceOrArrayer(sli.Interface())
tt.setShape(l, l)
default:
panic("AsDenseDiag is not available as an option for CS")
}
}
return f
}
|