RWKV / rwkv.cpp

INT4/INT5/INT8 and FP16 inference on CPU for RWKV language model
MIT License
1.41k stars 95 forks source link

Replace all assertions in Python code with if statements #165

Closed saharNooby closed 7 months ago

saharNooby commented 7 months ago

Thanks to @chenpan321, I've become aware that Python is allowed to ignore assertions if -O flag is used.

I've always meant the assertions to be always-run checks; they test important preconditions and are meant to make user's life better by providing clear messages specifying the exact problem.

So, we need to replace all asserts with ifs.

See this PR for an example of a replacement.

whitealpa commented 7 months ago

Hi @saharNooby!

I'm a first-time contributor and I'm interested to work on this. If I understand correctly, you want it to be fixed like this, right?

From assert init_prompt != '', 'Prompt must not be empty'

To if init_prompt != '': raise AssertionError('Prompt must not be empty')

If so, I think I'd be able to help. 👍

chenpan321 commented 7 months ago

@whitealpa It's good! But if possible, I'd like to suggest that we use error types other than AssertionError here. The Python documents (here) suggests:

exception AssertionError Raised when an assert statement fails.

Since we do not assert here, other error types would better fit the code context. Maybe ValueErrors would be better?

It's nice to see we have more contributors for help!

whitealpa commented 7 months ago

@chenpan321 Oh, my.. You’re absolutely right! I’ve read your PR but still missed it. 😅 Thank you for pointing that out. I’ll make a PR soon.

saharNooby commented 7 months ago

@whitealpa Note that the expression must be inverted. You've suggested:

if init_prompt != '':
    raise AssertionError('Prompt must not be empty')

...but it will actually throw the exception when the prompt is not empty, which is not what we've intended. The correct option would be either of those:

if not (init_prompt != ''):
    raise AssertionError('Prompt must not be empty')

# ...or in simplified form:
if init_prompt == '':
    raise AssertionError('Prompt must not be empty')
saharNooby commented 7 months ago

As for exception type, I think ValueError is fine indeed.

whitealpa commented 7 months ago

@saharNooby @chenpan321 I've finished the edit but have a problem with DCO. I'll try open a pull request again.

whitealpa commented 7 months ago

Please check: https://github.com/RWKV/rwkv.cpp/pull/167