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-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..83fa904 100644 --- a/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs +++ b/inferno-ml-server-types/src/Inferno/ML/Server/Types.hs @@ -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 @Int64) <*> 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/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/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 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 diff --git a/inferno-ml-server/src/Inferno/ML/Server/Inference.hs b/inferno-ml-server/src/Inferno/ML/Server/Inference.hs index ee6e50a..8275656 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 ) ] _ -> @@ -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 |] @@ -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 @@ -371,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 @@ -379,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 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..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 (..), + Value (VDouble, VOne, VTuple), 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)