Class ContributionCache

java.lang.Object
opennlp.tools.depparse.ContributionCache

@ThreadSafe final class ContributionCache extends Object
Caches the hidden-layer contribution of each (feature position, embedding row) pair a FeedforwardDependencyParser scores, within a byte budget.

Each position has one table sized to the vocabulary block it reads, so a tag or label position never reserves a slot per word. The tables are charged to the budget up front; contribution vectors are charged when they are computed. Pairs that do not fit are scored directly. Contributions keep double precision, so cached and direct scoring return the same values.

Since:
3.0.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final long
    The bytes charged per contribution vector beyond its values.
    private final AtomicReferenceArray<double[]>[]
    The cached contribution vectors, indexed by feature position and row offset.
    private static final long
    The bytes of one double value.
    private final AtomicLong
    The number of contribution vectors published so far.
    private final long
    The bytes one contribution vector costs.
    private final int[]
    The first embedding row of the vocabulary block each position reads.
    private final FeedforwardNetwork
     
    (package private) static final long
    The bytes charged per table slot.
    private final AtomicLong
    The bytes the cache may still spend.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ContributionCache(FeedforwardNetwork network, long budgetBytes)
    Allocates the tables of a cache.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) double[]
    contribution(int position, int row)
    Returns the cached hidden-layer contribution of one pair, computing and publishing it on first sight while the budget lasts.
    (package private) long
     
    (package private) long
     
    (package private) long
     
    private boolean
    Reserves the bytes of one contribution vector.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • REFERENCE_BYTES

      static final long REFERENCE_BYTES
      The bytes charged per table slot.
      See Also:
    • ARRAY_HEADER_BYTES

      static final long ARRAY_HEADER_BYTES
      The bytes charged per contribution vector beyond its values.
      See Also:
    • DOUBLE_BYTES

      private static final long DOUBLE_BYTES
      The bytes of one double value.
      See Also:
    • network

      private final FeedforwardNetwork network
    • byPosition

      private final AtomicReferenceArray<double[]>[] byPosition
      The cached contribution vectors, indexed by feature position and row offset.
    • firstRow

      private final int[] firstRow
      The first embedding row of the vocabulary block each position reads.
    • entryBytes

      private final long entryBytes
      The bytes one contribution vector costs.
    • remainingBytes

      private final AtomicLong remainingBytes
      The bytes the cache may still spend.
    • entries

      private final AtomicLong entries
      The number of contribution vectors published so far.
  • Constructor Details

    • ContributionCache

      ContributionCache(FeedforwardNetwork network, long budgetBytes)
      Allocates the tables of a cache.
      Parameters:
      network - The network whose contributions are cached. Must not be null.
      budgetBytes - The bytes the tables and the contribution vectors may use together.
      Throws:
      IllegalArgumentException - Thrown if the tables alone exceed budgetBytes.
  • Method Details

    • contribution

      double[] contribution(int position, int row)
      Returns the cached hidden-layer contribution of one pair, computing and publishing it on first sight while the budget lasts.
      Parameters:
      position - The feature position.
      row - The embedding row at that position.
      Returns:
      The contribution vector, or null if the pair is not cached and the budget is spent.
    • reserve

      private boolean reserve()
      Reserves the bytes of one contribution vector.
      Returns:
      Whether the vector fits the remaining budget.
    • entries

      long entries()
      Returns:
      The number of contribution vectors the cache holds.
    • remainingBytes

      long remainingBytes()
      Returns:
      The bytes the cache may still spend on contribution vectors.
    • entryBytes

      long entryBytes()
      Returns:
      The bytes one contribution vector costs.