Copyright | Copyright (c) 2018 Preferred Networks Inc. |
---|---|
License | MIT (see the file LICENSE) |
Maintainer | Masahiro Sakai <sakai@preferred.jp> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Haskell binding for Menoh DNN inference library.
Basic usage
- Load computation graph from ONNX file using
makeModelDataFromONNXFile
. - Specify input variable type/dimentions (in particular batch size) and
which output variables you want to retrieve. This can be done by
constructing
VariableProfileTable
usingmakeVariableProfileTable
. - Optimize
ModelData
with respect to yourVariableProfileTable
by usingoptimizeModelData
. - Construct a
Model
usingmakeModel
ormakeModelWithConfig
. If you want to use custom buffers instead of internally allocated ones, You need to use more low levelModelBuilder
. - Load input data. This can be done conveniently using
writeBuffer
. There are also more low-level API such asunsafeGetBuffer
andwithBuffer
. - Run inference using
run
. - Retrieve the result data. This can be done conveniently using
readBuffer
.
Note on thread safety
TL;DR: If you want to use Menoh from multiple haskell threads, you need to
use threaded RTS by supplying -threaded
option to GHC.
Menoh uses thread local storage (TLS) for storing error information, and the only way to use TLS safely is to use in bound threads (see Control.Concurrent).
- In threaded RTS (i.e.
rtsSupportsBoundThreads
is True), this module runs computation in bound threads by usingrunInBoundThread
. (If the calling thread is not bound,runInBoundThread
create a bound thread temporarily and run the computation inside it). - In non-threaded RTS, this module does not use
runInBoundThread
and is therefore unsafe to use from multiple haskell threads. Using non-threaded RTS is allowed for the sake of convenience (e.g. running in GHCi) despite its unsafety.
Synopsis
- type Dims = [Int]
- data DType
- data Error
- = ErrorStdError String
- | ErrorUnknownError String
- | ErrorInvalidFilename String
- | ErrorONNXParseError String
- | ErrorInvalidDType String
- | ErrorInvalidAttributeType String
- | ErrorUnsupportedOperatorAttribute String
- | ErrorDimensionMismatch String
- | ErrorVariableNotFound String
- | ErrorIndexOutOfRange String
- | ErrorJSONParseError String
- | ErrorInvalidBackendName String
- | ErrorUnsupportedOperator String
- | ErrorFailedToConfigureOperator String
- | ErrorBackendError String
- | ErrorSameNamedVariableAlreadyExist String
- | UnsupportedInputDims String
- | SameNamedParameterAlreadyExist String
- | SameNamedAttributeAlreadyExist String
- | InvalidBackendConfigError String
- | InputNotFoundError String
- | OutputNotFoundError String
- newtype ModelData = ModelData (ForeignPtr MenohModelData)
- makeModelDataFromONNXFile :: MonadIO m => FilePath -> m ModelData
- makeModelDataFromONNX :: MonadIO m => FilePath -> m ModelData
- makeModelDataFromONNXByteString :: MonadIO m => ByteString -> m ModelData
- optimizeModelData :: MonadIO m => ModelData -> VariableProfileTable -> m ()
- makeModelData :: MonadIO m => m ModelData
- addParameterFromPtr :: MonadIO m => ModelData -> String -> DType -> Dims -> Ptr a -> m ()
- addNewNode :: MonadIO m => ModelData -> String -> m ()
- addInputNameToCurrentNode :: MonadIO m => ModelData -> String -> m ()
- addOutputNameToCurrentNode :: MonadIO m => ModelData -> String -> m ()
- class AttributeType value where
- addAttribute :: (AttributeType value, MonadIO m) => ModelData -> String -> value -> m ()
- newtype VariableProfileTable = VariableProfileTable (ForeignPtr MenohVariableProfileTable)
- makeVariableProfileTable :: (AddOutput a, MonadIO m) => [(String, DType, Dims)] -> [a] -> ModelData -> m VariableProfileTable
- vptGetDType :: MonadIO m => VariableProfileTable -> String -> m DType
- vptGetDims :: MonadIO m => VariableProfileTable -> String -> m Dims
- newtype Model = Model (ForeignPtr MenohModel)
- makeModel :: MonadIO m => VariableProfileTable -> ModelData -> String -> m Model
- makeModelWithConfig :: (MonadIO m, ToJSON a) => VariableProfileTable -> ModelData -> String -> a -> m Model
- run :: MonadIO m => Model -> m ()
- getDType :: MonadIO m => Model -> String -> m DType
- getDims :: MonadIO m => Model -> String -> m Dims
- class ToBuffer a where
- class FromBuffer a where
- writeBuffer :: (ToBuffer a, MonadIO m) => Model -> String -> a -> m ()
- readBuffer :: (FromBuffer a, MonadIO m) => Model -> String -> m a
- unsafeGetBuffer :: MonadIO m => Model -> String -> m (Ptr a)
- withBuffer :: forall m r a. (MonadIO m, MonadBaseControl IO m) => Model -> String -> (Ptr a -> m r) -> m r
- class Storable a => HasDType a where
- writeBufferFromVector :: forall v a m. (Vector v a, HasDType a, MonadIO m) => Model -> String -> v a -> m ()
- writeBufferFromStorableVector :: forall a m. (HasDType a, MonadIO m) => Model -> String -> Vector a -> m ()
- readBufferToVector :: forall v a m. (Vector v a, HasDType a, MonadIO m) => Model -> String -> m (v a)
- readBufferToStorableVector :: forall a m. (HasDType a, MonadIO m) => Model -> String -> m (Vector a)
- version :: Version
- bindingVersion :: Version
- newtype VariableProfileTableBuilder = VariableProfileTableBuilder (ForeignPtr MenohVariableProfileTableBuilder)
- makeVariableProfileTableBuilder :: MonadIO m => m VariableProfileTableBuilder
- addInputProfileDims2 :: MonadIO m => VariableProfileTableBuilder -> String -> DType -> (Int, Int) -> m ()
- addInputProfileDims4 :: MonadIO m => VariableProfileTableBuilder -> String -> DType -> (Int, Int, Int, Int) -> m ()
- addOutputName :: MonadIO m => VariableProfileTableBuilder -> String -> m ()
- addOutputProfile :: MonadIO m => VariableProfileTableBuilder -> String -> DType -> m ()
- class AddOutput a where
- buildVariableProfileTable :: MonadIO m => VariableProfileTableBuilder -> ModelData -> m VariableProfileTable
- newtype ModelBuilder = ModelBuilder (ForeignPtr MenohModelBuilder)
- makeModelBuilder :: MonadIO m => VariableProfileTable -> m ModelBuilder
- attachExternalBuffer :: MonadIO m => ModelBuilder -> String -> Ptr a -> m ()
- buildModel :: MonadIO m => ModelBuilder -> ModelData -> String -> m Model
- buildModelWithConfig :: (MonadIO m, ToJSON a) => ModelBuilder -> ModelData -> String -> a -> m Model
Basic data types
Data type of array elements
DTypeFloat | single precision floating point number |
DTypeUnknown !MenohDType | types that this binding is unware of |
Functions in this module can throw this exception type.
ModelData type
ModelData
contains model parameters and computation graph structure.
makeModelDataFromONNXFile :: MonadIO m => FilePath -> m ModelData Source #
Load onnx file and make ModelData
.
makeModelDataFromONNX :: MonadIO m => FilePath -> m ModelData Source #
Deprecated: use makeModelDataFromONNXFile instead
Load onnx file and make ModelData
.
makeModelDataFromONNXByteString :: MonadIO m => ByteString -> m ModelData Source #
make ModelData
from on-memory ByteString
.
optimizeModelData :: MonadIO m => ModelData -> VariableProfileTable -> m () Source #
Manual model data construction API
addParameterFromPtr :: MonadIO m => ModelData -> String -> DType -> Dims -> Ptr a -> m () Source #
Add a new parameter in ModelData
This API is tentative and will be changed in the future.
Duplication of parameter_name is not allowed and it throws error.
addInputNameToCurrentNode :: MonadIO m => ModelData -> String -> m () Source #
Add a new input name to latest added node in ModelData
addOutputNameToCurrentNode :: MonadIO m => ModelData -> String -> m () Source #
Add a new output name to latest added node in ModelData
class AttributeType value where Source #
A class of types that can be added to nodes using addAttribute
.
basicAddAttribute :: Ptr MenohModelData -> CString -> value -> IO () Source #
Instances
AttributeType Float Source # | |
Defined in Menoh basicAddAttribute :: Ptr MenohModelData -> CString -> Float -> IO () Source # | |
AttributeType Int Source # | |
Defined in Menoh basicAddAttribute :: Ptr MenohModelData -> CString -> Int -> IO () Source # | |
AttributeType [Float] Source # | |
Defined in Menoh basicAddAttribute :: Ptr MenohModelData -> CString -> [Float] -> IO () Source # | |
AttributeType [Int] Source # | |
Defined in Menoh basicAddAttribute :: Ptr MenohModelData -> CString -> [Int] -> IO () Source # |
addAttribute :: (AttributeType value, MonadIO m) => ModelData -> String -> value -> m () Source #
Add a new attribute to latest added node in model_data
VariableProfileTable
newtype VariableProfileTable Source #
VariableProfileTable
contains information of dtype and dims of variables.
Users can access to dtype and dims via vptGetDType
and vptGetDims
.
makeVariableProfileTable Source #
:: (AddOutput a, MonadIO m) | |
=> [(String, DType, Dims)] | input names with dtypes and dims |
-> [a] | |
-> ModelData | model data |
-> m VariableProfileTable |
Convenient function for constructing VariableProfileTable
.
If you need finer control, you can use VariableProfileTableBuidler
.
vptGetDType :: MonadIO m => VariableProfileTable -> String -> m DType Source #
Accessor function for VariableProfileTable
Select variable name and get its DType
.
vptGetDims :: MonadIO m => VariableProfileTable -> String -> m Dims Source #
Accessor function for VariableProfileTable
Select variable name and get its Dims
.
Model type
:: MonadIO m | |
=> VariableProfileTable | variable profile table |
-> ModelData | model data |
-> String | backend name |
-> m Model |
Convenient methods for constructing a Model
.
:: (MonadIO m, ToJSON a) | |
=> VariableProfileTable | variable profile table |
-> ModelData | model data |
-> String | backend name |
-> a | backend config |
-> m Model |
Similar to makeModel
but backend-specific configuration can be supplied.
run :: MonadIO m => Model -> m () Source #
Run model inference.
This function can't be called asynchronously.
Accessors for buffers
class ToBuffer a where Source #
Type that can be written to menoh's buffer.
class FromBuffer a where Source #
Type that can be read from menoh's buffer.
Instances
FromBuffer a => FromBuffer [a] Source # | |
FromBuffer (Vector Float) Source # | |
FromBuffer (Vector Float) Source # | |
FromBuffer (Vector Float) Source # | |
writeBuffer :: (ToBuffer a, MonadIO m) => Model -> String -> a -> m () Source #
Write values to the given model's buffer
readBuffer :: (FromBuffer a, MonadIO m) => Model -> String -> m a Source #
Read values from the given model's buffer
Low-level accessors for buffers
unsafeGetBuffer :: MonadIO m => Model -> String -> m (Ptr a) Source #
Get a buffer handle attached to target variable.
Users can get a buffer handle attached to target variable.
If that buffer is allocated by users and attached to the variable by calling
attachExternalBuffer
, returned buffer handle is same to it.
This function is unsafe because it does not prevent the model to be GC'ed and the returned pointer become dangling pointer.
See also withBuffer
.
withBuffer :: forall m r a. (MonadIO m, MonadBaseControl IO m) => Model -> String -> (Ptr a -> m r) -> m r Source #
This function takes a function which is applied to the buffer associated to specified variable. The resulting action is then executed. The buffer is kept alive at least during the whole action, even if it is not used directly inside. Note that it is not safe to return the pointer from the action and use it after the action completes.
See also unsafeGetBuffer
.
Deprecated accessors for buffers
class Storable a => HasDType a where Source #
Deprecated: use FromBuffer/ToBuffer instead
Haskell types that have associated DType
type code.
writeBufferFromVector :: forall v a m. (Vector v a, HasDType a, MonadIO m) => Model -> String -> v a -> m () Source #
Deprecated: Use ToBuffer class and writeBuffer instead
Copy whole elements of Vector
into a model's buffer
writeBufferFromStorableVector :: forall a m. (HasDType a, MonadIO m) => Model -> String -> Vector a -> m () Source #
Deprecated: Use ToBuffer class and writeBuffer instead
Copy whole elements of
into a model's bufferVector
a
readBufferToVector :: forall v a m. (Vector v a, HasDType a, MonadIO m) => Model -> String -> m (v a) Source #
Deprecated: Use FromBuffer class and readBuffer instead
Read whole elements of Array
and return as a Vector
.
readBufferToStorableVector :: forall a m. (HasDType a, MonadIO m) => Model -> String -> m (Vector a) Source #
Deprecated: Use FromBuffer class and readBuffer instead
Read whole eleemnts of Array
and return as a Vector
.
Misc
bindingVersion :: Version Source #
Version of this Haskell binding. (Not the version of Menoh itself)
Low-level API
Builder for VariableProfileTable
newtype VariableProfileTableBuilder Source #
Builder for creation of VariableProfileTable
.
makeVariableProfileTableBuilder :: MonadIO m => m VariableProfileTableBuilder Source #
Factory function for VariableProfileTableBuilder
.
Deprecated: use addInputProfileDims instead
Add 2D input profile.
Input profile contains name, dtype and dims (num, size)
.
This 2D input is conventional batched 1D inputs.
:: MonadIO m | |
=> VariableProfileTableBuilder | |
-> String | |
-> DType | |
-> (Int, Int, Int, Int) | (num, channel, height, width) |
-> m () |
Deprecated: use addInputProfileDims instead
Add 4D input profile
Input profile contains name, dtype and dims (num, channel, height, width)
.
This 4D input is conventional batched image inputs. Image input is
3D (channel, height, width).
addOutputName :: MonadIO m => VariableProfileTableBuilder -> String -> m () Source #
addOutputProfile :: MonadIO m => VariableProfileTableBuilder -> String -> DType -> m () Source #
Deprecated: use addOutputName instead
Add output profile
Output profile contains name and dtype. Its Dims
are calculated automatically,
so that you don't need to specify explicitly.
class AddOutput a where Source #
Type class for abstracting addOutputProfile
and addOutputName
.
addOutput :: VariableProfileTableBuilder -> a -> IO () Source #
buildVariableProfileTable :: MonadIO m => VariableProfileTableBuilder -> ModelData -> m VariableProfileTable Source #
Factory function for VariableProfileTable
Builder for Model
newtype ModelBuilder Source #
Helper for creating of Model
.
makeModelBuilder :: MonadIO m => VariableProfileTable -> m ModelBuilder Source #
Factory function for ModelBuilder
attachExternalBuffer :: MonadIO m => ModelBuilder -> String -> Ptr a -> m () Source #
Attach a buffer which allocated by users.
Users can attach a external buffer which they allocated to target variable.
Variables attached no external buffer are attached internal buffers allocated automatically.
Users can get that internal buffer handle by calling unsafeGetBuffer
etc. later.
:: MonadIO m | |
=> ModelBuilder | |
-> ModelData | |
-> String | backend name |
-> m Model |
Factory function for Model
.
:: (MonadIO m, ToJSON a) | |
=> ModelBuilder | |
-> ModelData | |
-> String | backend name |
-> a | backend config |
-> m Model |
Similar to buildModel
, but backend specific configuration can be supplied as JSON.