Skip to content

Commit

Permalink
Add usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsmeding committed Sep 7, 2023
1 parent 450e82a commit 5c96d00
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ A shorthand for the `env LD_LIBRARY_PATH="..."` prefix is [`./in-env.sh`](in-env
The version of TensorFlow being used is that in the submodule contained within the `tensorflow-haskell` submodule.
This is TensorFlow version 2.10.1.
Both repositories have some patches applied at the time of writing, and are hence forks of upstream.


## Usage example

For an example of how to use both backends defined in this repository, see [accelerate-tensorflow-lite/test/examples/Main.hs](accelerate-tensorflow-lite/test/examples/Main.hs).
15 changes: 15 additions & 0 deletions accelerate-tensorflow-lite/accelerate-tensorflow-lite.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ test-suite nofib-tensorflow-lite
, tasty-hedgehog
, tasty-hunit
default-language: Haskell2010

test-suite acctflite-examples
type: exitcode-stdio-1.0
main-is: Main.hs
other-modules:
hs-source-dirs:
test/examples
ghc-options: -Wall -O2 -Werror=tabs
build-depends:
accelerate ==1.3.*
, accelerate-tensorflow
, accelerate-tensorflow-lite
, base >=4.7 && <5
, random
default-language: Haskell2010
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ However, simply using 'compile' and 'execute' will yield very unsatisfactory per
to do anything else; 'execute' will automatically pick up the existing held
device context if there is one.
=== Usage example
For a "Hello World"-style example of how to use this backend, see
<https://github.com/AccelerateHS/accelerate-tensorflow/blob/master/accelerate-tensorflow-lite/test/examples/Main.hs>.
-}

module Data.Array.Accelerate.TensorFlow.Lite (
Expand Down
62 changes: 62 additions & 0 deletions accelerate-tensorflow-lite/test/examples/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module Main where

import Control.Monad (replicateM)
import System.Random (randomRIO)

import qualified Data.Array.Accelerate as A
import qualified Data.Array.Accelerate.Interpreter as I
import qualified Data.Array.Accelerate.TensorFlow as TFCPU
import qualified Data.Array.Accelerate.TensorFlow.Lite as TPU


-- matrix is a vector of rows
matmat :: A.Acc (A.Matrix Float) -> A.Acc (A.Matrix Float) -> A.Acc (A.Matrix Float)
matmat a b =
let A.I2 k m = A.shape a
A.I2 _ n = A.shape b
in A.sum $
A.generate (A.I3 k n m) $ \(A.I3 i j p) ->
a A.! A.I2 i p * b A.! A.I2 p j

genmatrix :: A.DIM2 -> IO (A.Matrix Float)
genmatrix dim@(A.Z A.:. n A.:. m) = A.fromList dim <$> replicateM (n * m) (randomRIO (0, 10))

main :: IO ()
main = do
-- Inputs
let dimA = A.Z A.:. 3 A.:. 2
dimB = A.Z A.:. 2 A.:. 4
dimC = A.Z A.:. 3 A.:. 4 -- result dimension
let a1 = A.fromList dimA
[1, 2
,3, 4
,5, 6]
b1 = A.fromList dimB
[1, 0, 1, 0
,0, 1, 0, 1]

-- representative sample input
samples <- replicateM 10 $ do
a <- genmatrix dimA
b <- genmatrix dimB
return (a TPU.:-> b TPU.:-> TPU.Result dimC)

-- First let's try it in the accelerate interpreter
putStrLn "## Running in the interpreter"
print $ I.runN matmat a1 b1

-- First let's run it on the CPU using TensorFlow
putStrLn "## Running on TensorFlow native CPU"
print $ TFCPU.runN matmat a1 b1

-- Then let's run it on a TPU, the easy way
putStrLn "## Running on TPU, easy"
do let model = TPU.compile matmat samples
print $ TPU.execute model a1 b1

-- And then the hard way, which scales better to multiple model executions
putStrLn "## Running on TPU, better"
do TPU.withConverterPy $ \converter -> do
TPU.withDeviceContext $ do
let model = TPU.compileWith converter matmat samples
print $ TPU.execute model a1 b1
2 changes: 2 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cradle:
component: "accelerate-tensorflow-lite:lib:accelerate-tensorflow-lite"
- path: "./accelerate-tensorflow-lite/test/nofib"
component: "accelerate-tensorflow-lite:test:nofib-tensorflow-lite"
- path: "./accelerate-tensorflow-lite/test/examples"
component: "accelerate-tensorflow-lite:test:acctflite-examples"
- path: "./extra-deps/accelerate/src"
component: "accelerate:lib:accelerate"
- path: "./extra-deps/accelerate/test/doctest"
Expand Down

0 comments on commit 5c96d00

Please sign in to comment.