GithubHelp home page GithubHelp logo

Comments (1)

La1c avatar La1c commented on July 22, 2024

I played around with it a bit more and the funny thing is that the following code works just fine.
The main difference is the order of operations: encoding "candidates" first and "queries" later.

candidates_dataset = Dataset.from_dict({'text': texts})
candidates_dataset = candidates_dataset.map(
        lambda x: tokenizer(x['text'],
                            padding=False,
                            max_length=512,
                            truncation=True),
        batched=True, remove_columns=['text']
    )
candidates_dataset.set_format(type='torch', columns=['input_ids',
                                                      'attention_mask',
                                                      'token_type_ids'])
tokenized_query = tokenizer(query,
                            padding=False,
                            max_length=512,
                            truncation=True,
                            return_tensors='pt')

data_collator = DataCollatorWithPadding(tokenizer, return_tensors='pt', padding=True)

dataloader = torch.utils.data.DataLoader(candidates_dataset,
                                          batch_size=8,
                                          collate_fn=data_collator,
                                          pin_memory=True
                                          )

## Here is the main difference with above: process inputs from data loader first and the query after that.
with torch.no_grad():
  for batch_texts in dataloader:
      batch_input = {
            'input_ids': batch_texts['input_ids'].to('cuda'),
            'token_type_ids': batch_texts['token_type_ids'].to('cuda'),
            'attention_mask': batch_texts['attention_mask'].to('cuda')
        }
      encoded = model_ds(**batch_input)

  query_input = {
        'input_ids': tokenized_query['input_ids'].to('cuda'),
        'token_type_ids': tokenized_query['token_type_ids'].to('cuda'),
        'attention_mask': tokenized_query['attention_mask'].to('cuda')
    }
  query_output = model_ds(**query_input)

I have no idea, why it works this way, so any comment on the issue would be really appreciated.

from deepspeed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.