lamp

package lamp

Lamp provides utilities to build state of the art machine learning applications

=Overview= Notable types and packages:

  • lamp.STen is a memory managed wrapper around aten.ATen, an off the heap, native n-dimensionl array backed by libtorch.
  • lamp.autograd implements reverse mode automatic differentiation.
  • lamp.nn contains neural network building blocks, see e.g. lamp.nn.Linear.
  • lamp.data.IOLoops implements a training loop and other data related abstractions.
  • lamp.knn implements k-nearest neighbor search on the CPU and GPU
  • lamp.umap.Umap implements the UMAP dimension reduction algorithm
  • lamp.onnx implements serialization of computation graphs into ONNX format
  • lamp.io contains CSV and NPY readers

===How to get data into lamp=== Use one of the file readers in lamp.io or one of the factories in lamp.STen$.

===How to define a custom neural network layer=== See the documentation on lamp.nn.GenericModule

===How to compose neural network layers=== See the documentation on lamp.nn

===How to train models=== See the training loops in lamp.data.IOLoops

Type members

Classlikes

case class BufferPair(source: STen, destination: STen)
Companion:
object
object BufferPair
Companion:
class
case object CPU extends Device
case class CudaDevice(i: Int) extends Device
sealed trait Device

Represents a device where tensors are stored and tensor operations are executed

Represents a device where tensors are stored and tensor operations are executed

Companion:
object
object Device
Companion:
class
class EmptyMovable[-R]
Companion:
object
Companion:
class
trait Movable[-R]
Companion:
object
object Movable
Companion:
class
case class NcclUniqueId(base64: String)
Companion:
object
Companion:
class
object STen

Companion object of lamp.STen

Companion object of lamp.STen

Companion:
class
case class STen

Memory managed, off-heap, GPU and CPU compatible N-dimensional array.

Memory managed, off-heap, GPU and CPU compatible N-dimensional array.

This class is a wrapper around aten.Tensor providing a more convenient API. All allocating operations require an implicit lamp.Scope.

STen instances are associated with a device which determines where the memory is allocated, and where the operations are performed. Operations on multiple tensors expect that all the arguments reside on the same device.

lamp.STen.options returns a lamp.STenOptions which describes the device, shape, data type and storage layout of a tensor. Most factory methods in the companion object in turn require a lamp.STenOptions to specify the device, data types and storage layout.

Naming convention of most operations follows libtorch. Operations return their result in a copy, i.e. not in place. These operations need a lamp.Scope. Operations whose name ends with an underscore are in place. Operations whose name contains out will write their results into the specified output tensor, these are in the companion object. Some operations are exempt from this naming rule, e.g. +=, -=, *= etc.

Semantics of operations follow those of libtorch with the same name. Many of the operations broadcasts. See https://numpy.org/doc/stable/user/basics.broadcasting.html#general-broadcasting-rules for broadcasting rules. In short:

  1. shapes are aligned from the right, extending with ones to the left as needed. 2. If two aligned dimensions are not matching but one of them is 1, then it is expanded to the size of the other dimension, pretending a copy of all its values. If two aligned dimension are not matching and neither of them is 1, then the operation fails. =Examples=
Scope.root { implicit scope =>
   val sum = Scope { implicit scope =>
   val ident = STen.eye(3, STenOptions.d)
   val ones = STen.ones(List(3, 3), STenOptions.d)
   ident + ones
   }
   assert(sum.toMat == mat.ones(3, 3) + mat.ident(3))
}

===Broadcasting examples===

// successful
3 x 4 x 6 A
   4 x 6 B
3 x 4 x 6 Result // B is repeated 3 times first dimensions

// successful
3 x 4 x 6 A
3 x 1 x 6 B
3 x 4 x 6 Result // B's second dimension is repeated 4 times

// fail
3 x 4 x 6 A
3 x 2 x 6 B
3 x 4 x 6 Result // 2 != 4

The companion object contains various factories which copy data from the JVM memory to STen tensors.

Companion:
object
case class STenOptions(value: TensorOptions)
Companion:
object
Companion:
class
final class Scope

Faciliates memory management of off-heap data structures.

Faciliates memory management of off-heap data structures.

Tracks allocations of aten.Tensor and aten.TensorOption instances.

aten.Tensor and aten.TensorOption instances are not freed up by the garbage collector. Lamp implements zoned memory management around these object. The managed counterpart of aten.Tensor is lamp.STen, while for aten.TensorOption it is lamp.STenOptions.

One can only create a lamp.STen instance with a lamp.Scope in implicit scope.

Create new scopes with lamp.Scope.root, lamp.Scope.apply or lamp.Scope.root.

=Examples=

// Scope.root returns Unit
Scope.root { implicit scope =>
   val sum = Scope { implicit scope =>
   // Intermediate values allocated in this block (`ident` and `ones`) are freed when
   // this block returns
   // The return value (`ident + ones`) of this block is moved to the outer scope
   val ident = STen.eye(3, STenOptions.d)
   val ones = STen.ones(List(3, 3), STenOptions.d)
   ident + ones
   }
   assert(sum.toMat == mat.ones(3, 3) + mat.ident(3))
   // `sum` is freed once this block exits
}
Companion:
object
object Scope
Companion:
class

Types

type Sc[_] = Scope

Value members

Concrete methods

def scope(implicit s: Scope): Scope
def scoped(r: Tensor)(implicit s: Scope): Tensor