• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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/compiler/mlir/tfrt/saved_model/saved_model.h"
17 
18 #include "absl/strings/match.h"
19 #include "mlir/IR/Dialect.h"  // from @llvm-project
20 #include "mlir/Parser/Parser.h"  // from @llvm-project
21 #include "tensorflow/compiler/mlir/tensorflow/dialect_registration.h"
22 #include "tensorflow/compiler/mlir/tfrt/translate/import_model.h"
23 #include "tensorflow/core/lib/core/status_test_util.h"
24 #include "tensorflow/core/platform/resource_loader.h"
25 #include "tensorflow/core/platform/test.h"
26 
27 namespace tensorflow {
28 namespace {
29 
TEST(SavedModelTest,MapSignatures)30 TEST(SavedModelTest, MapSignatures) {
31   std::string saved_model_mlir_path = tensorflow::GetDataDependencyFilepath(
32       "tensorflow/compiler/mlir/tfrt/tests/saved_model/testdata/test.mlir");
33 
34   mlir::DialectRegistry registry;
35   mlir::RegisterAllTensorFlowDialects(registry);
36   mlir::MLIRContext context(registry);
37   auto module =
38       mlir::parseSourceFile<mlir::ModuleOp>(saved_model_mlir_path, &context);
39   ASSERT_TRUE(module);
40 
41   std::vector<std::string> inputs;
42   std::vector<std::pair<tensorflow::DataType, tensorflow::PartialTensorShape>>
43       in_specs;
44   std::vector<std::string> outputs;
45   std::vector<std::pair<tensorflow::DataType, tensorflow::PartialTensorShape>>
46       out_specs;
47   std::vector<mlir::Operation*> bound_inputs;
48   TF_ASSERT_OK(MapFunctionSignaturesFromTFSavedModelMLIR(
49       module.get(), [&](const TFRTSavedModelSignatureInfo& sig_info) {
50         // Only check the signature of "serving_default".
51         if (sig_info.func_name != "serving_default") return;
52 
53         transform(sig_info.input_names, std::back_inserter(inputs),
54                   [](llvm::StringRef x) { return x.str(); });
55         in_specs.assign(sig_info.input_specs.begin(),
56                         sig_info.input_specs.end());
57         transform(sig_info.output_names, std::back_inserter(outputs),
58                   [](llvm::StringRef x) { return x.str(); });
59         out_specs.assign(sig_info.output_specs.begin(),
60                          sig_info.output_specs.end());
61         bound_inputs.assign(sig_info.bound_inputs.begin(),
62                             sig_info.bound_inputs.end());
63       }));
64 
65   ASSERT_EQ(inputs.size(), 1);
66   EXPECT_EQ(inputs[0], "x");
67   ASSERT_EQ(outputs.size(), 1);
68   EXPECT_EQ(outputs[0], "r");
69 
70   ASSERT_EQ(in_specs.size(), 1);
71   ASSERT_EQ(in_specs[0].first, tensorflow::DT_INT32);
72   ASSERT_TRUE(in_specs[0].second.IsIdenticalTo(PartialTensorShape({1, 3})));
73 
74   ASSERT_EQ(out_specs.size(), 1);
75   ASSERT_EQ(out_specs[0].first, tensorflow::DT_INT32);
76   ASSERT_TRUE(out_specs[0].second.IsIdenticalTo(PartialTensorShape({1, 1})));
77 
78   ASSERT_EQ(bound_inputs.size(), 2);
79 
80   auto global_tensor =
81       llvm::cast<mlir::tf_saved_model::GlobalTensorOp>(bound_inputs[0]);
82   auto asset = llvm::cast<mlir::tf_saved_model::AssetOp>(bound_inputs[1]);
83 
84   EXPECT_EQ(global_tensor.sym_name(), "y");
85   EXPECT_EQ(asset.sym_name(), "z");
86 }
87 
TEST(SavedModelTest,CompileToBEF)88 TEST(SavedModelTest, CompileToBEF) {
89   std::string saved_model_mlir_path = tensorflow::GetDataDependencyFilepath(
90       "tensorflow/compiler/mlir/tfrt/tests/saved_model/testdata/test.mlir");
91 
92   mlir::DialectRegistry registry;
93   mlir::RegisterAllTensorFlowDialects(registry);
94   mlir::MLIRContext context(registry);
95   auto module =
96       mlir::parseSourceFile<mlir::ModuleOp>(saved_model_mlir_path, &context);
97   ASSERT_TRUE(module);
98 
99   tfrt::BefBuffer bef_buffer;
100   TfrtCompileOptions options;
101   TF_ASSERT_OK(ConvertTfMlirToBef(options, module.get(), &bef_buffer));
102 }
103 
104 // TODO(b/162442824): Add a SavedModel test that covers the error pass.
105 
106 }  // namespace
107 }  // namespace tensorflow
108