mirror of
https://github.com/pytorch/pytorch.git
synced 2026-11-20 09:04:25 +08:00
Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified (#29632)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/29632 This PR is BC-breaking in the following way: Previously, C++ `torch::tensor` with a floating-point literal with no suffix (e.g. `torch::tensor(1.1)`) or a (nested) braced-init-list of floating-point literals with no suffix (e.g. `torch::tensor({{1.1, 2.2}})` produces a tensor with dtype `at::kDouble`. After this PR, it produces a tensor with dtype `torch::get_default_dtype()`, matching Python `torch.tensor` behavior. Test Plan: Imported from OSS Differential Revision: D18465819 Pulled By: yf225 fbshipit-source-id: 6834fe50335c677bc3832f2a5e9cf8d1ede9f665
This commit is contained in:
committed by
Facebook Github Bot
parent
3fb9bbc99b
commit
2bcac59a30
@@ -92,16 +92,16 @@ void check_exact_values(
|
||||
assign_parameter(
|
||||
parameters,
|
||||
"0.weight",
|
||||
torch::tensor({-0.2109, -0.4976, -0.1413, -0.3420, -0.2524, 0.6976}));
|
||||
torch::tensor({-0.2109, -0.4976, -0.1413, -0.3420, -0.2524, 0.6976}, torch::kFloat64));
|
||||
assign_parameter(
|
||||
parameters, "0.bias", torch::tensor({-0.1085, -0.2979, 0.6892}));
|
||||
parameters, "0.bias", torch::tensor({-0.1085, -0.2979, 0.6892}, torch::kFloat64));
|
||||
assign_parameter(
|
||||
parameters, "2.weight", torch::tensor({-0.0508, -0.3941, -0.2843}));
|
||||
assign_parameter(parameters, "2.bias", torch::tensor({-0.0711}));
|
||||
parameters, "2.weight", torch::tensor({-0.0508, -0.3941, -0.2843}, torch::kFloat64));
|
||||
assign_parameter(parameters, "2.bias", torch::tensor({-0.0711}, torch::kFloat64));
|
||||
|
||||
auto optimizer = OptimizerClass(parameters.values(), options);
|
||||
torch::Tensor input =
|
||||
torch::tensor({0.1, 0.2, 0.3, 0.4, 0.5, 0.6}).reshape({3, 2});
|
||||
torch::tensor({0.1, 0.2, 0.3, 0.4, 0.5, 0.6}, torch::kFloat64).reshape({3, 2});
|
||||
|
||||
for (size_t i = 0; i < kIterations; ++i) {
|
||||
optimizer.zero_grad();
|
||||
@@ -116,8 +116,9 @@ void check_exact_values(
|
||||
expected_parameters.at(i / kSampleEvery).size() == parameters.size());
|
||||
for (size_t p = 0; p < parameters.size(); ++p) {
|
||||
ASSERT_TRUE(parameters[p]->defined());
|
||||
auto computed = parameters[p]->flatten();
|
||||
auto expected = expected_parameters.at(i / kSampleEvery).at(p);
|
||||
// Always compare using double dtype, regardless of the original dtype of the tensors
|
||||
auto computed = parameters[p]->flatten().to(torch::kFloat64);
|
||||
auto expected = expected_parameters.at(i / kSampleEvery).at(p).to(torch::kFloat64);
|
||||
if (!computed.allclose(expected, /*rtol=*/1e-3, /*atol=*/5e-4)) {
|
||||
std::cout << "Iteration " << i << ": " << computed
|
||||
<< " != " << expected << " (parameter " << p << ")"
|
||||
|
||||
Reference in New Issue
Block a user