cloud-ai-sdk icon indicating copy to clipboard operation
cloud-ai-sdk copied to clipboard

LLM Output Text formatting Issue

Open yh-yao opened this issue 1 year ago • 1 comments

https://github.com/quic/cloud-ai-sdk/blob/249ffee217d353c0b710abcf836645c211ef16bb/models/language_processing/decoder/LlamaForCausalLM/runModel.py#L109

  1. Incorrect format for streaming output Unknown

  2. Incorrect format for non-steaming output Unknown-2

yh-yao avatar Apr 04 '24 23:04 yh-yao

Hello Yuhang , I checked the source code of runModel.py. It just call the qaic_infer directly. There is no pre-process and post-process similar in transformers. So the output is strange. If you would like to leverage the function(like logits_processor,logits_warper and etc). You need to implement similar function as transformers. In parallel, I know Qualcomm are developing similar python package.

You can refer the code in ./transformers/generation/utils.py::GenerationMixin:_sample. ........................

        if synced_gpus and this_peer_finished:
            continue  # don't waste resources running the code we don't need

        next_token_logits = outputs.logits[:, -1, :]

        # pre-process distribution
        next_token_scores = logits_processor(input_ids, next_token_logits)
        next_token_scores = logits_warper(input_ids, next_token_scores)

        # Store scores, attentions and hidden_states when required
        if return_dict_in_generate:
            if output_scores:
                scores += (next_token_scores,)
            if output_logits:
                raw_logits += (next_token_logits,)
            if output_attentions:
                decoder_attentions += (
                    (outputs.decoder_attentions,) if self.config.is_encoder_decoder else (outputs.attentions,)
                )
                if self.config.is_encoder_decoder:
                    cross_attentions += (outputs.cross_attentions,)

            if output_hidden_states:
                decoder_hidden_states += (
                    (outputs.decoder_hidden_states,)
                    if self.config.is_encoder_decoder
                    else (outputs.hidden_states,)
                )

        # sample
        probs = nn.functional.softmax(next_token_scores, dim=-1)
        next_tokens = torch.multinomial(probs, num_samples=1).squeeze(1)

        # finished sentences should have their next token be a padding token
        if eos_token_id is not None:
            if pad_token_id is None:
                raise ValueError("If `eos_token_id` is defined, make sure that `pad_token_id` is defined.")
            next_tokens = next_tokens * unfinished_sequences + pad_token_id * (1 - unfinished_sequences)

        # update generated ids, model inputs, and length for next step
        input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1)
        if streamer is not None:
            streamer.put(next_tokens.cpu())
        model_kwargs = self._update_model_kwargs_for_generation(
            outputs,
            model_kwargs,
            is_encoder_decoder=self.config.is_encoder_decoder,
        )

        # if eos_token was found in one sentence, set sentence to finished
        if eos_token_id_tensor is not None:
            unfinished_sequences = unfinished_sequences.mul(
                next_tokens.tile(eos_token_id_tensor.shape[0], 1).ne(eos_token_id_tensor.unsqueeze(1)).prod(dim=0)
            )

        unfinished_sequences = unfinished_sequences & ~stopping_criteria(input_ids, scores)
        this_peer_finished = unfinished_sequences.max() == 0

    if streamer is not None:
        streamer.end()

...................

dapengsmith avatar Apr 23 '24 06:04 dapengsmith