Repository navigation
Expand file tree
/
Copy pathElementwiseRefMeta.h
More file actions
64 lines (51 loc) · 2.24 KB
/
Copy pathElementwiseRefMeta.h
File metadata and controls
64 lines (51 loc) · 2.24 KB
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
#pragma once
#include <optional>
#include <utility>
#include <ATen/core/Tensor.h>
#include <c10/core/Scalar.h>
#include <c10/util/FunctionRef.h>
// C++ versions of what Python FakeTensorMode runs for elementwise binary ops
// (the torch._refs implementation and the fake_impls fast path), for
// SymInt-aware Meta kernels that match Python fake tensor exactly.
namespace at::native {
enum class ELEMENTWISE_TYPE_PROMOTION_KIND { DEFAULT, INT_TO_FLOAT, ALWAYS_BOOL };
// utils.elementwise_dtypes: the (computation, result) dtypes.
std::pair<ScalarType, ScalarType> elementwise_dtypes(
const Tensor& a,
const Tensor& b,
ELEMENTWISE_TYPE_PROMOTION_KIND kind);
// alpha_fn is the body of refs.add / refs.sub between _maybe_broadcast and the
// prim: it runs the ref's checks given the computation dtype, and returns the
// alpha that prims.mul scales other by, if any.
Tensor binary_ref_meta(
const Tensor& self,
const Tensor& other,
ELEMENTWISE_TYPE_PROMOTION_KIND kind,
bool fake_devices,
c10::function_ref<std::optional<Scalar>(ScalarType)> alpha_fn = nullptr);
// utils.is_weakly_lesser_type(type(alpha), utils.dtype_to_type(dtype)), with the
// refs' error message.
void check_alpha_type(const Scalar& alpha, ScalarType dtype);
// _make_elementwise_binary_reference, with its Python scalar checks. name is
// the prim's name, used in the error messages.
Tensor elementwise_binary_ref_meta(
const char* name,
const Tensor& self,
const Tensor& other,
ELEMENTWISE_TYPE_PROMOTION_KIND kind,
bool fake_devices,
bool supports_lhs_python_scalar = true);
// other is a Scalar argument, which the ref sees as a Python number.
Tensor elementwise_binary_ref_meta(
const char* name,
const Tensor& self,
const Scalar& other,
ELEMENTWISE_TYPE_PROMOTION_KIND kind,
bool fake_devices,
bool supports_lhs_python_scalar = true);
// Whether Python fake sees the operand as symbolic: symbolic sizes, or a SymInt
// passed for a Tensor argument.
bool is_symbolic_operand(const Tensor& t);
void check_inplace_broadcast(c10::SymIntArrayRef self_shape, c10::SymIntArrayRef other_shape);
TORCH_API Tensor fast_binary_impl(const Tensor& self, const Tensor& other, ELEMENTWISE_TYPE_PROMOTION_KIND kind);
} // namespace at::native