-
Notifications
You must be signed in to change notification settings - Fork 743
Support Half/Bfloat for rand() and fill(). #8123
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,6 +234,20 @@ TEST_F(TensorPtrMakerTest, CreateFull) { | |
| EXPECT_EQ(tensor4->size(1), 5); | ||
| EXPECT_EQ(tensor4->scalar_type(), executorch::aten::ScalarType::Double); | ||
| EXPECT_EQ(tensor4->const_data_ptr<double>()[0], 11); | ||
|
|
||
| auto tensor5 = full({4, 5}, 13, executorch::aten::ScalarType::Half); | ||
| EXPECT_EQ(tensor5->dim(), 2); | ||
| EXPECT_EQ(tensor5->size(0), 4); | ||
| EXPECT_EQ(tensor5->size(1), 5); | ||
| EXPECT_EQ(tensor5->scalar_type(), executorch::aten::ScalarType::Half); | ||
| EXPECT_EQ(tensor5->const_data_ptr<executorch::aten::Half>()[0], 13); | ||
|
|
||
| auto tensor6 = full({4, 5}, 15, executorch::aten::ScalarType::BFloat16); | ||
| EXPECT_EQ(tensor6->dim(), 2); | ||
| EXPECT_EQ(tensor6->size(0), 4); | ||
| EXPECT_EQ(tensor6->size(1), 5); | ||
| EXPECT_EQ(tensor6->scalar_type(), executorch::aten::ScalarType::BFloat16); | ||
| EXPECT_EQ(tensor6->const_data_ptr<executorch::aten::BFloat16>()[0], 15); | ||
| } | ||
|
|
||
| TEST_F(TensorPtrMakerTest, CreateScalar) { | ||
|
|
@@ -363,6 +377,36 @@ TEST_F(TensorPtrMakerTest, CreateRandTensorWithDoubleType) { | |
| } | ||
| } | ||
|
|
||
| TEST_F(TensorPtrMakerTest, CreateRandTensorWithHalfType) { | ||
| auto tensor = rand({4, 5}, executorch::aten::ScalarType::Half); | ||
|
|
||
| EXPECT_EQ(tensor->dim(), 2); | ||
| EXPECT_EQ(tensor->size(0), 4); | ||
| EXPECT_EQ(tensor->size(1), 5); | ||
| EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::Half); | ||
|
|
||
| for (auto i = 0; i < tensor->numel(); ++i) { | ||
| auto val = tensor->const_data_ptr<executorch::aten::Half>()[i]; | ||
| EXPECT_GE(val, 0.0); | ||
| EXPECT_LT(val, 1.0); | ||
| } | ||
| } | ||
|
|
||
| TEST_F(TensorPtrMakerTest, CreateRandTensorWithBFloatType) { | ||
| auto tensor = rand({4, 5}, executorch::aten::ScalarType::BFloat16); | ||
|
|
||
| EXPECT_EQ(tensor->dim(), 2); | ||
| EXPECT_EQ(tensor->size(0), 4); | ||
| EXPECT_EQ(tensor->size(1), 5); | ||
| EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::BFloat16); | ||
|
|
||
| for (auto i = 0; i < tensor->numel(); ++i) { | ||
| auto val = tensor->const_data_ptr<executorch::aten::BFloat16>()[i]; | ||
| EXPECT_GE(val, 0.0); | ||
| EXPECT_LT(val, 1.0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused. doesn't rand generate normally distributed numbers? the mean is set to 0 and stddev is 1, and the normal distribution can certainly include numbers 1 or more standard deviations away from the mean.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess what you described is PyTorch's
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot to mention that I brought this up because this test is flaking; val is sometimes 1.0
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
you're right: I misread earlier. however, the test is still flaking. see inline code comment |
||
| } | ||
| } | ||
|
|
||
| TEST_F(TensorPtrMakerTest, CreateRandnTensor) { | ||
| auto tensor = randn({100, 100}); | ||
|
|
||
|
|
||
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 think the problem is here -- there are presumably fp32 values that, while not 1.0f, convert to bfloat16 as 1.0 thanks to round-to-nearest-even.
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.
So, I think both rand_strided and randint_strided (the two functions that use a uniform distribution on a half-open interval) need special handling not to generate the excluded endpoint of their interval. PyTorch's implementation seems to suggest this is a bit of a pain: https://github.com/pytorch/pytorch/blob/dcac3c3e06556bc0e729dd1fa75f4f1e81caa356/aten/src/ATen/native/DistributionTemplates.h#L30
Technically, code sharing support landed today, so we could refactor update_to and update_from out of this header and share them...
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.
Hope #8340 should be enough to fix the flakiness.