ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
einsum_op.h
Go to the documentation of this file.
1#ifndef ATEN_KERNELS_EINSUM_OP_H_
2#define ATEN_KERNELS_EINSUM_OP_H_
3
4#include <unordered_map>
5
6#include <ATen/core/tensor.h>
8
9namespace container {
10
12 bool conj_x = false;
13 bool conj_y = false;
14 float alpha = 1.0;
15 float beta = 0.0;
16 Tensor* out = nullptr;
17
18 EinsumOption(bool conj_x_ = false, bool conj_y_ = false, float alpha_ = 1.0, float beta_ = 0.0, Tensor* out_ = nullptr)
19 : conj_x(conj_x_), conj_y(conj_y_), alpha(alpha_), beta(beta_), out(out_) {}
20};
21
22namespace einsum_utils {
23struct BCast;
24
25// Dummy axis label used to denote an ellipsis in an input or output subscript.
26constexpr int kEllipsisLabel = -1;
27
28// Each dimension is categorized into exactly one of five types based on
29// whether its corresponding label is present in the input and/or the output
30// subscripts.
32 // Batch dimensions are those present in two inputs as well as the output.
33 // They are part of the batch dimensions during Tensor contraction. Such
34 // dimensions may be broadcasting dimensions (those mapping to ellipsis)
35 // or explicit batch dimensions corresponding to named axis labels.
37 kBatch = 1,
38 // Free dimensions are present in exactly one of the inputs, and also the
39 // output. These are non-contracted axes in the Tensor contraction.
40 kFree = 2,
41 // Contract dimensions are present in two inputs, but not the output. These
42 // dimensions are contracted in Tensor contraction.
44 // Reduce dimensions are present in exactly one input; and not in the output
45 // and are summed over prior to Tensor contraction.
47};
48
49// Parses and validates an einsum equation in explicit form.
51 const std::string& equation,
52 std::vector<std::string>& input_subscripts,
53 std::string& output_subscript);
54
55// Parses and validates the equation and the input shapes. Single character
56// labels are integerized, and we populate input and output label subscripts
57// and corresponding counts. Also create the mapping from (named) labels to
58// their EinsumDimensionType.
60 const std::string& equation,
61 std::vector<EinsumDimensionType>& label_types,
62 std::vector<std::vector<int>>& input_labels,
63 std::vector<int>& output_labels,
64 std::vector<std::vector<int>>& input_label_counts,
65 std::vector<int>& output_label_counts,
66 std::vector<bool>& input_has_ellipsis,
67 bool& output_has_ellipsis);
68
70 const std::vector<const Tensor*>& inputs,
71 std::vector<EinsumDimensionType>& label_types,
72 std::vector<std::vector<int>>& input_labels,
73 std::vector<int>& output_labels,
74 std::vector<std::vector<int>>& input_label_counts,
75 std::vector<int>& output_label_counts,
76 const std::vector<bool>& input_has_ellipsis,
77 const bool output_has_ellipsis,
78 std::unordered_map<int, int64_t>& label_to_dim_sizes);
79
80// This function records the mapping of a label to its corresponding dimension for a specific axis in the input tensor.
81// It also validates that the label and dimension mapping is consistent with previous recordings, ensuring that the
82// same label is not mapped to different dimensions along different axes.
84 const int label,
85 const int axis,
86 const Tensor& input,
87 std::unordered_map<int, int64_t>& label_to_dim_sizes);
88
89bool ReduceOperand(
90 const Tensor& input,
91 const std::vector<EinsumDimensionType>& label_types,
92 std::vector<int>& labels,
93 const std::vector<int>& label_counts,
94 std::vector<int>& free_labels,
95 int& swap_free_and_contract,
96 Tensor& output);
97
108 std::vector<Tensor>& inputs,
109 const std::vector<int>& swap_free_and_contract,
110 const EinsumOption& option,
111 Tensor& output);
112
113void ProcessOutput(
114 const Tensor& input,
115 const std::vector<einsum_utils::EinsumDimensionType>& label_types,
116 const std::vector<std::vector<int>>& free_labels,
117 std::unordered_map<int, int64_t>& label_to_dim_sizes,
118 const std::vector<int>& output_labels,
119 const std::vector<int>& output_label_counts,
120 Tensor& output);
121
122} // namespace einsum_utils
123
124namespace op {
125
126// TODO: implement this method this week!
127// piapia pat face
128
143template <typename... Tensors>
144typename std::enable_if<std::is_same<
145 typename std::common_type<Tensors...>::type, Tensor>::value, Tensor>::type
146 einsum_impl(const std::string& equation, const EinsumOption& option, const Tensors&... tensors)
147{
148 // Check the input dimension
149 constexpr int num_inputs = sizeof...(Tensors);
150 if (num_inputs > 2) {
151 throw std::invalid_argument("Einstein notation only support two or less tensors!");
152 }
153 const std::vector<const Tensor*> inputs{reinterpret_cast<const Tensor*>(&tensors)...};
154 // Init the input and output labels
155 std::vector<std::vector<int>> input_labels = {};
156 std::vector<int> output_labels = {};
157 std::vector<einsum_utils::EinsumDimensionType> label_types = {};
158 std::vector<std::vector<int>> input_label_counts = {};
159 std::vector<int> output_label_counts = {};
160 std::vector<bool> input_has_ellipsis = {};
161 bool output_has_ellipsis = {};
162
164 equation, label_types,
165 input_labels, output_labels,
166 input_label_counts, output_label_counts,
167 input_has_ellipsis, output_has_ellipsis);
168
169 if (input_labels.size() != num_inputs) {
170 throw std::runtime_error("The number of input tensors does not match the number of input labels!");
171 }
172
173 std::unordered_map<int, int64_t> label_to_dim_sizes = {};
174
176 inputs, label_types,
177 input_labels, output_labels,
178 input_label_counts, output_label_counts,
179 input_has_ellipsis, output_has_ellipsis,
180 label_to_dim_sizes);
181
182 std::vector<std::vector<int>> free_labels(num_inputs);
183 std::vector<int> swap_free_and_contract(num_inputs);
184 std::vector<Tensor> inputs_reduced(num_inputs, Tensor(DataType::DT_FLOAT, {}));
185
186 for (int ii = 0; ii < num_inputs; ++ii) {
188 *inputs[ii], label_types,
189 input_labels[ii], input_label_counts[ii],
190 free_labels[ii], swap_free_and_contract[ii],
191 inputs_reduced[ii]);
192 }
193
194 // After reduction, the inputs should be reshaped to Tensors suitable for
195 // contraction. If num_inputs is 1, the reduced input is simply forwarded to
196 // the output.
197 Tensor contraction_output_reshaped;
199 inputs_reduced, swap_free_and_contract,
200 option, contraction_output_reshaped);
201
203 // Copy the batch labels from the contraction output. Recover the batch
204 // shape, which may have been broadcasted.
206 contraction_output_reshaped, label_types,
207 free_labels, label_to_dim_sizes,
208 output_labels, output_label_counts,
209 output);
210
211 return std::move(output);
212}
213
214// Make the conj params only works for the matmul equations.
215inline static Tensor einsum(const std::string& equation, const Tensor& A) {
216 const EinsumOption& option = {};
217 return std::move(op::einsum_impl(equation, option, A));
218}
219
220inline static Tensor einsum(const std::string& equation, const Tensor& A, const Tensor& B, const EinsumOption& option = {}) {
221 return std::move(op::einsum_impl(equation, option, A, B));
222}
223
224} // namespace op
225} // namespace container
226
227#endif // ATEN_KERNELS_EINSUM_OP_H_
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
Definition output.h:13
bool ValidateEinsumEquation(const std::string &equation, std::vector< std::string > &input_subscripts, std::string &output_subscript)
Check the validation of the input equations.
Definition einsum_op.cpp:445
bool ReduceOperand(const Tensor &input, const std::vector< EinsumDimensionType > &label_types, std::vector< int > &labels, const std::vector< int > &label_counts, std::vector< int > &free_labels, int &swap_free_and_contract, Tensor &output)
Definition einsum_op.cpp:692
constexpr int kEllipsisLabel
Definition einsum_op.h:26
bool ParseEinsumEquation(const std::string &equation, std::vector< EinsumDimensionType > &label_types, std::vector< std::vector< int > > &input_labels, std::vector< int > &output_labels, std::vector< std::vector< int > > &input_label_counts, std::vector< int > &output_label_counts, std::vector< bool > &input_has_ellipsis, bool &output_has_ellipsis)
Definition einsum_op.cpp:510
bool ContractOperands(std::vector< Tensor > &inputs, const std::vector< int > &swap_free_and_contract, const EinsumOption &option, Tensor &output)
A function to perform contraction operation on multiple Tensors.
Definition einsum_op.cpp:928
EinsumDimensionType
Definition einsum_op.h:31
@ kFree
Definition einsum_op.h:40
@ kBatch
Definition einsum_op.h:37
@ kBroadcasting
Definition einsum_op.h:36
@ kReduce
Definition einsum_op.h:46
@ kContract
Definition einsum_op.h:43
bool ProcessDimensions(const std::vector< const Tensor * > &inputs, std::vector< EinsumDimensionType > &label_types, std::vector< std::vector< int > > &input_labels, std::vector< int > &output_labels, std::vector< std::vector< int > > &input_label_counts, std::vector< int > &output_label_counts, const std::vector< bool > &input_has_ellipsis, const bool output_has_ellipsis, std::unordered_map< int, int64_t > &label_to_dim_sizes)
Definition einsum_op.cpp:591
bool RecordLabelToDimension(const int label, const int axis, const Tensor &input, std::unordered_map< int, int64_t > &label_to_dim_sizes)
Definition einsum_op.cpp:575
void ProcessOutput(const Tensor &input, const std::vector< einsum_utils::EinsumDimensionType > &label_types, const std::vector< std::vector< int > > &free_labels, std::unordered_map< int, int64_t > &label_to_dim_sizes, const std::vector< int > &output_labels, const std::vector< int > &output_label_counts, Tensor &output)
Definition einsum_op.cpp:986
Definition einsum_op.h:124
std::enable_if< std::is_same< typenamestd::common_type< Tensors... >::type, Tensor >::value, Tensor >::type einsum_impl(const std::string &equation, const EinsumOption &option, const Tensors &... tensors)
Computes the Einstein summation convention on the given tensors based on the expression passed as a s...
Definition einsum_op.h:146
Definition tensor.cpp:8
@ DT_FLOAT
Definition tensor_enums.h:17
Definition einsum_op.h:11
Tensor * out
Definition einsum_op.h:16
bool conj_y
Definition einsum_op.h:13
float alpha
Definition einsum_op.h:14
EinsumOption(bool conj_x_=false, bool conj_y_=false, float alpha_=1.0, float beta_=0.0, Tensor *out_=nullptr)
Definition einsum_op.h:18
float beta
Definition einsum_op.h:15
bool conj_x
Definition einsum_op.h:12
Input_para input
Definition test_io_sys_var.cpp:58