├── README.md └── torch ├── x ├── y ├── README.md └── simple.bkp /README.md: -------------------------------------------------------------------------------- 1 | # backpack-examples 2 | Backpack examples repository 3 | -------------------------------------------------------------------------------- /torch/x: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 6 7 | 7 8 | 8 9 | 9 10 | 10 11 | -------------------------------------------------------------------------------- /torch/y: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 6 7 | 7 8 | 8 9 | 9 10 | 10 11 | -------------------------------------------------------------------------------- /torch/README.md: -------------------------------------------------------------------------------- 1 | # How to build this example 2 | 3 | This example uses Torch library (http://torch.ch). You can get a copy of Torch library from pytorch. 4 | 5 | ``` 6 | git clone https://github.com/pytorch/pytorch.git 7 | cd tools/torch/lib/TH 8 | mkdir build 9 | cd build 10 | cmake .. 11 | make 12 | make install 13 | ``` 14 | Once TH library is installed, run in this example's directory: 15 | ``` 16 | ghc -O -lTH -L/pytorch/torch/lib/TH/build --backpack simple.bkp 17 | ``` 18 | then 19 | ``` 20 | main/Main 21 | ``` 22 | 23 | `Tensor=int-tensor:Tensor` can be changed to `Tensor=float-tensor:Tensor`, then the result would become `440.0` instead of `440` 24 | -------------------------------------------------------------------------------- /torch/simple.bkp: -------------------------------------------------------------------------------- 1 | unit torch-indef where 2 | signature Tensor where 3 | import Data.Int 4 | data Tensor 5 | data AccReal 6 | instance Show AccReal 7 | instance Num AccReal 8 | read1dFile :: FilePath -> Int64 -> IO Tensor 9 | dot :: Tensor -> Tensor -> IO AccReal 10 | sumall :: Tensor -> IO AccReal 11 | module App where 12 | import Tensor 13 | app = do 14 | x <- read1dFile "x" 10 15 | y <- read1dFile "y" 10 16 | d <- dot x y 17 | s <- sumall x 18 | print (d + s) 19 | return () 20 | 21 | 22 | unit float-tensor where 23 | module Tensor where 24 | import Foreign 25 | import Foreign.C.Types 26 | import Foreign.C.String 27 | import Foreign.ForeignPtr 28 | 29 | type CTHFile = () 30 | 31 | foreign import ccall "THDiskFile.h THDiskFile_new" 32 | c_THDiskFile_new :: CString -> CString -> CInt -> IO (Ptr CTHFile) 33 | 34 | type CTHFloatTensor = () 35 | 36 | foreign import ccall "THTensor.h THFloatTensor_newWithSize1d" 37 | c_THFloatTensor_newWithSize1d :: CLong -> IO (Ptr CTHFloatTensor) 38 | 39 | type CTHFloatStorage = () 40 | 41 | foreign import ccall "THFile.h THFile_readFloat" 42 | c_THFile_readFloat :: (Ptr CTHFile) -> (Ptr CTHFloatStorage) -> IO CSize 43 | 44 | foreign import ccall "THTensorMath.h THFloatTensor_dot" 45 | c_THFloatTensor_dot :: (Ptr CTHFloatTensor) -> (Ptr CTHFloatTensor) -> IO CDouble 46 | 47 | foreign import ccall "THTensorMath.h THFloatTensor_sumall" 48 | c_THFloatTensor_sumall :: (Ptr CTHFloatTensor) -> IO CDouble 49 | 50 | foreign import ccall "THTensor.h THFloatTensor_free" 51 | c_THFloatTensor_free :: (Ptr CTHFloatTensor) -> IO () 52 | 53 | foreign import ccall "THTensor.h &THFloatTensor_free" 54 | p_THFloatTensor_free :: FunPtr ((Ptr CTHFloatTensor) -> IO ()) 55 | 56 | foreign import ccall "THFile.h THFile_free" 57 | c_THFile_free :: (Ptr CTHFile) -> IO CSize 58 | 59 | foreign import ccall "THTensor.h THFloatTensor_storage" 60 | c_THFloatTensor_storage :: (Ptr CTHFloatTensor) -> IO (Ptr CTHFloatStorage) 61 | 62 | data FloatTensor = FT (ForeignPtr CTHFloatTensor) 63 | 64 | type Tensor = FloatTensor 65 | type AccReal = Double 66 | 67 | 68 | read1dFile :: FilePath -> Int64 -> IO Tensor 69 | read1dFile f n = withCString f $ \x -> 70 | withCString "r" $ \r -> do 71 | xfile <- c_THDiskFile_new x r 0 72 | t <- c_THFloatTensor_newWithSize1d $ fromIntegral n 73 | ft <- newForeignPtr p_THFloatTensor_free t 74 | storaget <- withForeignPtr ft c_THFloatTensor_storage 75 | c_THFile_readFloat xfile storaget 76 | c_THFile_free xfile 77 | return (FT ft) 78 | 79 | dot :: Tensor -> Tensor -> IO AccReal 80 | dot (FT f) (FT g) = withForeignPtr f $ \x -> 81 | withForeignPtr g $ \y -> do 82 | d <- c_THFloatTensor_dot x y 83 | return (realToFrac d) 84 | 85 | sumall :: Tensor -> IO AccReal 86 | sumall (FT f) = withForeignPtr f $ \x -> do 87 | d <- c_THFloatTensor_sumall x 88 | return (realToFrac d) 89 | 90 | unit int-tensor where 91 | module Tensor where 92 | import Foreign 93 | import Foreign.C.Types 94 | import Foreign.C.String 95 | import Foreign.ForeignPtr 96 | 97 | type CTHFile = () 98 | 99 | foreign import ccall "THDiskFile.h THDiskFile_new" 100 | c_THDiskFile_new :: CString -> CString -> CInt -> IO (Ptr CTHFile) 101 | 102 | type CTHIntTensor = () 103 | 104 | foreign import ccall "THTensor.h THIntTensor_newWithSize1d" 105 | c_THIntTensor_newWithSize1d :: CLong -> IO (Ptr CTHIntTensor) 106 | 107 | type CTHIntStorage = () 108 | 109 | foreign import ccall "THFile.h THFile_readInt" 110 | c_THFile_readInt :: (Ptr CTHFile) -> (Ptr CTHIntStorage) -> IO CSize 111 | 112 | foreign import ccall "THTensorMath.h THIntTensor_dot" 113 | c_THIntTensor_dot :: (Ptr CTHIntTensor) -> (Ptr CTHIntTensor) -> IO CInt 114 | 115 | foreign import ccall "THTensorMath.h THIntTensor_sumall" 116 | c_THIntTensor_sumall :: (Ptr CTHIntTensor) -> IO CInt 117 | 118 | foreign import ccall "THTensor.h THIntTensor_free" 119 | c_THIntTensor_free :: (Ptr CTHIntTensor) -> IO () 120 | 121 | foreign import ccall "THTensor.h &THIntTensor_free" 122 | p_THIntTensor_free :: FunPtr ((Ptr CTHIntTensor) -> IO ()) 123 | 124 | foreign import ccall "THFile.h THFile_free" 125 | c_THFile_free :: (Ptr CTHFile) -> IO CSize 126 | 127 | foreign import ccall "THTensor.h THIntTensor_storage" 128 | c_THIntTensor_storage :: (Ptr CTHIntTensor) -> IO (Ptr CTHIntStorage) 129 | 130 | data IntTensor = FT (ForeignPtr CTHIntTensor) 131 | 132 | type Tensor = IntTensor 133 | type AccReal = Int 134 | 135 | read1dFile :: FilePath -> Int64 -> IO Tensor 136 | read1dFile f n = withCString f $ \x -> 137 | withCString "r" $ \r -> do 138 | xfile <- c_THDiskFile_new x r 0 139 | t <- c_THIntTensor_newWithSize1d $ fromIntegral n 140 | ft <- newForeignPtr p_THIntTensor_free t 141 | storaget <- withForeignPtr ft c_THIntTensor_storage 142 | c_THFile_readInt xfile storaget 143 | c_THFile_free xfile 144 | return (FT ft) 145 | 146 | dot :: Tensor -> Tensor -> IO AccReal 147 | dot (FT f) (FT g) = withForeignPtr f $ \x -> 148 | withForeignPtr g $ \y -> do 149 | d <- c_THIntTensor_dot x y 150 | return (fromIntegral d) 151 | 152 | sumall :: Tensor -> IO AccReal 153 | sumall (FT f) = withForeignPtr f $ \x -> do 154 | d <- c_THIntTensor_sumall x 155 | return (fromIntegral d) 156 | 157 | unit main where 158 | dependency torch-indef[Tensor=int-tensor:Tensor] 159 | module Main where 160 | import App 161 | main = app 162 | 163 | --------------------------------------------------------------------------------