SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Towards Hasktorch 1.0
Junji Hashimoto, Sam Stites, and Austin Huang
on behalf of the Hasktorch Contributor Team
1
● Junji Hashimoto
● Austin Huang
● Adam Pazske
● Sam Stites
Contributor Team
2
Google Summer of Code
● Atreya Ghosal
● Jesse Segal
Outline
● What is hasktorch?
● Codegen
○ Architecture. (Easy to maintain.)
○ How to connect C++ library.
○ Reference Counter and Garbage Collection
● Pure functions and ML models
○ High level API but pure.
○ Easy to write like numpy
○ ML model (Spec, data model and computational model)
● Examples
○ Gaussian Process -- Not using gradient
○ Gated Recurrent Unit -- Using gradient
● Results
● Next Steps
3
What is hasktorch?
● General machine learning framework.
● Type Safe
● Differentiable programming
● To put Pytorch-power into Haskell
● Easy to use (Based on pure functions)
4
= +
Hasktorch Haskell Pytorch
Codegen
● Pytorch has thousands of functions in its API.
● The APIs are changed everyday. (Stable release is every 3 months.)
● Base library is C++.
5
● What is generated?
● How to connect C++?
● How to manage memory?
● Is it efficient for calculation?
Generate haskell from Pytorch’s spec
Architecture
6
Raw layer without GC
Managed layer with GC
libtorch(C++)
libtorch(Spec) Codegen
High level API
with pure funcitons
High level API
with static types
under
development
Generate
Haskell
Code at::Tensor
About 1500 functions
Ptr Tensor
ForeignPtr Tensor
Tensor
Tensor Float ‘[3,3]
How to connect C++?
● Inline-c-cpp
○ Write haskell and generate codes at compilation-time
7
main = do
let x = 1
print [C.pure| double{ cos($(double x)) } |]
Template Haskell
foreign import ccall "cos" c_cos ::Double -> Double
main = do
let x = 1
print (c_cos x)
extern “C” {
double c_cos (double x) {
return cos(x);
}
}
Haskell
C++
How to connect objects of C++?
● Connect object’s pointer.
● Support namespace(at::Tensor) and template(std::vector<at::Tensor>)
8
tensor_cos :: Ptr Tensor -> Ptr Tensor
tensor_cos x =
[C.pure| at::Tensor* {
return new at::Tensor(cos(*$(at::Tensor* x)))
} |]
haskell
Ptr Tensor
c
c++
Ptr Tensorderef. newcos
How to manage memory?
● Use GHC’s garbage collection with ForeignPtr.
● Generic cast converts Ptr into ForeignPtr with GC.
9
tensor_cos :: Ptr Tensor -> IO (Ptr Tensor)
tensor_cos’ :: ForeignPtr Tensor -> IO (ForeignPtr Tensor)
tensor_cos’ = cast tensor_cos
Does GHC know Tensor’s memory-usage?
● Use memory-monitor and memory-exception for both CPU and GPU.
● Call performGC.
heap
stack
ghc runtime
libtorch
memory-monitor
memory-exception checker
CPU
memory
GPU
memory
libtorch
Call GC
How to connect objects of C++?
● Is it OK to call new at::Tensor?
● at::Tensor is just reference counter.
10
at::Tensor
(Reference Counter)
c10::TensorImpl
(Numerical Array)
ForeignPtr Tensor
at::Tensor
(Reference Counter)
ForeignPtr Tensor
The cost creating at::Tensor is low.
Outline
● What is hasktorch?
● Codegen
○ Architecture. (Easy to maintain.)
○ How to connect C++ library.
○ Reference Counter and Garbage Collection
● Pure functions and ML models
○ High level API but pure.
○ Easy to write like numpy
○ ML model (Spec, data model and computational model)
● Examples
○ Gaussian Process -- Not using gradient
○ Gated Recurrent Unit -- Using gradient
● Results
● Next Steps
11
High-level API: Pure functions and ML model
(Adam Pazske)
● High-Level API (not typed dimensions) == Pure functions
12
LinearSpec
- input size : 10
- output size : 1
Linear
- weight : Tensor [10,1]
- bias: Tensor [1]
linear :: Linear -> Tensor -> Tensor
linear Linear{..} input =
( input `matmul` weight )+ bias
Function
(e.g. sin)
Tensor Tensor
● Design pattern of ML model
○ Spec, Data Model, Computational Model
Pure functions
● Almost high level API are pure functions.
● Autograd is a pure function.
13
newtype IndependentTensor = IndependentTensor Tensor
grad :: Tensor -> [IndependentTensor] -> [Tensor]
grad y inputs = unsafePerformIO …
main = do
xi <- makeIndependent $ ones' [2,2]
yi <- makeIndependent $ ones' [2,2]
let x = toDependent xi
y = toDependent yi
z = x * x * y
print (grad z [xi])
Function
(e.g. sin)
Tensor Tensor
ones’ [2,2]
xi yi
z
Allocate new memory area
(call makeIndependent)
grad z [xi]
Pure functions
● List <-> 1 d Tensor
● List of List <-> 2d Tensor
14
> t = asTensor ([0..3]::[Float])
> t2 = asTensor ([0..3]::[Float])
> t + t2
Tensor Float [4] [ 0.0000, 2.0000 , 4.0000 , 6.0000 ]
> asValue (t + t2) :: [Float]
[0.0,2.0,4.0,6.0]
> t = asTensor ([[0,1],[2,3]]::[[Float]])
> t2 = asTensor ([[3,2],[2,1]]::[[Float]])
> matmul t t2
Tensor Float [2,2] [[ 2.0000 , 1.0000 ],
[ 12.0000 , 7.0000 ]]
Implementation Pattern for Machine Learning Models
15
● Model consists of spec, data-model and computational model.
● Spec initializes data-model including dimensions and some functions.
○ This may be for tensor not using typed dimensions.
● Data-model is parameters for Machine Learning.
● Computational model calculates Tensor-values.
Spec Data Model Computational Model
Spec Data Model
Compute
Model
Autograd
Init
Replace
parameters
LinearSpec
- input size : 10
- output size : 1
Linear
- weight : Tensor [10,1]
- bias: Tensor [1]
linear :: Linear -> Tensor -> Tensor
linear Linear{..} input =
( input `matmul` weight )+ bias
Spec and data-model
16
Serialize parameters of data-model
17
Computational Models as Pure Functions
18
Training Loop
19
Spec Data Model
(state)
Compute
Model
(model)
Autograd
(grad)
Init
Flatten
parameters
Replace
parameters
Outline
● What is hasktorch?
● Codegen
○ Architecture. (Easy to maintain.)
○ How to connect C++ library.
○ Reference Counter and Garbage Collection
● Pure functions and ML models
○ High level API but pure.
○ Easy to write like numpy
○ ML model (Spec, data model and computational model)
● Examples
○ Gaussian Process -- Not using gradient
○ Gated Recurrent Unit -- Using gradient
● Results
● Next Steps
20
Gaussian Process
21
From “A Visual Exploration of Gaussian Processes”
Jochen Görtler, Rebecca Kehlbeck, Oliver Deussen
https://distill.pub/2019/visual-exploration-gaussian-processes/
Gaussian Process:Covariance Matrix
22
Gaussian Processs: Gen posterior distribution
23
Gaussian Processs: Gen posterior distribution
24
Gated Recurrent Unit: Define single cell
(Atreya Ghosal)
25
Gated Recurrent Unit: Fold cells
26
Results
● Hasktorch = Pytorch + Haskell
● Codegen generates thousands of APIs.
● Garbage Collection manages memory of both CPU and GPU.
● High level API == Pure functions and ML model
● The examples of practical ML-models are available.
○ gaussian process.
○ recurrent networks.(GRU, LSTM and Elman)
27
Next Steps
● Refine High-level Interfaces, including typed dimensions
○ Broadcast operators, High level LSTM with napian functor(Jesse Segal)
● More examples for test case (e.g. MNIST)
● Optimizers except for sgd
● Second major release around Q4 2019!
Contributors Welcome!
https://github.com/hasktorch/hasktorch/
28
29

Contenu connexe

Tendances

Python memory managment. Deeping in Garbage collector
Python memory managment. Deeping in Garbage collectorPython memory managment. Deeping in Garbage collector
Python memory managment. Deeping in Garbage collectorJose Manuel Ortega Candel
 
Measuring SGX Texturing Performance
Measuring SGX Texturing PerformanceMeasuring SGX Texturing Performance
Measuring SGX Texturing PerformancePrabindh Sundareson
 
Meetup C++ A brief overview of c++17
Meetup C++  A brief overview of c++17Meetup C++  A brief overview of c++17
Meetup C++ A brief overview of c++17Daniel Eriksson
 
F# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, HerefordF# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, HerefordKit Eason
 
Core Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysCore Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysWebStackAcademy
 
Java & OOP Core Concept
Java & OOP Core ConceptJava & OOP Core Concept
Java & OOP Core ConceptPin-Lun Huang
 
Using VI Java from Scala
Using VI Java from ScalaUsing VI Java from Scala
Using VI Java from Scaladcbriccetti
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksStoyan Nikolov
 
Functional Programming, simplified
Functional Programming, simplifiedFunctional Programming, simplified
Functional Programming, simplifiedNaveenkumar Muguda
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template LibraryAnirudh Raja
 
Coding convention
Coding conventionCoding convention
Coding conventionKhoa Nguyen
 
Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy AminaRepo
 
Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)Dina Goldshtein
 

Tendances (20)

Python memory managment. Deeping in Garbage collector
Python memory managment. Deeping in Garbage collectorPython memory managment. Deeping in Garbage collector
Python memory managment. Deeping in Garbage collector
 
Measuring SGX Texturing Performance
Measuring SGX Texturing PerformanceMeasuring SGX Texturing Performance
Measuring SGX Texturing Performance
 
Meetup C++ A brief overview of c++17
Meetup C++  A brief overview of c++17Meetup C++  A brief overview of c++17
Meetup C++ A brief overview of c++17
 
Templates
TemplatesTemplates
Templates
 
F# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, HerefordF# Presentation for SmartDevs, Hereford
F# Presentation for SmartDevs, Hereford
 
Core Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysCore Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - Arrays
 
Reactive x
Reactive xReactive x
Reactive x
 
Java & OOP Core Concept
Java & OOP Core ConceptJava & OOP Core Concept
Java & OOP Core Concept
 
Using VI Java from Scala
Using VI Java from ScalaUsing VI Java from Scala
Using VI Java from Scala
 
Queue
QueueQueue
Queue
 
Robust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time ChecksRobust C++ Task Systems Through Compile-time Checks
Robust C++ Task Systems Through Compile-time Checks
 
Functional Programming, simplified
Functional Programming, simplifiedFunctional Programming, simplified
Functional Programming, simplified
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
 
Coding convention
Coding conventionCoding convention
Coding convention
 
Python Memory Management 101(Europython)
Python Memory Management 101(Europython)Python Memory Management 101(Europython)
Python Memory Management 101(Europython)
 
Lec1
Lec1Lec1
Lec1
 
Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy
 
Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)
 
Queue oop
Queue   oopQueue   oop
Queue oop
 
Lab 1
Lab 1Lab 1
Lab 1
 

Similaire à Towards hasktorch 1.0

Java 8 - functional features
Java 8 - functional featuresJava 8 - functional features
Java 8 - functional featuresRafal Rybacki
 
Functional programming with streams
Functional programming with streamsFunctional programming with streams
Functional programming with streamsRiadh MNASRI
 
Parallel program design
Parallel program designParallel program design
Parallel program designZongYing Lyu
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersJayaram Sankaranarayanan
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaPriyanka Rana
 
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...Qbeast
 
Towards typesafe deep learning in scala
Towards typesafe deep learning in scalaTowards typesafe deep learning in scala
Towards typesafe deep learning in scalaTongfei Chen
 
Programming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxProgramming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxNoorAntakia
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues listsJames Wong
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
StacksqueueslistsFraboni Ec
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsTony Nguyen
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsHarry Potter
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsYoung Alista
 
Introduction to nand2 tetris
Introduction to nand2 tetrisIntroduction to nand2 tetris
Introduction to nand2 tetrisYodalee
 
Introduction to Chainer
Introduction to ChainerIntroduction to Chainer
Introduction to ChainerSeiya Tokui
 

Similaire à Towards hasktorch 1.0 (20)

Java 8 - functional features
Java 8 - functional featuresJava 8 - functional features
Java 8 - functional features
 
Auto Tuning
Auto TuningAuto Tuning
Auto Tuning
 
Java 8
Java 8Java 8
Java 8
 
2013 05 ny
2013 05 ny2013 05 ny
2013 05 ny
 
Async fun
Async funAsync fun
Async fun
 
Functional programming with streams
Functional programming with streamsFunctional programming with streams
Functional programming with streams
 
Parallel program design
Parallel program designParallel program design
Parallel program design
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 Developers
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
Extending Spark for Qbeast's SQL Data Source​ with Paola Pardo and Cesare Cug...
 
Towards typesafe deep learning in scala
Towards typesafe deep learning in scalaTowards typesafe deep learning in scala
Towards typesafe deep learning in scala
 
Programming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxProgramming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptx
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Introduction to nand2 tetris
Introduction to nand2 tetrisIntroduction to nand2 tetris
Introduction to nand2 tetris
 
Introduction to Chainer
Introduction to ChainerIntroduction to Chainer
Introduction to Chainer
 

Dernier

RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATIONRACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATIONRachelAnnTenibroAmaz
 
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.KathleenAnnCordero2
 
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.comSaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.comsaastr
 
Call Girls In Aerocity 🤳 Call Us +919599264170
Call Girls In Aerocity 🤳 Call Us +919599264170Call Girls In Aerocity 🤳 Call Us +919599264170
Call Girls In Aerocity 🤳 Call Us +919599264170Escort Service
 
CHROMATOGRAPHY and its types with procedure,diagrams,flow charts,advantages a...
CHROMATOGRAPHY and its types with procedure,diagrams,flow charts,advantages a...CHROMATOGRAPHY and its types with procedure,diagrams,flow charts,advantages a...
CHROMATOGRAPHY and its types with procedure,diagrams,flow charts,advantages a...university
 
proposal kumeneger edited.docx A kumeeger
proposal kumeneger edited.docx A kumeegerproposal kumeneger edited.docx A kumeeger
proposal kumeneger edited.docx A kumeegerkumenegertelayegrama
 
Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸mathanramanathan2005
 
Engaging Eid Ul Fitr Presentation for Kindergartners.pptx
Engaging Eid Ul Fitr Presentation for Kindergartners.pptxEngaging Eid Ul Fitr Presentation for Kindergartners.pptx
Engaging Eid Ul Fitr Presentation for Kindergartners.pptxAsifArshad8
 
Event 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxEvent 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxaryanv1753
 
Application of GIS in Landslide Disaster Response.pptx
Application of GIS in Landslide Disaster Response.pptxApplication of GIS in Landslide Disaster Response.pptx
Application of GIS in Landslide Disaster Response.pptxRoquia Salam
 
INDIAN GCP GUIDELINE. for Regulatory affair 1st sem CRR
INDIAN GCP GUIDELINE. for Regulatory  affair 1st sem CRRINDIAN GCP GUIDELINE. for Regulatory  affair 1st sem CRR
INDIAN GCP GUIDELINE. for Regulatory affair 1st sem CRRsarwankumar4524
 
The Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationThe Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationNathan Young
 
Quality by design.. ppt for RA (1ST SEM
Quality by design.. ppt for  RA (1ST SEMQuality by design.. ppt for  RA (1ST SEM
Quality by design.. ppt for RA (1ST SEMCharmi13
 
Chizaram's Women Tech Makers Deck. .pptx
Chizaram's Women Tech Makers Deck.  .pptxChizaram's Women Tech Makers Deck.  .pptx
Chizaram's Women Tech Makers Deck. .pptxogubuikealex
 
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...漢銘 謝
 
Internship Presentation | PPT | CSE | SE
Internship Presentation | PPT | CSE | SEInternship Presentation | PPT | CSE | SE
Internship Presentation | PPT | CSE | SESaleh Ibne Omar
 
Early Modern Spain. All about this period
Early Modern Spain. All about this periodEarly Modern Spain. All about this period
Early Modern Spain. All about this periodSaraIsabelJimenez
 
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...Henrik Hanke
 
Dutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
Dutch Power - 26 maart 2024 - Henk Kras - Circular PlasticsDutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
Dutch Power - 26 maart 2024 - Henk Kras - Circular PlasticsDutch Power
 

Dernier (19)

RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATIONRACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
RACHEL-ANN M. TENIBRO PRODUCT RESEARCH PRESENTATION
 
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
PAG-UNLAD NG EKONOMIYA na dapat isaalang alang sa pag-aaral.
 
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.comSaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
SaaStr Workshop Wednesday w/ Kyle Norton, Owner.com
 
Call Girls In Aerocity 🤳 Call Us +919599264170
Call Girls In Aerocity 🤳 Call Us +919599264170Call Girls In Aerocity 🤳 Call Us +919599264170
Call Girls In Aerocity 🤳 Call Us +919599264170
 
CHROMATOGRAPHY and its types with procedure,diagrams,flow charts,advantages a...
CHROMATOGRAPHY and its types with procedure,diagrams,flow charts,advantages a...CHROMATOGRAPHY and its types with procedure,diagrams,flow charts,advantages a...
CHROMATOGRAPHY and its types with procedure,diagrams,flow charts,advantages a...
 
proposal kumeneger edited.docx A kumeeger
proposal kumeneger edited.docx A kumeegerproposal kumeneger edited.docx A kumeeger
proposal kumeneger edited.docx A kumeeger
 
Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸Mathan flower ppt.pptx slide orchids ✨🌸
Mathan flower ppt.pptx slide orchids ✨🌸
 
Engaging Eid Ul Fitr Presentation for Kindergartners.pptx
Engaging Eid Ul Fitr Presentation for Kindergartners.pptxEngaging Eid Ul Fitr Presentation for Kindergartners.pptx
Engaging Eid Ul Fitr Presentation for Kindergartners.pptx
 
Event 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptxEvent 4 Introduction to Open Source.pptx
Event 4 Introduction to Open Source.pptx
 
Application of GIS in Landslide Disaster Response.pptx
Application of GIS in Landslide Disaster Response.pptxApplication of GIS in Landslide Disaster Response.pptx
Application of GIS in Landslide Disaster Response.pptx
 
INDIAN GCP GUIDELINE. for Regulatory affair 1st sem CRR
INDIAN GCP GUIDELINE. for Regulatory  affair 1st sem CRRINDIAN GCP GUIDELINE. for Regulatory  affair 1st sem CRR
INDIAN GCP GUIDELINE. for Regulatory affair 1st sem CRR
 
The Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism PresentationThe Ten Facts About People With Autism Presentation
The Ten Facts About People With Autism Presentation
 
Quality by design.. ppt for RA (1ST SEM
Quality by design.. ppt for  RA (1ST SEMQuality by design.. ppt for  RA (1ST SEM
Quality by design.. ppt for RA (1ST SEM
 
Chizaram's Women Tech Makers Deck. .pptx
Chizaram's Women Tech Makers Deck.  .pptxChizaram's Women Tech Makers Deck.  .pptx
Chizaram's Women Tech Makers Deck. .pptx
 
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
THE COUNTRY WHO SOLVED THE WORLD_HOW CHINA LAUNCHED THE CIVILIZATION REVOLUTI...
 
Internship Presentation | PPT | CSE | SE
Internship Presentation | PPT | CSE | SEInternship Presentation | PPT | CSE | SE
Internship Presentation | PPT | CSE | SE
 
Early Modern Spain. All about this period
Early Modern Spain. All about this periodEarly Modern Spain. All about this period
Early Modern Spain. All about this period
 
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
DGT @ CTAC 2024 Valencia: Most crucial invest to digitalisation_Sven Zoelle_v...
 
Dutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
Dutch Power - 26 maart 2024 - Henk Kras - Circular PlasticsDutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
Dutch Power - 26 maart 2024 - Henk Kras - Circular Plastics
 

Towards hasktorch 1.0

  • 1. Towards Hasktorch 1.0 Junji Hashimoto, Sam Stites, and Austin Huang on behalf of the Hasktorch Contributor Team 1
  • 2. ● Junji Hashimoto ● Austin Huang ● Adam Pazske ● Sam Stites Contributor Team 2 Google Summer of Code ● Atreya Ghosal ● Jesse Segal
  • 3. Outline ● What is hasktorch? ● Codegen ○ Architecture. (Easy to maintain.) ○ How to connect C++ library. ○ Reference Counter and Garbage Collection ● Pure functions and ML models ○ High level API but pure. ○ Easy to write like numpy ○ ML model (Spec, data model and computational model) ● Examples ○ Gaussian Process -- Not using gradient ○ Gated Recurrent Unit -- Using gradient ● Results ● Next Steps 3
  • 4. What is hasktorch? ● General machine learning framework. ● Type Safe ● Differentiable programming ● To put Pytorch-power into Haskell ● Easy to use (Based on pure functions) 4 = + Hasktorch Haskell Pytorch
  • 5. Codegen ● Pytorch has thousands of functions in its API. ● The APIs are changed everyday. (Stable release is every 3 months.) ● Base library is C++. 5 ● What is generated? ● How to connect C++? ● How to manage memory? ● Is it efficient for calculation? Generate haskell from Pytorch’s spec
  • 6. Architecture 6 Raw layer without GC Managed layer with GC libtorch(C++) libtorch(Spec) Codegen High level API with pure funcitons High level API with static types under development Generate Haskell Code at::Tensor About 1500 functions Ptr Tensor ForeignPtr Tensor Tensor Tensor Float ‘[3,3]
  • 7. How to connect C++? ● Inline-c-cpp ○ Write haskell and generate codes at compilation-time 7 main = do let x = 1 print [C.pure| double{ cos($(double x)) } |] Template Haskell foreign import ccall "cos" c_cos ::Double -> Double main = do let x = 1 print (c_cos x) extern “C” { double c_cos (double x) { return cos(x); } } Haskell C++
  • 8. How to connect objects of C++? ● Connect object’s pointer. ● Support namespace(at::Tensor) and template(std::vector<at::Tensor>) 8 tensor_cos :: Ptr Tensor -> Ptr Tensor tensor_cos x = [C.pure| at::Tensor* { return new at::Tensor(cos(*$(at::Tensor* x))) } |] haskell Ptr Tensor c c++ Ptr Tensorderef. newcos
  • 9. How to manage memory? ● Use GHC’s garbage collection with ForeignPtr. ● Generic cast converts Ptr into ForeignPtr with GC. 9 tensor_cos :: Ptr Tensor -> IO (Ptr Tensor) tensor_cos’ :: ForeignPtr Tensor -> IO (ForeignPtr Tensor) tensor_cos’ = cast tensor_cos Does GHC know Tensor’s memory-usage? ● Use memory-monitor and memory-exception for both CPU and GPU. ● Call performGC. heap stack ghc runtime libtorch memory-monitor memory-exception checker CPU memory GPU memory libtorch Call GC
  • 10. How to connect objects of C++? ● Is it OK to call new at::Tensor? ● at::Tensor is just reference counter. 10 at::Tensor (Reference Counter) c10::TensorImpl (Numerical Array) ForeignPtr Tensor at::Tensor (Reference Counter) ForeignPtr Tensor The cost creating at::Tensor is low.
  • 11. Outline ● What is hasktorch? ● Codegen ○ Architecture. (Easy to maintain.) ○ How to connect C++ library. ○ Reference Counter and Garbage Collection ● Pure functions and ML models ○ High level API but pure. ○ Easy to write like numpy ○ ML model (Spec, data model and computational model) ● Examples ○ Gaussian Process -- Not using gradient ○ Gated Recurrent Unit -- Using gradient ● Results ● Next Steps 11
  • 12. High-level API: Pure functions and ML model (Adam Pazske) ● High-Level API (not typed dimensions) == Pure functions 12 LinearSpec - input size : 10 - output size : 1 Linear - weight : Tensor [10,1] - bias: Tensor [1] linear :: Linear -> Tensor -> Tensor linear Linear{..} input = ( input `matmul` weight )+ bias Function (e.g. sin) Tensor Tensor ● Design pattern of ML model ○ Spec, Data Model, Computational Model
  • 13. Pure functions ● Almost high level API are pure functions. ● Autograd is a pure function. 13 newtype IndependentTensor = IndependentTensor Tensor grad :: Tensor -> [IndependentTensor] -> [Tensor] grad y inputs = unsafePerformIO … main = do xi <- makeIndependent $ ones' [2,2] yi <- makeIndependent $ ones' [2,2] let x = toDependent xi y = toDependent yi z = x * x * y print (grad z [xi]) Function (e.g. sin) Tensor Tensor ones’ [2,2] xi yi z Allocate new memory area (call makeIndependent) grad z [xi]
  • 14. Pure functions ● List <-> 1 d Tensor ● List of List <-> 2d Tensor 14 > t = asTensor ([0..3]::[Float]) > t2 = asTensor ([0..3]::[Float]) > t + t2 Tensor Float [4] [ 0.0000, 2.0000 , 4.0000 , 6.0000 ] > asValue (t + t2) :: [Float] [0.0,2.0,4.0,6.0] > t = asTensor ([[0,1],[2,3]]::[[Float]]) > t2 = asTensor ([[3,2],[2,1]]::[[Float]]) > matmul t t2 Tensor Float [2,2] [[ 2.0000 , 1.0000 ], [ 12.0000 , 7.0000 ]]
  • 15. Implementation Pattern for Machine Learning Models 15 ● Model consists of spec, data-model and computational model. ● Spec initializes data-model including dimensions and some functions. ○ This may be for tensor not using typed dimensions. ● Data-model is parameters for Machine Learning. ● Computational model calculates Tensor-values. Spec Data Model Computational Model Spec Data Model Compute Model Autograd Init Replace parameters LinearSpec - input size : 10 - output size : 1 Linear - weight : Tensor [10,1] - bias: Tensor [1] linear :: Linear -> Tensor -> Tensor linear Linear{..} input = ( input `matmul` weight )+ bias
  • 17. Serialize parameters of data-model 17
  • 18. Computational Models as Pure Functions 18
  • 19. Training Loop 19 Spec Data Model (state) Compute Model (model) Autograd (grad) Init Flatten parameters Replace parameters
  • 20. Outline ● What is hasktorch? ● Codegen ○ Architecture. (Easy to maintain.) ○ How to connect C++ library. ○ Reference Counter and Garbage Collection ● Pure functions and ML models ○ High level API but pure. ○ Easy to write like numpy ○ ML model (Spec, data model and computational model) ● Examples ○ Gaussian Process -- Not using gradient ○ Gated Recurrent Unit -- Using gradient ● Results ● Next Steps 20
  • 21. Gaussian Process 21 From “A Visual Exploration of Gaussian Processes” Jochen Görtler, Rebecca Kehlbeck, Oliver Deussen https://distill.pub/2019/visual-exploration-gaussian-processes/
  • 23. Gaussian Processs: Gen posterior distribution 23
  • 24. Gaussian Processs: Gen posterior distribution 24
  • 25. Gated Recurrent Unit: Define single cell (Atreya Ghosal) 25
  • 26. Gated Recurrent Unit: Fold cells 26
  • 27. Results ● Hasktorch = Pytorch + Haskell ● Codegen generates thousands of APIs. ● Garbage Collection manages memory of both CPU and GPU. ● High level API == Pure functions and ML model ● The examples of practical ML-models are available. ○ gaussian process. ○ recurrent networks.(GRU, LSTM and Elman) 27
  • 28. Next Steps ● Refine High-level Interfaces, including typed dimensions ○ Broadcast operators, High level LSTM with napian functor(Jesse Segal) ● More examples for test case (e.g. MNIST) ● Optimizers except for sgd ● Second major release around Q4 2019! Contributors Welcome! https://github.com/hasktorch/hasktorch/ 28
  • 29. 29