Coder Social home page Coder Social logo

ctongfei / nexus Goto Github PK

View Code? Open in Web Editor NEW
255.0 25.0 15.0 3.45 MB

Experimental tensor-typed deep learning

Home Page: https://tongfei.me/nexus/

License: MIT License

Scala 96.97% TeX 0.13% Java 1.27% C++ 0.01% Python 0.33% Makefile 1.29%
deep-learning scala tensor probabilistic-programming differentiable-programming neural-network

nexus's Introduction

Nexus

🚧 Ongoing project 🚧 Status: Prototype 🚧

Nexus is a prototypical typesafe deep learning system in Scala.

Nexus is a departure from common deep learning libraries such as TensorFlow, PyTorch, MXNet, etc.

  • Ever been baffled by the axes of tensors? Which axis should I max out?
  • Ever got TypeErrors in Python?
  • Ever spending hours or days getting the tensors' axes and dimensions right?

Nexus' answer to these problems is static types. By specifying tensor axes' semantics in types exploiting Scala's expressive types, compilers can validate the program at compile time, freeing developers' burden of remembering axes by heart, and eliminating nearly all errors above before even running.

Nexus embraces declarative and functional programming: Neural networks are built using small composable components, making code very easy to follow, understand and maintain.

A first glance

A simple neural network for learning the XOR function can be found here.

Building a typesafe XOR network:

  class In extends Dim;     val In = new In          
  class Hidden extends Dim; val Hidden = new Hidden
  class Out extends Dim;    val Out = new Out // tensor axis labels declared as types and singletons

  val x = Input[FloatTensor[In]]()     // input vectors
  val y = Input[FloatTensor[Out]]()    // gold labels

  val ŷ = x                       |>   // type: Symbolic[FloatTensor[In]]
    Affine(In -> 2, Hidden -> 2)  |>   // type: Symbolic[FloatTensor[Hidden]]
    Logistic                      |>   // type: Symbolic[FloatTensor[Hidden]]
    Affine(Hidden -> 2, Out -> 2) |>   // type: Symbolic[FloatTensor[Out]]
    Softmax                            // type: Symbolic[FloatTensor[Out]]
  val loss = CrossEntropy(y, ŷ)        // type: Symbolic[Float]

Design goals

  • Typeful. Each axis of a tensor is statically typed using tuples. For example, an image is typed as FloatTensor[(Width, Height, Channel)], whereas an embedded sentence is typed as FloatTensor[(Word, Embedding)]. This frees programmers from remembering what each axis stands for.
  • Typesafe. Very strong static type checking to eliminate most bugs at compile time.
  • Never, ever specify axis index again. For things like reduce_sum(x, axis=1), write x |> SumAlong(AxisName).
  • Automatic typeclass derivation: Differentiation through any case class (product type).
  • Versatile switching between eager and lazy evaluation.
  • [TODO] Typesafe tensor sizes using literal singleton types (Scala 2.13+).
  • [TODO] Automatic batching over sequences/trees (Neubig, Goldberg, Dyer, NIPS 2017). Free programmers from the pain of manual batching.
  • [TODO] GPU Acceleration. Reuse Torch C++ core through Swig (bindings).
  • [TODO] Multiple backends. Torch / MXNet? / TensorFlow.js for Scala.js? / libtorch for ScalaNative?
  • [TODO] Automatic operator fusion for optimization.
  • [TODO] Typesafe higher-order gradients / Jacobians.

Modules

Nexus is modularized. It contains the following modules:

Module Description
nexus-tensor Foundations for typesafe tensors
nexus-diff Typesafe deep learning (differentiable programming)
nexus-prob Typesafe probabilistic programming
nexus-ml High-level machine learning abstractions / models
nexus-jvm-backend JVM reference backend (slow)
nexus-torch Torch native CPU backend
nexus-torch-cuda Torch CUDA GPU backend

Citation

Please cite this in academic work as

@inproceedings{chen2017typesafe,
 author = {Chen, Tongfei},
 title = {Typesafe Abstractions for Tensor Operations (Short Paper)},
 booktitle = {Proceedings of the 8th ACM SIGPLAN International Symposium on Scala},
 series = {SCALA 2017},
 year = {2017},
 pages = {45--50},
 url = {http://doi.acm.org/10.1145/3136000.3136001},
 doi = {10.1145/3136000.3136001}
}

nexus's People

Contributors

ctongfei avatar fvlankvelt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nexus's Issues

Support for Complex / ComplexTensor

nexus.algebra.IsComplex[C]
nexus.algebra.IsComplexTensorK[TC[_], C, TR[_], R]

Update existing ops if they support complex
Additional complex ops:
nexus.ops.{Re, Im, Conj, Arg}

`fatal error: cuda_runtime_api.h` (linux)

Similar error to #24 (identical?)

$ ./build.sh 
PyTorch 1.0.0 at /shared/shelley/khashab2/nexus/env3.6/lib/python3.6/site-packages/torch detected
Operating system is linux-gnu
Copying include files...
Preprocessing all header files for SWIG to parse...
Preprocessing C headers...
In file included from torch.h:1:0:
TH/TH.h:4:26: fatal error: TH/THGeneral.h: No such file or directory
compilation terminated.
Generating SWIG bindings...
Language subdirectory: java
Search paths:
   ./
   ./swig_lib/java/
   /usr/share/swig3.0/java/
   ./swig_lib/
   /usr/share/swig3.0/
Preprocessing...
Starting language-specific parse...
Processing types...
C++ analysis...
Processing nested classes...
Generating wrappers...
Compiling SWIG generated JNI wrapper code...
Compiling using Java: /usr/lib/jvm/java-8-openjdk-amd64
In file included from /shared/shelley/khashab2/nexus/env3.6/lib/python3.6/site-packages/torch/lib/include/THC/THCGeneral.h:12:0,
                 from /shared/shelley/khashab2/nexus/env3.6/lib/python3.6/site-packages/torch/lib/include/THC/THC.h:4,
                 from torch_wrap_fixed.cxx:236:
/shared/shelley/khashab2/nexus/env3.6/lib/python3.6/site-packages/torch/lib/include/ATen/cuda/CUDAStream.h:6:30: fatal error: cuda_runtime_api.h: No such file or directory
compilation terminated.

Btw, I have cuda installed on my machine:

$ cuda
cuda                         cudafe                       cudafe++                     cuda-gdb                     cuda-gdbserver               cuda-install-samples-9.1.sh  cuda-memcheck   

SymDiff Implementation?

The "Symmetric Difference" approach to generalize some often recurring tensor operations is cool. With backward autograd being very elegant.

However, the implementation is currently just a stub. This also seems to require serious type-level scala programming, at least more that I'm familiar with. @ctongfei have you been able to make this work at some time in the past?

IndexTensor

Amazing idea from @kitsing:

A LongTensor tensor used as an indexer should not be typed as LongTensor[U], instead it should be IndexTensor[U, X <: Dim], where X is the single dimension which it is indexed against. This creates extra typesafety:

oneHot: TI[U, I] => TR[Append[U, I]]
argmax: (T[U], I) => TI[Remove[U, I], I]

Compile error: int.scala:6: object creation impossible, since: [error] it has 2 unimplemented members.

The error message:

[error] /Users/daniel/ideaProjects/nexus/tensor/src/main/scala/nexus/instances/int.scala:6: object creation impossible, since:
[error] it has 2 unimplemented members.
[error] /** As seen from object ByteIsInt, the missing signatures are as follows.
[error]  *  For convenience, these are usable as stub implementations.
[error]  */
[error]   def fromLong(x: Long): Byte = ???
[error]   def toLong(x: Byte): Long = ???
[error] object ByteIsInt extends IsInt[Byte] {
[error]        ^
[error] /Users/daniel/ideaProjects/nexus/tensor/src/main/scala/nexus/instances/int.scala:23: object creation impossible, since:
[error] it has 2 unimplemented members.
[error] /** As seen from object ShortIsInt, the missing signatures are as follows.
[error]  *  For convenience, these are usable as stub implementations.
[error]  */
[error]   def fromLong(x: Long): Short = ???
[error]   def toLong(x: Short): Long = ???
[error] object ShortIsInt extends IsInt[Short] {
[error]        ^
[error] /Users/daniel/ideaProjects/nexus/tensor/src/main/scala/nexus/instances/int.scala:40: object creation impossible, since:
[error] it has 2 unimplemented members.
[error] /** As seen from object IntIsInt, the missing signatures are as follows.
[error]  *  For convenience, these are usable as stub implementations.
[error]  */
[error]   def fromLong(x: Long): Int = ???
[error]   def toLong(x: Int): Long = ???
[error] object IntIsInt extends IsInt[Int] {
[error]        ^
[error] /Users/daniel/ideaProjects/nexus/tensor/src/main/scala/nexus/instances/int.scala:57: object creation impossible, since:
[error] it has 2 unimplemented members.
[error] /** As seen from object LongIsInt, the missing signatures are as follows.
[error]  *  For convenience, these are usable as stub implementations.
[error]  */
[error]   def fromLong(x: Long): Long = ???
[error]   def toLong(x: Long): Long = ???
[error] object LongIsInt extends IsInt[Long] {
[error]        ^
[error] /Users/daniel/ideaProjects/nexus/tensor/src/main/scala/nexus/instances/real.scala:5: object creation impossible, since method toDouble in trait IsReal of type (x: Float)Double is not defined
[error] object FloatIsReal extends IsReal[Float] {
[error]        ^
[error] /Users/daniel/ideaProjects/nexus/tensor/src/main/scala/nexus/instances/real.scala:44: object creation impossible, since method toDouble in trait IsReal of type (x: Double)Double is not defined
[error] object DoubleIsReal extends IsReal[Double] {
[error]        ^
[error] 6 errors found
[error] (tensor/compile:compileIncremental) Compilation failed
[error] Total time: 7 s, completed Dec 31, 2018 11:42:21 AM

Implement convolutional layers

Convolutional layers are necessary for a range of deep network architectures, to leverage invariance in certain dimensions (e.g. x, y coordinates in images).

Customizable gradient computation

For certain 2nd-order optimization algorithms, e.g. (Martens & Grosse, 2015 JMLR):

  def backward[G[_]: Algebra, X, Y](dy: G[Y], y: Y, x: X): G[X]

where G[_] encapsulates backward computations. Trivially G = Id.

Jacobian (extension of gradients)

Given D[_]: DifferentiableAlgebra,

  def jacobian[X, Y, `∂Y/∂X`](y: D[Y], x: D[X])(implicit J: Jacobian.Aux[X, Y, `∂Y/∂X`]): `∂Y/∂X`

How should we define Jacobian.Aux for all differentiable types?

NEScala unconference session

A few of us have talked about doing a session around understanding / using / contributing to nexus tomorrow at the NEScala unconference.

If @ctongfei or anyone wants to discuss or participate remotely, let me know!

cc @non

Does not compile

As of the latest ( cfe4f00 ), this project does not compile.
First, there is no project/build.properties included, therefore which version of sbt to use is not immediately clear. Determine to use 0.13.x.
Second, the sbt unidoc plugin is missing.
Finally, I get compile errors: ToHList.scala:29: not found: type Label and IsRealTensorH.scala:104: not found: type IsAxisGroundedRealTensor
Tried earlier commits, also no success.

torch backend build failed

when building torch backend it gives :

Preprocessing C headers...
In file included from torch.h:1:
TH/TH.h:4:10: fatal error: TH/THGeneral.h: No such file or directory
 #include "TH/THGeneral.h"
          ^~~~~~~~~~~~~~~~
compilation terminated.
Generating SWIG bindings...
Language subdirectory: java
Search paths:
   ./
   ./swig_lib/java/
   /usr/share/swig/3.0.12/java/
   ./swig_lib/
   /usr/share/swig/3.0.12/
Preprocessing...
Starting language-specific parse...
Processing types...
C++ analysis...
Processing nested classes...
Generating wrappers...
Compiling SWIG generated JNI wrapper code...
In file included from /usr/lib/python3.7/site-packages/torch-1.0.0-py3.7-linux-x86_64.egg/torch/lib/include/THC/THCGeneral.h:12,
                 from /usr/lib/python3.7/site-packages/torch-1.0.0-py3.7-linux-x86_64.egg/torch/lib/include/THC/THC.h:4,
                 from torch_wrap_fixed.cxx:238:
/usr/lib/python3.7/site-packages/torch-1.0.0-py3.7-linux-x86_64.egg/torch/lib/include/ATen/cuda/CUDAStream.h:6:10: fatal error: cuda_runtime_api.h: No such file or directory
 #include "cuda_runtime_api.h"
          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.

torch : /usr/lib/python3.7/site-packages/torch-1.0.0-py3.7-linux-x86_64.egg/

numpy-style indexing

Would be nice if it is possible to index tensors with other index-typed tensors.

How do you 'include' your project?

I'm new to scala, and I'm having difficulties adding dependency with your project using sbt.
Even simply loading the project using sbt command will give [error] (torch/*:update) sbt.ResolveException: unresolved dependency: me.tongfei#jtorch-cpu;0.3.0-SNAPSHOT: not found.

Could you please add a guide about how to use nexus?

Issue in installing `Torch` (MacOS)

After trying to build it, here is what I get:

> ./build.sh 
PyTorch 1.0.0 at /Users/daniel/miniconda3/lib/python3.7/site-packages/torch detected
Operating system is darwin17
Copying include files...
cp: /Users/daniel/miniconda3/lib/python3.7/site-packages/torch/lib/include/THC: No such file or directory

Taking a closer look into the include folder, here is what I see:

> ls -lha /Users/daniel/miniconda3/lib/python3.7/site-packages/torch/lib/include/ 
total 0
drwxr-xr-x    9 daniel  staff   288B Dec 30 15:48 .
drwxr-xr-x   20 daniel  staff   640B Dec 30 15:48 ..
drwxr-xr-x  143 daniel  staff   4.5K Dec 30 15:48 ATen
drwxr-xr-x   39 daniel  staff   1.2K Dec 30 15:48 TH
drwxr-xr-x    4 daniel  staff   128B Nov  1 16:26 THNN
drwxr-xr-x   14 daniel  staff   448B Dec 30 15:48 c10
drwxr-xr-x    5 daniel  staff   160B Dec 30 15:48 caffe2
drwxr-xr-x   21 daniel  staff   672B Dec 30 15:48 pybind11
drwxr-xr-x    6 daniel  staff   192B Dec 30 15:48 torch

So there are TH and THNN folders, but no THC. Can this something specific to my operating system?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.