forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorFactories.cpp
80 lines (74 loc) · 2.73 KB
/
TensorFactories.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <ATen/ATen.h>
#include <ATen/quantized/Quantizer.h>
#include <c10/core/QScheme.h>
#include <c10/core/TensorOptions.h>
namespace at {
namespace native {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ empty ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// We explicitly pass in scale and zero_point because we don't have the infra
// ready to support quantizer in python frontend, once that is ready, we'll
// change to use quantizer
Tensor empty_affine_quantized(
IntArrayRef size,
const TensorOptions& options_,
double scale,
int64_t zero_point,
c10::optional<c10::MemoryFormat> optional_memory_format) {
TORCH_CHECK(
!(options_.has_memory_format() && optional_memory_format.has_value()),
"Cannot set memory_format both in TensorOptions and explicit argument; please delete "
"the redundant setter.");
auto options = options_.merge_in(TensorOptions().memory_format(optional_memory_format));
TORCH_CHECK(
options.has_dtype(),
"Must provide data type for Tensor creation functions.");
return new_qtensor(
size,
options,
make_per_tensor_affine_quantizer(
scale, zero_point, typeMetaToScalarType(options.dtype())));
}
Tensor empty_per_channel_affine_quantized_cpu(
IntArrayRef size,
const Tensor& scales,
const Tensor& zero_points,
int64_t axis,
const TensorOptions& options_,
c10::optional<c10::MemoryFormat> optional_memory_format) {
TORCH_CHECK(
!(options_.has_memory_format() && optional_memory_format.has_value()),
"Cannot set memory_format both in TensorOptions and explicit argument; please delete "
"the redundant setter.");
auto options = options_.merge_in(TensorOptions().memory_format(optional_memory_format));
TORCH_CHECK(
options.has_dtype(),
"Must provide data type for Tensor creation functions.");
TORCH_CHECK(
options.dtype() == kQInt8 || options.dtype() == kQUInt8,
"Supported data type for tensor creation is int8 or uint8");
return new_qtensor(
size,
options,
make_per_channel_affine_quantizer(
scales, zero_points, axis, typeMetaToScalarType(options.dtype())));
}
// Provide better error message if dtype is wrong
Tensor empty_affine_quantized_other_backends_stub(
IntArrayRef,
const TensorOptions&,
double,
int64_t,
c10::optional<c10::MemoryFormat>) {
TORCH_CHECK(false, "Creation of quantized tensor requires quantized dtype like torch.quint8");
}
Tensor empty_per_channel_affine_quantized_other_backends_stub(
IntArrayRef,
const Tensor&,
const Tensor&,
int64_t,
const TensorOptions&,
c10::optional<c10::MemoryFormat>) {
TORCH_CHECK(false, "Creation of quantized tensor requires quantized dtype like torch.quint8");
}
} // namespace native
} // namespace at