Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[inferno-ml] Add resolution to inference params #123

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions inferno-ml-server-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Revision History for inferno-ml-server-types
*Note*: we use https://pvp.haskell.org/ (MAJOR.MAJOR.MINOR.PATCH)

## 0.5.0
* Add `resolution` to `InferenceParam`

## 0.4.0
* Change representation of script inputs/outputs

Expand Down
2 changes: 1 addition & 1 deletion inferno-ml-server-types/inferno-ml-server-types.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: inferno-ml-server-types
version: 0.4.0
version: 0.5.0
synopsis: Types for Inferno ML server
description: Types for Inferno ML server
homepage: https://github.com/plow-technologies/inferno.git#readme
Expand Down
4 changes: 3 additions & 1 deletion inferno-ml-server-types/src/Inferno/ML/Server/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ inferenceC ::
-- | SQL identifier of the inference parameter to be run
Id (InferenceParam uid gid p s) ->
-- | Optional resolution for scripts that use e.g. @valueAt@; defaults to
-- 128 if not specified
-- the param\'s stored resolution if not provided. This lets users override
-- the resolution on an ad-hoc basis without needing to alter the stored
-- values for the parameter
Maybe Int64 ->
-- | Job identifer. This is used to save execution statistics for each
-- inference evaluation
Expand Down
4 changes: 4 additions & 0 deletions inferno-ml-server-types/src/Inferno/ML/Server/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ data InferenceParam uid gid p s = InferenceParam
-- Inferno identifiers are always pointing to the correct input\/output;
-- otherwise we would need to rely on the order of the original identifiers
inputs :: Map Ident (SingleOrMany p, ScriptInputType),
-- | Resolution, passed to bridge routes
resolution :: Word64,
-- | The time that this parameter was \"deleted\", if any. For active
-- parameters, this will be @Nothing@
terminated :: Maybe UTCTime,
Expand All @@ -637,6 +639,7 @@ instance
<*> fmap wrappedTo (field @VCObjectHashRow)
<*> field
<*> fmap getAeson field
<*> fmap fromIntegral (field @Int64)
<*> field
<*> field

Expand All @@ -652,6 +655,7 @@ instance
ip ^. the @"script" & VCObjectHashRow & toField,
ip ^. the @"model" & toField,
ip ^. the @"inputs" & Aeson & toField,
ip ^. the @"resolution" & Aeson & toField,
toField Default,
ip ^. the @"user" & toField
]
Expand Down
3 changes: 3 additions & 0 deletions inferno-ml-server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2023.6.1
* Add `resolution` to `InferenceParam`

## 2023.5.29
* Change representation of script inputs/outputs

Expand Down
3 changes: 2 additions & 1 deletion inferno-ml-server/exe/ParseAndSave.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ saveScriptAndParam x now inputs conn = insertScript *> insertParam
-- tests, so we can just hard-code the ID here
(Id 1)
inputs
128
Nothing
$ entityIdFromInteger 0
where
q :: Query
q = [sql| INSERT INTO params VALUES (?, ?, ?, ?, ?, ?) |]
q = [sql| INSERT INTO params VALUES (?, ?, ?, ?, ?, ?, ?) |]

vcfunc :: VCObject
vcfunc = uncurry VCFunction x
Expand Down
2 changes: 1 addition & 1 deletion inferno-ml-server/inferno-ml-server.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.4
name: inferno-ml-server
version: 2023.5.29
version: 2023.6.1
synopsis: Server for Inferno ML
description: Server for Inferno ML
homepage: https://github.com/plow-technologies/inferno.git#readme
Expand Down
35 changes: 23 additions & 12 deletions inferno-ml-server/src/Inferno/ML/Server/Inference.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import Data.Generics.Wrapped (wrappedTo)
import Data.Int (Int64)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
import qualified Data.Text as Text
import Data.Time (UTCTime, getCurrentTime)
import Data.Time.Clock.POSIX (getPOSIXTime)
Expand Down Expand Up @@ -97,12 +96,13 @@ import UnliftIO.Timeout (timeout)
-- in the @/status@ endpoint (using @tryTakeMVar@)
runInferenceParam ::
Id InferenceParam ->
-- | Optional resolution, defaulting to 128. This is needed in case the
-- parameter evaluates a script that calls e.g. @valueAt@
-- | Optional resolution. If not provided, @InferenceParam.resolution@ will
-- be used. This is provided in order to allow users to override the stored
-- resolution without needing to alter the DB
Maybe Int64 ->
UUID ->
RemoteM (WriteStream IO)
runInferenceParam ipid (fromMaybe 128 -> res) uuid =
runInferenceParam ipid mres uuid =
withTimeoutMillis $ \t -> do
logTrace $ RunningInference ipid t
maybe (throwM (ScriptTimeout t)) pure
Expand Down Expand Up @@ -231,13 +231,22 @@ runInferenceParam ipid (fromMaybe 128 -> res) uuid =
mkChunks = awaitForever $ \(p, ws) ->
sourceList ws .| chunksOf 500 .| mapC (wrappedTo p,)

-- If the optional resolution override has been provided,
-- use that. Otherwise, use the resolution stored in the
-- parameter
resolution :: InverseResolution
resolution =
mres
^. non
(view (#resolution . to fromIntegral) param)
& toResolution

implEnv :: Map ExtIdent (Value BridgeMlValue m)
implEnv =
Map.fromList
[ (ExtIdent $ Right "now", VEpochTime t),
( ExtIdent $ Right "resolution",
VCustom . VExtended . VResolution $
toResolution res
VCustom . VExtended $ VResolution resolution
)
]
_ ->
Expand Down Expand Up @@ -347,7 +356,8 @@ getParameter iid =
q =
[sql|
SELECT * FROM params
WHERE id = ? AND terminated IS NULL
WHERE id = ?
AND terminated IS NULL
LIMIT 1
|]

Expand All @@ -361,7 +371,6 @@ getParameter iid =
-- cache! It can be run using e.g. 'withCurrentDirectory'
getAndCacheModel :: ModelCache -> Id ModelVersion -> RemoteM FilePath
getAndCacheModel cache mid = do
logTrace $ CopyingModel mid
-- Both the individual version is required (in order to fetch the contents)
-- as well as the parent model row (for the model name)
mversion <- getModelVersion mid
Expand All @@ -371,17 +380,19 @@ getAndCacheModel cache mid = do
copyAndCache :: Model -> ModelVersion -> RemoteM FilePath
copyAndCache model mversion =
versioned <$ do
unlessM (doesPathExist versioned) $
unlessM (doesPathExist versioned) $ do
logTrace $ CopyingModel mid
bitraverse_ checkCacheSize (writeBinaryFileDurableAtomic versioned)
=<< getModelSizeAndContents (view #contents mversion)
where
-- Cache the model with its specific version, i.e.
-- `<name>.ts.pt.<version>`, which will later be
-- symlinked to `<name>.ts.pt`
versioned :: FilePath
versioned =
model ^. #name . unpacked
& (<.> view (#version . to showVersion . unpacked) mversion)
versioned = model ^. #name . unpacked & (<.> v)
where
v :: FilePath
v = mversion ^. #version . to showVersion . unpacked

-- Checks that the configured cache size will not be exceeded by
-- caching the new model. If it will, least-recently-used models
Expand Down
2 changes: 1 addition & 1 deletion inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Inferno.ML.Server.Types
import Inferno.Module.Cast (ToValue (toValue))
import Inferno.Types.Value
( ImplicitCast (ImplicitCast),
Value (..),
Value (VDouble, VOne, VTuple),
liftImplEnvM,
)
import System.Posix.Types (EpochTime)
Expand Down
5 changes: 3 additions & 2 deletions inferno-ml-server/src/Inferno/ML/Server/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,12 @@ pattern InferenceParam ::
VCObjectHash ->
Id ModelVersion ->
Map Ident (SingleOrMany PID, ScriptInputType) ->
Word64 ->
Maybe UTCTime ->
EntityId UId ->
InferenceParam
pattern InferenceParam iid s m ios mt uid =
Types.InferenceParam iid s m ios mt uid
pattern InferenceParam iid s m ios res mt uid =
Types.InferenceParam iid s m ios res mt uid

pattern VCMeta ::
CTime ->
Expand Down
2 changes: 2 additions & 0 deletions nix/inferno-ml/migrations/v1-create-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ create table if not exists params
-- corresponding Haskell type contains `(p, ScriptInputType)`, with
-- the second element determining readability and writability
, inputs jsonb not null
-- Resolution passed to script evaluator
, resolution integer not null
-- See note above
, terminated timestamptz
, "user" integer references users (id)
Expand Down
Loading