1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/core/util/einsum_op_util.h"
17
18 #include <string>
19
20 #include "absl/strings/str_split.h"
21 #include "tensorflow/core/lib/core/errors.h"
22 #include "tensorflow/core/lib/core/status.h"
23 #include "tensorflow/core/lib/gtl/inlined_vector.h"
24
25 namespace tensorflow {
26
ParseEinsumEquation(const string & equation,gtl::InlinedVector<string,2> * input_subscripts,string * output_subscript)27 Status ParseEinsumEquation(const string& equation,
28 gtl::InlinedVector<string, 2>* input_subscripts,
29 string* output_subscript) {
30 gtl::InlinedVector<string, 2> inputs_and_output_subscripts =
31 absl::StrSplit(equation, "->");
32 if (inputs_and_output_subscripts.size() != 2) {
33 return errors::InvalidArgument(
34 "Expecting exactly one '->' in einsum equation: ", equation);
35 }
36 *output_subscript = std::move(inputs_and_output_subscripts[1]);
37 *input_subscripts =
38 absl::StrSplit(std::move(inputs_and_output_subscripts[0]), ',');
39 if (input_subscripts->size() != 1 && input_subscripts->size() != 2) {
40 return errors::InvalidArgument(
41 "Expecting 1 or 2 input subscripts in equation '", equation,
42 "' but got: ", input_subscripts->size());
43 }
44 return Status::OK();
45 }
46
47 } // namespace tensorflow
48