-
Notifications
You must be signed in to change notification settings - Fork 49
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
Add sample unit tests #61
Add sample unit tests #61
Conversation
the black formatter checks are failing |
Thanks |
I tried it on my laptop and on a Red Hat server... both looked good: Mac Laptop:
Red Hat Server:
|
|
||
def test_str_to_torch_dtype(): | ||
for t in dtype_dict.keys(): | ||
assert data_type_utils.str_to_torch_dtype(t) == dtype_dict.get(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im not sure if this is a good function to test. THis is a function that deals with configs, but for such things, its better to defer to huggingface, because changes come in very fast
As an example, this function will fail if auto
is passed in, because it is not an "actual" torch dtype
however, it is a valid flag, as seen in this code https://github.com/huggingface/transformers/blob/0ad770c3733f9478a8d9d0bc18cc6143877b47a2/src/transformers/modeling_utils.py#L3317-L3339
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am testing based on my knowledge what those functions will return. If auto is a valid input then our functions should have logic to handle it. Then we can pass in auto to the unit test to make sure our functions will not get ValueError.
>>> from tuning.utils import data_type_utils
>>> data_type_utils.str_to_torch_dtype("auto")
ValueError: Unrecognized data type of a torch.Tensor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok i see, I think i misunderstood https://github.com/huggingface/transformers/blob/0ad770c3733f9478a8d9d0bc18cc6143877b47a2/src/transformers/modeling_utils.py#L2707-L2727,
the only str
value it will take is auto
, the rest relies on it to be a torch dtype. So i think its a valid function.
If think omitting auto
is a design decision. either way is fine.,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry an update to my previous comment
the only str value it will take is auto, the rest relies on it to be a torch dtype. So i think its a valid function.
It turns out that trl
has a ModelConfig
dataclass which handles torch_dtype
. I think its better to rely on that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabianlim Are you suggesting
- These functions need to to handle
auto
? If so create an issue. - Or we need to add more test case to test these functions? If so, what additional input to the functions do I need to test and what is the expected output of the functions do I need to assert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fabianlim @tedhtchang to re-iterate the purpose of this PR is to get some basic unit tests into fms-tuning to get started with tests in our CI/CD that automatically run with every pull request to help us catch any code changes that are breaking functionality.
The unit test being written here is to validate the function in library https://github.com/foundation-model-stack/fms-hf-tuning/blob/main/tuning/utils/data_type_utils.py#L11 which is a custom function written by us.
Any changes that need to be made to the underlying capabilities are NOT in scope of this PR. This PR is just testing what we support.
we do not currently support "auto" str to from_pretrained https://github.com/foundation-model-stack/fms-hf-tuning/blob/5a0cf5c1f2649d38f75ed02c2153c9d1850b20c7/tuning/sft_trainer.py#L129C21-L129C36
- that might be a good feature to add as a separate issue. (see line 129)
- @fabianlim when you see gaps like that, would advise to create an issue and tag me - you can also create one in this repo. when that feature is added, the corresponding unit test will be requested in that PR
on your point of using ModelConfig Fabian - TRL does the exact same thing with the torch_dtype = (
model_config.torch_dtype
if model_config.torch_dtype in ["auto", None]
else getattr(torch, model_config.torch_dtype)
https://github.com/huggingface/trl/blob/3bd02380c7e39cae2ec5013f27907b30bc4ce6bf/examples/scripts/sft.py#L74C2-L77C54
They use the modelConfig just to accept the parameter from user, we use our own config
fms-hf-tuning/tuning/config/configs.py
Line 20 in 5a0cf5c
class ModelArguments: |
The discussions on how to improve code should be separated from adding tests for existing code and getting a framework ready
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tedhtchang as far as these unit go, they look great , thank you! we do not need to test each and every torch dtype . If conversion works for some , it will work for others as it is using same codepath. So i will add a separate comment on reducing the number of tests - remember every parameter value you pass is like another test - CI/CD tests also need to run fast.
So we need CODE coverage and pass parameters that would go through a separate codepath while testing - example if "float32" were handled differently from "float64" then it makes sense to have 2 tests. Here all string to torch dtype is happening with 1 codepath - so we need not have 20+ tests for one codepath
Signed-off-by: ted chang <htchang@us.ibm.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some small minor changes, comments and test for ValueError
when these changes are addressed, PR will be merged
Co-authored-by: Sukriti Sharma <Ssukriti@users.noreply.github.com> Signed-off-by: ted chang <htchang@us.ibm.com>
Co-authored-by: Sukriti Sharma <Ssukriti@users.noreply.github.com> Signed-off-by: ted chang <htchang@us.ibm.com>
Signed-off-by: ted chang <htchang@us.ibm.com>
@@ -22,7 +22,7 @@ def str_to_torch_dtype(dtype_str: str) -> torch.dtype: | |||
dt = getattr(torch, dtype_str, None) | |||
if not isinstance(dt, torch.dtype): | |||
logger.error(" ValueError: Unrecognized data type of a torch.Tensor") | |||
exit(-1) | |||
raise ValueError("Unrecognized data type of a torch.Tensor") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work, thank you!
Thanks for the review. |
* add sample unit test Signed-off-by: ted chang <htchang@us.ibm.com> * Update tests/utils/test_data_type_utils.py Co-authored-by: Sukriti Sharma <Ssukriti@users.noreply.github.com> Signed-off-by: ted chang <htchang@us.ibm.com> * Update tests/utils/test_data_type_utils.py Co-authored-by: Sukriti Sharma <Ssukriti@users.noreply.github.com> Signed-off-by: ted chang <htchang@us.ibm.com> * re-raise valueError instead of exit Signed-off-by: ted chang <htchang@us.ibm.com> --------- Signed-off-by: ted chang <htchang@us.ibm.com> Co-authored-by: Sukriti Sharma <Ssukriti@users.noreply.github.com>
* add sample unit test Signed-off-by: ted chang <htchang@us.ibm.com> * Update tests/utils/test_data_type_utils.py Co-authored-by: Sukriti Sharma <Ssukriti@users.noreply.github.com> Signed-off-by: ted chang <htchang@us.ibm.com> * Update tests/utils/test_data_type_utils.py Co-authored-by: Sukriti Sharma <Ssukriti@users.noreply.github.com> Signed-off-by: ted chang <htchang@us.ibm.com> * re-raise valueError instead of exit Signed-off-by: ted chang <htchang@us.ibm.com> --------- Signed-off-by: ted chang <htchang@us.ibm.com> Co-authored-by: Sukriti Sharma <Ssukriti@users.noreply.github.com>
Changes:
Closes #5
Verify in a virtual env:
git clone https://github.com/tedhtchang/fms-hf-tuning -b add_sample_unit_test cd fms-hf-tuning pip install -r setup_requirement.txt tox run -e py
Should see something like: