mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
Add proper from_blob overloads (#13982)
Summary: There was an overload for `torch::from_blob` missing that allowed passing strides. ezyang soumith Pull Request resolved: https://github.com/pytorch/pytorch/pull/13982 Differential Revision: D13108089 Pulled By: goldsborough fbshipit-source-id: b87594ec0bf55b35d106b4438bc18b2ce9fc8f71
This commit is contained in:
committed by
Facebook Github Bot
parent
b30c803662
commit
fa73037233
+55
-10
@@ -72,7 +72,6 @@ TEST(TensorTest, ToTensorAndTensorAttributes) {
|
||||
REQUIRE_TENSOR_OPTIONS(at::kCPU, -1, at::kInt, at::kStrided);
|
||||
}
|
||||
|
||||
|
||||
// Not currently supported.
|
||||
// TEST(TensorTest, ToLayout) {
|
||||
// auto tensor = at::empty({3, 4});
|
||||
@@ -95,8 +94,10 @@ TEST(TensorTest, ToOptionsWithRequiresGrad) {
|
||||
ASSERT_TRUE(tensor.requires_grad());
|
||||
|
||||
// Throws if requires_grad is set in TensorOptions
|
||||
ASSERT_THROW(tensor.to(at::TensorOptions().requires_grad(true)), c10::Error);
|
||||
ASSERT_THROW(tensor.to(at::TensorOptions().requires_grad(false)), c10::Error);
|
||||
ASSERT_THROW(
|
||||
tensor.to(at::TensorOptions().requires_grad(true)), c10::Error);
|
||||
ASSERT_THROW(
|
||||
tensor.to(at::TensorOptions().requires_grad(false)), c10::Error);
|
||||
}
|
||||
{
|
||||
auto tensor = torch::empty({3, 4});
|
||||
@@ -107,8 +108,10 @@ TEST(TensorTest, ToOptionsWithRequiresGrad) {
|
||||
ASSERT_FALSE(tensor.requires_grad());
|
||||
|
||||
// Throws if requires_grad is set in TensorOptions
|
||||
ASSERT_THROW(tensor.to(at::TensorOptions().requires_grad(true)), c10::Error);
|
||||
ASSERT_THROW(tensor.to(at::TensorOptions().requires_grad(false)), c10::Error);
|
||||
ASSERT_THROW(
|
||||
tensor.to(at::TensorOptions().requires_grad(true)), c10::Error);
|
||||
ASSERT_THROW(
|
||||
tensor.to(at::TensorOptions().requires_grad(false)), c10::Error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,11 +228,53 @@ TEST(TensorTest, UsesOptionsThatAreSupplied) {
|
||||
}
|
||||
|
||||
TEST(TensorTest, FromBlob) {
|
||||
std::vector<int32_t> v = {1, 2, 3};
|
||||
auto tensor = torch::from_blob(v.data(), v.size(), torch::kInt32);
|
||||
std::vector<double> v = {1.0, 2.0, 3.0};
|
||||
auto tensor = torch::from_blob(
|
||||
v.data(), v.size(), torch::dtype(torch::kFloat64).requires_grad(true));
|
||||
ASSERT_TRUE(tensor.is_variable());
|
||||
ASSERT_TRUE(tensor.requires_grad());
|
||||
ASSERT_EQ(tensor.dtype(), torch::kFloat64);
|
||||
ASSERT_EQ(tensor.numel(), 3);
|
||||
ASSERT_EQ(tensor[0].item<int32_t>(), 1);
|
||||
ASSERT_EQ(tensor[1].item<int32_t>(), 2);
|
||||
ASSERT_EQ(tensor[2].item<int32_t>(), 3);
|
||||
ASSERT_EQ(tensor[0].item<double>(), 1);
|
||||
ASSERT_EQ(tensor[1].item<double>(), 2);
|
||||
ASSERT_EQ(tensor[2].item<double>(), 3);
|
||||
}
|
||||
|
||||
TEST(TensorTest, FromBlobUsesDeleter) {
|
||||
bool called = false;
|
||||
{
|
||||
std::vector<int32_t> v = {1, 2, 3};
|
||||
auto tensor = torch::from_blob(
|
||||
v.data(),
|
||||
v.size(),
|
||||
/*deleter=*/[&called](void* data) { called = true; },
|
||||
torch::kInt32);
|
||||
}
|
||||
ASSERT_TRUE(called);
|
||||
}
|
||||
|
||||
TEST(TensorTest, FromBlobWithStrides) {
|
||||
// clang-format off
|
||||
std::vector<int32_t> v = {
|
||||
1, 2, 3,
|
||||
4, 5, 6,
|
||||
7, 8, 9
|
||||
};
|
||||
// clang-format on
|
||||
auto tensor = torch::from_blob(
|
||||
v.data(),
|
||||
/*sizes=*/{3, 3},
|
||||
/*strides=*/{1, 3},
|
||||
torch::kInt32);
|
||||
ASSERT_TRUE(tensor.is_variable());
|
||||
ASSERT_EQ(tensor.dtype(), torch::kInt32);
|
||||
ASSERT_EQ(tensor.numel(), 9);
|
||||
const std::vector<int64_t> expected_strides = {1, 3};
|
||||
ASSERT_EQ(tensor.strides(), expected_strides);
|
||||
for (int64_t i = 0; i < tensor.size(0); ++i) {
|
||||
for (int64_t j = 0; j < tensor.size(1); ++j) {
|
||||
// NOTE: This is column major because the strides are swapped.
|
||||
EXPECT_EQ(tensor[i][j].item<int32_t>(), 1 + (j * tensor.size(1)) + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user