mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-14 20:57:59 +00:00
On Linux and Mac `int64_t` is an alias to either `long` (Linux) or `long long` (Mac)
Because of that, attempt to construct `c10::Scalar` from the other type will fail with `conversion from ‘long long int’ to ‘c10::Scalar’ is ambiguous`.
I.e. attempt to compile:
```cpp
int main() {
c10::Scalar s = 1L;
}
```
on MacOS failed with:
```
foo.cpp:3:15: error: conversion from 'long' to 'c10::Scalar' is ambiguous
c10::Scalar s = 1L;
^ ~~
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:59:7: note: candidate constructor
DEFINE_IMPLICIT_CTOR)
^
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:59:7: note: candidate constructor
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:59:7: note: candidate constructor
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:59:7: note: candidate constructor
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:59:7: note: candidate constructor
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:59:7: note: candidate constructor
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:59:7: note: candidate constructor
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:62:3: note: candidate constructor
Scalar(uint16_t vv) : Scalar(vv, true) {}
^
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:63:3: note: candidate constructor
Scalar(uint32_t vv) : Scalar(vv, true) {}
^
/Users/nshulga/git/pytorch/pytorch/torch/include/c10/core/Scalar.h:64:3: note: candidate constructor
Scalar(uint64_t vv) {
^
```
Prevent this by providing missing constructors when needed. Alas one can not use SFINAE, as template constructors on Scalar mess up a lot of implicit conversions, so I use `static_asserts` to detect early on if premise for constructing this class holds.
Add ScalarTest::LongsAndLongLongs that is essentially a compile time test
Discovered while trying to enable AOTI on MacOS
Pull Request resolved: https://github.com/pytorch/pytorch/pull/118149
Approved by: https://github.com/ezyang, https://github.com/albanD
ghstack dependencies: #118077, #118076
55 lines
1.7 KiB
C++
55 lines
1.7 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <c10/core/Scalar.h>
|
|
|
|
using namespace c10;
|
|
|
|
TEST(ScalarTest, UnsignedConstructor) {
|
|
uint16_t x = 0xFFFF;
|
|
uint32_t y = 0xFFFFFFFF;
|
|
uint64_t z0 = 0;
|
|
uint64_t z1 = 0x7FFFFFFFFFFFFFFF;
|
|
uint64_t z2 = 0xFFFFFFFFFFFFFFFF;
|
|
auto sx = Scalar(x);
|
|
auto sy = Scalar(y);
|
|
auto sz0 = Scalar(z0);
|
|
auto sz1 = Scalar(z1);
|
|
auto sz2 = Scalar(z2);
|
|
ASSERT_TRUE(sx.isIntegral(false));
|
|
ASSERT_TRUE(sy.isIntegral(false));
|
|
ASSERT_TRUE(sz0.isIntegral(false));
|
|
ASSERT_TRUE(sz1.isIntegral(false));
|
|
ASSERT_TRUE(sz2.isIntegral(false));
|
|
ASSERT_EQ(sx.type(), ScalarType::Long);
|
|
ASSERT_EQ(sy.type(), ScalarType::Long);
|
|
ASSERT_EQ(sz0.type(), ScalarType::Long);
|
|
ASSERT_EQ(sz1.type(), ScalarType::Long);
|
|
ASSERT_EQ(sz2.type(), ScalarType::UInt64);
|
|
ASSERT_EQ(sx.toUInt16(), x);
|
|
ASSERT_EQ(sx.toInt(), x);
|
|
ASSERT_EQ(sy.toUInt32(), y);
|
|
EXPECT_THROW(sy.toInt(), std::runtime_error); // overflows
|
|
ASSERT_EQ(sy.toLong(), y);
|
|
ASSERT_EQ(sz0.toUInt64(), z0);
|
|
ASSERT_EQ(sz0.toInt(), z0);
|
|
ASSERT_EQ(sz1.toUInt64(), z1);
|
|
EXPECT_THROW(sz1.toInt(), std::runtime_error); // overflows
|
|
ASSERT_EQ(sz1.toLong(), z1);
|
|
ASSERT_EQ(sz2.toUInt64(), z2);
|
|
EXPECT_THROW(sz2.toInt(), std::runtime_error); // overflows
|
|
EXPECT_THROW(sz2.toLong(), std::runtime_error); // overflows
|
|
}
|
|
|
|
TEST(ScalarTest, Equality) {
|
|
ASSERT_TRUE(Scalar(static_cast<uint64_t>(0xFFFFFFFFFFFFFFFF))
|
|
.equal(0xFFFFFFFFFFFFFFFF));
|
|
ASSERT_FALSE(Scalar(0).equal(0xFFFFFFFFFFFFFFFF));
|
|
// ensure that we don't incorrectly coerce bitrep
|
|
ASSERT_FALSE(Scalar(-1).equal(0xFFFFFFFFFFFFFFFF));
|
|
}
|
|
|
|
TEST(ScalarTest, LongsAndLongLongs) {
|
|
Scalar longOne = 1L;
|
|
Scalar longlongOne = 1LL;
|
|
ASSERT_EQ(longOne.toInt(), longlongOne.toInt());
|
|
}
|