8000 Update on "eliminate generated_cpu_cpp and generated_cuda_cpp from Ba… · pytorch/pytorch@dd40e59 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd40e59

Browse files
author
Michael Andreas Dagitses
committed
Update on "eliminate generated_cpu_cpp and generated_cuda_cpp from Bazel"
These are now redundant with shared variables. Differential Revision: [D36492536](https://our.internmc.facebook.com/intern/diff/D36492536/) [ghstack-poisoned]
2 parents d6b82e5 + 65248ab commit dd40e59

File tree

15 files changed

+130
-25
lines changed

15 files changed

+130
-25
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
if: ${{ always() && steps.requirements.outcome == 'success' }}
101101
run: |
102102
set -eux
103-
python torch/testing/_check_kernel_launches.py |& tee "${GITHUB_WORKSPACE}"/cuda_kernel_launch_checks.txt
103+
python torch/testing/_internal/check_kernel_launches.py |& tee "${GITHUB_WORKSPACE}"/cuda_kernel_launch_checks.txt
104104
105105
workflow-checks:
106106
name: workflow-checks

aten/src/ATen/core/TensorBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class TORCH_API TensorBase {
221221
return impl_->sizes();
222222
}
223223
c10::SymIntArrayRef sym_sizes() const {
224-
return c10::SymIntArrayRef(reinterpret_cast<const SymInt*>(sizes().data()), sizes().size());
224+
return impl_->sym_sizes();
225225
}
226226
IntArrayRef strides() const {
227227
return impl_->strides();

c10/core/SymInt.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ namespace c10 {
77
std::shared_ptr<SymbolicIntNode> SymInt::toSymbolicIntNode() {
88
auto& st = getSymIntTable();
99
TORCH_CHECK(is_symbolic());
10-
return st.getNode(SymInt::SYM_TAG_MASK ^ static_cast<uint64_t>(data_));
10+
return st.getNode(static_cast<uint64_t>(data_) & ~MASK);
1111
}
1212

1313
c10::SymInt SymInt::toSymInt(std::shared_ptr<SymbolicIntNode> sin_sp) {
1414
auto& sit = getSymIntTable();
15-
auto data = sit.addNode(sin_sp) | SYM_TAG_MASK;
16-
return c10::SymInt(data);
15+
uint64_t idx = sit.addNode(sin_sp);
16+
TORCH_CHECK(idx < MAX_SYM_IDX, "SymbolicIntNode index overflow: ", idx);
17+
uint64_t data = idx | IS_SYM;
18+
return c10::SymInt(static_cast<int64_t>(data));
1719
}
1820
} // namespace c10

c10/core/SymInt.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class SymbolicIntNode;
2525
// SymInt will be extenteded to represent a union structure Union[int64_t,
2626
// SymbolicIntNode*] which will be implemented as a single packed int64_t field
2727
// named data_.
28-
//
29-
// data_ can be either a plain int64_t or (1 << 63 | `index`). `index` points to
30-
// SymbolicIntNode* that will be responsible for constructing an IR node for
31-
// a traced operation to represent it in LTC or Fx graphs.
3228
class C10_API SymInt {
3329
public:
3430
explicit SymInt(int64_t d) : data_(d){};
@@ -39,8 +35,7 @@ class C10_API SymInt {
3935
}
4036

4137
bool is_symbolic() const {
42-
return static_cast<uint64_t>(SYM_TAG_MASK) &
43-
static_cast<uint64_t>(this->data_);
38+
return (MASK & static_cast<uint64_t>(this->data_)) == IS_SYM;
4439
}
4540

4641
bool operator==(const SymInt& p2) const {
@@ -62,8 +57,31 @@ class C10_API SymInt {
6257
return data_;
6358
}
6459

60+
// Return whether the integer is representable as a SymInt.
61+
static bool check_range(int64_t i) {
62+
return i > MIN_INT;
63+
}
64+
6565
private:
66-
const static int64_t SYM_TAG_MASK = 1LL << 63;
66+
// Constraints on the internal representation:
67+
// - Should represent positive and negative ints
68+
// - No conversion necessary for operations on ints.
69+
// - We reserve some values to act as indices into our sym int table.
70+
//
71+
// So, the scheme is to reserve large negative numbers:
72+
// - 0b0.... means we are a positive int (following two's complement)
73+
// - 0b11... means we are a negative int (following two's complement)
74+
// - 0b10... means we are index into the sym table. This means that
75+
// [-2^63, -2^62-1] are not representable as ints.
76+
static constexpr uint64_t MASK = 1ULL << 63 | 1ULL << 62;
77+
static constexpr uint64_t IS_SYM = 1ULL << 63;
78+
// Since we use the top two bits to determine whether something is symbolic,
79+
// we cannot represent symbolic indices that are large enough to use those
80+
// bits. This will probably never happen.
81+
static constexpr uint64_t MAX_SYM_IDX = 1ULL << 62;
82+
// Since 0b10... is reserved for symbolic indices, any integers lower than
83+
// this value would collide with our representation.
84+
static constexpr int64_t MIN_INT = -1LL & ~(1ULL << 62);
6785
int64_t data_;
6886
};
6987

c10/core/SymIntTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace c10 {
44

5-
int64_t SymIntTable::addNode(std::shared_ptr<SymbolicIntNode> sin) {
5+
uint64_t SymIntTable::addNode(std::shared_ptr<SymbolicIntNode> sin) {
66
std::lock_guard<std::mutex> lock(mutex_);
77
auto index = nodes_.size();
88
nodes_.push_back(sin);

c10/core/SymbolicIntNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class C10_API SymbolicIntNode
2020

2121
class C10_API SymIntTable {
2222
public:
23-
int64_t addNode(std::shared_ptr<SymbolicIntNode> sin);
23+
uint64_t addNode(std::shared_ptr<SymbolicIntNode> sin);
2424
std::shared_ptr<SymbolicIntNode> getNode(size_t index);
2525

2626
private:

c10/core/TensorImpl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <c10/core/Backend.h>
44
#include <c10/core/InferenceMode.h>
5+
#include <c10/core/SymIntArrayRef.h>
56
#include <c10/core/WrapDimMinimal.h>
67
#include <c10/core/impl/LocalDispatchKeySet.h>
78
#include <c10/util/Optional.h>
@@ -371,6 +372,13 @@ IntArrayRef TensorImpl::sizes_custom() const {
371372
TORCH_CHECK(
372373
false, "Tensors of type ", tensorimpl_type_name(), " do not have sizes");
373374
}
375+
c10::SymIntArrayRef TensorImpl::sym_sizes_custom() const {
376+
TORCH_CHECK(
377+
false,
378+
"Tensors of type ",
379+
tensorimpl_type_name(),
380+
" do not have sym sizes");
381+
}
374382
IntArrayRef TensorImpl::strides_custom() const {
375383
TORCH_CHECK(
376384
false,

c10/core/TensorImpl.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,15 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
548548
return sizes_default();
549549
}
550550

551+
c10::SymIntArrayRef sym_sizes() const {
552+
if (C10_UNLIKELY(
553+
sizes_strides_policy_ >=
554+
static_cast<uint8_t>(SizesStridesPolicy::CustomSizes))) {
555+
return sym_sizes_custom();
556+
}
557+
return sym_sizes_default();
558+
}
559+
551560
/**
552561
* Return a reference to the strides of this tensor. This reference remains
553562
* valid as long as the tensor is live and not restrided.
@@ -655,6 +664,7 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
655664
virtual bool is_contiguous_custom(at::MemoryFormat memory_format) const;
656665
// sizes_strides_policy_ >= CustomSizes
657666
virtual IntArrayRef sizes_custom() const;
667+
virtual c10::SymIntArrayRef sym_sizes_custom() const;
658668
virtual int64_t dim_custom() const;
659669
virtual int64_t numel_custom() const;
660670

@@ -675,6 +685,11 @@ struct C10_API TensorImpl : public c10::intrusive_ptr_target {
675685
inline IntArrayRef sizes_default() const {
676686
return sizes_and_strides_.sizes_arrayref();
677687
}
688+
inline c10::SymIntArrayRef sym_sizes_default() const {
689+
return c10::SymIntArrayRef(
690+
reinterpret_cast<const c10::SymInt*>(sizes_and_strides_.sizes_data()),
691+
sizes_and_strides_.size());
692+
}
678693
inline int64_t dim_default() const {
679694
return sizes_and_strides_.size();
680695
}

c10/test/core/SymInt_test.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <c10/core/SymInt.h>
4+
#include <c10/core/SymbolicIntNode.h>
5+
6+
using namespace c10;
7+
8+
void check(int64_t value) {
9+
EXPECT_TRUE(SymInt::check_range(value));
10+
const auto i = SymInt(value);
11+
EXPECT_FALSE(i.is_symbolic());
12+
EXPECT_EQ(i.data(), value);
13+
}
14+
15+
TEST(SymIntTest, ConcreteInts) {
16+
check(INT64_MAX);
17+
check(0);
18+
check(-1);
19+
// This is 2^62, which is the most negative number we can support.
20+
check(-4611686018427387904LL);
21+
}
22+
23+
TEST(SymIntTest, AddNode) {
24+
auto n = std::make_shared<SymbolicIntNode>();
25+
auto i = n->toSymInt();
26+
EXPECT_TRUE(i.is_symbolic());
27+
}
28+
29+
TEST(SymIntTest, CheckRange) {
30+
EXPECT_FALSE(SymInt::check_range(INT64_MIN));
31+
}

caffe2/core/tensor.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,7 @@ class TORCH_API Tensor final {
430430
}
431431

432432
inline c10::SymIntArrayRef sym_sizes() const {
433-
auto sizes = impl_.get()->sizes();
434-
return c10::SymIntArrayRef(reinterpret_cast<const c10::SymInt*>(sizes.data()), sizes.size());
433+
return impl_->sym_sizes();
435434
}
436435

437436
inline int64_t size_from_dim(int k) const {

0 commit comments

Comments
 (0)
0