Skip to content

perplexity : fix integer overflow #9783

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

Merged
merged 2 commits into from
Oct 9, 2024
Merged

Conversation

ggerganov
Copy link
Member

fix #9779

There was an integer overflow in i*n_vocab:

frame #1: 0x0000000100403db8 llama-perplexity`process_logits(int, float const*, int const*, int, std::__1::vector<std::__1::thread, std::__1::allocator<std::__1::thread>>&, double&, double&, float*, float*)::$_0::operator()(this=0x0000600013e88068) const at perplexity.cpp:172:49
   169 	               break;
   170 	           }
   171 	           lock.unlock();
-> 172 	           const results_log_softmax results = log_softmax(n_vocab, logits + i*n_vocab, tokens[i+1]);
   173 	           const double v = -results.log_softmax;
   174 	           local_nll += v;
   175 	           local_nll2 += v*v;

(lldb) print i
(int) 14242
(lldb) print n_vocab
(int) 152064
(lldb) print i*n_vocab
(int) -2129271808

@ggerganov ggerganov force-pushed the gg/perplexity-fix-int-overflow branch from 3229586 to 22cc760 Compare October 8, 2024 06:23
Comment on lines 1720 to 1723
int64_t n_vocab;
int64_t n_chunk;
in.read((char *)&n_vocab, sizeof(n_vocab));
in.read((char *)&n_chunk, sizeof(n_chunk));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using int64_t here is for n_chunk was incorrect. Pushing a fix

Comment on lines +696 to +697
for (int i = 0; i < (int) batch.n_tokens; i += n_batch) {
const int n_tokens = std::min<int>(n_batch, batch.n_tokens - i);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the C standard only guarantees at least 16 bit for int.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Wikpedia:

The standard integer size is platform-dependent. In C, it is denoted by int and required to be at least 16 bits. Windows and Unix systems have 32-bit ints on both 32-bit and 64-bit architectures.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to cppreference:

int — basic integer type. The keyword int may be omitted if any of the modifiers listed below are used. If no length modifiers are present, it's guaranteed to have a width of at least 16 bits. However, on 32/64 bit systems it is almost exclusively guaranteed to have width of at least 32 bits (see below).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt this is a concern. We can safely assume that int will always be 32-bit

@ggerganov ggerganov merged commit e702206 into master Oct 9, 2024
58 checks passed
@ggerganov ggerganov deleted the gg/perplexity-fix-int-overflow branch October 9, 2024 14:00
dsx1986 pushed a commit to dsx1986/llama.cpp that referenced this pull request Oct 29, 2024
* perplexity : fix integer overflow

ggml-ci

* perplexity : keep n_vocab as int and make appropriate casts

ggml-ci
arthw pushed a commit to arthw/llama.cpp that referenced this pull request Nov 15, 2024
* perplexity : fix integer overflow

ggml-ci

* perplexity : keep n_vocab as int and make appropriate casts

ggml-ci
arthw pushed a commit to arthw/llama.cpp that referenced this pull request Nov 18, 2024
* perplexity : fix integer overflow

ggml-ci

* perplexity : keep n_vocab as int and make appropriate casts

ggml-ci
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: llama-perplexity segfaults
2 participants