Set requires grad in TensorMaker::make_tensor() (#148255)

Fixes #146419

Pull Request resolved: https://github.com/pytorch/pytorch/pull/148255
Approved by: https://github.com/soulitzer
This commit is contained in:
Irshad CC
2025-03-29 08:06:42 +00:00
committed by PyTorch MergeBot
parent b8ef642f04
commit f3c77b2458
2 changed files with 11 additions and 1 deletions

View File

@ -53,7 +53,9 @@ Tensor TensorMaker::make_tensor() {
tensor_impl->set_storage_offset(*storage_offset_);
}
return tensor;
tensor_impl->set_requires_grad(opts_.requires_grad());
return tensor;
}
std::size_t TensorMaker::computeStorageSize() const noexcept {

View File

@ -76,6 +76,14 @@ TEST(TensorTest, ToTensorAndTensorAttributes) {
REQUIRE_TENSOR_OPTIONS(at::kCPU, -1, at::kInt, at::kStrided);
}
TEST(TensorTest, TensorAttributes) {
std::vector<float> v = {1.0, 2.0, 3.0};
auto options = c10::requires_grad(true);
auto tensor = at::from_blob(v.data(), {3}, options);
REQUIRE_TENSOR_OPTIONS(at::kCPU, -1, at::kFloat, at::kStrided);
ASSERT_TRUE(tensor.requires_grad());
}
// Not currently supported.
// TEST(TensorTest, ToLayout) {
// auto tensor = at::empty({3, 4});