Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in torch_multimodal_clip: IndexError: list index out of range #2458

Closed
ostrowskimarcin opened this issue Sep 17, 2024 · 0 comments
Closed

Comments

@ostrowskimarcin
Copy link
Contributor

Hi, I was testing some of torchbench models and I found a simple bug that can be resolved.

Model: torch_multimodal_clip

Reproducing steps:

  1. python3 install.py model torch_multimodal_clip
  2. python3 test.py -v -k "test_torch_multimodal_clip_eval_cuda"

Error:

Running tests...

test_torch_multimodal_clip_eval_cuda (main.TestBenchmark) ... ERROR (9.481s)

======================================================================
ERROR [9.481s]: test_torch_multimodal_clip_eval_cuda (main.TestBenchmark)

torchbenchmark._components._impl.workers.subprocess_rpc.ChildTraceException: Traceback (most recent call last):
File "/root/torchbench/torchbenchmark/_components/_impl/workers/subprocess_rpc.py", line 482, in _run_block
exec( # noqa: P204
File "", line 2, in
File "/root/torchbench/torchbenchmark/util/model.py", line 317, in invoke
out = self.eval()
File "/root/torchbench/torchbenchmark/models/torch_multimodal_clip/init.py", line 91, in eval
return self.text[torch.argmax(score)]
IndexError: list index out of range

Solution:
The problem is inside init.py script. Default batch size is equal to 32, so the product of matrix multiplication returned as score is size [32,32]. In my opinion the goal is to find the highest score in each batch, but the method torch.argmax(score) returns global index (960). I think there's a missing argument of dim=1 to focus on each batch.
Moreover, after getting index or rather indices, in next step we want to get values from self.text field. I assume that we want to refer to self.texts according to proper batch_size.

I propose to change some lines in eval() method:

def eval(self):
        self.model.eval()
        image_embedding, text_embedding = self.model(
            self.image_tensor, self.text_tensor
        )
        score = image_embedding @ text_embedding.t()

        return self.text[torch.argmax(score)]

to

def eval(self):
        self.model.eval()
        image_embedding, text_embedding = self.model(
            self.image_tensor, self.text_tensor
        )
        score = image_embedding @ text_embedding.t()

        indices = torch.argmax(score, dim=1)
        return [self.texts[i][indices[i].item()] for i in range(self.batch_size)]

This works for any batch_size:

Running tests...

test_torch_multimodal_clip_eval_cuda (main.TestBenchmark) ... ok (9.999s)


Ran 1 test in 9.999s

OK

ostrowskimarcin added a commit to ostrowskimarcin/benchmark that referenced this issue Sep 17, 2024
facebook-github-bot pushed a commit that referenced this issue Sep 28, 2024
Summary:
#2458

Pull Request resolved: #2459

Reviewed By: xuzhao9

Differential Revision: D63476542

Pulled By: kit1980

fbshipit-source-id: 01e9db9cb03d34e82a773897417df2ccda410634
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant