Skip to content

Commit 1f44c8a

Browse files
committed
Update on "Delegate Python ~ (invert operator) to Tensor.bitwise_not()."
Delegate Python ~ (invert operator) to Tensor.bitwise_not(). Close #20024, Close #22246, Close #22262 Related #22324 gh-metadata: pytorch pytorch 22326 gh/xuhdev/8/head
2 parents 857ab08 + 9ca2457 commit 1f44c8a

File tree

11 files changed

+119
-16
lines changed

11 files changed

+119
-16
lines changed

test/test_mkldnn.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,24 @@ def test_zero_(self):
389389
x2.zero_().to_dense(),
390390
)
391391

392+
def test_is_mkldnn(self):
393+
x = torch.randn(1, dtype=torch.float32)
394+
self.assertFalse(x.is_mkldnn)
395+
self.assertTrue(x.to_mkldnn().is_mkldnn)
396+
397+
def test_is_mkldnn_jit(self):
398+
class EnsureMkldnn(torch.jit.ScriptModule):
399+
@torch.jit.script_method
400+
def forward(self, x):
401+
if not x.is_mkldnn:
402+
x = x.to_mkldnn()
403+
return x
404+
405+
m = EnsureMkldnn()
406+
x = torch.randn(1, dtype=torch.float32)
407+
self.assertTrue(m(x).is_mkldnn)
408+
self.assertTrue(m(x.to_mkldnn()).is_mkldnn)
409+
392410
def _test_imagenet_model(self, model):
393411
model = model.train(False).float()
394412
mkldnn_model = mkldnn_utils.to_mkldnn(copy.deepcopy(model))

test/test_throughput_benchmark.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ def linear_test(self, Module):
6565
num_iters=1000,
6666
)
6767

68-
print("Avg latency (ms): {}".format(stats.latency_avg_ms))
69-
print("Number of iterations: {}".format(stats.num_iters))
68+
print(stats)
7069

7170

7271
def test_script_module(self):

test/test_torch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ def test_bitwise_not(self):
17541754
torch.ByteTensor, torch.LongTensor, torch.IntTensor, torch.ShortTensor, torch.CharTensor):
17551755
if t == torch.BoolTensor:
17561756
a = torch.tensor([True, False])
1757-
expected_res = torch.BoolTensor([False, True])
1757+
expected_res = torch.tensor([False, True])
17581758
else:
17591759
a = torch.arange(127, dtype=t.dtype)
17601760
expected_res = res.type(t)

tools/autograd/gen_variable_factories.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
FUNCTION_TEMPLATE = CodeTemplate("""\
1212
inline at::Tensor ${name}(${formals}) {
1313
${pre_record_trace}
14-
at::Tensor tensor = at::${name}(${actuals});
14+
at::Tensor tensor = ([&]() {
15+
at::AutoNonVariableTypeMode non_var_type_mode(true);
16+
return at::${name}(${actuals});
17+
})();
1518
at::Tensor result =
1619
autograd::make_variable_consuming(std::move(tensor), /*requires_grad=*/${requires_grad});
1720
${post_record_trace}

tools/autograd/templates/variable_factories.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ namespace torch {
2222
#define TENSOR(T, S, _1) \
2323
inline at::Tensor tensor( \
2424
at::ArrayRef<T> values, const at::TensorOptions& options) { \
25-
at::Tensor result = \
26-
at::tensor(values, at::TensorOptions(options).is_variable(false)); \
25+
at::Tensor result = ([&]() { \
26+
at::AutoNonVariableTypeMode non_var_type_mode(true); \
27+
return at::tensor(values, at::TensorOptions(options).is_variable(false)); \
28+
})(); \
2729
return autograd::make_variable(result, options.requires_grad()); \
2830
} \
2931
inline at::Tensor tensor( \
@@ -62,8 +64,10 @@ inline at::Tensor from_blob(
6264
at::IntArrayRef strides,
6365
const Deleter& deleter,
6466
const at::TensorOptions& options = at::TensorOptions()) {
65-
at::Tensor tensor =
66-
at::from_blob(data, sizes, strides, deleter, options.is_variable(false));
67+
at::Tensor tensor = ([&]() {
68+
at::AutoNonVariableTypeMode non_var_type_mode(true);
69+
return at::from_blob(data, sizes, strides, deleter, options.is_variable(false));
70+
})();
6771
return autograd::make_variable(tensor, options.requires_grad());
6872
}
6973

@@ -96,8 +100,10 @@ inline at::Tensor from_blob(
96100
at::IntArrayRef sizes,
97101
const Deleter& deleter,
98102
const at::TensorOptions& options = at::TensorOptions()) {
99-
at::Tensor tensor =
100-
at::from_blob(data, sizes, deleter, options.is_variable(false));
103+
at::Tensor tensor = ([&]() {
104+
at::AutoNonVariableTypeMode non_var_type_mode(true);
105+
return at::from_blob(data, sizes, deleter, options.is_variable(false));
106+
})();
101107
return autograd::make_variable(tensor, options.requires_grad());
102108
}
103109

torch/csrc/autograd/python_variable.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,14 @@ PyObject *THPVariable_is_sparse(THPVariable *self)
420420
END_HANDLE_TH_ERRORS
421421
}
422422

423+
PyObject *THPVariable_is_mkldnn(THPVariable *self)
424+
{
425+
HANDLE_TH_ERRORS
426+
auto& self_ = self->cdata;
427+
return torch::autograd::utils::wrap(self_.is_mkldnn());
428+
END_HANDLE_TH_ERRORS
429+
}
430+
423431
PyObject *THPVariable_is_quantized(THPVariable *self)
424432
{
425433
HANDLE_TH_ERRORS
@@ -468,6 +476,7 @@ static struct PyGetSetDef THPVariable_properties[] = {
468476
{"shape", (getter)THPVariable_get_shape, nullptr, nullptr, nullptr},
469477
{"is_cuda", (getter)THPVariable_is_cuda, nullptr, nullptr, nullptr},
470478
{"is_sparse", (getter)THPVariable_is_sparse, nullptr, nullptr, nullptr},
479+
{"is_mkldnn", (getter)THPVariable_is_mkldnn, nullptr, nullptr, nullptr},
471480
{"is_quantized", (getter)THPVariable_is_quantized, nullptr, nullptr, nullptr},
472481
{"dtype", (getter)THPVariable_dtype, nullptr, nullptr, nullptr},
473482
{"layout", (getter)THPVariable_layout, nullptr, nullptr, nullptr},

torch/csrc/jit/register_prim_ops.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,14 @@ RegisterOperators reg(
500500
push(stack, a.is_cuda());
501501
return 0;
502502
}),
503+
Operator(
504+
"prim::is_mkldnn(Tensor a) -> bool",
505+
[](Stack& stack) {
506+
at::Tensor a;
507+
pop(stack, a);
508+
push(stack, a.is_mkldnn());
509+
return 0;
510+
}),
503511
Operator(
504512
"aten::cpu(Tensor(a) self) -> Tensor(a|b)",
505513
[](Stack& stack) {

torch/csrc/jit/script/sugared_value.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ std::shared_ptr<SugaredValue> SimpleValue::attr(
7373
"device",
7474
"shape",
7575
"is_cuda",
76+
"is_mkldnn",
7677
"requires_grad",
7778
};
7879
if (fields.count(field)) {

torch/csrc/utils/throughput_benchmark-inl.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ BenchmarkExecutionStats BenchmarkHelper<Input, Output, Model>::benchmark(
5050
int64_t initialized{0};
5151
int64_t finished{0};
5252
bool start{false};
53-
std::atomic<int64_t> num_forwards{0};
53+
std::atomic<int64_t> num_attempted_iters{0};
5454
std::vector<std::thread> callers;
5555

5656
for (auto thread_id = 0; thread_id < config.num_calling_threads;
@@ -71,7 +71,7 @@ BenchmarkExecutionStats BenchmarkHelper<Input, Output, Model>::benchmark(
7171
}
7272
}
7373
LOG(INFO) << "Starting forward thread " << thread_id;
74-
while (num_forwards.fetch_add(1) < config.num_iters) {
74+
while (num_attempted_iters.fetch_add(1) < config.num_iters) {
7575
runOnce(std::move(thread_inputs[thread_id][input_iters[thread_id]]));
7676
++input_iters[thread_id];
7777
}
@@ -115,9 +115,12 @@ BenchmarkExecutionStats BenchmarkHelper<Input, Output, Model>::benchmark(
115115
end_time - start_time)
116116
.count() /
117117
1000.0 / 1000.0;
118+
// We use config.num_iters instead of num_attempted_iters as it is
119+
// repsesatative of the real work done. Last attempted iteration on each
120+
// calling threads doesn't represent the real work (i.e. running the model)
118121
stats.latency_avg_ms =
119-
total_time_ms * config.num_calling_threads / num_forwards;
120-
stats.num_iters = num_forwards;
122+
total_time_ms * config.num_calling_threads / config.num_iters;
123+
stats.num_iters = config.num_iters;
121124

122125
for (auto& t : callers) {
123126
t.join();

0 commit comments

Comments
 (0)