Skip to content

Commit e6852eb

Browse files
author
Zafar Takhirov
committed
Update on "[quant] Adding Scalar add/mul."
Note: This should be landed ONLY after #24259 Pull Request resolved: #24447 Differential Revision: [D16846006](https://our.internmc.facebook.com/intern/diff/D16846006)
2 parents 119c587 + 4c35ecd commit e6852eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+4408
-271
lines changed

.circleci/scripts/should_run_job.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# WARNING: Actually, this is a lie; we're currently also controlling
99
# the set of jobs to run via the Workflows filters in CircleCI config.
1010

11-
default_set = [
11+
default_set = set([
1212
# PyTorch CPU
1313
# Selected oldest Python 2 version to ensure Python 2 coverage
1414
'pytorch-linux-xenial-py2.7.9',
@@ -52,7 +52,15 @@
5252
'pytorch-short-perf-test-gpu',
5353
'pytorch-python-doc-push',
5454
'pytorch-cpp-doc-push',
55-
]
55+
])
56+
57+
# Collection of jobs that are *temporarily* excluded from running on PRs.
58+
# Use this if there is a long-running job breakage that we can't fix with a
59+
# single revert.
60+
skip_override = {
61+
# example entry:
62+
# 'pytorch-cpp-doc-push': "https://github.com/pytorch/pytorch/issues/<related issue>"
63+
}
5664

5765
# Takes in commit message to analyze via stdin
5866
#
@@ -65,7 +73,8 @@
6573
# Semantics in the presence of multiple tags:
6674
# - Let D be the set of default builds
6775
# - Let S be the set of explicitly specified builds
68-
# - Run S \/ D
76+
# - Let O be the set of temporarily skipped builds
77+
# - Run S \/ (D - O)
6978

7079
parser = argparse.ArgumentParser()
7180
parser.add_argument('build_environment')
@@ -88,10 +97,16 @@
8897
print("Accepting {} due to commit marker {}".format(args.build_environment, m.group(0)))
8998
sys.exit(0)
9099

91-
for spec in default_set:
100+
skip_override_set = set(skip_override.keys())
101+
should_run_set = default_set - skip_override_set
102+
for spec in should_run_set:
92103
if spec in args.build_environment:
93104
print("Accepting {} as part of default set".format(args.build_environment))
94105
sys.exit(0)
95106

96107
print("Rejecting {}".format(args.build_environment))
108+
for spec, issue in skip_override.items():
109+
if spec in args.build_environment:
110+
print("This job is temporarily excluded from running on PRs. Reason: {}".format(issue))
111+
break
97112
sys.exit(1)

.jenkins/caffe2/test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ if [[ "$BUILD_ENVIRONMENT" == *py3* ]]; then
106106
export LC_ALL=C.UTF-8
107107
export LANG=C.UTF-8
108108
fi
109+
110+
if [[ "$BUILD_ENVIRONMENT" == *py2* ]]; then
111+
pip install --user requests
112+
fi
113+
109114
pip install --user pytest-sugar
110115
"$PYTHON" \
111116
-m pytest \
@@ -137,3 +142,4 @@ if [[ "$BUILD_ENVIRONMENT" == *onnx* ]]; then
137142
fi
138143
"$ROOT_DIR/scripts/onnx/test.sh"
139144
fi
145+

.jenkins/pytorch/test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ if [[ "$BUILD_ENVIRONMENT" != *ppc64le* ]]; then
5656
pip_install --user mypy || true
5757
fi
5858

59+
if [[ $PYTHON_VERSION == "2" ]]; then
60+
pip_install --user requests
61+
fi
62+
5963
# faulthandler become built-in since 3.3
6064
if [[ ! $(python -c "import sys; print(int(sys.version_info >= (3, 3)))") == "1" ]]; then
6165
pip_install --user faulthandler

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ if (INTERN_BUILD_MOBILE AND NOT BUILD_CAFFE2_MOBILE)
234234
set(USE_DISTRIBUTED OFF)
235235
set(FEATURE_TORCH_MOBILE ON)
236236
set(NO_API ON)
237+
set(USE_FBGEMM OFF)
237238
endif()
238239

239240
if (BUILD_ATEN_ONLY)

android/pytorch_android/generate_test_torchscripts.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import torch
2-
from typing import List, Tuple, Dict
32

43
OUTPUT_DIR = "src/androidTest/assets/"
54

65
def scriptAndSave(module, fileName):
7-
print('-'*80)
6+
print('-' * 80)
87
script_module = torch.jit.script(module)
98
print(script_module.graph)
109
outputFileName = OUTPUT_DIR + fileName
1110
script_module.save(outputFileName)
1211
print("Saved to " + outputFileName)
13-
print('='*80)
12+
print('=' * 80)
1413

1514
class Test(torch.jit.ScriptModule):
1615
def __init__(self):

aten/src/ATen/NamedTensor.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ bool NamedTensorMeta::has_names() const {
1212
});
1313
}
1414

15+
thread_local bool NamesMode_enabled = true;
16+
17+
bool NamesMode::is_enabled() {
18+
return NamesMode_enabled;
19+
}
20+
21+
void NamesMode::set_enabled(bool enabled) {
22+
NamesMode_enabled = enabled;
23+
}
24+
1525
Tensor& internal_set_names_inplace(Tensor& tensor, optional<DimnameList> names) {
1626
impl::internal_set_names_inplace(tensor.unsafeGetTensorImpl(), names);
1727
return tensor;
@@ -22,8 +32,12 @@ Tensor& internal_set_names_inplace(Tensor& tensor, std::vector<Dimname>&& names,
2232
return tensor;
2333
}
2434

25-
std::vector<Dimname> FIXME_default_names(size_t len) {
26-
return { len, Dimname::wildcard() };
35+
DimnameList default_names(size_t len) {
36+
static std::vector<Dimname> all_unnamed(kMaxNamedTensorDim, Dimname::wildcard());
37+
TORCH_INTERNAL_ASSERT(
38+
len <= kMaxNamedTensorDim,
39+
"Only tensors with up to ", kMaxNamedTensorDim, " are supported.");
40+
return DimnameList(&all_unnamed.front(), len);
2741
}
2842

2943
namespace impl {
@@ -63,15 +77,22 @@ static void check_unique_names(DimnameList names) {
6377
}
6478

6579
static NamedTensorMeta* get_named_tensor_meta(TensorImpl* impl) {
80+
if (!NamesMode::is_enabled()) {
81+
return nullptr;
82+
}
6683
return static_cast<NamedTensorMeta*>(impl->named_tensor_meta());
6784
}
6885

6986
void check_valid_names(TensorImpl* impl, DimnameList names) {
7087
auto ndim = impl->dim();
88+
TORCH_CHECK(
89+
ndim <= kMaxNamedTensorDim,
90+
"Named tensors only support up to ", kMaxNamedTensorDim, " dims: "
91+
"Attempted to create a tensor with dim ", ndim, " with names ", names);
7192
TORCH_CHECK(ndim == names.size(),
7293
"Number of names (", names.size(), ") and "
7394
"number of dimensions in tensor (", ndim, ") ",
74-
"do not match.");
95+
"do not match. Attempted to create a tensor with names ", names);
7596
check_unique_names(names);
7697
}
7798

@@ -85,7 +106,7 @@ void internal_set_names_inplace(TensorImpl* impl, optional<DimnameList> names) {
85106
if (meta == nullptr) {
86107
impl->set_named_tensor_meta(torch::make_unique<NamedTensorMeta>(*names));
87108
} else {
88-
meta->set_names_(*names);
109+
meta->set_names(*names);
89110
}
90111
}
91112

@@ -97,11 +118,11 @@ void internal_set_names_inplace(TensorImpl* impl, std::vector<Dimname>&& names,
97118
if (meta == nullptr) {
98119
impl->set_named_tensor_meta(torch::make_unique<NamedTensorMeta>(names));
99120
} else {
100-
meta->set_names_(names);
121+
meta->set_names(names);
101122
}
102123
}
103124

104-
optional<DimnameList> get_names(TensorImpl* impl) {
125+
optional<DimnameList> get_opt_names(TensorImpl* impl) {
105126
const auto* meta = get_named_tensor_meta(impl);
106127
if (meta == nullptr) {
107128
return nullopt;
@@ -110,6 +131,14 @@ optional<DimnameList> get_names(TensorImpl* impl) {
110131
}
111132
}
112133

134+
DimnameList get_names(TensorImpl* impl) {
135+
auto maybe_names = get_opt_names(impl);
136+
if (maybe_names) {
137+
return *maybe_names;
138+
}
139+
return default_names(impl->dim());
140+
}
141+
113142
bool has_names(TensorImpl* impl) {
114143
const auto* named_tensor_meta = get_named_tensor_meta(impl);
115144
return named_tensor_meta != nullptr && named_tensor_meta->has_names();

aten/src/ATen/NamedTensor.h

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ struct CAFFE2_API NamedTensorMeta : public c10::NamedTensorMetaInterface {
3333
bool has_names() const;
3434
DimnameList names() const { return names_; }
3535

36-
void set_names_(DimnameList new_names) {
36+
void set_names(DimnameList new_names) {
3737
TORCH_INTERNAL_ASSERT(new_names.size() == names_.size());
3838
std::copy(new_names.begin(), new_names.end(), names_.begin());
3939
}
4040

41-
void set_names_(std::vector<Dimname>&& new_names) {
41+
void set_names(std::vector<Dimname>&& new_names) {
4242
TORCH_INTERNAL_ASSERT(new_names.size() == names_.size());
4343
names_ = std::move(new_names);
4444
}
@@ -47,23 +47,60 @@ struct CAFFE2_API NamedTensorMeta : public c10::NamedTensorMetaInterface {
4747
std::vector<Dimname> names_;
4848
};
4949

50+
// When NamesMode is disabled, then all operations ignore tensors' names fields.
51+
// Concretely speaking, all tensors are treated as having nullopt names.
52+
struct CAFFE2_API NamesMode {
53+
static bool is_enabled();
54+
static void set_enabled(bool enabled);
55+
};
56+
57+
// A RAII, thread local (!) guard that enables or disables names upon
58+
// construction, and sets it back to the original value upon destruction.
59+
struct CAFFE2_API NoNamesGuard {
60+
NoNamesGuard() : prev_mode(NamesMode::is_enabled()) {
61+
NamesMode::set_enabled(false);
62+
}
63+
~NoNamesGuard() {
64+
NamesMode::set_enabled(prev_mode);
65+
}
66+
private:
67+
bool prev_mode;
68+
};
69+
70+
5071
// Sets the names of `tensor` to be `names`.
5172
CAFFE2_API Tensor& internal_set_names_inplace(Tensor& tensor, optional<DimnameList> names);
5273
CAFFE2_API Tensor& internal_set_names_inplace(Tensor& tensor, std::vector<Dimname>&& names, bool validate_names);
5374

54-
// Everywhere this is used, it is possible to not instantiate the vector by doing
55-
// some more clever bookkeeping. This is important for performance.
56-
std::vector<Dimname> FIXME_default_names(size_t len);
75+
constexpr size_t kMaxNamedTensorDim = 64;
76+
77+
DimnameList default_names(size_t len);
5778

5879
namespace impl {
5980

6081
// Some helper functions on TensorImpl. Useful for working with names in TH.
6182
// XXX: Ideally these would exist as methods on TensorImpl
6283
CAFFE2_API void internal_set_names_inplace(TensorImpl* impl, optional<DimnameList> names);
6384
CAFFE2_API void internal_set_names_inplace(TensorImpl* impl, std::vector<Dimname>&& names, bool validate_names);
64-
CAFFE2_API optional<DimnameList> get_names(TensorImpl* impl);
85+
86+
// Returns true if the tensor's names exist and are not all 'None'.
87+
// Returns false if the tensor's names don't exist (were not allocated),
88+
// or if all names are 'None'.
89+
// We treat not-allocated-names the same as allocated names that are all 'None'.
6590
CAFFE2_API bool has_names(TensorImpl* impl);
6691

92+
// Returns the names of the tensor's dimensions.
93+
// Unnamed tensors are treated as having 'None' in all dimension; this method
94+
// would return a DimnameList of all 'None's for an unnamed tensor.
95+
CAFFE2_API DimnameList get_names(TensorImpl* impl);
96+
97+
// This is more of an implementation detail; one should use impl::get_names /
98+
// Tensor::names() whenever possible because it provides a cleaner API.
99+
// Returns the names of the tensor if they have been allocated; returns nullopt
100+
// instead if the haven't been. The names of a tensor are not allocated if a
101+
// tensor is constructed with names=None.
102+
CAFFE2_API optional<DimnameList> get_opt_names(TensorImpl* impl);
103+
67104

68105
} // namespace impl
69106

0 commit comments

Comments
 (0)