Store

public struct Store

A key-value based parameter store.

  • Declaration

    Swift

    public struct OpenFlag : OptionSet
  • Declaration

    Swift

    public struct Codec : OptionSet
  • Read a type-erased tensor from the store.

    Declaration

    Swift

    public func read(_ key: String, kind: DeviceKind? = nil, codec: Codec = []) -> NNC.AnyTensor?

    Parameters

    key

    The key corresponding to that particular tensor.

  • Read only shape of a tensor from the store.

    Declaration

    Swift

    public func read(like key: String) -> AnyTensor?

    Parameters

    like

    The key corresponding to that particular tensor.

  • Retrieve codec for a particular key. It must be a tensor to make sense of this.

    Declaration

    Swift

    public func codec(for key: String) -> Codec?

    Parameters

    key

    The key corresponding to a particular tensor.

  • Read a tensor from the store into tensor variable from dynamic graph.

    Declaration

    Swift

    @discardableResult
    public func read(_ key: String, variable: DynamicGraph_Any, kind: DeviceKind? = nil, codec: Codec = []) -> Bool

    Parameters

    key

    The key corresponding to that particular tensor.

    variable

    The tensor variable to be initialized with.

  • Declaration

    Swift

    public enum ModelReaderResult
  • Declaration

    Swift

    public enum ModelReadError : Error
  • Read parameters into a given model.

    Declaration

    Swift

    public func read(
      _ key: String, model: Model, strict: Bool, codec: Codec = [],
      reader: ((String, DataType, TensorFormat, TensorShape) -> ModelReaderResult)? = nil
    ) throws

    Parameters

    key

    The key corresponding to a particular model.

    model

    The model to be initialized with parameters from a given key.

    strict

    When this is true, will throw error if any parameters are missing.

    codec

    The codec for potential encoded parameters.

    reader

    You can customize your reader to load parameter with a different name etc.

  • Read parameters into a given model.

    Declaration

    Swift

    @inlinable
    public func read(
      _ key: String, model: Model, codec: Codec = [],
      reader: ((String, DataType, TensorFormat, TensorShape) -> ModelReaderResult)? = nil
    )

    Parameters

    key

    The key corresponding to a particular model.

    model

    The model to be initialized with parameters from a given key.

    codec

    The codec for potential encoded parameters.

    reader

    You can customize your reader to load parameter with a different name etc.

  • Read parameters into a given model builder.

    Declaration

    Swift

    @inlinable
    public func read(
      _ key: String, model: AnyModelBuilder, strict: Bool, codec: Codec = [],
      reader: ((String, DataType, TensorFormat, TensorShape) -> ModelReaderResult)? = nil
    ) throws

    Parameters

    key

    The key corresponding to a particular model.

    model

    The model builder to be initialized with parameters from a given key.

    strict

    When this is true, will throw error if any parameters are missing.

    codec

    The codec for potential encoded parameters.

    reader

    You can customize your reader to load parameter with a different name etc.

  • Read parameters into a given model builder.

    Declaration

    Swift

    @inlinable
    public func read(
      _ key: String, model: AnyModelBuilder, codec: Codec = [],
      reader: ((String, DataType, TensorFormat, TensorShape) -> ModelReaderResult)? = nil
    )

    Parameters

    key

    The key corresponding to a particular model.

    model

    The model builder to be initialized with parameters from a given key.

    codec

    The codec for potential encoded parameters.

    reader

    You can customize your reader to load parameter with a different name etc.

  • Read parameters into a given model.

    Declaration

    Swift

    @inlinable
    public func read(
      _ key: String, model: AnyModel, strict: Bool, codec: Codec = [],
      reader: ((String, DataType, TensorFormat, TensorShape) -> ModelReaderResult)? = nil
    ) throws

    Parameters

    key

    The key corresponding to a particular model.

    model

    The model to be initialized with parameters from a given key.

    strict

    When this is true, will throw error if any parameters are missing.

    codec

    The codec for potential encoded parameters.

    reader

    You can customize your reader to load parameter with a different name etc.

  • Read parameters into a given model.

    Declaration

    Swift

    @inlinable
    public func read(
      _ key: String, model: AnyModel, codec: Codec = [],
      reader: ((String, DataType, TensorFormat, TensorShape) -> ModelReaderResult)? = nil
    )

    Parameters

    key

    The key corresponding to a particular model.

    model

    The model to be initialized with parameters from a given key.

    codec

    The codec for potential encoded parameters.

    reader

    You can customize your reader to load parameter with a different name etc.

  • Declaration

    Swift

    public enum ModelWriteError : Error
  • Write a tensor to the store.

    Declaration

    Swift

    public func write(_ key: String, tensor: NNC.AnyTensor, strict: Bool, codec: Codec = []) throws

    Parameters

    key

    The key corresponding to a particular tensor.

    tensor

    The tensor to be persisted.

    strict

    Whether check error.

  • Write a tensor to the store.

    Declaration

    Swift

    @inlinable
    public func write(_ key: String, tensor: NNC.AnyTensor, codec: Codec = [])

    Parameters

    key

    The key corresponding to a particular tensor.

    tensor

    The tensor to be persisted.

  • Write a tensor variable to the store.

    Declaration

    Swift

    public func write(_ key: String, variable: DynamicGraph_Any, strict: Bool, codec: Codec = []) throws

    Parameters

    key

    The key corresponding to a particular tensor.

    variable

    The tensor variable to be persisted.

    strict

    Whether check error.

  • Write a tensor variable to the store.

    Declaration

    Swift

    @inlinable
    public func write(_ key: String, variable: DynamicGraph_Any, codec: Codec = [])

    Parameters

    key

    The key corresponding to a particular tensor.

    variable

    The tensor variable to be persisted.

  • Declaration

    Swift

    public enum ModelWriterResult
  • Write a model to the store.

    Declaration

    Swift

    public func write(
      _ key: String, model: Model, strict: Bool, codec: Codec = [],
      writer: ((String, NNC.AnyTensor) -> ModelWriterResult)? = nil
    ) throws

    Parameters

    key

    The key corresponding to a particular model.

    model

    The model where its parameters to be persisted.

    strict

    Whether check error.

    writer

    You can customize your writer to writer parameter with a different name or skip entirely.

  • Write a model to the store.

    Declaration

    Swift

    @inlinable
    public func write(
      _ key: String, model: Model, codec: Codec = [],
      writer: ((String, NNC.AnyTensor) -> ModelWriterResult)? = nil
    )

    Parameters

    key

    The key corresponding to a particular model.

    model

    The model where its parameters to be persisted.

    writer

    You can customize your writer to writer parameter with a different name or skip entirely.

  • Retrieve a list of all tensors in this file. This reads from the disk and could take some time to finish.

    Declaration

    Swift

    public var keys: [String] { get }
  • Remove one tensor by its key.

    Declaration

    Swift

    public func remove(_ key: String)
  • Remove all tensors from the store. This also vacuums the store to minimize its size.

    Declaration

    Swift

    public func removeAll()
  • Explicit vacuum the database.

    Declaration

    Swift

    public func vacuum()
  • Wrap the database ops in a transaction.

    Declaration

    Swift

    public func withTransaction<Result>(_ closure: () throws -> Result) rethrows -> Result