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

Attributes

Members list

Packages

package lamp.data

Type members

Classlikes

case class BufferPair(source: STen, destination: STen)

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object BufferPair

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
BufferPair.type
case object CPU extends Device

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
trait Device
class Object
trait Matchable
class Any
Show all
Self type
CPU.type
case class CudaDevice(i: Int) extends Device

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
trait Device
class Object
trait Matchable
class Any
Show all
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

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
Known subtypes
object CPU.type
class CudaDevice
object MPS.type
Self type
object Device

Attributes

Companion
trait
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
Device.type

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
Self type
class EmptyMovable[-R]

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
object EmptyMovable

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
sealed trait FloatingPointPrecision

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
object DoublePrecision.type
object HalfPrecision.type
object SinglePrecision.type

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
Self type
case object MPS extends Device

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
trait Device
class Object
trait Matchable
class Any
Show all
Self type
MPS.type
trait Movable[-R]

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
object Movable

Attributes

Companion
trait
Supertypes
class Object
trait Matchable
class Any
Self type
Movable.type
case class NcclUniqueId(base64: String)

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object NcclUniqueId

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
object STen

Companion object of lamp.STen

Companion object of lamp.STen

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
STen.type
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.

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
case class STenOptions(value: TensorOptions)

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object STenOptions

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
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
}

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
object Scope

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
Scope.type

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
Self type
object TensorHelpers

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type

Types

type Sc[_] = Scope

Value members

Concrete methods

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