Exemple #1
0
    def init_constraints(self, batch_constraints: Optional[Tensor], beam_size: int):
        self.constraint_states = []
        for constraint_tensor in batch_constraints:
            if self.representation == "ordered":
                constraint_state = OrderedConstraintState.create(constraint_tensor)
            elif self.representation == "unordered":
                constraint_state = UnorderedConstraintState.create(constraint_tensor)

            self.constraint_states.append([constraint_state for i in range(beam_size)])
Exemple #2
0
    def test_sequences(self):
        for constraints, tokens, expected in self.sequences:
            state = UnorderedConstraintState.create(
                pack_constraints([constraints])[0])
            for token in tokens:
                state = state.advance(token)
            result = {}
            for attr in expected.keys():
                result[attr] = getattr(state, attr)

            assert (result == expected
                    ), f"TEST({tokens}) GOT: {result} WANTED: {expected}"