Skip to content

Commit

Permalink
Add inferno-ml-server-types package (#102)
Browse files Browse the repository at this point in the history
Adds the `inferno-ml-server-types` package containing the API for a
server that can evaluate Inferno scripts using ML primitives. There is
no actual implementation of `inferno-ml-server` here

This and the whole Inferno ML infrastructure is still a WIP. However, a
WIP implementation will need to be merged here in order to merge some
WIPs in other repos. The following at least are not yet complete:
- making a polymorphic prelude for `inferno-ml` that doesn't depend on
`hasktorch` (helpful for typechecking in cases where we can't use
`hasktorch`)
- golden and other tests for the new types; however, since many of the
types are polymorphic on something, it probably makes more sense to just
add them in one of the places where `inferno-ml-server` or the bridge
server are implemented
- a metrics endpoint for `inferno-ml-server`

I've also made a few changes to `inferno-ml`, which are listed in the
changelog

---------

Co-authored-by: Siddharth Krishna <[email protected]>
  • Loading branch information
ngua and siddharth-krishna authored Feb 26, 2024
1 parent f229ad9 commit 9be95fa
Show file tree
Hide file tree
Showing 15 changed files with 1,022 additions and 35 deletions.
5 changes: 5 additions & 0 deletions inferno-ml-server-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision History for inferno-ml-server-types
*Note*: we use https://pvp.haskell.org/ (MAJOR.MAJOR.MINOR.PATCH)

## 0.0.1
* Initial pre-release
2 changes: 2 additions & 0 deletions inferno-ml-server-types/hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cradle:
cabal:
66 changes: 66 additions & 0 deletions inferno-ml-server-types/inferno-ml-server-types.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
cabal-version: 2.4
name: inferno-ml-server-types
version: 0.0.1
synopsis: Types for Inferno ML server
description: Types for Inferno ML server
homepage: https://github.com/plow-technologies/inferno.git#readme
bug-reports: https://github.com/plow-technologies/inferno.git/issues
copyright: Plow-Technologies LLC
license: MIT
author: Rory Tyler hayford
maintainer: [email protected]
build-type: Simple

source-repository head
type: git
location: https://github.com/plow-technologies/inferno.git

common common
ghc-options:
-Wall -Werror -Wincomplete-uni-patterns -Wincomplete-record-updates
-Wmissing-deriving-strategies

default-language: Haskell2010
default-extensions:
DerivingStrategies
LambdaCase
OverloadedStrings
TupleSections
TypeApplications

build-depends: base >=4.7 && <5

library
import: common
exposed-modules:
Inferno.ML.Server.Client
Inferno.ML.Server.Client.Bridge
Inferno.ML.Server.Types

hs-source-dirs: src
build-depends:
, aeson
, attoparsec
, base
, bytestring
, conduit
, containers
, deepseq
, extra
, generic-lens
, http-api-data
, inferno-core
, inferno-types
, inferno-vc
, iproute
, microlens-platform
, postgresql-simple
, scientific
, servant-client
, servant-conduit
, servant-server
, text
, unix
, uri-bytestring
, uri-bytestring-aeson
, vector
43 changes: 43 additions & 0 deletions inferno-ml-server-types/src/Inferno/ML/Server/Client.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Inferno.ML.Server.Client
( statusC,
inferenceC,
cancelC,
registerBridgeC,
checkBridgeC,
)
where

import Data.Int (Int64)
import Data.Proxy (Proxy (Proxy))
import Inferno.ML.Server.Types
import Servant ((:<|>) ((:<|>)))
import Servant.Client.Streaming (ClientM, client)

-- | Get the status of the server. @Nothing@ indicates that an inference job
-- is being evaluated. @Just ()@ means the server is idle
statusC :: ClientM (Maybe ())

-- | Run an inference parameter
inferenceC :: Id (InferenceParam uid gid p s) -> Maybe Int64 -> ClientM ()

-- | Cancel the existing inference job, if it exists
cancelC :: ClientM ()

-- | Register the information required to communicate with the bridge server
registerBridgeC :: BridgeInfo -> ClientM ()

-- | Check if any bridge information has been previously registered with this
-- server instance
checkBridgeC :: ClientM (Maybe BridgeInfo)
statusC
:<|> inferenceC
:<|> cancelC
:<|> registerBridgeC
:<|> checkBridgeC =
client api

api :: Proxy (InfernoMlServerAPI uid gid p s)
api = Proxy
52 changes: 52 additions & 0 deletions inferno-ml-server-types/src/Inferno/ML/Server/Client/Bridge.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoMonomorphismRestriction #-}

module Inferno.ML.Server.Client.Bridge where

import Data.Aeson (ToJSON)
import Data.Data (Proxy (Proxy))
import Data.Int (Int64)
import Inferno.ML.Server.Types
import Servant ((:<|>) (..))
import Servant.Client.Streaming (ClientM, client)
import Web.HttpApiData (ToHttpApiData)

-- | Write a stream of @(t, Double)@ pairs to the bridge server, where @t@ will
-- typically represent some time value (e.g. @EpochTime@)
writePairsC ::
( ToJSON t,
ToHttpApiData p,
ToHttpApiData t
) =>
p ->
PairStream t IO ->
ClientM ()

-- | Get the value at the given time via the bridge, for the given entity @p@
valueAtC ::
( ToJSON t,
ToHttpApiData p,
ToHttpApiData t
) =>
Int64 ->
p ->
t ->
ClientM IValue

-- | Get the latest value (and associated time) for entity @p@ before time @t@
latestValueAndTimeBeforeC ::
( ToJSON t,
ToHttpApiData p,
ToHttpApiData t
) =>
t ->
p ->
ClientM IValue
writePairsC
:<|> valueAtC
:<|> latestValueAndTimeBeforeC =
client api

api :: Proxy (BridgeAPI p t)
api = Proxy
Loading

0 comments on commit 9be95fa

Please sign in to comment.