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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
package tensor
import (
"github.com/pkg/errors"
)
// exported API for arithmetics and the stupidly crazy amount of overloaded semantics
// Add performs a pointwise a+b. a and b can either be float64 or Tensor
//
// If both operands are Tensor, shape is checked first.
// Even though the underlying data may have the same size (say (2,2) vs (4,1)), if they have different shapes, it will error out.
//
// Add performs elementwise addition on the Tensor(s). These operations are supported:
// Add(*Dense, scalar)
// Add(scalar, *Dense)
// Add(*Dense, *Dense)
// If the Unsafe flag is passed in, the data of the first tensor will be overwritten
func Add(a, b interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
var adder Adder
var oe standardEngine
var ok bool
switch at := a.(type) {
case Tensor:
oe = at.standardEngine()
switch bt := b.(type) {
case Tensor:
if !bt.Shape().IsScalar() && !at.Shape().IsScalar() { // non-scalar Tensor addition
if oe != nil {
return oe.Add(at, bt, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.Add(at, bt, opts...)
}
if adder, ok = at.Engine().(Adder); ok {
return adder.Add(at, bt, opts...)
}
if adder, ok = bt.Engine().(Adder); ok {
return adder.Add(at, bt, opts...)
}
return nil, errors.New("Neither engines of either operand support Add")
} else { // at least one of the operands is a scalar
var leftTensor bool
if !bt.Shape().IsScalar() {
leftTensor = false // a Scalar-Tensor * b Tensor
tmp := at
at = bt
bt = tmp
} else {
leftTensor = true // a Tensor * b Scalar-Tensor
}
if oe != nil {
return oe.AddScalar(at, bt, leftTensor, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.AddScalar(at, bt, leftTensor, opts...)
}
if adder, ok = at.Engine().(Adder); ok {
return adder.AddScalar(at, bt, leftTensor, opts...)
}
if adder, ok = bt.Engine().(Adder); ok {
return adder.AddScalar(at, bt, leftTensor, opts...)
}
return nil, errors.New("Neither engines of either operand support Add")
}
default:
if oe != nil {
return oe.AddScalar(at, bt, true, opts...)
}
if adder, ok = at.Engine().(Adder); ok {
return adder.AddScalar(at, bt, true, opts...)
}
return nil, errors.New("Operand A's engine does not support Add")
}
default:
switch bt := b.(type) {
case Tensor:
if oe = bt.standardEngine(); oe != nil {
return oe.AddScalar(bt, at, false, opts...)
}
if adder, ok = bt.Engine().(Adder); ok {
return adder.AddScalar(bt, at, false, opts...)
}
return nil, errors.New("Operand B's engine does not support Add")
default:
return nil, errors.Errorf("Cannot perform Add of %T and %T", a, b)
}
}
panic("Unreachable")
}
// Sub performs elementwise subtraction on the Tensor(s). These operations are supported:
// Sub(*Dense, scalar)
// Sub(scalar, *Dense)
// Sub(*Dense, *Dense)
// If the Unsafe flag is passed in, the data of the first tensor will be overwritten
func Sub(a, b interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
var suber Suber
var oe standardEngine
var ok bool
switch at := a.(type) {
case Tensor:
oe = at.standardEngine()
switch bt := b.(type) {
case Tensor:
if !bt.Shape().IsScalar() && !at.Shape().IsScalar() { // non-scalar Tensor substraction
if oe != nil {
return oe.Sub(at, bt, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.Sub(at, bt, opts...)
}
if suber, ok = at.Engine().(Suber); ok {
return suber.Sub(at, bt, opts...)
}
if suber, ok = bt.Engine().(Suber); ok {
return suber.Sub(at, bt, opts...)
}
return nil, errors.New("Neither engines of either operand support Sub")
} else { // at least one of the operands is a scalar
var leftTensor bool
if !bt.Shape().IsScalar() {
leftTensor = false // a Scalar-Tensor * b Tensor
tmp := at
at = bt
bt = tmp
} else {
leftTensor = true // a Tensor * b Scalar-Tensor
}
if oe != nil {
return oe.SubScalar(at, bt, leftTensor, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.SubScalar(at, bt, leftTensor, opts...)
}
if suber, ok = at.Engine().(Suber); ok {
return suber.SubScalar(at, bt, leftTensor, opts...)
}
if suber, ok = bt.Engine().(Suber); ok {
return suber.SubScalar(at, bt, leftTensor, opts...)
}
return nil, errors.New("Neither engines of either operand support Sub")
}
default:
if oe != nil {
return oe.SubScalar(at, bt, true, opts...)
}
if suber, ok = at.Engine().(Suber); ok {
return suber.SubScalar(at, bt, true, opts...)
}
return nil, errors.New("Operand A's engine does not support Sub")
}
default:
switch bt := b.(type) {
case Tensor:
if oe = bt.standardEngine(); oe != nil {
return oe.SubScalar(bt, at, false, opts...)
}
if suber, ok = bt.Engine().(Suber); ok {
return suber.SubScalar(bt, at, false, opts...)
}
return nil, errors.New("Operand B's engine does not support Sub")
default:
return nil, errors.Errorf("Cannot perform Sub of %T and %T", a, b)
}
}
panic("Unreachable")
}
// Mul performs elementwise multiplication on the Tensor(s). These operations are supported:
// Mul(*Dense, scalar)
// Mul(scalar, *Dense)
// Mul(*Dense, *Dense)
// If the Unsafe flag is passed in, the data of the first tensor will be overwritten
func Mul(a, b interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
var muler Muler
var oe standardEngine
var ok bool
switch at := a.(type) {
case Tensor:
oe = at.standardEngine()
switch bt := b.(type) {
case Tensor:
if !bt.Shape().IsScalar() && !at.Shape().IsScalar() { // non-scalar Tensor multiplication
if oe != nil {
return oe.Mul(at, bt, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.Mul(at, bt, opts...)
}
if muler, ok = at.Engine().(Muler); ok {
return muler.Mul(at, bt, opts...)
}
if muler, ok = bt.Engine().(Muler); ok {
return muler.Mul(at, bt, opts...)
}
return nil, errors.New("Neither engines of either operand support Mul")
} else { // at least one of the operands is a scalar
var leftTensor bool
if !bt.Shape().IsScalar() {
leftTensor = false // a Scalar-Tensor * b Tensor
tmp := at
at = bt
bt = tmp
} else {
leftTensor = true // a Tensor * b Scalar-Tensor
}
if oe != nil {
return oe.MulScalar(at, bt, leftTensor, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.MulScalar(at, bt, leftTensor, opts...)
}
if muler, ok = at.Engine().(Muler); ok {
return muler.MulScalar(at, bt, leftTensor, opts...)
}
if muler, ok = bt.Engine().(Muler); ok {
return muler.MulScalar(at, bt, leftTensor, opts...)
}
return nil, errors.New("Neither engines of either operand support Mul")
}
default: // a Tensor * b interface
if oe != nil {
return oe.MulScalar(at, bt, true, opts...)
}
if muler, ok = at.Engine().(Muler); ok {
return muler.MulScalar(at, bt, true, opts...)
}
return nil, errors.New("Operand A's engine does not support Mul")
}
default:
switch bt := b.(type) {
case Tensor: // b Tensor * a interface
if oe = bt.standardEngine(); oe != nil {
return oe.MulScalar(bt, at, false, opts...)
}
if muler, ok = bt.Engine().(Muler); ok {
return muler.MulScalar(bt, at, false, opts...)
}
return nil, errors.New("Operand B's engine does not support Mul")
default: // b interface * a interface
return nil, errors.Errorf("Cannot perform Mul of %T and %T", a, b)
}
}
panic("Unreachable")
}
// Div performs elementwise division on the Tensor(s). These operations are supported:
// Div(*Dense, scalar)
// Div(scalar, *Dense)
// Div(*Dense, *Dense)
// If the Unsafe flag is passed in, the data of the first tensor will be overwritten
func Div(a, b interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
var diver Diver
var oe standardEngine
var ok bool
switch at := a.(type) {
case Tensor:
oe = at.standardEngine()
switch bt := b.(type) {
case Tensor:
if !bt.Shape().IsScalar() && !at.Shape().IsScalar() { // non-scalar Tensor division
if oe != nil {
return oe.Div(at, bt, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.Div(at, bt, opts...)
}
if diver, ok = at.Engine().(Diver); ok {
return diver.Div(at, bt, opts...)
}
if diver, ok = bt.Engine().(Diver); ok {
return diver.Div(at, bt, opts...)
}
return nil, errors.New("Neither engines of either operand support Div")
} else { // at least one of the operands is a scalar
var leftTensor bool
if !bt.Shape().IsScalar() {
leftTensor = false // a Scalar-Tensor * b Tensor
tmp := at
at = bt
bt = tmp
} else {
leftTensor = true // a Tensor * b Scalar-Tensor
}
if oe != nil {
return oe.DivScalar(at, bt, leftTensor, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.DivScalar(at, bt, leftTensor, opts...)
}
if diver, ok = at.Engine().(Diver); ok {
return diver.DivScalar(at, bt, leftTensor, opts...)
}
if diver, ok = bt.Engine().(Diver); ok {
return diver.DivScalar(at, bt, leftTensor, opts...)
}
return nil, errors.New("Neither engines of either operand support Div")
}
default:
if oe != nil {
return oe.DivScalar(at, bt, true, opts...)
}
if diver, ok = at.Engine().(Diver); ok {
return diver.DivScalar(at, bt, true, opts...)
}
return nil, errors.New("Operand A's engine does not support Div")
}
default:
switch bt := b.(type) {
case Tensor:
if oe = bt.standardEngine(); oe != nil {
return oe.DivScalar(bt, at, false, opts...)
}
if diver, ok = bt.Engine().(Diver); ok {
return diver.DivScalar(bt, at, false, opts...)
}
return nil, errors.New("Operand B's engine does not support Div")
default:
return nil, errors.Errorf("Cannot perform Div of %T and %T", a, b)
}
}
panic("Unreachable")
}
// Pow performs elementwise exponentiation on the Tensor(s). These operations are supported:
// Pow(*Dense, scalar)
// Pow(scalar, *Dense)
// Pow(*Dense, *Dense)
// If the Unsafe flag is passed in, the data of the first tensor will be overwritten
func Pow(a, b interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
var power Power
var oe standardEngine
var ok bool
switch at := a.(type) {
case Tensor:
oe = at.standardEngine()
switch bt := b.(type) {
case Tensor:
if !bt.Shape().IsScalar() && !at.Shape().IsScalar() { // non-scalar Tensor exponentiation
if oe != nil {
return oe.Pow(at, bt, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.Pow(at, bt, opts...)
}
if power, ok = at.Engine().(Power); ok {
return power.Pow(at, bt, opts...)
}
if power, ok = bt.Engine().(Power); ok {
return power.Pow(at, bt, opts...)
}
return nil, errors.New("Neither engines of either operand support Pow")
} else { // at least one of the operands is a scalar
var leftTensor bool
if !bt.Shape().IsScalar() {
leftTensor = false // a Scalar-Tensor * b Tensor
tmp := at
at = bt
bt = tmp
} else {
leftTensor = true // a Tensor * b Scalar-Tensor
}
if oe != nil {
return oe.PowScalar(at, bt, leftTensor, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.PowScalar(at, bt, leftTensor, opts...)
}
if power, ok = at.Engine().(Power); ok {
return power.PowScalar(at, bt, leftTensor, opts...)
}
if power, ok = bt.Engine().(Power); ok {
return power.PowScalar(at, bt, leftTensor, opts...)
}
return nil, errors.New("Neither engines of either operand support Pow")
}
default:
if oe != nil {
return oe.PowScalar(at, bt, true, opts...)
}
if power, ok = at.Engine().(Power); ok {
return power.PowScalar(at, bt, true, opts...)
}
return nil, errors.New("Operand A's engine does not support Pow")
}
default:
switch bt := b.(type) {
case Tensor:
if oe = bt.standardEngine(); oe != nil {
return oe.PowScalar(bt, at, false, opts...)
}
if power, ok = bt.Engine().(Power); ok {
return power.PowScalar(bt, at, false, opts...)
}
return nil, errors.New("Operand B's engine does not support Pow")
default:
return nil, errors.Errorf("Cannot perform Pow of %T and %T", a, b)
}
}
panic("Unreachable")
}
// Mod performs elementwise modulo on the Tensor(s). These operations are supported:
// Mod(*Dense, scalar)
// Mod(scalar, *Dense)
// Mod(*Dense, *Dense)
// If the Unsafe flag is passed in, the data of the first tensor will be overwritten
func Mod(a, b interface{}, opts ...FuncOpt) (retVal Tensor, err error) {
var moder Moder
var oe standardEngine
var ok bool
switch at := a.(type) {
case Tensor:
oe = at.standardEngine()
switch bt := b.(type) {
case Tensor:
if !bt.Shape().IsScalar() && !at.Shape().IsScalar() { // non-scalar Tensor modulo
if oe != nil {
return oe.Mod(at, bt, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.Mod(at, bt, opts...)
}
if moder, ok = at.Engine().(Moder); ok {
return moder.Mod(at, bt, opts...)
}
if moder, ok = bt.Engine().(Moder); ok {
return moder.Mod(at, bt, opts...)
}
return nil, errors.New("Neither engines of either operand support Mod")
} else { // at least one of the operands is a scalar
var leftTensor bool
if !bt.Shape().IsScalar() {
leftTensor = false // a Scalar-Tensor * b Tensor
tmp := at
at = bt
bt = tmp
} else {
leftTensor = true // a Tensor * b Scalar-Tensor
}
if oe != nil {
return oe.ModScalar(at, bt, leftTensor, opts...)
}
if oe = bt.standardEngine(); oe != nil {
return oe.ModScalar(at, bt, leftTensor, opts...)
}
if moder, ok = at.Engine().(Moder); ok {
return moder.ModScalar(at, bt, leftTensor, opts...)
}
if moder, ok = bt.Engine().(Moder); ok {
return moder.ModScalar(at, bt, leftTensor, opts...)
}
return nil, errors.New("Neither engines of either operand support Mod")
}
default:
if oe != nil {
return oe.ModScalar(at, bt, true, opts...)
}
if moder, ok = at.Engine().(Moder); ok {
return moder.ModScalar(at, bt, true, opts...)
}
return nil, errors.New("Operand A's engine does not support Mod")
}
default:
switch bt := b.(type) {
case Tensor:
if oe = bt.standardEngine(); oe != nil {
return oe.ModScalar(bt, at, false, opts...)
}
if moder, ok = bt.Engine().(Moder); ok {
return moder.ModScalar(bt, at, false, opts...)
}
return nil, errors.New("Operand B's engine does not support Mod")
default:
return nil, errors.Errorf("Cannot perform Mod of %T and %T", a, b)
}
}
panic("Unreachable")
}
// Dot is a highly opinionated API for performing dot product operations on two *Denses, a and b.
// This function is opinionated with regard to the vector operations because of how it treats operations with vectors.
// Vectors in this package comes in two flavours - column or row vectors. Column vectors have shape (x, 1), while row vectors have shape (1, x).
//
// As such, it is easy to assume that performing a linalg operation on vectors would follow the same rules (i.e shapes have to be aligned for things to work).
// For the most part in this package, this is true. This function is one of the few notable exceptions.
//
// Here I give three specific examples of how the expectations of vector operations will differ.
// Given two vectors, a, b with shapes (4, 1) and (4, 1), Dot() will perform an inner product as if the shapes were (1, 4) and (4, 1). This will result in a scalar value
// Given matrix A and vector b with shapes (2, 4) and (1, 4), Dot() will perform a matrix-vector multiplication as if the shapes were (2,4) and (4,1). This will result in a column vector with shape (2,1)
// Given vector a and matrix B with shapes (3, 1) and (3, 2), Dot() will perform a matrix-vector multiplication as if it were Báµ€ * a
//
// The main reason why this opinionated route was taken was due to the author's familiarity with NumPy, and general laziness in translating existing machine learning algorithms
// to fit the API of the package.
func Dot(x, y Tensor, opts ...FuncOpt) (retVal Tensor, err error) {
if xdottir, ok := x.Engine().(Dotter); ok {
return xdottir.Dot(x, y, opts...)
}
if ydottir, ok := y.Engine().(Dotter); ok {
return ydottir.Dot(x, y, opts...)
}
return nil, errors.New("Neither x's nor y's engines support Dot")
}
// FMA performs Y = A * X + Y.
func FMA(a Tensor, x interface{}, y Tensor) (retVal Tensor, err error) {
if xTensor, ok := x.(Tensor); ok {
if oe := a.standardEngine(); oe != nil {
return oe.FMA(a, xTensor, y)
}
if oe := xTensor.standardEngine(); oe != nil {
return oe.FMA(a, xTensor, y)
}
if oe := y.standardEngine(); oe != nil {
return oe.FMA(a, xTensor, y)
}
if e, ok := a.Engine().(FMAer); ok {
return e.FMA(a, xTensor, y)
}
if e, ok := xTensor.Engine().(FMAer); ok {
return e.FMA(a, xTensor, y)
}
if e, ok := y.Engine().(FMAer); ok {
return e.FMA(a, xTensor, y)
}
} else {
if oe := a.standardEngine(); oe != nil {
return oe.FMAScalar(a, x, y)
}
if oe := y.standardEngine(); oe != nil {
return oe.FMAScalar(a, x, y)
}
if e, ok := a.Engine().(FMAer); ok {
return e.FMAScalar(a, x, y)
}
if e, ok := y.Engine().(FMAer); ok {
return e.FMAScalar(a, x, y)
}
}
return Mul(a, x, WithIncr(y))
}
// MatMul performs matrix-matrix multiplication between two Tensors
func MatMul(a, b Tensor, opts ...FuncOpt) (retVal Tensor, err error) {
if a.Dtype() != b.Dtype() {
err = errors.Errorf(dtypeMismatch, a.Dtype(), b.Dtype())
return
}
switch at := a.(type) {
case *Dense:
bt := b.(*Dense)
return at.MatMul(bt, opts...)
}
panic("Unreachable")
}
// MatVecMul performs matrix-vector multiplication between two Tensors. `a` is expected to be a matrix, and `b` is expected to be a vector
func MatVecMul(a, b Tensor, opts ...FuncOpt) (retVal Tensor, err error) {
if a.Dtype() != b.Dtype() {
err = errors.Errorf(dtypeMismatch, a.Dtype(), b.Dtype())
return
}
switch at := a.(type) {
case *Dense:
bt := b.(*Dense)
return at.MatVecMul(bt, opts...)
}
panic("Unreachable")
}
// Inner finds the inner products of two vector Tensors. Both arguments to the functions are eexpected to be vectors.
func Inner(a, b Tensor) (retVal interface{}, err error) {
if a.Dtype() != b.Dtype() {
err = errors.Errorf(dtypeMismatch, a.Dtype(), b.Dtype())
return
}
switch at := a.(type) {
case *Dense:
bt := b.(*Dense)
return at.Inner(bt)
}
panic("Unreachable")
}
// Outer performs the outer product of two vector Tensors. Both arguments to the functions are expected to be vectors.
func Outer(a, b Tensor, opts ...FuncOpt) (retVal Tensor, err error) {
if a.Dtype() != b.Dtype() {
err = errors.Errorf(dtypeMismatch, a.Dtype(), b.Dtype())
return
}
switch at := a.(type) {
case *Dense:
bt := b.(*Dense)
return at.Outer(bt, opts...)
}
panic("Unreachable")
}
// Contract performs a contraction of given tensors along given axes
func Contract(a, b Tensor, aAxes, bAxes []int) (retVal Tensor, err error) {
if a.Dtype() != b.Dtype() {
err = errors.Errorf(dtypeMismatch, a.Dtype(), b.Dtype())
return
}
switch at := a.(type) {
case *Dense:
bt := b.(*Dense)
return at.TensorMul(bt, aAxes, bAxes)
default:
panic("Unreachable")
}
}
|