-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor backend for type-safety and clarity (#274)
* add type-safe layer round FFI * separate XLA Idris API from high-level usage of this API * simplify functionality for reading and writing `Literal`s
- Loading branch information
1 parent
8276a38
commit c604608
Showing
37 changed files
with
1,288 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
{-- | ||
Copyright 2022 Joel Berkeley | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--} | ||
module Compiler.Computation | ||
|
||
import Control.Monad.State | ||
import Data.SortedMap | ||
|
||
import Data.Hashable | ||
|
||
import Compiler.Graph | ||
import Compiler.Xla.TensorFlow.Compiler.Xla.Client.XlaBuilder | ||
import Compiler.Xla.TensorFlow.Compiler.Xla.Client.XlaComputation | ||
import Compiler.Xla.TensorFlow.Compiler.Xla.Shape | ||
import Compiler.Xla.TensorFlow.Compiler.Xla.ShapeUtil | ||
import Compiler.Xla.TensorFlow.Compiler.Xla.XlaData | ||
import Types | ||
|
||
public export | ||
data CachingBuilder : Type where | ||
MkCachingBuilder : XlaBuilder -> SortedMap Bits64 XlaOp -> CachingBuilder | ||
|
||
public export | ||
Computation : Type -> Type | ||
Computation = StateT CachingBuilder IO | ||
|
||
export | ||
cached : Graph -> Computation XlaOp -> Computation XlaOp | ||
cached graph xs = assert_total $ let graphHash = hash graph in do | ||
builder <- get | ||
case cacheLookup builder graphHash of | ||
Just op => pure op | ||
Nothing => do | ||
op <- xs | ||
builder <- get | ||
put (cacheInsert builder graphHash op) | ||
pure op | ||
|
||
where | ||
cacheInsert : CachingBuilder -> Bits64 -> XlaOp -> CachingBuilder | ||
cacheInsert (MkCachingBuilder builder cache) key xlaOp = | ||
MkCachingBuilder builder (insert key xlaOp cache) | ||
|
||
cacheLookup : CachingBuilder -> Bits64 -> Maybe XlaOp | ||
cacheLookup (MkCachingBuilder _ cache) key = lookup key cache | ||
|
||
export | ||
build : HasIO io => String -> Computation XlaOp -> io XlaComputation | ||
build computationName x = do | ||
builder <- mkXlaBuilder computationName | ||
MkCachingBuilder builder _ <- liftIO $ execStateT (MkCachingBuilder builder empty) x | ||
build builder | ||
|
||
export | ||
buildWithSubBuilder : | ||
String -> List (Computation XlaOp) -> Computation XlaOp -> Computation XlaComputation | ||
buildWithSubBuilder computationName computationArguments computationResult = do | ||
MkCachingBuilder builder _ <- get | ||
subBuilder <- createSubBuilder builder computationName | ||
let cachingSubBuilder = MkCachingBuilder subBuilder empty | ||
allOps = sequence_ (computationArguments ++ [computationResult]) | ||
MkCachingBuilder subBuilder _ <- liftIO $ execStateT cachingSubBuilder allOps | ||
build subBuilder | ||
|
||
export | ||
opToString : Computation XlaOp -> String | ||
opToString x = unsafePerformIO $ do | ||
builder <- mkXlaBuilder "toString" | ||
(MkCachingBuilder builder _, xlaOp) <- runStateT (MkCachingBuilder builder empty) x | ||
pure $ opToString builder xlaOp | ||
|
||
export | ||
parameter : Primitive dtype => Nat -> Types.Shape -> String -> (Graph, Computation XlaOp) | ||
parameter position shape name = | ||
let graph = Parameter {dtype} shape position | ||
|
||
param : Computation XlaOp | ||
param = do | ||
MkCachingBuilder builder _ <- get | ||
xlaShape <- mkShape {dtype} shape | ||
cached graph $ parameter builder position xlaShape name | ||
|
||
in (graph, param) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{-- | ||
Copyright 2022 Joel Berkeley | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--} | ||
module Compiler.LiteralRW | ||
|
||
import Compiler.Xla.TensorFlow.Compiler.Xla.XlaData | ||
import Compiler.Xla.TensorFlow.Compiler.Xla.Literal | ||
import Literal | ||
import Util | ||
|
||
range : (n : Nat) -> Literal [n] Nat | ||
range n = impl n [] | ||
where | ||
impl : (p : Nat) -> Literal [q] Nat -> Literal [q + p] Nat | ||
impl Z xs = rewrite plusZeroRightNeutral q in xs | ||
impl (S p) xs = rewrite sym $ plusSuccRightSucc q p in impl p (Scalar p :: xs) | ||
|
||
indexed : {shape : _} -> Literal shape (List Nat) | ||
indexed = go shape [] | ||
where | ||
concat : Literal [d] (Literal ds a) -> Literal (d :: ds) a | ||
concat [] = [] | ||
concat (Scalar x :: xs) = x :: concat xs | ||
|
||
go : (shape : Types.Shape) -> List Nat -> Literal shape (List Nat) | ||
go [] idxs = Scalar idxs | ||
go (0 :: _) _ = [] | ||
go (S d :: ds) idxs = concat $ map (\i => go ds (snoc idxs i)) (range (S d)) | ||
|
||
export | ||
interface Primitive dtype => LiteralRW dtype ty where | ||
set : Literal -> List Nat -> ty -> IO () | ||
get : Literal -> List Nat -> ty | ||
|
||
export | ||
write : (HasIO io, LiteralRW dtype a) => {shape : _} -> Literal shape a -> io Literal | ||
write xs = liftIO $ do | ||
literal <- allocLiteral {dtype} shape | ||
sequence_ [| (\idxs => set {dtype} literal idxs) indexed xs |] | ||
pure literal | ||
|
||
export | ||
read : LiteralRW dtype a => Literal -> {shape : _} -> Literal shape a | ||
read lit = map (get {dtype} lit) indexed | ||
|
||
export | ||
LiteralRW PRED Bool where | ||
set = set | ||
get = get | ||
|
||
export | ||
LiteralRW F64 Double where | ||
set = set | ||
get = get | ||
|
||
export | ||
LiteralRW S32 Int where | ||
set = set | ||
get = get | ||
|
||
export | ||
LiteralRW U32 Nat where | ||
set lit idx x = Int.set lit idx (cast x) | ||
get = cast .: Int.get |
44 changes: 0 additions & 44 deletions
44
src/Compiler/TensorFlow/Compiler/XLA/Client/LocalClient.idr
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.