From d445b9f69293acafce77504ce5276a3ba848cf7e Mon Sep 17 00:00:00 2001 From: Rory Tyler Hayford Date: Sat, 1 Jun 2024 13:37:55 +0700 Subject: [PATCH 1/6] WIP: Add `InferenceParam.resolution` --- .../src/Inferno/ML/Server/Client.hs | 4 +++- .../src/Inferno/ML/Server/Types.hs | 6 +++++- .../src/Inferno/ML/Server/Inference.hs | 21 +++++++++++++------ .../src/Inferno/ML/Server/Module/Bridge.hs | 2 +- .../src/Inferno/ML/Server/Types.hs | 5 +++-- .../migrations/v1-create-tables.sql | 2 ++ 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/inferno-ml-server-types/src/Inferno/ML/Server/Client.hs b/inferno-ml-server-types/src/Inferno/ML/Server/Client.hs index 9cefbbe..bb3fbe9 100644 --- a/inferno-ml-server-types/src/Inferno/ML/Server/Client.hs +++ b/inferno-ml-server-types/src/Inferno/ML/Server/Client.hs @@ -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 diff --git a/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs b/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs index 447057a..cb543dd 100644 --- a/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs +++ b/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs @@ -28,7 +28,7 @@ import Data.Data (Typeable) import Data.Generics.Product (HasType (typed), the) import Data.Generics.Wrapped (wrappedTo) import qualified Data.IP -import Data.Int (Int64) +import Data.Int (Int64, Int32) import Data.List.NonEmpty (NonEmpty) import qualified Data.List.NonEmpty as NonEmpty import Data.Map.Strict (Map) @@ -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, @@ -637,6 +639,7 @@ instance <*> fmap wrappedTo (field @VCObjectHashRow) <*> field <*> fmap getAeson field + <*> fmap fromIntegral (field @Int32) <*> field <*> field @@ -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 ] diff --git a/inferno-ml-server/src/Inferno/ML/Server/Inference.hs b/inferno-ml-server/src/Inferno/ML/Server/Inference.hs index ee6e50a..18df6a3 100644 --- a/inferno-ml-server/src/Inferno/ML/Server/Inference.hs +++ b/inferno-ml-server/src/Inferno/ML/Server/Inference.hs @@ -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) @@ -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 @@ -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 ) ] _ -> diff --git a/inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs b/inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs index e0364cc..42e073e 100644 --- a/inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs +++ b/inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs @@ -11,7 +11,7 @@ import Inferno.ML.Server.Types import Inferno.Module.Cast (ToValue (toValue)) import Inferno.Types.Value ( ImplicitCast (ImplicitCast), - Value (..), + Value (VDouble, VTuple, VOne), liftImplEnvM, ) import System.Posix.Types (EpochTime) diff --git a/inferno-ml-server/src/Inferno/ML/Server/Types.hs b/inferno-ml-server/src/Inferno/ML/Server/Types.hs index c2d4ef3..3775a22 100644 --- a/inferno-ml-server/src/Inferno/ML/Server/Types.hs +++ b/inferno-ml-server/src/Inferno/ML/Server/Types.hs @@ -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 -> diff --git a/nix/inferno-ml/migrations/v1-create-tables.sql b/nix/inferno-ml/migrations/v1-create-tables.sql index 4d0bdbe..b9a90fa 100644 --- a/nix/inferno-ml/migrations/v1-create-tables.sql +++ b/nix/inferno-ml/migrations/v1-create-tables.sql @@ -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) From 4d16c9d5c4ba1f79be6974edb6b2f181d820bdcd Mon Sep 17 00:00:00 2001 From: Rory Tyler Hayford Date: Sat, 1 Jun 2024 13:44:30 +0700 Subject: [PATCH 2/6] Fix test exe --- inferno-ml-server/exe/ParseAndSave.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inferno-ml-server/exe/ParseAndSave.hs b/inferno-ml-server/exe/ParseAndSave.hs index 1baf63f..d5a03bc 100644 --- a/inferno-ml-server/exe/ParseAndSave.hs +++ b/inferno-ml-server/exe/ParseAndSave.hs @@ -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 From c019941ff2e3f0cc6e7305f488d61717192198d7 Mon Sep 17 00:00:00 2001 From: Rory Tyler Hayford Date: Sat, 1 Jun 2024 13:46:40 +0700 Subject: [PATCH 3/6] Version bump, changelog --- inferno-ml-server-types/CHANGELOG.md | 3 +++ inferno-ml-server-types/inferno-ml-server-types.cabal | 2 +- inferno-ml-server/CHANGELOG.md | 3 +++ inferno-ml-server/inferno-ml-server.cabal | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/inferno-ml-server-types/CHANGELOG.md b/inferno-ml-server-types/CHANGELOG.md index 6450fca..aca9cd9 100644 --- a/inferno-ml-server-types/CHANGELOG.md +++ b/inferno-ml-server-types/CHANGELOG.md @@ -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 diff --git a/inferno-ml-server-types/inferno-ml-server-types.cabal b/inferno-ml-server-types/inferno-ml-server-types.cabal index 7688808..6a7ac48 100644 --- a/inferno-ml-server-types/inferno-ml-server-types.cabal +++ b/inferno-ml-server-types/inferno-ml-server-types.cabal @@ -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 diff --git a/inferno-ml-server/CHANGELOG.md b/inferno-ml-server/CHANGELOG.md index 34e71fe..e894832 100644 --- a/inferno-ml-server/CHANGELOG.md +++ b/inferno-ml-server/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2023.6.1 +* Add `resolution` to `InferenceParam` + ## 2023.5.29 * Change representation of script inputs/outputs diff --git a/inferno-ml-server/inferno-ml-server.cabal b/inferno-ml-server/inferno-ml-server.cabal index ef74667..5c17c85 100644 --- a/inferno-ml-server/inferno-ml-server.cabal +++ b/inferno-ml-server/inferno-ml-server.cabal @@ -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 From 4d5350b8bb6adfe40a5c90f014b769fca8409479 Mon Sep 17 00:00:00 2001 From: Rory Tyler Hayford Date: Sat, 1 Jun 2024 13:53:25 +0700 Subject: [PATCH 4/6] Fix logging --- .../src/Inferno/ML/Server/Inference.hs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/inferno-ml-server/src/Inferno/ML/Server/Inference.hs b/inferno-ml-server/src/Inferno/ML/Server/Inference.hs index 18df6a3..8275656 100644 --- a/inferno-ml-server/src/Inferno/ML/Server/Inference.hs +++ b/inferno-ml-server/src/Inferno/ML/Server/Inference.hs @@ -356,7 +356,8 @@ getParameter iid = q = [sql| SELECT * FROM params - WHERE id = ? AND terminated IS NULL + WHERE id = ? + AND terminated IS NULL LIMIT 1 |] @@ -370,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 @@ -380,7 +380,8 @@ 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 @@ -388,9 +389,10 @@ getAndCacheModel cache mid = do -- `.ts.pt.`, which will later be -- symlinked to `.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 From b2651a6066afde745b10a76bc665e310df8a24f4 Mon Sep 17 00:00:00 2001 From: Rory Tyler Hayford Date: Mon, 3 Jun 2024 11:31:09 +0700 Subject: [PATCH 5/6] Formatting --- inferno-ml-server-types/src/Inferno/ML/Server/Types.hs | 2 +- inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs b/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs index cb543dd..5b9c0f8 100644 --- a/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs +++ b/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs @@ -28,7 +28,7 @@ import Data.Data (Typeable) import Data.Generics.Product (HasType (typed), the) import Data.Generics.Wrapped (wrappedTo) import qualified Data.IP -import Data.Int (Int64, Int32) +import Data.Int (Int32, Int64) import Data.List.NonEmpty (NonEmpty) import qualified Data.List.NonEmpty as NonEmpty import Data.Map.Strict (Map) diff --git a/inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs b/inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs index 42e073e..be7ec9b 100644 --- a/inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs +++ b/inferno-ml-server/src/Inferno/ML/Server/Module/Bridge.hs @@ -11,7 +11,7 @@ import Inferno.ML.Server.Types import Inferno.Module.Cast (ToValue (toValue)) import Inferno.Types.Value ( ImplicitCast (ImplicitCast), - Value (VDouble, VTuple, VOne), + Value (VDouble, VOne, VTuple), liftImplEnvM, ) import System.Posix.Types (EpochTime) From a821d74da517e33369a82068a56f7b11dd16d72b Mon Sep 17 00:00:00 2001 From: Rory Tyler Hayford Date: Tue, 4 Jun 2024 11:25:59 +0700 Subject: [PATCH 6/6] Ints --- inferno-ml-server-types/src/Inferno/ML/Server/Types.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs b/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs index 5b9c0f8..83fa904 100644 --- a/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs +++ b/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs @@ -28,7 +28,7 @@ import Data.Data (Typeable) import Data.Generics.Product (HasType (typed), the) import Data.Generics.Wrapped (wrappedTo) import qualified Data.IP -import Data.Int (Int32, Int64) +import Data.Int (Int64) import Data.List.NonEmpty (NonEmpty) import qualified Data.List.NonEmpty as NonEmpty import Data.Map.Strict (Map) @@ -639,7 +639,7 @@ instance <*> fmap wrappedTo (field @VCObjectHashRow) <*> field <*> fmap getAeson field - <*> fmap fromIntegral (field @Int32) + <*> fmap fromIntegral (field @Int64) <*> field <*> field