TorchLean API

NN.API.Text.Vocabulary

Vocabulary Projection #

A bounded vocabulary retains token ids in first-occurrence order. Local id zero is reserved for an explicit unknown token; tokens beyond capacity map to it. Token streams remain tensors while the reverse lookup uses a hash map of vocabulary metadata.

Original token ids and reverse lookup for an assigned prefix of a fixed-capacity vocabulary.

  • originals : Tensor [capacity]

    Original token ids in local-id order; only the assigned prefix is used.

  • size :

    Number of assigned local ids, including the reserved unknown-token slot.

  • size_le_capacity : self.size capacity

    The assigned prefix fits in the storage tensor.

  • toLocalMap : Std.HashMap

    Lookup from original token ids to their assigned local ids.

  • unknownToken :

    Original token id returned when decoding an unassigned local id.

Instances For
    def TorchLean.text.VocabularyProjection.toLocal {capacity : } (vocab : VocabularyProjection capacity) (id : ) :

    Map an original token id to its assigned local id, defaulting to the unknown-token slot.

    Instances For
      def TorchLean.text.VocabularyProjection.toOriginal {capacity : } (vocab : VocabularyProjection capacity) (localId : ) :

      Decode an assigned local id, using the explicit unknown token for an unassigned id.

      Instances For
        def TorchLean.text.VocabularyProjection.singleton (capacity unknownToken : ) [NeZero capacity] :

        Start a vocabulary with one reserved unknown token.

        Instances For
          def TorchLean.text.VocabularyProjection.extend {capacity : } {shape : Shape} (vocab : VocabularyProjection capacity) (tokens : Tensor shape) :

          Extend in row-major token order, retaining existing ids when capacity is full.

          Instances For
            def TorchLean.text.VocabularyProjection.fromTokens (capacity unknownToken : ) [NeZero capacity] {shape : Shape} (tokens : Tensor shape) :

            Build a vocabulary from a token tensor, reserving local id zero for unknownToken.

            Instances For
              def TorchLean.text.VocabularyProjection.encode {capacity : } {shape : Shape} (vocab : VocabularyProjection capacity) (tokens : Tensor shape) :
              Tensor shape

              Project every token while retaining the shape of the original stream or batch.

              Instances For
                def TorchLean.text.VocabularyProjection.decode {capacity : } {shape : Shape} (vocab : VocabularyProjection capacity) (tokens : Tensor shape) :
                Tensor shape

                Decode every local token while retaining the stream or batch shape.

                Instances For