From 551ff8822e7253ba0d5fd6a8b50f3a824db5ac97 Mon Sep 17 00:00:00 2001 From: jjyaoao <88936287+jjyaoao@users.noreply.github.com> Date: Thu, 30 Mar 2023 10:56:55 +0800 Subject: [PATCH] [Test Mv] remove infrt (#52270) --- paddle/CMakeLists.txt | 1 - paddle/infrt/CMakeLists.txt | 137 ---- paddle/infrt/api/.gitignore | 1 - paddle/infrt/api/CMakeLists.txt | 10 - paddle/infrt/api/infrt_api.cc | 335 --------- paddle/infrt/api/infrt_api.h | 75 -- paddle/infrt/api/infrt_api_test.cc.in | 217 ------ paddle/infrt/backends/CMakeLists.txt | 5 - paddle/infrt/backends/host/phi_allocator.h | 51 -- paddle/infrt/backends/host/phi_context.h | 47 -- paddle/infrt/backends/tensorrt/CMakeLists.txt | 14 - .../backends/tensorrt/plugin/CMakeLists.txt | 1 - .../backends/tensorrt/plugin/plugin_utils.h | 153 ---- .../tensorrt/plugin/pool_op_plugin.cu | 288 ------- .../backends/tensorrt/plugin/pool_op_plugin.h | 196 ----- .../backends/tensorrt/test_trt_engine.cc | 420 ----------- paddle/infrt/backends/tensorrt/trt_engine.cc | 386 ---------- paddle/infrt/backends/tensorrt/trt_engine.h | 126 ---- paddle/infrt/backends/tensorrt/trt_options.h | 94 --- paddle/infrt/backends/tensorrt/trt_utils.h | 149 ---- paddle/infrt/common/CMakeLists.txt | 17 - paddle/infrt/common/buffer.cc | 98 --- paddle/infrt/common/buffer.h | 296 -------- paddle/infrt/common/common.h | 61 -- paddle/infrt/common/dtype.cc | 50 -- paddle/infrt/common/dtype.def | 18 - paddle/infrt/common/dtype.h | 85 --- paddle/infrt/common/global.cc | 30 - paddle/infrt/common/global.h | 33 - paddle/infrt/common/macros.h | 52 -- paddle/infrt/common/memory.cc | 42 -- paddle/infrt/common/memory.h | 76 -- paddle/infrt/common/object.cc | 19 - paddle/infrt/common/object.h | 81 -- paddle/infrt/common/shared.cc | 15 - paddle/infrt/common/shared.h | 153 ---- paddle/infrt/common/string.cc | 128 ---- paddle/infrt/common/string.h | 84 --- paddle/infrt/common/target.cc | 118 --- paddle/infrt/common/target.h | 112 --- paddle/infrt/common/type.cc | 358 --------- paddle/infrt/common/type.h | 257 ------- paddle/infrt/dialect/CMakeLists.txt | 31 - paddle/infrt/dialect/dense_tensor.cc | 105 --- paddle/infrt/dialect/dense_tensor.h | 27 - paddle/infrt/dialect/dense_tensor.td | 216 ------ paddle/infrt/dialect/diagnostic_utils.cc | 56 -- paddle/infrt/dialect/diagnostic_utils.h | 41 - paddle/infrt/dialect/dialect.cc | 38 - paddle/infrt/dialect/infrt/CMakeLists.txt | 3 - .../infrt/dialect/infrt/common/CMakeLists.txt | 3 - paddle/infrt/dialect/infrt/common/types.cc | 101 --- paddle/infrt/dialect/infrt/common/types.h | 63 -- paddle/infrt/dialect/infrt/common/utils.cc | 28 - paddle/infrt/dialect/infrt/common/utils.h | 31 - paddle/infrt/dialect/infrt/ir/CMakeLists.txt | 14 - .../infrt/dialect/infrt/ir/basic_kernels.cc | 104 --- paddle/infrt/dialect/infrt/ir/basic_kernels.h | 21 - .../infrt/dialect/infrt/ir/basic_kernels.td | 93 --- paddle/infrt/dialect/infrt/ir/infrt_base.td | 129 ---- .../infrt/dialect/infrt/ir/infrt_dialect.cc | 223 ------ paddle/infrt/dialect/infrt/ir/infrt_dialect.h | 36 - paddle/infrt/dialect/infrt/ir/infrt_ops.td | 85 --- paddle/infrt/dialect/infrt/ir/test_kernels.cc | 164 ---- paddle/infrt/dialect/infrt/ir/test_kernels.h | 20 - paddle/infrt/dialect/infrt/ir/test_kernels.td | 65 -- .../infrt/dialect/infrt/pass/CMakeLists.txt | 5 - .../infrt/dialect/infrt/pass/infrt_op_fuse.td | 19 - .../dialect/infrt/pass/infrt_op_fuse_pass.cc | 61 -- .../dialect/infrt/pass/infrt_op_fuse_pass.h | 24 - .../infrt/pass/infrt_weights_unfold_pass.cc | 126 ---- .../infrt/pass/infrt_weights_unfold_pass.h | 25 - paddle/infrt/dialect/init_dialects.cc | 44 -- paddle/infrt/dialect/init_dialects.h | 21 - paddle/infrt/dialect/mlir_loader.cc | 79 -- paddle/infrt/dialect/mlir_loader.h | 31 - paddle/infrt/dialect/mlir_loader_test.cc | 58 -- paddle/infrt/dialect/opt.cc | 26 - paddle/infrt/dialect/pd/CMakeLists.txt | 3 - paddle/infrt/dialect/pd/common/CMakeLists.txt | 3 - paddle/infrt/dialect/pd/ir/CMakeLists.txt | 5 - paddle/infrt/dialect/pd/ir/pd_extra_ops.td | 18 - paddle/infrt/dialect/pd/ir/pd_op_base.td | 81 -- paddle/infrt/dialect/pd/ir/pd_ops.cc | 39 - paddle/infrt/dialect/pd/ir/pd_ops.h | 33 - paddle/infrt/dialect/pd/pass/CMakeLists.txt | 5 - paddle/infrt/dialect/pd/pass/pd_op_fuse.td | 95 --- .../infrt/dialect/pd/pass/pd_op_fuse_pass.cc | 45 -- .../infrt/dialect/pd/pass/pd_op_fuse_pass.h | 24 - paddle/infrt/dialect/phi/CMakeLists.txt | 11 - paddle/infrt/dialect/phi/data_type.cc | 125 ---- paddle/infrt/dialect/phi/data_type.h | 38 - paddle/infrt/dialect/phi/ir/CMakeLists.txt | 9 - paddle/infrt/dialect/phi/ir/infrt_phi_base.td | 44 -- .../infrt/dialect/phi/ir/infrt_phi_kernel.td | 37 - .../infrt/dialect/phi/ir/infrt_phi_tensor.cc | 36 - .../infrt/dialect/phi/ir/infrt_phi_tensor.h | 38 - .../infrt/dialect/phi/ir/infrt_phi_tensor.td | 147 ---- paddle/infrt/dialect/phi/ir/phi_base.cc | 48 -- paddle/infrt/dialect/phi/ir/phi_base.h | 47 -- paddle/infrt/dialect/phi/ir/phi_kernels.cc | 44 -- paddle/infrt/dialect/phi/ir/phi_kernels.h | 40 - paddle/infrt/dialect/phi/pass/CMakeLists.txt | 9 - .../infrt/dialect/phi/pass/kernel_op_desc.cc | 114 --- .../infrt/dialect/phi/pass/kernel_op_desc.h | 37 - .../dialect/phi/pass/kernel_op_desc_test.cc | 34 - .../dialect/phi/pass/phi_op_convert_pass.cc | 359 --------- .../dialect/phi/pass/phi_op_convert_pass.h | 27 - .../dialect/phi/pass/proto_arg_map_context.cc | 96 --- .../dialect/phi/pass/proto_arg_map_context.h | 62 -- paddle/infrt/dialect/phi/phi_exec.cc | 58 -- paddle/infrt/dialect/print_ir.cc | 132 ---- paddle/infrt/dialect/tensor_shape.cc | 69 -- paddle/infrt/dialect/tensor_shape.h | 40 - paddle/infrt/dialect/tensor_shape.td | 49 -- paddle/infrt/dialect/tensor_shape_base.td | 36 - paddle/infrt/dialect/tensorrt/CMakeLists.txt | 16 - paddle/infrt/dialect/tensorrt/convert.h | 371 --------- .../infrt/dialect/tensorrt/pd_lower_to_trt.td | 56 -- .../dialect/tensorrt/trt_dialect_types.h | 31 - paddle/infrt/dialect/tensorrt/trt_exec.cc | 93 --- .../dialect/tensorrt/trt_graph_fuse_pass.cc | 191 ----- .../dialect/tensorrt/trt_graph_fuse_pass.h | 65 -- .../dialect/tensorrt/trt_graph_split_pass.cc | 54 -- .../dialect/tensorrt/trt_graph_split_pass.h | 62 -- paddle/infrt/dialect/tensorrt/trt_op_base.td | 29 - .../dialect/tensorrt/trt_op_converter_pass.cc | 268 ------- .../dialect/tensorrt/trt_op_converter_pass.h | 62 -- .../dialect/tensorrt/trt_op_teller_pass.cc | 68 -- .../dialect/tensorrt/trt_op_teller_pass.h | 64 -- paddle/infrt/dialect/tensorrt/trt_ops.cc | 76 -- paddle/infrt/dialect/tensorrt/trt_ops.h | 52 -- paddle/infrt/dialect/tensorrt/trt_ops.td | 223 ------ .../dialect/tensorrt/trt_type_convert_pass.cc | 184 ----- .../dialect/tensorrt/trt_type_convert_pass.h | 25 - paddle/infrt/external_kernels/CMakeLists.txt | 15 - paddle/infrt/external_kernels/basic.mlir | 21 - .../infrt/external_kernels/basic_kernels.cc | 59 -- paddle/infrt/external_kernels/fc.mlir | 43 -- paddle/infrt/external_kernels/paddle.mlir | 50 -- paddle/infrt/gtest_main.cc | 23 - paddle/infrt/host_context/CMakeLists.txt | 35 - paddle/infrt/host_context/core_runtime.cc | 94 --- paddle/infrt/host_context/core_runtime.h | 88 --- .../infrt/host_context/core_runtime_test.cc | 96 --- paddle/infrt/host_context/function.cc | 19 - paddle/infrt/host_context/function.h | 62 -- paddle/infrt/host_context/kernel_frame.cc | 54 -- paddle/infrt/host_context/kernel_frame.h | 198 ----- paddle/infrt/host_context/kernel_registry.cc | 80 -- paddle/infrt/host_context/kernel_registry.h | 75 -- .../host_context/kernel_registry_test.cc | 49 -- paddle/infrt/host_context/kernel_utils.cc | 19 - paddle/infrt/host_context/kernel_utils.h | 354 --------- .../infrt/host_context/kernel_utils_test.cc | 111 --- paddle/infrt/host_context/mlir_exec.cc | 115 --- .../host_context/mlir_function_executable.cc | 138 ---- .../host_context/mlir_function_executable.h | 80 -- .../host_context/mlir_program_executor.cc | 19 - .../host_context/mlir_program_executor.h | 79 -- .../infrt/host_context/mlir_tests/basic.mlir | 30 - .../host_context/mlir_tests/dense_tensor.mlir | 9 - .../infrt/host_context/mlir_tests/shape.mlir | 7 - .../host_context/mlir_to_runtime_translate.cc | 707 ------------------ .../host_context/mlir_to_runtime_translate.h | 109 --- .../mlir_to_runtime_translate_test.cc | 164 ---- paddle/infrt/host_context/op_executable.cc | 165 ---- paddle/infrt/host_context/op_executable.h | 93 --- .../infrt/host_context/op_executable_test.cc | 56 -- paddle/infrt/host_context/paddle_mlir.cc | 523 ------------- paddle/infrt/host_context/paddle_mlir.h | 119 --- .../host_context/paddle_mlir_converter.cc | 58 -- paddle/infrt/host_context/symbol_table.cc | 82 -- paddle/infrt/host_context/symbol_table.h | 64 -- paddle/infrt/host_context/value.cc | 73 -- paddle/infrt/host_context/value.h | 257 ------- paddle/infrt/host_context/value_test.cc | 44 -- paddle/infrt/kernel/CMakeLists.txt | 14 - paddle/infrt/kernel/basic_kernels.cc | 87 --- paddle/infrt/kernel/basic_kernels.h | 38 - paddle/infrt/kernel/control_flow_kernels.cc | 44 -- paddle/infrt/kernel/control_flow_kernels.h | 31 - paddle/infrt/kernel/phi/CMakeLists.txt | 39 - paddle/infrt/kernel/phi/context_kernels.cc | 43 -- paddle/infrt/kernel/phi/context_kernels.h | 34 - .../infrt/kernel/phi/dense_tensor_kernels.cc | 395 ---------- .../infrt/kernel/phi/dense_tensor_kernels.h | 89 --- .../infershaped/infershape_launchers_test.cc | 99 --- .../infershaped_kernel_launcher.cc | 68 -- .../infershaped/infershaped_kernel_launcher.h | 50 -- .../infershaped_kernel_launchers.h | 25 - .../phi/infershaped/infershaped_utils.h | 79 -- .../phi/infershaped/phi_kernel_launcher.h | 74 -- paddle/infrt/kernel/phi/registry.cc | 85 --- paddle/infrt/kernel/phi/registry.h | 35 - paddle/infrt/kernel/tensor_kernels.cc | 168 ----- paddle/infrt/kernel/tensor_kernels.h | 29 - paddle/infrt/kernel/tensor_shape_kernels.cc | 40 - paddle/infrt/kernel/tensor_shape_kernels.h | 31 - paddle/infrt/kernel/tensorrt/CMakeLists.txt | 10 - paddle/infrt/kernel/tensorrt/registry.cc | 34 - paddle/infrt/kernel/tensorrt/registry.h | 35 - paddle/infrt/kernel/tensorrt/trt_helper.h | 78 -- paddle/infrt/kernel/tensorrt/trt_kernels.cc | 201 ----- paddle/infrt/kernel/tensorrt/trt_kernels.h | 48 -- paddle/infrt/kernel/tensorrt/trt_layers.h | 323 -------- paddle/infrt/kernel/test_kernels.cc | 202 ----- paddle/infrt/kernel/test_kernels.h | 35 - paddle/infrt/paddle/CMakeLists.txt | 20 - paddle/infrt/paddle/cpp/CMakeLists.txt | 11 - paddle/infrt/paddle/cpp/desc_api.h | 233 ------ paddle/infrt/paddle/framework.proto | 213 ------ paddle/infrt/paddle/model_parser.cc | 269 ------- paddle/infrt/paddle/model_parser.h | 68 -- paddle/infrt/paddle/pb/CMakeLists.txt | 13 - paddle/infrt/paddle/pb/block_desc.cc | 47 -- paddle/infrt/paddle/pb/block_desc.h | 81 -- paddle/infrt/paddle/pb/op_desc.cc | 143 ---- paddle/infrt/paddle/pb/op_desc.h | 202 ----- paddle/infrt/paddle/pb/program_desc.cc | 39 - paddle/infrt/paddle/pb/program_desc.h | 65 -- paddle/infrt/paddle/pb/var_desc.cc | 371 --------- paddle/infrt/paddle/pb/var_desc.h | 128 ---- paddle/infrt/paddle/scope.cc | 44 -- paddle/infrt/paddle/scope.h | 67 -- paddle/infrt/paddle/tensor.cc | 19 - paddle/infrt/paddle/tensor.h | 107 --- paddle/infrt/support/CMakeLists.txt | 1 - paddle/infrt/support/type_traits.h | 148 ---- paddle/infrt/support/variant.h | 219 ------ paddle/infrt/tensor/CMakeLists.txt | 23 - paddle/infrt/tensor/dense_host_tensor.cc | 95 --- paddle/infrt/tensor/dense_host_tensor.h | 99 --- paddle/infrt/tensor/dense_tensor_view.cc | 17 - paddle/infrt/tensor/dense_tensor_view.h | 64 -- paddle/infrt/tensor/phi/CMakeLists.txt | 1 - paddle/infrt/tensor/phi/tensor_map.cc | 48 -- paddle/infrt/tensor/phi/tensor_map.h | 39 - paddle/infrt/tensor/tensor_map.cc | 95 --- paddle/infrt/tensor/tensor_map.h | 29 - paddle/infrt/tensor/tensor_metadata.cc | 30 - paddle/infrt/tensor/tensor_metadata.h | 58 -- paddle/infrt/tensor/tensor_shape.cc | 98 --- paddle/infrt/tensor/tensor_shape.h | 87 --- .../efficientnet-b4/net/efficientnet.py | 326 -------- .../tests/models/efficientnet-b4/net/utils.py | 424 ----------- paddle/infrt/tests/models/linear.py | 82 -- 247 files changed, 22454 deletions(-) delete mode 100644 paddle/infrt/CMakeLists.txt delete mode 100644 paddle/infrt/api/.gitignore delete mode 100644 paddle/infrt/api/CMakeLists.txt delete mode 100644 paddle/infrt/api/infrt_api.cc delete mode 100644 paddle/infrt/api/infrt_api.h delete mode 100644 paddle/infrt/api/infrt_api_test.cc.in delete mode 100644 paddle/infrt/backends/CMakeLists.txt delete mode 100644 paddle/infrt/backends/host/phi_allocator.h delete mode 100644 paddle/infrt/backends/host/phi_context.h delete mode 100644 paddle/infrt/backends/tensorrt/CMakeLists.txt delete mode 100644 paddle/infrt/backends/tensorrt/plugin/CMakeLists.txt delete mode 100644 paddle/infrt/backends/tensorrt/plugin/plugin_utils.h delete mode 100644 paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.cu delete mode 100644 paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.h delete mode 100644 paddle/infrt/backends/tensorrt/test_trt_engine.cc delete mode 100644 paddle/infrt/backends/tensorrt/trt_engine.cc delete mode 100644 paddle/infrt/backends/tensorrt/trt_engine.h delete mode 100644 paddle/infrt/backends/tensorrt/trt_options.h delete mode 100644 paddle/infrt/backends/tensorrt/trt_utils.h delete mode 100644 paddle/infrt/common/CMakeLists.txt delete mode 100644 paddle/infrt/common/buffer.cc delete mode 100644 paddle/infrt/common/buffer.h delete mode 100644 paddle/infrt/common/common.h delete mode 100644 paddle/infrt/common/dtype.cc delete mode 100644 paddle/infrt/common/dtype.def delete mode 100644 paddle/infrt/common/dtype.h delete mode 100644 paddle/infrt/common/global.cc delete mode 100644 paddle/infrt/common/global.h delete mode 100644 paddle/infrt/common/macros.h delete mode 100644 paddle/infrt/common/memory.cc delete mode 100644 paddle/infrt/common/memory.h delete mode 100644 paddle/infrt/common/object.cc delete mode 100644 paddle/infrt/common/object.h delete mode 100644 paddle/infrt/common/shared.cc delete mode 100644 paddle/infrt/common/shared.h delete mode 100644 paddle/infrt/common/string.cc delete mode 100644 paddle/infrt/common/string.h delete mode 100644 paddle/infrt/common/target.cc delete mode 100644 paddle/infrt/common/target.h delete mode 100644 paddle/infrt/common/type.cc delete mode 100644 paddle/infrt/common/type.h delete mode 100644 paddle/infrt/dialect/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/dense_tensor.cc delete mode 100644 paddle/infrt/dialect/dense_tensor.h delete mode 100644 paddle/infrt/dialect/dense_tensor.td delete mode 100644 paddle/infrt/dialect/diagnostic_utils.cc delete mode 100644 paddle/infrt/dialect/diagnostic_utils.h delete mode 100644 paddle/infrt/dialect/dialect.cc delete mode 100644 paddle/infrt/dialect/infrt/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/infrt/common/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/infrt/common/types.cc delete mode 100644 paddle/infrt/dialect/infrt/common/types.h delete mode 100644 paddle/infrt/dialect/infrt/common/utils.cc delete mode 100644 paddle/infrt/dialect/infrt/common/utils.h delete mode 100644 paddle/infrt/dialect/infrt/ir/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/infrt/ir/basic_kernels.cc delete mode 100644 paddle/infrt/dialect/infrt/ir/basic_kernels.h delete mode 100644 paddle/infrt/dialect/infrt/ir/basic_kernels.td delete mode 100644 paddle/infrt/dialect/infrt/ir/infrt_base.td delete mode 100644 paddle/infrt/dialect/infrt/ir/infrt_dialect.cc delete mode 100644 paddle/infrt/dialect/infrt/ir/infrt_dialect.h delete mode 100644 paddle/infrt/dialect/infrt/ir/infrt_ops.td delete mode 100644 paddle/infrt/dialect/infrt/ir/test_kernels.cc delete mode 100644 paddle/infrt/dialect/infrt/ir/test_kernels.h delete mode 100644 paddle/infrt/dialect/infrt/ir/test_kernels.td delete mode 100644 paddle/infrt/dialect/infrt/pass/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td delete mode 100644 paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc delete mode 100644 paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h delete mode 100644 paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.cc delete mode 100644 paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.h delete mode 100644 paddle/infrt/dialect/init_dialects.cc delete mode 100644 paddle/infrt/dialect/init_dialects.h delete mode 100644 paddle/infrt/dialect/mlir_loader.cc delete mode 100644 paddle/infrt/dialect/mlir_loader.h delete mode 100644 paddle/infrt/dialect/mlir_loader_test.cc delete mode 100644 paddle/infrt/dialect/opt.cc delete mode 100644 paddle/infrt/dialect/pd/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/pd/common/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/pd/ir/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/pd/ir/pd_extra_ops.td delete mode 100644 paddle/infrt/dialect/pd/ir/pd_op_base.td delete mode 100644 paddle/infrt/dialect/pd/ir/pd_ops.cc delete mode 100644 paddle/infrt/dialect/pd/ir/pd_ops.h delete mode 100644 paddle/infrt/dialect/pd/pass/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/pd/pass/pd_op_fuse.td delete mode 100644 paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.cc delete mode 100644 paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.h delete mode 100644 paddle/infrt/dialect/phi/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/phi/data_type.cc delete mode 100644 paddle/infrt/dialect/phi/data_type.h delete mode 100644 paddle/infrt/dialect/phi/ir/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/phi/ir/infrt_phi_base.td delete mode 100644 paddle/infrt/dialect/phi/ir/infrt_phi_kernel.td delete mode 100644 paddle/infrt/dialect/phi/ir/infrt_phi_tensor.cc delete mode 100644 paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h delete mode 100644 paddle/infrt/dialect/phi/ir/infrt_phi_tensor.td delete mode 100644 paddle/infrt/dialect/phi/ir/phi_base.cc delete mode 100644 paddle/infrt/dialect/phi/ir/phi_base.h delete mode 100644 paddle/infrt/dialect/phi/ir/phi_kernels.cc delete mode 100644 paddle/infrt/dialect/phi/ir/phi_kernels.h delete mode 100644 paddle/infrt/dialect/phi/pass/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/phi/pass/kernel_op_desc.cc delete mode 100644 paddle/infrt/dialect/phi/pass/kernel_op_desc.h delete mode 100644 paddle/infrt/dialect/phi/pass/kernel_op_desc_test.cc delete mode 100644 paddle/infrt/dialect/phi/pass/phi_op_convert_pass.cc delete mode 100644 paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h delete mode 100644 paddle/infrt/dialect/phi/pass/proto_arg_map_context.cc delete mode 100644 paddle/infrt/dialect/phi/pass/proto_arg_map_context.h delete mode 100644 paddle/infrt/dialect/phi/phi_exec.cc delete mode 100644 paddle/infrt/dialect/print_ir.cc delete mode 100644 paddle/infrt/dialect/tensor_shape.cc delete mode 100644 paddle/infrt/dialect/tensor_shape.h delete mode 100644 paddle/infrt/dialect/tensor_shape.td delete mode 100644 paddle/infrt/dialect/tensor_shape_base.td delete mode 100755 paddle/infrt/dialect/tensorrt/CMakeLists.txt delete mode 100644 paddle/infrt/dialect/tensorrt/convert.h delete mode 100644 paddle/infrt/dialect/tensorrt/pd_lower_to_trt.td delete mode 100644 paddle/infrt/dialect/tensorrt/trt_dialect_types.h delete mode 100644 paddle/infrt/dialect/tensorrt/trt_exec.cc delete mode 100644 paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.cc delete mode 100644 paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.h delete mode 100644 paddle/infrt/dialect/tensorrt/trt_graph_split_pass.cc delete mode 100644 paddle/infrt/dialect/tensorrt/trt_graph_split_pass.h delete mode 100755 paddle/infrt/dialect/tensorrt/trt_op_base.td delete mode 100644 paddle/infrt/dialect/tensorrt/trt_op_converter_pass.cc delete mode 100644 paddle/infrt/dialect/tensorrt/trt_op_converter_pass.h delete mode 100644 paddle/infrt/dialect/tensorrt/trt_op_teller_pass.cc delete mode 100644 paddle/infrt/dialect/tensorrt/trt_op_teller_pass.h delete mode 100644 paddle/infrt/dialect/tensorrt/trt_ops.cc delete mode 100644 paddle/infrt/dialect/tensorrt/trt_ops.h delete mode 100755 paddle/infrt/dialect/tensorrt/trt_ops.td delete mode 100644 paddle/infrt/dialect/tensorrt/trt_type_convert_pass.cc delete mode 100644 paddle/infrt/dialect/tensorrt/trt_type_convert_pass.h delete mode 100644 paddle/infrt/external_kernels/CMakeLists.txt delete mode 100644 paddle/infrt/external_kernels/basic.mlir delete mode 100644 paddle/infrt/external_kernels/basic_kernels.cc delete mode 100644 paddle/infrt/external_kernels/fc.mlir delete mode 100644 paddle/infrt/external_kernels/paddle.mlir delete mode 100644 paddle/infrt/gtest_main.cc delete mode 100644 paddle/infrt/host_context/CMakeLists.txt delete mode 100644 paddle/infrt/host_context/core_runtime.cc delete mode 100644 paddle/infrt/host_context/core_runtime.h delete mode 100644 paddle/infrt/host_context/core_runtime_test.cc delete mode 100644 paddle/infrt/host_context/function.cc delete mode 100644 paddle/infrt/host_context/function.h delete mode 100644 paddle/infrt/host_context/kernel_frame.cc delete mode 100644 paddle/infrt/host_context/kernel_frame.h delete mode 100644 paddle/infrt/host_context/kernel_registry.cc delete mode 100644 paddle/infrt/host_context/kernel_registry.h delete mode 100644 paddle/infrt/host_context/kernel_registry_test.cc delete mode 100644 paddle/infrt/host_context/kernel_utils.cc delete mode 100644 paddle/infrt/host_context/kernel_utils.h delete mode 100644 paddle/infrt/host_context/kernel_utils_test.cc delete mode 100644 paddle/infrt/host_context/mlir_exec.cc delete mode 100644 paddle/infrt/host_context/mlir_function_executable.cc delete mode 100644 paddle/infrt/host_context/mlir_function_executable.h delete mode 100644 paddle/infrt/host_context/mlir_program_executor.cc delete mode 100644 paddle/infrt/host_context/mlir_program_executor.h delete mode 100644 paddle/infrt/host_context/mlir_tests/basic.mlir delete mode 100644 paddle/infrt/host_context/mlir_tests/dense_tensor.mlir delete mode 100644 paddle/infrt/host_context/mlir_tests/shape.mlir delete mode 100644 paddle/infrt/host_context/mlir_to_runtime_translate.cc delete mode 100644 paddle/infrt/host_context/mlir_to_runtime_translate.h delete mode 100644 paddle/infrt/host_context/mlir_to_runtime_translate_test.cc delete mode 100644 paddle/infrt/host_context/op_executable.cc delete mode 100644 paddle/infrt/host_context/op_executable.h delete mode 100644 paddle/infrt/host_context/op_executable_test.cc delete mode 100644 paddle/infrt/host_context/paddle_mlir.cc delete mode 100644 paddle/infrt/host_context/paddle_mlir.h delete mode 100644 paddle/infrt/host_context/paddle_mlir_converter.cc delete mode 100644 paddle/infrt/host_context/symbol_table.cc delete mode 100644 paddle/infrt/host_context/symbol_table.h delete mode 100644 paddle/infrt/host_context/value.cc delete mode 100644 paddle/infrt/host_context/value.h delete mode 100644 paddle/infrt/host_context/value_test.cc delete mode 100644 paddle/infrt/kernel/CMakeLists.txt delete mode 100644 paddle/infrt/kernel/basic_kernels.cc delete mode 100644 paddle/infrt/kernel/basic_kernels.h delete mode 100644 paddle/infrt/kernel/control_flow_kernels.cc delete mode 100644 paddle/infrt/kernel/control_flow_kernels.h delete mode 100644 paddle/infrt/kernel/phi/CMakeLists.txt delete mode 100644 paddle/infrt/kernel/phi/context_kernels.cc delete mode 100644 paddle/infrt/kernel/phi/context_kernels.h delete mode 100644 paddle/infrt/kernel/phi/dense_tensor_kernels.cc delete mode 100644 paddle/infrt/kernel/phi/dense_tensor_kernels.h delete mode 100644 paddle/infrt/kernel/phi/infershaped/infershape_launchers_test.cc delete mode 100644 paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.cc delete mode 100644 paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.h delete mode 100644 paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h delete mode 100644 paddle/infrt/kernel/phi/infershaped/infershaped_utils.h delete mode 100644 paddle/infrt/kernel/phi/infershaped/phi_kernel_launcher.h delete mode 100644 paddle/infrt/kernel/phi/registry.cc delete mode 100644 paddle/infrt/kernel/phi/registry.h delete mode 100644 paddle/infrt/kernel/tensor_kernels.cc delete mode 100644 paddle/infrt/kernel/tensor_kernels.h delete mode 100644 paddle/infrt/kernel/tensor_shape_kernels.cc delete mode 100644 paddle/infrt/kernel/tensor_shape_kernels.h delete mode 100644 paddle/infrt/kernel/tensorrt/CMakeLists.txt delete mode 100644 paddle/infrt/kernel/tensorrt/registry.cc delete mode 100644 paddle/infrt/kernel/tensorrt/registry.h delete mode 100644 paddle/infrt/kernel/tensorrt/trt_helper.h delete mode 100644 paddle/infrt/kernel/tensorrt/trt_kernels.cc delete mode 100644 paddle/infrt/kernel/tensorrt/trt_kernels.h delete mode 100644 paddle/infrt/kernel/tensorrt/trt_layers.h delete mode 100644 paddle/infrt/kernel/test_kernels.cc delete mode 100644 paddle/infrt/kernel/test_kernels.h delete mode 100644 paddle/infrt/paddle/CMakeLists.txt delete mode 100644 paddle/infrt/paddle/cpp/CMakeLists.txt delete mode 100644 paddle/infrt/paddle/cpp/desc_api.h delete mode 100644 paddle/infrt/paddle/framework.proto delete mode 100644 paddle/infrt/paddle/model_parser.cc delete mode 100644 paddle/infrt/paddle/model_parser.h delete mode 100644 paddle/infrt/paddle/pb/CMakeLists.txt delete mode 100644 paddle/infrt/paddle/pb/block_desc.cc delete mode 100644 paddle/infrt/paddle/pb/block_desc.h delete mode 100644 paddle/infrt/paddle/pb/op_desc.cc delete mode 100644 paddle/infrt/paddle/pb/op_desc.h delete mode 100644 paddle/infrt/paddle/pb/program_desc.cc delete mode 100644 paddle/infrt/paddle/pb/program_desc.h delete mode 100644 paddle/infrt/paddle/pb/var_desc.cc delete mode 100644 paddle/infrt/paddle/pb/var_desc.h delete mode 100644 paddle/infrt/paddle/scope.cc delete mode 100644 paddle/infrt/paddle/scope.h delete mode 100644 paddle/infrt/paddle/tensor.cc delete mode 100644 paddle/infrt/paddle/tensor.h delete mode 100644 paddle/infrt/support/CMakeLists.txt delete mode 100644 paddle/infrt/support/type_traits.h delete mode 100644 paddle/infrt/support/variant.h delete mode 100644 paddle/infrt/tensor/CMakeLists.txt delete mode 100644 paddle/infrt/tensor/dense_host_tensor.cc delete mode 100644 paddle/infrt/tensor/dense_host_tensor.h delete mode 100644 paddle/infrt/tensor/dense_tensor_view.cc delete mode 100644 paddle/infrt/tensor/dense_tensor_view.h delete mode 100644 paddle/infrt/tensor/phi/CMakeLists.txt delete mode 100644 paddle/infrt/tensor/phi/tensor_map.cc delete mode 100644 paddle/infrt/tensor/phi/tensor_map.h delete mode 100644 paddle/infrt/tensor/tensor_map.cc delete mode 100644 paddle/infrt/tensor/tensor_map.h delete mode 100644 paddle/infrt/tensor/tensor_metadata.cc delete mode 100644 paddle/infrt/tensor/tensor_metadata.h delete mode 100644 paddle/infrt/tensor/tensor_shape.cc delete mode 100644 paddle/infrt/tensor/tensor_shape.h delete mode 100644 paddle/infrt/tests/models/efficientnet-b4/net/efficientnet.py delete mode 100644 paddle/infrt/tests/models/efficientnet-b4/net/utils.py delete mode 100644 paddle/infrt/tests/models/linear.py diff --git a/paddle/CMakeLists.txt b/paddle/CMakeLists.txt index 23aae893cc2..c4a4d256458 100644 --- a/paddle/CMakeLists.txt +++ b/paddle/CMakeLists.txt @@ -7,7 +7,6 @@ add_subdirectory(scripts) add_subdirectory(testing) add_subdirectory(phi) -add_subdirectory(infrt) add_subdirectory(fluid) add_subdirectory(ir) diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt deleted file mode 100644 index c0c3b141b24..00000000000 --- a/paddle/infrt/CMakeLists.txt +++ /dev/null @@ -1,137 +0,0 @@ -if(NOT WITH_INFRT) - return() -endif() - -option(INFRT_WITH_PHI "Compile INFRT with PHI" ON) -option(INFRT_WITH_GPU "Compile INFRT with GPU" OFF) -option(INFRT_WITH_TRT "Compile INFRT with TensorRT" OFF) - -#TODO(xiaowei) remove fluid -include_directories(${PADDLE_SOURCE_DIR}/paddle/fluid/platform) - -if(WITH_GPU) - set(INFRT_WITH_GPU ON) -endif() - -if(INFRT_WITH_PHI) - add_definitions("-DINFRT_WITH_PHI") - - # TODO(wilber): Now Infrt gpu/trt depends on phi's components, Modify compile dependency options later. - if(INFRT_WITH_GPU) - add_definitions("-DINFRT_WITH_GPU") - if(INFRT_WITH_TRT) - add_definitions("-DINFRT_WITH_TRT") - endif() - endif() -endif() - -# compile flags -set(INFRT_FLAGS -Wno-comment) -foreach(flag ${INFRT_FLAGS}) - safe_set_cflag(CMAKE_C_FLAGS ${flag}) - safe_set_cxxflag(CMAKE_CXX_FLAGS ${flag}) -endforeach() - -set(INFRT_SOURCE_DIR "${PADDLE_SOURCE_DIR}/paddle/infrt") -set(INFRT_BINARY_DIR "${PADDLE_BINARY_DIR}/paddle/infrt") -set(INFRT_TEST_TARGETS CACHE INTERNAL "") -include(infrt_lib) - -set(infrt_src CACHE INTERNAL "" FORCE) - -# Gather headers for library publish. -function(core_gather_headers) - file( - GLOB includes - LIST_DIRECTORIES false - RELATIVE ${CMAKE_SOURCE_DIR} - *.h) - - foreach(header ${includes}) - set(core_includes - "${core_includes};${header}" - CACHE INTERNAL "") - endforeach() -endfunction() - -function(gather_srcs SRC_GROUP) - set(options) - set(oneValueArgs) - set(multiValueArgs "SRCS") - cmake_parse_arguments(prefix "" "" "${multiValueArgs}" ${ARGN}) - foreach(cpp ${prefix_SRCS}) - set(${SRC_GROUP} - "${${SRC_GROUP}};${CMAKE_CURRENT_SOURCE_DIR}/${cpp}" - CACHE INTERNAL "") - endforeach() -endfunction() - -# This method is similar to the global cc_test, but discard the huge amount default dependencies those are -# not needed by INFRT. -function(cc_test_tiny TARGET_NAME) - if(WITH_TESTING) - set(options SERIAL) - set(oneValueArgs "") - set(multiValueArgs SRCS DEPS ARGS) - cmake_parse_arguments(cc_test_tiny "${options}" "${oneValueArgs}" - "${multiValueArgs}" ${ARGN}) - add_executable(${TARGET_NAME} ${cc_test_tiny_SRCS}) - get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES) - target_link_libraries(${TARGET_NAME} ${cc_test_tiny_DEPS} - ${os_dependency_modules} infrt_gtest_main gtest) - add_dependencies(${TARGET_NAME} ${cc_test_tiny_DEPS} infrt_gtest_main gtest - extern_gtest) - - add_test( - NAME ${TARGET_NAME} - COMMAND ${TARGET_NAME} "${cc_test_tiny_ARGS}" - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) - if(${cc_test_tiny_SERIAL}) - set_property(TEST ${TARGET_NAME} PROPERTY RUN_SERIAL 1) - endif() - set(INFRT_TEST_TARGETS - ${INFRT_TEST_TARGETS} ${TARGET_NAME} - CACHE INTERNAL "") - endif() - -endfunction() - -if(WITH_TESTING) - cc_library( - infrt_gtest_main - SRCS gtest_main.cc - DEPS gtest glog gflags) -endif() - -add_subdirectory(api) -add_subdirectory(backends) -add_subdirectory(common) -add_subdirectory(dialect) -add_subdirectory(host_context) -add_subdirectory(kernel) -add_subdirectory(tensor) -add_subdirectory(support) -add_subdirectory(external_kernels) -add_subdirectory(paddle) - -# MLIR td file generations -set(infrt_mlir_incs basic_kernels_inc test_kernels_inc tensor_shape_inc - dense_tensor_inc pd_extra_ops_inc trt_ops_inc) - -if(INFRT_WITH_PHI) - set(phi_libs phi) - set(infrt_mlir_incs ${infrt_mlir_incs} MLIRinfrt_phi_tensorIncGen - MLIRinfrt_phi_baseIncGen) -endif() - -cc_library( - infrt SHARED - SRCS ${infrt_src} - DEPS glog ${mlir_libs} ${phi_libs} paddle_framework_proto infrt_naive) -cc_library( - infrt_static - SRCS ${infrt_src} - DEPS glog ${mlir_libs} ${phi_libs} paddle_framework_proto) -add_dependencies(infrt ${infrt_mlir_incs} mlir-headers) - -add_custom_target(test_infrt_exec DEPENDS ${INFRT_TEST_TARGETS}) diff --git a/paddle/infrt/api/.gitignore b/paddle/infrt/api/.gitignore deleted file mode 100644 index 06196d34f87..00000000000 --- a/paddle/infrt/api/.gitignore +++ /dev/null @@ -1 +0,0 @@ -infrt_api_test.cc diff --git a/paddle/infrt/api/CMakeLists.txt b/paddle/infrt/api/CMakeLists.txt deleted file mode 100644 index 2d88af7d5b5..00000000000 --- a/paddle/infrt/api/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -core_gather_headers() - -gather_srcs(infrt_src SRCS infrt_api.cc) -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/infrt_api_test.cc.in - ${CMAKE_CURRENT_SOURCE_DIR}/infrt_api_test.cc) - -# Disable temporarily for the external-kernel's mkldnn is outdate -cc_test_tiny(test_infrt_api SRCS infrt_api_test.cc DEPS infrt ${MLIR_IR_LIBS}) -# TODO(inference): remove after optimize weight unfold. -set_tests_properties(test_infrt_api PROPERTIES TIMEOUT 200) diff --git a/paddle/infrt/api/infrt_api.cc b/paddle/infrt/api/infrt_api.cc deleted file mode 100644 index e2fdd9a4871..00000000000 --- a/paddle/infrt/api/infrt_api.cc +++ /dev/null @@ -1,335 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/api/infrt_api.h" - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "paddle/infrt/backends/host/phi_allocator.h" -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h" -#include "paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.h" -#include "paddle/infrt/dialect/mlir_loader.h" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" -#include "paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h" -#include "paddle/infrt/host_context/core_runtime.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/mlir_function_executable.h" -#include "paddle/infrt/host_context/mlir_to_runtime_translate.h" -#include "paddle/infrt/host_context/op_executable.h" -#include "paddle/infrt/host_context/paddle_mlir.h" -#include "paddle/infrt/host_context/value.h" -#include "paddle/infrt/kernel/basic_kernels.h" -#include "paddle/infrt/kernel/control_flow_kernels.h" -#include "paddle/infrt/kernel/phi/dense_tensor_kernels.h" -#include "paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h" -#include "paddle/infrt/kernel/phi/registry.h" -#include "paddle/infrt/kernel/tensor_kernels.h" -#include "paddle/infrt/kernel/tensor_shape_kernels.h" -#include "paddle/infrt/kernel/test_kernels.h" -#include "paddle/infrt/tensor/tensor_map.h" - -#if defined(INFRT_WITH_GPU) && defined(INFRT_WITH_TRT) -#include "paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.h" -#include "paddle/infrt/dialect/tensorrt/trt_graph_split_pass.h" -#include "paddle/infrt/dialect/tensorrt/trt_op_converter_pass.h" -#include "paddle/infrt/dialect/tensorrt/trt_op_teller_pass.h" -#include "paddle/infrt/dialect/tensorrt/trt_type_convert_pass.h" -#include "paddle/infrt/kernel/tensorrt/registry.h" -#endif - -using namespace infrt::host_context; // NOLINT -using namespace infrt::tensor; // NOLINT -using namespace infrt::tensor; // NOLINT - -namespace infrt { - -template -std::string DumpToString(T& op) { // NOLINT - std::string buffer; - llvm::raw_string_ostream os(buffer); - op.print(os); - os.flush(); - return buffer; -} - -struct MlirToRuntimeTranslator::Impl { - mlir::ModuleOp module; - // The runtime for a function call. - CoreRuntimeBuilder* runtime{}; - - // The current working op, the translator process the ops one by one, each - // time it updates `cur_op` here to current op - // working on. - OpExecutableBuilder* cur_op{}; - - // record the current function name. - std::string cur_func_name; - - // Name to function definitions. - std::unordered_map func_defs; - - // Map from an operation to its results. - std::unordered_map> op_results; - llvm::DenseMap value_map; -}; - -/** - * Execute the mlir program in predict mode. - */ -class PredictExecutor : public MlirToRuntimeTranslator { - public: - CoreRuntimeBuilder core_runtime; - - PredictExecutor(mlir::ModuleOp module, - KernelRegistry* registry, - ::infrt::phi::DenseTensorMap&& map) - : MlirToRuntimeTranslator(module, &core_runtime), - core_runtime(registry), - registry_(registry) { - CHECK(registry_); - Init(std::move(map)); - } - - void Run() { - auto arguments = llvm::makeArrayRef(arguments_); - auto results = llvm::makeMutableArrayRef(results_.begin(), results_.size()); - function_executable_->Execute(arguments, results); - } - - int GetInputNum() { return inputs_.size(); } - - ::Tensor* GetInput(int i) { return inputs_[i]; } - - int GetOutputNum() { return outputs_.size(); } - - ::Tensor* GetOutput(int i) { return outputs_[i]; } - - private: - void Init(::infrt::phi::DenseTensorMap&& map) { - EmitFunctions(); - llvm::Optional predict_func_ = llvm::None; - for (auto func_op : impl_->module.getOps()) { - if (func_op.getName().str() != "main_graph") continue; - predict_func_ = func_op; - break; - } - if (!predict_func_) { - std::cout << "ERROR: init failed, no predict function found in mlir." - << std::endl; - return; - } - auto& predict_func = predict_func_.getValue(); - function_executable_ = - new MlirFunctionExecutable(predict_func, registry_, impl_->func_defs); - - // process parammeters - VLOG(3) << "Arguments num of predict func: " - << predict_func.getNumArguments(); - for (size_t i = 0; i < predict_func.getNumArguments(); ++i) { - auto arg = predict_func.getArgument(i); - auto type = arg.getType(); - // this param is TensorMap - if (type.isa<::infrt::phi::DenseTensorMapType>()) { - auto* value = new host_context::Value(std::move(map)); - arguments_.push_back(value); - AddValue(predict_func.getArgument(i), value); - } else if (type.isa<::infrt::DenseTensorType>()) { - // this param is an input Tensor - auto dht = ::Tensor(); - auto* value = new host_context::Value(std::move(dht)); - arguments_.push_back(value); - inputs_.push_back(&(value->get<::Tensor>())); - } else { - llvm_unreachable("The input type has not been supported by predictor."); - } - } - - // process results - auto& last_op = predict_func.front().back(); - if (last_op.getName().getStringRef() == "infrt.return") { - for (size_t i = 0; i < last_op.getNumOperands(); ++i) { - auto operand = last_op.getOperand(i); - if (operand.getType().isa<::infrt::DenseTensorType>()) { - auto r = impl_->value_map.try_emplace( - operand, ValueRef(new host_context::Value(::Tensor()))); - CHECK(r.second) << "Duplicate add mlir value [" - << DumpToString(operand) << "]"; - auto* value = r.first->second.get(); - results_.push_back(ValueRef(value)); - outputs_.push_back(&(value->get<::Tensor>())); - } else { - llvm_unreachable("infrt.return only supports DenseTensor now."); - } - } - } - } - - protected: - std::unordered_map func_def_table; - - void EmitFunction(mlir::FuncOp op) override { - CHECK(!impl_->func_defs.count(op.getName().str())) - << "Duplicate function defition found for function [" - << op.getName().str(); - impl_->func_defs.emplace(op.getName().str(), op); - } - - private: - KernelRegistry* registry_{}; - MlirFunctionExecutable* function_executable_; - llvm::SmallVector<::Tensor*, 1> inputs_; - llvm::SmallVector arguments_; - llvm::SmallVector<::Tensor*, 1> outputs_; - llvm::SmallVector results_; -}; - -std::unique_ptr CreateInfRtPredictor( - const InfRtConfig& config) { - auto x = std::make_unique(); - x->Init(config); - return x; -} - -struct InfRtPredictor::Impl { - std::unique_ptr executor; - MLIRModelGenImpl module_gen_; -}; - -InfRtPredictor::InfRtPredictor() : impl_(new Impl) {} -InfRtPredictor::~InfRtPredictor() {} - -void InfRtPredictor::Run() { impl_->executor->Run(); } - -int InfRtPredictor::Init(const InfRtConfig& config) { - mlir::MLIRContext* context = ::infrt::Global::getMLIRContext(); - - KernelRegistry* registry = new KernelRegistry(); - - kernel::RegisterBasicKernels(registry); - kernel::RegisterTestKernels(registry); - kernel::RegisterTensorShapeKernels(registry); - kernel::RegisterTensorKernels(registry); - kernel::RegisterControlFlowKernels(registry); -#ifdef INFRT_WITH_PHI - kernel::RegisterPhiKernels(registry); - kernel::RegisterInferShapeLaunchers(registry); -#if defined(INFRT_WITH_GPU) && defined(INFRT_WITH_TRT) - kernel::RegisterTrtKernels(registry); -#endif // INFRT_WITH_GPU && INFRT_WITH_TRT -#endif - - mlir::ModuleOp module_op; - if (config.tensorrt_enabled()) { - module_op = impl_->module_gen_.ImportPaddleModel( - config.model_dir(), config.param_dir(), false); - } else { - module_op = impl_->module_gen_.ImportPaddleModel(config.model_dir(), - config.param_dir()); - } - - context->loadAllAvailableDialects(); - ::mlir::PassManager pm(context); - ::mlir::OpPassManager& pass_manager = pm.nest<::mlir::FuncOp>(); - if (config.tensorrt_enabled()) { - pass_manager.addPass(::infrt::CreateInfrtWeightsUnfoldPass()); -#if defined(INFRT_WITH_GPU) && defined(INFRT_WITH_TRT) - pass_manager.addPass(::infrt::trt::CreateTrtOpTellerPass()); - pass_manager.addPass(::infrt::trt::CreateTrtGraphFusePass()); - pass_manager.addPass(::infrt::trt::CreateTrtGraphSplitPass(1)); - pass_manager.addPass(::infrt::trt::CreateTrtOpConverterPass()); - pass_manager.addPass(::infrt::trt::CreateTrtTypeConvertPass()); -#endif - pass_manager.addPass(::mlir::createCanonicalizerPass()); - } else { - std::vector<::infrt::Place> valid_places = { - {::infrt::TargetType::CPU, - ::infrt::PrecisionType::FLOAT32, - ::infrt::LayoutType::NCHW}}; - if (config.gpu_enabled()) { - valid_places.insert(valid_places.begin(), - ::infrt::Place(::infrt::TargetType::GPU, - ::infrt::PrecisionType::FLOAT32, - ::infrt::LayoutType::NCHW)); - } - pass_manager.addPass(CreatePhiOpCvtPass(valid_places)); - pass_manager.addPass(CreateInfrtOpFusePass()); - } - if (mlir::failed(pm.run(module_op))) { - std::cout << "\npass failed!\n" << std::endl; - return 4; - } -#ifndef NDEBUG - module_op.dump(); -#endif // NDEBUG - - // load extra shared library - for (const std::string& lib_path : config.shared_libs()) { - std::string err; - llvm::sys::DynamicLibrary dynLib = - llvm::sys::DynamicLibrary::getPermanentLibrary(lib_path.c_str(), &err); - if (!dynLib.isValid()) { - llvm::errs() << "Load shared library failed. Error: " << err << "\n"; - return 1; - } - if (auto reg_sym = dynLib.SearchForAddressOfSymbol("RegisterKernels")) { - auto reg_func = reinterpret_cast(reg_sym); - reg_func(registry); - } else { - llvm::outs() << "Symbol \"RegisterKernels\" not found in \"" << lib_path - << "\". Skip.\n"; - } - } - - // Load params - if (config.gpu_enabled() && !config.tensorrt_enabled()) { - auto tensor_map = ::infrt::kernel::phi::LoadCombinedParamsToGpu( - config.model_dir(), config.param_dir()); - impl_->executor.reset( - new PredictExecutor(module_op, registry, std::move(tensor_map))); - - } else { - auto tensor_map = ::infrt::kernel::phi::LoadCombinedParameters( - config.model_dir(), config.param_dir()); - impl_->executor.reset( - new PredictExecutor(module_op, registry, std::move(tensor_map))); - } - - return 0; -} - -int InfRtPredictor::GetInputNum() { return impl_->executor->GetInputNum(); } - -::Tensor* InfRtPredictor::GetInput(int i) { - return impl_->executor->GetInput(i); -} - -int InfRtPredictor::GetOutputNum() { return impl_->executor->GetOutputNum(); } - -::Tensor* InfRtPredictor::GetOutput(int i) { - return impl_->executor->GetOutput(i); -} - -} // namespace infrt diff --git a/paddle/infrt/api/infrt_api.h b/paddle/infrt/api/infrt_api.h deleted file mode 100644 index 511d51648d7..00000000000 --- a/paddle/infrt/api/infrt_api.h +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include - -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { - -class InfRtConfig { - std::string model_dir_; - std::string param_dir_; - std::vector shared_libs_; - - // TODO(wilber): Design an easy-to-use interface. - bool gpu_enabled_{false}; - bool tensorrt_enabled_{false}; - - public: - InfRtConfig() = default; - void set_model_dir(const std::string& model_dir) { model_dir_ = model_dir; } - const std::string& model_dir() const { return model_dir_; } - - void set_param_dir(const std::string& param_dir) { param_dir_ = param_dir; } - const std::string& param_dir() const { return param_dir_; } - - void set_shared_libs(const std::vector& shared_libs) { - shared_libs_ = shared_libs; - } - const std::vector& shared_libs() const { return shared_libs_; } - - void enable_gpu() { gpu_enabled_ = true; } - bool gpu_enabled() const { return gpu_enabled_; } - - // TODO(wilber): Design an easy-to-use interface. - void enable_tensorrt() { tensorrt_enabled_ = true; } - void disable_tensorrt() { tensorrt_enabled_ = false; } - bool tensorrt_enabled() const { return tensorrt_enabled_; } - - virtual ~InfRtConfig() = default; -}; - -class InfRtPredictor { - public: - InfRtPredictor(); - ~InfRtPredictor(); - void Run(); - int Init(const InfRtConfig& config); - int GetInputNum(); - ::Tensor* GetInput(int i); - int GetOutputNum(); - ::Tensor* GetOutput(int i); - - protected: - struct Impl; - std::unique_ptr impl_; -}; - -std::unique_ptr CreateInfRtPredictor(const InfRtConfig& config); - -} // namespace infrt diff --git a/paddle/infrt/api/infrt_api_test.cc.in b/paddle/infrt/api/infrt_api_test.cc.in deleted file mode 100644 index 32c8c25cd29..00000000000 --- a/paddle/infrt/api/infrt_api_test.cc.in +++ /dev/null @@ -1,217 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include - -#include -#include - -#include "llvm/Support/raw_ostream.h" -#include "paddle/infrt/api/infrt_api.h" -#include "paddle/infrt/backends/host/phi_allocator.h" -#include "paddle/infrt/common/buffer.h" -#include "paddle/infrt/common/dtype.h" - -using infrt::InfRtConfig; -using infrt::InfRtPredictor; -using infrt::CreateInfRtPredictor; - -namespace infrt { - -TEST(InfRtPredictor, predictor) { - std::vector shared_libs; - - InfRtConfig config; - - config.set_model_dir("@CMAKE_BINARY_DIR@/linear/linear.pdmodel"); - config.set_param_dir("@CMAKE_BINARY_DIR@/linear/linear.pdiparams"); - - std::unique_ptr predictor = CreateInfRtPredictor(config); - - ::infrt::backends::CpuPhiAllocator cpu_allocator; - ::Tensor* input = predictor->GetInput(0); - input->Resize({16, 784}); - input->AllocateFrom(&cpu_allocator, ::phi::DataType::FLOAT32); - auto* input_data = reinterpret_cast(input->data()); - for (int i = 0; i < input->numel(); i++) input_data[i] = 1.0; - - predictor->Run(); - - // get and print output tensor - auto* output = predictor->GetOutput(0); - - // TODO(Shixiaowei02): Automatic result validation for training then inference. - // auto* output_data = reinterpret_cast(output->data()); - - ASSERT_EQ(output->dims(), ::phi::DDim({16, 10})); -} - -TEST(InfRtPredictor, cpu_predictor) { - std::vector shared_libs; - - InfRtConfig config; - - config.set_model_dir("@CMAKE_BINARY_DIR@/models/resnet50/model.pdmodel"); - config.set_param_dir("@CMAKE_BINARY_DIR@/models/resnet50/model.pdiparams"); - - std::unique_ptr predictor = CreateInfRtPredictor(config); - - ::infrt::backends::CpuPhiAllocator cpu_allocator; - ::Tensor* input = predictor->GetInput(0); - input->Resize({2, 3, 256, 256}); - input->AllocateFrom(&cpu_allocator, ::phi::DataType::FLOAT32); - auto* input_data = reinterpret_cast(input->data()); - for (int i = 0; i < input->numel(); i++) input_data[i] = 1.0; - - for(int i = 0; i < 10; i++) { - predictor->Run(); - } - auto start = std::chrono::steady_clock::now(); - for(int i = 0; i < 10; i++) { - predictor->Run(); - } - auto end = std::chrono::steady_clock::now(); - auto msec = std::chrono::duration_cast(end-start); - std::cout <<"One predict period costs " << msec.count()/1000 << "ms.\n"; - - // get and print output tensor - auto* output = predictor->GetOutput(0); - - ASSERT_EQ(output->dims(), ::phi::DDim({2, 1000})); - const std::vector true_vals { - -3.319006264209747314e-01, -1.418896913528442383e+00, - -6.934890151023864746e-01, -1.498023152351379395e+00, - 3.078042864799499512e-01, -1.340998053550720215e+00, - 3.508620023727416992e+00, 2.274388313293457031e+00, - -1.321727275848388672e+00, -8.888689428567886353e-02, - -3.319006264209747314e-01, -1.418896913528442383e+00, - -6.934890151023864746e-01, -1.498023152351379395e+00, - 3.078042864799499512e-01, -1.340998053550720215e+00, - 3.508620023727416992e+00, 2.274388313293457031e+00, - -1.321727275848388672e+00, -8.888689428567886353e-02 - }; - - for (size_t i = 0; i < true_vals.size(); i+=100) { - CHECK_NEAR(output->data()[i*100], true_vals[i], 1e-5); - } -} - - -#ifdef INFRT_WITH_TRT -TEST(InfRtPredictor, trt_predictor) { - std::vector shared_libs; - - InfRtConfig config; - config.enable_tensorrt(); - - config.set_model_dir("@CMAKE_BINARY_DIR@/models/resnet50/model.pdmodel"); - config.set_param_dir("@CMAKE_BINARY_DIR@/models/resnet50/model.pdiparams"); - - std::unique_ptr predictor = CreateInfRtPredictor(config); - - ::infrt::backends::CpuPhiAllocator cpu_allocator; - ::Tensor* input = predictor->GetInput(0); - input->Resize({2, 3, 256, 256}); - input->AllocateFrom(&cpu_allocator, ::phi::DataType::FLOAT32); - auto* input_data = reinterpret_cast(input->data()); - for (int i = 0; i < input->numel(); i++) input_data[i] = 1.0; - predictor->Run(); - - // get and print output tensor - auto* output = predictor->GetOutput(0); - - ASSERT_EQ(output->dims(), ::phi::DDim({2, 1000})); - const std::vector true_vals { - -3.319006264209747314e-01, -1.418896913528442383e+00, - -6.934890151023864746e-01, -1.498023152351379395e+00, - 3.078042864799499512e-01, -1.340998053550720215e+00, - 3.508620023727416992e+00, 2.274388313293457031e+00, - -1.321727275848388672e+00, -8.888689428567886353e-02, - -3.319006264209747314e-01, -1.418896913528442383e+00, - -6.934890151023864746e-01, -1.498023152351379395e+00, - 3.078042864799499512e-01, -1.340998053550720215e+00, - 3.508620023727416992e+00, 2.274388313293457031e+00, - -1.321727275848388672e+00, -8.888689428567886353e-02 - }; - - for (size_t i = 0; i < true_vals.size(); i+=100) { - CHECK_NEAR(output->data()[i*100], true_vals[i], 1e-5); - } -} -#endif - -#ifdef INFRT_WITH_GPU -TEST(InfRtPredictor, gpu_predictor) { - std::vector shared_libs; - - InfRtConfig config; - config.enable_gpu(); - - config.set_model_dir("@CMAKE_BINARY_DIR@/models/resnet50/model.pdmodel"); - config.set_param_dir("@CMAKE_BINARY_DIR@/models/resnet50/model.pdiparams"); - - std::unique_ptr predictor = CreateInfRtPredictor(config); - - ::infrt::backends::GpuPhiAllocator gpu_allocator; - - - ::Tensor* input = predictor->GetInput(0); - input->Resize({2, 3, 256, 256}); - input->AllocateFrom(&gpu_allocator, ::phi::DataType::FLOAT32); - auto* data = reinterpret_cast(input->data()); - - std::vector input_data(2 * 3 * 256 * 256, 1.0); - cudaMemcpy(data, - input_data.data(), - sizeof(float) * input->numel(), - cudaMemcpyHostToDevice); - - for(int i = 0; i < 10; i++) { - predictor->Run(); - } - auto start = std::chrono::steady_clock::now(); - for(int i = 0; i < 1000; i++) { - predictor->Run(); - } - auto end = std::chrono::steady_clock::now(); - auto msec = std::chrono::duration_cast(end-start); - std::cout <<"One predict period costs " << msec.count()/1000 << "ms.\n"; - - auto* output = predictor->GetOutput(0); - std::vector output_data(output->numel()); - cudaMemcpy(output_data.data(), - output->data(), - sizeof(float) * output->numel(), - cudaMemcpyDeviceToHost); - - ASSERT_EQ(output->dims(), ::phi::DDim({2, 1000})); - const std::vector true_vals { - -3.319006264209747314e-01, -1.418896913528442383e+00, - -6.934890151023864746e-01, -1.498023152351379395e+00, - 3.078042864799499512e-01, -1.340998053550720215e+00, - 3.508620023727416992e+00, 2.274388313293457031e+00, - -1.321727275848388672e+00, -8.888689428567886353e-02, - -3.319006264209747314e-01, -1.418896913528442383e+00, - -6.934890151023864746e-01, -1.498023152351379395e+00, - 3.078042864799499512e-01, -1.340998053550720215e+00, - 3.508620023727416992e+00, 2.274388313293457031e+00, - -1.321727275848388672e+00, -8.888689428567886353e-02 - }; - for (size_t i = 0; i < true_vals.size(); i+=100) { - CHECK_NEAR(output_data[i*100], true_vals[i], 1e-5); - } -} -#endif - -} // namespace infrt diff --git a/paddle/infrt/backends/CMakeLists.txt b/paddle/infrt/backends/CMakeLists.txt deleted file mode 100644 index 606fec5d92d..00000000000 --- a/paddle/infrt/backends/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -if(INFRT_WITH_PHI - AND WITH_GPU - AND WITH_TENSORRT) - add_subdirectory(tensorrt) -endif() diff --git a/paddle/infrt/backends/host/phi_allocator.h b/paddle/infrt/backends/host/phi_allocator.h deleted file mode 100644 index 810c79509e7..00000000000 --- a/paddle/infrt/backends/host/phi_allocator.h +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#pragma once - -#include "paddle/fluid/memory/malloc.h" -#include "paddle/phi/core/allocator.h" - -#ifdef INFRT_WITH_GPU -#include -#endif - -namespace infrt { -namespace backends { - -class CpuPhiAllocator : public phi::Allocator { - public: - static void deleter(phi::Allocation* ptr) { ::operator delete(ptr); } - - AllocationPtr Allocate(size_t bytes_size) { - return AllocationPtr( - new phi::Allocation(::operator new(bytes_size), - bytes_size, - phi::Place(phi::AllocationType::CPU)), - deleter); - } -}; - -#ifdef INFRT_WITH_GPU -// TODO(wilber): Just for demo test. we need a more efficient gpu allocator. -class GpuPhiAllocator : public phi::Allocator { - public: - static void deleter(phi::Allocation* ptr) { cudaFree(ptr->ptr()); } - - AllocationPtr Allocate(size_t bytes_size) { - return paddle::memory::Alloc(phi::Place(phi::AllocationType::GPU), - bytes_size); - } -}; -#endif - -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/host/phi_context.h b/paddle/infrt/backends/host/phi_context.h deleted file mode 100644 index 880d1f03d87..00000000000 --- a/paddle/infrt/backends/host/phi_context.h +++ /dev/null @@ -1,47 +0,0 @@ -/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#pragma once - -#include "paddle/infrt/backends/host/phi_allocator.h" -#include "paddle/phi/backends/cpu/cpu_context.h" -#include "paddle/phi/backends/gpu/gpu_context.h" - -namespace infrt { -namespace backends { - -class CpuPhiContext : public ::phi::CPUContext { - public: - using Base = ::phi::CPUContext; - using ::phi::CPUContext::SetEigenDevice; - - CpuPhiContext() { - Init(); - SetAllocator(alloc_.get()); - } - - private: - std::unique_ptr<::phi::Allocator> alloc_{std::make_unique()}; -}; - -class GpuPhiContext : public ::phi::GPUContext { - public: - using Base = ::phi::GPUContext; - using ::phi::GPUContext::SetBlasHandle; - using ::phi::GPUContext::SetDnnHandle; - using ::phi::GPUContext::SetEigenDevice; - using ::phi::GPUContext::SetSolverHandle; - using ::phi::GPUContext::SetSparseHandle; - using ::phi::GPUContext::SetStream; -}; - -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/tensorrt/CMakeLists.txt b/paddle/infrt/backends/tensorrt/CMakeLists.txt deleted file mode 100644 index 9a9db6b737c..00000000000 --- a/paddle/infrt/backends/tensorrt/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -add_subdirectory(plugin) - -core_gather_headers() - -gather_srcs(infrt_src SRCS trt_engine.cc) - -cc_test_tiny( - test_infrt_trt - SRCS - test_trt_engine.cc - DEPS - infrt - phi_dynload_cuda - tensorrt_converter) diff --git a/paddle/infrt/backends/tensorrt/plugin/CMakeLists.txt b/paddle/infrt/backends/tensorrt/plugin/CMakeLists.txt deleted file mode 100644 index 8848148f2c6..00000000000 --- a/paddle/infrt/backends/tensorrt/plugin/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -gather_srcs(infrt_src SRCS pool_op_plugin.cu) diff --git a/paddle/infrt/backends/tensorrt/plugin/plugin_utils.h b/paddle/infrt/backends/tensorrt/plugin/plugin_utils.h deleted file mode 100644 index 49e96e6eab0..00000000000 --- a/paddle/infrt/backends/tensorrt/plugin/plugin_utils.h +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -#include -#include -#include -#include -#include - -#include "paddle/phi/backends/dynload/tensorrt.h" - -namespace infrt { -namespace backends { -namespace tensorrt { -namespace plugin { - -template -inline void SerializeValue(void** buffer, T const& value); - -template -inline void DeserializeValue(void const** buffer, - size_t* buffer_size, - T* value); - -namespace details { - -template -struct Serializer {}; - -template -struct Serializer::value || - std::is_enum::value || - std::is_pod::value>::type> { - static size_t SerializedSize(T const& value) { return sizeof(T); } - - static void Serialize(void** buffer, T const& value) { - std::memcpy(*buffer, &value, sizeof(T)); - reinterpret_cast(*buffer) += sizeof(T); - } - - static void Deserialize(void const** buffer, size_t* buffer_size, T* value) { - assert(*buffer_size >= sizeof(T)); - std::memcpy(value, *buffer, sizeof(T)); - reinterpret_cast(*buffer) += sizeof(T); - *buffer_size -= sizeof(T); - } -}; - -template <> -struct Serializer { - static size_t SerializedSize(const char* value) { return strlen(value) + 1; } - - static void Serialize(void** buffer, const char* value) { - std::strcpy(static_cast(*buffer), value); // NOLINT - reinterpret_cast(*buffer) += strlen(value) + 1; - } - - static void Deserialize(void const** buffer, - size_t* buffer_size, - const char** value) { - *value = static_cast(*buffer); - size_t data_size = strnlen(*value, *buffer_size) + 1; - assert(*buffer_size >= data_size); - reinterpret_cast(*buffer) += data_size; - *buffer_size -= data_size; - } -}; - -template -struct Serializer, - typename std::enable_if::value || - std::is_enum::value || - std::is_pod::value>::type> { - static size_t SerializedSize(std::vector const& value) { - return sizeof(value.size()) + value.size() * sizeof(T); - } - - static void Serialize(void** buffer, std::vector const& value) { - SerializeValue(buffer, value.size()); - size_t nbyte = value.size() * sizeof(T); - std::memcpy(*buffer, value.data(), nbyte); - reinterpret_cast(*buffer) += nbyte; - } - - static void Deserialize(void const** buffer, - size_t* buffer_size, - std::vector* value) { - size_t size; - DeserializeValue(buffer, buffer_size, &size); - value->resize(size); - size_t nbyte = value->size() * sizeof(T); - CHECK_GE(*buffer_size, nbyte); - std::memcpy(value->data(), *buffer, nbyte); - reinterpret_cast(*buffer) += nbyte; - *buffer_size -= nbyte; - } -}; - -} // namespace details - -template -inline size_t SerializedSize(T const& value) { - return details::Serializer::SerializedSize(value); -} - -template -inline void SerializeValue(void** buffer, T const& value) { - return details::Serializer::Serialize(buffer, value); -} - -template -inline void DeserializeValue(void const** buffer, - size_t* buffer_size, - T* value) { - return details::Serializer::Deserialize(buffer, buffer_size, value); -} - -template -class TrtPluginRegistrar { - public: - TrtPluginRegistrar() { - static auto func_ptr = static_cast( - ::phi::dynload::getPluginRegistry()); - func_ptr->registerCreator(instance, ""); - } - - private: - //! Plugin instance. - T instance{}; -}; - -#define REGISTER_TRT_PLUGIN(name) \ - static TrtPluginRegistrar pluginRegistrar##name {} - -} // namespace plugin -} // namespace tensorrt -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.cu b/paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.cu deleted file mode 100644 index f3e2fe35074..00000000000 --- a/paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.cu +++ /dev/null @@ -1,288 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "glog/logging.h" -#include "paddle/infrt/backends/tensorrt/plugin/plugin_utils.h" -#include "paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.h" -#include "paddle/phi/kernels/funcs/pooling.h" - -namespace infrt { -namespace backends { -namespace tensorrt { -namespace plugin { - -PoolPlugin::PoolPlugin(bool ceil_mode, - PoolType pool_type, - bool adaptive, - bool exclusive, - std::vector ksize, - std::vector strides, - std::vector paddings, - std::vector input_shape, - std::vector real_paddings) - : ceil_mode_(ceil_mode), - pool_type_(pool_type), - adaptive_(adaptive), - exclusive_(exclusive), - ksize_(ksize), - strides_(strides), - paddings_(paddings), - real_paddings_(real_paddings), - input_shape_(input_shape) { - output_shape_ = input_shape_; - std::vector output_shape = - CalcOutputSize({input_shape_[1], input_shape_[2]}, - ceil_mode_, - adaptive_, - ksize_, - strides_, - real_paddings_); - output_shape_[1] = output_shape[0]; - output_shape_[2] = output_shape[1]; -} - -PoolPlugin::PoolPlugin(void const* serialData, size_t serialLength) { - // deserializeBase(serialData, serialLength); - DeserializeValue(&serialData, &serialLength, &ceil_mode_); - DeserializeValue(&serialData, &serialLength, &pool_type_); - DeserializeValue(&serialData, &serialLength, &adaptive_); - DeserializeValue(&serialData, &serialLength, &exclusive_); - DeserializeValue(&serialData, &serialLength, &ksize_); - DeserializeValue(&serialData, &serialLength, &strides_); - DeserializeValue(&serialData, &serialLength, &paddings_); - DeserializeValue(&serialData, &serialLength, &real_paddings_); - DeserializeValue(&serialData, &serialLength, &input_shape_); - DeserializeValue(&serialData, &serialLength, &output_shape_); -} - -const char* PoolPlugin::getPluginType() const noexcept { return "pool_plugin"; } - -const char* PoolPlugin::getPluginVersion() const noexcept { return "1"; } - -int PoolPlugin::getNbOutputs() const noexcept { return 1; } - -nvinfer1::Dims PoolPlugin::getOutputDimensions(int outputIndex, - const nvinfer1::Dims* inputs, - int nbInputs) noexcept { - assert(nbInputs == 1); - assert(index == 0); - assert(inputs[0].nbDims == 3); - nvinfer1::Dims const& input_dims = inputs[0]; - - nvinfer1::Dims output_dims = input_dims; - - output_dims.d[1] = output_shape_[1]; - output_dims.d[2] = output_shape_[2]; - return output_dims; -} - -int32_t PoolPlugin::initialize() noexcept { return 0; } - -void PoolPlugin::terminate() noexcept {} - -size_t PoolPlugin::getWorkspaceSize(int32_t maxBatchSize) const noexcept { - return 0; -} - -#if IS_TRT_VERSION_LT(8000) -int PoolPlugin::enqueue(int batch_size, - const void* const* inputs, - void** outputs, -#else -int PoolPlugin::enqueue(int batch_size, - const void* const* inputs, - void* const* outputs, -#endif - void* workspace, - cudaStream_t stream) noexcept { - // TODO(wilber) - int input_size = 0; - float const* idata = reinterpret_cast(inputs[0]); - float* const* odatas = reinterpret_cast(outputs); - - std::vector input_shape = input_shape_; - std::vector output_shape = output_shape_; - input_shape.insert(input_shape.begin(), batch_size); - output_shape.insert(output_shape.begin(), batch_size); - - if (pool_type_ == PoolType::max) { - ::phi::funcs::MaxPool pool_process; - ::phi::funcs::Pool2dDirectCUDAFunctor, float> - pool2d_forward; - pool2d_forward(idata, - input_shape, - output_shape, - ksize_, - strides_, - paddings_, - true, - false, - odatas[0], - stream, - pool_process); - } else if (pool_type_ == PoolType::avg) { - ::phi::funcs::AvgPool pool_process; - ::phi::funcs::Pool2dDirectCUDAFunctor, float> - pool2d_forward; - pool2d_forward(idata, - input_shape, - output_shape, - ksize_, - strides_, - paddings_, - exclusive_, - adaptive_, - odatas[0], - stream, - pool_process); - } - - return cudaGetLastError() != cudaSuccess; -} - -// TODO(wilber): serialize base info? -size_t PoolPlugin::getSerializationSize() const noexcept { - return SerializedSize(ceil_mode_) + SerializedSize(pool_type_) + - SerializedSize(adaptive_) + SerializedSize(exclusive_) + - SerializedSize(ksize_) + SerializedSize(strides_) + - SerializedSize(paddings_) + SerializedSize(real_paddings_) + - SerializedSize(input_shape_) + SerializedSize(output_shape_); -} -// TODO(wilber): serialize base info? -void PoolPlugin::serialize(void* buffer) const noexcept { - // serializeBase(buffer); - SerializeValue(&buffer, ceil_mode_); - SerializeValue(&buffer, pool_type_); - SerializeValue(&buffer, adaptive_); - SerializeValue(&buffer, exclusive_); - SerializeValue(&buffer, ksize_); - SerializeValue(&buffer, strides_); - SerializeValue(&buffer, paddings_); - SerializeValue(&buffer, real_paddings_); - SerializeValue(&buffer, input_shape_); - SerializeValue(&buffer, output_shape_); -} - -void PoolPlugin::destroy() noexcept { delete this; } - -void PoolPlugin::setPluginNamespace(char const* plugin_namespace) noexcept { - namespace_ = plugin_namespace; -} - -char const* PoolPlugin::getPluginNamespace() const noexcept { - return namespace_.c_str(); -} - -nvinfer1::DataType PoolPlugin::getOutputDataType( - int32_t index, - nvinfer1::DataType const* input_types, - int32_t nbInputs) const noexcept { - CHECK_EQ(index, 0); - CHECK_EQ((input_types[0] == nvinfer1::DataType::kFLOAT), true); - return input_types[0]; -} - -bool PoolPlugin::isOutputBroadcastAcrossBatch(int32_t outputIndex, - bool const* inputIsBroadcasted, - int32_t nbInputs) const noexcept { - return false; -} - -bool PoolPlugin::canBroadcastInputAcrossBatch( - int32_t inputIndex) const noexcept { - return false; -} - -nvinfer1::IPluginV2Ext* PoolPlugin::clone() const noexcept { - auto* plugin = new PoolPlugin(ceil_mode_, - pool_type_, - adaptive_, - exclusive_, - ksize_, - strides_, - paddings_, - input_shape_, - real_paddings_); - plugin->setPluginNamespace(namespace_.c_str()); - return plugin; -} - -void PoolPlugin::configurePlugin(nvinfer1::PluginTensorDesc const* in, - int32_t nb_input, - nvinfer1::PluginTensorDesc const* out, - int32_t nb_output) noexcept { - CHECK_EQ(nb_input, 1); - CHECK_EQ(nb_output, 1); - - input_dims_ = in[0].dims; - data_format_ = in[0].format; - data_type_ = in[0].type; -} - -bool PoolPlugin::supportsFormatCombination( - int32_t pos, - nvinfer1::PluginTensorDesc const* in_out, - int32_t nb_inputs, - int32_t nb_outputs) const noexcept { - CHECK_LT(pos, nb_inputs + nb_outputs); - CHECK_NOTNULL(in_out); - - return ((in_out[pos].type == nvinfer1::DataType::kFLOAT) && - in_out[pos].format == nvinfer1::PluginFormat::kLINEAR); -} - -nvinfer1::IPluginV2* PoolPluginCreator::createPlugin( - const char* name, const nvinfer1::PluginFieldCollection* fc) noexcept { - // auto* plugin = new UffPoolPluginV2(*fc); - field_collection_ = *fc; - plugin_name_ = name; - const nvinfer1::PluginField* fields = fc->fields; - - bool ceil_mode; - PoolPlugin::PoolType pool_type; - bool adaptive; - bool exclusive; - std::vector ksize; - std::vector strides; - std::vector paddings; - std::vector real_paddings; - std::vector input_shape; - std::vector output_shape; - - // TODO(wilber): add implement. - CHECK(false) << "not implement"; - // for (int i = 0; i < fc->nbFields; ++i) { - // const char* attr_name = fields[i].name; - // if (!strcmp(attr_name, "ceil_mode")) { - // CHECK_EQ(fields[i].type == nvinfer1::PluginFieldType::kINT8, true); - // ceil_mode = *static_cast(fields[i].data); - // // mParam.numOutputBoxesPerClass = - // // *(static_cast(fields[i].data)); - // } - // } - - return nullptr; -} - -nvinfer1::IPluginV2* PoolPluginCreator::deserializePlugin( - const char* name, const void* serialData, size_t serialLength) noexcept { - auto* plugin = new PoolPlugin(serialData, serialLength); - plugin_name_ = name; - return plugin; -} - -} // namespace plugin -} // namespace tensorrt -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.h b/paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.h deleted file mode 100644 index 34189f95438..00000000000 --- a/paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.h +++ /dev/null @@ -1,196 +0,0 @@ -// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include - -#include -#include -#include - -#include "paddle/infrt/backends/tensorrt/plugin/plugin_utils.h" -#include "paddle/infrt/backends/tensorrt/trt_utils.h" - -namespace infrt { -namespace backends { -namespace tensorrt { -namespace plugin { - -static std::vector CalcOutputSize(const std::vector& input_shape, - const bool& ceil_mode, - const bool& adaptive, - const std::vector& ksize, - const std::vector& strides, - const std::vector& real_paddings) { - std::vector output_shape = input_shape; - if (adaptive) { - output_shape[0] = ksize[0]; - output_shape[1] = ksize[1]; - } else { - int output_h = 0, output_w = 0; - if (ceil_mode) { - output_h = (input_shape[0] - ksize[0] + real_paddings[0] + - real_paddings[1] + strides[0] - 1) / - strides[0] + - 1; - output_w = (input_shape[1] - ksize[1] + real_paddings[2] + - real_paddings[3] + strides[1] - 1) / - strides[1] + - 1; - } - // TRT will use native layer when ceil_model=false - /* - else{ - output_h = (input_shape[0] - ksize[0] + real_paddings[0] + - real_paddings[1]) / strides[0] + 1; - output_w = (input_shape[1] - ksize[1] + real_paddings[2] + - real_paddings[3]) / strides[1] + 1; - } - */ - output_shape[0] = output_h; - output_shape[1] = output_w; - } - return output_shape; -} - -class PoolPlugin : public nvinfer1::IPluginV2IOExt { - public: - enum class PoolType { - max = 0, - avg, - }; - - PoolPlugin() {} - PoolPlugin(bool ceil_mode, - PoolType pool_type, - bool adaptive, - bool exclusive, - std::vector ksize, - std::vector strides, - std::vector paddings, - std::vector input_shape, - std::vector real_paddings); - - PoolPlugin(void const* serialData, size_t serialLength); - - // IPluginV2 methods - const char* getPluginType() const noexcept override; - const char* getPluginVersion() const noexcept override; - int getNbOutputs() const noexcept override; - nvinfer1::Dims getOutputDimensions(int outputIndex, - const nvinfer1::Dims* inputs, - int nbInputs) noexcept override; - int32_t initialize() noexcept override; - void terminate() noexcept override; - size_t getWorkspaceSize(int32_t maxBatchSize) const noexcept override; -#if IS_TRT_VERSION_LT(8000) - int enqueue(int batchSize, - const void* const* inputs, - void** outputs, -#else - int enqueue(int batchSize, - const void* const* inputs, - void* const* outputs, -#endif - void* workspace, - cudaStream_t stream) noexcept override; - size_t getSerializationSize() const noexcept override; - void serialize(void* buffer) const noexcept override; - void destroy() noexcept override; - void setPluginNamespace(char const* pluginNamespace) noexcept override; - char const* getPluginNamespace() const noexcept override; - - // IPluginV2Ext methods - nvinfer1::DataType getOutputDataType( - int32_t index, - nvinfer1::DataType const* inputTypes, - int32_t nbInputs) const noexcept override; - bool isOutputBroadcastAcrossBatch(int32_t outputIndex, - bool const* inputIsBroadcasted, - int32_t nbInputs) const noexcept override; - bool canBroadcastInputAcrossBatch(int32_t inputIndex) const noexcept override; - // void attachToContext(cudnnContext*, - // cublasContext*, - // IGpuAllocator*) noexcept override; - // void detachFromContext() noexcept override; - IPluginV2Ext* clone() const noexcept override; - - // IPluginV2IOExt methods - void configurePlugin(nvinfer1::PluginTensorDesc const* in, - int32_t nb_input, - nvinfer1::PluginTensorDesc const* out, - int32_t nb_output) noexcept override; - bool supportsFormatCombination(int32_t pos, - nvinfer1::PluginTensorDesc const* inOut, - int32_t nb_inputs, - int32_t nb_outputs) const noexcept override; - - private: - bool ceil_mode_; - PoolType pool_type_; - bool adaptive_; - bool exclusive_; - std::vector ksize_; - std::vector strides_; - std::vector paddings_; - std::vector real_paddings_; - std::vector input_shape_; - std::vector output_shape_; - - private: - nvinfer1::Dims input_dims_; - nvinfer1::DataType data_type_; - nvinfer1::PluginFormat data_format_; - std::string namespace_; -}; - -class PoolPluginCreator : public nvinfer1::IPluginCreator { - public: - const char* getPluginName() const noexcept override { return "pool_plugin"; } - - const char* getPluginVersion() const noexcept override { return "1"; } - - const nvinfer1::PluginFieldCollection* getFieldNames() noexcept override { - return &field_collection_; - } - - nvinfer1::IPluginV2* createPlugin( - const char* name, - const nvinfer1::PluginFieldCollection* fc) noexcept override; - - nvinfer1::IPluginV2* deserializePlugin(const char* name, - const void* serialData, - size_t serialLength) noexcept override; - - void setPluginNamespace(const char* plugin_namespace) noexcept override { - plugin_namespace_ = plugin_namespace; - } - - const char* getPluginNamespace() const noexcept override { - return plugin_namespace_.c_str(); - } - - private: - std::string plugin_namespace_; - std::string plugin_name_; - nvinfer1::PluginFieldCollection field_collection_{0, nullptr}; -}; -REGISTER_TRT_PLUGIN(PoolPluginCreator); - -} // namespace plugin -} // namespace tensorrt -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/tensorrt/test_trt_engine.cc b/paddle/infrt/backends/tensorrt/test_trt_engine.cc deleted file mode 100644 index 7e081362f9c..00000000000 --- a/paddle/infrt/backends/tensorrt/test_trt_engine.cc +++ /dev/null @@ -1,420 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include -#include -#include - -#include "paddle/fluid/inference/tensorrt/plugin/split_op_plugin.h" -#include "paddle/fluid/inference/tensorrt/plugin/trt_plugin.h" -#include "paddle/fluid/memory/allocation/allocator_facade.h" -#include "paddle/fluid/memory/memcpy.h" -#include "paddle/infrt/backends/tensorrt/trt_engine.h" -#include "paddle/infrt/backends/tensorrt/trt_options.h" -#include "paddle/phi/backends/gpu/gpu_context.h" -#include "paddle/phi/common/data_type.h" -#include "paddle/phi/common/place.h" -#include "paddle/phi/core/allocator.h" -#include "paddle/phi/core/ddim.h" -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/meta_tensor.h" - -namespace infrt { -namespace backends { -namespace tensorrt { - -const char* model_input = "input_0"; -const char* model_output = "output_0"; -const char* model_output2 = "output_1"; - -TrtUniquePtr ConstructNetwork( - nvinfer1::IBuilder* builder, nvinfer1::Dims dims, bool is_static_shape) { - TrtUniquePtr network; - if (is_static_shape) { - network.reset(builder->createNetworkV2(0U)); - } else { - auto networkFlags = - 1U << static_cast( - nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH); - network.reset(builder->createNetworkV2(networkFlags)); - } - - ITensor* data = - network->addInput(model_input, nvinfer1::DataType::kFLOAT, dims); - CHECK_NOTNULL(data); - IActivationLayer* act = - network->addActivation(*data, ActivationType::kSIGMOID); - CHECK_NOTNULL(act); - auto* act_out = act->getOutput(0); - std::vector output_length{1, 2}; - int axis; - nvinfer1::IPluginV2Layer* split_layer; - if (is_static_shape) { - axis = 0; - paddle::inference::tensorrt::plugin::SplitPlugin plugin( - axis, output_length, false); - split_layer = network->addPluginV2(&act_out, 1, plugin); - } else { - axis = 1; - paddle::inference::tensorrt::plugin::SplitPluginDynamic plugin( - axis, output_length, false); - split_layer = network->addPluginV2(&act_out, 1, plugin); - } - - split_layer->getOutput(0)->setName(model_output); - split_layer->getOutput(1)->setName(model_output2); - network->markOutput(*split_layer->getOutput(0)); - network->markOutput(*split_layer->getOutput(1)); - return network; -} - -TrtUniquePtr ConstructFCNetwork( - nvinfer1::IBuilder* builder, nvinfer1::Dims dims, bool is_static_shape) { - TrtUniquePtr network; - if (is_static_shape) { - network.reset(builder->createNetworkV2(0U)); - } else { - auto networkFlags = - 1U << static_cast( - nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH); - network.reset(builder->createNetworkV2(networkFlags)); - } - - ITensor* data = - network->addInput(model_input, nvinfer1::DataType::kFLOAT, dims); - CHECK_NOTNULL(data); - nvinfer1::Weights kernel_weights; - kernel_weights.type = nvinfer1::DataType::kFLOAT; - kernel_weights.count = 7840; - std::vector weight_data(kernel_weights.count); - for (size_t i = 0; i < weight_data.size(); ++i) { - weight_data[i] = i % 255 * 0.02f; - } - kernel_weights.values = weight_data.data(); - auto* layer = network->addFullyConnected( - *data, 10, kernel_weights, nvinfer1::Weights{}); - CHECK_NOTNULL(layer); - auto* out = layer->getOutput(0); - out->setName(model_output); - network->markOutput(*out); - return network; -} - -TrtUniquePtr ConstructConvNetwork( - nvinfer1::IBuilder* builder, nvinfer1::Dims dims, bool is_static_shape) { - TrtUniquePtr network; - if (is_static_shape) { - network.reset(builder->createNetworkV2(0U)); - } else { - auto networkFlags = - 1U << static_cast( - nvinfer1::NetworkDefinitionCreationFlag::kEXPLICIT_BATCH); - network.reset(builder->createNetworkV2(networkFlags)); - } - - ITensor* data = - network->addInput(model_input, nvinfer1::DataType::kFLOAT, dims); - CHECK_NOTNULL(data); - nvinfer1::Weights kernel_weights, bias_weights; - kernel_weights.type = nvinfer1::DataType::kFLOAT; - bias_weights.type = nvinfer1::DataType::kFLOAT; - kernel_weights.count = 81; - bias_weights.count = 3; - std::vector weight_data(kernel_weights.count); - for (size_t i = 0; i < weight_data.size(); ++i) { - weight_data[i] = i * 0.02f; - } - std::vector bias_data(bias_weights.count); - for (size_t i = 0; i < bias_data.size(); ++i) { - bias_data[i] = i * 0.5f; - } - kernel_weights.values = weight_data.data(); - bias_weights.values = bias_data.data(); - nvinfer1::Dims ksize; - ksize.nbDims = 2; - ksize.d[0] = 3; - ksize.d[1] = 3; - auto* layer = - network->addConvolutionNd(*data, 3, ksize, kernel_weights, bias_weights); - CHECK_NOTNULL(layer); - auto* out = layer->getOutput(0); - out->setName(model_output); - network->markOutput(*out); - return network; -} - -// sigmoid(x) = 1 / (1 + exp(-x)) -inline float sigmoid(float x) { return 1.f / (1.f + exp(-1 * x)); } - -TEST(trt, run_fc_static) { - TrtEngine engine(0); - auto net = ConstructFCNetwork( - engine.GetTrtBuilder(), nvinfer1::Dims3{1, 28, 28}, true); - BuildOptions build_options; - build_options.max_batch = 4; - build_options.workspace = 1024; - engine.Build(std::move(net), build_options); - - InferenceOptions inference_options; - inference_options.batch = 1; - - phi::GPUPlace place; - phi::GPUContext context; - context.PartialInitWithoutAllocator(); - context.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance() - .GetAllocator(place, context.stream()) - .get()); - context.PartialInitWithAllocator(); - - phi::DenseTensorMeta meta( - phi::DataType::FLOAT32, - phi::make_ddim({inference_options.batch, 1, 28, 28})); - phi::DenseTensor input; - input.set_meta(meta); - context.Alloc(&input, input.numel() * sizeof(float)); - std::vector host_data(inference_options.batch * 1 * 28 * 28, 0); - for (size_t i = 0; i < host_data.size(); ++i) { - host_data[i] = i % 100 * 0.016f; - } - paddle::memory::Copy(place, - input.data(), - phi::CPUPlace(), - host_data.data(), - sizeof(float) * host_data.size(), - context.stream()); - - std::unordered_map inputs; - inputs.emplace(std::make_pair(model_input, &input)); - engine.PrepareOutputHandle("output_0"); - engine.SetUpInference(inference_options, inputs); - engine.GetEngineInfo(); - engine.Run(context); - cudaStreamSynchronize(context.stream()); -} - -TEST(trt, run_conv_static) { - TrtEngine engine(0); - auto net = ConstructConvNetwork( - engine.GetTrtBuilder(), nvinfer1::Dims3{3, 28, 28}, true); - BuildOptions build_options; - build_options.max_batch = 4; - build_options.workspace = 1024; - engine.Build(std::move(net), build_options); - - InferenceOptions inference_options; - inference_options.batch = 1; - - phi::GPUPlace place; - phi::GPUContext context; - context.PartialInitWithoutAllocator(); - context.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance() - .GetAllocator(place, context.stream()) - .get()); - context.PartialInitWithAllocator(); - - phi::DenseTensorMeta meta( - phi::DataType::FLOAT32, - phi::make_ddim({inference_options.batch, 3, 28, 28})); - phi::DenseTensor input; - input.set_meta(meta); - context.Alloc(&input, input.numel() * sizeof(float)); - std::vector host_data(inference_options.batch * 3 * 28 * 28, 0); - for (size_t i = 0; i < host_data.size(); ++i) { - host_data[i] = i % 100 * 0.016f; - } - paddle::memory::Copy(place, - input.data(), - phi::CPUPlace(), - host_data.data(), - sizeof(float) * host_data.size(), - context.stream()); - - std::unordered_map inputs; - inputs.emplace(std::make_pair(model_input, &input)); - engine.PrepareOutputHandle("output_0"); - engine.SetUpInference(inference_options, inputs); - engine.GetEngineInfo(); - engine.Run(context); - cudaStreamSynchronize(context.stream()); -} - -TEST(trt, run_static) { - TrtEngine static_trt_engine(0); - auto net = ConstructNetwork( - static_trt_engine.GetTrtBuilder(), nvinfer1::Dims3{3, 28, 28}, true); - BuildOptions static_build_options; - static_build_options.max_batch = 4; - static_trt_engine.Build(std::move(net), static_build_options); - InferenceOptions inference_options; - inference_options.batch = 2; - - phi::GPUPlace place; - phi::GPUContext context; - context.PartialInitWithoutAllocator(); - context.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance() - .GetAllocator(place, context.stream()) - .get()); - context.PartialInitWithAllocator(); - - phi::DenseTensorMeta meta( - phi::DataType::FLOAT32, - phi::make_ddim({inference_options.batch, 3, 28, 28})); - phi::DenseTensor input; - input.set_meta(meta); - context.Alloc(&input, input.numel() * sizeof(float)); - std::vector host_data(inference_options.batch * 3 * 28 * 28, 0); - for (size_t i = 0; i < host_data.size(); ++i) { - host_data[i] = i % 100 * 0.016f; - } - paddle::memory::Copy(place, - input.data(), - phi::CPUPlace(), - host_data.data(), - sizeof(float) * host_data.size(), - context.stream()); - - std::unordered_map inputs; - inputs.emplace(std::make_pair(model_input, &input)); - static_trt_engine.PrepareOutputHandle("output_0"); - static_trt_engine.PrepareOutputHandle("output_1"); - static_trt_engine.SetUpInference(inference_options, inputs); - static_trt_engine.GetEngineInfo(); - static_trt_engine.Run(context); - - phi::DenseTensor* output0 = static_trt_engine.GetOutput("output_0"); - phi::DenseTensor* output1 = static_trt_engine.GetOutput("output_1"); - std::vector output_data1(inference_options.batch * 1 * 28 * 28, 0); - std::vector output_data2(inference_options.batch * 2 * 28 * 28, 0); - paddle::memory::Copy(phi::CPUPlace(), - output_data1.data(), - place, - output0->data(), - sizeof(float) * output_data1.size(), - context.stream()); - paddle::memory::Copy(phi::CPUPlace(), - output_data2.data(), - place, - output1->data(), - sizeof(float) * output_data2.size(), - context.stream()); - cudaStreamSynchronize(context.stream()); - - for (size_t i = 0; i < host_data.size(); ++i) { - int w = i % 28; - int h = (i / 28) % 28; - int c = i / (28 * 28) % 3; - int n = i / (28 * 28 * 3); - if (c == 0) { - CHECK_NEAR( - sigmoid(host_data[i]), output_data1[n * 28 * 28 + h * 28 + w], 1e-5); - } else { - CHECK_NEAR(sigmoid(host_data[i]), - output_data2[n * 28 * 28 * 2 + (c - 1) * 28 * 28 + h * 28 + w], - 1e-5); - } - } -} - -TEST(trt, run_dynamic) { - TrtEngine engine(0); - auto net = ConstructNetwork( - engine.GetTrtBuilder(), nvinfer1::Dims4{-1, 3, -1, -1}, false); - BuildOptions build_options; - build_options.max_batch = 4; - build_options.workspace = 32; - // build_options.fp16 = true; - std::vector min_shape{1, 3, 16, 16}; - std::vector opt_shape{2, 3, 28, 28}; - std::vector max_shape{4, 3, 28, 28}; - build_options.shapes[model_input][0] = min_shape; - build_options.shapes[model_input][1] = opt_shape; - build_options.shapes[model_input][2] = max_shape; - engine.Build(std::move(net), build_options); - - InferenceOptions inference_options; - inference_options.batch = 2; - - phi::GPUPlace place; - phi::GPUContext context; - context.PartialInitWithoutAllocator(); - context.SetAllocator(paddle::memory::allocation::AllocatorFacade::Instance() - .GetAllocator(place, context.stream()) - .get()); - context.PartialInitWithAllocator(); - - phi::DenseTensorMeta meta( - phi::DataType::FLOAT32, - phi::make_ddim({inference_options.batch, 3, 16, 16})); - phi::DenseTensor input, output, output2; - input.set_meta(meta); - context.Alloc(&input, input.numel() * sizeof(float)); - std::vector host_data(inference_options.batch * 3 * 16 * 16, 0); - for (size_t i = 0; i < host_data.size(); ++i) { - host_data[i] = i % 100 * 0.016f; - } - paddle::memory::Copy(place, - input.data(), - phi::CPUPlace(), - host_data.data(), - sizeof(float) * host_data.size(), - context.stream()); - - std::unordered_map inputs; - inputs.emplace(std::make_pair(model_input, &input)); - engine.PrepareOutputHandle("output_0"); - engine.PrepareOutputHandle("output_1"); - engine.SetUpInference(inference_options, inputs); - engine.GetEngineInfo(); - engine.Run(context); - phi::DenseTensor* output0 = engine.GetOutput("output_0"); - phi::DenseTensor* output1 = engine.GetOutput("output_1"); - - std::vector output_data1(inference_options.batch * 1 * 16 * 16, 0); - std::vector output_data2(inference_options.batch * 2 * 16 * 16, 0); - paddle::memory::Copy(phi::CPUPlace(), - output_data1.data(), - place, - output0->data(), - sizeof(float) * output_data1.size(), - context.stream()); - paddle::memory::Copy(phi::CPUPlace(), - output_data2.data(), - place, - output1->data(), - sizeof(float) * output_data2.size(), - context.stream()); - cudaStreamSynchronize(context.stream()); - - for (size_t i = 0; i < host_data.size(); ++i) { - int w = i % 16; - int h = (i / 16) % 16; - int c = i / (16 * 16) % 3; - int n = i / (16 * 16 * 3); - if (c == 0) { - CHECK_NEAR( - sigmoid(host_data[i]), output_data1[n * 16 * 16 + h * 16 + w], 1e-5); - } else { - CHECK_NEAR(sigmoid(host_data[i]), - output_data2[n * 16 * 16 * 2 + (c - 1) * 16 * 16 + h * 16 + w], - 1e-5); - } - } -} - -} // namespace tensorrt -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/tensorrt/trt_engine.cc b/paddle/infrt/backends/tensorrt/trt_engine.cc deleted file mode 100644 index 97f36829dda..00000000000 --- a/paddle/infrt/backends/tensorrt/trt_engine.cc +++ /dev/null @@ -1,386 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/backends/tensorrt/trt_engine.h" - -#include -#include -#include - -#include "paddle/phi/backends/dynload/tensorrt.h" -#include "paddle/phi/backends/gpu/gpu_info.h" -#include "paddle/phi/core/ddim.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace backends { -namespace tensorrt { - -// The following two API are implemented in TensorRT's header file, cannot load -// from the dynamic library. So create our own implementation and directly -// trigger the method from the dynamic library. -static nvinfer1::IBuilder* createInferBuilder( - nvinfer1::ILogger& logger) { // NOLINT - return static_cast( - ::phi::dynload::createInferBuilder_INTERNAL(&logger, - NV_TENSORRT_VERSION)); -} -static nvinfer1::IRuntime* createInferRuntime( - nvinfer1::ILogger& logger) { // NOLINT - return static_cast( - ::phi::dynload::createInferRuntime_INTERNAL(&logger, - NV_TENSORRT_VERSION)); -} - -TrtEngine::TrtEngine(int device_id) : device_id_(device_id) { - FreshDeviceId(); - logger_.reset(new TrtLogger()); - builder_.reset(createInferBuilder(logger_->GetTrtLogger())); - ::phi::dynload::initLibNvInferPlugins(&logger_->GetTrtLogger(), ""); -} - -nvinfer1::IBuilder* TrtEngine::GetTrtBuilder() { - CHECK_NOTNULL(builder_); - return builder_.get(); -} - -void TrtEngine::Build(TrtUniquePtr network, - const BuildOptions& build_options) { - FreshDeviceId(); - ModelToBuildEnv(std::move(network), build_options); - CHECK_NOTNULL(engine_); -} - -bool TrtEngine::ModelToBuildEnv( - TrtUniquePtr network, - const BuildOptions& build) { - CHECK_NOTNULL(builder_); - std::swap(network, network_); - CHECK_NOTNULL(network_); - // ModelToNetwork(network_, logger); - NetworkToEngine(build); - return true; -} - -bool TrtEngine::NetworkToEngine(const BuildOptions& build) { - TrtUniquePtr config{builder_->createBuilderConfig()}; - CHECK_NOTNULL(config); - CHECK(SetupNetworkAndConfig(build, *network_, *config)); - -#if IS_TRT_VERSION_LT(8000) - engine_.reset(builder_->buildEngineWithConfig(*network_, *config)); -#else - serialized_engine_.reset( - builder_->buildSerializedNetwork(*network_, *config)); - CHECK_NOTNULL(serialized_engine_); - - TrtUniquePtr runtime{createInferRuntime(logger_->GetTrtLogger())}; - CHECK_NOTNULL(runtime); - engine_.reset(runtime->deserializeCudaEngine(serialized_engine_->data(), - serialized_engine_->size())); - CHECK_NOTNULL(engine_); -#endif - return true; -} - -bool TrtEngine::SetupNetworkAndConfig(const BuildOptions& build, - INetworkDefinition& network, - IBuilderConfig& config) { - builder_->setMaxBatchSize(build.max_batch); - // TODO(wilber): handle one engine - multi execution context case. - IOptimizationProfile* profile{nullptr}; - if (!build.shapes.empty()) { - profile = builder_->createOptimizationProfile(); - CHECK_NOTNULL(profile); - } - - // Set formats and data types of inputs - for (int32_t i = 0; i < network.getNbInputs(); ++i) { - auto* input = network.getInput(i); - if (!build.input_formats.empty()) { - input->setType(build.input_formats[i].first); - input->setAllowedFormats(build.input_formats[i].second); - } else { - switch (input->getType()) { - case DataType::kINT32: - case DataType::kBOOL: - case DataType::kHALF: - // Leave these as is. - break; - case DataType::kFLOAT: - case DataType::kINT8: - // User did not specify a floating-point format. Default to kFLOAT. - input->setType(DataType::kFLOAT); - break; - } - input->setAllowedFormats(1U << static_cast(TensorFormat::kLINEAR)); - } - - if (profile) { - Dims dims = input->getDimensions(); - // TODO(wilber): shape tensor. - const bool is_dynamic_input = std::any_of( - dims.d, dims.d + dims.nbDims, [](int dim) { return dim == -1; }); - if (is_dynamic_input) { - is_dynamic_shape_ = true; - auto shape = build.shapes.find(input->getName()); - - // If no shape is provided - if (shape == build.shapes.end()) { - // TODO(wilber): add infomation. - CHECK(false); - } - LOG(INFO) << "Run Paddle-TRT Dynamic Shape mode."; - std::vector profile_dims{}; - profile_dims = - shape->second[static_cast(OptProfileSelector::kMIN)]; - CHECK(profile->setDimensions(input->getName(), - OptProfileSelector::kMIN, - VecToDims(profile_dims))); - profile_dims = - shape->second[static_cast(OptProfileSelector::kOPT)]; - CHECK(profile->setDimensions(input->getName(), - OptProfileSelector::kOPT, - VecToDims(profile_dims))); - profile_dims = - shape->second[static_cast(OptProfileSelector::kMAX)]; - CHECK(profile->setDimensions(input->getName(), - OptProfileSelector::kMAX, - VecToDims(profile_dims))); - } - } - } - - if (profile && is_dynamic_shape_) { - CHECK(profile->isValid()); // Required optimization profile is invalid - CHECK_NE(config.addOptimizationProfile(profile), -1); - } - - // Set formats and data types of outputs - for (int32_t i = 0, n = network.getNbOutputs(); i < n; i++) { - auto* output = network.getOutput(i); - if (!build.output_formats.empty()) { - // int outputFormatIndex = broadcastOutputFormats ? 0 : i; - output->setType(build.output_formats[i].first); - output->setAllowedFormats(build.output_formats[i].second); - } else { - output->setAllowedFormats(1U << static_cast(TensorFormat::kLINEAR)); - } - } - - config.setMaxWorkspaceSize(static_cast(build.workspace) << 20); - - if (build.fp16) { - config.setFlag(BuilderFlag::kFP16); - bool support_fp16 = builder_->platformHasFastFp16(); - if (support_fp16) { - LOG(INFO) << "Run INFRT-TRT FP16 mode"; - } else { - LOG(INFO) << "You specify FP16 mode, but the hardware do not support " - "FP16 speed up, use FP32 instead."; - } - } - - if (build.tf32) { - config.setFlag(BuilderFlag::kTF32); - bool support_tf32 = builder_->platformHasTf32(); - if (support_tf32) { - LOG(INFO) << "Run INFRT-TRT TF32 mode"; - } else { - LOG(INFO) << "You specify TF32 mode, but the hardware do not support " - "TF32 speed up, use FP32 instead."; - } - } - - // TODO(wilber): other precision. - - // TODO(wilber): precision config. - switch (build.precision_constraints) { - case PrecisionConstraints::kNONE: - // It's the default for TensorRT. - break; -#if IS_TRT_VERSION_GE(8200) - case PrecisionConstraints::kOBEY: - config.setFlag(BuilderFlag::kOBEY_PRECISION_CONSTRAINTS); - break; - case PrecisionConstraints::kPREFER: - config.setFlag(BuilderFlag::kPREFER_PRECISION_CONSTRAINTS); - break; -#endif // IS_TRT_VERSION_GE(8200) - default: - break; - } - - // TODO(TRT): DLA config. - - // TODO(TRT): int8 config. - // TODO(TRT): support int8 - if (build.int8) { - assert(false); - config.setFlag(BuilderFlag::kINT8); - bool support_int8 = builder_->platformHasFastInt8(); - if (support_int8) { - LOG(INFO) << "Run INFRT-TRT FP16 mode"; - } - } - - // TODO(TRT): calib config. - - // TODO(TRT): sparse config. - - return true; -} - -void TrtEngine::PrepareOutputHandle(const std::string& out_name) { - ::Tensor t; - outputs_.emplace(out_name, t); -} - -::Tensor* TrtEngine::GetOutput(const std::string& name) { - return &outputs_[name]; -} - -size_t TrtEngine::GetOutputNum() const { return outputs_.size(); } - -bool TrtEngine::SetUpInference( - const InferenceOptions& inference, - const std::unordered_map& inputs) { - // TODO(wilber): now only create one exec_context - FreshDeviceId(); - CHECK(engine_ != nullptr); - nvinfer1::IExecutionContext* ec = engine_->createExecutionContext(); - CHECK(ec != nullptr); - contexts_.emplace_back(ec); - bindings_.emplace_back(new Bindings()); - - for (const auto& it : inputs) { - const int bind_index = engine_->getBindingIndex(it.first.c_str()); - bindings_.front()->AddBinding( - bind_index, it.first, true, it.second, nvinfer1::DataType::kFLOAT); - } - for (auto& it : outputs_) { - const int bind_index = engine_->getBindingIndex(it.first.c_str()); - bindings_.front()->AddBinding( - bind_index, it.first, false, &it.second, nvinfer1::DataType::kFLOAT); - } - - return true; -} - -void TrtEngine::Run(const ::phi::GPUContext& ctx) { - if (is_dynamic_shape_) { - DynamicRun(ctx); - } else { - StaticRun(ctx); - } -} - -void TrtEngine::StaticRun(const ::phi::GPUContext& ctx) { - const int num_bindings = engine_->getNbBindings(); - std::vector buffers(num_bindings, nullptr); - - int runtime_batch = -1; - auto input_binds = bindings_.front()->GetInputBindings(); - for (auto bind : input_binds) { - const int bind_index = engine_->getBindingIndex(bind.name.c_str()); - buffers[bind_index] = - const_cast(static_cast(bind.buffer->data())); - if (runtime_batch != -1) { - CHECK_EQ(runtime_batch, - ::phi::vectorize(bind.buffer->dims())[0]); - } - runtime_batch = bind.buffer->dims()[0]; - } - - auto output_binds = bindings_.front()->GetOutputBindings(); - for (auto bind : output_binds) { - const int bind_index = engine_->getBindingIndex(bind.name.c_str()); - std::vector ddim; - auto dims = engine_->getBindingDimensions(bind_index); - CHECK_NE(runtime_batch, -1) << "runtime_batch should not be -1."; - ddim.push_back(runtime_batch); - for (int i = 0; i < dims.nbDims; ++i) { - ddim.push_back(dims.d[i]); - } - bind.buffer->Resize(::phi::make_ddim(ddim)); - // TODO(wilber): now only support float output. - ctx.Alloc(bind.buffer, sizeof(float) * bind.buffer->numel()); - buffers[bind_index] = static_cast(bind.buffer->data()); - } - - contexts_.front()->enqueue( - runtime_batch, buffers.data(), ctx.stream(), nullptr); -} - -void TrtEngine::DynamicRun(const ::phi::GPUContext& ctx) { - const int num_bindings = engine_->getNbBindings(); - std::vector buffers(num_bindings, nullptr); - - auto input_binds = bindings_.front()->GetInputBindings(); - for (auto bind : input_binds) { - const int bind_index = engine_->getBindingIndex(bind.name.c_str()); - buffers[bind_index] = - const_cast(static_cast(bind.buffer->data())); - nvinfer1::Dims trt_dims; - trt_dims.nbDims = bind.buffer->dims().size(); - - for (int i = 0; i < trt_dims.nbDims; ++i) { - trt_dims.d[i] = bind.buffer->dims()[i]; - } - contexts_.front()->setBindingDimensions(bind_index, trt_dims); - } - - CHECK(contexts_.front()->allInputDimensionsSpecified()); - - auto output_binds = bindings_.front()->GetOutputBindings(); - for (auto bind : output_binds) { - const int bind_index = engine_->getBindingIndex(bind.name.c_str()); - auto dims = contexts_.front()->getBindingDimensions(bind_index); - std::vector ddim(dims.nbDims); - for (int i = 0; i < dims.nbDims; ++i) { - ddim[i] = dims.d[i]; - } - bind.buffer->Resize(::phi::make_ddim(ddim)); - ctx.Alloc(bind.buffer, sizeof(float) * bind.buffer->numel()); - buffers[bind_index] = static_cast(bind.buffer->data()); - } - - contexts_.front()->enqueueV2(buffers.data(), ctx.stream(), nullptr); -} - -void TrtEngine::FreshDeviceId() { - int count; - cudaGetDeviceCount(&count); - CHECK_LT(device_id_, count); - ::phi::backends::gpu::SetDeviceId(device_id_); -} - -void TrtEngine::GetEngineInfo() { -#if IS_TRT_VERSION_GE(8200) - LOG(INFO) << "====== engine info ======"; - std::unique_ptr infer_inspector( - engine_->createEngineInspector()); - infer_inspector->setExecutionContext(contexts_.front().get()); - LOG(INFO) << infer_inspector->getEngineInformation( - nvinfer1::LayerInformationFormat::kONELINE); - LOG(INFO) << "====== engine info end ======"; -#else - LOG(INFO) << "Inspector needs TensorRT version 8.2 and after."; -#endif -} - -} // namespace tensorrt -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/tensorrt/trt_engine.h b/paddle/infrt/backends/tensorrt/trt_engine.h deleted file mode 100644 index 5d7787f68a0..00000000000 --- a/paddle/infrt/backends/tensorrt/trt_engine.h +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include - -#include "paddle/infrt/backends/tensorrt/trt_options.h" -#include "paddle/infrt/backends/tensorrt/trt_utils.h" -#include "paddle/phi/backends/dynload/tensorrt.h" -#include "paddle/phi/backends/gpu/gpu_context.h" -#include "paddle/phi/common/place.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace backends { -namespace tensorrt { -using namespace nvinfer1; // NOLINT - -// The trt programing model as follows: -// 1. The build phase: -// IBuilder* builder = createInferBuilder(&logger_); -// 2. Create a network definition: -// INetworkDefinition* network = builder->createNetworkV2(...); -// 3. Build network: -// network->AddLayer(...) -// 4. Configure network: -// IBuilderConfig* config = builder->createBuilderConfig(); -// config->setMaxWorkspaceSize(...) -// 5. Get cuda engine and deserializing a plan: -// IHostMemory* serialized_model = builder->buildSerializedNetwork(...); -// IRuntime* runtime = createInferRuntime(&logger_); -// ICudaEngine* engine = runtime->deserializeCudaEngine(...); -// 6. Get execution context: -// IExecutionContext* exec_context = engine->createExecutionContext(); -// 7. Set input data: -// int32_t input_index = engine->getBindingIndex("input"); -// int32_t output_index = engine->getBindingIndex("output"); -// void* buffers[2]; -// buffers[input_index] = input_buffer; -// buffers[output_index] = output_buffer; -// 8. Performance inference: -// exec_context->enqueueV2(buffers, stream, nullptr); -// -// We have encapsulated this logic, please use the following programming model. -// -// TrtEngine trt_engine; -// trt_engine.Build(...); -// trt_engine.SetUpInference(...); -// trt_engine.Run(...); -class TrtEngine { - public: - explicit TrtEngine(int device_id = 0); - - TrtEngine(const TrtEngine&) = delete; - TrtEngine& operator=(const TrtEngine&) = delete; - TrtEngine(TrtEngine&&) = default; - TrtEngine& operator=(TrtEngine&&) = default; - - nvinfer1::IBuilder* GetTrtBuilder(); - - // TODO(wilber): Modify signature after infrt-trt ready. - void Build(TrtUniquePtr network, - const BuildOptions& build_options); - - // TODO(wilber): Modify signature after infrt-trt ready. - void Run(const ::phi::GPUContext& ctx); - - // TODO(wilber): How to support multiple execution contexts? - bool SetUpInference(const InferenceOptions& inference, - const std::unordered_map& inputs); - - void GetEngineInfo(); - - void PrepareOutputHandle(const std::string& out_name); - - // TODO(wilber): The output tensor names are: output_0, output_1, ... - ::Tensor* GetOutput(const std::string&); - - size_t GetOutputNum() const; - - private: - void FreshDeviceId(); - - bool SetupNetworkAndConfig(const BuildOptions& build, - INetworkDefinition& network, // NOLINT - IBuilderConfig& config); // NOLINT - - bool NetworkToEngine(const BuildOptions& build); - - bool ModelToBuildEnv(TrtUniquePtr network, - const BuildOptions& build); - - void StaticRun(const ::phi::GPUContext& ctx); - - void DynamicRun(const ::phi::GPUContext& ctx); - - private: - std::unique_ptr logger_{nullptr}; - TrtUniquePtr builder_{nullptr}; - TrtUniquePtr network_{nullptr}; - std::unique_ptr serialized_engine_{nullptr}; - TrtUniquePtr engine_{nullptr}; - std::vector> contexts_; - std::vector> bindings_; - int device_id_{0}; - bool is_dynamic_shape_{false}; - std::unordered_map outputs_; -}; - -} // namespace tensorrt -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/tensorrt/trt_options.h b/paddle/infrt/backends/tensorrt/trt_options.h deleted file mode 100644 index b4e36da2058..00000000000 --- a/paddle/infrt/backends/tensorrt/trt_options.h +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -#include -#include -#include - -namespace infrt { -namespace backends { -namespace tensorrt { - -// Build default params -constexpr int32_t max_batch_not_provided{0}; -constexpr int32_t default_workspace{16}; -// Inference default params -constexpr int32_t default_batch{1}; -constexpr int32_t batch_not_provided{0}; - -enum class PrecisionConstraints { kNONE, kOBEY, kPREFER }; - -enum class SparsityFlag { kDISABLE, kENABLE, kFORCE }; - -using ShapeRange = - std::array, - nvinfer1::EnumMax()>; - -using IOFormat = std::pair; - -struct BuildOptions { - // Set max batch size. - int32_t max_batch{max_batch_not_provided}; - - // Set workspace size in megabytes (default = 16) - int32_t workspace{default_workspace}; - - // Enable tf32 precision, in addition to fp32 (default = disabled) - bool tf32{false}; - - // Enable fp16 precision, in addition to fp32 (default = disabled) - bool fp16{false}; - - // Enable int8 precision, in addition to fp32 (default = disabled) - bool int8{false}; - - // Control precision constraints. (default = none) - // Precision Constaints: = none, obey, prefer - // none = no constraints - // prefer = meet precision constraints if possible - // obey = meet precision constraints or fail otherwise - PrecisionConstraints precision_constraints{PrecisionConstraints::kNONE}; - - // Save the serialized engine. - bool save{false}; - - // Load a serialized engine. - bool load{false}; - - // Build with dynamic shapes using a profile with the min, max and opt shapes - // provided - std::unordered_map shapes; - - // Type and format of each of the input tensors (default = all inputs in - // fp32:chw) - std::vector input_formats; - - // Type and format of each of the output tensors (default = all outputs in - // fp32:chw) - std::vector output_formats; -}; - -struct InferenceOptions { - int32_t batch{batch_not_provided}; - std::unordered_map> shapes; -}; - -} // namespace tensorrt -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/backends/tensorrt/trt_utils.h b/paddle/infrt/backends/tensorrt/trt_utils.h deleted file mode 100644 index e61b76e542e..00000000000 --- a/paddle/infrt/backends/tensorrt/trt_utils.h +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace backends { -namespace tensorrt { - -#define IS_TRT_VERSION_GE(version) \ - ((NV_TENSORRT_MAJOR * 1000 + NV_TENSORRT_MINOR * 100 + \ - NV_TENSORRT_PATCH * 10 + NV_TENSORRT_BUILD) >= version) - -#define IS_TRT_VERSION_LT(version) \ - ((NV_TENSORRT_MAJOR * 1000 + NV_TENSORRT_MINOR * 100 + \ - NV_TENSORRT_PATCH * 10 + NV_TENSORRT_BUILD) < version) - -#define TRT_VERSION \ - NV_TENSORRT_MAJOR * 1000 + NV_TENSORRT_MINOR * 100 + \ - NV_TENSORRT_PATCH * 10 + NV_TENSORRT_BUILD - -inline nvinfer1::Dims VecToDims(const std::vector& vec) { - int limit = static_cast(nvinfer1::Dims::MAX_DIMS); - if (static_cast(vec.size()) > limit) { - assert(false); - } - // Pick first nvinfer1::Dims::MAX_DIMS elements - nvinfer1::Dims dims; - dims.nbDims = std::min(static_cast(vec.size()), limit); - std::copy_n(vec.begin(), dims.nbDims, std::begin(dims.d)); - return dims; -} - -template -struct TrtDestroyer { - void operator()(T* t) { t->destroy(); } -}; - -template -using TrtUniquePtr = std::unique_ptr>; - -class TrtLogger : public nvinfer1::ILogger { - public: - void log(nvinfer1::ILogger::Severity severity, - const char* msg) noexcept override { - switch (severity) { - case Severity::kVERBOSE: - VLOG(3) << msg; - break; - case Severity::kINFO: - VLOG(2) << msg; - break; - case Severity::kWARNING: - LOG(WARNING) << msg; - break; - case Severity::kINTERNAL_ERROR: - case Severity::kERROR: - LOG(ERROR) << msg; - break; - default: - break; - } - } - nvinfer1::ILogger& GetTrtLogger() noexcept { return *this; } - ~TrtLogger() override = default; -}; - -struct Binding { - bool is_input{false}; - nvinfer1::DataType data_type{nvinfer1::DataType::kFLOAT}; - ::Tensor* buffer{nullptr}; - std::string name; -}; - -class Bindings { - public: - Bindings() = default; - - void AddBinding(int32_t b, - const std::string& name, - bool is_input, - ::Tensor* buffer, - nvinfer1::DataType data_type) { - while (bindings_.size() <= static_cast(b)) { - bindings_.emplace_back(); - } - names_[name] = b; - bindings_[b].buffer = buffer; - bindings_[b].is_input = is_input; - bindings_[b].data_type = data_type; - bindings_[b].name = name; - } - - std::vector GetInputBindings() { - return GetBindings([](const Binding& b) -> bool { return b.is_input; }); - } - - std::vector GetOutputBindings() { - return GetBindings([](const Binding& b) -> bool { return !b.is_input; }); - } - - std::vector GetBindings() { - return GetBindings([](const Binding& b) -> bool { return true; }); - } - - std::vector GetBindings( - std::function predicate) { - std::vector bindings; - for (const auto& b : bindings_) { - if (predicate(b)) { - bindings.push_back(b); - } - } - return bindings; - } - - private: - std::unordered_map names_; - std::vector bindings_; -}; - -} // namespace tensorrt -} // namespace backends -} // namespace infrt diff --git a/paddle/infrt/common/CMakeLists.txt b/paddle/infrt/common/CMakeLists.txt deleted file mode 100644 index c77f099aef4..00000000000 --- a/paddle/infrt/common/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -core_gather_headers() -set(core_includes - "${core_includes};infrt/common/dtype.def" - CACHE INTERNAL "") - -gather_srcs( - infrt_src - SRCS - dtype.cc - global.cc - target.cc - type.cc - shared.cc - object.cc - string.cc - buffer.cc - memory.cc) diff --git a/paddle/infrt/common/buffer.cc b/paddle/infrt/common/buffer.cc deleted file mode 100644 index bc4ec7feada..00000000000 --- a/paddle/infrt/common/buffer.cc +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/buffer.h" - -#include -#include - -#include - -namespace infrt { -void Buffer::Resize(uint32_t size) { - if (size_ > 0) { - Free(); - size_ = 0; - } - - if (size_ != size) { - data_.memory = reinterpret_cast(Malloc(size)); - size_ = size; - } -} - -void Buffer::Resize(uint32_t alignment, uint32_t size) { - if (size_ > 0) { - Free(); - size_ = 0; - } - - if (size_ != size) { - data_.memory = reinterpret_cast(AlignedAlloc(alignment, size)); - size_ = size; - } -} - -void Buffer::SetTarget(const infrt::common::Target& target) { - target_ = target; - memory_mng_cache_ = MemoryManager::Global().RetrieveSafely(target_.arch); -} - -void Buffer::ResizeLazy(uint32_t size) { - if (size <= size_) return; - Resize(size); -} - -void Buffer::ResizeLazy(uint32_t alignment, uint32_t size) { - if (size <= size_) return; - Resize(alignment, size); -} - -void Buffer::Resize(uint32_t size, const infrt::common::Target& target) { - if (target.arch != target_.arch) { - Free(); - SetTarget(target); - } - Resize(size); -} - -void Buffer::Resize(uint32_t alignment, - uint32_t size, - const infrt::common::Target& target) { - if (target.arch != target_.arch) { - Free(); - SetTarget(target); - } - Resize(alignment, size); -} - -void Buffer::ResizeLazy(uint32_t size, const infrt::common::Target& target) { - if (target.arch != target_.arch) { - Free(); - SetTarget(target); - } - ResizeLazy(size); -} - -void Buffer::ResizeLazy(uint32_t alignment, - uint32_t size, - const infrt::common::Target& target) { - if (target.arch != target_.arch) { - Free(); - SetTarget(target); - } - ResizeLazy(alignment, size); -} - -} // namespace infrt diff --git a/paddle/infrt/common/buffer.h b/paddle/infrt/common/buffer.h deleted file mode 100644 index cae2a7ead96..00000000000 --- a/paddle/infrt/common/buffer.h +++ /dev/null @@ -1,296 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -#include - -#include "paddle/infrt/common/macros.h" -#include "paddle/infrt/common/memory.h" -#include "paddle/infrt/common/target.h" - -namespace infrt { - -#ifdef __cplusplus -extern "C" { -#endif - -#define INFRT_ALWAYS_INLINE __attribute__((always_inline)) inline - -//! Code for the primitive types supported in INFRT. -typedef enum infrt_type_code_t { - infrt_type_unk = -1, //! Unknown type - infrt_type_int = 0, //! signed int - infrt_type_uint = 1, //! unsigned int - infrt_type_float = 2, //! floating point - infrt_type_handle = 3 //! void* -} infrt_type_code_t; - -#ifndef INFRT_ATTRIBUTE_ALIGN -#define INFRT_ATTRIBUTE_ALIGN(n) __attribute__((aligned(n))) -#endif - -/** - * A tuntime tag for type in INFRT system. - */ -typedef struct infrt_type_t { -#if __cplusplus >= 201103L - INFRT_ATTRIBUTE_ALIGN(1) infrt_type_code_t code; -#else - uint8_t code; -#endif - - //! Number of bits. - uint8_t bits; - - //! Number of elements in a vector, 1 for scalar. - uint16_t lanes; - - //! Number of '*', e.g. for `float*`, the num_asterisks is 1, `float**` it is - //! 2. - uint8_t num_asterisks{0}; - -#ifdef __cplusplus - INFRT_ALWAYS_INLINE infrt_type_t() - : code(infrt_type_int), bits(0), lanes(0) {} - INFRT_ALWAYS_INLINE infrt_type_t(infrt_type_code_t code, - uint8_t bits, - uint16_t lanes = 1, - uint8_t num_asterisks = 0) - : code(code), bits(bits), lanes(lanes), num_asterisks(num_asterisks) {} - INFRT_ALWAYS_INLINE bool operator==(const infrt_type_t& other) const { - return code == other.code && bits == other.bits && lanes == other.lanes; - } - INFRT_ALWAYS_INLINE bool operator!=(const infrt_type_t& other) const { - return !(*this == other); - } - INFRT_ALWAYS_INLINE uint16_t bytes() const { return (bits + 7) / 8; } -#endif // __cplusplus -} infrt_type_t; - -//! Help to define the size of a dimension, due to polyhedral representation, we -//! no need to record the extend or -//! min(default to 0). -typedef int infrt_dimension_t; - -//! Help to tell the kind of the device. -typedef enum infrt_device_kind_t { - infrt_unk_device = -1, // Undefined device. - infrt_x86_device = 0, // X86 device - infrt_opencl_device = 1, // OpenCL device - infrt_arm_device = 2 // ARM device -} infrt_device_kind_t; - -struct infrt_buffer_t; - -/** - * All INFRT backends implementation should provide an interface to be used. - */ -struct infrt_device_interface_impl_t; - -struct infrt_device_interface_t { - int (*malloc)(void* context, struct infrt_buffer_t* buf); - int (*free)(void* context, struct infrt_buffer_t* buf); - int (*sync)(void* context, struct infrt_buffer_t* buf); - int (*release)(void* context, - const struct infrt_device_interface_t* device_interface); - int (*copy_to_host)(void* context, struct infrt_buffer_t* buf); - int (*copy_to_device)(void* context, struct infrt_buffer_t* buf); - int (*buffer_copy)(void* context, - struct infrt_buffer_t* src, - struct infrt_buffer_t* dst); - struct infrt_device_interface_impl_t* impl; -}; - -//! The raw representation of a buffer,used in the generated code/lib. -#define INFRT_BUFFER_MAX_DIMS 8 -typedef struct infrt_buffer_t { - //! Tell which kind of device this buffer locates. - infrt_device_kind_t device; - - //! The interface used to operate on device. - const struct infrt_device_interface_t* device_interface; - - //! A pointer to the memory in host. - uint8_t* memory; - - //! Extra flags. - uint64_t flag; - - //! Data type. - infrt_type_t type; - - //! Number of dimensions. - int32_t dimensions; - infrt_dimension_t dims[INFRT_BUFFER_MAX_DIMS]; - - //! Allocate and deallocate lazily, default true. - char lazy; - - //! The actual memory size(in bytes). - uint64_t memory_size; - - uint16_t align; - -#ifdef __cplusplus - infrt_buffer_t() - : device(infrt_unk_device), - device_interface(NULL), - memory(NULL), - flag(0UL), - type(infrt_type_t()), - dimensions(0), - lazy(true), - memory_size(0), - align(0) {} - - static void delete_(struct infrt_buffer_t* x) { delete x; } - - ~infrt_buffer_t() {} - - // NOTE the buffer should be resized first. - static void alloc(struct infrt_buffer_t*); - - //! Set the shape of the buffer. NOTE this just record the shape, not allocate - //! the memory. - INFRT_ALWAYS_INLINE void resize(const infrt_dimension_t* dims, - int dimensions) { - this->dimensions = dimensions; - memcpy(this->dims, dims, dimensions * sizeof(infrt_dimension_t)); - } - - INFRT_ALWAYS_INLINE uint64_t num_elements() const { - uint64_t res = 1; - for (int i = 0; i < dimensions; i++) { - res *= dims[i]; - } - return res; - } - - INFRT_ALWAYS_INLINE int device_sync(void* ctx = NULL) { - if (device_interface && device_interface->sync) { - return device_interface->sync(ctx, this); - } - return 0; - } - - INFRT_ALWAYS_INLINE uint8_t* begin() const { return 0; } - INFRT_ALWAYS_INLINE uint8_t* end() const { - return memory + num_elements() * type.bytes(); - } - -#endif // __cplusplus -} infrt_buffer_t; - -#ifdef __cplusplus -struct infrt_device_interface_impl_t { - int (*malloc)(void* context, struct infrt_buffer_t* buf); - int (*free)(void* context, struct infrt_buffer_t* buf); - int (*sync)(void* context, struct infrt_buffer_t* buf); - int (*release)(void* context); - int (*copy_to_host)(void* context, struct infrt_buffer_t* buf); - int (*copy_to_device)(void* context, struct infrt_buffer_t* buf); - int (*buffer_copy)(void* context, - struct infrt_buffer_t* src, - struct infrt_buffer_t* dst); -}; - -// The device implementations -extern struct infrt_device_interface_t* infrt_x86_device_interface(); -#endif // __cplusplus - -#ifdef __cplusplus -} // extern "C" -#endif - -#define INFRT_LOG(fmt, ...) \ - do { \ - fprintf(stderr, \ - "%s:%d:%s(): " fmt, \ - __FILE__, \ - __LINE__, \ - __func__, \ - __VA_ARGS__); \ - } while (0) - -#define INFRT_CHECK(cond) \ - if (!(cond)) { \ - INFRT_LOG("check %s failed", #cond); \ - abort(); \ - } -/** - * Buffer helps to hold the memory, and offers a set of methods to help manage - * the memory. - */ -struct Buffer final { - Buffer() = default; - explicit Buffer(const common::Target& target) { SetTarget(target); } - - //! Resize the memory hold by this buffer *exactlly* to \p size. - void Resize(uint32_t size); - void Resize(uint32_t alignment, uint32_t size); - - //! Lazily resize the memory. - void ResizeLazy(uint32_t size); - void ResizeLazy(uint32_t alignment, uint32_t size); - - //! Resize the memory to \p size in target \p target. - void Resize(uint32_t size, const common::Target& target); - void Resize(uint32_t alignment, uint32_t size, const common::Target& target); - - //! Lazily resize the memory to \p size in target \p target. - void ResizeLazy(uint32_t size, const common::Target& target); - void ResizeLazy(uint32_t alignment, - uint32_t size, - const common::Target& target); - - void SetTarget(const common::Target& target); - - const infrt_buffer_t* data() const { return &data_; } - infrt_buffer_t* data() { return &data_; } - - //! Free all the memory owned by this buffer. - void Free() { - if (!data_.memory) return; - memory_mng_cache_->free(data_.memory); - } - - private: - inline void* Malloc(uint32_t size) INFRT_RESULT_SHOULD_USE { - CHECK(memory_mng_cache_) << "Should set target first"; - return memory_mng_cache_->malloc(size); - } - - inline void* AlignedAlloc(uint32_t alignment, - uint32_t size) INFRT_RESULT_SHOULD_USE { - CHECK(memory_mng_cache_) << "Should set target first"; - return memory_mng_cache_->aligned_alloc(alignment, size); - } - - private: - infrt_buffer_t data_; - - //! The place where this buffer locates. - common::Target target_; - - //! Number of bytes of this buffer. - uint32_t size_{}; - - //! Hold the corresponding memory manager for speed. - MemoryInterface* memory_mng_cache_{}; -}; - -} // namespace infrt diff --git a/paddle/infrt/common/common.h b/paddle/infrt/common/common.h deleted file mode 100644 index a15bc69b603..00000000000 --- a/paddle/infrt/common/common.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -#include "paddle/infrt/common/macros.h" -#include "paddle/infrt/common/shared.h" -#include "paddle/infrt/common/target.h" -#include "paddle/infrt/common/type.h" - -namespace infrt { - -// export some general concepts. -using common::make_shared; -using common::Object; -using common::ref_count; -using common::Shared; - -// Type related. -using common::Bool; -using common::Float; -using common::Int; -using common::UInt; -using common::Void; - -using common::type_of; - -using common::Target; -using common::Type; -using common::UnkTarget; - -template -T& Reference(const T* x) { - return *const_cast(x); -} - -static void CheckVarNameValid(const std::string& name) { - CHECK(!name.empty()); - CHECK(name.find(' ') == std::string::npos && // - name.find('.') == std::string::npos && // - name.find('/') == std::string::npos && // - name.find('\t') == std::string::npos && // - name.find('\n') == std::string::npos && // - name.find('\r') == std::string::npos) - << "Some invalid character found"; -} - -} // namespace infrt diff --git a/paddle/infrt/common/dtype.cc b/paddle/infrt/common/dtype.cc deleted file mode 100644 index d5cf67d8a3c..00000000000 --- a/paddle/infrt/common/dtype.cc +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/dtype.h" - -namespace infrt { - -const char* DType::name() const { - switch (kind_) { - case Kind::Unk: - return "Unk"; - break; -#define INFRT_DTYPE(enum__, value__) \ - case Kind::enum__: \ - return #enum__; \ - break; -#include "paddle/infrt/common/dtype.def" -#undef INFRT_DTYPE - } - - return ""; -} - -size_t DType::GetHostSize() const { - switch (kind_) { -#define INFRT_DTYPE(enum__, value__) \ - case DType::Kind::enum__: \ - return sizeof(DTypeInternal::type); -#include "paddle/infrt/common/dtype.def" // NOLINT -#undef INFRT_DTYPE - - case Kind::Unk: - return 0; - break; - } - return 0; -} - -} // namespace infrt diff --git a/paddle/infrt/common/dtype.def b/paddle/infrt/common/dtype.def deleted file mode 100644 index 32df72aa764..00000000000 --- a/paddle/infrt/common/dtype.def +++ /dev/null @@ -1,18 +0,0 @@ -// Define all INFRT dtypes -// DTYPE(ENUM, VALUE) -#ifdef INFRT_DTYPE - -INFRT_DTYPE(UI8, 1) -INFRT_DTYPE(UI16, 2) -INFRT_DTYPE(UI32, 3) -INFRT_DTYPE(UI64, 4) -INFRT_DTYPE(I1, 5) -INFRT_DTYPE(I8, 6) -INFRT_DTYPE(I16, 7) -INFRT_DTYPE(I32, 8) -INFRT_DTYPE(I64, 9) -INFRT_DTYPE(F32, 10) -INFRT_DTYPE(F64, 11) -INFRT_DTYPE(STRING, 12) - -#endif \ No newline at end of file diff --git a/paddle/infrt/common/dtype.h b/paddle/infrt/common/dtype.h deleted file mode 100644 index 8b57299fa94..00000000000 --- a/paddle/infrt/common/dtype.h +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include -#include - -namespace infrt { -class DType { - public: - enum class Kind : uint8_t { - Unk = 0, - -// Automatically generate the enum definition -#define INFRT_DTYPE(enum__, value__) enum__ = value__, -#include "paddle/infrt/common/dtype.def" -#undef INFRT_DTYPE - - BOOL = I1, - }; - - DType() = default; - explicit constexpr DType(Kind kind) : kind_(kind) { assert(IsValid()); } - - DType(const DType&) = default; - DType& operator=(const DType&) = default; - bool operator==(DType other) const { return kind_ == other.kind_; } - bool operator!=(DType other) const { return !(*this == other); } - - constexpr Kind kind() const { return kind_; } - - bool IsValid() const { return kind_ != Kind::Unk; } - bool IsInvalid() const { return !IsValid(); } - - const char* name() const; - - size_t GetHostSize() const; - - private: - Kind kind_{Kind::Unk}; -}; - -template -constexpr DType GetDType(); - -template -struct DTypeInternal; - -#define INFRT_IMPL_GET_DTYPE(cpp_type__, enum__) \ - template <> \ - inline constexpr DType GetDType() { \ - return DType{DType::Kind::enum__}; \ - } \ - template <> \ - struct DTypeInternal { \ - using type = cpp_type__; \ - }; - -INFRT_IMPL_GET_DTYPE(bool, I1); -INFRT_IMPL_GET_DTYPE(int8_t, I8); -INFRT_IMPL_GET_DTYPE(int16_t, I16); -INFRT_IMPL_GET_DTYPE(int32_t, I32); -INFRT_IMPL_GET_DTYPE(int64_t, I64); -INFRT_IMPL_GET_DTYPE(uint8_t, UI8); -INFRT_IMPL_GET_DTYPE(uint16_t, UI16); -INFRT_IMPL_GET_DTYPE(uint32_t, UI32); -INFRT_IMPL_GET_DTYPE(uint64_t, UI64); -INFRT_IMPL_GET_DTYPE(float, F32); -INFRT_IMPL_GET_DTYPE(double, F64); -INFRT_IMPL_GET_DTYPE(std::string, STRING); - -} // namespace infrt diff --git a/paddle/infrt/common/global.cc b/paddle/infrt/common/global.cc deleted file mode 100644 index 54ecf1589aa..00000000000 --- a/paddle/infrt/common/global.cc +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/global.h" - -namespace infrt { - -Global::Global() {} - -mlir::MLIRContext* Global::context = nullptr; - -mlir::MLIRContext* Global::getMLIRContext() { - if (nullptr == context) { - context = new mlir::MLIRContext(); - } - return context; -} - -} // namespace infrt diff --git a/paddle/infrt/common/global.h b/paddle/infrt/common/global.h deleted file mode 100644 index 2d7735d5252..00000000000 --- a/paddle/infrt/common/global.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -#include "paddle/infrt/tensor/dense_host_tensor.h" - -namespace infrt { - -// global variables -class Global { - private: - static mlir::MLIRContext *context; - Global(); - - public: - static mlir::MLIRContext *getMLIRContext(); -}; // class Global - -} // namespace infrt diff --git a/paddle/infrt/common/macros.h b/paddle/infrt/common/macros.h deleted file mode 100644 index 4481f6b38ae..00000000000 --- a/paddle/infrt/common/macros.h +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#if !defined(NDEBUG) -#define INFRT_DEBUG -#endif - -#define INFRT_DISALLOW_COPY_AND_ASSIGN(TypeName) \ - TypeName(const TypeName&) = delete; \ - void operator=(const TypeName&) = delete - -#ifndef INFRT_NOT_IMPLEMENTED -#define INFRT_NOT_IMPLEMENTED LOG(FATAL) << "Not Implemented"; -#endif - -#define INFRT_RESULT_SHOULD_USE __attribute__((warn_unused_result)) - -/** - * A trick to enforce the registry. - * - * usage: - * - * INFRT_REGISTER_HELPER(some_key) { - * // register methods - * } - * - * INFRT_USE_REGISTER(some_key); - */ -#define INFRT_REGISTER_HELPER(symbol__) bool __infrt__##symbol__##__registrar() -#define INFRT_USE_REGISTER(symbol__) \ - extern bool __infrt__##symbol__##__registrar(); \ - [[maybe_unused]] static bool __infrt_extern_registrar_##symbol__ = \ - __infrt__##symbol__##__registrar(); - -#if __cplusplus >= 201703L -#define INFRT_NODISCARD [[nodiscard]] -#else -#define INFRT_NODISCARD -#endif diff --git a/paddle/infrt/common/memory.cc b/paddle/infrt/common/memory.cc deleted file mode 100644 index aa5983a56c4..00000000000 --- a/paddle/infrt/common/memory.cc +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/memory.h" - -namespace infrt { - -using infrt::common::Target; - -namespace { - -class X86MemoryMng : public MemoryInterface { - public: - void* malloc(size_t nbytes) override { return ::malloc(nbytes); } - void free(void* data) override { - if (!data) return; - ::free(data); - } - void* aligned_alloc(size_t alignment, size_t nbytes) override { - return ::aligned_alloc(alignment, nbytes); - } -}; - -} // namespace - -MemoryManager::MemoryManager() { - Register(Target::Arch::Unk, new X86MemoryMng); - Register(Target::Arch::X86, new X86MemoryMng); -} - -} // namespace infrt diff --git a/paddle/infrt/common/memory.h b/paddle/infrt/common/memory.h deleted file mode 100644 index 643b2147761..00000000000 --- a/paddle/infrt/common/memory.h +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -#include -#include - -#include "paddle/infrt/common/macros.h" -#include "paddle/infrt/common/target.h" - -namespace infrt { - -class MemoryInterface { - public: - virtual void* malloc(size_t nbytes) = 0; - virtual void free(void* data) = 0; - virtual void* aligned_alloc(size_t alignment, size_t nbytes) { - return nullptr; - } - virtual ~MemoryInterface() {} -}; - -/** - * MemoryManager holds a map of MemoryInterface for each articture. - */ -class MemoryManager final { - public: - using key_t = common::Target::Arch; - - static MemoryManager& Global() { - static auto* x = new MemoryManager; - return *x; - } - - MemoryInterface* Retrieve(key_t key) INFRT_RESULT_SHOULD_USE { - auto it = memory_mngs_.find(key); - if (it != memory_mngs_.end()) return it->second.get(); - return nullptr; - } - - MemoryInterface* RetrieveSafely(key_t key) { - auto* res = Retrieve(key); - CHECK(res) << "no MemoryInterface for architecture [" << key << "]"; - return res; - } - - MemoryInterface* Register(key_t key, MemoryInterface* item) { - CHECK(!memory_mngs_.count(key)) << "Duplicate register [" << key << "]"; - memory_mngs_[key].reset(item); - return item; - } - - private: - MemoryManager(); - - std::unordered_map> - memory_mngs_; - - INFRT_DISALLOW_COPY_AND_ASSIGN(MemoryManager); -}; - -} // namespace infrt diff --git a/paddle/infrt/common/object.cc b/paddle/infrt/common/object.cc deleted file mode 100644 index 6842ff7ba00..00000000000 --- a/paddle/infrt/common/object.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/object.h" - -namespace infrt { -namespace common {} // namespace common -} // namespace infrt diff --git a/paddle/infrt/common/object.h b/paddle/infrt/common/object.h deleted file mode 100644 index 797595cc7c5..00000000000 --- a/paddle/infrt/common/object.h +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include - -#include "paddle/infrt/common/shared.h" - -namespace infrt { -namespace common { - -template -class Shared; -/** - * Object is the basic element in the INFRT, with `Shared` wrapper, the object - * can be shared across the system. - */ -struct Object { - //! Get the type representation of this object. - virtual const char* type_info() const = 0; - virtual ~Object() {} - - //! Cast to a derived type. - template - T* as() { - return static_cast(this); - } - - //! Cast to a derived type. - template - const T* as() const { - return static_cast(this); - } - - //! Type safe cast. - template - T* safe_as() { - if (std::strcmp(type_info(), T::__type_info__) == 0) { - return static_cast(this); - } - return nullptr; - } - //! Type safe cast. - template - const T* safe_as() const { - if (std::strcmp(type_info(), T::__type_info__) == 0) { - return static_cast(this); - } - return nullptr; - } - - //! Check if the type is right. - template - bool is_type() const { - if (std::strcmp(type_info(), T::__type_info__) == 0) { - return true; - } - return false; - } - - //! The reference count, which make all the derived type able to share. - mutable RefCount __ref_count__; -}; - -using object_ptr = Object*; -using shared_object = Shared; - -} // namespace common -} // namespace infrt diff --git a/paddle/infrt/common/shared.cc b/paddle/infrt/common/shared.cc deleted file mode 100644 index 78457b7ed35..00000000000 --- a/paddle/infrt/common/shared.cc +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/shared.h" diff --git a/paddle/infrt/common/shared.h b/paddle/infrt/common/shared.h deleted file mode 100644 index dbcf2b05978..00000000000 --- a/paddle/infrt/common/shared.h +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include - -namespace infrt { -namespace common { - -class RefCount { - public: - using value_type = int32_t; - RefCount() = default; - - value_type Inc() { return ++count_; } - value_type Dec() { return --count_; } - bool is_zero() const { return 0 == count_; } - std::string to_string() { return std::to_string(count_.load()); } - int32_t val() const { return count_; } - - private: - std::atomic count_{0}; -}; - -class Object; -/** - * The templated methods are used to unify the way to get the RefCount instance - * in client classes. - */ -template -RefCount& ref_count(const T* t) { - static_assert(std::is_base_of::value, "T is not a Object"); - return t->__ref_count__; -} -template -void Destroy(const T* t) { - delete t; -} - -template -struct Shared { - using object_ptr = T*; - - Shared() = default; - explicit Shared(T* p) : p_(p) { - if (p) IncRef(p); - } - Shared(const Shared& other) : p_(other.p_) { IncRef(p_); } - Shared(Shared&& other) : p_(other.p_) { other.p_ = nullptr; } - Shared& operator=(const Shared& other); - - //! Reset to another pointer \p x. - void Reset(T* x = nullptr); - - //! Access the pointer in various ways. - // @{ - inline T* get() const { return p_; } - inline T& operator*() const { return *p_; } - inline T* operator->() const { return p_; } - inline T* self() { return p_; } - inline const T* self() const { return p_; } - // @} - - inline bool same_as(const Shared& other) { return p_ == other.p_; } - inline bool defined() const { return p_; } - inline bool operator<(const Shared& other) const { return p_ < other.p_; } - inline Shared& operator=(T* x); - inline bool operator==(const Shared& other) const { return p_ == other.p_; } - - ~Shared(); - - private: - //! Increase the share count. - void IncRef(T* p); - - //! Decrease the share count. - void DecRef(T* p); - - protected: - T* p_{}; -}; - -template -void Shared::IncRef(T* p) { - if (p) { - ref_count(p).Inc(); - } -} -template -void Shared::DecRef(T* p) { - if (p) { - if (ref_count(p).Dec() == 0) { - Destroy(p); - } - } -} -template -Shared& Shared::operator=(const Shared& other) { - if (other.p_ == p_) return *this; - // Other can be inside of something owned by this, so we should be careful to - // incref other before we decref - // ourselves. - T* tmp = other.p_; - IncRef(tmp); - DecRef(p_); - p_ = tmp; - return *this; -} - -template -T* make_shared(Args&&... args) { - return new T(args...); -} - -template -Shared& Shared::operator=(T* x) { - if (p_ == x) return *this; - - T* tmp = x; - IncRef(tmp); - DecRef(p_); - p_ = tmp; - return *this; -} - -template -Shared::~Shared() { - DecRef(p_); - p_ = nullptr; -} - -template -void Shared::Reset(T* x) { - if (x) IncRef(x); - DecRef(p_); - p_ = x; -} - -} // namespace common -} // namespace infrt diff --git a/paddle/infrt/common/string.cc b/paddle/infrt/common/string.cc deleted file mode 100644 index d02643825a7..00000000000 --- a/paddle/infrt/common/string.cc +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/string.h" - -#include - -#include - -namespace infrt { -namespace infrt { - -std::string StringFormat(const std::string &fmt_str, ...) { - /* Reserve two times as much as the length of the fmt_str */ - int final_n, n = (static_cast(fmt_str.size())) * 2; - std::unique_ptr formatted; - va_list ap; - while (1) { - formatted.reset( - new char[n]); /* Wrap the plain char array into the unique_ptr */ - std::strcpy(&formatted[0], fmt_str.c_str()); // NOLINT - va_start(ap, fmt_str); - final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap); - va_end(ap); - if (final_n < 0 || final_n >= n) - n += abs(final_n - n + 1); - else - break; - } - return std::string(formatted.get()); -} - -std::string Trim(const std::string &s, const char *empty) { - if (s.empty()) return s; - auto start = s.find_first_not_of(empty); - if (start == std::string::npos) return ""; - auto end = s.find_last_not_of(empty); - return s.substr(start, end - start + 1); -} - -std::string Uppercase(const std::string &x) { - auto res = x; - for (auto &c : res) { - c = toupper(c); - } - return res; -} - -bool Startswith(const std::string &x, const std::string &str) { - return x.find(str) == 0; -} -bool Endswith(const std::string &x, const std::string &str) { - if (x.length() >= str.length()) { - return std::equal(str.rbegin(), str.rend(), x.rbegin()); - } - return false; -} - -std::vector Split(const std::string &str, - const std::string &splitter) { - std::vector results; - std::string::size_type pos1, pos2; - pos2 = str.find(splitter); - pos1 = 0; - while (std::string::npos != pos2) { - results.push_back(str.substr(pos1, pos2 - pos1)); - pos1 = pos2 + splitter.size(); - pos2 = str.find(splitter, pos1); - } - if (pos1 != str.length()) { - results.push_back(str.substr(pos1)); - } - return results; -} - -void Replace(std::string *s, const std::string &from, const std::string &to) { - size_t pos = 0; - while ((pos = s->find(from, pos)) != std::string::npos) { - s->replace(pos, from.size(), to); - pos += to.length(); - } -} - -size_t Count(std::string *s, const std::string &sub) { - size_t pos = 0; - size_t times = 0; - while ((pos = s->find(sub, pos)) != std::string::npos) { - if ((pos == 0 || !IsPrefix(s->at(pos - 1))) && - (pos + sub.length() == s->size() || - !IsSuffix(s->at(pos + sub.length())))) { - pos += sub.length(); - times++; - } else { - pos++; - } - } - return times; -} - -bool IsPrefix(const char &c) { - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_'); -} - -bool IsSuffix(const char &c) { - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_') || - (c >= '0' && c <= '9') || (c == '\''); -} - -std::string TransValidVarName(std::string name) { - Replace(&name, ".", "__"); - Replace(&name, "/", "___"); - name.erase(0, name.find_first_not_of("_")); - return name; -} - -} // namespace infrt -} // namespace infrt diff --git a/paddle/infrt/common/string.h b/paddle/infrt/common/string.h deleted file mode 100644 index f744470603f..00000000000 --- a/paddle/infrt/common/string.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include -#include - -namespace infrt { -namespace infrt { - -//! Get the content of a stream. -template -std::string GetStreamCnt(const T& x); - -/** - * Construct a formatted string with arguments. - * @param fmt_str The format. - * @param ... The parameters of the format. - * @return The formated string. - */ -std::string StringFormat(const std::string& fmt_str, ...); - -/** - * Join multiple fields to a single string. Similar to Python's str.join method. - */ -template -std::string Join(const std::vector& fields, const std::string& splitter) { - if (fields.empty()) return ""; - std::stringstream ss; - for (int i = 0; i < fields.size() - 1; i++) ss << fields[i] << splitter; - ss << fields.back(); - return ss.str(); -} - -std::vector Split(const std::string& str, - const std::string& splitter); - -std::string Trim(const std::string& s, const char* empty = " \n\r\t"); - -//! Convert a string to its uppercase. -std::string Uppercase(const std::string& x); - -//! Replace a substr 'from' to 'to' in string s. -void Replace(std::string* s, const std::string& from, const std::string& to); - -//! Count how many times substr 'sub' appears in string s. -size_t Count(std::string* s, const std::string& sub); - -//! Tell if a char is prefix of a tensor's name. -bool IsPrefix(const char& c); - -//! Tell if a char is suffix of a tensor's name. -bool IsSuffix(const char& c); - -//! Tell if a string \p x start with \p str. -bool Startswith(const std::string& x, const std::string& str); - -//! Tell if a string \p x ends with \p str. -bool Endswith(const std::string& x, const std::string& str); - -template -std::string GetStreamCnt(const T& x) { - std::stringstream os; - os << x; - return os.str(); -} - -std::string TransValidVarName(std::string name); - -} // namespace infrt -} // namespace infrt diff --git a/paddle/infrt/common/target.cc b/paddle/infrt/common/target.cc deleted file mode 100644 index d376ad7db02..00000000000 --- a/paddle/infrt/common/target.cc +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/target.h" - -#include - -namespace infrt { -namespace common { - -bool Target::operator==(const Target &other) const { - return os == other.os && // - arch == other.arch && // - bits == other.bits && // - features == other.features; -} - -int Target::max_num_threads() const { - CHECK(arch == Arch::NVGPU) - << "The target is not NVGPU! Cannot get max number of threads."; - return 1024; -} - -std::vector Target::get_target_libs() const { return libs; } - -int Target::get_target_bits() const { - switch (bits) { - case Bit::k32: - return 32; - case Bit::k64: - return 64; - case Bit::Unk: - return 0; - default: - LOG(FATAL) << "Not supported Bit"; - } - return -1; -} - -std::ostream &operator<<(std::ostream &os, const Target &target) { - os << "Target<"; - switch (target.os) { - case Target::OS::Linux: - os << "linux"; - break; - case Target::OS::Windows: - os << "windows"; - break; - case Target::OS::Unk: - os << "unk"; - break; - } - - os << ","; - - switch (target.arch) { - case Target::Arch::X86: - os << "x86"; - break; - case Target::Arch::ARM: - os << "arm"; - break; - case Target::Arch::NVGPU: - os << "nvgpu"; - break; - case Target::Arch::Unk: - os << "unk"; - break; - } - os << ","; - - switch (target.bits) { - case Target::Bit::k32: - os << "32"; - break; - case Target::Bit::k64: - os << "64"; - break; - case Target::Bit::Unk: - os << "unk"; - break; - } - os << ">"; - - return os; -} - -std::ostream &operator<<(std::ostream &os, Target::Arch arch) { - switch (arch) { - case Target::Arch::Unk: - os << "Unk"; - break; - case Target::Arch::X86: - os << "X86"; - break; - case Target::Arch::ARM: - os << "ARM"; - break; - case Target::Arch::NVGPU: - os << "NVGPU"; - break; - } - return os; -} - -} // namespace common -} // namespace infrt diff --git a/paddle/infrt/common/target.h b/paddle/infrt/common/target.h deleted file mode 100644 index eaf19efbfe7..00000000000 --- a/paddle/infrt/common/target.h +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include - -namespace infrt { -namespace common { - -struct Target { - /** - * The operating system used by the target. Determines which system calls to - * generate. - */ - enum class OS : int { - Unk = -1, - Linux, - Windows, - }; - - /** - * The architecture used by the target. Determines the instruction set to use. - */ - enum class Arch : int { - Unk = -1, - X86, - ARM, - NVGPU, - }; - - enum class Bit : int { - Unk = -1, - k32, - k64, - }; - - OS os{OS::Unk}; - Arch arch{Arch::Unk}; - Bit bits{Bit::Unk}; - - enum class Feature : int { - JIT = 0, - Debug, - }; - - /** - * The library used by the target. - */ - enum class Lib : int { - Unk = -1, - MKL, - }; - std::vector features; - std::vector libs; - - explicit Target(OS o = OS::Linux, - Arch a = Arch::Unk, - Bit b = Bit::Unk, - const std::vector& features = {}, - const std::vector& libs = {}) - : os(o), arch(a), bits(b), features(features), libs(libs) {} - - bool defined() const { - return os != OS::Unk && arch != Arch::Unk && bits != Bit::Unk; - } - - int max_num_threads() const; - - int get_target_bits() const; - - std::vector get_target_libs() const; - - bool operator==(const Target& other) const; - bool operator!=(const Target& other) const { return !(*this == other); } - friend std::ostream& operator<<(std::ostream& os, const Target& target); -}; - -static const Target& UnkTarget() { - static Target target( - Target::OS::Unk, Target::Arch::Unk, Target::Bit::Unk, {}, {}); - return target; -} - -static const Target& DefaultHostTarget() { - static Target target( - Target::OS::Linux, Target::Arch::X86, Target::Bit::k64, {}, {}); - return target; -} - -static const Target& DefaultNVGPUTarget() { - static Target target( - Target::OS::Linux, Target::Arch::NVGPU, Target::Bit::k64, {}, {}); - return target; -} - -std::ostream& operator<<(std::ostream& os, Target::Arch arch); - -} // namespace common -} // namespace infrt diff --git a/paddle/infrt/common/type.cc b/paddle/infrt/common/type.cc deleted file mode 100644 index f262bd4697b..00000000000 --- a/paddle/infrt/common/type.cc +++ /dev/null @@ -1,358 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/common/type.h" - -#include - -namespace infrt { -namespace common { - -struct Type::Storage { - Storage() = default; - Storage(type_t t, int b, int w) : type_(t), bits_(b), lanes_(w) {} - - type_t type_{type_t::Unk}; - cpp_type_t cpp_type_{cpp_type_t::None}; - - //! How many bits per element. - int bits_{}; - - //! How many elements(if a vector type), for scalar types, it should be 1. - int lanes_{1}; - - //! Name of the customized type. - std::string customized_type_; -}; - -Type::~Type() {} - -std::ostream &operator<<(std::ostream &os, const Type &t) { - if (t.is_cpp_const()) os << "const "; - switch (t.type()) { - case Type::type_t::Int: - if (t.bits() == 1) { - os << "bool"; - } else { - os << "int" << t.bits(); - } - - break; - case Type::type_t::UInt: - os << "uint" << t.bits(); - break; - - case Type::type_t::Float: - os << "float" << t.bits(); - break; - case Type::type_t::Void: - os << "void"; - break; - case Type::type_t::Customized: - os << t.customized_type(); - break; - case Type::type_t::String: - os << "string"; - break; - case Type::type_t::Unk: - os << "unk"; - break; - } - - if (t.lanes() > 1) os << "<" << t.lanes() << ">"; - if (t.is_cpp_handle()) os << "*"; - if (t.is_cpp_handle2()) os << "**"; - - return os; -} - -std::ostream &operator<<(std::ostream &os, Type::type_t t) { - switch (t) { - case Type::type_t::String: - os << "String"; - break; - case Type::type_t::Void: - os << "Void"; - break; - case Type::type_t::UInt: - os << "UInt"; - break; - case Type::type_t::Int: - os << "Int"; - break; - case Type::type_t::Float: - os << "Float"; - break; - case Type::type_t::Unk: - os << "Unk"; - break; - case Type::type_t::Customized: - os << "Customized"; - } - return os; -} - -Type &Type::set_cpp_handle(bool x) { - // unset the other handle-related bits. - set_cpp_handle2(false); - - auto &v = (*reinterpret_cast(&GetStorage().cpp_type_)); - // unset the other handle-related bits. - v &= ~static_cast(cpp_type_t::Handle); - v &= ~static_cast(cpp_type_t::HandleHandle); - - if (x) - v |= static_cast(cpp_type_t::Handle); - else - v &= ~static_cast(cpp_type_t::Handle); - - return *this; -} - -Type &Type::set_cpp_handle2(bool x) { - auto &v = (*reinterpret_cast(&GetStorage().cpp_type_)); - - // unset the other handle-related bits. - v &= ~static_cast(cpp_type_t::Handle); - v &= ~static_cast(cpp_type_t::HandleHandle); - - if (x) - v |= static_cast(cpp_type_t::HandleHandle); - else - v &= ~static_cast(cpp_type_t::HandleHandle); - - return *this; -} - -Type Type::VectorOf(int w) const { - CheckTypeValid(); - return Type(type(), w, bits()); -} - -Type::Type(const Type &other) { - if (other.storage_) storage_.reset(new Storage(*other.storage_)); -} - -Type Type::ElementOf() const { - CheckTypeValid(); - auto type = *this; - type.storage_->lanes_ = 1; - return type; -} - -void Type::CheckTypeValid() const { CHECK_NE(GetStorage().type_, type_t::Unk); } - -Type Type::PointerOf() const { - CheckTypeValid(); - auto x = *this; - CHECK(!x.is_cpp_handle2()) << "Not support three level of PointerOf"; - if (x.is_cpp_handle()) - x.set_cpp_handle2(); - else - x.set_cpp_handle(); - return x; -} - -Type Type::ConstOf() const { - CheckTypeValid(); - auto x = *this; - x.set_cpp_const(); - return x; -} - -Type Type::IgnoreConst() const { - CheckTypeValid(); - auto x = *this; - x.set_cpp_const(false); - return x; -} - -Type Type::with_bits(int x) const { - CHECK(is_primitive()); - Type type = *this; - type.GetStorage().bits_ = x; - return type; -} - -Type Type::with_type(Type::type_t x) const { - Type type = *this; - type.GetStorage().type_ = x; - return type; -} - -Type Type::with_lanes(int x) const { - CHECK(valid()); - Type type = *this; - type.GetStorage().lanes_ = x; - return type; -} - -Type Type::with_cpp_const(bool x) const { - Type type = *this; - type.set_cpp_const(x); - return type; -} - -Type &Type::set_cpp_const(bool is_const) { - uint8_t &data = *reinterpret_cast(&GetStorage().cpp_type_); - if (is_const) { - data |= static_cast(cpp_type_t::Const); - } else { - data &= ~(static_cast(cpp_type_t::Const)); - } - - return *this; -} -Type &Type::set_customized_type(const std::string &t) { - GetStorage().type_ = type_t::Customized; - GetStorage().customized_type_ = t; - - return *this; -} - -bool Type::valid() const { - if (is_unk()) return false; - if (is_customized()) { - return !GetStorage().customized_type_.empty(); - } - if (is_primitive()) { - return bits() != 0; - } - return true; -} - -Type::Type(Type::type_t t, int b, int w) : storage_(new Storage(t, b, w)) {} -bool Type::is_primitive() const { - return !is_unk() && type() != type_t::Customized; -} -bool Type::is_customized() const { - return !is_unk() && type() == type_t::Customized; -} -bool Type::is_unk() const { return type() == type_t::Unk; } -bool Type::is_bool() const { return type() == type_t::UInt && bits() == 1; } -bool Type::is_void() const { return type() == type_t::Void; } -bool Type::is_vector() const { return lanes() > 1; } -bool Type::is_scalar() const { return lanes() == 1; } -bool Type::is_float(int bits) const { - return type() == type_t::Float && (bits < 0 || bits == this->bits()); -} -bool Type::is_uint(int bits) const { - return type() == type_t::UInt && (bits < 0 || bits == this->bits()); -} -bool Type::is_int(int bits) const { - return type() == type_t::Int && (bits < 0 || bits == this->bits()); -} -bool Type::is_integer(int bits) const { - return (type() == type_t::Int || type() == type_t::UInt) && - (bits < 0 || bits == this->bits()); -} -bool Type::is_index_type() { - return is_int() && lanes() == 1 && (bits() == 32 || bits() == 64); -} -bool Type::is_cpp_handle() const { - return static_cast(GetStorage().cpp_type_) & - static_cast(cpp_type_t::Handle); -} -bool Type::is_cpp_handle2() const { - return static_cast(GetStorage().cpp_type_) & - static_cast(cpp_type_t::HandleHandle); -} -bool Type::is_cpp_const() const { - return static_cast(cpp_type_t::Const) & - static_cast(GetStorage().cpp_type_); -} -const std::string &Type::customized_type() const { - return GetStorage().customized_type_; -} -bool Type::is_customized_type() const { - return !GetStorage().customized_type_.empty(); -} -Type::type_t Type::type() const { return GetStorage().type_; } -int Type::bits() const { return GetStorage().bits_; } -int Type::lanes() const { return GetStorage().lanes_; } -Type::cpp_type_t Type::cpp_type() const { return GetStorage().cpp_type_; } -bool Type::operator==(const Type &other) const { - return type() == other.type() && bits() == other.bits() && - lanes() == other.lanes() && - GetStorage().cpp_type_ == other.GetStorage().cpp_type_ && - customized_type() == other.customized_type(); -} -bool Type::is_string() const { return type() == type_t::String; } - -Type &Type::operator=(const Type &other) { - if (other.storage_) storage_.reset(new Storage(*other.storage_)); - return *this; -} - -Type::Storage &Type::GetStorage() { return *storage_; } -const Type::Storage &Type::GetStorage() const { return *storage_; } - -Type::Type() : storage_(new Storage) {} -Type::Type(Type &&other) : storage_(std::move(other.storage_)) {} - -const Type &F16() { - static auto t = Float(16); - return t; -} -const Type &F32() { - static auto t = Float(32); - return t; -} -const Type &F64() { - static auto t = Float(64); - return t; -} -const Type &I8() { - static auto t = Int(8); - return t; -} -const Type &I16() { - static auto t = Int(16); - return t; -} -const Type &I32() { - static auto t = Int(32); - return t; -} -const Type &I64() { - static auto t = Int(64); - return t; -} -const Type &UI8() { - static auto t = UInt(8); - return t; -} -const Type &UI16() { - static auto t = UInt(16); - return t; -} -const Type &UI32() { - static auto t = UInt(32); - return t; -} -const Type &UI64() { - static auto t = UInt(64); - return t; -} -const Type &I1() { - static auto t = Int(1); - return t; -} -const Type &UI1() { - static auto t = UInt(1); - return t; -} - -} // namespace common -} // namespace infrt diff --git a/paddle/infrt/common/type.h b/paddle/infrt/common/type.h deleted file mode 100644 index 70dd2c5cb46..00000000000 --- a/paddle/infrt/common/type.h +++ /dev/null @@ -1,257 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -#include -#include - -#include "paddle/infrt/common/macros.h" - -//! Much of the concepts are borrowed from Halide project. - -namespace infrt { -namespace common { - -/** - * Types in the INFRT type system. They can be ints, unsigned ints, or floats of - * various bit-widths. - * They can also be vectors of the same (by setting the `lanes` field to - * something larger than one). - * NOTE: Front-end code other than vectorize shouldn't use vector types. - */ -struct Type { - enum class type_t { - Unk = -1, - Int, - UInt, - Float, - String, - Void, - // stupid idea to mix the Customized with other primitive types, large - // refactor needs here. - Customized, // Customized type - }; - - //! type decorators in C++, the different code can used together. - enum class cpp_type_t : uint8_t { - None = 0, // None information. - Const = 1, // const. - Handle = 1 << 1, // pointer type, such as `infrt_buffer_t*`. - HandleHandle = 1 << 2, // pointer of pointer, such as `infrt_buffer_t**`. - }; - - Type(); - Type(type_t t, int b, int w); - Type(const Type& other); - explicit Type(Type&& other); - Type& operator=(const Type& other); - - INFRT_NODISCARD bool is_primitive() const; - INFRT_NODISCARD bool is_customized() const; - INFRT_NODISCARD bool valid() const; - - //! Some helper functions to check a type. - // @{ - INFRT_NODISCARD bool is_unk() const; - INFRT_NODISCARD bool is_void() const; - INFRT_NODISCARD bool is_bool() const; - INFRT_NODISCARD bool is_vector() const; - INFRT_NODISCARD bool is_scalar() const; - INFRT_NODISCARD bool is_float(int bits = -1) const; - INFRT_NODISCARD bool is_int(int bits = -1) const; - INFRT_NODISCARD bool is_integer(int bits = -1) const; - INFRT_NODISCARD bool is_uint(int bits = -1) const; - INFRT_NODISCARD bool is_string() const; - INFRT_NODISCARD bool is_index_type(); - // @} - - Type& set_cpp_handle(bool x = true); - INFRT_NODISCARD bool is_cpp_handle() const; - - Type& set_cpp_handle2(bool x = true); - INFRT_NODISCARD bool is_cpp_handle2() const; - - Type& set_cpp_const(bool is_const = true); - INFRT_NODISCARD bool is_cpp_const() const; - - Type& set_customized_type(const std::string& t); - const std::string& customized_type() const; - INFRT_NODISCARD bool is_customized_type() const; - - // Get a new type with bits set to \p x. - Type with_bits(int x) const; - // Get a new type with type set to \p x. - Type with_type(type_t x) const; - // Get a new type with lanes set to \p x. - Type with_lanes(int x) const; - // Get a new type with cpp_const set to \p x. - Type with_cpp_const(bool x = true) const; - - //! Getters - // @{ - type_t type() const; - int bits() const; - int lanes() const; - cpp_type_t cpp_type() const; - // @} - - //! Compare two types for equality. - bool operator==(const Type& other) const; - - //! Compare two types for inequality. - bool operator!=(const Type& other) const { return !(*this == other); } - - //! Generate a vector of this type, with `w` elements. - Type VectorOf(int w) const; - //! Generate a element type of this type. - Type ElementOf() const; - //! Generate the address type. - Type PointerOf() const; - //! Ignore const. - Type IgnoreConst() const; - //! Add const. - Type ConstOf() const; - - friend std::ostream& operator<<(std::ostream& os, const Type& t); - - ~Type(); - - private: - void CheckTypeValid() const; - - struct Storage; - Storage& GetStorage(); - const Storage& GetStorage() const; - - std::unique_ptr storage_; -}; // namespace common - -inline Type Void() { return Type(Type::type_t::Void, 1, 0); } -inline Type Int(int bits, int lanes = 1) { - return Type(Type::type_t::Int, bits, lanes); -} -inline Type UInt(int bits, int lanes = 1) { - return Type(Type::type_t::UInt, bits, lanes); -} -inline Type Float(int bits, int lanes = 1) { - return Type(Type::type_t::Float, bits, lanes); -} -inline Type Bool(int lanes = 1) { return Type(Type::type_t::UInt, 1, lanes); } -inline Type String() { return Type(Type::type_t::String, 1, 1); } - -//! Builtin native types as global singletons. -// @{ -const Type& F16(); -const Type& F32(); -const Type& F64(); -const Type& I8(); -const Type& I16(); -const Type& I32(); -const Type& I64(); -const Type& UI8(); -const Type& UI16(); -const Type& UI32(); -const Type& UI64(); -const Type& I1(); -const Type& UI1(); -// @} - -template -Type type_of(); - -template <> -inline Type type_of() { - return F32(); -} -template <> -inline Type type_of() { - return F64(); -} -template <> -inline Type type_of() { - return UI8(); -} -template <> -inline Type type_of() { - return UI16(); -} -template <> -inline Type type_of() { - return I32(); -} -template <> -inline Type type_of() { - return UI32(); -} -template <> -inline Type type_of() { - return UI1(); -} -template <> -inline Type type_of() { - return I8(); -} -template <> -inline Type type_of() { - return I64(); -} -template <> -inline Type type_of() { - return UI64(); -} -template <> -inline Type type_of() { - return I8(); -} -template <> -inline Type type_of() { - return Void(); -} -template <> -inline Type type_of() { - Type x = Int(8); - x.set_cpp_handle(); - return x; -} -template <> -inline Type type_of() { - Type x = type_of(); - x.set_cpp_handle(); - return x; -} -template <> -inline Type type_of() { - Type x = type_of(); - x.set_cpp_handle2(); - return x; -} -template <> -inline Type type_of() { - Type x = type_of(); - x.set_cpp_handle(); - return x; -} -template <> -inline Type type_of() { - Type x = type_of(); - x.set_cpp_handle(); - return x; -} - -std::ostream& operator<<(std::ostream& os, Type::type_t t); - -} // namespace common -} // namespace infrt diff --git a/paddle/infrt/dialect/CMakeLists.txt b/paddle/infrt/dialect/CMakeLists.txt deleted file mode 100644 index 33206dbd56b..00000000000 --- a/paddle/infrt/dialect/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -core_gather_headers() - -gather_srcs( - infrt_src - SRCS - dialect.cc - init_dialects.cc - tensor_shape.cc - dense_tensor.cc - mlir_loader.cc - diagnostic_utils.cc) - -mlir_tablegen_on(tensor_shape DIALECT ts) -mlir_tablegen_on(dense_tensor DIALECT dt) - -# TODO(Superjomn) add a cmake function cc_executable to ecapsulate the following code -add_executable(infrtopt opt.cc) -target_link_libraries(infrtopt infrt) - -add_executable(print-ir print_ir.cc) -target_link_libraries(print-ir infrt ${mlir_libs}) -cc_test_tiny(test_infrt_mlir_loader SRCS mlir_loader_test.cc DEPS infrt - ${MLIR_IR_LIBS}) - -add_subdirectory(infrt) -add_subdirectory(pd) -add_subdirectory(tensorrt) - -if(INFRT_WITH_PHI) - add_subdirectory(phi) -endif() diff --git a/paddle/infrt/dialect/dense_tensor.cc b/paddle/infrt/dialect/dense_tensor.cc deleted file mode 100644 index 7b8d48ff716..00000000000 --- a/paddle/infrt/dialect/dense_tensor.cc +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/dense_tensor.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/dialect/tensor_shape.h" - -namespace infrt { -namespace dt { -void DTDialect::initialize() { - addOperations< -#define GET_OP_LIST -#include "paddle/infrt/dialect/dense_tensor.cpp.inc" - >(); -} -static mlir::Type getTensorType(mlir::MLIRContext *context) { - auto t_dialect = mlir::Identifier::get("t", context); - return mlir::OpaqueType::get(t_dialect, "tensor"); -} - -static mlir::ParseResult parseCreateUninitTensorOp( - mlir::OpAsmParser &parser, // NOLINT - mlir::OperationState &result) { // NOLINT - auto loc = parser.getCurrentLocation(); - mlir::Type outputRawTypes[1]; - ::llvm::ArrayRef outputTypes(outputRawTypes); - - mlir::ArrayAttr shapeAttr; - if (parser.parseAttribute(shapeAttr, - parser.getBuilder().getI64Type(), - "shape", - result.attributes)) - return mlir::failure(); - if (parser.parseOptionalAttrDict(result.attributes)) return mlir::failure(); - - if (parser.parseArrow()) return mlir::failure(); - if (parser.parseType(outputRawTypes[0])) return mlir::failure(); - if (!outputRawTypes[0].isa()) - return parser.emitError(loc, "invalid kind of type specified"); - result.addTypes(outputTypes); - return mlir::success(); -} - -template -static void printCreateUninitTensorOp(mlir::OpAsmPrinter &p, // NOLINT - CreateUninitTensorOp op) { - p << CreateUninitTensorOp::getOperationName(); - p << " "; - p.printAttributeWithoutType(op.shapeAttr()); - p.printOptionalAttrDict(op->getAttrs(), /*elidedAttrs=*/{"shape"}); - p << " -> "; - p << op.getOperation()->getResultTypes(); -} - -static mlir::ParseResult parseSetTensorOp( - mlir::OpAsmParser &parser, // NOLINT - mlir::OperationState &result) { // NOLINT - llvm::SmallVector operands; - if (parser.parseOperandList(operands, 1)) return mlir::failure(); - - auto tensor_type = getTensorType(result.getContext()); - - mlir::Attribute value_attr; - return mlir::failure( - parser.resolveOperand(operands[0], tensor_type, result.operands) || - parser.parseAttribute(value_attr, "values", result.attributes)); -} - -template -static void printSetTensorOp(mlir::OpAsmPrinter &p, SetTensorOp op) { // NOLINT - p << SetTensorOp::getOperationName() << " "; - p.printOperand(op.getOperand()); - p << " " << op->getAttr("values"); -} -} // namespace dt -} // namespace infrt - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/dense_tensor.cpp.inc" // NOLINT -#include "paddle/infrt/dialect/dense_tensor_dialect.cpp.inc" diff --git a/paddle/infrt/dialect/dense_tensor.h b/paddle/infrt/dialect/dense_tensor.h deleted file mode 100644 index eebcbbbcbc6..00000000000 --- a/paddle/infrt/dialect/dense_tensor.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include - -#include - -#include "paddle/infrt/dialect/dense_tensor_dialect.hpp.inc" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/dense_tensor.hpp.inc" diff --git a/paddle/infrt/dialect/dense_tensor.td b/paddle/infrt/dialect/dense_tensor.td deleted file mode 100644 index 822a4879e6f..00000000000 --- a/paddle/infrt/dialect/dense_tensor.td +++ /dev/null @@ -1,216 +0,0 @@ -#ifdef DT_OPS -#else -#define DT_OPS - -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "paddle/infrt/dialect/tensor_shape_base.td" -include "mlir/Interfaces/SideEffectInterfaces.td" - -def DT_Dialect : Dialect { - let name = "dt"; - - let description = [{ - The DenseTensor dialect. - }]; - - let cppNamespace = "::infrt::dt"; -} - -class DT_Op traits = []> : - Op; - -class CreateUninitTensorOp - : DT_Op<"create_uninit_tensor." # dtype, [NoSideEffect]> { - let summary = "dt.create_uninit_tensor operation"; - - let description = [{ - An operation that creates an uninitialized tensor. - }]; - - let arguments = (ins I64ArrayAttr:$shape); - let results = (outs DenseTensor:$output); - - let parser = [{ return infrt::dt::parseCreateUninitTensorOp(parser, result); }]; - let printer = [{ return infrt::dt::printCreateUninitTensorOp(p, *this); }]; -} - - -def ShallowCopyTensorOp - : DT_Op<"shallow_copy_tensor", [NoSideEffect]> { - let summary = "dt.shallow_copy_tensor operation"; - - let description = [{ - An operation that copy a tensor shallowly. - }]; - - let arguments = (ins DenseTensor:$input); - let results = (outs DenseTensor:$output); - - let assemblyFormat = "$input attr-dict `:` type($input) `->` type($output)"; -} - - -class FillTensorWithConstantOp : - DT_Op<"fill_tensor_with_constant." # dtype> { - let summary = "dt.fill_tensor_with_constant operation"; - - let description = [{ - An operation that fills an input tensor with a value. - }]; - - let arguments = (ins - DenseTensor:$input, - AnyAttr:$value - ); - let results = (outs); - - // TODO: can be removed? - //let parser = [{ return infrt::dt::parseFillTensorWithConstantOp(parser, result); }]; - //let printer = [{ return infrt::dt::printFillTensorWithConstantOp(p, *this); }]; - let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict"; -} - -def PrintTensorOp : DT_Op<"print_tensor"> { - let summary = "dt.print_tensor operation"; - - let description = [{ - An operation that prints a tensor. - }]; - - let arguments = (ins DenseTensor:$input); - let results = (outs); - let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict"; -} - -class SetTensorOp : - DT_Op<"set_tensor_with_constant_values." # dtype> { - let summary = "dt.set_tensor_with_constant_values operation"; - - let description = [{ - An operation that sets an input tensor with given values. - }]; - - let arguments = (ins DenseTensor); - let results = (outs); - - let parser = [{ return infrt::dt::parseSetTensorOp(parser, result); }]; - let printer = [{ return infrt::dt::printSetTensorOp(p, *this); }]; -} - -def LoadParamsOp : DT_Op<"load_params", [NoSideEffect]> { - let summary = "dt.load_params operation"; - - let description = [{ - An operation that can load tensors to TensorMap. - }]; - - // input path of model params. - let arguments = (ins StrAttr:$path); - let results = (outs DenseHostTensorMap:$out); - - let assemblyFormat = "`(``)`attr-dict"; -} - - -def TensorMapGetTensorOp : DT_Op<"tensor_map_get_tensor", [NoSideEffect]> { - let summary = "dt.tensor_map_get_tensor operation"; - - let description = [{ - An operation that can get a tensor from a TensorMap. - }]; - - // input path of model params. - let arguments = (ins - DenseHostTensorMap:$map, - StrAttr:$name - ); - let results = (outs DenseTensor:$output); - let assemblyFormat = "`(` operands `)` attr-dict `->` type($output)"; - let verifier = ?; -} - -def TensorMapGetSizeOp : DT_Op<"tensor_map_get_size", [NoSideEffect]> { - let summary = "dt.tensor_map_get_size operation"; - - let description = [{ - An operation that get the size of a TensorMap. - }]; - - let arguments = (ins DenseHostTensorMap:$map); - let results = (outs I32:$size); - let assemblyFormat = "`(` $map `)` attr-dict `->` type($size)"; -} - -def Infrt_TensorListGetTensorOp : DT_Op<"tensor_list_get_tensor", [NoSideEffect]> { - let summary = "dt.tensor_list_get_tensor operation"; - - let description = [{ - An operation that can get a tensor from a TensorList. - }]; - - let arguments = (ins - DenseTensorList:$l, - I32Attr:$id - ); - let results = (outs DenseTensor:$output); - let verifier = ?; -} - -def TensorListGetSizeOp : DT_Op<"tensor_list_get_size", [NoSideEffect]> { - let summary = "dt.tensor_list_get_size operation"; - - let description = [{ - An operation that get the size of a TensorList. - }]; - - let arguments = (ins DenseTensorList:$map); - let results = (outs I32:$size); -} - -def GetTensorShapeOp : DT_Op<"get_tensor_shape", [NoSideEffect]> { - let summary = "dt.get_tensor_shape operation"; - - let description = [{ - An operation that returns the shape of the input tensor. - }]; - - let arguments = (ins DenseTensor:$input); - let results = (outs TS_Shape:$output); - let assemblyFormat = "$input attr-dict `:` type($input) `->` type($output)"; -} - -class NaiveElementwiseAddOp : - DT_Op<"naive_elementwise_add." # dtype, [NoSideEffect]> { - let summary = "dt.naive_elementwise_add operation"; - - let description = [{ - Naive elementwise_add operation. - Just for testing. - }]; - let arguments = (ins DenseTensor:$a, DenseTensor:$b); - let results = (outs DenseTensor:$output); - let assemblyFormat = "`(` $a `,` $b `)` attr-dict `:` `(` type($a) `,` type($b) `)` `->` type($output)"; -} - -class NaiveMatmulOp : - DT_Op<"naive_matmul." # dtype, [NoSideEffect]> { - let summary = "dt.naive_matmul operation"; - - let description = [{ - Naive matmul operation. - Just for testing. - }]; - let arguments = (ins DenseTensor:$x, DenseTensor:$w); - let results = (outs DenseTensor:$output); - let assemblyFormat = "`(` $x `,` $w `)` attr-dict `:` `(` type($x) `,` type($w) `)` `->` type($output)"; -} - -foreach dtype = ["ui8", "ui16", "ui32", "ui64", "i32", "f32", "f64", "i64"] in { - def DT_CreateUninitTensorOp_#dtype : CreateUninitTensorOp; - def DT_FillTensorOp_#dtype : FillTensorWithConstantOp; - def DT_SetTensorOp_#dtype : SetTensorOp; - def DT_NaiveElementwiseAddOp_#dtype : NaiveElementwiseAddOp; - def DT_NaiveMatmulOp_#dtype : NaiveMatmulOp; -} - -#endif // DT_OPS diff --git a/paddle/infrt/dialect/diagnostic_utils.cc b/paddle/infrt/dialect/diagnostic_utils.cc deleted file mode 100644 index 8785ce69b8e..00000000000 --- a/paddle/infrt/dialect/diagnostic_utils.cc +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/diagnostic_utils.h" - -#include - -#include - -namespace infrt { -namespace dialect { - -struct MyScopedDiagnosicHandler::Impl { - Impl() : diag_stream_(diag_str_) {} - - // String stream to assemble the final error message. - std::string diag_str_; - llvm::raw_string_ostream diag_stream_; - - // A SourceMgr to use for the base handler class. - llvm::SourceMgr source_mgr_; - - // Log detail information. - bool log_info_{}; -}; - -MyScopedDiagnosicHandler::MyScopedDiagnosicHandler(mlir::MLIRContext *ctx, - bool propagate) - : mlir::SourceMgrDiagnosticHandler( - impl_->source_mgr_, ctx, impl_->diag_stream_), - impl_(new Impl) { - setHandler([this](mlir::Diagnostic &diag) { return this->handler(&diag); }); -} - -mlir::LogicalResult MyScopedDiagnosicHandler::handler(mlir::Diagnostic *diag) { - if (diag->getSeverity() != mlir::DiagnosticSeverity::Error && - !impl_->log_info_) - return mlir::success(); - emitDiagnostic(*diag); - impl_->diag_stream_.flush(); - return mlir::failure(true); -} - -} // namespace dialect -} // namespace infrt diff --git a/paddle/infrt/dialect/diagnostic_utils.h b/paddle/infrt/dialect/diagnostic_utils.h deleted file mode 100644 index 746e61c8fe5..00000000000 --- a/paddle/infrt/dialect/diagnostic_utils.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include - -#include - -namespace infrt { -namespace dialect { - -/** - * A scoped diagnostic handler to help debug MLIR process. - */ -class MyScopedDiagnosicHandler : public mlir::SourceMgrDiagnosticHandler { - public: - MyScopedDiagnosicHandler(mlir::MLIRContext* ctx, bool propagate); - - mlir::LogicalResult handler(mlir::Diagnostic* diag); - - ~MyScopedDiagnosicHandler(); - - private: - class Impl; - std::unique_ptr impl_; -}; - -} // namespace dialect -} // namespace infrt diff --git a/paddle/infrt/dialect/dialect.cc b/paddle/infrt/dialect/dialect.cc deleted file mode 100644 index fe07b91d22e..00000000000 --- a/paddle/infrt/dialect/dialect.cc +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include -#include -#include -#include - -namespace infrt { -namespace hlir { -namespace dialect { - -class CinnDialect : public mlir::Dialect { - public: - explicit CinnDialect(mlir::MLIRContext* ctx); - - //! We should register this function in dialect - static llvm::StringRef getDialectNamespace() { - return "infrt::hlir::dialect"; - } -}; -} // namespace dialect -} // namespace hlir -} // namespace infrt diff --git a/paddle/infrt/dialect/infrt/CMakeLists.txt b/paddle/infrt/dialect/infrt/CMakeLists.txt deleted file mode 100644 index 5f65336453f..00000000000 --- a/paddle/infrt/dialect/infrt/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_subdirectory(common) -add_subdirectory(ir) -add_subdirectory(pass) diff --git a/paddle/infrt/dialect/infrt/common/CMakeLists.txt b/paddle/infrt/dialect/infrt/common/CMakeLists.txt deleted file mode 100644 index 593030be0a5..00000000000 --- a/paddle/infrt/dialect/infrt/common/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -core_gather_headers() - -gather_srcs(infrt_src SRCS types.cc utils.cc) diff --git a/paddle/infrt/dialect/infrt/common/types.cc b/paddle/infrt/dialect/infrt/common/types.cc deleted file mode 100644 index c10679b0134..00000000000 --- a/paddle/infrt/dialect/infrt/common/types.cc +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/infrt/common/types.h" - -namespace infrt { - -llvm::Optional GetTargetType(llvm::StringRef key) { - if (key.equals_insensitive("CPU")) - return TargetType::CPU; - else if (key.equals_insensitive("GPU")) - return TargetType::GPU; - else - return llvm::None; -} - -llvm::Optional GetLayoutType(llvm::StringRef key) { - if (key.equals_insensitive("NCHW")) - return LayoutType::NCHW; - else if (key.equals_insensitive("NHWC")) - return LayoutType::NHWC; - else if (key.equals_insensitive("ANY")) - return LayoutType::ANY; - else - return llvm::None; -} - -llvm::Optional GetPrecisionType(llvm::StringRef key) { - if (key.equals_insensitive("FP32")) - return PrecisionType::FLOAT32; - else if (key.equals_insensitive("FP16")) - return PrecisionType::FLOAT16; - else if (key.equals_insensitive("UNK")) - return PrecisionType::UNK; - else - return llvm::None; -} - -llvm::StringRef GetString(TargetType type) { - llvm::StringRef str; - switch (type) { - case (TargetType::CPU): - str = "CPU"; - break; - case (TargetType::GPU): - str = "GPU"; - break; - default: - str = "Unsupported"; - } - return str; -} - -llvm::StringRef GetString(LayoutType type) { - llvm::StringRef str; - switch (type) { - case (LayoutType::NCHW): - str = "NCHW"; - break; - case (LayoutType::NHWC): - str = "NHWC"; - break; - case (LayoutType::ANY): - str = "ANY"; - break; - default: - str = "Unsupported"; - } - return str; -} - -llvm::StringRef GetString(PrecisionType type) { - llvm::StringRef str; - switch (type) { - case (PrecisionType::FLOAT32): - str = "FP32"; - break; - case (PrecisionType::FLOAT16): - str = "FP16"; - break; - case (PrecisionType::UNK): - str = "UNK"; - break; - default: - str = "Unsupported"; - } - return str; -} - -} // namespace infrt diff --git a/paddle/infrt/dialect/infrt/common/types.h b/paddle/infrt/dialect/infrt/common/types.h deleted file mode 100644 index 5bd1f40262b..00000000000 --- a/paddle/infrt/dialect/infrt/common/types.h +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include - -namespace infrt { - -enum class TargetType : uint8_t { CPU, GPU, UNK }; -enum class LayoutType : uint8_t { NCHW, NHWC, ANY, UNK }; -enum class PrecisionType : uint8_t { - UINT8, - INT8, - INT16, - INT32, - INT64, - FLOAT16, - BFLOAT16, - FLOAT32, - FLOAT64, - COMPLEX64, - COMPLEX128, - BOOL, - UNK -}; - -struct Place { - TargetType target = TargetType::UNK; - PrecisionType precision = PrecisionType::UNK; - LayoutType layout = LayoutType::UNK; - Place(TargetType tar, PrecisionType pre, LayoutType lay) - : target(tar), precision(pre), layout(lay) {} - Place() = default; -}; - -llvm::Optional GetTargetType(llvm::StringRef key); -llvm::Optional GetLayoutType(llvm::StringRef key); -llvm::Optional GetPrecisionType(llvm::StringRef key); - -llvm::StringRef GetString(TargetType type); -llvm::StringRef GetString(LayoutType type); -llvm::StringRef GetString(PrecisionType type); - -template -llvm::raw_ostream &operator<<(llvm::raw_ostream &os, T type) { - os << GetString(type); - return os; -} -} // end namespace infrt diff --git a/paddle/infrt/dialect/infrt/common/utils.cc b/paddle/infrt/dialect/infrt/common/utils.cc deleted file mode 100644 index 0ffb23c490f..00000000000 --- a/paddle/infrt/dialect/infrt/common/utils.cc +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/infrt/common/utils.h" - -mlir::SmallVector infrt::cvtValueToValueRange( - const mlir::Value &operand) { - return mlir::SmallVector(1, operand); -} - -mlir::SmallVector infrt::concatTwoValueRange( - mlir::ValueRange operand_0, mlir::ValueRange operand_1) { - mlir::SmallVector operands; - operands.append(operand_0.begin(), operand_0.end()); - operands.append(operand_1.begin(), operand_1.end()); - return operands; -} diff --git a/paddle/infrt/dialect/infrt/common/utils.h b/paddle/infrt/dialect/infrt/common/utils.h deleted file mode 100644 index 886407b5664..00000000000 --- a/paddle/infrt/dialect/infrt/common/utils.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include -#include -#include - -namespace infrt { - -mlir::SmallVector cvtValueToValueRange( - const mlir::Value &operand); - -mlir::SmallVector concatTwoValueRange( - mlir::ValueRange operand_0, mlir::ValueRange operand_1); -} // namespace infrt diff --git a/paddle/infrt/dialect/infrt/ir/CMakeLists.txt b/paddle/infrt/dialect/infrt/ir/CMakeLists.txt deleted file mode 100644 index 103c603e765..00000000000 --- a/paddle/infrt/dialect/infrt/ir/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -core_gather_headers() - -gather_srcs(infrt_src SRCS infrt_dialect.cc basic_kernels.cc test_kernels.cc) - -add_mlir_dialect(infrt_ops infrt) - -set(LLVM_TARGET_DEFINITIONS infrt_ops.td) -mlir_tablegen(infrt_opsAttributes.h.inc -gen-attrdef-decls -dialect=infrt) -mlir_tablegen(infrt_opsAttributes.cpp.inc -gen-attrdef-defs -dialect=infrt) -add_public_tablegen_target(MLIRinfrt_opsAttributesIncGen) -add_dependencies(mlir-headers MLIRinfrt_opsAttributesIncGen) - -mlir_tablegen_on(basic_kernels) -mlir_tablegen_on(test_kernels) diff --git a/paddle/infrt/dialect/infrt/ir/basic_kernels.cc b/paddle/infrt/dialect/infrt/ir/basic_kernels.cc deleted file mode 100644 index ba83f3e36c9..00000000000 --- a/paddle/infrt/dialect/infrt/ir/basic_kernels.cc +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/dialect/dense_tensor.h" - -namespace infrt { -namespace dialect { -using namespace mlir; // NOLINT - -static ParseResult parseConstantOp(Type attrType, - OpAsmParser &parser, // NOLINT - OperationState &result) { // NOLINT - Attribute valueAttr; - if (parser.parseOptionalAttrDict(result.attributes) || - parser.parseAttribute(valueAttr, attrType, "value", result.attributes) || - parser.addTypeToList(attrType, result.types)) - return failure(); - return success(); -} - -static ParseResult parseConstantF32Op(OpAsmParser &parser, // NOLINT - OperationState &result) { // NOLINT - return parseConstantOp( - FloatType::getF32(result.getContext()), parser, result); -} -static ParseResult parseConstantF64Op(OpAsmParser &parser, // NOLINT - OperationState &result) { // NOLINT - return parseConstantOp( - FloatType::getF64(result.getContext()), parser, result); -} -static ParseResult parseConstantI32Op(OpAsmParser &parser, // NOLINT - OperationState &result) { // NOLINT - return parseConstantOp( - IntegerType::get(result.getContext(), 32), parser, result); -} -static ParseResult parseConstantI64Op(OpAsmParser &parser, // NOLINT - OperationState &result) { // NOLINT - return parseConstantOp( - IntegerType::get(result.getContext(), 64), parser, result); -} - -static void printConstant(OpAsmPrinter &p, mlir::Operation *op) { // NOLINT - p << " "; - p.printOptionalAttrDict(op->getAttrs(), /*elidedAttrs=*/{"value"}); - - if (op->getAttrs().size() > 1) p << ' '; - Attribute attr = op->getAttr("value"); - if (auto int_attr = attr.dyn_cast()) { - bool is_signed = int_attr.getType().isIndex() || - int_attr.getType().getIntOrFloatBitWidth() != 1; - int_attr.getValue().print(p.getStream(), is_signed); - } else if (auto float_attr = attr.dyn_cast()) { - p << float_attr.getValue().convertToFloat(); - } else { - op->emitOpError("unknown attribute type"); - } -} - -static void print(OpAsmPrinter &p, ConstantF32Op op) { // NOLINT - printConstant(p, op); -} -static void print(OpAsmPrinter &p, ConstantF64Op op) { // NOLINT - printConstant(p, op); -} -static void print(OpAsmPrinter &p, ConstantI32Op op) { // NOLINT - printConstant(p, op); -} -static void print(OpAsmPrinter &p, ConstantI64Op op) { // NOLINT - printConstant(p, op); -} - -static LogicalResult verify(ConstantF32Op op) { return success(); } -static LogicalResult verify(ConstantI32Op op) { return success(); } -static LogicalResult verify(ConstantF64Op op) { return success(); } -static LogicalResult verify(ConstantI64Op op) { return success(); } - -} // namespace dialect -} // namespace infrt - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.cpp.inc" diff --git a/paddle/infrt/dialect/infrt/ir/basic_kernels.h b/paddle/infrt/dialect/infrt/ir/basic_kernels.h deleted file mode 100644 index a36f55691b7..00000000000 --- a/paddle/infrt/dialect/infrt/ir/basic_kernels.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.hpp.inc" diff --git a/paddle/infrt/dialect/infrt/ir/basic_kernels.td b/paddle/infrt/dialect/infrt/ir/basic_kernels.td deleted file mode 100644 index 60315b45dd0..00000000000 --- a/paddle/infrt/dialect/infrt/ir/basic_kernels.td +++ /dev/null @@ -1,93 +0,0 @@ -// Operation definitions for basic kernels. - -#ifdef BASIC_OPS -#else -#define BASIC_OPS - -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "mlir/Interfaces/SideEffectInterfaces.td" - -class INFRT_Op traits = []> : Op { - - // Each registered op needs to provide all of a printer, parser and verifier. - let printer = [{ return infrt::dialect::print(p, *this); }]; - let verifier = [{ return infrt::dialect::verify(*this); }]; - let parser = [{ return infrt::dialect::parse$cppClass(parser, result); }]; -} - -class ConstantOp - : INFRT_Op<"constant." # suffix, [NoSideEffect]> { - let summary = "constant value constructor in host"; - - let arguments = (ins attr:$value); - let results = (outs baseType); -} - -def ConstantI32Op : ConstantOp<"i32", I32, I32Attr>; -def ConstantI64Op : ConstantOp<"i64", I64, I64Attr>; -def ConstantF32Op : ConstantOp<"f32", F32, F32Attr>; -def ConstantF64Op : ConstantOp<"f64", F64, F64Attr>; - -class AddOp : INFRT_Op<"add." # suffix, [NoSideEffect]> { - let summary = "infrt.add operation"; - let description = [{ - An operation that takes two inputs and returns their sum as result. - }]; - - let arguments = (ins type, type); - let results = (outs type); - let assemblyFormat = "operands attr-dict"; - let verifier = ?; -} - -def AddI32Op : AddOp<"i32", I32>; -def AddI64Op : AddOp<"i64", I64>; -def AddF32Op : AddOp<"f32", F32>; -def AddF64Op : AddOp<"f64", F64>; - -class MulOp : INFRT_Op<"mul." # suffix, [NoSideEffect]> { - let summary = "infrt.mul operation"; - let description = [{ - An operation that takes two inputs and returns their mul as result. - }]; - - let arguments = (ins type, type); -let results = (outs type); -let assemblyFormat = "operands attr-dict"; -let verifier = ?; -} - -def MulI32Op : MulOp<"i32", I32>; -def MulI64Op : MulOp<"i64", I64>; -def MulF32Op : MulOp<"f32", F32>; -def MulF64Op : MulOp<"f64", F64>; - -class PrintOp : INFRT_Op<"print." # suffix> { - let summary = "infrt.print operation"; - let description = [{ - An operation takes a number as input and prints to stdout. - }]; - - let arguments = (ins type); - let assemblyFormat = "operands attr-dict"; - let verifier = ?; -} - -def PrintI32Op : PrintOp<"i32", I32>; -def PrintI64Op : PrintOp<"i64", I64>; -def PrintF32Op : PrintOp<"f32", F32>; -def PrintF64Op : PrintOp<"f64", F64>; - -def PrintStringOp : INFRT_Op<"print_string"> { - let summary = "infrt.print_string"; - let description = [{ - An operation that prints a string. - }]; - - let arguments = (ins StrAttr:$input); - let results = (outs); - let assemblyFormat = "`(` $input `)` attr-dict"; - let verifier = ?; -} - -#endif // basic kernels diff --git a/paddle/infrt/dialect/infrt/ir/infrt_base.td b/paddle/infrt/dialect/infrt/ir/infrt_base.td deleted file mode 100644 index ba8867d223a..00000000000 --- a/paddle/infrt/dialect/infrt/ir/infrt_base.td +++ /dev/null @@ -1,129 +0,0 @@ -#ifndef INFRT_OPS_BASE -#define INFRT_OPS_BASE - -include "mlir/IR/OpBase.td" -include "mlir/Interfaces/SideEffectInterfaces.td" - -def Infrt_Dialect : Dialect { - let summary = - "A dialect containing the Infrt Attributes, Operations, and Types"; - - let name = "infrt"; - let cppNamespace = "::infrt"; - let hasConstantMaterializer = 1; - let useDefaultAttributePrinterParser = 1; -} - -// Type definitions -// Base class for Infrt dialect types. -class Infrt_Type traits = [], - string baseCppClass = "::mlir::Type"> - : TypeDef { -} - -class Infrt_EnumParam : TypeParameter { - let parser = [{[&]() -> ::mlir::FailureOr<}] # cppEnumType # [{> { - ::llvm::StringRef enumKeyword; - if (::mlir::failed($_parser.parseKeyword(&enumKeyword))) - return ::mlir::failure(); - auto maybeEnum = }] # stringToSymbolFnName # [{(enumKeyword); - if (maybeEnum) - return *maybeEnum; - llvm_unreachable("}] # cppEnumType # [{ can not be found."); - return {}; - }()}]; - let printer = "$_printer << " # symbolToStringFnName # "($_self)"; -} - -def TargetParam : Infrt_EnumParam<"::infrt::TargetType", "GetTargetType", "GetString">; -def PrecisionParam : Infrt_EnumParam<"::infrt::PrecisionType", "GetPrecisionType", "GetString">; -def LayoutParam : Infrt_EnumParam<"::infrt::LayoutType", "GetLayoutType", "GetString">; - -def TargetAttr : AttrDef { - let mnemonic = "target"; - let parameters = (ins - TargetParam:$target - ); - let assemblyFormat = "`<` $target `>`"; -} - -def PrecisionAttr : AttrDef { - let mnemonic = "precision"; - let parameters = (ins - PrecisionParam:$precision - ); - let assemblyFormat = "`<` $precision `>`"; -} - -def LayoutAttr : AttrDef { - let mnemonic = "layout"; - let parameters = (ins - LayoutParam:$layout - ); - let assemblyFormat = "`<` $layout `>`"; -} - -def LoDTensor : Infrt_Type<"LoDTensor"> { - let summary = "infrt lod tensor"; - let description = [{lod_tensor<3x64x3x3xf32, 3>}]; - let parameters = (ins - ArrayRefParameter<"int64_t">:$shape, - "mlir::Type":$elementType, - "int32_t":$lod_level - ); -} - -def DenseTensor : Infrt_Type<"DenseTensor"> { - let summary = "infrt dense tensor"; - let description = [{dense_tensor<, 3>}]; - let parameters = (ins - "::infrt::TargetType":$target, - "::infrt::PrecisionType":$precision, - "::infrt::LayoutType":$layout - ); -} - -def DenseHostTensorMap : Infrt_Type<"DenseHostTensorMap"> { - let summary = "infrt dense tensor map"; - let description = [{dense_tensor map}]; - let parameters = (ins); -} - -// TODO(wilber): Add !infrt.vec type. -def DenseTensorList : Infrt_Type<"DenseTensorList"> { - let summary = "infrt dense tensor map"; - let description = [{dense_tensor map}]; - let parameters = (ins); -} - -// Type Constrait for concrete DenseTensor type. -class DenseTensor : - Type, - "!infrt.DenseTensor<"#target#","#precision#","#layout#">", - "::infrt::DenseTensorType">; - -// Base class for infrt dialect attributes. -class Infrt_Attr traits = [], - string baseCppClass = "::mlir::Attribute"> - : AttrDef { - let mnemonic = ?; -} - -// tools function. used for pattern rewriter -class INFRT_createI32Attr : NativeCodeCall< - "$_builder.getI32IntegerAttr(" # value # ")">; - -class INFRT_createSI32Attr : NativeCodeCall< - "$_builder.getSI32IntegerAttr(" # value # ")">; - -class INFRT_createF32Attr : NativeCodeCall< - "$_builder.getF32FloatAttr(" # value # ")">; - -def INFRT_cvtValueToValueRange : NativeCodeCall< - "infrt::cvtValueToValueRange($0)">; - -def INFRT_concatTwoValueRange : NativeCodeCall< - "infrt::concatTwoValueRange($0, $1)">; - -#endif // INFRT_OPS_BASE diff --git a/paddle/infrt/dialect/infrt/ir/infrt_dialect.cc b/paddle/infrt/dialect/infrt/ir/infrt_dialect.cc deleted file mode 100644 index c4f20cb4d35..00000000000 --- a/paddle/infrt/dialect/infrt/ir/infrt_dialect.cc +++ /dev/null @@ -1,223 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" - -#include -#include -#include -#include -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_opsDialect.cpp.inc" - -#define GET_TYPEDEF_CLASSES -#include "paddle/infrt/dialect/infrt/ir/infrt_opsTypes.cpp.inc" - -#define GET_ATTRDEF_CLASSES -#include "paddle/infrt/dialect/infrt/ir/infrt_opsAttributes.cpp.inc" - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/infrt/ir/infrt_ops.cpp.inc" - -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.h" - -#include "paddle/infrt/dialect/infrt/ir/test_kernels.h" - -namespace infrt { - -void InfrtDialect::initialize() { - addTypes< -#define GET_TYPEDEF_LIST -#include "paddle/infrt/dialect/infrt/ir/infrt_opsTypes.cpp.inc" // NOLINT - >(); - - addAttributes< -#define GET_ATTRDEF_LIST -#include "paddle/infrt/dialect/infrt/ir/infrt_opsAttributes.cpp.inc" // NOLINT - >(); - - addOperations< -#define GET_OP_LIST -#include "paddle/infrt/dialect/infrt/ir/infrt_ops.cpp.inc" // NOLINT - >(); - addOperations< -#define GET_OP_LIST -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.cpp.inc" - >(); - addOperations< -#define GET_OP_LIST -#include "paddle/infrt/dialect/infrt/ir/test_kernels.cpp.inc" - >(); -} - -/// Parse a type registered to this dialect. -mlir::Type InfrtDialect::parseType(::mlir::DialectAsmParser &parser) const { - llvm::StringRef keyword; - if (parser.parseKeyword(&keyword)) return nullptr; - // parse TensorType, for example: !infrt.lod_tensor<3x64x3x3xf32,5> - // 5 is the lod_level - if (keyword == "lod_tensor") { - // Parse the size and elementType. - llvm::SmallVector shape; - mlir::Type elementType; - int32_t lod_level = 0; - // parse "<" - if (parser.parseLess()) return nullptr; - - if (parser.parseDimensionList(shape)) return nullptr; - - // Parse the element type. - if (parser.parseType(elementType)) return nullptr; - // parse optional lod_level - if (parser.parseOptionalComma().succeeded()) { - // llvm::APInt lod_level; - if (parser.parseInteger(lod_level)) return nullptr; - } - // parse ">" - if (parser.parseGreater()) return nullptr; - - return LoDTensorType::get( - parser.getContext(), shape, elementType, lod_level); - } - if (keyword == "dense_tensor_map") { - return DenseHostTensorMapType::get(parser.getContext()); - } - if (keyword == "dense_tensor") { - // parse DenseTensor, for example: !i=Infrt.tensor - llvm::StringRef target; - llvm::StringRef layout; - llvm::StringRef precision; - - // parse "<" - if (parser.parseLess()) return mlir::Type(); - // parse target - if (parser.parseKeyword(&target)) return mlir::Type(); - auto targetType = GetTargetType(target); - if (!targetType) { - parser.emitError(parser.getCurrentLocation(), "unknown target type: ") - << target; - return mlir::Type(); - } - - // parse "," - if (parser.parseComma()) return mlir::Type(); - // parse precision - if (parser.parseKeyword(&precision)) return mlir::Type(); - auto precisionType = GetPrecisionType(precision); - if (!precisionType) { - parser.emitError(parser.getCurrentLocation(), "unknown precision type: ") - << precision; - return mlir::Type(); - } - - // parse "," - if (parser.parseComma()) return mlir::Type(); - - // parse layout - if (parser.parseKeyword(&layout)) return mlir::Type(); - auto layoutType = GetLayoutType(layout); - if (!layoutType) { - parser.emitError(parser.getCurrentLocation(), "unknown layout type: ") - << layout; - return mlir::Type(); - } - // parse ">" - if (parser.parseGreater()) return mlir::Type(); - return DenseTensorType::get( - parser.getContext(), *targetType, *precisionType, *layoutType); - } - - if (keyword == "tensor_list") { - return infrt::DenseTensorListType::get(parser.getContext()); - } - - // Todo: parse other type - return mlir::Type(); -} - -void InfrtDialect::printType(::mlir::Type type, - ::mlir::DialectAsmPrinter &os) const { - // print LoDTensorType, for example: !infrt.lod_tensor<3x64x3x3xf32,5> - if (type.isa()) { - auto lod_tensor_type = type.cast(); - os << "lod_tensor<"; - auto shape = lod_tensor_type.getShape(); - for (auto dim = shape.begin(), e = shape.end() - 1; dim != e; ++dim) { - *dim < 0 ? os << '?' : os << *dim; - os << 'x'; - } - shape.back() < 0 ? os << '?' : os << shape.back(); - os << 'x' << lod_tensor_type.getElementType() << ", " - << lod_tensor_type.getLod_level() << ">"; - return; - } - if (type.isa()) { - os << "dense_tensor_map"; - return; - } - - // print DenseTensorType, for example: !infrt.dense_tensor - if (type.isa()) { - auto dense_tensor_type = type.cast(); - os << "dense_tensor<" << dense_tensor_type.getTarget() << ", " - << dense_tensor_type.getPrecision() << ", " - << dense_tensor_type.getLayout() << ">"; - return; - } - - if (type.isa()) { - os << "tensor_list"; - return; - } - llvm_unreachable("unknown infrt type."); -} - -mlir::Operation *InfrtDialect::materializeConstant(mlir::OpBuilder &builder, - mlir::Attribute value, - mlir::Type type, - mlir::Location loc) { - return builder.create(loc, value); -} - -void ConstantOp::build(mlir::OpBuilder &builder, - mlir::OperationState &state, - mlir::Attribute value) { - if (auto elem_attr = value.dyn_cast()) { - return ConstantOp::build(builder, state, elem_attr); - } else if (value.isa()) { - mlir::ShapedType type = - mlir::RankedTensorType::get(/*shape=*/{}, value.getType()); - state.addAttribute("value", mlir::DenseElementsAttr::get(type, value)); - state.addTypes(type); - return; - } - llvm_unreachable("unsupported attribute type for building pd.constant"); -} - -mlir::LogicalResult ConstantOp::inferReturnTypes( - mlir::MLIRContext *context, - mlir::Optional location, - mlir::ValueRange operands, - mlir::DictionaryAttr attributes, - mlir::RegionRange regions, - llvm::SmallVectorImpl &inferredReturnTypes) { - inferredReturnTypes.push_back(attributes.get("value").getType()); - return mlir::success(); -} -mlir::OpFoldResult ConstantOp::fold( - ::llvm::ArrayRef operands) { - return value(); -} - -} // namespace infrt diff --git a/paddle/infrt/dialect/infrt/ir/infrt_dialect.h b/paddle/infrt/dialect/infrt/ir/infrt_dialect.h deleted file mode 100644 index 5a7c45b3205..00000000000 --- a/paddle/infrt/dialect/infrt/ir/infrt_dialect.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -//===----------------------------------------------------------------------===// -// Dialect -//===----------------------------------------------------------------------===// -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/dialect/infrt/common/types.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_opsDialect.h.inc" -#define GET_TYPEDEF_CLASSES -#include "paddle/infrt/dialect/infrt/ir/infrt_opsTypes.h.inc" - -#define GET_ATTRDEF_CLASSES -#include "paddle/infrt/dialect/infrt/ir/infrt_opsAttributes.h.inc" - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/infrt/ir/infrt_ops.h.inc" diff --git a/paddle/infrt/dialect/infrt/ir/infrt_ops.td b/paddle/infrt/dialect/infrt/ir/infrt_ops.td deleted file mode 100644 index 2736b7ad8c2..00000000000 --- a/paddle/infrt/dialect/infrt/ir/infrt_ops.td +++ /dev/null @@ -1,85 +0,0 @@ -include "mlir/Interfaces/InferTypeOpInterface.td" -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" - -// Op definition -class Infrt_Op traits = []> : Op { - - // Each registered op needs to provide all of a printer, parser and verifier. - // let printer = [{ return infrt::print(p, *this); }]; - // let verifier = [{ return infrt::verify(*this); }]; - // let parser = [{ return infrt::parse$cppClass(parser, result); }]; -} - -def Infrt_GraphOp : Infrt_Op<"graph", [SingleBlockImplicitTerminator<"::infrt::ReturnOp">]> { - let summary = "paddle graph Op"; - let description = [{ - Describe a paddle graph or subgraph. - }]; - let regions = (region SizedRegion<1>:$body); - let arguments = (ins Variadic:$inputs); - let results = (outs Variadic:$outputs); -} - -def Infrt_KernelOp : Infrt_Op<"kernel", [NoSideEffect]> { - let summary = "kernel op"; - let description = [{kernel op!}]; - let arguments = (ins Variadic:$operands, - StrAttr:$name, - OptionalAttr:$attrs); - let results = (outs Variadic); -} - -def Infrt_ReturnOp : Infrt_Op<"return", [Terminator]> { - let summary = "host executor return operation"; - let description = [{ - The "infrt.return" operation represents a return operation within a function. - - func @foo() : (i32, f8) { - infrt.return %0, %1 : i32, f8 - } - }]; - - let arguments = (ins Variadic:$operands); - - let assemblyFormat = "attr-dict ($operands^ `:` type($operands))?"; -} - -def Infrt_CallOp : Infrt_Op<"call"> { - let summary = "call a host operation"; - let description = [{ - The "infrt.call" operation represents a direct call to a function. The operands and result types of the call must match the specified function type. - - %2 = infrt.call @add(%0, %1) : (f32, f32) -> f32 - }]; - - let arguments = (ins FlatSymbolRefAttr:$callee, Variadic:$operands); - let results = (outs Variadic); - - //let extraClassDeclaration = [{ - // mlir::StringRef getCallee() { return callee(); } - // mlir::FunctionType getCalleeType(); - // }]; - let assemblyFormat = [{ - $callee `(` $operands `)` attr-dict `:` functional-type($operands, results) - }]; -} - -def Infrt_TensorCastOp : Infrt_Op<"tensor_cast", [NoSideEffect]> { - let summary = "cast tensor type op"; - let description = [{cast tensor type op!}]; - let arguments = (ins AnyType:$input); - let results = (outs AnyType:$output); -} - -def Infrt_ConstantOp : Infrt_Op<"constant", [NoSideEffect, ConstantLike, DeclareOpInterfaceMethods, AllTypesMatch<["value", "output"]>]> { - let summary = "constant Op"; - let description = [{}]; - - let arguments = (ins ElementsAttr:$value); - let results = (outs AnyType:$output); - let hasFolder = 1; - - let builders = [ - OpBuilder<(ins "mlir::Attribute":$value)>, - ]; -} diff --git a/paddle/infrt/dialect/infrt/ir/test_kernels.cc b/paddle/infrt/dialect/infrt/ir/test_kernels.cc deleted file mode 100644 index 5f7f83a9dfa..00000000000 --- a/paddle/infrt/dialect/infrt/ir/test_kernels.cc +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/infrt/ir/test_kernels.h" - -#include -#include -#include -#include - -namespace infrt { -namespace dialect { -//===----------------------------------------------------------------------===// -// BenchmarkOp -//===----------------------------------------------------------------------===// - -// Parse the BenchmarkOp in the following format -// infrt.benchmark "add.i32"(%c : i32, %d : f32) -// max_count = 100, duration_secs = 1 { -// ... -// } - -static mlir::ParseResult parseBenchmarkOp( - mlir::OpAsmParser &parser, // NOLINT - mlir::OperationState &result) { // NOLINT - mlir::StringAttr nameAttr; - if (parser.parseAttribute(nameAttr, "name", result.attributes)) - return mlir::failure(); - - // Parse the operands, e.g. (%c : i32, %d : f32) - if (parser.parseLParen()) return mlir::failure(); - - llvm::SmallVector operands; - llvm::SmallVector types; - llvm::SMLoc type_loc = parser.getCurrentLocation(); - - if (parser.parseOptionalRParen()) { - // Parse non-empty operands - do { - // Parse %c : i32, - mlir::OpAsmParser::OperandType operand; - mlir::Type type; - - if (parser.parseOperand(operand) || parser.parseColonType(type)) - return mlir::failure(); - - operands.push_back(operand); - types.push_back(type); - } while (succeeded(parser.parseOptionalComma())); - - if (parser.parseRParen()) return mlir::failure(); - } - - if (parser.resolveOperands(operands, types, type_loc, result.operands)) - return mlir::failure(); - - // Parse the keyword attribute, e.g. max_count = 100, duration_secs = 1 - do { - mlir::StringRef attr; - mlir::Attribute resultAttr; - if (parser.parseKeyword(&attr) || parser.parseEqual() || - parser.parseAttribute(resultAttr, - parser.getBuilder().getIntegerType(32), - attr, - result.attributes)) - return mlir::failure(); - } while (mlir::succeeded(parser.parseOptionalComma())); - - // Set the default attribute num_warmup_runs to 1 if unset - auto setDefaultAttrIfUnset = [&](const char *attr_name, int value) { - bool found = llvm::any_of(result.attributes, - [attr_name](const mlir::NamedAttribute &attr) { - return attr.getName() == attr_name; - }); - if (!found) { - mlir::IntegerAttr default_val = - parser.getBuilder().getI32IntegerAttr(value); - result.addAttribute(attr_name, default_val); - } - }; - setDefaultAttrIfUnset("num_warmup_runs", 1); - - mlir::Region *target = result.addRegion(); - return parser.parseRegion(*target, - operands, - types, - /*enableNameShadowing=*/true); -} - -// Print the BenchmarkOp in the following format -// infrt.benchmark "add.i32"(%c : i32, %d : f32) -// max_count = 100, duration_secs = 1 { -// ... -// } -static void print(mlir::OpAsmPrinter &p, BenchmarkOp op) { // NOLINT - p << "infrt.benchmark "; - - // Print the name attribute, e.g "add.i32" - auto name_attr = op->getAttr("name"); - p << name_attr; - - // Print the operands and types, e.g. (%c : i32, %d : f32) - p << '('; - llvm::interleaveComma(llvm::zip(op.getOperands(), op.getOperandTypes()), - p, - [&](const auto &it) { - p << std::get<0>(it) << " : " << std::get<1>(it); - }); - p << ") "; - - bool need_comma = false; - // Print the attributes, e.g. max_count = 100, duration_secs = 1 - for (auto &name_attr : op->getAttrs()) { - auto id = name_attr.getName(); - if (id == "name") continue; - if (need_comma) p << ", "; - auto attr = name_attr.getValue(); - p << id << " = "; - if (auto int_attr = attr.dyn_cast()) { - int_attr.getValue().print(p.getStream(), /*isSigned=*/false); - } else { - op.emitOpError("Unexpected attribute"); - } - need_comma = true; - } - p << ' '; - - // Print the region - // Reuse the argument names provided to the op for the bbarg names within - // the region. - p.shadowRegionArgs(op.region(), op.getOperands()); - p.printRegion(op.region(), /*printEntryBlockArgs=*/false); -} - -static mlir::LogicalResult verify(BenchmarkOp op) { - // Verify that the target benchmark region has exactly one return value. - auto ®ion = op.region(); - auto &last_op = region.front().back(); - if (last_op.getName().getStringRef() != "infrt.return") { - return op.emitOpError("missing return statement"); - } - if (last_op.getNumOperands() != 1) { - return op.emitOpError( - "incorrect number of return values. One return value is expected"); - } - - return mlir::success(); -} -} // namespace dialect -} // namespace infrt - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/infrt/ir/test_kernels.cpp.inc" diff --git a/paddle/infrt/dialect/infrt/ir/test_kernels.h b/paddle/infrt/dialect/infrt/ir/test_kernels.h deleted file mode 100644 index 1fe5020b240..00000000000 --- a/paddle/infrt/dialect/infrt/ir/test_kernels.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/infrt/ir/test_kernels.hpp.inc" diff --git a/paddle/infrt/dialect/infrt/ir/test_kernels.td b/paddle/infrt/dialect/infrt/ir/test_kernels.td deleted file mode 100644 index 0ce1f3f65e8..00000000000 --- a/paddle/infrt/dialect/infrt/ir/test_kernels.td +++ /dev/null @@ -1,65 +0,0 @@ -// Operation definitions for testing. - -#ifdef TEST_OPS -#else -#define TEST_OPS - -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "mlir/Interfaces/SideEffectInterfaces.td" - -// Base class for Test dialect ops. -class Test_Op traits = []> : - Op { - - // Each registered op in the Test namespace needs to provide all of a printer, - // parser and verifier. - let printer = [{ return infrt::dialect::print(p, *this); }]; - let verifier = [{ return infrt::dialect::verify(*this); }]; - let parser = [{ return infrt::dialect::parse$cppClass(parser, result); }]; -} - -def BenchmarkOp : Test_Op<"benchmark"> { - let summary = "benchmark operation"; - let description = [{ - The "infrt.benchmark" operation benchmarks the performance of an MLIR - region by executing the given MLIR region repeatedly up to the - `duratino_secs` seconds or `max_count` times. `num_warmup_runs` specifies - the number of warm up runs to run the given MLIR region before the - benchmark starts. - - The target MLIR region can take an arbitrary number of arguments and - should return exactly one value. The arguments for the MLIR region are - provided as the operands of the infrt.benchmark op. - - Example: - infrt.benchmark "add.i32"(%c : i32, %d : f32) max_count = 100, duration_secs = 1 { - // code for benchmarking - ... - } - - infrt.benchmark "add.i32"(%c : i32) - duration_secs = 1, - max_count = 100, - num_warmup_runs = 10 { - // The MLIR code to be benchmarked goes here. - // The following code benchmarks the infrt.add.i32 kernel. - %x = infrt.add.i32 %c, %c - // The benchmarked function needs to return exactly one value. - infrt.return %x : i32 - } - }]; - - let regions = (region SizedRegion<1>:$region); - - let arguments = (ins - Variadic, - I32Attr:$duration_secs, - I32Attr:$max_count, - StrAttr:$name, - DefaultValuedAttr:$num_warmup_runs - ); - - let results = (outs); -} - -#endif // TEST_OPS diff --git a/paddle/infrt/dialect/infrt/pass/CMakeLists.txt b/paddle/infrt/dialect/infrt/pass/CMakeLists.txt deleted file mode 100644 index 7fa0ee1c716..00000000000 --- a/paddle/infrt/dialect/infrt/pass/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -core_gather_headers() - -gather_srcs(infrt_src SRCS infrt_op_fuse_pass.cc infrt_weights_unfold_pass.cc) - -mlir_add_rewriter(infrt_op_fuse) diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td deleted file mode 100644 index 1d6c0a75382..00000000000 --- a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef INFRT_OP_FUSE -#define INFRT_OP_FUSE - -include "mlir/Interfaces/SideEffectInterfaces.td" -include "paddle/infrt/dialect/infrt/ir/infrt_ops.td" -include "paddle/infrt/dialect/pd/ir/pd_ops.td" - -def FuseTensorCastPattern : Pat< - (Infrt_TensorCastOp (Infrt_TensorCastOp $arg)), - (Infrt_TensorCastOp $arg)>; - -def TypesAreIdentical : Constraint>; -def RedundantTensorCastOptPattern : Pat< - (Infrt_TensorCastOp:$res $arg), (replaceWithValue $arg), - [(TypesAreIdentical $res, $arg)]>; - - - -#endif // INFRT_OP_FUSE diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc deleted file mode 100644 index 309e0f8b940..00000000000 --- a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h" - -#include - -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" -namespace { -#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse.cpp.inc" // NOLINT - -/* - * infrtOpFusePass. - */ -struct InfrtOpFusePass - : public mlir::PassWrapper { - public: - ::llvm::StringRef getName() const override { return "infrtOpFusePass"; } - - llvm::StringRef getArgument() const override { return "infrt-op-fuse"; } - - void runOnFunction() override; -}; - -// Implementation of the InfrtOpFusePass. -void InfrtOpFusePass::runOnFunction() { - ::mlir::RewritePatternSet patterns(&getContext()); - populateWithGenerated(patterns); - (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); - // Fuse infrt.return Operation - auto terminator_op = getFunction().front().getTerminator(); - if (nullptr == terminator_op) return; - for (auto operand : terminator_op->getOperands()) { - auto *op1 = operand.getDefiningOp(); - auto cvt_op = ::llvm::dyn_cast<::infrt::TensorCastOp>(op1); - if (!cvt_op) continue; - mlir::Value value = cvt_op.input(); - operand.replaceAllUsesWith(value); - cvt_op.erase(); - } -} - -} // namespace - -std::unique_ptr infrt::CreateInfrtOpFusePass() { - return std::make_unique(); -} - -mlir::PassRegistration infrt_op_fuse_pass; diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h deleted file mode 100644 index bca60b7c0d9..00000000000 --- a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -/* - * infrtOpFusePass. - */ -std::unique_ptr CreateInfrtOpFusePass(); - -} // namespace infrt diff --git a/paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.cc b/paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.cc deleted file mode 100644 index 23d411021f9..00000000000 --- a/paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.cc +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.h" - -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/Casting.h" -#include "mlir/IR/Builders.h" -#include "mlir/IR/BuiltinAttributes.h" -#include "mlir/IR/BuiltinOps.h" -#include "mlir/IR/Operation.h" -#include "mlir/IR/UseDefLists.h" -#include "mlir/IR/Value.h" -#include "paddle/infrt/dialect/infrt/common/types.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" -#include "paddle/infrt/paddle/model_parser.h" -#include "paddle/infrt/tensor/phi/tensor_map.h" -#include "paddle/phi/backends/all_context.h" -#include "paddle/phi/common/data_type.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace kernel { -namespace phi { -::infrt::phi::DenseTensorMap LoadCombinedParameters( - const std::string& model_path, const std::string& params_path); -} // namespace phi -} // namespace kernel -} // namespace infrt - -namespace { - -class InfrtWeightsFoldPass - : public mlir::PassWrapper { - public: - ::llvm::StringRef getName() const override { return "InfrtWeightsFoldPass"; } - - void runOnFunction() override; -}; - -void InfrtWeightsFoldPass::runOnFunction() { - mlir::Block& block = getFunction().body().front(); - mlir::OpBuilder builder(&block, block.begin()); - - ::llvm::StringRef model_path, params_path; - std::vector delete_op_list; - // Insert cpu context. If the pass failed, the context op will be removed by - // CanonicalizerPass. - auto context_op = builder.create( - block.front().getLoc(), - infrt::phi::ContextType::get(builder.getContext(), - infrt::TargetType::CPU)); - - for (auto& org_op : block) { - if (auto op = llvm::dyn_cast<::infrt::phi::LoadCombinedParamsOp>(org_op)) { - model_path = op.model_path(); - params_path = op.params_path(); - - // Load params. - auto map = ::infrt::kernel::phi::LoadCombinedParameters( - model_path.str(), params_path.str()); - bool delete_load_combined_op{false}; - // Find all use of map. - for (auto map_arg : op.getODSResults(0)) { - for (mlir::Operation* user_op : map_arg.getUsers()) { - if (auto tensor_map_get_op = - llvm::dyn_cast<::infrt::phi::TensorMapGetTensorOp>(user_op)) { - ::llvm::StringRef arg_name = tensor_map_get_op.name(); - ::Tensor* tensor = map.GetDenseTensor(arg_name.str()); - if (tensor->dtype() != ::phi::DataType::FLOAT32) { - CHECK(false) - << "the weight tensor type now only support float32."; - } - - builder.setInsertionPoint(tensor_map_get_op); - auto inited_weight_op = - builder.create<::infrt::phi::CreateHostInitedDenseTensorOp>( - tensor_map_get_op.getLoc(), - tensor_map_get_op.output().getType(), - context_op.output(), - builder.getI64ArrayAttr( - {tensor->dims().Get(), - tensor->dims().Get() + tensor->dims().size()}), - ::infrt::LayoutAttr::get(builder.getContext(), - ::infrt::LayoutType::NCHW), - builder.getI64ArrayAttr({0}), - builder.getF32ArrayAttr( - {tensor->data(), - static_cast(tensor->numel())})); - tensor_map_get_op.replaceAllUsesWith(inited_weight_op.output()); - delete_load_combined_op = true; - delete_op_list.push_back(tensor_map_get_op); - } - } - } - if (delete_load_combined_op) { - delete_op_list.push_back(op); - } - } - } - - // remove all map releation op. - for (size_t i = 0; i < delete_op_list.size(); ++i) { - delete_op_list[i]->erase(); - } -} - -} // namespace - -std::unique_ptr infrt::CreateInfrtWeightsUnfoldPass() { - return std::make_unique(); -} diff --git a/paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.h b/paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.h deleted file mode 100644 index 09effe54e69..00000000000 --- a/paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -namespace infrt { -/* - * InfrtWeightsFoldPass. - */ -std::unique_ptr CreateInfrtWeightsUnfoldPass(); - -} // namespace infrt diff --git a/paddle/infrt/dialect/init_dialects.cc b/paddle/infrt/dialect/init_dialects.cc deleted file mode 100644 index c204f9ea626..00000000000 --- a/paddle/infrt/dialect/init_dialects.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/init_dialects.h" - -#include - -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" -#include "paddle/infrt/dialect/phi/ir/phi_kernels.h" -#include "paddle/infrt/dialect/tensor_shape.h" -#include "paddle/infrt/dialect/tensorrt/trt_ops.h" - -namespace infrt { -void registerCinnDialects(mlir::DialectRegistry ®istry) { // NOLINT - registry.insert(); -} -} // namespace infrt diff --git a/paddle/infrt/dialect/init_dialects.h b/paddle/infrt/dialect/init_dialects.h deleted file mode 100644 index 0912e9ef255..00000000000 --- a/paddle/infrt/dialect/init_dialects.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -namespace infrt { -void registerCinnDialects(mlir::DialectRegistry ®istry); // NOLINT -} // namespace infrt diff --git a/paddle/infrt/dialect/mlir_loader.cc b/paddle/infrt/dialect/mlir_loader.cc deleted file mode 100644 index ab533a25c41..00000000000 --- a/paddle/infrt/dialect/mlir_loader.cc +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/mlir_loader.h" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "paddle/infrt/dialect/diagnostic_utils.h" -#include "paddle/infrt/dialect/init_dialects.h" - -namespace infrt { -namespace dialect { - -mlir::OwningModuleRef LoadMlirSource(mlir::MLIRContext* context, - const std::string& mlir_source) { - // context->allowUnregisteredDialects(); - mlir::DialectRegistry registry; - registerCinnDialects(registry); - context->appendDialectRegistry(registry); - // Currenetly, We only used the CinnDialect and mlir::BuiltinDialect is - // enough。Don't need StandardOpsDialect. - // context->getDialectRegistry().insert(); - - mlir::ScopedDiagnosticHandler scope_handler( - context, [](mlir::Diagnostic& diag) { - if (diag.getSeverity() != mlir::DiagnosticSeverity::Error) - return mlir::success(); - LOG(INFO) << "diag: " << diag.str(); - return mlir::failure(true); - }); - - auto res = mlir::parseSourceString( - llvm::StringRef(mlir_source.data(), mlir_source.length()), context); - CHECK(*res) << "failed to parse MLIR string"; - return res; -} - -mlir::OwningModuleRef LoadMlirFile(const std::string& file_name, - mlir::MLIRContext* context) { - // context->allowUnregisteredDialects(); - mlir::DialectRegistry registry; - registerCinnDialects(registry); - context->appendDialectRegistry(registry); - context->loadAllAvailableDialects(); - mlir::ScopedDiagnosticHandler scope_handler( - context, [](mlir::Diagnostic& diag) { - if (diag.getSeverity() != mlir::DiagnosticSeverity::Error) - return mlir::success(); - LOG(INFO) << "diag: " << diag.str(); - return mlir::failure(true); - }); - - return mlir::parseSourceFile(std::string(file_name), context); -} - -} // namespace dialect -} // namespace infrt diff --git a/paddle/infrt/dialect/mlir_loader.h b/paddle/infrt/dialect/mlir_loader.h deleted file mode 100644 index b4faba8068e..00000000000 --- a/paddle/infrt/dialect/mlir_loader.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include - -#include -#include - -namespace infrt { -namespace dialect { - -mlir::OwningModuleRef LoadMlirSource(mlir::MLIRContext* context, - const std::string& mlir_source); -mlir::OwningModuleRef LoadMlirFile(const std::string& file_name, - mlir::MLIRContext* context); -} // namespace dialect -} // namespace infrt diff --git a/paddle/infrt/dialect/mlir_loader_test.cc b/paddle/infrt/dialect/mlir_loader_test.cc deleted file mode 100644 index 8ccb07161d3..00000000000 --- a/paddle/infrt/dialect/mlir_loader_test.cc +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/mlir_loader.h" - -#include -#include -#include -#include -#include - -#include - -#include "paddle/infrt/dialect/init_dialects.h" - -namespace infrt { -namespace dialect { - -TEST(MlirLoader, basic) { - mlir::MLIRContext context; - - auto source = R"ROC( -func @main() -> f32 { - %v0 = infrt.constant.f32 1.0 - %v1 = infrt.constant.f32 2.0 - %value = "infrt.add.f32"(%v0, %v1) : (f32, f32) -> f32 - - "infrt.print.f32"(%v0) : (f32) -> () - - infrt.return %value : f32 -} -)ROC"; - - auto module = LoadMlirSource(&context, source); - EXPECT_TRUE(mlir::succeeded(module->verify())); - LOG(INFO) << "module name: " << module->getOperationName().data(); - for (auto func : module->getOps()) { - LOG(INFO) << "get func " << func.getName().str(); - int num_args = func.getNumArguments(); - for (int i = 0; i < num_args; i++) { - LOG(INFO) << "arg: " << func.getArgument(i).getArgNumber(); - } - } -} - -} // namespace dialect -} // namespace infrt diff --git a/paddle/infrt/dialect/opt.cc b/paddle/infrt/dialect/opt.cc deleted file mode 100644 index e57666ffca0..00000000000 --- a/paddle/infrt/dialect/opt.cc +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -#include "paddle/infrt/dialect/init_dialects.h" - -int main(int argc, char **argv) { - mlir::DialectRegistry registry; - infrt::registerCinnDialects(registry); - mlir::registerCanonicalizerPass(); - return mlir::failed( - mlir::MlirOptMain(argc, argv, "infrt mlir pass driver", registry)); -} diff --git a/paddle/infrt/dialect/pd/CMakeLists.txt b/paddle/infrt/dialect/pd/CMakeLists.txt deleted file mode 100644 index 5f65336453f..00000000000 --- a/paddle/infrt/dialect/pd/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_subdirectory(common) -add_subdirectory(ir) -add_subdirectory(pass) diff --git a/paddle/infrt/dialect/pd/common/CMakeLists.txt b/paddle/infrt/dialect/pd/common/CMakeLists.txt deleted file mode 100644 index d253a847557..00000000000 --- a/paddle/infrt/dialect/pd/common/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -core_gather_headers() - -gather_srcs(infrt_src SRCS) diff --git a/paddle/infrt/dialect/pd/ir/CMakeLists.txt b/paddle/infrt/dialect/pd/ir/CMakeLists.txt deleted file mode 100644 index 7c1c99a97a0..00000000000 --- a/paddle/infrt/dialect/pd/ir/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -core_gather_headers() - -gather_srcs(infrt_src SRCS pd_ops.cc) -add_mlir_dialect(pd_ops pd) -mlir_tablegen_on(pd_extra_ops) diff --git a/paddle/infrt/dialect/pd/ir/pd_extra_ops.td b/paddle/infrt/dialect/pd/ir/pd_extra_ops.td deleted file mode 100644 index cf17db211cb..00000000000 --- a/paddle/infrt/dialect/pd/ir/pd_extra_ops.td +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef PD_EXTRA_OPS -#define PD_EXTRA_OPS - -include "mlir/Interfaces/InferTypeOpInterface.td" -include "mlir/Interfaces/LoopLikeInterface.td" -include "mlir/IR/OpBase.td" -include "paddle/infrt/dialect/pd/ir/pd_op_base.td" - -def PD_FusedFC : PD_Op<"FC", [NoSideEffect]> { - let summary = "Computes the Fully Connected result of two tensors"; - let description = [{ - }]; - - let arguments = (ins PD_Tensor:$input, PD_Tensor:$w, PD_Tensor:$bias, DefaultValuedAttr:$in_num_col_dims); - let results = (outs PD_Tensor:$out); -} - -#endif diff --git a/paddle/infrt/dialect/pd/ir/pd_op_base.td b/paddle/infrt/dialect/pd/ir/pd_op_base.td deleted file mode 100644 index cb6f7aadd98..00000000000 --- a/paddle/infrt/dialect/pd/ir/pd_op_base.td +++ /dev/null @@ -1,81 +0,0 @@ -// This file defines some basic elements of Paddle(alias pd) dialect. -// We learned much from TensorFlow mlir dialect https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td - -#ifndef PD_OP_BASE -#define PD_OP_BASE - -include "mlir/IR/OpBase.td" -include "mlir/Interfaces/SideEffectInterfaces.td" -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" - -def Paddle_Dialect : Dialect { - let name = "pd"; - - let description = [{ - The PaddlePaddle dialect. - - This dialect contains the PaddlePaddle operators. - }]; - let cppNamespace = "infrt::pd"; -} - -class PD_Op traits = []> : - Op; - - -class PD_PaddleAttr : - Attr()">, - "PaddlePaddle " # description # " attribute">; - - -//===----------------------------------------------------------------------===// -// PaddlePaddle type definitions -//===----------------------------------------------------------------------===// - -def PD_PDDialectType : Type()">, "PaddlePaddle type">; - -class PD_PaddleType : - Type()">, - "Paddle " # description # " type">, - BuildableType<"getType()">; - -//===----------------------------------------------------------------------===// -// Integer types -def PD_Bool : AnyTypeOf<[I<1>], "bool">; -def PD_Int8 : AnyTypeOf<[I8], "8-bit integer">; -def PD_Int16 : AnyTypeOf<[I16], "16-bit integer">; -def PD_Int32 : AnyTypeOf<[I32], "32-bit integer">; -def PD_Int64 : AnyTypeOf<[I64], "64-bit integer">; - -def PD_UInt8 : AnyTypeOf<[UI<8>], "8-bit unsigned integer">; -def PD_UInt16 : AnyTypeOf<[UI<16>], "16-bit unsigned integer">; -def PD_UInt32 : AnyTypeOf<[UI<32>], "32-bit unsigned integer">; -def PD_UInt64 : AnyTypeOf<[UI<64>], "64-bit unsigned integer">; - -def PD_SInt : AnyTypeOf<[PD_Int8, PD_Int16, PD_Int32, PD_Int64], "signed integer">; -def PD_UInt : AnyTypeOf<[PD_UInt8, PD_UInt16, PD_UInt32, PD_UInt64], "unsigned integer">; -def PD_Int : AnyTypeOf<[PD_SInt, PD_UInt], "integer">; - -// Float types -def PD_Float16 : AnyTypeOf<[F16], "16-bit float">; -def PD_Float32 : AnyTypeOf<[F32], "32-bit float">; -def PD_Float64 : AnyTypeOf<[F64], "64-bit float">; - -def PD_Float : AnyTypeOf<[PD_Float16, PD_Float32, PD_Float64], "floating-point">; - - -// Tensor types - -def PD_ElementType : Type, - "pd.dtype">; - -// def PD_Tensor : TensorOf<[PD_ElementType]>; -def PD_Tensor1 : TensorOf<[PD_ElementType]>; - -def PD_Tensor : AnyTypeOf<[PD_Tensor1, LoDTensor, DenseTensor],"pd.ttype">; - -def PD_Tensor_Array : VectorOf<[PD_Tensor]>; - -#endif // PD_OP_BASE diff --git a/paddle/infrt/dialect/pd/ir/pd_ops.cc b/paddle/infrt/dialect/pd/ir/pd_ops.cc deleted file mode 100644 index be6ff4cf749..00000000000 --- a/paddle/infrt/dialect/pd/ir/pd_ops.cc +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" - -#include -#include - -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/pd/ir/pd_opsDialect.cpp.inc" -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/pd/ir/pd_ops.cpp.inc" // NOLINT -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/pd/ir/pd_extra_ops.cpp.inc" // NOLINT - -namespace infrt { -namespace pd { -void PaddleDialect::initialize() { - addOperations< -#define GET_OP_LIST -#include "paddle/infrt/dialect/pd/ir/pd_ops.cpp.inc" // NOLINT - , -#define GET_OP_LIST -#include "paddle/infrt/dialect/pd/ir/pd_extra_ops.cpp.inc" // NOLINT - >(); -} -} // namespace pd -} // namespace infrt diff --git a/paddle/infrt/dialect/pd/ir/pd_ops.h b/paddle/infrt/dialect/pd/ir/pd_ops.h deleted file mode 100644 index 8383ff6ed82..00000000000 --- a/paddle/infrt/dialect/pd/ir/pd_ops.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -//===----------------------------------------------------------------------===// -// Dialect -//===----------------------------------------------------------------------===// -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/pd/ir/pd_opsDialect.h.inc" -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/pd/ir/pd_ops.h.inc" -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/pd/ir/pd_extra_ops.hpp.inc" diff --git a/paddle/infrt/dialect/pd/pass/CMakeLists.txt b/paddle/infrt/dialect/pd/pass/CMakeLists.txt deleted file mode 100644 index be87052794e..00000000000 --- a/paddle/infrt/dialect/pd/pass/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -core_gather_headers() - -gather_srcs(infrt_src SRCS pd_op_fuse_pass.cc) - -mlir_add_rewriter(pd_op_fuse) diff --git a/paddle/infrt/dialect/pd/pass/pd_op_fuse.td b/paddle/infrt/dialect/pd/pass/pd_op_fuse.td deleted file mode 100644 index f5a8ea78d7d..00000000000 --- a/paddle/infrt/dialect/pd/pass/pd_op_fuse.td +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef INFRT_REWRITE -#define INFRT_REWRITE - -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "mlir/Interfaces/SideEffectInterfaces.td" -include "paddle/infrt/dialect/pd/ir/pd_ops.td" -include "paddle/infrt/dialect/pd/ir/pd_extra_ops.td" - -//===----------------------------------------------------------------------===// -// This is to fuse the composition: 'Matmul o ElementwiseAdd' into 'PD_FusedFC'. -// -// We have: -// (Matmul) z = x * y -// (Add) out = z + bias -// -// which corresponds to the following computation: -// (FusedFC) out = x * y + bias -// -// while meeting the following attribute constrait: -// Matmul: transpose_x: false -// transpose_y: false -// -// Todo: -// 1. Make the constrait more completely. -// 2. Consider the case of : out = bias + z -//===----------------------------------------------------------------------===// -def FuseMulAdd : Pat<(PD_Elementwise_addOp (PD_Matmul_v2Op $x, $y, ConstBoolAttrFalse:$_, ConstBoolAttrFalse:$_), $bias, $axis), - (PD_FusedFC $x, $y, $bias, (INFRT_createI32Attr<"1">))>; - - -/* -//===----------------------------------------------------------------------===// -// This is to fuse the composition: 'FusedFC o Relu' into 'FusedRepeatedFCRelu'. -// -// We have: -// (FusedFC) z = fc(x, y, bias) -// (Relu) out = relu(z) -// -// which corresponds to the following computation: -// (FusedRepeatedFCRelu) out = RepeatedFCRelu(x, [y], [bias]) -// -//===----------------------------------------------------------------------===// -def FuseFCRelu : Pat<(PD_ReluOp (PD_FusedFC $x, $y, $bias, $_)), - (PD_FusedRepeatedFCRelu $x, (INFRT_cvtValueToValueRange $y), (INFRT_cvtValueToValueRange $bias))>; - -//===----------------------------------------------------------------------===// -// This is to fold 'FusedRepeatedFCRelu' op. -// -// We have: -// (FusedRepeatedFCRelu) z = RepeatedFCRelu(x, [y, ...], [bias, ...]) -// (FusedRepeatedFCRelu) out = RepeatedFCRelu(z, [y1, ...], [bias1, ...]) -// -// which corresponds to the following computation: -// (FusedRepeatedFCRelu) out = RepeatedFCRelu(x, [y, ..., y1, ...], [bias, ..., bias1, ....]) -// -//===----------------------------------------------------------------------===// -def FuseRepeatedFCRelu2 : Pat<(PD_FusedRepeatedFCRelu (PD_FusedRepeatedFCRelu $x, $y, $bias), $y_2, $bias_2), - (PD_FusedRepeatedFCRelu $x, (INFRT_concatTwoValueRange $y, $y_2), (INFRT_concatTwoValueRange $bias, $bias_2))>; - - -//===----------------------------------------------------------------------===// -// This is to fuse the composition: 'BatchNorm o Conv' into 'Conv' -// by deriving new 'w' and 'b' for 'Conv': -// -// We have: -// (Conv) z = w * x + b -// (BatchNorm) y = scale * (z - mean) / sqrt(var + eps) + bias -// -// which corresponds to the following computation: -// y = w_ * x + b_ -// where -// w_ = scale * w / sqrt(var + eps) -// b_ = B + scale * (b - mean) / sqrt(var + eps) -// -//===----------------------------------------------------------------------===// -def FuseBatchNormWithConvPattern: Pat< - (PD_BatchNormOp - (PD_Conv2dOp $input, $filter, $bias), - $scale, $bias_2, $mean, $var, $epsilon), - (PD_Conv2dOp - $input, - (PD_MulOp $filter, - (PD_ElementwiseDiv:$coefficientW - $scale, - (PD_SqrtOp (PD_ElementwiseAdd $var, (PD_ConstantOp $epsilon), (INFRT_createI32Attr<"1">))), - (INFRT_createI32Attr<"1">))), - (PD_ElementwiseAdd - $bias, - (PD_MulOp - (PD_ElementwiseSub $bias, $mean, (INFRT_createI32Attr<"1">)), - $coefficientW), - (INFRT_createI32Attr<"1">))) ->; -*/ -#endif // INFRT_REWRITE diff --git a/paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.cc b/paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.cc deleted file mode 100644 index c9247abe695..00000000000 --- a/paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.cc +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#include "paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.h" // NOLINT - -#include - -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" - -namespace { -#include "paddle/infrt/dialect/pd/pass/pd_op_fuse.cpp.inc" // NOLINT - -/* - * PdOpFusePass. - */ -struct PdOpFusePass - : public mlir::PassWrapper { - public: - ::llvm::StringRef getName() const override { return "PdOpFusePass"; } - - llvm::StringRef getArgument() const override { return "pd-op-fuse"; } - - void runOnFunction() override; -}; - -// Implementation of the PdOpFusePass. -void PdOpFusePass::runOnFunction() { - ::mlir::RewritePatternSet patterns(&getContext()); - populateWithGenerated(patterns); - (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); -} - -} // namespace - -mlir::PassRegistration infrt_op_fuse_pass; diff --git a/paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.h b/paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.h deleted file mode 100644 index 854545ab1a2..00000000000 --- a/paddle/infrt/dialect/pd/pass/pd_op_fuse_pass.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -/* - * PdOpFusePass. - */ -std::unique_ptr CreatePdOpFusePass(); - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/CMakeLists.txt b/paddle/infrt/dialect/phi/CMakeLists.txt deleted file mode 100644 index f07c6f70fb6..00000000000 --- a/paddle/infrt/dialect/phi/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -if(NOT INFRT_WITH_PHI) - return() -endif() - -add_subdirectory(ir) -add_subdirectory(pass) - -add_executable(phi-exec phi_exec.cc) -target_link_libraries(phi-exec infrt) - -gather_srcs(infrt_src SRCS data_type.cc) diff --git a/paddle/infrt/dialect/phi/data_type.cc b/paddle/infrt/dialect/phi/data_type.cc deleted file mode 100644 index bbc296ea748..00000000000 --- a/paddle/infrt/dialect/phi/data_type.cc +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/phi/data_type.h" - -namespace infrt { - -phi::Backend ConvertTargetToPhi(TargetType target) { - switch (target) { - case TargetType::CPU: - return phi::Backend::CPU; - case TargetType::GPU: - return phi::Backend::GPU; - default: - return phi::Backend::UNDEFINED; - } -} - -TargetType ConvertTargetFromPhi(phi::Backend backend) { - switch (backend) { - case phi::Backend::CPU: - return TargetType::CPU; - case phi::Backend::GPU: - return TargetType::GPU; - default: - return TargetType::UNK; - } -} - -phi::DataType ConvertPrecisionToPhi(PrecisionType precision) { -#define CONVERT_PRECISION_TO_PHI(Precision) \ - case PrecisionType::Precision: \ - return phi::DataType::Precision; - - switch (precision) { - CONVERT_PRECISION_TO_PHI(FLOAT32) - CONVERT_PRECISION_TO_PHI(FLOAT16) - CONVERT_PRECISION_TO_PHI(FLOAT64) - CONVERT_PRECISION_TO_PHI(UINT8) - CONVERT_PRECISION_TO_PHI(INT8) - CONVERT_PRECISION_TO_PHI(INT16) - CONVERT_PRECISION_TO_PHI(INT32) - CONVERT_PRECISION_TO_PHI(INT64) - CONVERT_PRECISION_TO_PHI(COMPLEX64) - CONVERT_PRECISION_TO_PHI(COMPLEX128) - CONVERT_PRECISION_TO_PHI(BOOL) - default: - return phi::DataType::UNDEFINED; - } -#undef CONVERT_PRECISION_TO_PHI -} - -PrecisionType ConvertPrecisionFromPhi(phi::DataType datatype) { -#define CONVERT_PRECISION_FROM_PHI(Precision) \ - case phi::DataType::Precision: \ - return PrecisionType::Precision; - - switch (datatype) { - CONVERT_PRECISION_FROM_PHI(FLOAT32) - CONVERT_PRECISION_FROM_PHI(FLOAT16) - CONVERT_PRECISION_FROM_PHI(FLOAT64) - CONVERT_PRECISION_FROM_PHI(UINT8) - CONVERT_PRECISION_FROM_PHI(INT8) - CONVERT_PRECISION_FROM_PHI(INT16) - CONVERT_PRECISION_FROM_PHI(INT32) - CONVERT_PRECISION_FROM_PHI(INT64) - CONVERT_PRECISION_FROM_PHI(COMPLEX64) - CONVERT_PRECISION_FROM_PHI(COMPLEX128) - CONVERT_PRECISION_FROM_PHI(BOOL) - default: - return PrecisionType::UNK; - } -#undef CONVERT_PRECISION_FROM_PHI -} - -phi::DataLayout ConvertLayoutToPhi(LayoutType layout) { - switch (layout) { - case LayoutType::NCHW: - return phi::DataLayout::NCHW; - case LayoutType::NHWC: - return phi::DataLayout::NHWC; - case LayoutType::ANY: - return phi::DataLayout::ANY; - default: - return phi::DataLayout::UNDEFINED; - } -} - -LayoutType ConvertLayoutFromPhi(phi::DataLayout layout) { - switch (layout) { - case phi::DataLayout::NCHW: - return LayoutType::NCHW; - case phi::DataLayout::NHWC: - return LayoutType::NHWC; - case phi::DataLayout::ANY: - return LayoutType::ANY; - default: - return LayoutType::UNK; - } -} - -phi::KernelKey ConvertPlaceToPhi(const Place& place) { - return phi::KernelKey(ConvertTargetToPhi(place.target), - ConvertLayoutToPhi(place.layout), - ConvertPrecisionToPhi(place.precision)); -} - -Place ConvertPlaceFromPhi(phi::TensorArgDef tensor_arg) { - return Place(ConvertTargetFromPhi(tensor_arg.backend), - ConvertPrecisionFromPhi(tensor_arg.dtype), - ConvertLayoutFromPhi(tensor_arg.layout)); -} - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/data_type.h b/paddle/infrt/dialect/phi/data_type.h deleted file mode 100644 index 8e831c8c27d..00000000000 --- a/paddle/infrt/dialect/phi/data_type.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "paddle/infrt/dialect/infrt/common/types.h" -#include "paddle/phi/common/backend.h" -#include "paddle/phi/common/data_type.h" -#include "paddle/phi/common/layout.h" -#include "paddle/phi/common/place.h" -#include "paddle/phi/core/kernel_factory.h" - -namespace infrt { - -::phi::Backend ConvertTargetToPhi(TargetType target); -TargetType ConvertTargetFromPhi(::phi::Backend backend); - -::phi::DataType ConvertPrecisionToPhi(PrecisionType precision); -PrecisionType ConvertPrecisionFromPhi(::phi::DataType datatype); - -::phi::DataLayout ConvertLayoutToPhi(LayoutType layout); -LayoutType ConvertLayoutFromPhi(::phi::DataLayout layout); - -::phi::KernelKey ConvertPlaceToPhi(const Place& place); -Place ConvertPlaceFromPhi(::phi::TensorArgDef tensor_arg); - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/ir/CMakeLists.txt b/paddle/infrt/dialect/phi/ir/CMakeLists.txt deleted file mode 100644 index e038da564be..00000000000 --- a/paddle/infrt/dialect/phi/ir/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -#mlir_tablegen_on(infrt_phi_base DIALECT phi) -add_mlir_dialect(infrt_phi_base phi) -add_mlir_dialect(infrt_phi_tensor phi_dt) -add_mlir_dialect(phi_cpu_kernels phi_cpu) -add_mlir_dialect(phi_gpu_kernels phi_gpu) - -#mlir_tablegen_on(infrt_phi_tensor) - -gather_srcs(infrt_src SRCS phi_base.cc infrt_phi_tensor.cc phi_kernels.cc) diff --git a/paddle/infrt/dialect/phi/ir/infrt_phi_base.td b/paddle/infrt/dialect/phi/ir/infrt_phi_base.td deleted file mode 100644 index 376d62deece..00000000000 --- a/paddle/infrt/dialect/phi/ir/infrt_phi_base.td +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef PHI_BASE -#define PHI_BASE - -include "mlir/IR/OpBase.td" -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "mlir/Interfaces/InferTypeOpInterface.td" - -def PHI_Dialect : Dialect { - let name = "phi"; - - let description = [{ - The PHI host dialect. - }]; - - let cppNamespace = "::infrt::phi"; - let useDefaultTypePrinterParser = 1; -} - -def PhiOpTrait : NativeOpTrait<"PhiOpTrait">; - -class PHI_Type traits = [], string baseCppClass = "::mlir::Type"> - : TypeDef {} - -def Allocator : PHI_Type<"Allocator"> { - let mnemonic = "allocator"; - let parameters = (ins - TargetParam:$target - ); - let assemblyFormat = "`<` $target `>`"; - } - - def Context : PHI_Type<"Context"> { - let mnemonic = "context"; - let parameters = (ins - TargetParam:$target - ); - let assemblyFormat = "`<` $target `>`"; - } - -def PD_DenseTensorMap : PHI_Type<"DenseTensorMap"> { - let mnemonic = "dense_tensor_map"; -} - -#endif diff --git a/paddle/infrt/dialect/phi/ir/infrt_phi_kernel.td b/paddle/infrt/dialect/phi/ir/infrt_phi_kernel.td deleted file mode 100644 index d2ff7acfba8..00000000000 --- a/paddle/infrt/dialect/phi/ir/infrt_phi_kernel.td +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef PHI_KERNEL -#define PHI_KERNEL - -include "mlir/Interfaces/SideEffectInterfaces.td" -include "mlir/IR/OpBase.td" -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "paddle/infrt/dialect/phi/ir/infrt_phi_base.td" - -def PHI_CPUKernelDialect : Dialect { - let name = "phi_cpu"; - - let description = [{ - The PHI CPU Kernel dialect. - }]; - - let cppNamespace = "::infrt::phi"; -} - -def PHI_GPUKernelDialect : Dialect { - let name = "phi_gpu"; - - let description = [{ - The PHI GPU Kernel dialect. - }]; - - let cppNamespace = "::infrt::phi"; -} - -// PHI Kernel related ops. -class PDTCPU_Kernel traits = []> : Op { -} - -// PHI Kernel related ops. -class PDTGPU_Kernel traits = []> : Op { -} - -#endif diff --git a/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.cc b/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.cc deleted file mode 100644 index 64780294be9..00000000000 --- a/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.cc +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" - -#include - -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensorDialect.cpp.inc" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensorTypes.cpp.inc" - -namespace infrt { -namespace phi { - -void PHIDenseTensorDialect::initialize() { -#define GET_OP_LIST - addOperations< -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.cpp.inc" - >(); -} - -} // namespace phi -} // namespace infrt - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.cpp.inc" // NOLINT diff --git a/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h b/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h deleted file mode 100644 index 9e09cdde502..00000000000 --- a/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensorDialect.h.inc" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensorTypes.h.inc" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" -// NOLINT -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h.inc" diff --git a/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.td b/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.td deleted file mode 100644 index 7e612be05b1..00000000000 --- a/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.td +++ /dev/null @@ -1,147 +0,0 @@ -#ifdef PHI_TENSOR -#else -#define PHI_TENSOR - -include "paddle/infrt/dialect/phi/ir/infrt_phi_base.td" -include "mlir/Interfaces/SideEffectInterfaces.td" -include "mlir/IR/OpBase.td" -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" - -def PHI_DenseTensorDialect : Dialect { - let name = "phi_dt"; - - let description = [{ - The PHI DenseTensor dialect. - }]; - - let cppNamespace = "::infrt::phi"; -} - -// PHI DenseTensor related Op. -class PDT_Op traits = []> : Op {} - -class CreateDenseTensorOp - : PDT_Op<"create_dense_tensor." # target, [NoSideEffect]> { - let arguments = (ins Context:$context, I64ArrayAttr:$dims, - LayoutAttr:$layout, I64ArrayAttr:$lod, PrecisionAttr:$precision); - let results = (outs DenseTensor:$output); -} - -def CreateHostInitedDenseTensorOp : PDT_Op<"create_host_inited_dense_tensor.f32", [NoSideEffect]> { - let arguments = (ins - Context:$context, - I64ArrayAttr:$dims, - LayoutAttr:$layout, - I64ArrayAttr:$lod, - F32ArrayAttr:$values, - DefaultValuedAttr:$run_once - ); - let results = (outs DenseTensor:$output); -} - -def CreateInitedGpuFLOAT32DenseTensorOp - : PDT_Op<"create_inited_dense_tensor.gpu.f32", [NoSideEffect]> { - let arguments = (ins Context:$context, I64ArrayAttr:$dims, - LayoutAttr:$layout, I64ArrayAttr:$lod, F32Attr:$value); - let results = (outs DenseTensor:$output); -} - -def CreateInitedCpuFLOAT32DenseTensorOp - : PDT_Op<"create_inited_dense_tensor.cpu.f32", [NoSideEffect]> { - let arguments = (ins Context:$context, I64ArrayAttr:$dims, - LayoutAttr:$layout, I64ArrayAttr:$lod, F32Attr:$value); - let results = (outs DenseTensor:$output); -} - -class FillDenseTensorOp : - PDT_Op<"fill_dense_tensor." # dtype> { - let arguments = (ins - DenseTensor:$input, - attr_type:$value - ); - let results = (outs); - let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict"; -} - -class PrintDenseTensorOp: - PDT_Op<"print_tensor"> { - let arguments = (ins DenseTensor:$input); - let results = (outs); - let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict"; -} - -class CreateContextOp - : PDT_Op<"create_context." # target, [NoSideEffect]> { - let arguments = (ins); - let results = (outs Context:$output); -} - -def PDT_LoadParamsOp : PDT_Op<"load_params", [NoSideEffect]> { - // input path of model params. - let arguments = (ins StrAttr:$path); - let results = (outs PD_DenseTensorMap:$out); - - let assemblyFormat = "`(``)`attr-dict"; -} - -def PDT_LoadCombinedParamsOp : PDT_Op<"load_combined_params", [NoSideEffect]> { - // input path of model params. - let arguments = (ins StrAttr:$model_path, StrAttr:$params_path); - let results = (outs PD_DenseTensorMap:$out); - - let assemblyFormat = "`(``)`attr-dict"; -} - -def PDT_LoadCombinedParamsGpuOp : PDT_Op<"load_combined_params_to_gpu", [NoSideEffect]> { - // input path of model params. - let arguments = (ins StrAttr:$model_path, StrAttr:$params_path); - let results = (outs PD_DenseTensorMap:$out); - - let assemblyFormat = "`(``)`attr-dict"; -} - -def PDT_TensorMapGetSizeOp : PDT_Op<"tensor_map_get_size", [NoSideEffect]> { - let arguments = (ins PD_DenseTensorMap:$map); - let results = (outs I32:$size); - let assemblyFormat = "`(` $map `)` attr-dict `->` type($size)"; -} - -class TensorMapGetTensorOp: - PDT_Op<"tensor_map_get_tensor"> { - let arguments = (ins - PD_DenseTensorMap:$map, - StrAttr:$name - ); - let results = (outs DenseTensor:$output); - let assemblyFormat = "`(` operands `)` attr-dict `->` type($output)"; - let verifier = ?; -} - -def PDT_CreateCPUDenseTensorOp : CreateDenseTensorOp<"cpu">; -def PDT_CreateGPUDenseTensorOp : CreateDenseTensorOp<"gpu">; -def PDT_FillDenseTensorOp_f32 : FillDenseTensorOp; -def PDT_CreateCPUContextOp : CreateContextOp<"cpu">; -def PDT_CreateGPUContextOp : CreateContextOp<"gpu">; -def PDT_PrintDenseTensor : PrintDenseTensorOp; -def PDT_TensorMapGetTensorOp: TensorMapGetTensorOp; - -def FakeKernelOp : PDT_Op<"fake_phi_kernel"> { - let arguments = (ins Context:$dev_ctx, DenseTensor:$x, DenseTensor:$y, BoolAttr:$transpose_x, BoolAttr:$transpose_y); - let results = (outs DenseTensor:$output); -} - -// TODO(wilber): Add a infrt_gpu dialect. -def PDT_GpuMemCopyOp : PDT_Op<"memcpy.gpu", [NoSideEffect]> { - let summary = "phi_dt.gpu.memcpy"; - let description = [{gpu memcpy d2h or h2d}]; - // TODO(wilber): add context argument to support stream. - let arguments = (ins - DenseTensor:$input, - Context:$context, - BoolAttr:$d2h - ); - let results = (outs DenseTensor:$output); -} - -#endif diff --git a/paddle/infrt/dialect/phi/ir/phi_base.cc b/paddle/infrt/dialect/phi/ir/phi_base.cc deleted file mode 100644 index 39a23529ac3..00000000000 --- a/paddle/infrt/dialect/phi/ir/phi_base.cc +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/phi/ir/phi_base.h" - -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_base.cpp.inc" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_baseDialect.cpp.inc" - -namespace infrt { -namespace phi { - -void PHIDialect::initialize() { - LOG(INFO) << "PHI Dialect initalized"; - addOperations< -#define GET_OP_LIST -#include "paddle/infrt/dialect/phi/ir/infrt_phi_base.cpp.inc" // NOLINT - >(); - addTypes< -#define GET_TYPEDEF_LIST -#include "paddle/infrt/dialect/phi/ir/infrt_phi_baseTypes.cpp.inc" // NOLINT - >(); -} - -} // namespace phi -} // namespace infrt - -#define GET_TYPEDEF_CLASSES -#include "paddle/infrt/dialect/phi/ir/infrt_phi_baseTypes.cpp.inc" // NOLINT diff --git a/paddle/infrt/dialect/phi/ir/phi_base.h b/paddle/infrt/dialect/phi/ir/phi_base.h deleted file mode 100644 index 2cbdef5af90..00000000000 --- a/paddle/infrt/dialect/phi/ir/phi_base.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include - -#include - -#include "paddle/infrt/dialect/infrt/common/types.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_baseDialect.h.inc" - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/phi/ir/infrt_phi_base.h.inc" - -namespace mlir { -namespace OpTrait { - -template -class PhiOpTrait : public OpTrait::TraitBase { - public: - static LogicalResult verifyTrait(Operation *op) { - return LogicalResult::success(); - } -}; - -} // namespace OpTrait -} // namespace mlir - -#define GET_TYPEDEF_CLASSES -#include "paddle/infrt/dialect/phi/ir/infrt_phi_baseTypes.h.inc" - -namespace infrt { -namespace phi {} // namespace phi -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/ir/phi_kernels.cc b/paddle/infrt/dialect/phi/ir/phi_kernels.cc deleted file mode 100644 index 69c3f963391..00000000000 --- a/paddle/infrt/dialect/phi/ir/phi_kernels.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/phi/ir/phi_kernels.h" - -#include - -#include "paddle/infrt/dialect/phi/ir/phi_gpu_kernelsDialect.cpp.inc" -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/phi/ir/phi_cpu_kernels.cpp.inc" // NOLINT -#include "paddle/infrt/dialect/phi/ir/phi_cpu_kernelsDialect.cpp.inc" -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/phi/ir/phi_gpu_kernels.cpp.inc" // NOLINT - -namespace infrt { -namespace phi { - -void PHICPUKernelDialect::initialize() { -#define GET_OP_LIST - addOperations< -#include "paddle/infrt/dialect/phi/ir/phi_cpu_kernels.cpp.inc" // NOLINT - >(); -} - -void PHIGPUKernelDialect::initialize() { -#define GET_OP_LIST - addOperations< -#include "paddle/infrt/dialect/phi/ir/phi_gpu_kernels.cpp.inc" // NOLINT - >(); -} - -} // namespace phi -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/ir/phi_kernels.h b/paddle/infrt/dialect/phi/ir/phi_kernels.h deleted file mode 100644 index 9321ebb148f..00000000000 --- a/paddle/infrt/dialect/phi/ir/phi_kernels.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" -#include "paddle/infrt/dialect/phi/ir/phi_cpu_kernelsDialect.h.inc" -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/phi/ir/phi_cpu_kernels.h.inc" -#include "paddle/infrt/dialect/phi/ir/phi_gpu_kernelsDialect.h.inc" -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/phi/ir/phi_gpu_kernels.h.inc" diff --git a/paddle/infrt/dialect/phi/pass/CMakeLists.txt b/paddle/infrt/dialect/phi/pass/CMakeLists.txt deleted file mode 100644 index e664e05f9dd..00000000000 --- a/paddle/infrt/dialect/phi/pass/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -core_gather_headers() - -gather_srcs(infrt_src SRCS proto_arg_map_context.cc phi_op_convert_pass.cc - kernel_op_desc.cc) - -cc_test( - test_kernel_op_desc - SRCS kernel_op_desc_test.cc - DEPS infrt) diff --git a/paddle/infrt/dialect/phi/pass/kernel_op_desc.cc b/paddle/infrt/dialect/phi/pass/kernel_op_desc.cc deleted file mode 100644 index ff870a06752..00000000000 --- a/paddle/infrt/dialect/phi/pass/kernel_op_desc.cc +++ /dev/null @@ -1,114 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/phi/pass/kernel_op_desc.h" - -#include - -#include "paddle/infrt/dialect/phi/data_type.h" -#include "paddle/phi/core/type_defs.h" -#include "paddle/phi/kernels/declarations.h" - -namespace infrt { - -std::string getPhiTargetPrefix(TargetType target) { - switch (target) { - case TargetType::CPU: - return "phi_cpu."; - case TargetType::GPU: - return "phi_gpu."; - default: - LOG(FATAL) << "UnSupported target type !"; - return std::string(); - } -} -std::string getPhiPrecisionSuffix(PrecisionType precision) { - switch (precision) { - case PrecisionType::FLOAT32: - return ".float32"; - case PrecisionType::FLOAT16: - return ".float16"; - case PrecisionType::FLOAT64: - return ".float64"; - case PrecisionType::UINT8: - return ".uint8"; - case PrecisionType::INT8: - return ".int8"; - case PrecisionType::INT16: - return ".int16"; - case PrecisionType::INT32: - return ".int32"; - case PrecisionType::INT64: - return ".int64"; - case PrecisionType::COMPLEX64: - return ".complex64"; - case PrecisionType::COMPLEX128: - return ".complex128"; - case PrecisionType::BOOL: - return ".bool"; - default: - LOG(FATAL) << "UnSupported precision type !"; - return std::string(); - } -} -std::string getPhiLayoutSuffix(LayoutType layout) { - switch (layout) { - case LayoutType::NCHW: - return ".nchw"; - case LayoutType::NHWC: - return ".nhwc"; - case LayoutType::ANY: - return ".any"; - default: - LOG(FATAL) << "UnSupported layout type !"; - return std::string(); - } -} - -std::vector GetCandidateKernels( - std::string name, const std::vector& valid_palces) { - std::vector candidate_kernels; - PhiKernelDesc phi_kernel_desc; - phi::KernelKeyMap kernel_key_map = - phi::KernelFactory::Instance().SelectKernelMap(name); - for (Place place : valid_palces) { - phi::KernelKey kernel_key = ConvertPlaceToPhi(place); - if (kernel_key_map.find(kernel_key) == kernel_key_map.end()) { - kernel_key = phi::KernelKey(kernel_key.backend(), - phi::DataLayout::ALL_LAYOUT, - kernel_key.dtype()); - if (kernel_key_map.find(kernel_key) == kernel_key_map.end()) continue; - place.layout = LayoutType::ANY; - } - phi_kernel_desc.kernel_type = place; - phi_kernel_desc.input_types.clear(); - phi_kernel_desc.output_types.clear(); - phi::KernelArgsDef args_def = kernel_key_map.at(kernel_key).args_def(); - const paddle::small_vector& - input_arg = args_def.input_defs(); - const paddle::small_vector& - output_arg = args_def.output_defs(); - for (auto tensor_arg : input_arg) { - phi_kernel_desc.input_types.emplace_back(ConvertPlaceFromPhi(tensor_arg)); - } - for (auto tensor_arg : output_arg) { - phi_kernel_desc.output_types.emplace_back( - ConvertPlaceFromPhi(tensor_arg)); - } - candidate_kernels.emplace_back(phi_kernel_desc); - } - return candidate_kernels; -} - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/pass/kernel_op_desc.h b/paddle/infrt/dialect/phi/pass/kernel_op_desc.h deleted file mode 100644 index 4385d3c9417..00000000000 --- a/paddle/infrt/dialect/phi/pass/kernel_op_desc.h +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include - -#include "paddle/infrt/dialect/infrt/common/types.h" - -namespace infrt { - -struct PhiKernelDesc { - std::vector input_types; // kernel input place - std::vector output_types; // kernel output place - Place kernel_type; // kernel place -}; - -std::string getPhiTargetPrefix(TargetType target); -std::string getPhiPrecisionSuffix(PrecisionType precision); -std::string getPhiLayoutSuffix(LayoutType layout); - -std::vector GetCandidateKernels( - std::string name, const std::vector& valid_palces); - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/pass/kernel_op_desc_test.cc b/paddle/infrt/dialect/phi/pass/kernel_op_desc_test.cc deleted file mode 100644 index 530d0981f1e..00000000000 --- a/paddle/infrt/dialect/phi/pass/kernel_op_desc_test.cc +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/phi/pass/kernel_op_desc.h" - -#include -#include - -#include - -#include "paddle/phi/kernels/declarations.h" - -namespace infrt { - -TEST(phi, get_op_desc) { - std::vector places; - places.emplace_back( - TargetType::CPU, PrecisionType::FLOAT32, LayoutType::NCHW); - auto kernels = GetCandidateKernels("addmm", places); - ASSERT_GE(kernels.size(), 1UL); -} - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.cc b/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.cc deleted file mode 100644 index f4de56b42a6..00000000000 --- a/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.cc +++ /dev/null @@ -1,359 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "paddle/infrt/common/string.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" -#include "paddle/infrt/dialect/phi/ir/phi_kernels.h" -#include "paddle/infrt/dialect/phi/pass/kernel_op_desc.h" -#include "paddle/infrt/dialect/phi/pass/proto_arg_map_context.h" -#include "paddle/phi/core/compat/op_utils.h" -#include "paddle/phi/core/kernel_factory.h" -#include "paddle/phi/ops/compat/signatures.h" - -namespace { - -infrt::Place ParsePlaceFromStr(const std::string &key) { - size_t first_index = key.find_first_of('-'); - size_t second_index = key.find_last_of('-'); - if (first_index != second_index) { - llvm::Optional tar = - infrt::GetTargetType(key.substr(0, first_index)); - llvm::Optional pre = infrt::GetPrecisionType( - key.substr(first_index + 1, second_index - first_index - 1)); - llvm::Optional lay = - infrt::GetLayoutType(key.substr(second_index + 1)); - if (tar && pre && lay) { - return infrt::Place(tar.getValue(), pre.getValue(), lay.getValue()); - } - } - LOG(FATAL) << "Can't parse infrt::Place from string:" << key; - return infrt::Place(); -} - -class PhiOpConvertPass - : public mlir::PassWrapper { - public: - ::llvm::StringRef getName() const override { return "PhiOpConvertPass"; } - void runOnFunction() override; - - /// Initialize the valid_places_ by the valid_places_options_ while - /// valid_places_options_ has values. - mlir::LogicalResult initialize(mlir::MLIRContext *context) override; - PhiOpConvertPass() {} - explicit PhiOpConvertPass(const std::vector &valid_places) - : valid_places_(valid_places) {} - - PhiOpConvertPass(const PhiOpConvertPass &other) - : mlir::PassWrapper(*this), - valid_places_(other.valid_places_) {} - - ::llvm::StringRef getArgument() const override { return "phi-op-convert"; } - void getDependentDialects(mlir::DialectRegistry ®istry) const override; - - private: - void updateInputsAndResults(infrt::TargetType target); - void convertStage(); - void dispatchStage(); - - ListOption valid_places_options_{ - *this, - "valid-targets", - llvm::cl::desc( - "Set the valids target, such as: CPU-FP32-NCHW,GPU-FP32-NCHW"), - llvm::cl::MiscFlags::CommaSeparated}; - - std::vector valid_places_; -}; - -/// Initialize the canonicalizer by building the set of patterns used during -/// execution. -mlir::LogicalResult PhiOpConvertPass::initialize(mlir::MLIRContext *context) { - if (valid_places_options_.hasValue()) { - VLOG(4) << "Start parse valid_places from commond line:"; - if (!valid_places_.empty()) { - LOG(WARNING) << "Find valid place from commandline, current value will " - "be overwrittern."; - valid_places_.clear(); - } - for (auto &val : *valid_places_options_) { - VLOG(4) << "place string:" << val; - valid_places_.emplace_back(ParsePlaceFromStr(val)); - } - VLOG(4) << "End parse valid_places from commond line:"; - } - return mlir::success(); -} - -// Implementation of the PhiOpConvertPass. -void PhiOpConvertPass::runOnFunction() { - updateInputsAndResults(valid_places_[0].target); - convertStage(); - dispatchStage(); -} - -void PhiOpConvertPass::updateInputsAndResults(infrt::TargetType target) { - mlir::Block &body = getFunction().front(); - auto loc = getFunction().getLoc(); - mlir::Operation &operation = body.front(); - mlir::MLIRContext *context = operation.getContext(); - size_t num_input = body.getNumArguments(); - - // step1. update input cpu tensors into gpu tensors - for (size_t index = 0; index < num_input; index++) { - auto argument = body.getArgument(index); - if (auto t = argument.getType().dyn_cast<::infrt::DenseTensorType>()) { - mlir::Type replace_type = infrt::DenseTensorType::get( - context, target, t.getPrecision(), infrt::LayoutType::NCHW); - getFunction().insertArgument(index, replace_type, {}, loc); - argument.replaceAllUsesWith(getFunction().getArgument(index)); - getFunction().eraseArgument(index + 1); - } - } - // update output tensors - unsigned int num_result = getFunction().getNumResults(); - for (unsigned int index = 0; index < num_result; index++) { - mlir::Type replace_type = - infrt::DenseTensorType::get(context, - target, - infrt::PrecisionType::FLOAT32, - infrt::LayoutType::NCHW); - getFunction().eraseResult(index); - getFunction().insertResult(index, replace_type, {}); - } - // update dense_tensor_map - mlir::Type replace_type = infrt::DenseTensorType::get( - context, target, infrt::PrecisionType::FLOAT32, infrt::LayoutType::NCHW); - - for (auto &op : body.without_terminator()) { - if (op.getName().getIdentifier().str() == "phi_dt.tensor_map_get_tensor") - op.getResult(0).setType(replace_type); - } -} - -void PhiOpConvertPass::convertStage() { - mlir::Block &body = getFunction().front(); - std::vector worklist; - for (auto &op : body.without_terminator()) { - worklist.push_back(&op); - } - mlir::OpBuilder builder(&body, body.begin()); - while (!worklist.empty()) { - auto *op = worklist.back(); - worklist.pop_back(); - if (!op) continue; - - auto op_name = op->getName().getIdentifier().str(); - - // only convert op in pd dialect. - if (op_name.substr(0, 3) != "pd.") continue; - op_name = op_name.substr(3); - if (pd_dialect_inputs_info_map_.find(op_name) == - pd_dialect_inputs_info_map_.end() || - pd_dialect_outputs_info_map_.find(op_name) == - pd_dialect_outputs_info_map_.end()) { - LOG(WARNING) << "No op info found for " << op_name; - // Todo: print log - continue; - } - auto loc = getFunction().getLoc(); - builder.setInsertionPoint(op); - - if (!::phi::OpUtilsMap::Instance().HasArgumentMappingFn(op_name)) { - op_name = phi::TransToPhiKernelName(op_name); - auto kernel_op = builder.create(loc, - op->getResultTypes(), - op->getOperands(), - op_name, - op->getAttrDictionary()); - op->replaceAllUsesWith(kernel_op.getResults()); - } else { - ::phi::KernelSignature kernel_sign = - (*::phi::OpUtilsMap::Instance().GetArgumentMappingFn(op_name))( - infrt::ProtoArgumentMappingContext(op)); - VLOG(3) << "IncompatiblePhiKernel: op(" << op_name << "), kernel(" - << kernel_sign.name << ")"; - // resort input&output according to kernel_sign - ::llvm::SmallVector inputs, ori_output; - ::llvm::SmallVector output_types; - for (const std::string &str : kernel_sign.input_names) { - if (pd_dialect_inputs_info_map_.at(op_name).count(str) == 0) { - LOG(ERROR) << "No input info for Op " << op_name << " and argument " - << str; - return; - } - uint8_t index = pd_dialect_inputs_info_map_.at(op_name).at(str); - inputs.push_back(op->getOperands()[index]); - } - - for (const std::string &str : kernel_sign.output_names) { - if (pd_dialect_outputs_info_map_.at(op_name).count(str) == 0) { - LOG(ERROR) << "No output info for Op " << op_name << " and argument " - << str; - return; - } - uint8_t index = pd_dialect_outputs_info_map_.at(op_name).at(str); - output_types.push_back(op->getResultTypes()[index]); - ori_output.push_back(op->getResult(index)); - } - auto kernel_op = builder.create( - loc, output_types, inputs, kernel_sign.name, op->getAttrDictionary()); - for (size_t index = 0; index < ori_output.size(); ++index) { - ori_output[index].replaceAllUsesWith(kernel_op.getResult(index)); - } - } - CHECK(op->use_empty()); - op->erase(); - } -} - -void PhiOpConvertPass::dispatchStage() { - std::vector worklist; - mlir::Block &block = getFunction().front(); - for (auto &op : block) { - infrt::KernelOp kernel_op = ::llvm::dyn_cast_or_null(&op); - if (nullptr != kernel_op) worklist.push_back(kernel_op); - } - - mlir::OpBuilder builder(&block, block.begin()); - std::map phi_context; - - for (infrt::KernelOp kernel_op : worklist) { - std::string kernel_name = kernel_op.name().str(); - std::vector candidates = - GetCandidateKernels(kernel_name, valid_places_); - if (candidates.empty()) { - LOG(FATAL) << "No candidate kernels for op:" << kernel_name; - continue; - } - builder.setInsertionPoint(kernel_op); - - // Todo: Implimentation the concrete pass pick strategy - const infrt::PhiKernelDesc &phi_kernel_desc = candidates.front(); - - kernel_name = - infrt::getPhiTargetPrefix(phi_kernel_desc.kernel_type.target) + - kernel_name + - infrt::getPhiPrecisionSuffix(phi_kernel_desc.kernel_type.precision) + - infrt::getPhiLayoutSuffix(phi_kernel_desc.kernel_type.layout); - - mlir::OperationName operation_name(kernel_name, kernel_op.getContext()); - mlir::OperationState operation_state(kernel_op.getLoc(), operation_name); - - if (phi_context.find(phi_kernel_desc.kernel_type.target) == - phi_context.end()) { - switch (phi_kernel_desc.kernel_type.target) { - case infrt::TargetType::CPU: { - auto context_value = - builder - .create( - kernel_op.getLoc(), - infrt::phi::ContextType::get(kernel_op.getContext(), - infrt::TargetType::CPU)) - .output(); - phi_context[infrt::TargetType::CPU] = context_value; - } break; - case infrt::TargetType::GPU: { - auto context_value = - builder - .create( - kernel_op.getLoc(), - infrt::phi::ContextType::get(kernel_op.getContext(), - infrt::TargetType::GPU)) - .output(); - phi_context[infrt::TargetType::GPU] = context_value; - } break; - case infrt::TargetType::UNK: - default: - LOG(FATAL) << "Unsupported TargetType"; - break; - } - } - operation_state.addOperands( - phi_context.at(phi_kernel_desc.kernel_type.target)); - - for (size_t index = 0; index < phi_kernel_desc.input_types.size(); - ++index) { - mlir::Value input = kernel_op.getOperand(index); - if (input.getType().dyn_cast<::infrt::DenseTensorType>().getTarget() == - ::infrt::TargetType::CPU && - phi_kernel_desc.input_types[index].target == - ::infrt::TargetType::GPU) { - auto cvt_tensor_type_op = builder.create( - kernel_op.getLoc(), - infrt::DenseTensorType::get( - kernel_op.getContext(), - phi_kernel_desc.input_types[index].target, - phi_kernel_desc.input_types[index].precision, - phi_kernel_desc.input_types[index].layout), - input, - phi_context[infrt::TargetType::GPU], - mlir::BoolAttr::get(kernel_op.getContext(), /*d2h*/ false)); - - operation_state.addOperands(cvt_tensor_type_op.output()); - } else { - operation_state.addOperands(input); - } - } - - for (size_t index = 0; index < phi_kernel_desc.output_types.size(); - ++index) { - operation_state.addTypes(infrt::DenseTensorType::get( - kernel_op.getContext(), - phi_kernel_desc.output_types[index].target, - phi_kernel_desc.output_types[index].precision, - phi_kernel_desc.output_types[index].layout)); - } - operation_state.addAttributes(kernel_op.attrsAttr().getValue()); - mlir::Operation *phi_operation = builder.createOperation(operation_state); - for (size_t index = 0; index < phi_kernel_desc.output_types.size(); - ++index) { - kernel_op.getResult(index).replaceAllUsesWith( - phi_operation->getResult(index)); - } - kernel_op.erase(); - } -} - -void PhiOpConvertPass::getDependentDialects( - mlir::DialectRegistry ®istry) const { - registry.insert(); - registry.insert(); - registry.insert(); - registry.insert(); - registry.insert(); -} - -} // namespace - -mlir::PassRegistration phi_op_convert; - -std::unique_ptr infrt::CreatePhiOpCvtPass( - std::vector valid_places) { - return std::make_unique(valid_places); -} diff --git a/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h b/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h deleted file mode 100644 index 9748e1679d3..00000000000 --- a/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -#include "paddle/infrt/dialect/infrt/common/types.h" - -namespace infrt { -/* - * phiOpCvtPass. - * Convert the general operators from pd Dialect to phi dialect. - */ -std::unique_ptr CreatePhiOpCvtPass(std::vector valid_places); - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/pass/proto_arg_map_context.cc b/paddle/infrt/dialect/phi/pass/proto_arg_map_context.cc deleted file mode 100644 index 6f5db28abf6..00000000000 --- a/paddle/infrt/dialect/phi/pass/proto_arg_map_context.cc +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/phi/pass/proto_arg_map_context.h" - -namespace infrt { - -bool ProtoArgumentMappingContext::HasInput(const std::string& name) const { - if (input_map_.find(name) == input_map_.end()) { - return false; - } - uint8_t index = input_map_.at(name); - return static_cast(op_->getOperand(index)); -} - -bool ProtoArgumentMappingContext::HasOutput(const std::string& name) const { - if (output_map_.find(name) == output_map_.end()) { - return false; - } - return true; -} - -bool ProtoArgumentMappingContext::HasAttr(const std::string& name) const { - if (name == "is_test") return true; - return op_->hasAttr(name); -} - -paddle::any ProtoArgumentMappingContext::Attr(const std::string& name) const { - if (name == "is_test") { - return paddle::any(true); - } - mlir::Attribute attr = op_->getAttr(name); - if (!attr) { - return paddle::any(); - } - if (mlir::StringAttr str_attr = attr.dyn_cast()) { - return paddle::any(str_attr.str()); - } - - // ToDO: implementation in the ext PR. - return paddle::any(0); -} - -size_t ProtoArgumentMappingContext::InputSize(const std::string& name) const { - return op_->getNumOperands(); -} -size_t ProtoArgumentMappingContext::OutputSize(const std::string& name) const { - return op_->getNumResults(); -} - -bool ProtoArgumentMappingContext::IsDenseTensorInput( - const std::string& name) const { - return true; -} - -bool ProtoArgumentMappingContext::IsDenseTensorInputs( - const std::string& name) const { - return true; -} - -bool ProtoArgumentMappingContext::IsSelectedRowsInputs( - const std::string& name) const { - return false; -} - -bool ProtoArgumentMappingContext::IsSelectedRowsInput( - const std::string& name) const { - return false; -} - -bool ProtoArgumentMappingContext::IsDenseTensorVectorInput( - const std::string& name) const { - return false; -} - -bool ProtoArgumentMappingContext::IsDenseTensorOutput( - const std::string& name) const { - return true; -} -bool ProtoArgumentMappingContext::IsSelectedRowsOutput( - const std::string& name) const { - return false; -} - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/pass/proto_arg_map_context.h b/paddle/infrt/dialect/phi/pass/proto_arg_map_context.h deleted file mode 100644 index 7c0bd0ff399..00000000000 --- a/paddle/infrt/dialect/phi/pass/proto_arg_map_context.h +++ /dev/null @@ -1,62 +0,0 @@ -/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#pragma once - -#include - -#include - -#include "paddle/infrt/dialect/pd/common/pd_ops_info.h" -#include "paddle/phi/core/compat/arg_map_context.h" - -namespace infrt { -class ProtoArgumentMappingContext : public ::phi::ArgumentMappingContext { - public: - // only support op in pd dialect - explicit ProtoArgumentMappingContext(mlir::Operation* op) - : op_(op), - input_map_(pd_dialect_inputs_info_map_.at( - op->getName().getIdentifier().str().substr(3))), - output_map_(pd_dialect_outputs_info_map_.at( - op->getName().getIdentifier().str().substr(3))) {} - bool HasInput(const std::string& name) const override; - bool HasOutput(const std::string& name) const override; - bool HasAttr(const std::string& name) const override; - - // now we can't use Attribute here, it will cause phi relay on - // paddle::variant and BlockDesc - paddle::any Attr(const std::string& name) const override; - - size_t InputSize(const std::string& name) const override; - size_t OutputSize(const std::string& name) const override; - - bool IsDenseTensorInput(const std::string& name) const override; - bool IsDenseTensorInputs(const std::string& name) const override; - bool IsSelectedRowsInput(const std::string& name) const override; - bool IsSelectedRowsInputs(const std::string& name) const override; - bool IsDenseTensorVectorInput(const std::string& name) const override; - - bool IsDenseTensorOutput(const std::string& name) const override; - bool IsSelectedRowsOutput(const std::string& name) const override; - - bool IsForInferShape() const override { return false; } - - private: - mlir::Operation* op_; - const std::unordered_map& input_map_; - const std::unordered_map& output_map_; -}; - -} // namespace infrt diff --git a/paddle/infrt/dialect/phi/phi_exec.cc b/paddle/infrt/dialect/phi/phi_exec.cc deleted file mode 100644 index 0aae8cc9337..00000000000 --- a/paddle/infrt/dialect/phi/phi_exec.cc +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/paddle_mlir.h" - -void print_usage() { - std::cout << "Error inputs format, two kinds of inputs are supported:\n"; - std::cout << " [1] ./paddle-mlir-convert $path_to_model_file " - "$path_to_params_file\n"; - std::cout << " [2] ./paddle-mlir-convert $path_to_model_dir(__model__ + " - "params)\n"; -} - -bool parse_inputs(int argc, - char** argv, - std::string* model_file_name, - std::string* params_file_name) { - switch (argc) { - case 1: { - print_usage(); - return false; - } - case 2: { - *model_file_name = std::string(argv[1]) + std::string("/__model__"); - *params_file_name = std::string(argv[1]) + std::string("/params"); - return true; - } - case 3: { - *model_file_name = argv[1]; - *params_file_name = argv[2]; - return true; - } - default: { - return false; - } - } -} - -int main(int argc, char** argv) { - std::string model_file_name; - std::string params_file_name; - if (parse_inputs(argc, argv, &model_file_name, ¶ms_file_name)) { - MLIRModelGenImpl myGen; - auto module_ = myGen.ImportPaddleModel(model_file_name, params_file_name); - module_.dump(); - } -} diff --git a/paddle/infrt/dialect/print_ir.cc b/paddle/infrt/dialect/print_ir.cc deleted file mode 100644 index a240cebe736..00000000000 --- a/paddle/infrt/dialect/print_ir.cc +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/dialect/init_dialects.h" - -namespace cl = llvm::cl; - -static cl::opt inputFilename(cl::Positional, - cl::desc(""), - cl::init("-"), - cl::value_desc("filename")); - -llvm::raw_ostream &printIndent(int indent = 0) { - for (int i = 0; i < indent; ++i) llvm::outs() << " "; - return llvm::outs(); -} - -void printOperation(mlir::Operation *op, int indent); -void printRegion(mlir::Region ®ion, int indent); // NOLINT -void printBlock(mlir::Block &block, int indent); // NOLINT - -void printOperation(mlir::Operation *op, int indent) { - llvm::Optional module_op = llvm::None; - if (llvm::isa(op)) - module_op = llvm::dyn_cast(op); - llvm::Optional func_op = llvm::None; - if (llvm::isa(op)) func_op = llvm::dyn_cast(op); - - printIndent(indent) << "op: '" << op->getName(); - // This getName is inherited from Operation::getName - if (module_op) { - printIndent() << "@" << module_op->getName(); - } - // This getName is inherited from SymbolOpInterfaceTrait::getName, - // which return value of "sym_name" in ModuleOp or FuncOp attributes. - if (func_op) { - printIndent() << "@" << func_op->getName(); - } - printIndent() << "' with " << op->getNumOperands() << " operands" - << ", " << op->getNumResults() << " results" - << ", " << op->getAttrs().size() << " attributes" - << ", " << op->getNumRegions() << " regions" - << ", " << op->getNumSuccessors() << " successors\n"; - if (!op->getAttrs().empty()) { - printIndent(indent) << op->getAttrs().size() << " attributes:\n"; - for (mlir::NamedAttribute attr : op->getAttrs()) { - printIndent(indent + 1) - << "- {" << attr.getName() << " : " << attr.getValue() << "}\n"; - } - } - - if (op->getNumRegions() > 0) { - printIndent(indent) << op->getNumRegions() << " nested regions:\n"; - for (mlir::Region ®ion : op->getRegions()) { - printRegion(region, indent + 1); - } - } -} - -void printRegion(mlir::Region ®ion, int indent) { // NOLINT - printIndent(indent) << "Region with " << region.getBlocks().size() - << " blocks:\n"; - for (mlir::Block &block : region.getBlocks()) { - printBlock(block, indent + 1); - } -} - -void printBlock(mlir::Block &block, int indent) { // NOLINT - printIndent(indent) << "Block with " << block.getNumArguments() - << " arguments" - << ", " << block.getNumSuccessors() << " successors" - << ", " << block.getOperations().size() - << " operations\n"; - - for (mlir::Operation &operation : block.getOperations()) { - printOperation(&operation, indent + 1); - } -} - -int main(int argc, char **argv) { - mlir::registerAsmPrinterCLOptions(); - mlir::registerMLIRContextCLOptions(); - mlir::registerPassManagerCLOptions(); - cl::ParseCommandLineOptions(argc, argv, "mlir demo"); - - mlir::DialectRegistry registry; - infrt::registerCinnDialects(registry); - mlir::MLIRContext context(registry); - // mlir will verify module automatically after parsing. - // https://github.com/llvm/llvm-project/blob/38d18d93534d290d045bbbfa86337e70f1139dc2/mlir/lib/Parser/Parser.cpp#L2051 - // mlir::OwningModuleRef module_ref = mlir::parseSourceString(mlir_source, - // context); - mlir::OwningModuleRef module_ref = - mlir::parseSourceFile(inputFilename, &context); - std::cout << "----------print IR Structure begin----------" << std::endl; - printOperation(module_ref->getOperation(), 0); - std::cout << "----------print IR Structure end----------" << std::endl; - - module_ref->dump(); - return 0; -} diff --git a/paddle/infrt/dialect/tensor_shape.cc b/paddle/infrt/dialect/tensor_shape.cc deleted file mode 100644 index 9a825224f1d..00000000000 --- a/paddle/infrt/dialect/tensor_shape.cc +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/tensor_shape.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace infrt { -namespace ts { -using namespace mlir; // NOLINT - -void TensorShapeDialect::initialize() { - allowUnknownTypes(); - addTypes(); - addOperations< -#define GET_OP_LIST -#include "paddle/infrt/dialect/tensor_shape.cpp.inc" - >(); -} - -Type TensorShapeDialect::parseType(DialectAsmParser &parser) const { - StringRef keyword; - if (parser.parseKeyword(&keyword)) return Type(); - if (keyword == "shape") return ShapeType::get(getContext()); - if (keyword == "partial_shape") return PartialShapeType::get(getContext()); - - parser.emitError(parser.getNameLoc(), "unknown shape type: ") << keyword; - return Type(); -} - -void TensorShapeDialect::printType(mlir::Type type, - mlir::DialectAsmPrinter &os) const { - if (type.isa()) { - os << "shape"; - return; - } - - if (type.isa()) { - os << "partial_shape"; - return; - } - llvm_unreachable("unexpected 'shape' type kind"); -} -} // namespace ts -} // namespace infrt - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/tensor_shape.cpp.inc" // NOLINT -#include "paddle/infrt/dialect/tensor_shape_dialect.cpp.inc" diff --git a/paddle/infrt/dialect/tensor_shape.h b/paddle/infrt/dialect/tensor_shape.h deleted file mode 100644 index af892af735d..00000000000 --- a/paddle/infrt/dialect/tensor_shape.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include - -namespace infrt { -namespace ts { - -class ShapeType - : public mlir::Type::TypeBase { - public: - using Base::Base; -}; - -class PartialShapeType : public mlir::Type::TypeBase { - public: - using Base::Base; -}; -} // namespace ts -} // namespace infrt - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/tensor_shape.hpp.inc" -#include "paddle/infrt/dialect/tensor_shape_dialect.hpp.inc" diff --git a/paddle/infrt/dialect/tensor_shape.td b/paddle/infrt/dialect/tensor_shape.td deleted file mode 100644 index 2be21d6aa77..00000000000 --- a/paddle/infrt/dialect/tensor_shape.td +++ /dev/null @@ -1,49 +0,0 @@ -#ifdef INFRT_OPS -#else -#define INFRT_OPS - -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "paddle/infrt/dialect/tensor_shape_base.td" -include "mlir/Interfaces/SideEffectInterfaces.td" - -// Base class for the operation in the TensorShape dialect -class TS_Op traits = []> : - Op { - let parser = [{ return infrt::dialect::parse$cppClass(parser, result); }]; - let printer = " return infrt::dialect::printOpWithOperands(p, *this)" ";"; -} - -def TS_BuildShapeOp : TS_Op<"build_shape", [NoSideEffect]> { - let summary = "Build tensor shape operation"; - let description = [{ - An operation that builds a tensor shape of given ranks and extents. - }]; - - let arguments = (ins I64ArrayAttr:$value); - let results = (outs TS_Shape:$output); - let assemblyFormat = "$value attr-dict"; -} - -def TS_GetNumElementsOp : TS_Op<"get_num_elements"> { - let summary = "Returns the number of elements in the shape"; - - let description = [{ - An operation that returns the number of elements in the given shape. - }]; - - let arguments = (ins TS_Shape); - let results = (outs I64); - let assemblyFormat = "operands attr-dict"; -} - -def TS_PrintShapeOp : TS_Op<"print_shape"> { - let summary = "Print tensor shape operation"; - let description = [{ - An operation that prints a tensor shape. - }]; - - let arguments = (ins TS_Shape:$shape); - let assemblyFormat = "operands attr-dict"; -} - -#endif diff --git a/paddle/infrt/dialect/tensor_shape_base.td b/paddle/infrt/dialect/tensor_shape_base.td deleted file mode 100644 index c3988307f4d..00000000000 --- a/paddle/infrt/dialect/tensor_shape_base.td +++ /dev/null @@ -1,36 +0,0 @@ -#ifdef TS_OPS_BASE -#else -#define TS_OPS_BASE - -// Tensor shape dialect. -def TensorShapeDialect : Dialect { - let name = "ts"; - - let description = [{ - The Tensor Shape dialect. - - This dialect contains operations for working with tensor shapes. - }]; - - let cppNamespace = "::infrt::ts"; -} - -// Type definition. -def TS_Shape : DialectType()">, "!ts.shape type">, -BuildableType<"$_builder.getType<::infrt::ts::ShapeType>()"> { - let description = [{ - `!ts.shape type` represents a static tensor shape. -}]; -} - -def TS_PartialShape : DialectType()">, "!ts.partial_shape type">, -BuildableType<"$_builder.getType<::infrt::ts::PartialShapeType>()"> { - let description = [{ - `!ts.partial_shape type` represents either a static tensor shape, unranked - tensor shape or a ranked tensor shape with unknown dimension sizes. -}]; -} - -#endif // TS_OPS_BASE diff --git a/paddle/infrt/dialect/tensorrt/CMakeLists.txt b/paddle/infrt/dialect/tensorrt/CMakeLists.txt deleted file mode 100755 index 68c6da27464..00000000000 --- a/paddle/infrt/dialect/tensorrt/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -core_gather_headers() - -gather_srcs( - infrt_src - SRCS - trt_ops.cc - trt_op_converter_pass.cc - trt_op_teller_pass.cc - trt_graph_fuse_pass.cc - trt_graph_split_pass.cc - trt_type_convert_pass.cc) -mlir_tablegen_on(trt_ops) -mlir_add_rewriter(pd_lower_to_trt) - -add_executable(trt-exec trt_exec.cc) -target_link_libraries(trt-exec infrt ${MLIR_IR_LIBS}) diff --git a/paddle/infrt/dialect/tensorrt/convert.h b/paddle/infrt/dialect/tensorrt/convert.h deleted file mode 100644 index 2dcd86486f5..00000000000 --- a/paddle/infrt/dialect/tensorrt/convert.h +++ /dev/null @@ -1,371 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/dialect/infrt/common/types.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" -#include "paddle/infrt/dialect/tensorrt/trt_ops.h" -#include "paddle/infrt/kernel/tensorrt/trt_helper.h" - -namespace infrt { -namespace trt { - -#ifdef INFRT_WITH_TRT - -#define STRING_TO_ENUM_TYPE(enum_type) enum_type -#define STRING_TO_ENUM_VALUE(enum_value) enum_value -#include - -#else // INFRT_WITH_TRT - -#define STRING_TO_ENUM_TYPE(enum_type) std::string -#define STRING_TO_ENUM_VALUE(enum_value) #enum_value - -#endif // INFRT_WITH_TRT - -template -::mlir::IntegerAttr createNvinferEnumAttr( - ::mlir::PatternRewriter &rewriter, // NOLINT - T enum_value) { - return rewriter.getSI32IntegerAttr((int32_t)enum_value); -} - -template <> -::mlir::IntegerAttr createNvinferEnumAttr( - ::mlir::PatternRewriter &rewriter, std::string enum_value) { // NOLINT - (void)enum_value; - return rewriter.getSI32IntegerAttr(-1); -} - -static mlir::Value createTRTConv2dOp(mlir::PatternRewriter &rewriter, // NOLINT - mlir::Operation *op, - mlir::Value input, - mlir::Value filter) { - auto conv_op = ::llvm::dyn_cast(op); - ::mlir::SmallVector<::mlir::Value, 4> operands; - operands.push_back(input); - operands.push_back(filter); - - ::mlir::SmallVector<::mlir::Type, 4> resultTypes; - for (auto v : conv_op.getODSResults(0)) { - resultTypes.push_back(v.getType()); - } - - ::mlir::SmallVector<::mlir::NamedAttribute, 8> attributes; - - auto *filter_producer = filter.getDefiningOp(); - auto create_inited_tensor_op = - llvm::dyn_cast<::infrt::phi::CreateHostInitedDenseTensorOp>( - filter_producer); - - CHECK_NOTNULL(create_inited_tensor_op); - mlir::ArrayAttr dims = create_inited_tensor_op.dims(); - CHECK_EQ(dims.size(), 4U); - CHECK(dims[0].getType().isIntOrIndex()); - - const int32_t n_output = dims[0].cast().getInt(); - const int32_t filter_h = dims[2].cast().getInt(); - const int32_t filter_w = dims[3].cast().getInt(); - - auto padding_attr = conv_op->getAttrOfType<::mlir::ArrayAttr>("paddings"); - llvm::SmallVector paddings(padding_attr.size()); - for (size_t i = 0; i < padding_attr.size(); i++) { - paddings[i] = padding_attr[i].cast().getInt(); - } - - auto dilations_attr = conv_op->getAttrOfType<::mlir::ArrayAttr>("dilations"); - llvm::SmallVector dilations(dilations_attr.size()); - for (size_t i = 0; i < dilations_attr.size(); i++) { - dilations[i] = dilations_attr[i].cast().getInt(); - } - - llvm::SmallVector nv_paddings(2); - llvm::SmallVector nv_pre_paddings(2); - llvm::SmallVector nv_post_paddings(2); - llvm::SmallVector nv_dilations({dilations[0], dilations[1]}); - int32_t nv_padding_mode = 0; // nvinfer1::PaddingMode::kEXPLICIT_ROUND_DOWN - auto padding_algorithm_attr = - conv_op->getAttrOfType<::mlir::StringAttr>("padding_algorithm"); - if (padding_algorithm_attr.strref() == "VALID") { - for (size_t i = 0; i < paddings.size(); i++) { - paddings[i] = 0; - } - } - if (padding_algorithm_attr.strref() == "SAME") { - nv_padding_mode = 2; // nvinfer1::PaddingMode::kSAME_UPPER - nv_dilations[0] = 1; - nv_dilations[1] = 1; - } - - if (paddings.size() == 2) { - nv_paddings[0] = paddings[0]; - nv_paddings[1] = paddings[1]; - } else { - CHECK_EQ(paddings.size(), 4U); - nv_pre_paddings[0] = paddings[0]; - nv_pre_paddings[1] = paddings[2]; - nv_post_paddings[0] = paddings[1]; - nv_post_paddings[1] = paddings[3]; - } - - attributes.emplace_back(rewriter.getStringAttr("out_channel_num"), - rewriter.getSI32IntegerAttr(n_output)); - - attributes.emplace_back(rewriter.getStringAttr("kernel_size"), - rewriter.getI32ArrayAttr({filter_h, filter_w})); - - attributes.emplace_back( - rewriter.getStringAttr("dilations"), - rewriter.getI32ArrayAttr({nv_dilations[0], nv_dilations[1]})); - - attributes.emplace_back(rewriter.getStringAttr("padding_mode"), - rewriter.getSI32IntegerAttr(nv_padding_mode)); - - attributes.emplace_back(rewriter.getStringAttr("paddings"), - rewriter.getI32ArrayAttr({paddings[0], paddings[1]})); - - attributes.emplace_back( - rewriter.getStringAttr("pre_paddings"), - rewriter.getI32ArrayAttr({nv_pre_paddings[0], nv_pre_paddings[1]})); - - attributes.emplace_back( - rewriter.getStringAttr("post_paddings"), - rewriter.getI32ArrayAttr({nv_post_paddings[0], nv_post_paddings[1]})); - - { - auto tblgen_attr = conv_op->getAttrOfType<::mlir::IntegerAttr>("groups"); - attributes.emplace_back(rewriter.getStringAttr("groups"), tblgen_attr); - } - { - auto tblgen_attr = conv_op->getAttrOfType<::mlir::ArrayAttr>("strides"); - attributes.emplace_back(rewriter.getStringAttr("strides"), tblgen_attr); - } - return rewriter.create( - conv_op->getLoc(), resultTypes, operands, attributes); -} - -static inline mlir::ArrayAttr TransposeWeight( - mlir::PatternRewriter &builder, // NOLINT - const mlir::ArrayAttr &weight, - const mlir::ArrayAttr &dims) { - CHECK_EQ(dims.size(), 2U); - CHECK(!dims.empty()); - CHECK(dims[0].getType().isInteger(64)); - CHECK(!weight.empty()); - CHECK(weight[0].getType().isF32()); - - int row = dims[0].cast().getInt(); - int col = dims[1].cast().getInt(); - std::vector trans_weight(weight.size()); - for (int i = 0; i < row; ++i) { - for (int j = 0; j < col; ++j) { - trans_weight[j * row + i] = - weight[i * col + j].cast().getValueAsDouble(); - } - } - return builder.getF32ArrayAttr(trans_weight); -} - -// matmul_y and elt_y is weights. -inline ::llvm::SmallVector<::mlir::Value, 4> createTrtFcOp( - mlir::PatternRewriter &builder, // NOLINT - mlir::Value matmul_x, - mlir::Value matmul_y, - mlir::Value elt_y, - mlir::Value elt_out) { - ::llvm::SmallVector<::mlir::Operation *, 4> tblgen_ops; - - auto *y_producer = matmul_y.getDefiningOp(); - auto create_inited_tensor_op = - llvm::dyn_cast<::infrt::phi::CreateHostInitedDenseTensorOp>(y_producer); - CHECK_NOTNULL(create_inited_tensor_op); - - mlir::ArrayAttr dims = create_inited_tensor_op.dims(); - CHECK_EQ(dims.size(), 2U); - - std::vector new_dims(dims.size()); - CHECK(!dims.empty()); - CHECK(dims[0].getType().isIntOrIndex()); - for (size_t i = 0; i < new_dims.size(); ++i) { - new_dims[i] = dims[dims.size() - 1 - i].cast().getInt(); - } - auto insert_point = builder.saveInsertionPoint(); - builder.setInsertionPoint(create_inited_tensor_op); - auto new_inited_op = - builder.create<::infrt::phi::CreateHostInitedDenseTensorOp>( - create_inited_tensor_op->getLoc(), - create_inited_tensor_op.output().getType(), - create_inited_tensor_op.context(), - builder.getI64ArrayAttr(new_dims), - ::infrt::LayoutAttr::get(builder.getContext(), - ::infrt::LayoutType::NCHW), - create_inited_tensor_op.lod(), - TransposeWeight(builder, create_inited_tensor_op.values(), dims)); - builder.replaceOp(create_inited_tensor_op, new_inited_op->getResults()); - builder.restoreInsertionPoint(insert_point); - - auto ods_loc = builder.getFusedLoc({y_producer->getLoc()}); - ::infrt::trt::FullyConnectedOp fc_op; - { - ::mlir::SmallVector<::mlir::Type, 4> tblgen_types; - - fc_op = builder.create<::infrt::trt::FullyConnectedOp>( - ods_loc, - elt_out.getType(), - matmul_x, - new_inited_op.output(), - elt_y, - builder.getSI32IntegerAttr(new_dims[0])); - } - - ::llvm::SmallVector<::mlir::Value, 4> tblgen_repl_values; - for (auto v : ::llvm::SmallVector<::mlir::Value, 4>{fc_op.getODSResults(0)}) { - tblgen_repl_values.push_back(v); - } - return tblgen_repl_values; -} - -inline mlir::IntegerAttr CreatePoolingType( - mlir::PatternRewriter &builder, // NOLINT - mlir::StringAttr pool_type) { - // pool_type. - auto ptype = pool_type.str(); - if (ptype == "max") { - return createNvinferEnumAttr(builder, nvinfer1::PoolingType::kMAX); - } else if (ptype == "avg") { - return createNvinferEnumAttr(builder, nvinfer1::PoolingType::kAVERAGE); - } else { - llvm_unreachable("unknown pool_type."); - return {}; - } -} - -inline mlir::IntegerAttr CreatePaddingMode( - mlir::PatternRewriter &builder, // NOLINT - mlir::StringAttr padding_algorithm, - mlir::BoolAttr ceil_mode) { - // TODO(Inference): Phi pool kernel seems not process ceil_mode. - auto padding_algo = padding_algorithm.str(); - if (padding_algo == "SAME") { - return createNvinferEnumAttr(builder, nvinfer1::PaddingMode::kSAME_UPPER); - } - if (ceil_mode.getValue() && padding_algo != "SAME") { - return createNvinferEnumAttr(builder, - nvinfer1::PaddingMode::kEXPLICIT_ROUND_UP); - } else { - return createNvinferEnumAttr(builder, - nvinfer1::PaddingMode::kEXPLICIT_ROUND_DOWN); - } -} - -inline ::llvm::SmallVector<::mlir::Value, 4> CreatePaddleTrtPoolingOp( - mlir::PatternRewriter &builder, // NOLINT - mlir::Value input, - mlir::StringAttr pool_type, - mlir::ArrayAttr ksize, - mlir::BoolAttr global_pooling, - mlir::ArrayAttr strides, - mlir::ArrayAttr paddings, - mlir::BoolAttr exclusive, - mlir::BoolAttr adaptive, - mlir::BoolAttr ceil_mode, - mlir::StringAttr data_format, - mlir::StringAttr padding_algorithm) { - ::llvm::SmallVector<::mlir::Value, 4> tblgen_repl_values; - - // TODO(inference): Support NHWC. - if (data_format.str() != "NCHW") { - CHECK(false) << "The pool2d converter now only support NCHW."; - } - - // TODO(Wilber): How to support dynamic shape? - - auto *input_producer = input.getDefiningOp(); - - // Process pool_type. - auto pool_type_attr = CreatePoolingType(builder, pool_type); - - // Update padding. - auto padding_algorithm_str = padding_algorithm.str(); - auto paddings_attr = paddings; - if (padding_algorithm_str == "EXPLICIT") { - // Do nothing on paddings. - } else if (padding_algorithm_str == "SAME") { - // We should process this case in trt network build phase. - } else if (padding_algorithm_str == "VALID") { - // Set padding to zero. - paddings_attr = builder.getI32ArrayAttr({0, 0}); - } else { - CHECK(false) << "Unknown padding_algotithm."; - } - - // if global_pooling == true or adaptive == true, padding will be ignored - // if (global_pooling.getValue() || adaptive.getValue()) { - // paddings_attr = builder.getI32ArrayAttr({0, 0}); - // } - - // if global_pooling == true, then we should update kernel size to input dims. - if (global_pooling.getValue() == true) { - // Update ksize to input dims. - } - - // The adaptive logic should be processed when we get the context of - // INetworkDefinition, so we place the logic in infrt runtime(trt compile - // time). - - // The `exclusive` may be a naive attr, which can be forward to trt. - - auto padding_mode_attr = - CreatePaddingMode(builder, padding_algorithm, ceil_mode); - - if (global_pooling.getValue() == true) { - CHECK(false) << "Temporarily not support global_pool"; - return tblgen_repl_values; - } - - PoolingOp pool_op; - { - auto ods_loc = builder.getFusedLoc({input_producer->getLoc()}); - pool_op = builder.create(ods_loc, - input.getType(), - input, - pool_type_attr, - ksize, - strides, - paddings_attr, - padding_mode_attr, - exclusive, - adaptive, - padding_algorithm); - } - - for (auto v : - ::llvm::SmallVector<::mlir::Value, 4>{pool_op.getODSResults(0)}) { - tblgen_repl_values.push_back(v); - } - return tblgen_repl_values; -} - -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/pd_lower_to_trt.td b/paddle/infrt/dialect/tensorrt/pd_lower_to_trt.td deleted file mode 100644 index 0cd100aa5b9..00000000000 --- a/paddle/infrt/dialect/tensorrt/pd_lower_to_trt.td +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef PD_LOWER_TO_TRT -#define PD_LOWER_TO_TRT - -include "mlir/Interfaces/SideEffectInterfaces.td" -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "paddle/infrt/dialect/pd/ir/pd_ops.td" -include "paddle/infrt/dialect/tensorrt/trt_ops.td" - -class TRT_createNvinferEnumAttr : NativeCodeCall< - "infrt::trt::createNvinferEnumAttr($_builder, STRING_TO_ENUM_VALUE(" # enum_type # "::" # enum_value # "))">; - -def PD2TRT_Matmul_Lower : Pat< - (PD_MatmulOp $X, $Y, $transpose_X, $transpose_Y, ConstantAttr), - (TRT_MatrixMultiplyOp $X, $transpose_X, $Y, $transpose_Y)>; - -def PD2TRT_ElementwiseAdd_Lower : Pat< - (PD_Elementwise_addOp $X, $Y, $_), - (TRT_ElementWiseOp $X, $Y, (TRT_createNvinferEnumAttr<"nvinfer1::ElementWiseOperation", "kSUM">))>; - -def PD2TRT_Relu_Lower : Pat< - (PD_ReluOp $X), - (TRT_ActivationOp $X, (TRT_createNvinferEnumAttr<"nvinfer1::ActivationType", "kRELU">), (INFRT_createF32Attr<"0.0">), (INFRT_createF32Attr<"0.0">))>; - -def PD2TRT_Relu6_Lower : Pat< - (PD_Relu6Op $X, $threshold), - (TRT_ActivationOp $X, (TRT_createNvinferEnumAttr<"nvinfer1::ActivationType", "kCLIP">), (INFRT_createF32Attr<"0.0">), $threshold)>; - -def createTRTConv2dOp : NativeCodeCall<"createTRTConv2dOp($_builder, $0.getDefiningOp(), $1, $2)">; - -def PD2TRT_Conv2d_Lower : Pat< - (PD_Conv2dOp:$old_value $Input, $Filter, $strides, $paddings, $padding_algorithm, $groups, $dilations, $data_format), - (createTRTConv2dOp $old_value, $Input, $Filter)>; - -def createTrtPoolingOp : NativeCodeCall<"::infrt::trt::CreatePaddleTrtPoolingOp($_builder, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)">; -def PD2TRT_Pooling_Lower : Pat< - (PD_Pool2dOp $Input, $pooling_type, $ksize, $global_pooling, $strides, $paddings, $exclusive, $adaptive, $ceil_mode, $data_format, $padding_algorithm), - (createTrtPoolingOp $Input, $pooling_type, $ksize, $global_pooling, $strides, $paddings, $exclusive, $adaptive, $ceil_mode, $data_format, $padding_algorithm)>; - -def PD2TRT_MatrixMultipl_Lower : Pat< - (PD_MulOp $Input1, $Input2, $x_num_col_dims, $y_num_col_dims), - (TRT_MatrixMultiplOp $Input1, (INFRT_createI32Attr<"0">)/*kNONE*/, $Input2, (INFRT_createI32Attr<"0">)/*kNONE*/)>; - -def PD2TRT_SoftMax_Lower : Pat< - (PD_SoftmaxOp $Input, $axis, $_), - (TRT_SoftMaxOp $Input, $axis)>; - -// pd.matmul_v2 + pd.elementwise_add -> trt.fc -def createTrtFcOp : NativeCodeCall<"::infrt::trt::createTrtFcOp($_builder, $0, $1, $2, $3)">; -def PD2TRT_Fc_Lower : Pat< - (PD_Elementwise_addOp:$elt_out (PD_Matmul_v2Op $X, $Y, $trans_x, $trans_y), $elt_y, $axis), - (createTrtFcOp $X, $Y, $elt_y, $elt_out)>; - -def PD2TRT_Flatten_contiguous_range_Lower : Pat< - (PD_Flatten_contiguous_rangeOp $input, $start_axis, $end_axis), - (TRT_ShuffleOp $input, $start_axis, $end_axis)>; -#endif // PD_LOWER_TO_TRT diff --git a/paddle/infrt/dialect/tensorrt/trt_dialect_types.h b/paddle/infrt/dialect/tensorrt/trt_dialect_types.h deleted file mode 100644 index 0c3edcec1ed..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_dialect_types.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "mlir/IR/Types.h" - -namespace infrt { -namespace trt { - -class EngineType - : public mlir::Type::TypeBase { - public: - using Base::Base; - static EngineType get(); - static EngineType get(mlir::MLIRContext *context); -}; - -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_exec.cc b/paddle/infrt/dialect/tensorrt/trt_exec.cc deleted file mode 100644 index d1ce1c1b562..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_exec.cc +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -#include -#include - -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.h" -#include "paddle/infrt/dialect/mlir_loader.h" -#include "paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.h" -#include "paddle/infrt/dialect/tensorrt/trt_graph_split_pass.h" -#include "paddle/infrt/dialect/tensorrt/trt_op_converter_pass.h" -#include "paddle/infrt/dialect/tensorrt/trt_op_teller_pass.h" -#include "paddle/infrt/dialect/tensorrt/trt_type_convert_pass.h" -#include "paddle/infrt/host_context/core_runtime.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/mlir_to_runtime_translate.h" -#include "paddle/infrt/kernel/basic_kernels.h" -#include "paddle/infrt/kernel/control_flow_kernels.h" -#include "paddle/infrt/kernel/tensor_kernels.h" -#include "paddle/infrt/kernel/tensor_shape_kernels.h" -#include "paddle/infrt/kernel/tensorrt/registry.h" -#include "paddle/infrt/kernel/test_kernels.h" - -#ifdef INFRT_WITH_PHI -#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h" -#include "paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h" -#include "paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h" -#include "paddle/infrt/kernel/phi/registry.h" -#endif - -#include - -int main(int argc, char** argv) { - static llvm::cl::opt input_file( - llvm::cl::Positional, - llvm::cl::desc("Specify input filename"), - llvm::cl::init("-")); - - llvm::cl::ParseCommandLineOptions(argc, argv); - - mlir::MLIRContext* context = infrt::Global::getMLIRContext(); - auto module = infrt::dialect::LoadMlirFile(input_file.c_str(), context); - - infrt::host_context::KernelRegistry registry; - - ::infrt::kernel::RegisterBasicKernels(®istry); - ::infrt::kernel::RegisterTestKernels(®istry); - ::infrt::kernel::RegisterTensorShapeKernels(®istry); - ::infrt::kernel::RegisterTensorKernels(®istry); - ::infrt::kernel::RegisterControlFlowKernels(®istry); -#ifdef INFRT_WITH_PHI - ::infrt::kernel::RegisterPhiKernels(®istry); - ::infrt::kernel::RegisterInferShapeLaunchers(®istry); -#endif -#if defined(INFRT_WITH_GPU) && defined(INFRT_WITH_TRT) - ::infrt::kernel::RegisterTrtKernels(®istry); -#endif - - context->loadAllAvailableDialects(); - // module->dump(); - mlir::PassManager pm(context); - - mlir::OpPassManager& trt_pass_manager = pm.nest(); - trt_pass_manager.addPass(::infrt::CreateInfrtWeightsUnfoldPass()); - trt_pass_manager.addPass(std::make_unique()); - trt_pass_manager.addPass(std::make_unique()); - trt_pass_manager.addPass(std::make_unique(1)); - trt_pass_manager.addPass(std::make_unique()); - trt_pass_manager.addPass(infrt::trt::CreateTrtTypeConvertPass()); - trt_pass_manager.addPass(::mlir::createCanonicalizerPass()); - if (mlir::failed(pm.run(*module))) { - std::cout << "\npass failed!\n" << std::endl; - return 4; - } - module->dump(); - ::infrt::host_context::TestMlir(module.get(), ®istry); - return 0; -} diff --git a/paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.cc b/paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.cc deleted file mode 100644 index 7109fc772ec..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.cc +++ /dev/null @@ -1,191 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.h" - -#include -#include -#include -#include - -#include -#include -#include - -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" - -namespace infrt { -namespace trt { -namespace { -// ReverseDfs -// do reverse dfs. calls "func" to search when visit a node. -// The elements in 'source' can't be nullptr. -// Reference the function nameed "FlexibleDFS" but defined in: -// paddle/fluid/framework/ir/subgraph_detector.cc. - -bool reverseDfs(std::vector source, - const std::function &func) { - std::unordered_set visited; - while (!source.empty()) { - auto node = source.back(); - source.pop_back(); - if (visited.count(node)) continue; - visited.insert(node); - if (func(node)) return true; - auto values = node->getOperands(); - for (auto value : values) { - // if the value is a block argument, the node is nullptr. - mlir::Operation *node = value.getDefiningOp(); - if (node != nullptr && !visited.count(node)) { - source.emplace_back(node); - } - } - } - return false; -} - -// merge the first&second graph op to a new graph op. -void mergeTwoAdjacentGraphOp(mlir::OpBuilder &builder, // NOLINT - ::infrt::GraphOp first, - ::infrt::GraphOp second) { - // comput inputs and outputs - ::llvm::SmallVector inputs(first.getOperands()), outputs; - for (mlir::Value input : second.getOperands()) { - if (input.getDefiningOp() != first) { - inputs.push_back(input); - } - } - ::llvm::DenseMap op_output_mapping; - for (mlir::Value output : first.getResults()) { - for (mlir::Operation *user : output.getUsers()) { - if (user != second && user->getParentOp() != second) { - op_output_mapping[output] = outputs.size(); - outputs.push_back(output); - break; - } - } - } - auto return_op = second.getBody()->getTerminator(); - outputs.append(return_op->getOperands().begin(), - return_op->getOperands().end()); - ::llvm::SmallVector return_types; - for (auto value : outputs) { - return_types.push_back(value.getType()); - } - - // create the new graph op - builder.setInsertionPoint(first); - auto loc = first.getLoc(); - auto graph_op = builder.create<::infrt::GraphOp>(loc, return_types, inputs); - mlir::Block *block = new mlir::Block; - auto copy_range = second.getBody()->without_terminator(); - block->getOperations().splice(block->begin(), - second.getBody()->getOperations(), - copy_range.begin(), - copy_range.end()); - copy_range = first.getBody()->without_terminator(); - block->getOperations().splice(block->begin(), - first.getBody()->getOperations(), - copy_range.begin(), - copy_range.end()); - builder.setInsertionPointToEnd(block); - builder.create<::infrt::ReturnOp>(loc, outputs); - graph_op.body().push_back(block); - - // mapping the output - unsigned int num_result = first.getNumResults(); - return_op = first.getBody()->getTerminator(); - for (unsigned int index = 0; index < num_result; ++index) { - auto origin_value = first.getResult(index); - if (op_output_mapping.find(origin_value) == op_output_mapping.end()) { - origin_value.replaceAllUsesWith(return_op->getOperand(index)); - } else { - auto inner_value = return_op->getOperand(index); - auto outer_value = graph_op.getResult(op_output_mapping[origin_value]); - while (!origin_value.use_empty()) { - auto replace_value = - origin_value.use_begin()->getOwner()->getParentOp() == graph_op - ? inner_value - : outer_value; - origin_value.use_begin()->set(replace_value); - } - } - } - second.replaceAllUsesWith( - graph_op.getResults().take_back(second.getNumResults())); - first.erase(); - second.erase(); -} - -// Topological sort the function op. -void topoSortBlock(mlir::Block &body) { // NOLINT - llvm::SetVector toSort; - if (body.empty()) return; - for (auto it = body.rbegin(); it != body.rend(); ++it) { - toSort.insert(&*it); - } - llvm::SetVector result = mlir::topologicalSort(toSort); - for (auto *op : result) { - op->moveBefore(body.getTerminator()); - } -} - -} // namespace - -// Implementation of the trtGraphFusePass. -void TRTGraphFusePass::runOnFunction() { - mlir::Block &body = getFunction().front(); - mlir::OpBuilder builder(&body, body.begin()); - bool changed = false; - do { - changed = false; - for (auto &op : body) { - ::infrt::GraphOp graph_op = - ::llvm::dyn_cast_or_null<::infrt::GraphOp>(&op); - if (nullptr == graph_op) continue; - - for (auto user_op : op.getUsers()) { - ::infrt::GraphOp user_graph_op = - ::llvm::dyn_cast_or_null<::infrt::GraphOp>(user_op); - if (nullptr == user_graph_op) continue; - // get all dst input nodes except src. - std::vector source_nodes; - for (auto operand : user_op->getOperands()) { - auto input = operand.getDefiningOp(); - if (input != &op && input != nullptr) { - source_nodes.push_back(input); - } - } - // Reverse DFS from the source_nodes. - if (!reverseDfs(source_nodes, - [&op](const mlir::Operation *n) { return n == &op; })) { - mergeTwoAdjacentGraphOp(builder, graph_op, user_graph_op); - changed = true; - break; - } - } - if (changed) break; - } - } while (changed); - - // TODO(wilber): Implement a toposort for efficiency. - // topoSortBlock(body); -} - -std::unique_ptr CreateTrtGraphFusePass() { - return std::make_unique(); -} - -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.h b/paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.h deleted file mode 100644 index 515e73df854..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.h +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -namespace trt { - -std::unique_ptr CreateTrtGraphFusePass(); - -/* - * trtGraphFusePass. - * - * Merge the adjacent graph op to a new graph op. - * - * source func: - * - * func @main(%a : tensor) -> tensor { - * %c = "infrt.graph"(%a) { - * %m = "pd.conv2d"(%a)... - * infrt.return %m... - * } ... - * %d = "infrt.graph"(%c) { - * %m = "pd.conv3d"(%c)... - * infrt.return %m... - * } ... - * %f = "infrt.graph"(%a) { - * %m = "pd.conv2d"(%a)... - * infrt.return %m... - * } ... - * infrt.return %d, %f :... - * } - * - * destination func: - * func @main(%a : tensor) -> tensor { - * %d, %f = "infrt.graph"(%a) { - * %m = "pd.conv2d"(%a)... - * %n = "pd.conv3d"(%m)... - * %s = "pd.conv2d"(%a)... - * infrt.return %n, %s:... - * } ... - * infrt.return %d, %f:... - * } - */ -class TRTGraphFusePass - : public mlir::PassWrapper { - public: - void getDependentDialects(mlir::DialectRegistry ®istry) const override {} - ::llvm::StringRef getName() const override { return "trtGraphFusePass"; } - void runOnFunction() override; -}; -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_graph_split_pass.cc b/paddle/infrt/dialect/tensorrt/trt_graph_split_pass.cc deleted file mode 100644 index d74fe3e5e9c..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_graph_split_pass.cc +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/tensorrt/trt_graph_split_pass.h" - -#include - -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" - -namespace infrt { -namespace trt { -// Implementation of the trtGraphSplitPass。 -void TRTGraphSplitPass::runOnFunction() { - std::vector<::infrt::GraphOp> worklist; - mlir::Block& block = getFunction().front(); - for (auto& op : block) { - ::infrt::GraphOp graph_op = ::llvm::dyn_cast_or_null<::infrt::GraphOp>(&op); - if (nullptr != graph_op && - graph_op.getBody()->getOperations().size() <= min_subgraph_size_) { - worklist.push_back(graph_op); - } - } - while (!worklist.empty()) { - ::infrt::GraphOp graph_op = worklist.back(); - worklist.pop_back(); - mlir::Block* body = graph_op.getBody(); - auto return_op = body->getTerminator(); - graph_op.replaceAllUsesWith(return_op->getOperands()); - auto copy_range = body->without_terminator(); - block.getOperations().splice(mlir::Block::iterator(graph_op), - body->getOperations(), - copy_range.begin(), - copy_range.end()); - graph_op.erase(); - } -} - -std::unique_ptr CreateTrtGraphSplitPass(size_t min_subgraph_size) { - return std::make_unique(min_subgraph_size); -} - -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_graph_split_pass.h b/paddle/infrt/dialect/tensorrt/trt_graph_split_pass.h deleted file mode 100644 index fa101a8db02..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_graph_split_pass.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -namespace trt { - -std::unique_ptr CreateTrtGraphSplitPass(size_t min_subgraph_size); - -/* - * trtGraphSplitPass. - * - * Splite the graph op when the number of operations is too small. - * The feature is the opposite of 'trtOpTellerPass'. - * - * source func: - * - * func @main(%a : tensor) -> tensor { - * %d, %f = "infrt.graph"(%a) { - * %m = "pd.conv2d"(%a)... - * %n = "pd.conv3d"(%m)... - * %s = "pd.conv2d"(%a)... - * infrt.return %n, %s : ... - * } ... - * infrt.return %d, %f : ... - * } - * - * destination func: - * func @main(%a : tensor) -> tensor { - * %c = "pd.conv2d"(%a) ... - * %d = "pd.conv3d"(%c) ... - * %f = "pd.conv2d"(%a) ... - * infrt.return %d, %f:... - * } - */ -class TRTGraphSplitPass - : public mlir::PassWrapper { - public: - ::llvm::StringRef getName() const override { return "trtGraphSplitPass"; } - void getDependentDialects(mlir::DialectRegistry ®istry) const override {} - void runOnFunction() override; - explicit TRTGraphSplitPass(size_t min_subgraph_size = 3) - : min_subgraph_size_(min_subgraph_size) {} - - private: - size_t min_subgraph_size_; -}; -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_op_base.td b/paddle/infrt/dialect/tensorrt/trt_op_base.td deleted file mode 100755 index 81cb882fdf2..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_op_base.td +++ /dev/null @@ -1,29 +0,0 @@ -// This file defines some basic elements of Paddle(alias trt) dialect. -// We learned much from TensorFlow mlir dialect https://github.com/tensorflow/tensorflow/blob/master/tensorflow/compiler/mlir/tensorflow/ir/tf_op_base.td - -#ifndef TRT_OP_BASE -#define TRT_OP_BASE - -include "mlir/IR/OpBase.td" -include "mlir/Interfaces/SideEffectInterfaces.td" - -def TRT_Dialect : Dialect { - let name = "trt"; - - let description = [{ - The PaddlePaddle dialect. - - This dialect contains the PaddlePaddle operators. - }]; - - let cppNamespace = "::infrt::trt"; -} - -class TRT_Op traits = []> : - Op; - -def TRT_EngineType : - Type()">, "!trt.engine">, - BuildableType<"getType<::infrt::trt::EngineType>()">; - -#endif // TRT_OP_BASE diff --git a/paddle/infrt/dialect/tensorrt/trt_op_converter_pass.cc b/paddle/infrt/dialect/tensorrt/trt_op_converter_pass.cc deleted file mode 100644 index 6776f01e36d..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_op_converter_pass.cc +++ /dev/null @@ -1,268 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#include "paddle/infrt/dialect/tensorrt/trt_op_converter_pass.h" - -#include -#include -#include - -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" -#include "paddle/infrt/dialect/tensorrt/convert.h" -#include "paddle/infrt/dialect/tensorrt/trt_dialect_types.h" -#include "paddle/infrt/dialect/tensorrt/trt_ops.h" - -namespace infrt { -namespace trt { - -#include "paddle/infrt/dialect/tensorrt/pd_lower_to_trt.cpp.inc" // NOLINT - -struct PD2TRT_GraphLower : public ::mlir::RewritePattern { - explicit PD2TRT_GraphLower(::mlir::MLIRContext *context) - : ::mlir::RewritePattern( - "infrt.graph", 1, context, {"trt.create_engine"}) {} - ::mlir::LogicalResult matchAndRewrite( - ::mlir::Operation *op, ::mlir::PatternRewriter &rewriter) const override { - auto casted_op = ::llvm::dyn_cast<::infrt::GraphOp>(op); - ::mlir::Operation::operand_range inputs = casted_op.inputs(); - auto ods_loc = rewriter.getFusedLoc(op->getLoc()); - CreateEngineOp create_engine_op; - // inputs - ::mlir::SmallVector<::mlir::Value, 4> trt_inputs; - for (auto v : inputs) { - trt_inputs.push_back(v); - } - create_engine_op = rewriter.create( - ods_loc, - ::llvm::SmallVector(1, EngineType::get()), - trt_inputs, - true /*run_once*/); - auto &block = create_engine_op.body().emplaceBlock(); - block.getOperations().splice(block.begin(), - casted_op.getBody()->getOperations(), - casted_op.getBody()->begin(), - casted_op.getBody()->end()); - - // trt.compute - ::llvm::SmallVector<::mlir::Value, 4> replace_values2; - auto ctx_op = rewriter.create<::infrt::phi::CreateGPUContextOp>( - ods_loc, - infrt::phi::ContextType::get(rewriter.getContext(), - infrt::TargetType::GPU)); - auto compute_op = rewriter.create( - ods_loc, - ::infrt::DenseTensorListType::get(rewriter.getContext()), - create_engine_op.engine(), - ctx_op.output()); - auto tensor_list_val = compute_op.outputs(); - for (size_t i = 0; i < casted_op.getNumResults(); ++i) { - auto res = casted_op->getResult(i); - auto int_attr = mlir::IntegerAttr::get( - mlir::IntegerType::get(rewriter.getContext(), 32), i); - auto get_tensor_op = rewriter.create<::infrt::dt::TensorListGetTensorOp>( - ods_loc, res.getType(), tensor_list_val, int_attr); - replace_values2.push_back(get_tensor_op.output()); - } - ctx_op->moveBefore(ctx_op->getBlock(), ctx_op->getBlock()->begin()); - rewriter.replaceOp(op, replace_values2); - return ::mlir::success(); - } -}; - -struct PD2TRT_Batch_Norm_Lower : public ::mlir::RewritePattern { - explicit PD2TRT_Batch_Norm_Lower(::mlir::MLIRContext *context) - : ::mlir::RewritePattern("pd.batch_norm", 1, context, {"trt.scaleNd"}) {} - ::mlir::LogicalResult matchAndRewrite( - ::mlir::Operation *op, ::mlir::PatternRewriter &rewriter) const override { - auto casted_op = ::llvm::dyn_cast(op); - ::mlir::SmallVector<::mlir::Value, 4> operands; - ::mlir::Operation::operand_range Input = casted_op.getODSOperands(0); - ::mlir::Operation::operand_range Scale = casted_op.getODSOperands(1); - ::mlir::Operation::operand_range Bias = casted_op.getODSOperands(2); - ::mlir::Operation::operand_range Mean = casted_op.getODSOperands(3); - ::mlir::Operation::operand_range Variance = casted_op.getODSOperands(4); - operands.push_back(Input[0]); - operands.push_back(Bias[0]); - operands.push_back(Scale[0]); - - // TODO(weishengying) : recompute this via params - auto *scale_producer = Scale[0].getDefiningOp(); - auto create_scale_tensor_op = - llvm::dyn_cast<::infrt::phi::CreateHostInitedDenseTensorOp>( - scale_producer); - CHECK_NOTNULL(create_scale_tensor_op); - - auto *bias_producer = Bias[0].getDefiningOp(); - auto create_bias_tensor_op = - llvm::dyn_cast<::infrt::phi::CreateHostInitedDenseTensorOp>( - bias_producer); - CHECK_NOTNULL(create_bias_tensor_op); - - auto *mean_producer = Mean[0].getDefiningOp(); - auto create_mean_tensor_op = - llvm::dyn_cast<::infrt::phi::CreateHostInitedDenseTensorOp>( - mean_producer); - CHECK_NOTNULL(create_mean_tensor_op); - - auto *variance_producer = Variance[0].getDefiningOp(); - auto create_variance_tensor_op = - llvm::dyn_cast<::infrt::phi::CreateHostInitedDenseTensorOp>( - variance_producer); - CHECK_NOTNULL(create_variance_tensor_op); - - llvm::SmallVector scale_data; - mlir::ArrayAttr scale_array_attr = create_scale_tensor_op.values(); - CHECK_GT(scale_array_attr.size(), 0U); - CHECK(scale_array_attr[0].getType().isF32()); - scale_data.resize(scale_array_attr.size()); - for (size_t i = 0; i < scale_array_attr.size(); i++) { - scale_data[i] = - scale_array_attr[i].cast().getValueAsDouble(); - } - - llvm::SmallVector bias_data; - mlir::ArrayAttr bias_array_attr = create_bias_tensor_op.values(); - CHECK_GT(bias_array_attr.size(), 0U); - CHECK(bias_array_attr[0].getType().isF32()); - bias_data.resize(bias_array_attr.size()); - for (size_t i = 0; i < bias_array_attr.size(); i++) { - bias_data[i] = - bias_array_attr[i].cast().getValueAsDouble(); - } - - llvm::SmallVector mean_data; - mlir::ArrayAttr mean_array_attr = create_mean_tensor_op.values(); - CHECK_GT(mean_array_attr.size(), 0U); - CHECK(mean_array_attr[0].getType().isF32()); - mean_data.resize(mean_array_attr.size()); - for (size_t i = 0; i < mean_array_attr.size(); i++) { - mean_data[i] = - mean_array_attr[i].cast().getValueAsDouble(); - } - - llvm::SmallVector variance_data; - mlir::ArrayAttr variance_array_attr = create_variance_tensor_op.values(); - CHECK_GT(variance_array_attr.size(), 0U); - CHECK(variance_array_attr[0].getType().isF32()); - variance_data.resize(variance_array_attr.size()); - for (size_t i = 0; i < variance_array_attr.size(); i++) { - variance_data[i] = - variance_array_attr[i].cast().getValueAsDouble(); - } - - double eps = casted_op.epsilonAttr().getValueAsDouble(); - - llvm::SmallVector combile_scale_data; - combile_scale_data.resize(scale_data.size()); - llvm::SmallVector combile_bias_data; - combile_bias_data.resize(bias_data.size()); - - size_t ele_num = combile_scale_data.size(); - for (size_t i = 0; i < ele_num; i++) { - float scale = scale_data[i]; - float bias = bias_data[i]; - float mean = mean_data[i]; - float variance = variance_data[i]; - combile_scale_data[i] = scale / sqrtf(variance + eps); - combile_bias_data[i] = bias - mean * combile_scale_data[i]; - } - - rewriter.setInsertionPoint(create_scale_tensor_op); - auto new_scale_op = - rewriter.create<::infrt::phi::CreateHostInitedDenseTensorOp>( - create_scale_tensor_op->getLoc(), - create_scale_tensor_op.output().getType(), - create_scale_tensor_op.context(), - create_scale_tensor_op.dims(), - ::infrt::LayoutAttr::get(rewriter.getContext(), - ::infrt::LayoutType::NCHW), - create_scale_tensor_op.lod(), - rewriter.getF32ArrayAttr(combile_scale_data)); - rewriter.replaceOp(create_scale_tensor_op, new_scale_op->getResults()); - - rewriter.setInsertionPoint(create_bias_tensor_op); - auto new_bias_op = - rewriter.create<::infrt::phi::CreateHostInitedDenseTensorOp>( - create_bias_tensor_op->getLoc(), - create_bias_tensor_op.output().getType(), - create_bias_tensor_op.context(), - create_bias_tensor_op.dims(), - ::infrt::LayoutAttr::get(rewriter.getContext(), - ::infrt::LayoutType::NCHW), - create_bias_tensor_op.lod(), - rewriter.getF32ArrayAttr(combile_bias_data)); - rewriter.replaceOp(create_bias_tensor_op, new_bias_op->getResults()); - - trt::ScaleNdOp scaleNd_op; - // resultTypes - ::mlir::SmallVector<::mlir::Type, 4> resultTypes; - for (auto v : casted_op.getODSResults(0)) { - resultTypes.push_back(v.getType()); - } - - // attributes - rewriter.setInsertionPoint(op); - ::mlir::SmallVector<::mlir::NamedAttribute, 8> attributes; - auto result = rewriter - .create( - op->getLoc(), resultTypes, operands, attributes) - .getODSResults(0); - ::llvm::SmallVector<::mlir::Value, 4> tblgen_repl_values; - // TODO(weishengying) : update it - for (uint32_t i = 0; i < casted_op.getNumResults(); i++) { - for (auto v : ::llvm::SmallVector<::mlir::Value, 4>{result}) { - tblgen_repl_values.push_back(v); - } - } - rewriter.replaceOp(op, tblgen_repl_values); - return ::mlir::success(); - } -}; - -void TRTOpConverterPass::runOnOperation() { - // The first thing to define is the conversion target. This will define the - // final target for this lowering. - ::mlir::ConversionTarget target(getContext()); - - // We define the specific operations, or dialects, that are legal targets for - // this lowering. In our case, we are lowering to TensorRTDialect from - // PaddleDialect - target.addLegalDialect(); - target.addLegalDialect<::infrt::phi::PHIDialect>(); - target.addLegalDialect<::infrt::dt::DTDialect>(); - target.addLegalDialect(); - - // Now that the conversion target has been defined, we just need to provide - // the set of patterns that will lower the TensorRT operations. - ::mlir::RewritePatternSet patterns(&getContext()); - populateWithGenerated(patterns); - patterns.add(&getContext()); - patterns.add(&getContext()); - - // With the target and rewrite patterns defined, we can now attempt the - // conversion. The conversion will signal failure if any of our `illegal` - // operations were not converted successfully. - if (::mlir::failed( - applyPartialConversion(getOperation(), target, std::move(patterns)))) - signalPassFailure(); -} - -std::unique_ptr CreateTrtOpConverterPass() { - return std::make_unique(); -} - -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_op_converter_pass.h b/paddle/infrt/dialect/tensorrt/trt_op_converter_pass.h deleted file mode 100644 index 84bc7194636..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_op_converter_pass.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include "mlir/IR/Dialect.h" -#include "mlir/Pass/Pass.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/tensorrt/trt_ops.h" - -namespace infrt { -namespace trt { - -std::unique_ptr CreateTrtOpConverterPass(); - -/* - * trtOpConverterPass. - * - * source ir: - * func @main(%a : tensor) -> tensor { - * %d, %f = "infrt.graph"(%a) { - * %m = "pd.conv2d"(%a)... - * %n = "pd.conv3d"(%m)... - * %s = "pd.conv2d"(%a)... - * infrt.return %n, %s:... - * } ... - * infrt.return %d, %f:... - * } - * - * destination ir: - * func @main(%a : tensor) -> tensor { - * %engine = "trt.create_engine"(%a) ({ - * %m = "trt.Convolution"(%a)... - * %n = "trt.Convolution"(%m)... - * %s = "trt.Convolution"(%a)... - * infrt.return %n, %s :... - * }){run_once = true} ... - * %d, %f = "trt.execute"(%engine, %a)... - * infrt.return %d, %f :... - * } - */ -struct TRTOpConverterPass - : public mlir::PassWrapper> { - void getDependentDialects(mlir::DialectRegistry ®istry) const override { - registry.insert(); - } - ::llvm::StringRef getName() const override { return "trtOpConverterPass"; } - void runOnOperation() final; -}; -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_op_teller_pass.cc b/paddle/infrt/dialect/tensorrt/trt_op_teller_pass.cc deleted file mode 100644 index 35b869fb307..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_op_teller_pass.cc +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/tensorrt/trt_op_teller_pass.h" - -#include -#include - -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" - -namespace infrt { -namespace trt { -// Implementation of the trtOpTellerPass。 -void TRTOpTellerPass::runOnFunction() { - mlir::Block &body = getFunction().front(); - std::vector worklist; - worklist.reserve(body.getOperations().size()); - for (auto &op : body) { - worklist.push_back(&op); - } - // Build GraphOp. - mlir::OpBuilder builder(&body, body.begin()); - while (!worklist.empty()) { - auto *op = worklist.back(); - worklist.pop_back(); - if (op == nullptr) continue; - if (op->getName().getStringRef().substr(0, 3) != "pd.") continue; - builder.setInsertionPoint(op); - auto loc = getFunction().getLoc(); - auto graph_op = builder.create<::infrt::GraphOp>( - loc, op->getResultTypes(), op->getOperands()); - - ::llvm::SmallVector tblgen_repl_values; - for (auto v : - ::llvm::SmallVector{graph_op.getODSResults(0)}) { - tblgen_repl_values.push_back(v); - } - op->replaceAllUsesWith(tblgen_repl_values); - // Build graph op. - mlir::Block *block = new mlir::Block; - graph_op.body().push_back(block); - op->moveBefore(block, block->begin()); - builder.setInsertionPointToEnd(block); - builder.create<::infrt::ReturnOp>(loc, op->getResults()); - } -} - -std::unique_ptr CreateTrtOpTellerPass() { - return std::make_unique(); -} - -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_op_teller_pass.h b/paddle/infrt/dialect/tensorrt/trt_op_teller_pass.h deleted file mode 100644 index 566c5a45da0..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_op_teller_pass.h +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -namespace trt { - -std::unique_ptr CreateTrtOpTellerPass(); - -/* - * trtOpTellerPass. - * - * Pick out the operators supported by tensorrt and convert it to graph. - * - * source func: - * - * func @main(%a : tensor) -> tensor { - * %c = "pd.conv2d"(%a) ... - * %d = "pd.conv3d"(%c) ... - * %f = "pd.conv2d"(%a) ... - * infrt.return %d, %f: ... - * } - * - * destination func: - * func @main(%a : tensor) -> tensor { - * %c = "infrt.graph"(%a) { - * %m = "pd.conv2d"(%a)... - * infrt.return %m:... - * } ... - * %d = "infrt.graph"(%c) { - * %m = "pd.conv3d"(%c)... - * infrt.return %m:... - * } ... - * %f = "infrt.graph"(%a) { - * %m = "pd.conv2d"(%a)... - * infrt.return %m:... - * } ... - * infrt.return %d, %f:... - * } - * TODO(winter-wang): Supplementary how to judge the operators can be supported - * by tensorrt. - */ -class TRTOpTellerPass - : public mlir::PassWrapper { - public: - void getDependentDialects(mlir::DialectRegistry ®istry) const override {} - ::llvm::StringRef getName() const override { return "trtOpTellerPass"; } - void runOnFunction() override; -}; -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_ops.cc b/paddle/infrt/dialect/tensorrt/trt_ops.cc deleted file mode 100644 index 8e39fea4cd8..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_ops.cc +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/tensorrt/trt_ops.h" - -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" -#include "paddle/infrt/dialect/tensorrt/trt_dialect_types.h" - -namespace infrt { -namespace trt { - -EngineType EngineType::get() { - return Base::get(::infrt::Global::getMLIRContext()); -} - -EngineType EngineType::get(mlir::MLIRContext *context) { - return Base::get(context); -} - -TensorRTDialect::TensorRTDialect(mlir::MLIRContext *context) - : mlir::Dialect("trt", context, mlir::TypeID::get()) { - addTypes(); - addOperations< -#define GET_OP_LIST -#include "paddle/infrt/dialect/tensorrt/trt_ops.cpp.inc" // NOLINT - >(); -} - -mlir::Type TensorRTDialect::parseType(mlir::DialectAsmParser &parser) const { - llvm::StringRef keyword; - if (parser.parseKeyword(&keyword)) return mlir::Type(); - // parse trt dilaect types, for example: !trt.engine - if (keyword == "engine") { - return infrt::trt::EngineType::get(getContext()); - } - parser.emitError(parser.getCurrentLocation(), "unknown infrt::trt type: ") - << keyword; - return mlir::Type(); -} - -void TensorRTDialect::printType(mlir::Type type, - mlir::DialectAsmPrinter &printer) const { - // print trt dilaect types, for example: !trt.engien - if (type.isa()) { - printer << "engine"; - return; - } - llvm_unreachable("unknown infrt::trt type."); -} - -} // namespace trt -} // namespace infrt - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/tensorrt/trt_ops.cpp.inc" // NOLINT diff --git a/paddle/infrt/dialect/tensorrt/trt_ops.h b/paddle/infrt/dialect/tensorrt/trt_ops.h deleted file mode 100644 index e851c26c43c..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_ops.h +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" - -namespace infrt { -namespace trt { - -class TensorRTDialect : public mlir::Dialect { - public: - explicit TensorRTDialect(mlir::MLIRContext *context); - static llvm::StringRef getDialectNamespace() { return "trt"; } - mlir::Type parseType(mlir::DialectAsmParser &parser) const; // NOLINT - void printType(mlir::Type type, - mlir::DialectAsmPrinter &printer) const; // NOLINT -}; - -} // namespace trt -} // namespace infrt - -#define GET_OP_CLASSES -#include "paddle/infrt/dialect/tensorrt/trt_ops.hpp.inc" diff --git a/paddle/infrt/dialect/tensorrt/trt_ops.td b/paddle/infrt/dialect/tensorrt/trt_ops.td deleted file mode 100755 index b112cc748ec..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_ops.td +++ /dev/null @@ -1,223 +0,0 @@ -#ifndef TRT_OPS -#define TRT_OPS - -include "mlir/Interfaces/InferTypeOpInterface.td" -include "mlir/Interfaces/LoopLikeInterface.td" -include "mlir/Interfaces/CallInterfaces.td" -include "mlir/IR/OpBase.td" -include "paddle/infrt/dialect/tensorrt/trt_op_base.td" - -include "paddle/infrt/dialect/infrt/ir/infrt_base.td" -include "paddle/infrt/dialect/phi/ir/infrt_phi_base.td" -include "paddle/infrt/dialect/pd/ir/pd_op_base.td" - -def TRT_CreateEngineOp : TRT_Op<"create_engine", [SingleBlockImplicitTerminator<"::infrt::ReturnOp">]> { - let summary = "trt CreateEngine Op"; - let description = [{ - Describe a tensorrt subgraph. - }]; - let regions = (region SizedRegion<1>:$body); - let arguments = (ins Variadic:$inputs, DefaultValuedAttr:$run_once); - let results = (outs TRT_EngineType:$engine); -} - -def TRT_EngineComputeOp : TRT_Op<"compute", [NoSideEffect]> { - let summary = "trt compute engine"; - let description = [{ - execute engine - }]; - let arguments = (ins TRT_EngineType:$engine, Context:$context); - let results = (outs DenseTensorList:$outputs); -} - -def TRT_InspectEngineOp : TRT_Op<"inspect_engine", [NoSideEffect]> { - let summary = "trt inspect engine"; - let description = [{ - Show engine - }]; - let arguments = (ins TRT_EngineType:$engine); -} - -def TRT_ActivationOp : TRT_Op<"Activation", [NoSideEffect]> { - let summary = "TensorRT IActivationLayer"; - let description = [{ - - TensorRT IActivationLayer. - - }]; - let arguments = (ins DenseTensor:$input, SI32Attr:$activation_type, - DefaultValuedAttr:$alpha, - DefaultValuedAttr:$beta); - - let results = (outs DenseTensor:$output); -} - -def TRT_FullyConnectedOp : TRT_Op<"FullyConnected", [NoSideEffect]> { - let summary = "TensorRT IFullyConnectedLayer"; - let description = [{ - TensorRT IFullyConnectedLayer - }]; - let arguments = (ins - DenseTensor:$input_tensor, - DenseTensor:$kernel_weights, - DenseTensor:$bias_weights, - SI32Attr:$out_channel_num - ); - let results = (outs - DenseTensor:$output_tensor - ); -} - -def TRT_ConvolutionOp : TRT_Op<"Convolution", [NoSideEffect]> { - let summary = "TensorRT IConvolutionLayer"; - let description = [{ - TensorRT IConvolutionLayer - }]; - let arguments = (ins - DenseTensor:$input_tensor, - DenseTensor:$kernel_weights, - Optional:$bias_weights, - SI32Attr:$out_channel_num, - I32ArrayAttr:$kernel_size, - I32ArrayAttr:$strides, - I32ArrayAttr:$paddings, - I32ArrayAttr:$pre_paddings, - I32ArrayAttr:$post_paddings, - DefaultValuedAttr:$padding_mode, //kEXPLICIT_ROUND_DOWN - SI32Attr:$groups, - I32ArrayAttr:$dilations - ); - let results = (outs - DenseTensor:$output_tensor - ); -} - -def TRT_PoolingOp : TRT_Op<"Pooling", [NoSideEffect]> { - let summary = "TensorRT IPoolingLayer "; - let description = [{ - TensorRT IPoolingLayer - }]; - let arguments = (ins - DenseTensor:$input_tensor, - SI32Attr:$pool_type, - I32ArrayAttr:$window_size, - I32ArrayAttr:$strides, - I32ArrayAttr:$paddings, - SI32Attr:$padding_mode, - BoolAttr:$exclusive, - BoolAttr:$adaptive, - StrAttr:$padding_algorithm - ); - let results = (outs - DenseTensor:$output_tensor - ); -} - -def TRT_ElementWiseOp : TRT_Op<"ElementWise", [NoSideEffect]> { - let summary = "TensorRT IElementWiseLayer"; - let description = [{ - - TensorRT IElementWiseLayer. - - }]; - let arguments = (ins DenseTensor:$input1, DenseTensor:$input2, SI32Attr:$elementwise_operation); - - let results = (outs DenseTensor:$output); -} - -def TRT_MatrixMultiplyOp : TRT_Op<"MatrixMultiply", [NoSideEffect]> { - let summary = "TensorRT IMatrixMultiplyLayer"; - let description = [{ - - TensorRT IMatrixMultiplyLayer. - - }]; - let arguments = (ins DenseTensor:$input1, BoolAttr:$transpose1, - DenseTensor:$input2, BoolAttr:$transpose2); - - let results = (outs DenseTensor:$output); -} - -def TRT_ScaleOp : TRT_Op<"scale", [NoSideEffect]> { - let summary = "TensorRT IScaleLayer"; - let description = [{ - - TensorRT IScaleLayer - - }]; - let arguments = (ins - DenseTensor:$input_tensor, - DefaultValuedAttr:$mode, - DenseTensor:$shift, - DenseTensor:$scale, - DenseTensor:$power - ); - - let results = (outs DenseTensor:$Out); -} - -def TRT_MatrixMultiplOp : TRT_Op<"MatrixMultiplOp", [NoSideEffect]> { - let summary = "TensorRT IMatrixMultiplyLayer"; - let description = [{ - - TensorRT IMatrixMultiplyLayer - - }]; - let arguments = (ins - DenseTensor:$input1, - DefaultValuedAttr:$matrix_operation1, - DenseTensor:$input2, - DefaultValuedAttr:$matrix_operation2 - ); - - let results = (outs DenseTensor:$Out); -} - -def TRT_SoftMaxOp : TRT_Op<"SoftMaxOp", [NoSideEffect]> { - let summary = "TensorRT ISoftMaxLayer"; - let description = [{ - - TensorRT ISoftMaxLayer - - }]; - let arguments = (ins - DenseTensor:$input_tensor, - SI32Attr:$axis - ); - - let results = (outs DenseTensor:$Out); -} - -def TRT_ScaleNdOp : TRT_Op<"ScaleNd", [NoSideEffect]> { - let summary = "TensorRT IScaleLayer"; - let description = [{ - - TensorRT IScaleLayer - - }]; - let arguments = (ins - DenseTensor:$input_tensor, - DenseTensor:$shift, - DenseTensor:$scale, - Optional:$power - ); - - let results = (outs DenseTensor:$Out); -} - -def TRT_ShuffleOp : TRT_Op<"Shuffle", [NoSideEffect]> { - let summary = "TensorRT IShuffleLayer"; - let description = [{ - - TensorRT IShuffleLayer - - }]; - let arguments = (ins - DenseTensor:$input_tensor, - DefaultValuedAttr:$start_axis, - DefaultValuedAttr:$stop_axis - ); - - let results = (outs DenseTensor:$Out); -} -#endif // TRT_OPS diff --git a/paddle/infrt/dialect/tensorrt/trt_type_convert_pass.cc b/paddle/infrt/dialect/tensorrt/trt_type_convert_pass.cc deleted file mode 100644 index 1cb7c4155b9..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_type_convert_pass.cc +++ /dev/null @@ -1,184 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/dialect/tensorrt/trt_type_convert_pass.h" - -#include - -#include - -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/Casting.h" -#include "mlir/IR/Block.h" -#include "mlir/IR/BuiltinAttributes.h" -#include "mlir/IR/Dialect.h" -#include "mlir/IR/Operation.h" -#include "mlir/IR/OperationSupport.h" -#include "mlir/IR/Value.h" -#include "mlir/Pass/Pass.h" -#include "paddle/infrt/dialect/infrt/common/types.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" -#include "paddle/infrt/dialect/tensorrt/trt_ops.h" - -namespace { - -class TrtTypeConvertPass - : public mlir::PassWrapper { - public: - ::llvm::StringRef getName() const override { return "TrtTypeConvertPass"; } - - void runOnFunction() override; -}; - -void TrtTypeConvertPass::runOnFunction() { - mlir::Block& body = getFunction().front(); - auto* mlir_ctx = getFunction()->getContext(); - mlir::OpBuilder builder(&body, body.begin()); - - std::vector worklist; - mlir::Operation* ctx_op{nullptr}; - worklist.reserve(body.getOperations().size()); - for (auto& op : body) { - worklist.push_back(&op); - if (op.getName().getStringRef() == "phi_dt.create_context.gpu") { - ctx_op = &op; - } - } - - ::infrt::LayoutType layout = ::infrt::LayoutType::NCHW; - ::infrt::TargetType target = ::infrt::TargetType::GPU; - const std::set inited_op_repr{ - "phi_dt.tensor_map_get_tensor", - "phi_dt.create_inited_dense_tensor.cpu.f32", - "phi_dt.create_host_inited_dense_tensor.f32"}; - for (auto& op : worklist) { - if (auto tensor_map_get_op = - llvm::dyn_cast<::infrt::phi::TensorMapGetTensorOp>(op)) { - auto res = tensor_map_get_op.output(); - if (auto t = res.getType().dyn_cast<::infrt::DenseTensorType>()) { - auto replace_type = ::infrt::DenseTensorType::get( - mlir_ctx, t.getTarget(), t.getPrecision(), layout); - res.setType(replace_type); - } - } else if (auto create_inited_tensor_op = - llvm::dyn_cast<::infrt::phi::CreateHostInitedDenseTensorOp>( - op)) { - auto res = create_inited_tensor_op.output(); - if (auto t = res.getType().dyn_cast<::infrt::DenseTensorType>()) { - auto replace_type = ::infrt::DenseTensorType::get( - mlir_ctx, t.getTarget(), t.getPrecision(), layout); - res.setType(replace_type); - } - } else if (auto create_engine = - llvm::dyn_cast<::infrt::trt::CreateEngineOp>(op)) { - // Insert `infrt.gpu.memcpy` op. - for (auto arg : create_engine.getOperands()) { - if (mlir::Operation* producer = arg.getDefiningOp()) { - if (arg.getType().isa<::infrt::DenseTensorType>()) { - builder.setInsertionPointAfter(producer); - auto t = arg.getType().dyn_cast<::infrt::DenseTensorType>(); - if (!inited_op_repr.count( - producer->getName().getStringRef().str()) && - t.getTarget() != ::infrt::TargetType::GPU) { - auto replace_type = ::infrt::DenseTensorType::get( - mlir_ctx, target, t.getPrecision(), layout); - CHECK_NOTNULL(ctx_op); - auto mem_cpy_op = builder.create<::infrt::phi::GpuMemCopyOp>( - arg.getLoc(), - replace_type, - arg, - llvm::dyn_cast<::infrt::phi::CreateGPUContextOp>(ctx_op) - .output(), - builder.getBoolAttr(false)); - arg.replaceAllUsesExcept(mem_cpy_op.output(), mem_cpy_op); - } - } - } else { - auto blockArg = arg.cast(); - if (arg.getType().isa<::infrt::DenseTensorType>()) { - auto t = arg.getType().dyn_cast<::infrt::DenseTensorType>(); - builder.setInsertionPointAfter(ctx_op); - auto replace_type = ::infrt::DenseTensorType::get( - mlir_ctx, ::infrt::TargetType::GPU, t.getPrecision(), layout); - CHECK_NOTNULL(ctx_op); - auto mem_cpy_op = builder.create<::infrt::phi::GpuMemCopyOp>( - blockArg.getLoc(), - replace_type, - blockArg, - llvm::dyn_cast<::infrt::phi::CreateGPUContextOp>(ctx_op) - .output(), - builder.getBoolAttr(false)); - arg.replaceAllUsesExcept(mem_cpy_op.output(), mem_cpy_op); - } - } - } - - // Change ops(in block) types. - auto& block = create_engine.getRegion().getBlocks().front(); - for (auto& op : block.without_terminator()) { - for (size_t i = 0; i < op.getNumResults(); ++i) { - if (auto t = op.getResult(i) - .getType() - .dyn_cast<::infrt::DenseTensorType>()) { - auto replace_type = ::infrt::DenseTensorType::get( - mlir_ctx, ::infrt::TargetType::GPU, t.getPrecision(), layout); - op.getResult(i).setType(replace_type); - } - } - } - } else if (auto list_get_tensor_op = - llvm::dyn_cast<::infrt::dt::TensorListGetTensorOp>(op)) { - auto result = list_get_tensor_op.output(); - if (auto t = result.getType().dyn_cast<::infrt::DenseTensorType>()) { - result.setType(::infrt::DenseTensorType::get( - mlir_ctx, ::infrt::TargetType::GPU, t.getPrecision(), layout)); - } - } else if (auto return_op = llvm::dyn_cast<::infrt::ReturnOp>(op)) { - for (auto arg : return_op->getOperands()) { - if (auto t = arg.getType().dyn_cast<::infrt::DenseTensorType>()) { - if (t.getLayout() != ::infrt::LayoutType::ANY || - t.getTarget() != ::infrt::TargetType::CPU || - t.getPrecision() != ::infrt::PrecisionType::FLOAT32) { - builder.setInsertionPoint(return_op); - CHECK_NOTNULL(ctx_op); - auto mem_cpy_op = builder.create<::infrt::phi::GpuMemCopyOp>( - return_op.getLoc(), - ::infrt::DenseTensorType::get(mlir_ctx, - ::infrt::TargetType::CPU, - t.getPrecision(), - ::infrt::LayoutType::ANY), - arg, - llvm::dyn_cast<::infrt::phi::CreateGPUContextOp>(ctx_op) - .output(), - builder.getBoolAttr(true)); - arg.replaceAllUsesExcept(mem_cpy_op.output(), mem_cpy_op); - } - } - } - } - } -} - -} // namespace - -namespace infrt { -namespace trt { - -std::unique_ptr CreateTrtTypeConvertPass() { - return std::make_unique(); -} - -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/dialect/tensorrt/trt_type_convert_pass.h b/paddle/infrt/dialect/tensorrt/trt_type_convert_pass.h deleted file mode 100644 index 68a15696b3e..00000000000 --- a/paddle/infrt/dialect/tensorrt/trt_type_convert_pass.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -namespace infrt { -namespace trt { - -std::unique_ptr CreateTrtTypeConvertPass(); - -} // namespace trt -} // namespace infrt diff --git a/paddle/infrt/external_kernels/CMakeLists.txt b/paddle/infrt/external_kernels/CMakeLists.txt deleted file mode 100644 index 9b318872e75..00000000000 --- a/paddle/infrt/external_kernels/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -set(external_kernels_src "basic_kernels.cc") - -cc_library(external_kernels SHARED SRCS ${external_kernels_src}) -set_target_properties(external_kernels PROPERTIES LINK_FLAGS "${LINK_FLAGS}") - -set(basic_mlir "${CMAKE_CURRENT_SOURCE_DIR}/basic.mlir") -set(external_kernels_lib "${CMAKE_CURRENT_BINARY_DIR}/libexternal_kernels.so") -message(STATUS "basic_mlir: ${basic_mlir}") -message(STATUS "external_kernels_lib: ${external_kernels_lib}") -add_test( - NAME run_and_check_external_kernels - COMMAND - sh -c "${CMAKE_BINARY_DIR}/infrt/host_context/infrtexec -i ${basic_mlir} \ - --shared_libs=${external_kernels_lib} | \ - ${LLVM_PATH}/bin/FileCheck ${basic_mlir}") diff --git a/paddle/infrt/external_kernels/basic.mlir b/paddle/infrt/external_kernels/basic.mlir deleted file mode 100644 index 843b12ced21..00000000000 --- a/paddle/infrt/external_kernels/basic.mlir +++ /dev/null @@ -1,21 +0,0 @@ -// CHECK: basic -func @basic() -> f32 { - %v0 = infrt.constant.f32 1.0 - %v1 = infrt.constant.f32 2.0 - %v2 = "external.add.f32"(%v0, %v1) : (f32, f32) -> f32 - - // CHECK: 1 - "external.print.f32"(%v0) : (f32) -> () - // CHECK: 2 - "external.print.f32"(%v1) : (f32) -> () - - // CHECK: 3 - "external.print.f32"(%v2) : (f32) -> () - - %v3 = "external.mul.f32"(%v2, %v1) : (f32, f32) -> f32 - - // CHECK: 6 - "external.print.f32"(%v3) : (f32) -> () - - infrt.return %v3 : f32 -} diff --git a/paddle/infrt/external_kernels/basic_kernels.cc b/paddle/infrt/external_kernels/basic_kernels.cc deleted file mode 100644 index b59a8881fb0..00000000000 --- a/paddle/infrt/external_kernels/basic_kernels.cc +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" - -template -T add(T a, T b) { - return a + b; -} - -template -T sub(T a, T b) { - return a - b; -} - -template -T mul(T a, T b) { - return a * b; -} - -template -T div(T a, T b) { - return a / b; -} - -template -void print(T a) { - std::cout << a << std::endl; -} - -void RegisterKernels(infrt::host_context::KernelRegistry *registry) { - // int32 - registry->AddKernel("external.add.i32", INFRT_KERNEL(add)); - registry->AddKernel("external.sub.i32", INFRT_KERNEL(sub)); - registry->AddKernel("external.mul.i32", INFRT_KERNEL(mul)); - registry->AddKernel("external.div.i32", INFRT_KERNEL(div)); - registry->AddKernel("external.print.i32", INFRT_KERNEL(print)); - - // float - registry->AddKernel("external.add.f32", INFRT_KERNEL(add)); - registry->AddKernel("external.sub.f32", INFRT_KERNEL(sub)); - registry->AddKernel("external.mul.f32", INFRT_KERNEL(mul)); - registry->AddKernel("external.div.f32", INFRT_KERNEL(div)); - registry->AddKernel("external.print.f32", INFRT_KERNEL(print)); -} diff --git a/paddle/infrt/external_kernels/fc.mlir b/paddle/infrt/external_kernels/fc.mlir deleted file mode 100644 index 26b2d24cace..00000000000 --- a/paddle/infrt/external_kernels/fc.mlir +++ /dev/null @@ -1,43 +0,0 @@ -// CHECK-LABEL: @fc -func @fc(%input : !infrt.dense_tensor, - %w : !infrt.dense_tensor, - %bias : !infrt.dense_tensor) -> !infrt.dense_tensor -{ - %out = dt.create_uninit_tensor.f32 [30, 50] -> !infrt.dense_tensor - // dt.fill_tensor_with_constant.f32 (%out : !infrt.dense_tensor) {value=0.0:f32} - - // fc1 - "external.matmul"(%input, %w, %out) {}: (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> () - "external.elementwise_add"(%out, %bias, %out) {axis = -1}: (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> () - "external.sigmoid"(%out, %out) {}: (!infrt.dense_tensor, !infrt.dense_tensor) -> () - - // fc2 - "external.matmul"(%out, %w, %out) {}: (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> () - "external.elementwise_add"(%out, %bias, %out) {axis = -1}: (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> () - "external.sigmoid"(%out, %out) {}: (!infrt.dense_tensor, !infrt.dense_tensor) -> () - - infrt.return %out : !infrt.dense_tensor -} - -// CHECK-LABEL: @benchmark -func @benchmark() { - %input = dt.create_uninit_tensor.f32 [30, 50] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%input : !infrt.dense_tensor) {value=1.0:f32} - - %w = dt.create_uninit_tensor.f32 [50, 50] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%w : !infrt.dense_tensor) {value=2.0:f32} - - %bias = dt.create_uninit_tensor.f32 [30, 50] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%bias : !infrt.dense_tensor) {value=3.0:f32} - - infrt.benchmark "add.f32"( - %input:!infrt.dense_tensor, - %w:!infrt.dense_tensor, - %bias:!infrt.dense_tensor) - duration_secs = 100, max_count = 300000, num_warmup_runs = 3 - { - %res = infrt.call @fc(%input, %w, %bias) : (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> (!infrt.dense_tensor) - infrt.return %res : !infrt.dense_tensor - } - infrt.return -} diff --git a/paddle/infrt/external_kernels/paddle.mlir b/paddle/infrt/external_kernels/paddle.mlir deleted file mode 100644 index 97781e5c8c5..00000000000 --- a/paddle/infrt/external_kernels/paddle.mlir +++ /dev/null @@ -1,50 +0,0 @@ -// CHECK: paddle_func -func @paddle_func() -> () { - %input = dt.create_uninit_tensor.f32 [3, 5] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%input : !infrt.dense_tensor) {value=1.0:f32} - - %w = dt.create_uninit_tensor.f32 [5, 4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%w : !infrt.dense_tensor) {value=2.0:f32} - - %bias = dt.create_uninit_tensor.f32 [4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%bias : !infrt.dense_tensor) {value=3.0:f32} - - %out = dt.create_uninit_tensor.f32 [3, 4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%out : !infrt.dense_tensor) {value=0.0:f32} - - "external.fc2"(%input, %w, %bias, %out) {in_num_col_dims=3:i32, test_attr=5:i32}: (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> () - // CHECK-LABEL: tensor: shape=shape[3,5], values=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] - dt.print_tensor (%input : !infrt.dense_tensor) - // CHECK-LABEL: tensor: shape=shape[5,4], values=[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] - dt.print_tensor (%w : !infrt.dense_tensor) - dt.print_tensor (%bias : !infrt.dense_tensor) - dt.print_tensor (%out : !infrt.dense_tensor) - - // test external.matmul - %out1 = dt.create_uninit_tensor.f32 [3, 4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%out1 : !infrt.dense_tensor) {value=0.0:f32} - "external.matmul"(%input, %w, %out1) {}: (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> () - dt.print_tensor (%out1 : !infrt.dense_tensor) - - // test external.elementwise_add - %out2 = dt.create_uninit_tensor.f32 [3, 4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%out2 : !infrt.dense_tensor) {value=0.0:f32} - %bias1 = dt.create_uninit_tensor.f32 [3, 4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%bias1 : !infrt.dense_tensor) {value=3.0:f32} - "external.elementwise_add"(%out1, %bias1, %out2) {axis=-1}: (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> () - dt.print_tensor (%out2 : !infrt.dense_tensor) - - // test external.relu - %out3 = dt.create_uninit_tensor.f32 [3, 4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%out3 : !infrt.dense_tensor) {value=0.0:f32} - "external.relu"(%out1, %out3) {}: (!infrt.dense_tensor, !infrt.dense_tensor) -> () - dt.print_tensor (%out3 : !infrt.dense_tensor) - - // test external.sigmoid - %out4 = dt.create_uninit_tensor.f32 [3, 4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%out4 : !infrt.dense_tensor) {value=0.0:f32} - "external.sigmoid"(%out1, %out4) {}: (!infrt.dense_tensor, !infrt.dense_tensor) -> () - dt.print_tensor (%out4 : !infrt.dense_tensor) - - infrt.return -} diff --git a/paddle/infrt/gtest_main.cc b/paddle/infrt/gtest_main.cc deleted file mode 100644 index 26e2b5dcfc6..00000000000 --- a/paddle/infrt/gtest_main.cc +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -int main(int argc, char **argv) { - testing::InitGoogleTest(&argc, argv); - gflags::ParseCommandLineFlags(&argc, &argv, false); - - return RUN_ALL_TESTS(); -} diff --git a/paddle/infrt/host_context/CMakeLists.txt b/paddle/infrt/host_context/CMakeLists.txt deleted file mode 100644 index 2901a282cda..00000000000 --- a/paddle/infrt/host_context/CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ -core_gather_headers() - -gather_srcs( - infrt_src - SRCS - kernel_frame.cc - kernel_registry.cc - value.cc - kernel_utils.cc - symbol_table.cc - op_executable.cc - core_runtime.cc - mlir_to_runtime_translate.cc - function.cc - mlir_function_executable.cc - mlir_program_executor.cc - paddle_mlir.cc) - -cc_test_tiny(test_infrt_host_context_value SRCS value_test.cc DEPS infrt - ${MLIR_IR_LIBS}) -cc_test_tiny(test_infrt_kernel_utils SRCS kernel_utils_test.cc DEPS infrt - ${MLIR_IR_LIBS}) -cc_test_tiny(test_infrt_kernel_registry SRCS kernel_registry_test.cc DEPS infrt - ${MLIR_IR_LIBS}) -cc_test_tiny(test_infrt_op_executable SRCS op_executable_test.cc DEPS infrt - ${MLIR_IR_LIBS}) -cc_test_tiny(test_infrt_core_runtime SRCS core_runtime_test.cc DEPS infrt - ${MLIR_IR_LIBS}) -cc_test_tiny(test_infrt_mlir_to_runtime_translate SRCS - mlir_to_runtime_translate_test.cc DEPS infrt ${MLIR_IR_LIBS}) - -add_executable(paddle-mlir-convert paddle_mlir_converter.cc) -target_link_libraries(paddle-mlir-convert infrt ${MLIR_IR_LIBS}) -add_executable(infrtexec mlir_exec.cc) -target_link_libraries(infrtexec infrt ${MLIR_IR_LIBS}) diff --git a/paddle/infrt/host_context/core_runtime.cc b/paddle/infrt/host_context/core_runtime.cc deleted file mode 100644 index 3dbb0b41c9f..00000000000 --- a/paddle/infrt/host_context/core_runtime.cc +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/core_runtime.h" - -#include -#include -#include - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/op_executable.h" -#include "paddle/infrt/host_context/symbol_table.h" - -namespace infrt { -namespace host_context { - -struct CoreRuntime::Impl { - KernelRegistry* kernel_registry{}; - SymbolTable symbol_table; - std::vector op_executables; - - mutable std::vector results; -}; - -SymbolTable* CoreRuntime::symbol_table() { return &impl_->symbol_table; } - -CoreRuntime::CoreRuntime(CoreRuntime::Impl* impl) : impl_(impl) { CHECK(impl); } - -void CoreRuntime::Execute() { - // std::cout << "CoreRuntime::Execute" << std::endl; - int op_offset = 0; - for (auto& op : impl_->op_executables) { - VLOG(3) << "running op " << op_offset++ << " " << op.name(); - op.Execute(); - } -} - -KernelRegistry* CoreRuntime::kernel_registry() const { - return impl_->kernel_registry; -} - -size_t CoreRuntime::num_ops() const { return impl_->op_executables.size(); } - -CoreRuntimeBuilder::CoreRuntimeBuilder(KernelRegistry* kernel_registry) - : CoreRuntime(new Impl) { - impl_->kernel_registry = - kernel_registry ? kernel_registry : GetCpuKernelRegistry(); -} - -OpExecutableBuilder* CoreRuntimeBuilder::NewOpExecutable( - const std::string& op_name) { - CHECK(impl_.get()); - impl_->op_executables.emplace_back( - op_name, symbol_table(), impl_->kernel_registry); - return &impl_->op_executables.back(); -} - -void CoreRuntimeBuilder::FeedInArgs( - llvm::ArrayRef> args) { - for (auto& item : args) { - symbol_table()->Register(item.first, item.second); - } -} - -void CoreRuntimeBuilder::SetKernelRegistry(KernelRegistry* x) { - CHECK(x); - impl_->kernel_registry = x; -} - -llvm::SmallVector CoreRuntime::GetResults( - llvm::ArrayRef arg_names) { - llvm::SmallVector results; - for (auto& name : arg_names) { - results.push_back(ValueRef(symbol_table()->GetValue(name))); - } - - return results; -} - -CoreRuntime::~CoreRuntime() {} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/core_runtime.h b/paddle/infrt/host_context/core_runtime.h deleted file mode 100644 index 585369e249b..00000000000 --- a/paddle/infrt/host_context/core_runtime.h +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include - -#include -#include -#include - -#include "paddle/infrt/host_context/value.h" - -namespace infrt { -namespace host_context { - -class KernelRegistry; -class OpExecutable; -class OpExecutableBuilder; -class SymbolTable; - -/** - * CoreRuntime encapsulate the execution for a sequence of ops. - * Each function call will bind to a CoreRuntime instance, push the argument - * Values in to the argument-list, and get the - * result Values from the return-list. - */ -class CoreRuntime : public std::enable_shared_from_this { - public: - //! Execute a program. - void Execute(); - - //! Return the number of ops. - size_t num_ops() const; - - //! Get the results of the execution. - llvm::SmallVector // - GetResults(llvm::ArrayRef arg_names); - - std::shared_ptr getptr() { - return std::shared_ptr(this); - } - - KernelRegistry* kernel_registry() const; - - ~CoreRuntime(); - - protected: - //! Get the symbol table. - SymbolTable* symbol_table(); - - class Impl; - explicit CoreRuntime(Impl* impl); - std::unique_ptr impl_; -}; - -/** - * The builder for CoreRuntime, help to construct a function. - */ -class CoreRuntimeBuilder : public CoreRuntime { - public: - explicit CoreRuntimeBuilder(KernelRegistry* kernel_registry); - - using CoreRuntime::symbol_table; - - void SetKernelRegistry(KernelRegistry* x); - - //! Feed the input arguments, each item is a pair of arg-name and arg-value. - void FeedInArgs(llvm::ArrayRef> args); - - llvm::ArrayRef attr_names() const; - - OpExecutableBuilder* NewOpExecutable(const std::string& op_name); -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/core_runtime_test.cc b/paddle/infrt/host_context/core_runtime_test.cc deleted file mode 100644 index 3c0dadaad42..00000000000 --- a/paddle/infrt/host_context/core_runtime_test.cc +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/core_runtime.h" - -#include - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/host_context/op_executable.h" -#include "paddle/infrt/host_context/symbol_table.h" - -namespace infrt { -namespace host_context { - -int add(int a, int b) { return a + b; } -int sub(int a, int b) { return a - b; } - -TEST(CoreRuntime, basic) { - KernelRegistry registry; - registry.AddKernel("infrt.test.addi32", INFRT_KERNEL(add)); - registry.AddKernel("infrt.test.subi32", INFRT_KERNEL(sub)); - - CoreRuntimeBuilder builder(®istry); - auto* table = builder.symbol_table(); - table->Register("a", 1); - table->Register("b", 2); - table->Register("d", 4); - - // c = a + b - auto* op0 = builder.NewOpExecutable("infrt.test.addi32"); - op0->AppendArgument("a"); - op0->AppendArgument("b"); - op0->SetResults({"c"}); - - // e = c - d - auto* op1 = builder.NewOpExecutable("infrt.test.subi32"); - op1->AppendArgument("c"); - op1->AppendArgument("d"); - op1->SetResults({"e"}); - - builder.Execute(); - - ASSERT_EQ(table->GetValue("d")->get(), 4); - ASSERT_EQ(table->GetValue("c")->get(), 3); - ASSERT_EQ(table->GetValue("e")->get(), -1); -} - -TEST(CoreRuntime, function) { - // The function: - // func(int a, int b) { - // int c = a + b - // return c - // } - KernelRegistry registry; - registry.AddKernel("infrt.test.addi32", INFRT_KERNEL(add)); - registry.AddKernel("infrt.test.subi32", INFRT_KERNEL(sub)); - - CoreRuntimeBuilder builder(®istry); - auto* table = builder.symbol_table(); - - std::vector> feeds{ - {std::make_pair("a", ValueRef(new Value(1))), // - std::make_pair("b", ValueRef(new Value(2)))}}; - builder.FeedInArgs(llvm::ArrayRef>( - feeds.data(), feeds.size())); - - ASSERT_EQ(table->Get("a"), 1); - ASSERT_EQ(table->Get("b"), 2); - ASSERT_EQ(table->size(), 2UL); - - auto* op = builder.NewOpExecutable("infrt.test.addi32"); - op->AppendArgument("a"); - op->AppendArgument("b"); - op->SetResults({"c"}); - - builder.Execute(); - - auto res = builder.GetResults({"c"}); - ASSERT_EQ(res.size(), 1UL); - ASSERT_EQ(res[0].get(), 3); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/function.cc b/paddle/infrt/host_context/function.cc deleted file mode 100644 index 8b111f2645a..00000000000 --- a/paddle/infrt/host_context/function.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/function.h" - -namespace infrt { -namespace host_context {} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/function.h b/paddle/infrt/host_context/function.h deleted file mode 100644 index 030e3b6cfbc..00000000000 --- a/paddle/infrt/host_context/function.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -#include - -namespace infrt { -namespace host_context { - -struct Value; -struct ValueRef; - -/** - * Base class of all executable Function. - * - * This is used by `infrt.call` op, to execute a function. - */ -class Function { - public: - Function(Function&& other) - : name_(other.name_), - num_arguments_(other.num_arguments_), - num_results_(other.num_results_) {} - - Function() = delete; - - std::string name() const { return name_; } - - size_t num_arguments() const { return num_arguments_; } - size_t num_results() const { return num_results_; } - - virtual void Execute(llvm::ArrayRef arguments, - llvm::MutableArrayRef results, - bool is_region = false) const {} - - virtual ~Function() = default; - - protected: - Function(std::string name, size_t num_arguments, size_t num_results) - : name_(name), num_arguments_(num_arguments), num_results_(num_results) {} - - private: - std::string name_; - size_t num_arguments_{}; - size_t num_results_{}; -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/kernel_frame.cc b/paddle/infrt/host_context/kernel_frame.cc deleted file mode 100644 index a03ed0d156e..00000000000 --- a/paddle/infrt/host_context/kernel_frame.cc +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/kernel_frame.h" - -#include -#include - -namespace infrt { -namespace host_context { - -std::ostream& operator<<(std::ostream& os, const KernelFrame& frame) { - os << "KernelFrame: " << frame.GetNumArgs() << " args, " - << frame.GetNumResults() << " res, " << frame.GetNumResults() << " attrs"; - return os; -} - -#ifndef NDEBUG -std::string KernelFrame::DumpArgTypes() const { - std::stringstream ss; - for (auto* value : GetValues(0, GetNumElements())) { -#define DUMP(type_name) \ - if (value->is_type()) { \ - ss << #type_name << &value->get() << "), "; \ - } - DUMP(bool); - DUMP(tensor::DenseHostTensor); - DUMP(float); - DUMP(int); - DUMP(::Tensor); - DUMP(::phi::MetaTensor); - DUMP(::phi::CPUContext); - DUMP(host_context::None); - DUMP(backends::CpuPhiContext); -#undef DUMP - ss << "typeid: " << value->index() << ", "; - } - return ss.str(); -} -#endif - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/kernel_frame.h b/paddle/infrt/host_context/kernel_frame.h deleted file mode 100644 index 90887edb991..00000000000 --- a/paddle/infrt/host_context/kernel_frame.h +++ /dev/null @@ -1,198 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include - -#include - -#include "llvm/ADT/SmallVector.h" -#include "paddle/infrt/host_context/value.h" - -namespace infrt { -namespace host_context { - -/** - * KernelFrame captures the states(input arguments, attributes, results) - * associated with a kernel invocation. - */ -class KernelFrame { - public: - int GetNumArgs() const { return num_arguments_; } - int GetNumResults() const { - return value_or_attrs_.size() - num_arguments_ - GetNumAttributes(); - } - int GetNumAttributes() const { return num_attrs_ == -1 ? 0 : num_attrs_; } - - //! Get something at a specific position \p index. The element might be an - //! argument, an attribute or a result. - template - T& GetElementAt(int index) { - CHECK_LT(static_cast(index), GetNumElements()); - return value_or_attrs_[index]->template get_or_default(); - } - - Value* GetElementAt(int index) { - CHECK_LT(static_cast(index), GetNumElements()); - return value_or_attrs_[index]; - } - - // Get number of elements, either input, attributes or results. - size_t GetNumElements() const { return value_or_attrs_.size(); } - - template - T& GetArgAt(int index) { - CHECK_LT(index, GetNumArgs()); - return value_or_attrs_[index]->get(); - } - template - const T& GetArgAt(int index) const { - CHECK_LT(index, GetNumArgs()); - return value_or_attrs_[index]->get(); - } - - Value* GetArgAt(int index) { - CHECK_LT(index, GetNumArgs()); - return value_or_attrs_[index]; - } - - // Get all arguments. - llvm::ArrayRef GetArguments() const { - return GetValues(0, num_arguments_); - } - - Value* GetAttributeAt(int idx) { - // CHECK_NE(num_results_, -1) - //<< "Must call SetNumResults before GetAttributeAt"; - CHECK_LT(idx, GetNumAttributes()); - return value_or_attrs_[num_arguments_ + idx]; - } - - void AddAttribute(Value* v) { - CHECK_LE(num_results_, 0) - << "Must call SetNumResults after calling AddAttribute"; - value_or_attrs_.emplace_back(v); - if (num_attrs_ == -1) num_attrs_ = 0; - num_attrs_++; - - CHECK_EQ(value_or_attrs_.size(), - static_cast(num_arguments_ + num_attrs_)); - } - - template - void EmplaceResult(Args&&... args) { - EmplaceResult(0, std::forward(args)...); - } - - template - void EmplaceResult(int index, Args&&... args) { - SetResultAt(index, T(std::forward(args)...)); - } - - template - void SetResultAt(int index, T&& value) { - CHECK_LT(index, GetNumResults()) << "Invalid result index"; - CHECK(value_or_attrs_[num_arguments_ + GetNumAttributes() + index]); - value_or_attrs_[num_arguments_ + GetNumAttributes() + index]->set( - std::move(value)); - } - - llvm::ArrayRef GetResults() const { - CHECK_GE(num_results_, 0) << "Invalid results num"; - return GetValues(num_arguments_ + GetNumAttributes(), num_results_); - } - llvm::MutableArrayRef GetResults() { - CHECK_GE(num_results_, 0) << "Invalid results num"; - return GetMutableValues(num_arguments_ + GetNumAttributes(), num_results_); - } - - llvm::ArrayRef GetValues(size_t from, size_t length) const { - CHECK_LE(from + length, GetNumElements()); - if (length == 0) return {}; - - return llvm::makeArrayRef(&value_or_attrs_[from], length); - } - - llvm::MutableArrayRef GetMutableValues(size_t from, size_t length) { - CHECK_LE(from + length, GetNumElements()); - if (length == 0) return {}; - return llvm::makeMutableArrayRef(&value_or_attrs_[from], length); - } - -#ifndef NDEBUG - std::string DumpArgTypes() const; -#endif - - bool IsEmpty() const { return value_or_attrs_.empty(); } - - protected: - int num_arguments_{}; - int num_attrs_{-1}; - int num_results_{-1}; - - llvm::SmallVector value_or_attrs_; -}; - -std::ostream& operator<<(std::ostream& os, const KernelFrame& frame); - -class KernelFrameBuilder : public KernelFrame { - public: - void AddArgument(Value* value) { - CHECK(value); - CHECK_EQ(num_attrs_, -1) - << "Should call AddArgument before calling SetAttributes"; - value_or_attrs_.push_back(value); - ++num_arguments_; - } - - void SetResults(llvm::ArrayRef values) { - CHECK_EQ(num_arguments_ + GetNumAttributes(), - static_cast(value_or_attrs_.size())); - for (Value* x : values) { - value_or_attrs_.push_back(x); - } - num_results_ = values.size(); - } - - void SetNumResults(size_t n) { - CHECK_EQ(num_arguments_ + GetNumAttributes(), - static_cast(value_or_attrs_.size())); - for (size_t i = 0; i < n; i++) { - value_or_attrs_.emplace_back(new Value); - } - num_results_ = n; - } - - void SetResultAt(int result_id, Value* value) { - CHECK_EQ(static_cast(value_or_attrs_.size()), - num_arguments_ + GetNumAttributes() + num_results_) - << "Call SetNumResults first"; - CHECK_LT(result_id + num_arguments_ + GetNumAttributes(), - static_cast(value_or_attrs_.size())); - CHECK(value); - value_or_attrs_[num_arguments_ + GetNumAttributes() + result_id]->set( - value); - } - - void Reset() { - value_or_attrs_.clear(); - num_arguments_ = 0; - num_results_ = -1; - num_attrs_ = -1; - } -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/kernel_registry.cc b/paddle/infrt/host_context/kernel_registry.cc deleted file mode 100644 index 2518056ba9d..00000000000 --- a/paddle/infrt/host_context/kernel_registry.cc +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/kernel_registry.h" - -#include - -#include "glog/logging.h" -#include "llvm/ADT/SmallVector.h" - -namespace infrt { -namespace host_context { - -struct KernelRegistry::Impl { - std::unordered_map>> - data; -}; - -KernelRegistry::KernelRegistry() : impl_(std::make_unique()) {} - -const std::vector &KernelRegistry::GetAttrNameList( - const std::string &key) const { - CHECK(impl_->data.count(key)); - return impl_->data[key].second; -} - -void KernelRegistry::AddKernel(const std::string &key, - KernelImplementation fn, - const std::vector &attr_order) { - CHECK(!impl_->data.count(key)) - << "kernel [" << key << "] is registered twice"; - impl_->data.emplace( - key, std::make_pair([fn]() { return fn; }, std::move(attr_order))); -} - -void KernelRegistry::AddKernel(const std::string &key, - KernelLauncher fn, - const std::vector &attr_order) { - CHECK(!impl_->data.count(key)) - << "kernel [" << key << "] is registered twice"; - impl_->data.emplace(key, - std::make_pair(std::move(fn), std::move(attr_order))); -} - -KernelImplementation KernelRegistry::GetKernel(const std::string &key) const { - auto it = impl_->data.find(key); - return it != impl_->data.end() ? it->second.first() : KernelImplementation{}; -} - -std::vector KernelRegistry::GetKernelList() const { - std::vector res(impl_->data.size()); - for (auto i : impl_->data) { - res.push_back(i.first); - } - return res; -} - -KernelRegistry::~KernelRegistry() {} - -size_t KernelRegistry::size() const { return impl_->data.size(); } - -KernelRegistry *GetCpuKernelRegistry() { - static auto registry = std::make_unique(); - return registry.get(); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/kernel_registry.h b/paddle/infrt/host_context/kernel_registry.h deleted file mode 100644 index a9f2b407bd4..00000000000 --- a/paddle/infrt/host_context/kernel_registry.h +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include - -namespace infrt { -namespace host_context { - -class KernelFrame; - -using KernelImplementation = std::function; -using KernelLauncher = std::function; - -/** - * Hold the kernels registered in the system. - */ -class KernelRegistry { - public: - KernelRegistry(); - - void AddKernel(const std::string &key, - KernelImplementation fn, - const std::vector &attrs_order = {}); - void AddKernel(const std::string &key, - KernelLauncher fn, - const std::vector &attrs_order = {}); - - KernelImplementation GetKernel(const std::string &key) const; - const std::vector &GetAttrNameList( - const std::string &key) const; - - std::vector GetKernelList() const; - - size_t size() const; - - ~KernelRegistry(); - - private: - class Impl; - - std::unique_ptr impl_; -}; - -//! The global CPU kernel registry. -KernelRegistry *GetCpuKernelRegistry(); - -} // namespace host_context -} // namespace infrt - -/** - * compile function RegisterKernels in C way to avoid C++ name mangling. - */ -#ifdef __cplusplus -extern "C" { -#endif -void RegisterKernels(infrt::host_context::KernelRegistry *registry); -#ifdef __cplusplus -} -#endif diff --git a/paddle/infrt/host_context/kernel_registry_test.cc b/paddle/infrt/host_context/kernel_registry_test.cc deleted file mode 100644 index fd2aecb3e6c..00000000000 --- a/paddle/infrt/host_context/kernel_registry_test.cc +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/kernel_registry.h" - -#include - -#include "paddle/infrt/host_context/kernel_utils.h" - -namespace infrt { -namespace host_context { - -int add_i32(int a, int b) { return a + b; } - -TEST(KernelRegistry, basic) { - KernelRegistry registry; - std::string key = "infrt.test.add.i32"; - registry.AddKernel(key, INFRT_KERNEL(add_i32)); - - const auto& kernel_impl = registry.GetKernel(key); - ASSERT_TRUE(kernel_impl); - - ValueRef a(1); - ValueRef b(2); - KernelFrameBuilder fbuilder; - fbuilder.AddArgument(a.get()); - fbuilder.AddArgument(b.get()); - fbuilder.SetNumResults(1); - - kernel_impl(&fbuilder); - - auto results = fbuilder.GetResults(); - ASSERT_EQ(results.size(), 1UL); - ASSERT_EQ(results[0]->get(), 3); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/kernel_utils.cc b/paddle/infrt/host_context/kernel_utils.cc deleted file mode 100644 index cf9476da032..00000000000 --- a/paddle/infrt/host_context/kernel_utils.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/kernel_utils.h" - -namespace infrt { -namespace host_context {} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/kernel_utils.h b/paddle/infrt/host_context/kernel_utils.h deleted file mode 100644 index 2f630dcc213..00000000000 --- a/paddle/infrt/host_context/kernel_utils.h +++ /dev/null @@ -1,354 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include - -#include - -#include "paddle/infrt/host_context/kernel_frame.h" -#include "paddle/infrt/host_context/value.h" - -namespace infrt { -namespace host_context { - -template -class Argument { - public: - explicit Argument(ValueRef value) : value_(value) {} - - ValueRef& value() { return value_; } - const ValueRef& value() const { return value_; } - - T& get() const { return value_.get(); } - - private: - ValueRef value_; -}; - -/** - * RemainingArguments collects all remaining arguments in an ArrayRef. - */ -class RemainingArguments { - public: - explicit RemainingArguments(llvm::ArrayRef remaining_arguments) - : remaining_arguments_(remaining_arguments) {} - - llvm::ArrayRef values() const { return remaining_arguments_; } - size_t size() const { return remaining_arguments_.size(); } - const Value* operator[](size_t i) const { return remaining_arguments_[i]; } - - private: - llvm::ArrayRef remaining_arguments_; -}; - -/** - * RemainingResults collects all remaining results in a MutableArrayRef. - */ -class RemainingResults { - public: - explicit RemainingResults(llvm::MutableArrayRef remaining_results) - : remaining_results_(remaining_results) {} - llvm::MutableArrayRef values() { return remaining_results_; } - size_t size() const { return remaining_results_.size(); } - - template - const ValueRef& AllocateAt(int index) { - // eagerly create a ValueRef - if (remaining_results_[index].get()) return remaining_results_[index]; - remaining_results_[index] = ValueRef(new Value); - return remaining_results_[index]; - } - ValueRef& operator[](size_t i) const { return remaining_results_[i]; } - - private: - llvm::MutableArrayRef remaining_results_; -}; - -template -class Result { - public: - explicit Result(ValueRef* result) : result_(result) {} - - template - void Emplace(Args&&... args) { - ValueRef v; - Set(T(std::forward(args)...)); - } - - void Set(Argument argument) { - CHECK(!result_->IsValid()); - *result_ = argument.value(); - } - - private: - ValueRef* result_{}; -}; - -template -class Attribute { - public: - explicit Attribute(const Value* value) : value_(value) {} - - const T& get() const { return value_->get(); } - - private: - const Value* value_; -}; - -template -class ArgumentView { - using UnderlyingT = typename ViewT::UnderlyingT; - - public: - explicit ArgumentView(Value* value) - : value_(value), arg_(&value->template get()) {} - - Value* value() const { return value_; } - ViewT& get() const { return arg_; } - ViewT* operator->() const { return &get(); } - ViewT& operator*() const { return get(); } - - private: - Value* value_{}; - mutable ViewT arg_; -}; - -template -struct KernelImpl; - -template -struct TypeTag {}; - -#define INFRT_KERNEL(...) \ - ::infrt::host_context::KernelImpl::Invoke - -template -struct KernelImpl { - static void Invoke(KernelFrame* frame) { - KernelCallHelper>::template Invoke<0, 0, 0>(frame); - } - - // Helper that introspects the arguments to derive the signature and cast - // parts of the KernelFrame to their type before passing them to impl_fn. - template - struct KernelCallHelper; - - // Casts the return value of the kernel, if non-void. - // bool _ is an unnecessary parameter to make compiler allow templace specific - // in non-namespace scope. - template - struct KernelReturnHelper { - static void Invoke(KernelFrame* frame, const Args&... args) { - HandleReturn(frame, impl_fn(args...)); - } - }; - - template - struct KernelReturnHelper { - static void Invoke(KernelFrame* frame, const Args&... args) { - impl_fn(args...); - } - }; - - // Specialization to cast a single input argument(Head). - template - struct KernelCallHelper, Tail...> { - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - static_assert(in_idx != -1, - "Do not place Arguments after RemainingArguments"); - static_assert(out_idx == 0, "Arguments should appear before results"); - static_assert(const_idx == 0, - "Arguments and results should appear before attributes."); - - Argument arg(frame->GetArgAt(in_idx)); - KernelCallHelper< - Tail...>::template Invoke(frame, - pargs..., - arg); - } - }; - - template - struct KernelCallHelper, Tail...> { - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - static_assert(in_idx != -1, - "Do not place Arguments after RemainingArguments"); - static_assert(out_idx == 0, "Arguments should appear before results"); - static_assert(const_idx == 0, - "Arguments and results should appear before attributes."); - - ArgumentView arg(frame->GetArgAt(in_idx)); - KernelCallHelper< - Tail...>::template Invoke(frame, - pargs..., - arg); - } - }; - - // Specialization to cast a single result argument (Head). - template - struct KernelCallHelper, Tail...> { - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - static_assert(out_idx != -1, - "Do not place Results after RemainingResults"); - // static_assert(const_idx == 0, - // "Arguments and results should appear before attributes"); - - // Result arg(&frame->GetResults()[out_idx]); - Result arg(new ValueRef()); - KernelCallHelper< - Tail...>::template Invoke(frame, - pargs..., - arg); - } - }; - - // Specialization to cast a single attribute. - template - struct KernelCallHelper, Tail...> { - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - // static_assert(const_idx != -1, - // "Do not place Attributes after RemainingAttributes"); - Attribute arg(frame->GetAttributeAt(const_idx)); - KernelCallHelper< - Tail...>::template Invoke(frame, - pargs..., - arg); - } - }; - - // Treat other pointer as an Argument. - template - struct KernelCallHelper { - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - static_assert(in_idx != -1, - "Do not place Arguments after RemainingArguments"); - static_assert(out_idx == 0, "Arguments should appear before results"); - // static_assert(const_idx == 0, - // "Arguments and results should appear before attributes."); - auto* arg = &frame->template GetElementAt(in_idx); - KernelCallHelper< - Tail...>::template Invoke(frame, - pargs..., - arg); - } - }; - - // Treat any other type as an Argument. - template - struct KernelCallHelper { - using ArgT = std::decay_t; - - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - static_assert(in_idx != -1, - "Do not place Arguments after RemainingArguments"); - static_assert(out_idx == 0, "Arguments should appear before results"); - static_assert(const_idx == 0, - "Arguments and results should appear before attributes."); - - auto* value = frame->GetElementAt(in_idx); - auto&& arg = value->get(); - - KernelCallHelper< - Tail...>::template Invoke(frame, - pargs..., - arg); - } - }; - - // RemainingArguments provides an ArrayRef containing all - // remaining arguments. Useful for variadic - // kernels. - template - struct KernelCallHelper { - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - static_assert(in_idx != -1, - "Do not use more than one RemainingArguments"); - static_assert(out_idx == 0, "Arguments should appear before results."); - static_assert(const_idx == 0, - "Arguments and results should appear before attributes"); - RemainingArguments remaining_arguments( - frame->GetArguments().drop_front(in_idx)); - - KernelCallHelper::template Invoke<-1, out_idx, const_idx>( - frame, pargs..., remaining_arguments); - } - }; - - // RemainingResults provides an MutableArrayRef containing all - // remaining results. - template - struct KernelCallHelper { - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - static_assert(out_idx != -1, "Do not use more than one RemainingResults"); - static_assert(const_idx == 0, - "Arguments and results should appear before attributes"); - llvm::MutableArrayRef returned_results = - frame->GetResults().drop_front(out_idx); - - llvm::SmallVector result_values; - for (size_t i = 0; i < returned_results.size(); i++) - result_values.emplace_back(returned_results[i]); - - RemainingResults remaining_results(result_values); - KernelCallHelper::template Invoke( - frame, pargs..., remaining_results); - } - }; - - // No arguments left. - template - struct KernelCallHelper> { - template - static void Invoke(KernelFrame* frame, const PreviousArgs&... pargs) { - KernelReturnHelper::Invoke(frame, pargs...); - } - }; - - // Handle pair result - template - static void HandleReturn(KernelFrame* frame, std::pair&& t) { - CHECK_EQ(frame->GetNumResults(), 2); - StoreResultAt(frame, 0, std::move(t.first)); - StoreResultAt(frame, 1, std::move(t.second)); - } - - // Store the function result back to the output Value in KernelFrame. - template - static void HandleReturn(KernelFrame* frame, T&& t) { - assert(frame->GetNumResults() == 1 && "Extra results passed to kernel."); - StoreResultAt(frame, 0, std::forward(t)); - } - - // Store result as an Value output in KernelFrame. - template - static void StoreResultAt(KernelFrame* frame, int index, T&& t) { - frame->EmplaceResult>(index, std::forward(t)); - } -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/kernel_utils_test.cc b/paddle/infrt/host_context/kernel_utils_test.cc deleted file mode 100644 index 71d8904eb79..00000000000 --- a/paddle/infrt/host_context/kernel_utils_test.cc +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/kernel_utils.h" - -#include - -namespace infrt { -namespace host_context { - -int add_i32(int a, int b) { return a + b; } -float add_f32(float a, float b) { return a + b; } -std::pair add_pair(int a, float b) { return {a, b}; } - -TEST(KernelImpl, i32) { - KernelFrameBuilder fbuilder; - ValueRef a(new Value(1)); - ValueRef b(new Value(2)); - fbuilder.AddArgument(a.get()); - fbuilder.AddArgument(b.get()); - fbuilder.SetNumResults(1); - - INFRT_KERNEL(add_i32)(&fbuilder); - auto results = fbuilder.GetResults(); - ASSERT_EQ(results.size(), 1UL); - ASSERT_EQ(results.front()->get(), 3); -} - -TEST(KernelImpl, f32) { - KernelFrameBuilder fbuilder; - ValueRef a(new Value(1.f)); - ValueRef b(new Value(2.f)); - fbuilder.AddArgument(a.get()); - fbuilder.AddArgument(b.get()); - fbuilder.SetNumResults(1); - - INFRT_KERNEL(add_f32)(&fbuilder); - auto results = fbuilder.GetResults(); - ASSERT_EQ(results.size(), 1UL); - ASSERT_EQ(results.front()->get(), 3.f); -} - -TEST(KernelImpl, pair) { - KernelFrameBuilder fbuilder; - ValueRef a(new Value(1)); - ValueRef b(new Value(3.f)); - - fbuilder.AddArgument(a.get()); - fbuilder.AddArgument(b.get()); - fbuilder.SetNumResults(2); - - INFRT_KERNEL(add_pair)(&fbuilder); - auto results = fbuilder.GetResults(); - ASSERT_EQ(results.size(), 2UL); - ASSERT_EQ(results[0]->get(), 1); - ASSERT_EQ(results[1]->get(), 3.f); -} - -void TestFunc(const std::string& arg_0, - const std::string& arg_1, - const std::string& arg_2, - Attribute attr_0, - Result res_0, - Result res_1) { - CHECK_EQ(arg_0, "arg_0"); - CHECK_EQ(arg_1, "arg_1"); - CHECK_EQ(arg_2, "arg_2"); - CHECK_EQ(attr_0.get(), "attr_0"); - - // res_0.Set(Argument(ValueRef(new Value()))); - // res_1.Set(Argument(ValueRef(new Value()))); -} - -TEST(KernelRegistry, basic) { - KernelFrameBuilder kernel_frame; - - Value arg_0(std::string{"arg_0"}); - Value arg_1(std::string{"arg_1"}); - Value arg_2(std::string{"arg_2"}); - Value attr_0(std::string{"attr_0"}); - - kernel_frame.AddArgument(&arg_0); - kernel_frame.AddArgument(&arg_1); - kernel_frame.AddArgument(&arg_2); - kernel_frame.AddAttribute(&attr_0); - kernel_frame.SetNumResults(2); - - CHECK_EQ(kernel_frame.GetNumArgs(), 3); - CHECK_EQ(kernel_frame.GetNumResults(), 2); - CHECK_EQ(kernel_frame.GetNumAttributes(), 1); - CHECK_EQ(kernel_frame.GetNumElements(), 6UL); - - CHECK_EQ(kernel_frame.GetArgAt(2), "arg_2"); - CHECK_EQ(kernel_frame.GetAttributeAt(0)->get(), "attr_0"); - - KernelImpl::Invoke(&kernel_frame); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/mlir_exec.cc b/paddle/infrt/host_context/mlir_exec.cc deleted file mode 100644 index 1ae7cdc742a..00000000000 --- a/paddle/infrt/host_context/mlir_exec.cc +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -#include -#include - -#include "llvm/Support/DynamicLibrary.h" -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/dialect/mlir_loader.h" -#include "paddle/infrt/host_context/core_runtime.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/mlir_to_runtime_translate.h" -#include "paddle/infrt/kernel/basic_kernels.h" -#include "paddle/infrt/kernel/control_flow_kernels.h" -#include "paddle/infrt/kernel/tensor_kernels.h" -#include "paddle/infrt/kernel/tensor_shape_kernels.h" -#include "paddle/infrt/kernel/test_kernels.h" -#ifdef INFRT_WITH_PHI -#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h" -#include "paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h" -#include "paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h" -#include "paddle/infrt/kernel/phi/registry.h" -#if defined(INFRT_WITH_GPU) && defined(INFRT_WITH_TRT) -#include "paddle/infrt/kernel/tensorrt/registry.h" -#endif // INFRT_WITH_GPU && INFRT_WITH_TRT -#endif // INFRT_WITH_PHI - -static llvm::cl::list cl_shared_libs( // NOLINT - "shared_libs", - llvm::cl::desc("Specify shared library with kernels."), - llvm::cl::ZeroOrMore, - llvm::cl::MiscFlags::CommaSeparated); - -int main(int argc, char** argv) { - using namespace llvm; // NOLINT - using namespace infrt; // NOLINT - cl::opt input_file("i", - cl::desc("Specify input filename"), - cl::value_desc("input file name")); - cl::ParseCommandLineOptions(argc, argv); - - mlir::MLIRContext* context = infrt::Global::getMLIRContext(); - auto module = dialect::LoadMlirFile(input_file.c_str(), context); - - host_context::KernelRegistry registry; - - kernel::RegisterBasicKernels(®istry); - kernel::RegisterTestKernels(®istry); - kernel::RegisterTensorShapeKernels(®istry); - kernel::RegisterTensorKernels(®istry); - kernel::RegisterControlFlowKernels(®istry); -#ifdef INFRT_WITH_PHI - kernel::RegisterPhiKernels(®istry); - kernel::RegisterInferShapeLaunchers(®istry); -#if defined(INFRT_WITH_GPU) && defined(INFRT_WITH_TRT) - kernel::RegisterTrtKernels(®istry); -#endif // INFRT_WITH_GPU && INFRT_WITH_TRT -#endif - - // load extra shared library - for (const auto& lib_path : cl_shared_libs) { - std::string err; - llvm::sys::DynamicLibrary dynLib = - llvm::sys::DynamicLibrary::getPermanentLibrary(lib_path.c_str(), &err); - if (!dynLib.isValid()) { - llvm::errs() << "Load shared library failed. Error: " << err << "\n"; - return 1; - } - if (auto reg_sym = dynLib.SearchForAddressOfSymbol("RegisterKernels")) { - auto reg_func = - reinterpret_cast(reg_sym); - reg_func(®istry); - } else { - llvm::outs() << "Symbol \"RegisterKernels\" not found in \"" << lib_path - << "\". Skip.\n"; - } - } - - context->loadAllAvailableDialects(); - mlir::PassManager pm(context); - -#ifdef INFRT_WITH_PHI - mlir::OpPassManager& phi_pass_manager = pm.nest(); - - std::vector valid_places = {{infrt::TargetType::CPU, - infrt::PrecisionType::FLOAT32, - infrt::LayoutType::NCHW}}; - phi_pass_manager.addPass(infrt::CreatePhiOpCvtPass(valid_places)); - phi_pass_manager.addPass(infrt::CreateInfrtOpFusePass()); -#endif - - if (mlir::failed(pm.run(*module))) { - std::cout << "\npass failed!\n" << std::endl; - return 4; - } - - host_context::TestMlir(module.get(), ®istry); - - std::cout << std::endl; - return 0; -} diff --git a/paddle/infrt/host_context/mlir_function_executable.cc b/paddle/infrt/host_context/mlir_function_executable.cc deleted file mode 100644 index ec8d43f99ba..00000000000 --- a/paddle/infrt/host_context/mlir_function_executable.cc +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/mlir_function_executable.h" - -#include -#include - -#include // NOLINT - -#include "paddle/infrt/common/common.h" -#include "paddle/infrt/host_context/core_runtime.h" - -namespace infrt { -namespace host_context { - -template -std::string DumpToString(T& op) { // NOLINT - std::string buffer; - llvm::raw_string_ostream os(buffer); - op.print(os); - os.flush(); - return buffer; -} - -MlirFunctionExecutable::MlirFunctionExecutable( - mlir::FuncOp func_op, - KernelRegistry* kernel_registry, - MlirToRuntimeTranslator::function_defs_t& function_table) - : Function(func_op.getName().str(), - func_op.getNumArguments(), - func_op.getNumResults()), - MlirToRuntimeTranslator(&core_runtime_builder_), - region_(&func_op.getRegion()), - kernel_registry_(kernel_registry), - core_runtime_builder_(kernel_registry), - function_table_(function_table) {} - -MlirFunctionExecutable::MlirFunctionExecutable( - mlir::Region* region, - mlir::FunctionType func_type, - KernelRegistry* kernel_registry, - MlirToRuntimeTranslator::function_defs_t& function_table) - : Function("", func_type.getNumInputs(), func_type.getNumResults()), - MlirToRuntimeTranslator(&core_runtime_builder_), - region_(region), - kernel_registry_(kernel_registry), - core_runtime_builder_(kernel_registry), - function_table_(function_table) {} - -void MlirFunctionExecutable::BuildExecutables( - llvm::ArrayRef arguments, - llvm::MutableArrayRef results, - bool is_region) { - CHECK_EQ(arguments.size(), num_arguments()); - // We use the function call's arguments as op_executable's operands to avoid - // copy. - for (size_t i = 0; i < num_arguments(); i++) { - AddValue(region_->getArgument(i), arguments[i]); - } - - // build the program - auto& blocks = region_->getBlocks(); - CHECK_EQ(blocks.size(), 1UL) - << "function with more than one block is not supported yet"; - - llvm::SmallVector runtime_results; - for (auto& op : blocks.front()) { - if (EmitConstantOp(&op)) continue; - if (EmitBuildShapeOp(&op)) continue; - - llvm::SmallVector mlir_results; - if (EmitReturnOp(&op, &mlir_results)) { - if (!is_region) { - for (auto v : mlir_results) { - runtime_results.push_back(GetValue(v)); - } - } - continue; - } - - if (EmitCallOp(&op, &function_table_)) continue; - - if (EmitGeneralOp(&op, *kernel_registry_)) continue; - LOG(FATAL) << "Not supported op: " << DumpToString(op); - } - - // after the block is built, we can get the result values of the whole - // function call in the runtime_results. - - mlir::SmallVector results_copied; - if (!is_region) { - for (ValueRef& x : results) { - results_copied.push_back(x.get()); - } - } - - // set a lambda function to help copy the results from the runtime results in - // the local function to outer program. - CHECK_EQ(results_copied.size(), runtime_results.size()); - this->copy_res_fn_ = [results_copied, runtime_results] { - VLOG(4) << "copy results to result"; - for (size_t i = 0; i < results_copied.size(); i++) { - VLOG(4) << ".. copy " << runtime_results[i] << " to " - << results_copied[i]; - CopyTo(*runtime_results[i], results_copied[i]); - } - }; -} - -void MlirFunctionExecutable::Execute(llvm::ArrayRef arguments, - llvm::MutableArrayRef results, - bool is_region) const { - CHECK_EQ(arguments.size(), num_arguments()); - CHECK_EQ(results.size(), num_results()); - - if (core_runtime_builder_.num_ops() == 0) { - Reference(this).BuildExecutables(arguments, results, is_region); - } - - Reference(&core_runtime_builder_).Execute(); - - copy_res_fn_(); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/mlir_function_executable.h b/paddle/infrt/host_context/mlir_function_executable.h deleted file mode 100644 index cd9161d01bb..00000000000 --- a/paddle/infrt/host_context/mlir_function_executable.h +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include - -#include -#include - -#include "paddle/infrt/host_context/core_runtime.h" -#include "paddle/infrt/host_context/function.h" -#include "paddle/infrt/host_context/mlir_to_runtime_translate.h" - -namespace infrt { -namespace host_context { - -struct KernelRegistry; - -/** - * Executable function for a given MLIR function definition, mainly used in two - * scenerios: - * 1. infrt.call op - * 2. main function call - * - * A MlirFunctionExecutable might have one or more arguments and results. - */ -class MlirFunctionExecutable : public Function, public MlirToRuntimeTranslator { - public: - using function_defs_t = std::unordered_map; - - MlirFunctionExecutable(mlir::FuncOp func_op, - KernelRegistry* kernel_registry, - function_defs_t& function_table); // NOLINT - - MlirFunctionExecutable( - mlir::Region* region, - mlir::FunctionType func_type, - KernelRegistry* kernel_registry, - MlirToRuntimeTranslator::function_defs_t& function_table); // NOLINT - - /** - * Execute the function with the given arguments and results. - * NOTE the \param arguments and \param results should not be altered. - */ - void Execute(llvm::ArrayRef arguments, - llvm::MutableArrayRef results, - bool is_region = false) const; - - private: - /** - * Build the runtime executables once the function call arguments and results - * are passed in. - * This will trigger in the first execution. - */ - void BuildExecutables(llvm::ArrayRef arguments, - llvm::MutableArrayRef results, - bool is_region); - - private: - mlir::Region* region_{}; - KernelRegistry* kernel_registry_{}; - CoreRuntimeBuilder core_runtime_builder_; - MlirToRuntimeTranslator::function_defs_t& function_table_; - std::function copy_res_fn_; -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/mlir_program_executor.cc b/paddle/infrt/host_context/mlir_program_executor.cc deleted file mode 100644 index c5009bcc97c..00000000000 --- a/paddle/infrt/host_context/mlir_program_executor.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/mlir_program_executor.h" - -namespace infrt { -namespace host_context {} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/mlir_program_executor.h b/paddle/infrt/host_context/mlir_program_executor.h deleted file mode 100644 index 7808c460457..00000000000 --- a/paddle/infrt/host_context/mlir_program_executor.h +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "paddle/infrt/host_context/core_runtime.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/mlir_function_executable.h" -#include "paddle/infrt/host_context/mlir_to_runtime_translate.h" -#include "paddle/infrt/host_context/op_executable.h" - -namespace infrt { -namespace host_context { - -/** - * This get a MLIR program as input, it compiles it into runtime program, and - * one can retrieve the function and execute - * it by passing the input arguments. - */ -class MlirProgramExecutor : public MlirToRuntimeTranslator { - public: - CoreRuntimeBuilder runtime_builder; - mlir::ModuleOp module; - function_defs_t function_defs; - - MlirProgramExecutor(mlir::ModuleOp module, KernelRegistry* registry) - : MlirToRuntimeTranslator(module, &runtime_builder), - runtime_builder(registry), - module(module) {} - - // Build functions and generate executables. - void BuildFunctions() { EmitFunctions(); } - - void EmitFunction(mlir::FuncOp op) override { - LOG(INFO) << "Emit function: " << op.getName().str(); - function_defs[op.getName().str()] = op; - - func_executables_.emplace( - op.getName().str(), - new MlirFunctionExecutable( - op, runtime_builder.kernel_registry(), function_defs)); - } - - MlirFunctionExecutable* LookupFunc(const std::string& name) { - auto it = func_executables_.find(name); - if (it != func_executables_.end()) { - return it->second.get(); - } - return nullptr; - } - - private: - std::unordered_map> - func_executables_; -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/mlir_tests/basic.mlir b/paddle/infrt/host_context/mlir_tests/basic.mlir deleted file mode 100644 index 263d5884134..00000000000 --- a/paddle/infrt/host_context/mlir_tests/basic.mlir +++ /dev/null @@ -1,30 +0,0 @@ -// CHECK-LABEL: basic -func @basic() -> f32 { - %v0 = infrt.constant.f32 1.0 - %v1 = infrt.constant.f32 2.0 - %v2 = "infrt.add.f32"(%v0, %v1) : (f32, f32) -> f32 - - // CHECK: 1 - "infrt.print.f32"(%v0) : (f32) -> () - // CHECK: 2 - "infrt.print.f32"(%v1) : (f32) -> () - - // CHECK: 3 - "infrt.print.f32"(%v2) : (f32) -> () - - %v3 = "infrt.mul.f32"(%v2, %v1) : (f32, f32) -> f32 - - // CHECK: 6 - "infrt.print.f32"(%v3) : (f32) -> () - - infrt.return %v3 : f32 -} - -// CHECK-LABEL: basic1 -// Check the mlir executor can work with more than one function in a file. -func @basic1() -> () { - %v0 = infrt.constant.f32 1.0 - "infrt.print.f32"(%v0) : (f32) -> () - // CHECK: 1 - infrt.return -} \ No newline at end of file diff --git a/paddle/infrt/host_context/mlir_tests/dense_tensor.mlir b/paddle/infrt/host_context/mlir_tests/dense_tensor.mlir deleted file mode 100644 index 1a7fa28f1e5..00000000000 --- a/paddle/infrt/host_context/mlir_tests/dense_tensor.mlir +++ /dev/null @@ -1,9 +0,0 @@ -// CHECK-LABEL: build_tensor1 -func @build_tensor1() { - %a = dt.create_uninit_tensor.f32 [3, 4] -> !infrt.dense_tensor - dt.fill_tensor_with_constant.f32 (%a : !infrt.dense_tensor) {value=1.0:f32} - // CHECK: tensor: shape=shape[3,4], values=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] - dt.print_tensor (%a : !infrt.dense_tensor) - - infrt.return -} diff --git a/paddle/infrt/host_context/mlir_tests/shape.mlir b/paddle/infrt/host_context/mlir_tests/shape.mlir deleted file mode 100644 index 691ce62cbf8..00000000000 --- a/paddle/infrt/host_context/mlir_tests/shape.mlir +++ /dev/null @@ -1,7 +0,0 @@ -// CHECK-LABEL: build_tensor1 -func @build_tensor1() { - %a = ts.build_shape [1:i64, 57:i64, 92:i64] - // CHECK: shape[1,57,92] - ts.print_shape %a - infrt.return -} diff --git a/paddle/infrt/host_context/mlir_to_runtime_translate.cc b/paddle/infrt/host_context/mlir_to_runtime_translate.cc deleted file mode 100644 index 9b2190be23c..00000000000 --- a/paddle/infrt/host_context/mlir_to_runtime_translate.cc +++ /dev/null @@ -1,707 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/mlir_to_runtime_translate.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/common/string.h" -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/mlir_loader.h" -#include "paddle/infrt/dialect/tensor_shape.h" -#include "paddle/infrt/host_context/core_runtime.h" -#include "paddle/infrt/host_context/kernel_frame.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/mlir_function_executable.h" -#include "paddle/infrt/host_context/op_executable.h" -#include "paddle/infrt/host_context/value.h" -#include "paddle/infrt/tensor/tensor_shape.h" - -#ifdef INFRT_WITH_PHI -#ifdef INFRT_WITH_TRT -#include "paddle/infrt/kernel/tensorrt/trt_kernels.h" -#endif -#include "paddle/phi/core/dense_tensor.h" -#endif - -namespace infrt { -namespace host_context { - -template -std::string DumpToString(T& op) { // NOLINT - std::string buffer; - llvm::raw_string_ostream os(buffer); - op.print(os); - os.flush(); - return buffer; -} - -struct MlirToRuntimeTranslator::Impl { - mlir::ModuleOp module; - // The runtime for a function call. - CoreRuntimeBuilder* runtime{}; - // The current working op, the translator process the ops one by one, each - // time it updates `cur_op` here to current op - // working on. - OpExecutableBuilder* cur_op{}; - - // record the current function name. - std::string cur_func_name; - - // Name to function definitions. - std::unordered_map func_defs; - - // Map from an operation to its results. - std::unordered_map> op_results; - llvm::DenseMap value_map; -}; - -bool MlirToRuntimeTranslator::EmitConstantOp(mlir::Operation* op) { - if (!infrt::Startswith(op->getName().getStringRef().str(), "infrt.constant")) - return false; - VLOG(3) << "Emitting constant op [" << op->getName().getStringRef().str() - << "]"; - - auto attr = op->getAttr("value"); - if (attr.isa()) { - if (attr.getType().isF32()) { - impl_->op_results[op] = {ValueRef( - static_cast(attr.cast().getValueAsDouble()))}; - } else if (attr.getType().isF64()) { - impl_->op_results[op] = {ValueRef(static_cast( - attr.cast().getValueAsDouble()))}; - } else { - LOG(FATAL) << "Not supported attribute type"; - } - return true; - } - - if (attr.isa()) { - if (attr.getType().isInteger(32)) { - impl_->op_results[op] = {ValueRef( - static_cast(attr.cast().getSInt()))}; - } else if (attr.getType().isInteger(64)) { - impl_->op_results[op] = {ValueRef( - static_cast(attr.cast().getSInt()))}; - } else if (attr.getType().isInteger(1)) { - impl_->op_results[op] = { - ValueRef(static_cast(attr.cast().getInt()))}; - } else { - LOG(FATAL) << "Not supported attribute type"; - } - return true; - } - - LOG(FATAL) << "Not supported constant attribute type"; - return true; -} - -template <> -paddle::optional MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa()) return paddle::none; - if (attr.isa()) { - auto val = attr.cast(); - if (val.getType().isInteger(32)) { - return val.getValue().getSExtValue(); - } - } - return paddle::none; -} -template <> -paddle::optional MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa()) return paddle::none; - if (attr.isa()) { - auto val = attr.cast(); - if (val.getType().isInteger(64)) { - return val.getValue().getSExtValue(); - } - } - return paddle::none; -} - -// TODO(Superjomn) Make double and float parsing share some thing. -template <> -paddle::optional MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa()) return paddle::none; - if (attr.isa()) { - auto val = attr.cast(); - if (val.getType().isF32()) return val.getValueAsDouble(); - } - return paddle::none; -} - -template <> -paddle::optional MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa()) return paddle::none; - if (attr.isa()) { - auto val = attr.cast(); - return val.getValue(); - } - return paddle::none; -} - -template <> -paddle::optional MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa()) return paddle::none; - if (attr.isa()) { - auto val = attr.cast(); - if (val.getType().isF64()) return val.getValueAsDouble(); - } - return paddle::none; -} - -template <> -paddle::optional<::infrt::TargetType> MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa<::infrt::TargetAttr>()) return paddle::none; - if (attr.isa<::infrt::TargetAttr>()) { - return attr.cast<::infrt::TargetAttr>().getTarget(); - } - return paddle::none; -} - -template <> -paddle::optional<::infrt::LayoutType> MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa<::infrt::LayoutAttr>()) return paddle::none; - if (attr.isa<::infrt::LayoutAttr>()) { - return attr.cast<::infrt::LayoutAttr>().getLayout(); - } - return paddle::none; -} - -template <> -paddle::optional<::infrt::PrecisionType> MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa<::infrt::PrecisionAttr>()) return paddle::none; - if (attr.isa<::infrt::PrecisionAttr>()) { - return attr.cast<::infrt::PrecisionAttr>().getPrecision(); - } - return paddle::none; -} - -template <> -paddle::optional MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa()) return paddle::none; - return attr.cast().getValue().str(); -} - -#define PROCESS_ARRAY_INT(type__, bits__) \ - template <> \ - paddle::optional> \ - MlirToRuntimeTranslator::EmitAttribute(const mlir::Attribute& attr) { \ - if (!attr.isa()) return paddle::none; \ - auto array = attr.cast(); \ - CHECK(!array.empty()); \ - \ - if (!array[0].getType().isInteger(bits__)) { \ - return paddle::none; \ - } \ - \ - std::vector res; \ - for (auto& v : array) { \ - res.push_back(v.cast().getValue().getSExtValue()); \ - } \ - return res; \ - } - -PROCESS_ARRAY_INT(bool, 1); -PROCESS_ARRAY_INT(int16_t, 16); -PROCESS_ARRAY_INT(int32_t, 32); -PROCESS_ARRAY_INT(int64_t, 64); - -template <> -paddle::optional> MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa()) return paddle::none; - auto array = attr.cast(); - CHECK(!array.empty()); - - if (!array[0].getType().isF32()) return paddle::none; - - std::vector res; - for (auto& v : array) { - res.push_back(v.cast().getValueAsDouble()); - } - return res; -} - -template <> -paddle::optional> MlirToRuntimeTranslator::EmitAttribute( - const mlir::Attribute& attr) { - if (!attr.isa()) return paddle::none; - auto array = attr.cast(); - CHECK(!array.empty()); - - if (!array[0].getType().isF64()) return paddle::none; - - std::vector res; - for (auto& v : array) { - res.push_back(v.cast().getValueAsDouble()); - } - return res; -} - -static bool IsReturn(mlir::Operation* op) { - return op->getName().getStringRef() == "infrt.return"; -} - -bool MlirToRuntimeTranslator::EmitGeneralOp( - mlir::Operation* op, const KernelRegistry& kernel_registry) { - CHECK(impl_->runtime); - impl_->cur_op = - impl_->runtime->NewOpExecutable(op->getName().getStringRef().str()); - - VLOG(3) << "processing general op : " << op->getName().getStringRef().str(); - // TODO(wilber): Find a more appropriate way to handle special cases. - if (op->getName().getStringRef() == "trt.create_engine") { -#ifdef INFRT_WITH_TRT - auto* symbols = impl_->runtime->symbol_table(); - ::infrt::kernel::tensorrt::MlirOperationWithInfrtSymbol mlir_operation; - mlir_operation.operation = op; - mlir_operation.symbol_table = symbols; - impl_->cur_op->AppendArgument(new Value(mlir_operation)); - // TODO(wilber): how to pass DenseTensor to create_engine op? temporialiy - // add a naive implement. - for (int i = 0, e = op->getNumOperands(); i < e; ++i) { - auto operand = op->getOperand(i); - Value* arg_value{nullptr}; - if (operand.isa()) { - mlir::BlockArgument arg = operand.dyn_cast(); - arg_value = GetValue(arg); - } else { - arg_value = GetValue(operand); - if (!arg_value) { - auto upstream_op = operand.getDefiningOp(); - arg_value = GetOpResult(upstream_op); - } - } - if (arg_value->is_type<::Tensor>()) { - impl_->runtime->FeedInArgs( - std::make_pair(std::to_string(i), ValueRef(arg_value))); - } - } -#else - CHECK(false) << "should not reach here"; -#endif - } else { - // process operands - for (int i = 0, e = op->getNumOperands(); i < e; i++) { - // function argument as value - auto operand = op->getOperand(i); - /// if (operand.getKind() == mlir::Value::Kind::BlockArgument) { - if (operand.isa()) { - mlir::BlockArgument arg = operand.dyn_cast(); - Value* arg_value = GetValue(arg); - impl_->cur_op->AppendArgument(arg_value); - VLOG(3) << "* op mlir operand: " << DumpToString(arg) << " " - << GetValue(arg); - continue; - } - - // normal value - Value* arg_value = GetValue(operand); - if (!arg_value) { - auto upstream_op = operand.getDefiningOp(); - arg_value = GetOpResult(upstream_op); - } - CHECK(arg_value) << "No-exist argument value found: " - << DumpToString(operand); - impl_->cur_op->AppendArgument(arg_value); - - VLOG(3) << "* op mlir operand: " << DumpToString(operand) << " " - << GetValue(operand) << " vs " << arg_value; - } - } - - // process attributes - auto attrs = op->getAttrs(); - - // MLIR's underlying attr storage type is `Builtin_Dictionary`, and its - // elements are sorted by name. The following code adapts the order of - // function signatures of the phi operator library. - llvm::SmallVector tmp; - tmp.resize(attrs.size()); - const std::string& kernel_name = op->getName().getStringRef().str(); - const auto& attr_names = kernel_registry.GetAttrNameList(kernel_name); - if (attrs.size()) { - if (attr_names.empty()) { - LOG(WARNING) << "The kernel `" << kernel_name - << "` has not been registered with attributes order "; - } else { - CHECK_EQ(attr_names.size(), attrs.size()) - << "The number of kernel `" << kernel_name - << "` attributes specified by mlir (" << attrs.size() - << ") is inconsistent with the registration (" << attr_names.size() - << ")."; - } - } - - auto get_offset = [](const char* attr, - const std::vector& names, - const std::string& kernel_name) -> int { - for (size_t i = 0; i < names.size(); ++i) { - if (!std::strcmp(attr, names[i])) { - return i; - } - } - LOG(WARNING) << "The attribute `" << attr << "` of kernel `" << kernel_name - << "` is not properly register"; - return -1; - }; - - for (size_t i = 0; i < attrs.size(); i++) { - auto& attr = attrs[i]; - int offset{}; - if (attr_names.size()) { - offset = get_offset(attr.getName().data(), attr_names, kernel_name); - } else { - offset = i; - } - CHECK_GT(offset, -1); - if (auto v = EmitAttribute(attr.getValue())) { - tmp[offset] = new Value(*v); - } else if (auto v = EmitAttribute(attr.getValue())) { - tmp[offset] = new Value(*v); - } else if (auto v = EmitAttribute(attr.getValue())) { - tmp[offset] = new Value(*v); - } else if (auto v = EmitAttribute(attr.getValue())) { - tmp[offset] = new Value(*v); - } else if (auto v = EmitAttribute(attr.getValue())) { - tmp[offset] = new Value(std::move(*v)); - } else if (auto v = EmitAttribute(attr.getValue())) { - tmp[offset] = new Value(*v); - } else if (auto v = EmitAttribute<::infrt::TargetType>(attr.getValue())) { - tmp[offset] = new Value(*v); - } else if (auto v = - EmitAttribute<::infrt::PrecisionType>(attr.getValue())) { - tmp[offset] = new Value(*v); - } else if (auto v = EmitAttribute<::infrt::LayoutType>(attr.getValue())) { - tmp[offset] = new Value(*v); - } else if (auto v = EmitAttribute>(attr.getValue())) { - tmp[offset] = new Value(std::move(*v)); - } else if (auto v = EmitAttribute>(attr.getValue())) { - tmp[offset] = new Value(std::move(*v)); - } else if (auto v = EmitAttribute>(attr.getValue())) { - tmp[offset] = new Value(std::move(*v)); - } else if (auto v = EmitAttribute>(attr.getValue())) { - tmp[offset] = new Value(std::move(*v)); - } else if (auto v = EmitAttribute>(attr.getValue())) { - tmp[offset] = new Value(std::move(*v)); - } else { - LOG(FATAL) << "Not supported attribute type"; - } - } - - for (size_t i = 0; i < tmp.size(); i++) { - impl_->cur_op->AppendAttribute(tmp[i]); - } - - // process regions, we treat regions as attribute. - auto num_regions = op->getNumRegions(); - if (num_regions > 0) { - CHECK_EQ(num_regions, 1UL) - << "op with more than one region is not supported yet."; - auto& region = op->getRegions().front(); - auto num_blocks = region.getBlocks().size(); - CHECK_EQ(num_blocks, 1UL) - << "region with more than one block is not supported yet."; - - // process arguments - llvm::SmallVector inputs; - auto& block = region.getBlocks().front(); - for (auto arg : block.getArguments()) inputs.push_back(arg.getType()); - - // process results - // NOTE: if an op contains a region, we simply ignore the region's return - // values, - // or its return values will conflict with op's return values. - llvm::SmallVector results; - - auto func_type = - mlir::FunctionType::get(region.getContext(), inputs, results); - auto* function = impl_->cur_op->CreateFunctionExecutable( - ®ion, func_type, &impl_->func_defs); - impl_->cur_op->AppendAttribute(new Value(function)); - } - - // process results - llvm::SmallVector res_values; - for (int i = 0, e = op->getNumResults(); i < e; i++) { - auto res = op->getResult(i); - if (res.getType().isa<::infrt::DenseTensorType>()) { - auto r = - impl_->value_map.try_emplace(res, ValueRef(new Value{::Tensor()})); - CHECK(r.second) << "Duplicate add mlir value [" << DumpToString(res) - << "]"; - res_values.push_back(r.first->second.get()); - } else { - res_values.push_back(AddValue(res)); - } - - VLOG(3) << "* op mlir res: " << DumpToString(res) << " " << GetValue(res); - } - impl_->cur_op->SetResults(res_values); - -#ifdef INFRT_DEBUG - { - VLOG(3) << "check result"; - for (int i = 0; i < impl_->cur_op->frame().GetNumResults(); i++) { - VLOG(3) << "+ res value: " << impl_->cur_op->frame().GetResults()[i]; - } - } -#endif - - return true; -} - -bool MlirToRuntimeTranslator::EmitReturnOp( - mlir::Operation* op, llvm::SmallVectorImpl* results) { - CHECK(results); - if (op->getName().getStringRef() == "infrt.return") { - for (size_t i = 0; i < op->getNumOperands(); i++) { - results->push_back(op->getOperand(i)); - } - - return true; - } - return false; -} - -bool MlirToRuntimeTranslator::EmitFunctions() { - for (auto func_op : impl_->module.getOps()) { - EmitFunction(func_op); - } - return true; -} - -void MlirToRuntimeTranslator::EmitFunction(mlir::FuncOp op) { - impl_->func_defs[op.getName().str()] = op; -} - -Value* MlirToRuntimeTranslator::GetOpResult(mlir::Operation* op) { - auto it = impl_->op_results.find(op); - return it == impl_->op_results.end() ? nullptr : it->second.front().get(); -} - -Value* MlirToRuntimeTranslator::GetValue(mlir::Value value) { - auto it = impl_->value_map.find(value); - return it == impl_->value_map.end() ? nullptr : it->second.get(); -} - -Value* MlirToRuntimeTranslator::AddValue(mlir::Value value) { - auto res = impl_->value_map.try_emplace(value, ValueRef(new Value)); - CHECK(res.second) << "Duplicate add mlir value [" << DumpToString(value) - << "]"; - return res.first->second.get(); -} - -MlirToRuntimeTranslator::~MlirToRuntimeTranslator() {} - -void MlirToRuntimeTranslator::UpdateCurFuncName(const std::string& name) { - impl_->cur_func_name = std::string(name); -} - -MlirToRuntimeTranslator::MlirToRuntimeTranslator(mlir::ModuleOp module, - CoreRuntimeBuilder* runtime) - : impl_(new Impl) { - CHECK(runtime); - impl_->module = module; - impl_->runtime = runtime; -} - -bool MlirToRuntimeTranslator::EmitBuildShapeOp(mlir::Operation* op) { - if (op->getName().getStringRef() != "ts.build_shape") return false; - - auto value = op->getAttr("value"); - - CHECK(value.isa()); - auto values = value.cast().getValue(); - std::vector dims; - for (auto& attr_v : values) { - dims.push_back(attr_v.cast().getInt()); - } - impl_->op_results[op] = { - ValueRef(new Value(tensor::TensorShape(llvm::ArrayRef(dims))))}; - - return true; -} - -bool MlirToRuntimeTranslator::EmitCallOp(mlir::Operation* op, - function_defs_t* function_table) { - CHECK(op); - CHECK(function_table); - if (op->getName().getStringRef() != "infrt.call") return false; - - impl_->cur_op = - impl_->runtime->NewOpExecutable(op->getName().getStringRef().str()); - - auto callee = op->getAttr("callee"); - auto callee_name = callee.dyn_cast(); - - // process arguments - for (size_t i = 0; i < op->getNumOperands(); i++) { - auto operand = op->getOperand(i); - auto* arg_value = GetValue(operand); - - if (!arg_value) { - auto upstream_op = operand.getDefiningOp(); - arg_value = GetOpResult(upstream_op); - } - CHECK(arg_value) << "No-exist argument value found: " - << DumpToString(operand); - impl_->cur_op->AppendArgument(arg_value); - } - - // process attribute - auto& table = function_table ? *function_table : impl_->func_defs; - { - // lookup the callee function - auto it = table.find(callee_name.getValue().str()); - CHECK(it != table.end()) - << "can't find function [" << callee_name.getValue().str() << "]"; - auto* function = - impl_->cur_op->CreateFunctionExecutable(it->second, &impl_->func_defs); - impl_->cur_op->AppendAttribute(new Value(function)); - } - - // process results - llvm::SmallVector res_values; - for (int i = 0, e = op->getNumResults(); i < e; i++) { - auto res = op->getResult(i); - res_values.push_back(AddValue(res)); - } - impl_->cur_op->SetResults(res_values); - - VLOG(3) << "Emit call " << callee_name.getValue().str() << " " - << impl_->cur_op->frame(); - return true; -} - -MlirToRuntimeTranslator::MlirToRuntimeTranslator(CoreRuntimeBuilder* runtime) - : impl_(new Impl) { - CHECK(runtime); - impl_->runtime = runtime; -} - -Value* MlirToRuntimeTranslator::AddValue(mlir::Value mlir_value, Value* value) { - auto it = impl_->value_map.try_emplace(mlir_value, ValueRef(value)); - CHECK(it.second) << "duplicate add value " << DumpToString(mlir_value); - return value; -} - -void MlirToRuntimeTranslate(mlir::ModuleOp module, - CoreRuntimeBuilder* runtime) { - MlirToRuntimeTranslator(module, runtime).Run(); -} - -/** - * Execute the mlir program in test mode -- print some debug information to - * stdout. - */ -class MlirProgramTestExecutor : public MlirToRuntimeTranslator { - public: - CoreRuntimeBuilder core_runtime; - - MlirProgramTestExecutor(mlir::ModuleOp module, KernelRegistry* registry) - : MlirToRuntimeTranslator(module, &core_runtime), - core_runtime(registry), - registry(registry) { - CHECK(registry); - } - - void Run() { - EmitFunctions(); - - CHECK(registry); - for (auto func_op : impl_->module.getOps()) { - VLOG(3) << "Running function " << func_op.getName().str(); - EmitAndRunFuncWithoutArguments(func_op); - } - } - - protected: - std::unordered_map func_def_table; - - void EmitFunction(mlir::FuncOp op) override { - CHECK(!impl_->func_defs.count(op.getName().str())) - << "Duplicate function defition found for function [" - << op.getName().str(); - impl_->func_defs.emplace(op.getName().str(), op); - } - - private: - void EmitAndRunFuncWithoutArguments(mlir::FuncOp func) { - // print the function name for llvm FileChecker macro, CHECK-LABEL - std::cout << '@' << func.getName().str() << std::endl; - if (func.getNumArguments() == - 0) { // an entry function, execute it immediately - VLOG(3) << "executing function " << func.getName().str(); - // Emit and execute each function - CoreRuntimeBuilder runtime(registry); - impl_->runtime = &runtime; - - auto& blocks = func.getBlocks(); - CHECK_EQ(blocks.size(), 1UL) - << "function with more than one block is not supported yet"; - - for (auto& op : blocks.front()) { - if (EmitConstantOp(&op)) continue; - if (EmitBuildShapeOp(&op)) continue; - llvm::SmallVector results; - if (EmitReturnOp(&op, &results)) continue; - if (EmitCallOp(&op, &impl_->func_defs)) continue; - if (EmitGeneralOp(&op, *registry)) continue; - LOG(FATAL) << "Not supported op: " << DumpToString(op); - } - - runtime.Execute(); - - } else { - VLOG(2) << "get an callable function: " << func.getName().str(); - } - } - - private: - KernelRegistry* registry{}; -}; - -void TestMlir(mlir::ModuleOp module, KernelRegistry* registry) { - MlirProgramTestExecutor execute(module, registry); - execute.Run(); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/mlir_to_runtime_translate.h b/paddle/infrt/host_context/mlir_to_runtime_translate.h deleted file mode 100644 index 212b13c3c0c..00000000000 --- a/paddle/infrt/host_context/mlir_to_runtime_translate.h +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -#include // NOLINT -#include //NOLINT -#include // NOLINT - -namespace mlir { -class FuncOp; -class ModuleOp; -class Operation; -class Attribute; -class Value; -} // namespace mlir - -namespace infrt { -namespace host_context { - -class CoreRuntimeBuilder; -class Value; -class ValueRef; -class KernelRegistry; - -/** - * MlirToRuntimeTranslator helps to translate a MLIR program to a CoreRuntime. - * This is the base class of all the modules those parse a MLIR program and - * finally generate a CoreRuntime. - */ -class MlirToRuntimeTranslator { - public: - //! Holds all the function definitions. - using function_defs_t = std::unordered_map; - - explicit MlirToRuntimeTranslator(CoreRuntimeBuilder* runtime); - MlirToRuntimeTranslator(mlir::ModuleOp module, CoreRuntimeBuilder* runtime); - - void Run() { EmitFunctions(); } - - virtual ~MlirToRuntimeTranslator(); - - protected: - //! Emit a "infrt.constant.*" operation, return true if succeed. - bool EmitConstantOp(mlir::Operation* op); - //! Emit a "infrt.return" operation. - bool EmitReturnOp(mlir::Operation* op, - llvm::SmallVectorImpl* results); - //! Emit a "ts.build_shape" operation. - bool EmitBuildShapeOp(mlir::Operation* op); - //! Emit an operation other than the special cases above. - bool EmitGeneralOp(mlir::Operation* op, - const KernelRegistry& kernel_registry); - //! Emit all the functions. - bool EmitFunctions(); - - //! Emit a single function, this is an API that should be implemented by - //! inherients. - virtual void EmitFunction(mlir::FuncOp op); - - bool EmitCallOp(mlir::Operation* op, function_defs_t* function_table); - - template - paddle::optional EmitAttribute(const mlir::Attribute& attr); - - Value* GetOpResult(mlir::Operation* op); - - Value* GetValue(mlir::Value value); - - Value* AddValue(mlir::Value value); - - Value* AddValue(mlir::Value mlir_value, Value* value); - - void UpdateCurFuncName(const std::string& name); - - protected: - struct Impl; - std::unique_ptr impl_; -}; - -/** - * Build a CoreRuntime from a MLIR module. - */ -void MlirToRuntimeTranslate(mlir::ModuleOp module, CoreRuntimeBuilder* runtime); - -/** - * Execute a MLIR program, that is execute all the functions without input - * arguments. - * This is mainly used by testcase. - * @param module a MLIR module. - * @param registry the kernel registry containing all the valid kernels. - */ -void TestMlir(mlir::ModuleOp module, KernelRegistry* registry); - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/mlir_to_runtime_translate_test.cc b/paddle/infrt/host_context/mlir_to_runtime_translate_test.cc deleted file mode 100644 index 31615fbc3f6..00000000000 --- a/paddle/infrt/host_context/mlir_to_runtime_translate_test.cc +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/mlir_to_runtime_translate.h" - -#include -#include - -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/dialect/mlir_loader.h" -#include "paddle/infrt/host_context/core_runtime.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/host_context/mlir_program_executor.h" -#include "paddle/infrt/kernel/basic_kernels.h" -#include "paddle/infrt/kernel/control_flow_kernels.h" -#include "paddle/infrt/kernel/tensor_kernels.h" -#include "paddle/infrt/kernel/tensor_shape_kernels.h" -#include "paddle/infrt/kernel/test_kernels.h" - -namespace infrt { -namespace host_context { - -TEST(MlirToRuntimeTranslate, basic) { - mlir::MLIRContext context; - - auto source = R"ROC( -func @main() -> () { - %v0 = infrt.constant.f32 1.0 - %v1 = infrt.constant.f32 2.0 - %v2 = "infrt.add.f32"(%v0, %v1) : (f32, f32) -> f32 - %v3 = "infrt.mul.f32"(%v2, %v1) : (f32, f32) -> f32 - - "infrt.print.f32"(%v1) : (f32) -> () - - infrt.return -} -)ROC"; - - auto module = dialect::LoadMlirSource(&context, source); - EXPECT_TRUE(mlir::succeeded(module->verify())); - - KernelRegistry registry; - kernel::RegisterFloatBasicKernels(®istry); - kernel::RegisterIntBasicKernels(®istry); - - TestMlir(module.get(), ®istry); -} - -TEST(TestMlir, basic) { - mlir::MLIRContext context; - - auto source = R"ROC( -func @main() -> () { - %v0 = infrt.constant.f32 1.0 - %v1 = infrt.constant.f32 2.0 - %v2 = "infrt.add.f32"(%v0, %v1) : (f32, f32) -> f32 - %v3 = "infrt.mul.f32"(%v2, %v1) : (f32, f32) -> f32 - - "infrt.print.f32"(%v1) : (f32) -> () - - infrt.return -} -)ROC"; - - auto module = dialect::LoadMlirSource(&context, source); - EXPECT_TRUE(mlir::succeeded(module->verify())); - - KernelRegistry registry; - kernel::RegisterFloatBasicKernels(®istry); - kernel::RegisterIntBasicKernels(®istry); - - TestMlir(module.get(), ®istry); -} - -TEST(TestMlir, shadow_copy_tensor_profile) { - mlir::MLIRContext* context = infrt::Global::getMLIRContext(); - - auto head = R"ROC( -func @predict(%a: !infrt.dense_tensor, %b: !infrt.dense_tensor) -> (!infrt.dense_tensor, !infrt.dense_tensor) { -)ROC"; - - auto tpl0 = - "%a{0} = dt.shallow_copy_tensor %a : !infrt.dense_tensor -> " - "!infrt.dense_tensor"; - auto tpl1 = - "%b{0} = dt.shallow_copy_tensor %b : !infrt.dense_tensor -> " - "!infrt.dense_tensor"; - - auto end = R"ROC( -infrt.return %a0, %b0: !infrt.dense_tensor, !infrt.dense_tensor -} - )ROC"; - - std::stringstream ss; - ss << head; - for (int i = 0; i < 2000; i++) { - ss << llvm::formatv(tpl0, i).str() << "\n"; - ss << llvm::formatv(tpl1, i).str() << "\n"; - } - ss << end; - - auto content = ss.str(); - - // LOG(INFO) << "content: " << content << std::endl; - - auto module = dialect::LoadMlirSource(context, content); - EXPECT_TRUE(mlir::succeeded(module->verify())); - - host_context::KernelRegistry registry; - - kernel::RegisterBasicKernels(®istry); - kernel::RegisterTestKernels(®istry); - kernel::RegisterTensorShapeKernels(®istry); - kernel::RegisterTensorKernels(®istry); - kernel::RegisterControlFlowKernels(®istry); - - MlirProgramExecutor executor(*module, ®istry); - executor.BuildFunctions(); - - auto* func = executor.LookupFunc("predict"); - ASSERT_TRUE(func); - - std::vector in_args; - std::vector out_args( - {ValueRef(new Value(tensor::DenseHostTensor())), - ValueRef(new Value(tensor::DenseHostTensor()))}); - - auto create_tensor = [] { - tensor::DenseHostTensor a(tensor::TensorShape{{200, 3000}}, - DType(DType::Kind::F32)); - auto* data = reinterpret_cast(a.raw_data()); - for (int i = 0; i < a.shape().GetNumElements(); i++) { - data[i] = i; - } - return a; - }; - - std::vector inputs({ValueRef(new Value(create_tensor())), - ValueRef(new Value(create_tensor()))}); - in_args.assign({inputs[0].get(), inputs[1].get()}); - - for (int i = 0; i < 500; i++) { - func->Execute( - llvm::ArrayRef(in_args.data(), in_args.size()), - llvm::MutableArrayRef(out_args.data(), out_args.size())); - } -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/op_executable.cc b/paddle/infrt/host_context/op_executable.cc deleted file mode 100644 index b53dc0545c7..00000000000 --- a/paddle/infrt/host_context/op_executable.cc +++ /dev/null @@ -1,165 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/op_executable.h" - -#include - -#include -#include - -#include "paddle/infrt/host_context/kernel_frame.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/mlir_function_executable.h" -#include "paddle/infrt/host_context/symbol_table.h" - -namespace infrt { -namespace host_context { - -struct OpExecutable::Impl { - Impl(const std::string& op_name, - SymbolTable* symbol_table, - KernelRegistry* kernel_registry) - : name(op_name), - symbol_table(symbol_table), - kernel_registry(kernel_registry ? kernel_registry - : GetCpuKernelRegistry()) { - CHECK(kernel_registry); - } - - inline bool to_execute() const { - return !run_once || run_once && !has_executed; - } - inline void MarkRun() { has_executed = true; } - - std::string name; - SymbolTable* symbol_table{}; - KernelFrameBuilder frame; - KernelRegistry* kernel_registry{}; - - std::unique_ptr mlir_function_executable; - - KernelImplementation kernel_impl{}; - - //! Tell whether this Op should be executed only once. - bool run_once{}; - //! Tell whether this op has been executed. - bool has_executed{}; -}; - -OpExecutable::OpExecutable(OpExecutable::Impl* impl) : impl_(impl) {} - -const std::string& OpExecutable::name() const { return impl_->name; } - -OpExecutableBuilder::OpExecutableBuilder(const std::string& op_name, - SymbolTable* symbol_table, - KernelRegistry* kernel_registry) - : OpExecutable(new Impl(op_name, symbol_table, kernel_registry)) { - CHECK(impl_); - // CPU kernel registry is the default KernelRegistry. - impl_->kernel_impl = impl_->kernel_registry->GetKernel( - std::string(op_name.data(), op_name.size())); - // TODO(Superjomn) support other device other than CPU. - CHECK(impl_->kernel_impl) << "No CPU kernel called " << op_name; - - // TODO(wilber): Maybe we can use the MLIR trait or other facilities to remove - // the run_once set. - std::unordered_set run_once_set{ - "dt.get_param", - "trt.create_engine", - "phi_dt.create_host_inited_dense_tensor.f32", - "phi_dt.create_context.cpu", - "phi_dt.create_context.gpu"}; - if (run_once_set.count(op_name)) { - impl_->run_once = true; - } -} - -void OpExecutableBuilder::AppendArgument(const std::string& name) { - if (!impl_->symbol_table->GetValue(name)) { - impl_->symbol_table->Register(name); - } - impl_->frame.AddArgument(impl_->symbol_table->GetValue(name)); -} - -void OpExecutableBuilder::AppendArgument(Value* value) { - impl_->frame.AddArgument(value); -} - -KernelFrame& OpExecutable::frame() { return impl_->frame; } -const KernelFrame& OpExecutable::frame() const { return impl_->frame; } - -void OpExecutableBuilder::SetResults(llvm::ArrayRef result_names) { - llvm::SmallVector results; - for (size_t result_id = 0; result_id < result_names.size(); result_id++) { - Value* value = impl_->symbol_table->Register(result_names[result_id]); - results.push_back(value); - } - impl_->frame.SetResults(results); -} - -void OpExecutableBuilder::SetResults(llvm::ArrayRef results) { - impl_->frame.SetResults(results); -} - -void OpExecutableBuilder::AppendAttribute(Value* value) { - impl_->frame.AddAttribute(value); -} - -OpExecutableBuilder::OpExecutableBuilder(OpExecutableBuilder&& other) - : OpExecutable(other.impl_.release()) {} - -MlirFunctionExecutable* OpExecutableBuilder::CreateFunctionExecutable( - mlir::FuncOp op, MlirToRuntimeTranslator::function_defs_t* function_defs) { - CHECK(!impl_->mlir_function_executable); - impl_->mlir_function_executable.reset( - new MlirFunctionExecutable(op, impl_->kernel_registry, *function_defs)); - return impl_->mlir_function_executable.get(); -} - -MlirFunctionExecutable* OpExecutableBuilder::CreateFunctionExecutable( - mlir::Region* region, - mlir::FunctionType func_type, - function_defs_t* function_defs) { - CHECK(!impl_->mlir_function_executable); - impl_->mlir_function_executable.reset(new MlirFunctionExecutable( - region, func_type, impl_->kernel_registry, *function_defs)); - return impl_->mlir_function_executable.get(); -} - -void OpExecutable::Execute() { -#ifndef NDEBUG - VLOG(3) << "execute " << name() - << " --- frame args: " << impl_->frame.GetNumArgs() << " results " - << impl_->frame.GetNumResults() << " attributes " - << impl_->frame.GetNumAttributes() << "\n" - << frame().DumpArgTypes(); - for (int i = 0; i < impl_->frame.GetNumArgs(); i++) { - VLOG(3) << "function arg: " << impl_->frame.GetArgAt(i); - } - for (int i = 0; i < impl_->frame.GetNumResults(); i++) { - VLOG(3) << "function result: " << impl_->frame.GetResults()[i]; - } -#endif - - if (impl_->to_execute()) { - impl_->kernel_impl(&impl_->frame); - impl_->MarkRun(); - } -} - -OpExecutable::~OpExecutable() {} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/op_executable.h b/paddle/infrt/host_context/op_executable.h deleted file mode 100644 index b80b99fd414..00000000000 --- a/paddle/infrt/host_context/op_executable.h +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include - -#include -#include -#include - -namespace mlir { -class FuncOp; -} // namespace mlir - -namespace infrt { -namespace host_context { - -class SymbolTable; -class KernelRegistry; -class KernelFrame; -class Value; -class CoreRuntimeBuilder; -class MlirFunctionExecutable; - -/** - * OpExecutable is a runtime executable instance for an operation. It captures - * all the information(Tensors, attributes - * and so on) needed for execution. - * With the SymbolTable and op definition, it create and hold a KernelFrame once - * and execute any times. - */ -class OpExecutable { - public: - KernelFrame& frame(); - const KernelFrame& frame() const; - - void Execute(); - - const std::string& name() const; - - ~OpExecutable(); - - protected: - class Impl; - explicit OpExecutable(Impl* impl); - - std::unique_ptr impl_; -}; - -/** - * Builder to help contruct an OpExecutable. - */ -class OpExecutableBuilder : public OpExecutable { - public: - using function_defs_t = std::unordered_map; - - OpExecutableBuilder(const std::string& op_name, - SymbolTable* symbol_table, - KernelRegistry* kernel_registry = nullptr); - OpExecutableBuilder(OpExecutableBuilder&& other); - - void AppendArgument(const std::string& name); - void AppendArgument(Value* value); - - void SetResults(llvm::ArrayRef result_names); - void SetResults(llvm::ArrayRef results); - - void AppendAttribute(Value* value); - - MlirFunctionExecutable* CreateFunctionExecutable( - mlir::FuncOp op, function_defs_t* function_defs); - - MlirFunctionExecutable* CreateFunctionExecutable( - mlir::Region* region, - mlir::FunctionType func_type, - function_defs_t* function_defs); -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/op_executable_test.cc b/paddle/infrt/host_context/op_executable_test.cc deleted file mode 100644 index f981cca4426..00000000000 --- a/paddle/infrt/host_context/op_executable_test.cc +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/op_executable.h" - -#include - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/host_context/symbol_table.h" - -namespace infrt { -namespace host_context { - -int add(int a, int b) { return a + b; } - -TEST(OpExecutable, basic) { - // register kernel - KernelRegistry registry; - registry.AddKernel("infrt.test.add.i32", INFRT_KERNEL(add)); - - SymbolTable table; - table.Register("a", 1); - table.Register("b", 2); - - OpExecutableBuilder executable("infrt.test.add.i32", &table, ®istry); - executable.AppendArgument("a"); - executable.AppendArgument("b"); - executable.SetResults({"c"}); - - executable.Execute(); - - // check the kernel frame has the result. - auto results = executable.frame().GetResults(); - ASSERT_EQ(results.size(), 1UL); - ASSERT_EQ(results.front()->get(), 3); - - // check symbol table contains the same result instance. - LOG(INFO) << "type: " << table.GetValue("c")->type_info(); - int c = table.GetValue("c")->get(); - ASSERT_EQ(c, 3); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/paddle_mlir.cc b/paddle/infrt/host_context/paddle_mlir.cc deleted file mode 100644 index 0264920a600..00000000000 --- a/paddle/infrt/host_context/paddle_mlir.cc +++ /dev/null @@ -1,523 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/paddle_mlir.h" - -#include -#include - -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.h" -#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h" -#include "paddle/infrt/dialect/pd/common/pd_ops_info.h" -#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h" -#include "paddle/infrt/dialect/phi/ir/phi_base.h" - -MLIRModelGenImpl::MLIRModelGenImpl() - : context_(infrt::Global::getMLIRContext()), builder_(context_) { - context_->getOrLoadDialect(); - context_->getOrLoadDialect(); - context_->getOrLoadDialect(); - context_->getOrLoadDialect(); - context_->getOrLoadDialect<::infrt::InfrtDialect>(); - context_->getOrLoadDialect<::infrt::phi::PHIDialect>(); - context_->getOrLoadDialect<::infrt::phi::PHIDenseTensorDialect>(); - module_ = mlir::ModuleOp::create(mlir::UnknownLoc::get(context_)); -} - -infrt::paddle::framework_proto::ProgramDesc MLIRModelGenImpl::ParsePaddleModel( - const std::string &model_file) { - model_file_ = model_file; - infrt::paddle::framework_proto::ProgramDesc program_proto = - *infrt::paddle::LoadProgram(model_file); - return program_proto; -} - -mlir::ModuleOp MLIRModelGenImpl::ImportPaddleModel(const std::string &model_dir, - bool arg_has_map) { - model_dir_ = model_dir; - infrt::paddle::framework_proto::ProgramDesc program_proto = - ParsePaddleModel(model_dir + "/__model__"); - return ImportPaddleModel(program_proto, arg_has_map); -} - -mlir::ModuleOp MLIRModelGenImpl::ImportPaddleModel( - const std::string &model_file, - const std::string ¶m_file, - bool arg_has_map) { - model_file_ = model_file; - params_file_ = param_file; - infrt::paddle::framework_proto::ProgramDesc program_proto = - ParsePaddleModel(model_file); - return ImportPaddleModel(program_proto, arg_has_map); -} - -mlir::ModuleOp MLIRModelGenImpl::ImportPaddleModel( - const infrt::paddle::framework_proto::ProgramDesc &program, - bool arg_has_map) { - main_block_ = program.blocks(0); - llvm::SmallVector operandTypes = - GetModelInputsType(program, arg_has_map); - llvm::SmallVector resultTypes = GetModelOutputsType(program); - mlir::FuncOp mainFunc = UpdateModelModule(operandTypes, resultTypes); - UpdateModelParams(program, &mainFunc, arg_has_map); - UpdateModelOps(program); - UpdateModelOutputs(program); - return module_; -} - -mlir::FuncOp MLIRModelGenImpl::UpdateModelModule( - llvm::SmallVector operandTypes, - llvm::SmallVector resultTypes) { - // create main op - const std::string &name = "main_graph"; - auto mainFunc = mlir::FuncOp::create( - mlir::UnknownLoc::get(context_), - name, - /*type=*/builder_.getFunctionType({operandTypes}, {resultTypes}), - /*attrs=*/{}); - module_.push_back(mainFunc); - mainFunc.addEntryBlock(); - builder_.setInsertionPointToStart(&mainFunc.body().back()); - return mainFunc; -} - -llvm::SmallVector MLIRModelGenImpl::GetModelInputsType( - const infrt::paddle::framework_proto::ProgramDesc &program, - bool arg_has_map) { - llvm::SmallVector operandTypes; - if (arg_has_map) { - operandTypes.push_back(infrt::phi::DenseTensorMapType::get(context_)); - } - for (auto &op_desc : main_block_.ops()) { - if (op_desc.type() != "feed") continue; - for (int var_idx = 0; var_idx < op_desc.outputs_size(); ++var_idx) { - // update input variables - auto &in = op_desc.outputs()[var_idx]; - std::string input_var_name = in.arguments(0); - for (int i = 0; i < main_block_.vars_size(); i++) { - auto var_desc = main_block_.vars(i); - if (var_desc.name() == input_var_name) { - std::vector dims = RepeatedToVector( - var_desc.type().lod_tensor().tensor().dims()); - infrt::PrecisionType precision_; - ConvertDataTypeToInfrt( - var_desc.type().lod_tensor().tensor().data_type(), &precision_); - mlir::Type type_ = - infrt::DenseTensorType::get(context_, - infrt::TargetType::CPU, - precision_, - infrt::LayoutType::NCHW); - - operandTypes.push_back(type_); - } - } - } - } - return operandTypes; -} - -llvm::SmallVector MLIRModelGenImpl::GetModelOutputsType( - const infrt::paddle::framework_proto::ProgramDesc &program) { - llvm::SmallVector resultTypes; - for (auto &op_desc : main_block_.ops()) { - if (op_desc.type() != "fetch") continue; - for (int var_idx = 0; var_idx < op_desc.inputs_size(); ++var_idx) { - auto &in = op_desc.inputs()[var_idx]; - std::string input_var_name = in.arguments(0); - for (int i = 0; i < main_block_.vars_size(); i++) { - auto var_desc = main_block_.vars(i); - if (var_desc.name() == input_var_name) { - std::vector dims = RepeatedToVector( - var_desc.type().lod_tensor().tensor().dims()); - infrt::PrecisionType precision_; - ConvertDataTypeToInfrt( - var_desc.type().lod_tensor().tensor().data_type(), &precision_); - mlir::Type type_ = - infrt::DenseTensorType::get(context_, - infrt::TargetType::CPU, - precision_, - infrt::LayoutType::NCHW); - resultTypes.push_back(type_); - } - } - } - } - return resultTypes; -} - -void MLIRModelGenImpl::UpdateModelOps( - const infrt::paddle::framework_proto::ProgramDesc &program) { - for (auto &op_desc : main_block_.ops()) { - if (op_desc.type() == "feed" || op_desc.type() == "fetch") { - continue; - } - buildOperation(op_desc); - } -} - -void MLIRModelGenImpl::UpdateModelParams( - const infrt::paddle::framework_proto::ProgramDesc &program, - mlir::FuncOp *mainFunc, - bool arg_has_map) { - // update input vars - int input_index; - if (arg_has_map) - input_index = 1; - else - input_index = 0; - for (auto &op_desc : main_block_.ops()) { - if (op_desc.type() == "feed") { - for (int var_idx = 0; var_idx < op_desc.outputs_size(); ++var_idx) { - // update input variables - auto &in = op_desc.outputs()[var_idx]; - std::string input_var_name = in.arguments(0); - ::mlir::Value input_ = mainFunc->getArgument(input_index++); - params_map_.insert( - std::pair(input_var_name, input_)); - } - } - } - ::mlir::Value map; - if (arg_has_map) { - map = mainFunc->getArgument(0); - } else { - builder_.setInsertionPointToStart(&mainFunc->body().front()); - if (!model_dir_.empty()) { - auto load_op = builder_.create<::infrt::phi::LoadParamsOp>( - mlir::UnknownLoc::get(context_), - ::infrt::phi::DenseTensorMapType::get(context_), - builder_.getStringAttr(model_dir_)); - map = load_op.out(); - } else if (!model_file_.empty()) { - auto load_op = builder_.create<::infrt::phi::LoadCombinedParamsOp>( - mlir::UnknownLoc::get(context_), - ::infrt::phi::DenseTensorMapType::get(context_), - builder_.getStringAttr(model_file_), - builder_.getStringAttr(params_file_)); - map = load_op.out(); - } - } - - // update persistable tensors - for (int i = 0; i < main_block_.vars_size(); i++) { - auto var_desc = main_block_.vars(i); - if (params_map_.find(var_desc.name()) != params_map_.end()) continue; - if (var_desc.name() != "feed" && var_desc.name() != "fetch" && - var_desc.persistable()) { - auto name = builder_.getStringAttr(var_desc.name()); - std::vector dims = RepeatedToVector( - var_desc.type().lod_tensor().tensor().dims()); - infrt::PrecisionType precision_; - ConvertDataTypeToInfrt(var_desc.type().lod_tensor().tensor().data_type(), - &precision_); - mlir::Type type_ = infrt::DenseTensorType::get(context_, - infrt::TargetType::CPU, - precision_, - infrt::LayoutType::NCHW); - auto op = builder_.create<::infrt::phi::TensorMapGetTensorOp>( - mlir::UnknownLoc::get(context_), type_, map, name); - params_map_.insert(std::pair( - var_desc.name(), op.getOperation()->getResult(0))); - } - } -} - -void MLIRModelGenImpl::UpdateModelOutputs( - const infrt::paddle::framework_proto::ProgramDesc &program) { - // update outputs - for (auto &op_desc : main_block_.ops()) { - if (op_desc.type() == "fetch") { - for (int var_idx = 0; var_idx < op_desc.inputs_size(); ++var_idx) { - auto &in = op_desc.inputs()[var_idx]; - // varibale name - std::string input_var_name = in.arguments(0); - // update model outpus - mlir::Location loc = mlir::UnknownLoc::get(context_); - llvm::SmallVector operands; - - operands.push_back((params_map_[input_var_name])); - - llvm::SmallVector resultTypes; - llvm::SmallVector attrs; - - mlir::OperationState state(loc, - ::infrt::ReturnOp::getOperationName(), - operands, - resultTypes, - attrs); - builder_.createOperation(state); - } - } - } -} - -void MLIRModelGenImpl::buildOperation( - const infrt::paddle::framework_proto::OpDesc &op_) { - const std::string op_name = "pd." + op_.type(); - mlir::Location loc = mlir::UnknownLoc::get(context_); - mlir::OperationState result(loc, op_name); - - if (!result.name.isRegistered()) { - LOG(FATAL) << "Find unregistered operation: " << op_name; - return; - } - - if (result.name.hasTrait()) { - LOG(FATAL) << "Find operation: " << op_name - << "has trait: AttrSizedOperandSegments. Current not support!"; - return; - } - - llvm::SmallVector operands = GetOpInputValue(op_); - int empty_operand_cnt = 0; - for (auto it = operands.begin(); it != operands.end();) { - if (*it) { - ++it; - } else { - operands.erase(it); - ++empty_operand_cnt; - } - } - if (empty_operand_cnt > 1) { - LOG(FATAL) - << "Find operation: " << op_name << ", has " << empty_operand_cnt - << " empty operands. current not support empty operands more than one!"; - return; - } - result.addOperands(operands); - llvm::SmallVector resultTypes = GetOpOutputType(op_); - llvm::SmallVector attrs = GetOpAttributes(op_); - result.addTypes(resultTypes); - result.addAttributes(attrs); - mlir::Operation *mlir_op_ = builder_.createOperation(result); - RegisterOpOutputVars(op_, mlir_op_); -} - -llvm::SmallVector MLIRModelGenImpl::GetOpInputValue( - const infrt::paddle::framework_proto::OpDesc &op_) { - llvm::SmallVector operands; - - std::unordered_map inputs_info = {}; - if (pd_dialect_inputs_info_map_.count(op_.type())) - inputs_info = pd_dialect_inputs_info_map_.at(op_.type()); - operands.resize(inputs_info.size()); - for (int var_idx = 0; var_idx < op_.inputs_size(); ++var_idx) { - auto &var = op_.inputs(var_idx); - if (!var.arguments().empty()) { - if (!inputs_info.count(var.parameter())) continue; - operands[inputs_info.at(var.parameter())] = - params_map_[var.arguments()[0]]; - } - } - return operands; -} - -llvm::SmallVector MLIRModelGenImpl::GetOpOutputType( - const infrt::paddle::framework_proto::OpDesc &op_) { - llvm::SmallVector resultTypes; - - std::unordered_map pd_dialect_outputs_info = {}; - if (pd_dialect_outputs_info_map_.count(op_.type())) - pd_dialect_outputs_info = pd_dialect_outputs_info_map_.at(op_.type()); - resultTypes.resize(pd_dialect_outputs_info.size()); - // update op outputs info - for (int var_idx = 0; var_idx < op_.outputs_size(); ++var_idx) { - auto &var_name = op_.outputs(var_idx).arguments()[0]; - if (!pd_dialect_outputs_info.count(op_.outputs(var_idx).parameter())) - continue; - // update persistable tensors - for (int i = 0; i < main_block_.vars_size(); i++) { - auto var_desc = main_block_.vars(i); - if (var_desc.name() == var_name) { - std::vector dims = RepeatedToVector( - var_desc.type().lod_tensor().tensor().dims()); - infrt::PrecisionType precision_; - ConvertDataTypeToInfrt( - var_desc.type().lod_tensor().tensor().data_type(), &precision_); - mlir::Type type_ = infrt::DenseTensorType::get(context_, - infrt::TargetType::CPU, - precision_, - infrt::LayoutType::NCHW); - resultTypes[pd_dialect_outputs_info.at( - op_.outputs(var_idx).parameter())] = type_; - } - } - } - return resultTypes; -} - -llvm::SmallVector MLIRModelGenImpl::GetOpAttributes( - const infrt::paddle::framework_proto::OpDesc &op_) { - // GetInputVarName - llvm::SmallVector attrs; - -#define ATTR_IMPL_CASE(PROTO_TYPE, PROTO_TYPE_METHOD, MLIR_TYPE_METHOD) \ - case infrt::paddle::framework_proto::AttrType::PROTO_TYPE: { \ - auto data = op_.attrs(attrs_num).PROTO_TYPE_METHOD(); \ - auto value_ = builder_.MLIR_TYPE_METHOD(data); \ - auto name_ = builder_.getStringAttr(attr_name_); \ - auto attr_ = mlir::NamedAttribute(name_, value_); \ - attrs.push_back(attr_); \ - break; \ - } - -#define REPEATED_ATTR_IMPLE_CASE( \ - PROTO_TYPE, PROTO_TYPE_METHOD, MLIR_TYPE, MLIR_TYPE_METHOD) \ - case infrt::paddle::framework_proto::AttrType::PROTO_TYPE: { \ - std::vector data; \ - for (const auto &var : op_.attrs(attrs_num).PROTO_TYPE_METHOD()) { \ - data.push_back(MLIR_TYPE(var)); \ - } \ - auto value_ = \ - builder_.MLIR_TYPE_METHOD(llvm::makeArrayRef(data)); \ - auto name_ = builder_.getStringAttr(attr_name_); \ - auto attr_ = mlir::NamedAttribute(name_, value_); \ - attrs.push_back(attr_); \ - break; \ - } - -#define UNIMPLEMENTED_ATTR_IMPL_CASE(PROTO_TYPE) \ - case infrt::paddle::framework_proto::AttrType::PROTO_TYPE: { \ - std::cout << "Unimplemented attr type: framework_proto::AttrType::" \ - << #PROTO_TYPE << std::endl; \ - break; \ - } - - // get registered attributes - const std::string &op_name = "pd." + op_.type(); - mlir::RegisteredOperationName registered_op_name_ = - mlir::RegisteredOperationName::lookup(op_name, context_).getValue(); - llvm::ArrayRef attr_names_ = - registered_op_name_.getAttributeNames(); - std::vector attr_names_vec_ = attr_names_.vec(); - // update attrs - for (int attrs_num = 0; attrs_num < op_.attrs_size(); attrs_num++) { - auto attr_name_ = op_.attrs(attrs_num).name(); - auto type = op_.attrs(attrs_num).type(); - if (!std::count(attr_names_vec_.begin(), attr_names_vec_.end(), attr_name_)) - continue; - switch (type) { - ATTR_IMPL_CASE(FLOAT, f, getF32FloatAttr); - ATTR_IMPL_CASE(BOOLEAN, b, getBoolAttr); - ATTR_IMPL_CASE(INT, i, getSI32IntegerAttr); - ATTR_IMPL_CASE(LONG, l, getI64IntegerAttr); - ATTR_IMPL_CASE(STRING, s, getStringAttr); - - REPEATED_ATTR_IMPLE_CASE( - STRINGS, strings, llvm::StringRef, getStrArrayAttr); - REPEATED_ATTR_IMPLE_CASE(FLOATS, floats, float, getF32ArrayAttr); - REPEATED_ATTR_IMPLE_CASE(INTS, ints, int32_t, getI32ArrayAttr); - REPEATED_ATTR_IMPLE_CASE(LONGS, longs, int64_t, getI64ArrayAttr); - - // Unimplemented attr type, will be supported later @DannyIsFunny - // bools attribute is not supported due to bug of llvm. - // REPEATED_ATTR_IMPLE_CASE(BOOLEANS, bools, bool, getBoolArrayAttr); - UNIMPLEMENTED_ATTR_IMPL_CASE(BOOLEANS); - UNIMPLEMENTED_ATTR_IMPL_CASE(BLOCK); - UNIMPLEMENTED_ATTR_IMPL_CASE(BLOCKS); - default: - std::cout << "error attribute" << attr_name_ << std::endl; - } - } - return attrs; -} - -void MLIRModelGenImpl::RegisterOpOutputVars( - const infrt::paddle::framework_proto::OpDesc &op_, - mlir::Operation *mlir_op_) { - std::unordered_map pd_dialect_outputs_info = - pd_dialect_outputs_info_map_.at(op_.type()); - - // op outputs - for (int var_idx = 0; var_idx < op_.outputs_size(); ++var_idx) { - if (!pd_dialect_outputs_info.count(op_.outputs(var_idx).parameter())) - continue; - auto &var_name = op_.outputs(var_idx).arguments()[0]; - int index = pd_dialect_outputs_info[op_.outputs(var_idx).parameter()]; - // output name - auto var_ = mlir_op_->getResult(index); - params_map_.insert(std::pair(var_name, var_)); - } -} - -bool ConvertDataType(infrt::paddle::framework_proto::VarType::Type dtype, - mlir::Builder builder, - mlir::Type *type) { - switch (dtype) { - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_FP16: - *type = builder.getF16Type(); - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_FP32: - *type = builder.getF32Type(); - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_FP64: - *type = builder.getF64Type(); - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_BOOL: - *type = builder.getIntegerType(1); - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_INT8: - *type = builder.getIntegerType(8); - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_INT16: - *type = builder.getIntegerType(16); - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_INT32: - *type = builder.getIntegerType(32); - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_INT64: - *type = builder.getIntegerType(64); - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_UINT8: - *type = builder.getIntegerType(8, /*isSigned=*/false); - return true; - default: - return false; - } -} - -bool ConvertDataTypeToInfrt(infrt::paddle::framework_proto::VarType::Type dtype, - infrt::PrecisionType *type) { - switch (dtype) { - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_FP16: - *type = infrt::PrecisionType::FLOAT16; - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_FP32: - *type = infrt::PrecisionType::FLOAT32; - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_FP64: - *type = infrt::PrecisionType::FLOAT64; - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_BOOL: - *type = infrt::PrecisionType::BOOL; - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_INT8: - *type = infrt::PrecisionType::INT8; - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_INT16: - *type = infrt::PrecisionType::INT16; - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_INT32: - *type = infrt::PrecisionType::INT32; - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_INT64: - *type = infrt::PrecisionType::INT64; - return true; - case infrt::paddle::framework_proto::VarType::Type::VarType_Type_UINT8: - *type = infrt::PrecisionType::UINT8; - return true; - default: - return false; - } -} diff --git a/paddle/infrt/host_context/paddle_mlir.h b/paddle/infrt/host_context/paddle_mlir.h deleted file mode 100644 index 629181cca3d..00000000000 --- a/paddle/infrt/host_context/paddle_mlir.h +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#ifndef PADDLE_INFRT_HOST_CONTEXT_PADDLE_MLIR_H_ -#define PADDLE_INFRT_HOST_CONTEXT_PADDLE_MLIR_H_ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/common/string.h" -#include "paddle/infrt/dialect/dense_tensor.h" -#include "paddle/infrt/dialect/infrt/ir/basic_kernels.h" -#include "paddle/infrt/dialect/init_dialects.h" -#include "paddle/infrt/dialect/pd/ir/pd_ops.h" -#include "paddle/infrt/dialect/tensor_shape.h" -#include "paddle/infrt/paddle/model_parser.h" - -class MLIRModelGenImpl { - public: - MLIRModelGenImpl(); - mlir::ModuleOp ImportPaddleModel(const std::string &model_file, - const std::string ¶m_file, - bool arg_has_map = true); - mlir::ModuleOp ImportPaddleModel(const std::string &model_dir, - bool arg_has_map = true); - - private: - // parse paddle model file - infrt::paddle::framework_proto::ProgramDesc ParsePaddleModel( - const std::string &model_file); - - // convert paddle model proto into paddle dialect module - mlir::ModuleOp ImportPaddleModel( - const infrt::paddle::framework_proto::ProgramDesc &program, - bool arg_has_map); - - // get inputs and outputs info from program_desc - llvm::SmallVector GetModelInputsType( - const infrt::paddle::framework_proto::ProgramDesc &program, - bool arg_has_map); - llvm::SmallVector GetModelOutputsType( - const infrt::paddle::framework_proto::ProgramDesc &program); - // create main function module - mlir::FuncOp UpdateModelModule(llvm::SmallVector operandTypes, - llvm::SmallVector resultTypes); - // convert paddle ops into paddle dialect ops (in mlir form) - void UpdateModelOps( - const infrt::paddle::framework_proto::ProgramDesc &program); - // convert persistable params and inputs variable into mlir domain - void UpdateModelParams( - const infrt::paddle::framework_proto::ProgramDesc &program, - mlir::FuncOp *mainFunc, - bool arg_has_map); - // register model outpus into params_map_ - void UpdateModelOutputs( - const infrt::paddle::framework_proto::ProgramDesc &program); - - // method for converting proto::op into op in paddle dialect - void buildOperation(const infrt::paddle::framework_proto::OpDesc &op_); - - llvm::SmallVector GetOpInputValue( - const infrt::paddle::framework_proto::OpDesc &op_); - llvm::SmallVector GetOpOutputType( - const infrt::paddle::framework_proto::OpDesc &op_); - llvm::SmallVector GetOpAttributes( - const infrt::paddle::framework_proto::OpDesc &op_); - void RegisterOpOutputVars(const infrt::paddle::framework_proto::OpDesc &op_, - mlir::Operation *mlir_op_); - - private: - mlir::MLIRContext *context_; - mlir::OpBuilder builder_; - mlir::ModuleOp module_; - infrt::paddle::framework_proto::BlockDesc main_block_; - - std::string model_dir_{}; - std::string model_file_{}; - std::string params_file_{}; - - std::map params_map_; -}; - -// convert protobuf repeated to std::vector. -template -inline std::vector RepeatedToVector( - const google::protobuf::RepeatedField &repeated_field) { - std::vector ret; - ret.reserve(repeated_field.size()); - std::copy( - repeated_field.begin(), repeated_field.end(), std::back_inserter(ret)); - return ret; -} -// convert proto type to mlir type -bool ConvertDataType(infrt::paddle::framework_proto::VarType::Type dtype, - mlir::Builder builder, - mlir::Type *type); -bool ConvertDataTypeToInfrt(infrt::paddle::framework_proto::VarType::Type dtype, - infrt::PrecisionType *type); - -#endif // PADDLE_INFRT_HOST_CONTEXT_PADDLE_MLIR_H_ diff --git a/paddle/infrt/host_context/paddle_mlir_converter.cc b/paddle/infrt/host_context/paddle_mlir_converter.cc deleted file mode 100644 index 0aae8cc9337..00000000000 --- a/paddle/infrt/host_context/paddle_mlir_converter.cc +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/paddle_mlir.h" - -void print_usage() { - std::cout << "Error inputs format, two kinds of inputs are supported:\n"; - std::cout << " [1] ./paddle-mlir-convert $path_to_model_file " - "$path_to_params_file\n"; - std::cout << " [2] ./paddle-mlir-convert $path_to_model_dir(__model__ + " - "params)\n"; -} - -bool parse_inputs(int argc, - char** argv, - std::string* model_file_name, - std::string* params_file_name) { - switch (argc) { - case 1: { - print_usage(); - return false; - } - case 2: { - *model_file_name = std::string(argv[1]) + std::string("/__model__"); - *params_file_name = std::string(argv[1]) + std::string("/params"); - return true; - } - case 3: { - *model_file_name = argv[1]; - *params_file_name = argv[2]; - return true; - } - default: { - return false; - } - } -} - -int main(int argc, char** argv) { - std::string model_file_name; - std::string params_file_name; - if (parse_inputs(argc, argv, &model_file_name, ¶ms_file_name)) { - MLIRModelGenImpl myGen; - auto module_ = myGen.ImportPaddleModel(model_file_name, params_file_name); - module_.dump(); - } -} diff --git a/paddle/infrt/host_context/symbol_table.cc b/paddle/infrt/host_context/symbol_table.cc deleted file mode 100644 index 318dc0cc556..00000000000 --- a/paddle/infrt/host_context/symbol_table.cc +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/symbol_table.h" - -#include - -namespace infrt { -namespace host_context { - -struct SymbolTable::Impl { - std::unordered_map data; -}; - -SymbolTable::SymbolTable() : impl_(new Impl) {} - -Value* SymbolTable::Register(const std::string& key) { - CHECK(!impl_->data.count(key)) << "Duplicate register [" << key << "]"; - auto newitem = ValueRef(new Value); - impl_->data.emplace(key, newitem); - return newitem.get(); -} - -Value* SymbolTable::Register(const std::string& key, ValueRef value) { - CHECK(!impl_->data.count(key)) << "Duplicate register [" << key << "]"; - impl_->data.emplace(key, value); - return value.get(); -} - -Value* SymbolTable::GetValue(const std::string& key) const { - auto it = impl_->data.find(std::string(key)); - return it != impl_->data.end() ? it->second.get() : nullptr; -} - -// @{ -#define REGISTER_TYPE__(T) \ - template <> \ - T SymbolTable::Get(const std::string& key) { \ - auto it = impl_->data.find(std::string(key)); \ - CHECK(it != impl_->data.end()) << "No value called " << key; \ - return it->second->get(); \ - } -REGISTER_TYPE__(int32_t); -REGISTER_TYPE__(float); -REGISTER_TYPE__(double); -REGISTER_TYPE__(int64_t); -#undef REGISTER_TYPE__ -// @} - -SymbolTable::~SymbolTable() {} - -size_t SymbolTable::size() const { return impl_->data.size(); } - -// @{ -#define REGISTER_TYPE__(T) \ - template <> \ - Value* SymbolTable::Register(const std::string& key, T&& v) { \ - CHECK(!impl_->data.count(key)) << "Duplicate register [" << key << "]"; \ - auto newitem = ValueRef(v); \ - impl_->data.emplace(key, newitem); \ - return newitem.get(); \ - } -REGISTER_TYPE__(int) -REGISTER_TYPE__(float) -REGISTER_TYPE__(double) -REGISTER_TYPE__(bool) -#undef REGISTER_TYPE__ -// @} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/symbol_table.h b/paddle/infrt/host_context/symbol_table.h deleted file mode 100644 index 8c79c78c690..00000000000 --- a/paddle/infrt/host_context/symbol_table.h +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include - -#include "paddle/infrt/host_context/value.h" - -namespace infrt { -namespace host_context { - -/** - * SymbolTable holds all the states of the kernel graph in the runtime. - */ -class SymbolTable { - public: - SymbolTable(); - - /** - * Register a state called \p key. - */ - Value* Register(const std::string& key); - - Value* Register(const std::string& key, ValueRef value); - - /** - * Register a state and set value. - */ - template - Value* Register(const std::string& key, T&& v); - - size_t size() const; - - /** - * Get a state called \p key. - */ - Value* GetValue(const std::string& key) const; - - template - T Get(const std::string& key); - - ~SymbolTable(); - - private: - class Impl; - - std::unique_ptr impl_; -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/value.cc b/paddle/infrt/host_context/value.cc deleted file mode 100644 index 1bfdc59b960..00000000000 --- a/paddle/infrt/host_context/value.cc +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/value.h" - -#include "paddle/infrt/tensor/dense_tensor_view.h" - -namespace infrt { -namespace host_context { - -ValueRef::ValueRef(int32_t val) : Shared(new Value(val)) {} -ValueRef::ValueRef(int64_t val) : Shared(new Value(val)) {} -ValueRef::ValueRef(float val) : Shared(new Value(val)) {} -ValueRef::ValueRef(double val) : Shared(new Value(val)) {} -ValueRef::ValueRef(bool val) : Shared(new Value(val)) {} - -const char* Value::type_info() const { return __type_info__; } - -void CopyTo(const Value& from, Value* to) { - CHECK(from.valid()) << "from value is not valid, can't be copied"; - CHECK(to) << "to is not valid"; - visit( - [&](auto&& arg) { - using T = std::decay_t; - if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); - else if (std::is_same>::value) - to->data = reinterpret_cast const&>(arg); - else if (std::is_same>::value) - to->data = reinterpret_cast const&>(arg); - else if (std::is_same::value) - to->data = reinterpret_cast(arg); -#ifdef INFRT_WITH_PHI - else if (std::is_same::value) - to->data = reinterpret_cast<::Tensor const&>(arg); -#endif - else - LOG(FATAL) << "Not supported Value copy: " << typeid(T).name(); - }, - from.data); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/value.h b/paddle/infrt/host_context/value.h deleted file mode 100644 index 3953a1409cf..00000000000 --- a/paddle/infrt/host_context/value.h +++ /dev/null @@ -1,257 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include - -#include -#include -#include - -#include "paddle/infrt/common/object.h" -#include "paddle/infrt/common/shared.h" -#include "paddle/infrt/dialect/infrt/common/types.h" -#include "paddle/infrt/host_context/function.h" -#include "paddle/infrt/host_context/symbol_table.h" -#include "paddle/infrt/support/variant.h" -#include "paddle/infrt/tensor/dense_host_tensor.h" -#include "paddle/infrt/tensor/dense_tensor_view.h" -#include "paddle/infrt/tensor/tensor_map.h" -#include "paddle/infrt/tensor/tensor_shape.h" - -#ifdef INFRT_WITH_PHI -#include "paddle/infrt/backends/host/phi_allocator.h" -#include "paddle/infrt/backends/host/phi_context.h" -#include "paddle/infrt/tensor/phi/tensor_map.h" -#include "paddle/phi/backends/all_context.h" -#include "paddle/phi/common/backend.h" -#include "paddle/phi/common/data_type.h" -#include "paddle/phi/common/int_array.h" -#include "paddle/phi/common/layout.h" -#include "paddle/phi/common/scalar.h" -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/meta_tensor.h" - -#ifdef INFRT_WITH_GPU -#include "paddle/phi/backends/gpu/gpu_context.h" -#endif // INFRT_WITH_GPU -#ifdef INFRT_WITH_TRT -#include "paddle/infrt/backends/tensorrt/trt_engine.h" -#include "paddle/infrt/kernel/tensorrt/trt_kernels.h" -#endif // INFRT_WITH_TRT -#endif // INFRT_WITH_PHI - -namespace infrt { -namespace host_context { - -struct None {}; - -struct MlirFunctionExecutable; - -using ValueVariantType = - Variant, - std::vector<::Tensor*>, - paddle::experimental::ScalarBase<::Tensor>, - paddle::experimental::IntArrayBase<::Tensor>, - std::vector, - std::vector<::phi::MetaTensor*>, - ::phi::MetaConfig, - paddle::experimental::Backend, - phi::DataLayout, - phi::DataType, - ::infrt::phi::DenseTensorMap, -#endif // INFRT_WITH_PHI -#ifdef INFRT_WITH_TRT - ::infrt::backends::tensorrt::TrtEngine, - ::infrt::kernel::tensorrt::MlirOperationWithInfrtSymbol, -#endif // INFRT_WITH_TRT - std::vector, - std::vector, - std::vector, - std::vector, - std::vector>; - -//! Copy content from \param from to \param to. -void CopyTo(const Value& from, Value* to); - -/** - * Represents any data type for value in host context. - */ -class Value : public common::Object { - public: - using variant_type = ValueVariantType; - - explicit Value() {} // NOLINT - explicit Value(int32_t x) : data(x) {} - explicit Value(int64_t x) : data(x) {} - explicit Value(float x) : data(x) {} - explicit Value(double x) : data(x) {} - explicit Value(bool x) : data(x) {} - explicit Value(::infrt::TargetType x) : data(x) {} - explicit Value(::infrt::LayoutType x) : data(x) {} - explicit Value(::infrt::PrecisionType x) : data(x) {} - explicit Value(std::string x) : data(x) {} - explicit Value(tensor::TensorMap&& x) : data(x) {} - explicit Value(std::vector&& x) : data(x) {} - explicit Value(std::vector&& x) : data(x) {} - explicit Value(std::vector&& x) : data(x) {} - explicit Value(std::vector&& x) : data(x) {} - explicit Value(std::vector&& x) : data(x) {} - explicit Value(tensor::TensorShape&& x) : data(std::move(x)) {} - explicit Value(tensor::DenseHostTensor&& x) : data(std::move(x)) {} - explicit Value(MlirFunctionExecutable* x) : data(x) {} -#ifdef INFRT_WITH_PHI - explicit Value(::infrt::phi::DenseTensorMap&& x) : data(std::move(x)) {} - explicit Value(::phi::CPUContext&& x) : data(std::move(x)) {} - explicit Value(backends::CpuPhiContext&& x) : data(std::move(x)) {} -#ifdef INFRT_WITH_GPU - explicit Value(::phi::GPUContext&& x) : data(std::move(x)) {} - explicit Value(backends::GpuPhiContext&& x) : data(std::move(x)) {} -#endif - explicit Value(::Tensor&& x) : data(std::move(x)) {} - explicit Value(::phi::MetaTensor&& x) : data(std::move(x)) {} - explicit Value(::phi::MetaConfig&& x) : data(std::move(x)) {} -#ifdef INFRT_WITH_TRT - explicit Value(::infrt::backends::tensorrt::TrtEngine&& x) - : data(std::move(x)) {} - explicit Value(::infrt::kernel::tensorrt::MlirOperationWithInfrtSymbol x) - : data(x) {} -#endif // INFRT_WITH_TRT -#endif - - template - const T& get() const { - CHECK(data.template is()) - << "typeid: " << data.index() << " != " << ValueVariantType::IndexOf; - return data.get(); - } - - template - T& get() { - CHECK(data.template is()) - << "typeid: " << data.index() << " != " << ValueVariantType::IndexOf; - return data.get(); - } - - //! Get the value if assigned before or return a default value instead. - template - T& get_or_default() { - if (!data.template is()) { - this->set(T{}); - } - return get(); - } - - template - void set(T&& v) { - data = std::move(v); - } - - void set(Value* v) { data = std::move(v->data); } - - bool valid() const { return true; } - - template - bool is_type() const { - return data.template is(); - } - - const char* type_info() const override; - - ValueVariantType::IndexT index() const { return data.index(); } - - friend void CopyTo(const Value& from, Value* to); - - private: - ValueVariantType data; - static constexpr const char* __type_info__ = "host_context_value"; -}; - -/** - * Represents a counted reference of a Value. - */ -class ValueRef : common::Shared { - public: - ValueRef() = default; - explicit ValueRef(Value* n) : common::Shared(n) {} - explicit ValueRef(int32_t val); - explicit ValueRef(int64_t val); - explicit ValueRef(float val); - explicit ValueRef(double val); - explicit ValueRef(bool val); - - using common::Shared::get; - using common::Shared::Reset; - using common::Shared::operator->; - using common::Shared::operator*; - - //! Get a readonly data. - template - const T& get() const { - CHECK(p_); - return p_->get(); - } - - template - T& get() { - CHECK(p_); - return p_->get(); - } - - //! Assign a data. - template - void Assign(const T& x) { - if (!p_) { - p_ = common::make_shared(); - } - *p_ = x; - } - - template - void Assign(Args... args) { - p_ = common::make_shared(std::forward(args)...); - } - - inline bool IsValid() { return p_; } -}; - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/host_context/value_test.cc b/paddle/infrt/host_context/value_test.cc deleted file mode 100644 index 5ac9b60a22b..00000000000 --- a/paddle/infrt/host_context/value_test.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/host_context/value.h" - -#include - -namespace infrt { -namespace host_context { - -TEST(ValueRef, test) { - ValueRef x(12); - ASSERT_EQ(x.get(), 12); - - ValueRef y(1.2f); - ASSERT_EQ(y.get(), 1.2f); - - ValueRef z(true); - ASSERT_EQ(z.get(), true); -} - -// If the value is not assign, the get_or_default should return a default value. -TEST(Value, init) { - Value x; - ASSERT_EQ(x.get_or_default(), 0); - - Value tensor; - auto& t = tensor.get_or_default(); - ASSERT_EQ(t.shape().GetRank(), 0); -} - -} // namespace host_context -} // namespace infrt diff --git a/paddle/infrt/kernel/CMakeLists.txt b/paddle/infrt/kernel/CMakeLists.txt deleted file mode 100644 index 6a18047885d..00000000000 --- a/paddle/infrt/kernel/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -add_subdirectory(phi) -add_subdirectory(tensorrt) - -core_gather_headers() - -gather_srcs( - infrt_src - SRCS - basic_kernels.cc - # phi_kernels.cc - test_kernels.cc - tensor_shape_kernels.cc - tensor_kernels.cc - control_flow_kernels.cc) diff --git a/paddle/infrt/kernel/basic_kernels.cc b/paddle/infrt/kernel/basic_kernels.cc deleted file mode 100644 index b186cfcfd2b..00000000000 --- a/paddle/infrt/kernel/basic_kernels.cc +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/basic_kernels.h" - -#include -#include - -#include "llvm/Support/raw_ostream.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" - -using infrt::host_context::Attribute; - -namespace infrt { -namespace kernel { - -template -T add(T a, T b) { - return a + b; -} - -template -T sub(T a, T b) { - return a - b; -} - -template -T mul(T a, T b) { - return a * b; -} - -template -T div(T a, T b) { - return a / b; -} - -template -void print(T a) { - std::cout << a << std::endl; -} - -static std::string GetString(Attribute value) { - return value.get(); -} - -static void PrintString(const std::string &str) { - llvm::outs() << "string = " << str << '\n'; - llvm::outs().flush(); -} - -void RegisterBasicKernels(host_context::KernelRegistry *registry) { - RegisterIntBasicKernels(registry); - RegisterFloatBasicKernels(registry); - registry->AddKernel("infrt.get_string", INFRT_KERNEL(GetString)); - registry->AddKernel("infrt.print_string", INFRT_KERNEL(PrintString)); -} - -void RegisterIntBasicKernels(host_context::KernelRegistry *registry) { - registry->AddKernel("infrt.add.i32", INFRT_KERNEL(add)); - registry->AddKernel("infrt.sub.i32", INFRT_KERNEL(sub)); - registry->AddKernel("infrt.mul.i32", INFRT_KERNEL(mul)); - registry->AddKernel("infrt.div.i32", INFRT_KERNEL(div)); - registry->AddKernel("infrt.print.i32", INFRT_KERNEL(print)); -} - -void RegisterFloatBasicKernels(host_context::KernelRegistry *registry) { - registry->AddKernel("infrt.add.f32", INFRT_KERNEL(add)); - registry->AddKernel("infrt.sub.f32", INFRT_KERNEL(sub)); - registry->AddKernel("infrt.mul.f32", INFRT_KERNEL(mul)); - registry->AddKernel("infrt.div.f32", INFRT_KERNEL(div)); - registry->AddKernel("infrt.print.f32", INFRT_KERNEL(print)); -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/basic_kernels.h b/paddle/infrt/kernel/basic_kernels.h deleted file mode 100644 index feb66be61f5..00000000000 --- a/paddle/infrt/kernel/basic_kernels.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -namespace host_context { - -struct KernelRegistry; - -} // namespace host_context -} // namespace infrt - -namespace infrt { -namespace kernel { - -/** - * Register all the basic kernels to \p registry. - */ -void RegisterBasicKernels(host_context::KernelRegistry* registry); - -void RegisterIntBasicKernels(host_context::KernelRegistry* registry); -void RegisterFloatBasicKernels(host_context::KernelRegistry* registry); - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/control_flow_kernels.cc b/paddle/infrt/kernel/control_flow_kernels.cc deleted file mode 100644 index 6cc94dbcce0..00000000000 --- a/paddle/infrt/kernel/control_flow_kernels.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/control_flow_kernels.h" - -#include - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/mlir_function_executable.h" - -namespace infrt { -namespace kernel { - -static void INFRTCall( - host_context::RemainingArguments args, - host_context::RemainingResults results, - host_context::Attribute fn) { - VLOG(3) << "running call kernel ..."; - CHECK_EQ(fn.get()->num_arguments(), args.size()); - CHECK_EQ(fn.get()->num_results(), results.size()); - - for (auto& v : results.values()) { - CHECK(v.get()); - } - fn.get()->Execute(args.values(), results.values()); -} - -void RegisterControlFlowKernels(host_context::KernelRegistry* registry) { - registry->AddKernel("infrt.call", INFRT_KERNEL(INFRTCall)); -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/control_flow_kernels.h b/paddle/infrt/kernel/control_flow_kernels.h deleted file mode 100644 index 5fa6b985f0b..00000000000 --- a/paddle/infrt/kernel/control_flow_kernels.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "paddle/infrt/host_context/function.h" -#include "paddle/infrt/host_context/kernel_utils.h" - -namespace infrt { - -namespace host_context { -struct KernelRegistry; -} // namespace host_context - -namespace kernel { - -void RegisterControlFlowKernels(host_context::KernelRegistry* registry); - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/CMakeLists.txt b/paddle/infrt/kernel/phi/CMakeLists.txt deleted file mode 100644 index 92e4a49cd84..00000000000 --- a/paddle/infrt/kernel/phi/CMakeLists.txt +++ /dev/null @@ -1,39 +0,0 @@ -if(NOT INFRT_WITH_PHI) - return() -endif() - -core_gather_headers() - -gather_srcs(infrt_src SRCS registry.cc dense_tensor_kernels.cc - context_kernels.cc) - -set(infrt_register_phi_kernels_gen_source_file - ${CMAKE_SOURCE_DIR}/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.cc -) -set(infrt_register_phi_kernels_gen_file - ${CMAKE_SOURCE_DIR}/tools/infrt/get_phi_kernel_function.sh) -set(wrapped_infermeta_header_file - ${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.h) -set(wrapped_infermeta_source_file - ${CMAKE_SOURCE_DIR}/paddle/phi/infermeta/generated.cc) - -add_custom_command( - OUTPUT ${infrt_register_phi_kernels_gen_source_file} - COMMAND bash ${infrt_register_phi_kernels_gen_file} - DEPENDS wrapped_infermeta - VERBATIM) -add_custom_target( - infrt_register_phi_kernel - COMMAND bash ${infrt_register_phi_kernels_gen_file} - DEPENDS wrapped_infermeta - COMMENT "infrt generate ${infrt_register_phi_kernels_gen_source_file}" - VERBATIM) - -cc_library( - infrt_naive - SRCS infershaped/infershaped_kernel_launcher.cc - infershaped/infershaped_kernel_launchers.cc - DEPS phi wrapped_infermeta) - -cc_test_tiny(test_infrt_infershape_launchers SRCS - infershaped/infershape_launchers_test.cc DEPS infrt) diff --git a/paddle/infrt/kernel/phi/context_kernels.cc b/paddle/infrt/kernel/phi/context_kernels.cc deleted file mode 100644 index 9c5ab13d17b..00000000000 --- a/paddle/infrt/kernel/phi/context_kernels.cc +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/phi/context_kernels.h" - -namespace infrt { -namespace kernel { -namespace phi { - -::phi::CPUContext CreateCPUContext() { - ::phi::CPUContext ctx{}; - auto allocator = new backends::CpuPhiAllocator{}; - ctx.SetAllocator(allocator); - ctx.SetHostAllocator(allocator); - ctx.SetZeroAllocator(allocator); - return ctx; -} - -#ifdef INFRT_WITH_GPU -::phi::GPUContext CreateGPUContext() { - ::phi::GPUContext context; - context.PartialInitWithoutAllocator(); - context.SetAllocator(new ::infrt::backends::GpuPhiAllocator{}); - context.SetHostAllocator(new backends::CpuPhiAllocator{}); - context.PartialInitWithAllocator(); - return context; -} -#endif - -} // namespace phi -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/context_kernels.h b/paddle/infrt/kernel/phi/context_kernels.h deleted file mode 100644 index ae3f76c8fe5..00000000000 --- a/paddle/infrt/kernel/phi/context_kernels.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "paddle/infrt/backends/host/phi_allocator.h" -#include "paddle/infrt/backends/host/phi_context.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace kernel { -namespace phi { - -::phi::CPUContext CreateCPUContext(); - -#ifdef INFRT_WITH_GPU -::phi::GPUContext CreateGPUContext(); -#endif - -} // namespace phi -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/dense_tensor_kernels.cc b/paddle/infrt/kernel/phi/dense_tensor_kernels.cc deleted file mode 100644 index 645df691710..00000000000 --- a/paddle/infrt/kernel/phi/dense_tensor_kernels.cc +++ /dev/null @@ -1,395 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/phi/dense_tensor_kernels.h" - -#include - -#include "llvm/Support/ErrorHandling.h" -#include "paddle/infrt/backends/host/phi_allocator.h" -#include "paddle/infrt/common/string.h" -#include "paddle/infrt/dialect/phi/data_type.h" -#include "paddle/infrt/kernel/phi/context_kernels.h" -#include "paddle/infrt/paddle/model_parser.h" -#include "paddle/infrt/paddle/scope.h" -#include "paddle/infrt/tensor/tensor_map.h" -#include "paddle/phi/backends/all_context.h" -#include "paddle/phi/common/place.h" -#include "paddle/phi/core/allocator.h" -#include "paddle/phi/core/dense_tensor.h" - -#ifdef INFRT_WITH_GPU -#include -#endif - -namespace infrt { -namespace kernel { -namespace phi { - -::Tensor CreateDenseTensor( - const ::phi::CPUContext& context, - host_context::Attribute> dims, - host_context::Attribute> lod, - host_context::Attribute<::infrt::LayoutType> layout, - host_context::Attribute<::infrt::PrecisionType> precision) { - return ::Tensor(const_cast<::phi::Allocator*>(&context.GetAllocator()), - ::phi::DenseTensorMeta(ConvertPrecisionToPhi(precision.get()), - ::phi::make_ddim(dims.get()), - ConvertLayoutToPhi(layout.get()), - {})); -} - -::Tensor CreateInitedDenseTensorF32( - const ::phi::CPUContext& context, - host_context::Attribute> dims, - host_context::Attribute> lod, - host_context::Attribute<::infrt::LayoutType> layout, - host_context::Attribute value) { - ::Tensor dense_tensor( - const_cast<::phi::Allocator*>(&context.GetAllocator()), - ::phi::DenseTensorMeta( - ConvertPrecisionToPhi(::infrt::PrecisionType::FLOAT32), - ::phi::make_ddim(dims.get()), - ConvertLayoutToPhi(layout.get()), - {})); - float* a_data = dense_tensor.mutable_data(context.GetPlace()); - for (int64_t i = 0; i < dense_tensor.numel(); ++i) { - a_data[i] = value.get(); - } - return dense_tensor; -} - -::Tensor CreateHostInitedDenseTensorF32( - const ::phi::CPUContext& context, - host_context::Attribute> dims, - host_context::Attribute> lod, - host_context::Attribute<::infrt::LayoutType> layout, - host_context::Attribute> values) { - ::Tensor dense_tensor( - const_cast<::phi::Allocator*>(&context.GetAllocator()), - ::phi::DenseTensorMeta( - ConvertPrecisionToPhi(::infrt::PrecisionType::FLOAT32), - ::phi::make_ddim(dims.get()), - ConvertLayoutToPhi(layout.get()), - {})); - CHECK_EQ(dense_tensor.numel(), static_cast(values.get().size())); - float* data = dense_tensor.mutable_data(::phi::CPUPlace()); - for (int64_t i = 0; i < dense_tensor.numel(); ++i) { - data[i] = values.get()[i]; - } - return dense_tensor; -} - -::Tensor CreateGPUDenseTensor( - const ::phi::GPUContext& context, - host_context::Attribute> dims, - host_context::Attribute> lod, - host_context::Attribute<::infrt::LayoutType> layout, - host_context::Attribute<::infrt::PrecisionType> precision) { - return ::Tensor(const_cast<::phi::Allocator*>(&context.GetAllocator()), - ::phi::DenseTensorMeta(ConvertPrecisionToPhi(precision.get()), - ::phi::make_ddim(dims.get()), - ConvertLayoutToPhi(layout.get()), - {})); -} - -void FillDenseTensorF32(::Tensor* dense_tensor, - host_context::Attribute> value) { - auto place = dense_tensor->place(); - float* a_data = dense_tensor->mutable_data(place); - if (place.GetType() == ::phi::AllocationType::CPU) { - for (int64_t i = 0; i < dense_tensor->numel(); ++i) { - a_data[i] = (value.get())[i]; - } - } else if (place.GetType() == ::phi::AllocationType::GPU) { -#ifdef INFRT_WITH_GPU - // TODO(wilber): how to set the stream parameter to copy with stream. - cudaMemcpy(a_data, - value.get().data(), - sizeof(float) * value.get().size(), - cudaMemcpyHostToDevice); -#endif - } else { - llvm_unreachable("temporarily not support other target."); - } -} - -void PrintDenseTensor(::Tensor* dense_tensor) { -#ifndef INFRT_WITH_GPU -#define PRINT_META_DATA(PHI_DATATYPE, DTYPE) \ - case ::phi::DataType::PHI_DATATYPE: { \ - auto place = dense_tensor->place(); \ - if (place.GetType() == ::phi::AllocationType::CPU) { \ - DTYPE* data = dense_tensor->data(); \ - if (dense_tensor->numel() == 0) break; \ - std::cout << data[0]; \ - for (int64_t i = 1; i < dense_tensor->numel(); i++) { \ - std::cout << "," << data[i]; \ - } \ - } \ - break; \ - } -#else -#define PRINT_META_DATA(PHI_DATATYPE, DTYPE) \ - case ::phi::DataType::PHI_DATATYPE: { \ - auto place = dense_tensor->place(); \ - DTYPE* data = dense_tensor->data(); \ - if (dense_tensor->numel() == 0) break; \ - if (place.GetType() == ::phi::AllocationType::CPU) { \ - std::cout << data[0]; \ - for (int64_t i = 1; i < dense_tensor->numel(); i++) { \ - std::cout << "," << data[i]; \ - } \ - } else if (place.GetType() == ::phi::AllocationType::GPU) { \ - std::vector host_data(dense_tensor->numel(), 0); \ - cudaMemcpy(host_data.data(), \ - data, \ - sizeof(DTYPE) * dense_tensor->numel(), \ - cudaMemcpyDeviceToHost); \ - std::cout << host_data[0]; \ - for (int64_t i = 1; i < dense_tensor->numel(); i++) { \ - std::cout << "," << host_data[i]; \ - } \ - } else { \ - llvm_unreachable("temporarily not support other target."); \ - } \ - break; \ - } -#endif - - ::phi::DDim dims = dense_tensor->dims(); - std::cout << "dense_tensor: shape=shape" << dims.to_str() << "," - << " value=["; - switch (dense_tensor->dtype()) { - PRINT_META_DATA(FLOAT32, float); - PRINT_META_DATA(INT32, int32_t); - default: - std::cout << "Error! Unsupported data type!\n"; - } - std::cout << "]\n"; -#undef PRINT_META_DATA -} - -::infrt::phi::DenseTensorMap LoadParameters(const std::string& file_path) { - std::cout << "loading params from: " << file_path << std::endl; - ::infrt::phi::DenseTensorMap map; - - const std::string model_path = file_path + "/__model__"; - auto pb_proto_prog = paddle::LoadProgram(model_path); - auto main_block = pb_proto_prog->blocks(0); - - ::phi::CPUContext ctx; - auto allocator = std::make_unique(); - const auto* allocator_ptr = allocator.get(); - ctx.SetAllocator(allocator_ptr); - ctx.SetHostAllocator(allocator_ptr); - ctx.SetZeroAllocator(allocator_ptr); - for (auto& var : main_block.vars()) { - if (var.name() == "feed" || var.name() == "fetch" || !var.persistable()) - continue; - std::string param_path = file_path + "/" + var.name(); - std::ifstream param_file(param_path, std::ios::binary); - switch (var.type().type()) { - case ::paddle::framework::proto::VarType_Type_LOD_TENSOR: { - std::unique_ptr<::Tensor> tensor{std::make_unique<::Tensor>()}; - ::infrt::paddle::DeserializeFromStream(param_file, tensor.get(), ctx); - map.SetDenseTensor(var.name(), std::move(tensor)); - } break; - default: { - LOG(WARNING) << "Var `" << var.name() << "` type `" - << static_cast(var.type().type()) - << "` has not been supported now."; - } - } - } - return map; -} - -::infrt::phi::DenseTensorMap LoadParams( - host_context::Attribute path) { - return LoadParameters(path.get()); -} - -::infrt::phi::DenseTensorMap LoadCombinedParameters( - const std::string& model_path, const std::string& params_path) { - ::infrt::phi::DenseTensorMap map; - - auto pb_proto_prog = paddle::LoadProgram(model_path); - auto main_block = pb_proto_prog->blocks(0); - - std::ifstream param_file(params_path, std::ios::binary); - - std::set tmp; - for (auto& var : main_block.vars()) { - if (var.name() == "feed" || var.name() == "fetch" || !var.persistable()) { - continue; - } - if (var.type().type() == - ::paddle::framework::proto::VarType_Type_LOD_TENSOR) { - tmp.emplace(var.name()); - } else { - llvm_unreachable("the tensor type is illegal."); - } - } - - ::phi::CPUContext ctx; - auto allocator = std::make_unique(); - const auto* allocator_ptr = allocator.get(); - ctx.SetAllocator(allocator_ptr); - ctx.SetHostAllocator(allocator_ptr); - ctx.SetZeroAllocator(allocator_ptr); - for (auto& var : tmp) { - std::unique_ptr<::Tensor> tensor{std::make_unique<::Tensor>()}; - ::infrt::paddle::DeserializeFromStream(param_file, tensor.get(), ctx); - map.SetDenseTensor(var, std::move(tensor)); - } - - return map; -} - -::infrt::phi::DenseTensorMap LoadCombinedParamsToGpu( - const std::string& model_path, const std::string& params_path) { - ::infrt::phi::DenseTensorMap map; - - auto pb_proto_prog = paddle::LoadProgram(model_path); - auto main_block = pb_proto_prog->blocks(0); - - std::ifstream param_file(params_path, std::ios::binary); - - std::set tmp; - for (auto& var : main_block.vars()) { - if (var.name() == "feed" || var.name() == "fetch" || !var.persistable()) { - continue; - } - if (var.type().type() == - ::paddle::framework::proto::VarType_Type_LOD_TENSOR) { - tmp.emplace(var.name()); - } else { - llvm_unreachable("the tensor type is illegal."); - } - } - -#ifdef INFRT_WITH_GPU - ::phi::GPUContext ctx; - ctx.PartialInitWithoutAllocator(); - - for (auto& var : tmp) { - std::unique_ptr<::Tensor> tensor{std::make_unique<::Tensor>()}; - ::paddle::framework::DeserializeFromStream(param_file, tensor.get(), ctx); - map.SetDenseTensor(var, std::move(tensor)); - } -#endif - - return map; -} - -::infrt::phi::DenseTensorMap LoadCombinedParams( - host_context::Attribute model_path, - host_context::Attribute params_path) { - return LoadCombinedParameters(model_path.get(), params_path.get()); -} - -::Tensor TensorMapGetTensor(const ::infrt::phi::DenseTensorMap& map, - host_context::Attribute name) { - auto* tensor = map.GetDenseTensor(name.get()); - CHECK(tensor); - return *tensor; -} - -int32_t TensorMapGetSize(const ::infrt::phi::DenseTensorMap& map) { - return map.size(); -} - -#ifdef INFRT_WITH_GPU -inline size_t SizeOfDataType(::phi::DataType data_type) { - switch (data_type) { - case ::phi::DataType::BOOL: - case ::phi::DataType::UINT8: - case ::phi::DataType::INT8: - return 1; - case ::phi::DataType::BFLOAT16: - case ::phi::DataType::FLOAT16: - case ::phi::DataType::INT16: - case ::phi::DataType::UINT16: - return 2; - case ::phi::DataType::FLOAT32: - case ::phi::DataType::INT32: - case ::phi::DataType::UINT32: - return 4; - case ::phi::DataType::FLOAT64: - case ::phi::DataType::INT64: - case ::phi::DataType::UINT64: - case ::phi::DataType::COMPLEX64: - return 8; - case ::phi::DataType::COMPLEX128: - return 16; - case ::phi::DataType::UNDEFINED: - return 0; - default: - llvm_unreachable("should not reach here"); - return 0; - } - return 0; -} -void GpuMemCpy(const ::Tensor& input, - const ::phi::GPUContext& context, - bool d2h, - ::Tensor* output) { - if (d2h) { - CHECK(input.place().GetType() == ::phi::AllocationType::GPU); - - // TODO(wilber): Just a trick to avoid malloc. - if (input.numel() > output->numel()) { - // TODO(wilber): Use pinned memory. - output->Resize(input.dims()); - context.HostAlloc( - output, input.dtype(), input.numel() * SizeOfDataType(input.dtype())); - } - - cudaMemcpyAsync(output->data(), - input.data(), - SizeOfDataType(input.dtype()) * input.numel(), - cudaMemcpyDeviceToHost, - context.stream()); - // TODO(wilber): Ir add sync op. - cudaStreamSynchronize(context.stream()); - } else { - // h2d - CHECK(input.place().GetType() == ::phi::AllocationType::CPU || - input.place().GetType() == ::phi::AllocationType::GPUPINNED); - - if (input.numel() > output->numel()) { - output->Resize(input.dims()); - context.Alloc(output, - input.dtype(), - input.numel() * SizeOfDataType(input.dtype()), - false); - - } else { - output->Resize(input.dims()); - } - - // TODO(wilber): Add sync op and stream. - cudaMemcpyAsync(output->data(), - input.data(), - SizeOfDataType(input.dtype()) * input.numel(), - cudaMemcpyHostToDevice, - context.stream()); - } -} -#endif - -} // namespace phi -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/dense_tensor_kernels.h b/paddle/infrt/kernel/phi/dense_tensor_kernels.h deleted file mode 100644 index 4a41ccdcfa2..00000000000 --- a/paddle/infrt/kernel/phi/dense_tensor_kernels.h +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "paddle/infrt/backends/host/phi_allocator.h" -#include "paddle/infrt/dialect/infrt/common/types.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/tensor/phi/tensor_map.h" -#include "paddle/phi/backends/gpu/gpu_context.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace kernel { -namespace phi { - -::Tensor CreateDenseTensor( - const ::phi::CPUContext& context, - host_context::Attribute> dims, - host_context::Attribute> lod, - host_context::Attribute<::infrt::LayoutType> layout, - host_context::Attribute<::infrt::PrecisionType> precision); - -::Tensor CreateInitedDenseTensorF32( - const ::phi::CPUContext& context, - host_context::Attribute> dims, - host_context::Attribute> lod, - host_context::Attribute<::infrt::LayoutType> layout, - host_context::Attribute value); - -::Tensor CreateHostInitedDenseTensorF32( - const ::phi::CPUContext& context, - host_context::Attribute> dims, - host_context::Attribute> lod, - host_context::Attribute<::infrt::LayoutType> layout, - host_context::Attribute> values); - -::Tensor CreateGPUDenseTensor( - const ::phi::GPUContext& context, - host_context::Attribute> dims, - host_context::Attribute> lod, - host_context::Attribute<::infrt::LayoutType> layout, - host_context::Attribute<::infrt::PrecisionType> precision); - -void FillDenseTensorF32(::Tensor* dense_tensor, - host_context::Attribute> values); -void PrintDenseTensor(::Tensor* dense_tensor); - -::infrt::phi::DenseTensorMap LoadParameters(const std::string& path); - -::infrt::phi::DenseTensorMap LoadParams( - host_context::Attribute path); - -::Tensor TensorMapGetTensor(const ::infrt::phi::DenseTensorMap& map, - host_context::Attribute name); - -::infrt::phi::DenseTensorMap LoadCombinedParams( - host_context::Attribute model_path, - host_context::Attribute params_path); - -::infrt::phi::DenseTensorMap LoadCombinedParameters( - const std::string& model_path, const std::string& params_path); - -::infrt::phi::DenseTensorMap LoadCombinedParamsToGpu( - const std::string& model_path, const std::string& params_path); - -int32_t TensorMapGetSize(const ::infrt::phi::DenseTensorMap& map); - -#ifdef INFRT_WITH_GPU -void GpuMemCpy(const ::Tensor& input, - const ::phi::GPUContext& context, - bool d2h, - ::Tensor* output); -#endif - -} // namespace phi -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/infershaped/infershape_launchers_test.cc b/paddle/infrt/kernel/phi/infershaped/infershape_launchers_test.cc deleted file mode 100644 index c37569f8b4c..00000000000 --- a/paddle/infrt/kernel/phi/infershaped/infershape_launchers_test.cc +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include - -#include "paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.h" -#include "paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h" -#include "paddle/infrt/kernel/phi/infershaped/infershaped_utils.h" -#include "paddle/phi/backends/cpu/cpu_context.h" -#include "paddle/phi/common/place.h" -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/meta_tensor.h" - -namespace infrt { -namespace kernel { - -namespace { -static void ElementwiseAddTest(const ::Tensor& a, - const ::Tensor& b, - ::Tensor* c); -} - -TEST(utils, registry) { - constexpr uint8_t count = - InferShapeHelper::count; - CHECK_EQ(count, 2U); -} - -class FancyAllocator : public ::phi::Allocator { - public: - static void Delete(::phi::Allocation* allocation) { - ::operator delete(allocation->ptr()); - } - - AllocationPtr Allocate(size_t bytes_size) override { - void* data = ::operator new(bytes_size); - auto* allocation = - new ::phi::Allocation(data, bytes_size, ::phi::CPUPlace()); - return AllocationPtr(allocation, Delete); - } -}; - -TEST(ElementwiseAdd, launcher_registry) { - host_context::KernelRegistry registry; - RegisterInferShapeLaunchers(®istry); - ASSERT_GE(registry.size(), 1UL); - auto creator = registry.GetKernel("phi_cpu.add.float32.any"); - - const ::phi::DDim dims({1, 2}); - const ::phi::DataType dtype{::phi::DataType::FLOAT32}; - const ::phi::DataLayout layout{::phi::DataLayout::NHWC}; - const ::phi::LoD lod{}; - ::phi::DenseTensorMeta meta(dtype, dims, layout, lod); - - auto fancy_allocator = std::unique_ptr<::phi::Allocator>(new FancyAllocator); - auto* alloc = fancy_allocator.get(); - - ::Tensor a(alloc, meta); - ::Tensor b(alloc, meta); - ::Tensor c(alloc, meta); - - auto place = ::phi::CPUPlace(); - float* a_data = a.mutable_data(place); - float* b_data = b.mutable_data(place); - float* c_data = c.mutable_data(place); - for (size_t i = 0; i < 2; ++i) { - a_data[i] = 1.f; - b_data[i] = 2.f; - } - - ::phi::CPUContext context; - context.SetAllocator(alloc); - - host_context::KernelFrameBuilder kernel_frame_builder; - kernel_frame_builder.AddArgument(new host_context::Value(std::move(context))); - kernel_frame_builder.AddArgument(new host_context::Value(std::move(a))); - kernel_frame_builder.AddArgument(new host_context::Value(std::move(b))); - kernel_frame_builder.SetResults({new host_context::Value(std::move(c))}); - - creator(&kernel_frame_builder); - - for (size_t i = 0; i < 2; ++i) { - CHECK_EQ(c_data[i], 3.f); - } -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.cc b/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.cc deleted file mode 100644 index 6ee0bc20f99..00000000000 --- a/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.cc +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.h" - -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/meta_tensor.h" - -namespace infrt { -namespace kernel { - -void InferShapedKernelLauncher::CreateKernelFrameForInferShape( - host_context::KernelFrame* frame) { - for (host_context::Value* value : - frame->GetValues(1, frame->GetNumElements() - 1)) { - // TODO(Superjomn) To extend this. - if (value->is_type<::Tensor>()) { - values.emplace_back( - new host_context::Value{::phi::MetaTensor{&value->get<::Tensor>()}}); - infershape_kernel_frame_builder.AddArgument(values.back().get()); - } else { - infershape_kernel_frame_builder.AddArgument(value); - } - } - if (infershape_kernel_frame_builder.GetNumArgs() < arg_size_) { - infershape_kernel_frame_builder.AddArgument( - new host_context::Value(::phi::MetaConfig())); - } -} - -void InferShapedKernelLauncher::BuildInferShapeCache( - const uint16_t num_inputs) { - tensor_shape_cache.resize(num_inputs); - for (uint16_t i = 0; i < num_inputs; i++) { - tensor_shape_cache[i] = infershape_kernel_frame_builder.GetArgAt(i) - ->get<::phi::MetaTensor>() - .dims(); - } -} - -bool InferShapedKernelLauncher::IsShapeChanged( - const uint16_t num_inputs) const { - if (tensor_shape_cache.empty() && !infershape_kernel_frame_builder.IsEmpty()) - return true; - - bool changed = false; - for (uint16_t i = 0; i < num_inputs && !changed; i++) { - changed = - changed || - (tensor_shape_cache[i] != - infershape_kernel_frame_builder.GetArgAt<::phi::MetaTensor>(i).dims()); - } - return changed; -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.h b/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.h deleted file mode 100644 index 77007811532..00000000000 --- a/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -#include "paddle/infrt/host_context/kernel_frame.h" -#include "paddle/infrt/host_context/value.h" - -namespace infrt { -namespace kernel { - -struct InferShapedKernelLauncher { - explicit InferShapedKernelLauncher(int arg_size) : arg_size_(arg_size) {} - ~InferShapedKernelLauncher() = default; - //! Initialize the kernel frame for InferShape kernel. - // This method will create a new KernelFrame with all the Tensors(currently - // only DenseHostTensor) converted into MetaTensors so that the infer-shape - // function can work with. - // @frame: the frame containing argument list that is same with the ones of - // the corresponding kernel. - void CreateKernelFrameForInferShape(host_context::KernelFrame* frame); - - //! Build or update the infer-shape cache using the latest shape from - //! InferShapeFrame. - void BuildInferShapeCache(const uint16_t num_inputs); - - //! Compare the latest shape with the shape cache. - bool IsShapeChanged(const uint16_t num_inputs) const; - - // values to hold the TensorMeta. - llvm::SmallVector values; - llvm::SmallVector<::phi::DDim, 3> tensor_shape_cache; - host_context::KernelFrameBuilder infershape_kernel_frame_builder; - const int arg_size_; -}; - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h b/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h deleted file mode 100644 index ba25f06876c..00000000000 --- a/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "paddle/infrt/host_context/kernel_registry.h" - -namespace infrt { -namespace kernel { - -void RegisterInferShapeLaunchers(host_context::KernelRegistry* registry); - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/infershaped/infershaped_utils.h b/paddle/infrt/kernel/phi/infershaped/infershaped_utils.h deleted file mode 100644 index 999369c5826..00000000000 --- a/paddle/infrt/kernel/phi/infershaped/infershaped_utils.h +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include - -#include "paddle/infrt/tensor/dense_host_tensor.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace kernel { -namespace infershaped { - -using KeyType = const ::Tensor&; -using CountType = uint8_t; - -constexpr CountType value(std::true_type) { return 1; } - -constexpr CountType value(std::false_type) { return 0; } - -template -constexpr CountType value() { - return value(std::integral_constant::value>{}); -} - -template -constexpr CountType count(CountType num) { - return num; -} - -template -constexpr CountType count() { - return 0; -} - -template <> -constexpr CountType count(CountType num) { - return num + 1; -} - -template <> -constexpr CountType count() { - return 1; -} - -template -constexpr CountType count(CountType num) { - return count(num + value()); -} - -template -constexpr CountType count() { - return count(value()); -} - -} // namespace infershaped - -template -struct InferShapeHelper; - -template -struct InferShapeHelper { - static constexpr int count = infershaped::count(); -}; - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/infershaped/phi_kernel_launcher.h b/paddle/infrt/kernel/phi/infershaped/phi_kernel_launcher.h deleted file mode 100644 index bac25e0f437..00000000000 --- a/paddle/infrt/kernel/phi/infershaped/phi_kernel_launcher.h +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#pragma once - -#include - -#include - -#include "paddle/infrt/backends/host/phi_context.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.h" -#include "paddle/infrt/kernel/phi/infershaped/infershaped_utils.h" - -namespace infrt { -namespace kernel { - -template -struct FuncArgStatics {}; - -template -struct FuncArgStatics { - constexpr static int arg_size = sizeof...(Args); -}; - -template -::infrt::host_context::KernelImplementation KernelLauncherFunc() { - InferShapedKernelLauncher launcher(FuncArgStatics::arg_size); - static const uint16_t num_input_tensors{InferShapeHelper::count}; - static const bool turn_on_infer_shape_cache{true}; - - return [=](host_context::KernelFrame* frame) mutable { -#ifndef NDEBUG - LOG(INFO) << "Kernel.frame: " << frame->DumpArgTypes(); -#endif - // Build the infershape KernelFrame if needed. - // TODO(Superjomn) add unlikely here. - if (launcher.infershape_kernel_frame_builder.IsEmpty()) { - launcher.CreateKernelFrameForInferShape(frame); -#ifndef NDEBUG - LOG(INFO) << "infershape.frame: " - << launcher.infershape_kernel_frame_builder.DumpArgTypes(); -#endif - } - if (turn_on_infer_shape_cache) { - if (launcher.IsShapeChanged(num_input_tensors)) { - ::infrt::host_context::KernelImpl::Invoke( - &launcher.infershape_kernel_frame_builder); - launcher.BuildInferShapeCache(num_input_tensors); - } - } else { - ::infrt::host_context::KernelImpl::Invoke( - &launcher.infershape_kernel_frame_builder); - } - ::infrt::host_context::KernelImpl::Invoke(frame); - }; -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/registry.cc b/paddle/infrt/kernel/phi/registry.cc deleted file mode 100644 index fa51ab3566d..00000000000 --- a/paddle/infrt/kernel/phi/registry.cc +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/phi/registry.h" - -#include -#include - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/kernel/phi/context_kernels.h" -#include "paddle/infrt/kernel/phi/dense_tensor_kernels.h" -#include "paddle/infrt/kernel/phi/infershaped/phi_kernel_launcher.h" -#include "paddle/phi/include/infermeta.h" -#include "paddle/phi/include/kernels.h" -#include "paddle/phi/kernels/matmul_kernel.h" - -using infrt::host_context::Attribute; - -namespace infrt { -namespace kernel { - -void RegisterPhiKernels(host_context::KernelRegistry* registry) { - registry->AddKernel("phi_dt.create_context.cpu", - INFRT_KERNEL(infrt::kernel::phi::CreateCPUContext)); - registry->AddKernel("phi_dt.create_dense_tensor.cpu", - INFRT_KERNEL(infrt::kernel::phi::CreateDenseTensor), - {"dims", "lod", "layout", "precision"}); - - registry->AddKernel( - "phi_dt.create_inited_dense_tensor.cpu.f32", - INFRT_KERNEL(infrt::kernel::phi::CreateInitedDenseTensorF32), - {"dims", "lod", "layout", "value"}); - - registry->AddKernel( - "phi_dt.create_host_inited_dense_tensor.f32", - INFRT_KERNEL(infrt::kernel::phi::CreateHostInitedDenseTensorF32), - {"dims", "lod", "layout", "values", "run_once"}); - - registry->AddKernel("phi_dt.fill_dense_tensor.f32", - INFRT_KERNEL(infrt::kernel::phi::FillDenseTensorF32), - {"value"}); - - registry->AddKernel("phi_dt.print_tensor", - INFRT_KERNEL(infrt::kernel::phi::PrintDenseTensor)); - -#ifdef INFRT_WITH_GPU - registry->AddKernel("phi_dt.create_context.gpu", - INFRT_KERNEL(infrt::kernel::phi::CreateGPUContext)); - registry->AddKernel("phi_dt.create_dense_tensor.gpu", - INFRT_KERNEL(infrt::kernel::phi::CreateGPUDenseTensor), - {"dims", "lod", "layout", "precision"}); - registry->AddKernel("phi_dt.memcpy.gpu", - INFRT_KERNEL(infrt::kernel::phi::GpuMemCpy), - {"d2h"}); -#endif - registry->AddKernel("phi_dt.load_params", - INFRT_KERNEL(infrt::kernel::phi::LoadParams), - {"path"}); - registry->AddKernel("phi_dt.load_combined_params_to_gpu", - INFRT_KERNEL(infrt::kernel::phi::LoadCombinedParamsToGpu), - {"model_path", "params_path"}); - registry->AddKernel("phi_dt.load_combined_params", - INFRT_KERNEL(infrt::kernel::phi::LoadCombinedParams), - {"model_path", "params_path"}); - registry->AddKernel("phi_dt.tensor_map_get_tensor", - INFRT_KERNEL(infrt::kernel::phi::TensorMapGetTensor), - {"name"}); - registry->AddKernel("phi_dt.tensor_map_get_size", - INFRT_KERNEL(infrt::kernel::phi::TensorMapGetSize)); -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/registry.h b/paddle/infrt/kernel/phi/registry.h deleted file mode 100644 index c72085a50c1..00000000000 --- a/paddle/infrt/kernel/phi/registry.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -namespace host_context { - -struct KernelRegistry; - -} // namespace host_context -} // namespace infrt - -namespace infrt { -namespace kernel { - -/** - * Register all the phi kernels to registry. - */ -void RegisterPhiKernels(host_context::KernelRegistry* registry); - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensor_kernels.cc b/paddle/infrt/kernel/tensor_kernels.cc deleted file mode 100644 index 77c2f90b26b..00000000000 --- a/paddle/infrt/kernel/tensor_kernels.cc +++ /dev/null @@ -1,168 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/tensor_kernels.h" - -#include -#include - -#include "paddle/infrt/common/global.h" -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/tensor/dense_host_tensor.h" -#include "paddle/infrt/tensor/dense_tensor_view.h" -#include "paddle/infrt/tensor/tensor_map.h" -#include "paddle/infrt/tensor/tensor_shape.h" - -#ifdef INFRT_WITH_PHI -#include "paddle/phi/core/dense_tensor.h" -#endif - -namespace infrt { -namespace kernel { -using namespace host_context; // NOLINT -using namespace tensor; // NOLINT - -/// ===== Kernel begin ==== - -template -DenseHostTensor CreateUninitTensor(Attribute> shape) { - const auto &shape_data = shape.get(); - auto array = llvm::ArrayRef(shape_data.data(), shape_data.size()); - auto type = GetDType(); - return DenseHostTensor(TensorShape(array), type); -} - -void PrintTensor(const DenseHostTensor &tensor) { - std::cout << tensor << std::endl; -} - -template -void FillTensorWithConstant(Attribute v, DenseHostTensor *tensor) { - MutableDTArrayView(tensor).Fill(v.get()); -} - -TensorMap LoadParams(Attribute path) { - return *(infrt::tensor::LoadParams(path.get())); -} - -DenseHostTensor TensorMapGetTensor(TensorMap map, Attribute name) { - auto it = map.find(name.get()); - CHECK(it != map.end()) << "No tensor called " << name.get() - << " in the TensorMap"; - return *it->second; -} - -int32_t TensorMapGetSize(TensorMap map) { return map.size(); } - -// TODO(wilber): Maybe we should place TensorList type in dt dialect. -#ifdef INFRT_WITH_PHI -::Tensor TensorListGetTensor(std::vector<::Tensor *> list, - Attribute idx) { - CHECK_LT(idx.get(), static_cast(list.size())) - << "idx should less than list size"; - return *list[idx.get()]; -} - -int32_t TensorListGetSize(const std::vector<::Tensor *> &list) { - return list.size(); -} -#endif - -DenseHostTensor ShallowCopyTensor(DenseHostTensor v) { return v; } - -template -void NaiveElementwiseAdd(const DenseHostTensor &x, - const DenseHostTensor &y, - DenseHostTensor *out) { - CHECK_EQ(x.shape().GetNumElements(), y.shape().GetNumElements()); - - // Infer shape - *out = DenseHostTensor(x.shape(), GetDType()); - - const T *x_data = static_cast(x.raw_data()); - const T *y_data = static_cast(y.raw_data()); - T *out_data = static_cast(out->raw_data()); - for (size_t i = 0, n = x.shape().GetNumElements(); i < n; i++) { - out_data[i] = x_data[i] + y_data[i]; - } -} - -//! A naive implementation for x matmul w -template -void NaiveMatmul(const DenseHostTensor &x, - const DenseHostTensor &w, - DenseHostTensor *out) { - CHECK_EQ(x.shape().GetRank(), 2); - CHECK_EQ(w.shape().GetRank(), 2); - CHECK_EQ(x.shape().GetDim(x.shape().GetRank() - 1), w.shape().GetDim(0)); - std::vector out_dims({x.shape().GetDim(0), w.shape().GetDim(1)}); - *out = DenseHostTensor(TensorShape(out_dims), GetDType()); - - auto *out_data = static_cast(out->raw_data()); - auto *x_data = static_cast(x.raw_data()); - auto *w_data = static_cast(w.raw_data()); - - const int M = x.shape().GetDim(0); - const int K = x.shape().GetDim(1); - const int N = w.shape().GetDim(1); - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { - out_data[i * N + j] = 0; - for (int k = 0; k < K; k++) { - out_data[i * N + j] += x_data[i * K + k] * w_data[k * N + j]; - } - } - } -} - -/// ===== Kernel end ==== - -void RegisterTensorKernels(host_context::KernelRegistry *registry) { - registry->AddKernel("dt.create_uninit_tensor.f32", - INFRT_KERNEL(CreateUninitTensor), - {"shape"}); - registry->AddKernel("dt.print_tensor", INFRT_KERNEL(PrintTensor)); - registry->AddKernel("dt.fill_tensor_with_constant.f32", - INFRT_KERNEL(FillTensorWithConstant), - {"value"}); - registry->AddKernel("dt.fill_tensor_with_constant.f64", - INFRT_KERNEL(FillTensorWithConstant), - {"value"}); - - // TensorMap related methods. - registry->AddKernel("dt.load_params", INFRT_KERNEL(LoadParams)); - registry->AddKernel("dt.tensor_map_get_tensor", - INFRT_KERNEL(TensorMapGetTensor)); - registry->AddKernel("dt.tensor_map_get_size", INFRT_KERNEL(TensorMapGetSize)); - -// TensorList related methods. -#ifdef INFRT_WITH_PHI - registry->AddKernel( - "dt.tensor_list_get_tensor", INFRT_KERNEL(TensorListGetTensor), {"id"}); - registry->AddKernel("dt.tensor_list_get_size", - INFRT_KERNEL(TensorListGetSize)); -#endif - - registry->AddKernel("dt.shallow_copy_tensor", - INFRT_KERNEL(ShallowCopyTensor)); - - // Naive kernels. - registry->AddKernel("dt.naive_elementwise_add.f32", - INFRT_KERNEL(NaiveElementwiseAdd)); - registry->AddKernel("dt.naive_matmul.f32", INFRT_KERNEL(NaiveMatmul)); -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensor_kernels.h b/paddle/infrt/kernel/tensor_kernels.h deleted file mode 100644 index df8e25c3239..00000000000 --- a/paddle/infrt/kernel/tensor_kernels.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -namespace infrt { -namespace host_context { -struct KernelRegistry; -} // namespace host_context -} // namespace infrt - -namespace infrt { -namespace kernel { - -void RegisterTensorKernels(host_context::KernelRegistry* registry); - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensor_shape_kernels.cc b/paddle/infrt/kernel/tensor_shape_kernels.cc deleted file mode 100644 index 4edbecfa108..00000000000 --- a/paddle/infrt/kernel/tensor_shape_kernels.cc +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/tensor_shape_kernels.h" - -#include -#include -#include - -#include - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/tensor/tensor_shape.h" - -namespace infrt { -namespace kernel { - -void PrintShape(const tensor::TensorShape& shape) { - llvm::raw_os_ostream oos(std::cout); - oos << shape << '\n'; -} - -void RegisterTensorShapeKernels(host_context::KernelRegistry* registry) { - registry->AddKernel("ts.print_shape", INFRT_KERNEL(PrintShape)); -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensor_shape_kernels.h b/paddle/infrt/kernel/tensor_shape_kernels.h deleted file mode 100644 index e31a37463be..00000000000 --- a/paddle/infrt/kernel/tensor_shape_kernels.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -namespace infrt { -namespace host_context { - -class KernelRegistry; - -} // namespace host_context -} // namespace infrt - -namespace infrt { -namespace kernel { - -void RegisterTensorShapeKernels(host_context::KernelRegistry* registry); - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensorrt/CMakeLists.txt b/paddle/infrt/kernel/tensorrt/CMakeLists.txt deleted file mode 100644 index 2cb595f7ba4..00000000000 --- a/paddle/infrt/kernel/tensorrt/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -if(NOT - (INFRT_WITH_PHI - AND INFRT_WITH_GPU - AND INFRT_WITH_TRT)) - return() -endif() - -core_gather_headers() - -gather_srcs(infrt_src SRCS registry.cc trt_kernels.cc) diff --git a/paddle/infrt/kernel/tensorrt/registry.cc b/paddle/infrt/kernel/tensorrt/registry.cc deleted file mode 100644 index 197eb1ecb8a..00000000000 --- a/paddle/infrt/kernel/tensorrt/registry.cc +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/tensorrt/registry.h" - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/kernel/tensorrt/trt_kernels.h" - -namespace infrt { -namespace kernel { - -void RegisterTrtKernels(host_context::KernelRegistry* registry) { - registry->AddKernel("trt.create_engine", - INFRT_KERNEL(tensorrt::CreateTrtEngine), - {"run_once"}); - registry->AddKernel("trt.inspect_engine", - INFRT_KERNEL(tensorrt::PrintTrtLayer)); - registry->AddKernel("trt.compute", INFRT_KERNEL(tensorrt::TrtEngineCompute)); -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensorrt/registry.h b/paddle/infrt/kernel/tensorrt/registry.h deleted file mode 100644 index 762329ca61d..00000000000 --- a/paddle/infrt/kernel/tensorrt/registry.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -namespace host_context { - -struct KernelRegistry; - -} // namespace host_context -} // namespace infrt - -namespace infrt { -namespace kernel { - -/** - * Register all the trt kernels to registry. - */ -void RegisterTrtKernels(host_context::KernelRegistry* registry); - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensorrt/trt_helper.h b/paddle/infrt/kernel/tensorrt/trt_helper.h deleted file mode 100644 index 6f7455b848d..00000000000 --- a/paddle/infrt/kernel/tensorrt/trt_helper.h +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include - -#include "glog/logging.h" -#include "llvm/Support/ErrorHandling.h" -#include "mlir/IR/BuiltinAttributes.h" -#include "paddle/phi/common/data_type.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace kernel { -namespace tensorrt { - -static nvinfer1::DataType TensorTypeToWeightType(::phi::DataType tensor_type) { - switch (tensor_type) { - case ::phi::DataType::FLOAT32: - return nvinfer1::DataType::kFLOAT; - case ::phi::DataType::INT32: - return nvinfer1::DataType::kINT32; - case ::phi::DataType::FLOAT16: - return nvinfer1::DataType::kHALF; - default: - llvm_unreachable("should not reach here"); - } -} - -static nvinfer1::Dims ArrayAttrToNvDims(const mlir::ArrayAttr& int_array_attr) { - nvinfer1::Dims dims; - dims.nbDims = int_array_attr.size(); - CHECK(!int_array_attr.empty()); - CHECK(int_array_attr[0].getType().isIntOrIndex()); - for (int i = 0; i < dims.nbDims; ++i) { - dims.d[i] = int_array_attr[i].cast().getInt(); - } - return dims; -} - -template -static std::vector ArrayAttrToVec(const mlir::ArrayAttr& int_array_attr) { - std::vector ret; - ret.resize(int_array_attr.size()); - CHECK(!int_array_attr.empty()); - CHECK(int_array_attr[0].getType().isIntOrIndex()); - for (size_t i = 0; i < int_array_attr.size(); ++i) { - ret[i] = int_array_attr[i].cast().getInt(); - } - return ret; -} - -static nvinfer1::Weights TensorToWeights(::Tensor* tensor) { - CHECK_NOTNULL(tensor); - nvinfer1::Weights ret; - ret.type = TensorTypeToWeightType(tensor->dtype()); - ret.count = tensor->numel(); - ret.values = tensor->data(); - return ret; -} - -} // namespace tensorrt -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensorrt/trt_kernels.cc b/paddle/infrt/kernel/tensorrt/trt_kernels.cc deleted file mode 100644 index 3d30b0264c2..00000000000 --- a/paddle/infrt/kernel/tensorrt/trt_kernels.cc +++ /dev/null @@ -1,201 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/tensorrt/trt_kernels.h" - -#include -#include - -#include "NvInfer.h" -#include "NvInferRuntime.h" -#include "NvInferRuntimeCommon.h" -#include "glog/logging.h" -#include "llvm/ADT/STLExtras.h" -#include "llvm/Support/Casting.h" -#include "llvm/Support/raw_ostream.h" -#include "mlir/IR/BuiltinAttributes.h" -#include "mlir/IR/BuiltinTypes.h" -#include "mlir/IR/Operation.h" -#include "mlir/IR/Value.h" -#include "paddle/infrt/backends/tensorrt/trt_engine.h" -#include "paddle/infrt/backends/tensorrt/trt_options.h" -#include "paddle/infrt/dialect/tensorrt/trt_ops.h" -#include "paddle/infrt/host_context/symbol_table.h" -#include "paddle/infrt/kernel/tensorrt/trt_helper.h" -#include "paddle/infrt/kernel/tensorrt/trt_layers.h" -#include "paddle/phi/common/place.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace kernel { -namespace tensorrt { - -::infrt::backends::tensorrt::TrtEngine CreateTrtEngine( - MlirOperationWithInfrtSymbol create_engine_op) { - // TODO(wilber): The device_id needs to get from mlir. - int device_id = 0; - backends::tensorrt::TrtEngine engine(device_id); - - auto* builder = engine.GetTrtBuilder(); - // TODO(wilber): How to process weights? - backends::tensorrt::TrtUniquePtr network; - // TODO(wilber): static_shape or dynamic_shape network? The code is just - // static_shape test. - network.reset(builder->createNetworkV2(0)); - - // TODO(wilber): The build option shoule be fiiled from mlir info. - backends::tensorrt::BuildOptions options; - options.max_batch = 4; - options.workspace = 128; - - // Parse mlir Region which only has one block. - mlir::Operation& operation = *create_engine_op.operation; - auto* symbol_table = create_engine_op.symbol_table; - CHECK_NOTNULL(symbol_table); - - unsigned int num_regions = operation.getNumRegions(); - CHECK_EQ(num_regions, 1U) << "only support one region case."; - auto& region = operation.getRegion(0); - auto& block = region.getBlocks().front(); - - std::unordered_map trt_bind_inputs; - ValueToITensorMap value_to_trt_tensor_map; - ValueToTensorMap value_to_tensor_map; - - for (auto index_operand : llvm::enumerate(operation.getOperands())) { - mlir::Value operand = index_operand.value(); - size_t idx = index_operand.index(); - - const std::string input_name = "input_" + std::to_string(idx); - auto* v = symbol_table->GetValue(std::to_string(idx)); - CHECK_NOTNULL(v); - auto* t = &v->get<::Tensor>(); - value_to_tensor_map[operand] = t; - - // TODO(wilber): get input info from mlir. - - // TODO(wilber): input dims, now only support static_shape, and just remove - // the first dimension. If the first dim is not -1, maybe we can pass the - // origin dims. - - // TODO(wilber): now only suppot float input. - - if (operand.isa()) { - // TODO(wilber): A trick: the weights are CPU tensor and inputs are GPU - // tensor, so we treat all GPU tensors as inputs to trt. - if (t->place().GetType() == ::phi::AllocationType::GPU) { - trt_bind_inputs[input_name] = t; - nvinfer1::Dims dims; - dims.nbDims = t->dims().size() - 1; - for (int i = 0; i < dims.nbDims; ++i) { - dims.d[i] = t->dims()[i + 1]; - } - auto* in = network->addInput( - input_name.c_str(), nvinfer1::DataType::kFLOAT, dims); - value_to_trt_tensor_map[operand] = in; - } - } else { - // TODO(wilber): Replace with the op name that generates the weights. - std::unordered_set weight_flags{ - "phi_dt.tensor_map_get_tensor", - "phi_dt.create_dense_tensor.cpu", - "phi_dt.create_inited_dense_tensor.cpu.f32", - "phi_dt.create_host_inited_dense_tensor.f32"}; - if (!weight_flags.count( - operand.getDefiningOp()->getName().getStringRef().str())) { - trt_bind_inputs[input_name] = t; - nvinfer1::Dims dims; - dims.nbDims = t->dims().size() - 1; - for (int i = 0; i < dims.nbDims; ++i) { - dims.d[i] = t->dims()[i + 1]; - } - auto* in = network->addInput( - input_name.c_str(), nvinfer1::DataType::kFLOAT, dims); - value_to_trt_tensor_map[operand] = in; - } - } - } - - // TODO(wilber): Find a way to add layer. - for (auto& operation : block.without_terminator()) { - VLOG(1) << "process " << operation.getName().getStringRef().str() << " ..."; - if (trt::ActivationOp op = llvm::dyn_cast(operation)) { - ActivationFunc( - op, network.get(), value_to_trt_tensor_map, value_to_tensor_map); - } else if (trt::FullyConnectedOp op = - llvm::dyn_cast(operation)) { - FcFunc(op, network.get(), value_to_trt_tensor_map, value_to_tensor_map); - } else if (trt::ConvolutionOp op = - llvm::dyn_cast(operation)) { - ConvFunc(op, network.get(), value_to_trt_tensor_map, value_to_tensor_map); - } else if (trt::PoolingOp op = llvm::dyn_cast(operation)) { - PoolFunc(op, network.get(), value_to_trt_tensor_map, value_to_tensor_map); - } else if (trt::ShuffleOp op = llvm::dyn_cast(operation)) { - ShuffleFunc( - op, network.get(), value_to_trt_tensor_map, value_to_tensor_map); - } else if (trt::ScaleNdOp op = llvm::dyn_cast(operation)) { - ScaleNdFunc( - op, network.get(), value_to_trt_tensor_map, value_to_tensor_map); - } else if (trt::ElementWiseOp op = - llvm::dyn_cast(operation)) { - EltwiseFunc( - op, network.get(), value_to_trt_tensor_map, value_to_tensor_map); - } else { - CHECK(false) << "not supported operation."; - } - } - - for (auto index_operand : - llvm::enumerate(block.getTerminator()->getOperands())) { - mlir::Value arg = index_operand.value(); - CHECK(value_to_trt_tensor_map.count(arg)); - // TODO(wilber): A trick that we name trt output tensor's name as output_0, - // output_1, ... - value_to_trt_tensor_map[arg]->setName( - ("output_" + std::to_string(index_operand.index())).c_str()); - network->markOutput(*value_to_trt_tensor_map[arg]); - } - for (int i = 0; i < network->getNbOutputs(); ++i) { - engine.PrepareOutputHandle(network->getOutput(i)->getName()); - } - - VLOG(3) << "trt engine build start."; - engine.Build(std::move(network), options); - VLOG(3) << "trt engine build done."; - - // TODO(wilber): get inference options from mlir. - backends::tensorrt::InferenceOptions inference_options; - inference_options.batch = 1; - // TODO(wilber): bind trt input/output tensors. - engine.SetUpInference(inference_options, trt_bind_inputs); - return engine; -} - -void PrintTrtLayer(backends::tensorrt::TrtEngine* engine) { - engine->GetEngineInfo(); -} - -std::vector<::Tensor*> TrtEngineCompute(backends::tensorrt::TrtEngine* engine, - const ::phi::GPUContext& context) { - engine->Run(context); - std::vector<::Tensor*> res; - for (size_t i = 0; i < engine->GetOutputNum(); ++i) { - res.push_back(engine->GetOutput("output_" + std::to_string(i))); - } - return res; -} - -} // namespace tensorrt -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensorrt/trt_kernels.h b/paddle/infrt/kernel/tensorrt/trt_kernels.h deleted file mode 100644 index 254b8ed14d7..00000000000 --- a/paddle/infrt/kernel/tensorrt/trt_kernels.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include - -#include "mlir/IR/Operation.h" -#include "paddle/infrt/backends/tensorrt/trt_engine.h" -#include "paddle/phi/backends/gpu/gpu_context.h" - -namespace infrt { -namespace host_context { -class SymbolTable; -} // namespace host_context - -namespace kernel { -namespace tensorrt { - -struct MlirOperationWithInfrtSymbol { - mlir::Operation* operation; - ::infrt::host_context::SymbolTable* symbol_table; -}; - -::infrt::backends::tensorrt::TrtEngine CreateTrtEngine( - MlirOperationWithInfrtSymbol engine_op); - -void PrintTrtLayer(backends::tensorrt::TrtEngine* engine); - -std::vector<::Tensor*> TrtEngineCompute(backends::tensorrt::TrtEngine* engine, - const ::phi::GPUContext& context); - -} // namespace tensorrt -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/tensorrt/trt_layers.h b/paddle/infrt/kernel/tensorrt/trt_layers.h deleted file mode 100644 index 0f2c2c88ca0..00000000000 --- a/paddle/infrt/kernel/tensorrt/trt_layers.h +++ /dev/null @@ -1,323 +0,0 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include -#include - -#include - -#include "paddle/infrt/backends/tensorrt/plugin/pool_op_plugin.h" -#include "paddle/infrt/dialect/tensorrt/trt_ops.h" -#include "paddle/infrt/kernel/tensorrt/trt_helper.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace infrt { -namespace kernel { -namespace tensorrt { - -using ValueToTensorMap = llvm::DenseMap; -using ValueToITensorMap = llvm::DenseMap; - -inline void ActivationFunc( - trt::ActivationOp& act_op, // NOLINT - nvinfer1::INetworkDefinition* network, - ValueToITensorMap& value_to_trt_tensor_map, // NOLINT - ValueToTensorMap& value_to_tensor_map) { // NOLINT - auto in_arg = act_op.getOperand(); - CHECK(value_to_trt_tensor_map.count(in_arg)) - << "value_to_trt_tensor_map not has in_arg."; - - nvinfer1::ActivationType act_type = - static_cast(act_op.activation_type()); - auto* act_layer = - network->addActivation(*value_to_trt_tensor_map[in_arg], act_type); - act_layer->setAlpha(act_op.alpha().convertToFloat()); - act_layer->setBeta(act_op.beta().convertToFloat()); - for (size_t i = 0; i < act_op->getNumResults(); ++i) { - nvinfer1::ITensor* act_out_tensor = act_layer->getOutput(i); - mlir::Value act_out = act_op->getResult(i); - value_to_trt_tensor_map[act_out] = act_out_tensor; - } -} - -inline void ConvFunc(trt::ConvolutionOp& op, // NOLINT - nvinfer1::INetworkDefinition* network, - ValueToITensorMap& value_to_trt_tensor_map, // NOLINT - ValueToTensorMap& value_to_tensor_map) { // NOLINT - mlir::Value input_tensor_repr = op.input_tensor(); - int out_channel_num = op.out_channel_num(); - auto size_attrs = op.kernel_size(); - nvinfer1::Dims dims = ArrayAttrToNvDims(size_attrs); - auto kernel_weights = - TensorToWeights(value_to_tensor_map[op.kernel_weights()]); - nvinfer1::Weights bias_weights; - if (op.bias_weights() == mlir::Value()) { - bias_weights = nvinfer1::Weights{}; - } else { - bias_weights = TensorToWeights(value_to_tensor_map[op.bias_weights()]); - } - - auto* layer = - network->addConvolutionNd(*value_to_trt_tensor_map[input_tensor_repr], - out_channel_num, - dims, - kernel_weights, - bias_weights); - - layer->setPaddingNd(ArrayAttrToNvDims(op.paddings())); - layer->setStrideNd(ArrayAttrToNvDims(op.strides())); - CHECK_NOTNULL(layer); - mlir::Value out_repr = op.output_tensor(); - nvinfer1::ITensor* out_tensor = layer->getOutput(0); - value_to_trt_tensor_map[out_repr] = out_tensor; -} - -inline void PoolFunc(trt::PoolingOp& op, // NOLINT - nvinfer1::INetworkDefinition* network, - ValueToITensorMap& value_to_trt_tensor_map, // NOLINT - ValueToTensorMap& value_to_tensor_map) { // NOLINT - mlir::Value input_tensor_repr = op.input_tensor(); - nvinfer1::ITensor* input_itensor = value_to_trt_tensor_map[input_tensor_repr]; - nvinfer1::Dims input_shape = input_itensor->getDimensions(); - int input_dims = input_shape.nbDims; - - auto padding_mode = op.padding_mode(); - auto pool_type = op.pool_type(); - mlir::ArrayAttr paddings = op.paddings(); - mlir::ArrayAttr strides = op.strides(); - mlir::ArrayAttr ksize = op.window_size(); - bool exclusive = op.exclusive(); - bool adaptive = op.adaptive(); - auto padding_algorithm = op.padding_algorithm().str(); - - if (padding_algorithm == "SAME") { - // TODO(wilber) - CHECK(false) << "Not supported `same` padding algorithm"; - } - - if (adaptive) { - // TODO(Inference) - // CHECK(false) << "Not supported adaptive pool"; - - // TODO(wilber): Reformat. - // global average pooling. - auto ksize_vec = ArrayAttrToVec(ksize); - if (static_cast(pool_type) == - nvinfer1::PoolingType::kAVERAGE && - ksize_vec.size() == 2 && ksize_vec[0] == 1 && ksize_vec[1] == 1) { - nvinfer1::Dims dims; - dims.nbDims = 2; - dims.d[0] = input_shape.d[1]; - dims.d[1] = input_shape.d[2]; - auto* layer = network->addPoolingNd( - *input_itensor, static_cast(pool_type), dims); - CHECK_NOTNULL(layer); - - mlir::Value out_repr = op.output_tensor(); - nvinfer1::ITensor* out_tensor = layer->getOutput(0); - value_to_trt_tensor_map[out_repr] = out_tensor; - return; - } - - // plugin... - std::vector input_shape_v; - for (int i = 0; i < input_dims; i++) { - input_shape_v.push_back(input_shape.d[i]); - } - auto paddings_val = ArrayAttrToVec(paddings); - std::vector real_paddings = paddings_val; - for (int i = 0; i < 2; ++i) { - int copy_pad = *(paddings_val.begin() + i); - real_paddings.insert(real_paddings.begin() + 2 * i + 1, copy_pad); - } - - auto* plugin = new backends::tensorrt::plugin::PoolPlugin( - false, - backends::tensorrt::plugin::PoolPlugin::PoolType::avg, - adaptive, - exclusive, - ArrayAttrToVec(ksize), - ArrayAttrToVec(strides), - paddings_val, - input_shape_v, - real_paddings); - auto* layer = network->addPluginV2(&input_itensor, 1, *plugin); - - mlir::Value out_repr = op.output_tensor(); - nvinfer1::ITensor* out_tensor = layer->getOutput(0); - value_to_trt_tensor_map[out_repr] = out_tensor; - return; - } - - nvinfer1::Dims window_size = ArrayAttrToNvDims(ksize); - - auto* layer = - network->addPoolingNd(*input_itensor, - static_cast(pool_type), - window_size); - CHECK_NOTNULL(layer); - layer->setPaddingMode(static_cast(padding_mode)); - layer->setPaddingNd(ArrayAttrToNvDims(paddings)); - layer->setStrideNd(ArrayAttrToNvDims(strides)); - layer->setAverageCountExcludesPadding(exclusive); - - mlir::Value out_repr = op.output_tensor(); - nvinfer1::ITensor* out_tensor = layer->getOutput(0); - value_to_trt_tensor_map[out_repr] = out_tensor; -} - -inline void FcFunc(trt::FullyConnectedOp& op, // NOLINT - nvinfer1::INetworkDefinition* network, - ValueToITensorMap& value_to_trt_tensor_map, // NOLINT - ValueToTensorMap& value_to_tensor_map) { // NOLINT - mlir::Value input_tensor_repr = op.input_tensor(); - CHECK(value_to_trt_tensor_map.count(input_tensor_repr)); - - nvinfer1::ITensor* input_itensor = value_to_trt_tensor_map[input_tensor_repr]; - nvinfer1::Dims input_shape = input_itensor->getDimensions(); - int input_dims = input_shape.nbDims; - CHECK_EQ(input_dims, 1) << "Now we only support 2-d input."; - // TODO(wilber): We should place the logic to ir. Now only support 2-d input - // and we reshape to 4-d. - nvinfer1::Dims reshape_before_fc_dim; - reshape_before_fc_dim.nbDims = input_dims + 2; - // padding shape "* x q x 1 x 1" - for (int i = 0; i < reshape_before_fc_dim.nbDims; i++) { - reshape_before_fc_dim.d[i] = 1; - } - reshape_before_fc_dim.d[0] = input_shape.d[0]; - auto* reshape_before_fc_layer = network->addShuffle(*input_itensor); - reshape_before_fc_layer->setReshapeDimensions(reshape_before_fc_dim); - - auto* reshape_itensor = reshape_before_fc_layer->getOutput(0); - - auto kernel_weights = - TensorToWeights(value_to_tensor_map[op.kernel_weights()]); - auto bias_weights = TensorToWeights(value_to_tensor_map[op.bias_weights()]); - - int out_channel_num = op.out_channel_num(); - auto* layer = network->addFullyConnected( - *reshape_itensor, out_channel_num, kernel_weights, bias_weights); - - // TODO(wilber): fix. - nvinfer1::Dims reshape_after_fc_dim; - reshape_after_fc_dim.nbDims = 1; - reshape_after_fc_dim.d[0] = layer->getOutput(0)->getDimensions().d[0]; - auto* reshape_after_fc_layer = network->addShuffle(*layer->getOutput(0)); - reshape_after_fc_layer->setReshapeDimensions(reshape_after_fc_dim); - - mlir::Value out_repr = op.output_tensor(); - nvinfer1::ITensor* out_tensor = reshape_after_fc_layer->getOutput(0); - value_to_trt_tensor_map[out_repr] = out_tensor; -} - -inline void ShuffleFunc(trt::ShuffleOp& op, // NOLINT - nvinfer1::INetworkDefinition* network, - ValueToITensorMap& value_to_trt_tensor_map, // NOLINT - ValueToTensorMap& value_to_tensor_map) { // NOLINT - mlir::Value input_tensor_repr = op.input_tensor(); - nvinfer1::ITensor* input = value_to_trt_tensor_map[input_tensor_repr]; - int dims = input->getDimensions().nbDims; - int start_axis = op.start_axis(); - int stop_axis = op.stop_axis(); - - nvinfer1::IShuffleLayer* layer = nullptr; - if (start_axis < 0) start_axis += dims + 1; - if (stop_axis < 0) stop_axis += dims + 1; - int dim_prod = 1; - nvinfer1::Dims flatten_dim; - flatten_dim.nbDims = dims - (stop_axis - start_axis); - for (int i = 0, j = 0; i < dims; ++i) { - if (start_axis <= i + 1 && i + 1 <= stop_axis) { - int dim_i = input->getDimensions().d[i]; - CHECK_GT(dim_i, 0); - dim_prod *= dim_i; - if (i + 1 == stop_axis) { - flatten_dim.d[j++] = dim_prod; - } - } else { - flatten_dim.d[j++] = input->getDimensions().d[i]; - } - } - layer = network->addShuffle(*value_to_trt_tensor_map[input_tensor_repr]); - CHECK_NOTNULL(layer); - layer->setReshapeDimensions(flatten_dim); - for (size_t i = 0; i < op->getNumResults(); ++i) { - nvinfer1::ITensor* out_tensor = layer->getOutput(i); - mlir::Value out_value = op->getResult(i); - value_to_trt_tensor_map[out_value] = out_tensor; - } -} - -inline void ScaleNdFunc(trt::ScaleNdOp& op, // NOLINT - nvinfer1::INetworkDefinition* network, - ValueToITensorMap& value_to_trt_tensor_map, // NOLINT - ValueToTensorMap& value_to_tensor_map) { // NOLINT - mlir::Value input_tensor_repr = op.input_tensor(); - nvinfer1::ITensor* input = value_to_trt_tensor_map[input_tensor_repr]; - - mlir::Value shift_tensor_repr = op.shift(); - nvinfer1::Weights shift = - TensorToWeights(value_to_tensor_map[shift_tensor_repr]); - - mlir::Value scale_tensor_repr = op.scale(); - - nvinfer1::Weights scale = - TensorToWeights(value_to_tensor_map[scale_tensor_repr]); - - nvinfer1::Weights power_weights{nvinfer1::DataType::kFLOAT, nullptr, 0}; - - nvinfer1::IScaleLayer* layer = nullptr; - layer = network->addScaleNd( - *input, nvinfer1::ScaleMode::kCHANNEL, shift, scale, power_weights, 0); - CHECK_NOTNULL(layer); - - for (size_t i = 0; i < op->getNumResults(); ++i) { - nvinfer1::ITensor* out_tensor = layer->getOutput(i); - mlir::Value out_value = op->getResult(i); - value_to_trt_tensor_map[out_value] = out_tensor; - } -} - -inline void EltwiseFunc(trt::ElementWiseOp& op, // NOLINT - nvinfer1::INetworkDefinition* network, - ValueToITensorMap& value_to_trt_tensor_map, // NOLINT - ValueToTensorMap& value_to_tensor_map) { // NOLINT - mlir::Value input1_tensor_repr = op.input1(); - mlir::Value input2_tensor_repr = op.input2(); - nvinfer1::ITensor* input1 = value_to_trt_tensor_map[input1_tensor_repr]; - nvinfer1::ITensor* input2 = value_to_trt_tensor_map[input2_tensor_repr]; - - auto eltwise_operation = op.elementwise_operation(); - - auto* layer = network->addElementWise( - *input1, - *input2, - static_cast(eltwise_operation)); - CHECK_NOTNULL(layer); - for (size_t i = 0; i < op->getNumResults(); ++i) { - nvinfer1::ITensor* out_tensor = layer->getOutput(i); - mlir::Value out_value = op->getResult(i); - value_to_trt_tensor_map[out_value] = out_tensor; - } -} - -} // namespace tensorrt -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/test_kernels.cc b/paddle/infrt/kernel/test_kernels.cc deleted file mode 100644 index e00afa4b790..00000000000 --- a/paddle/infrt/kernel/test_kernels.cc +++ /dev/null @@ -1,202 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/kernel/test_kernels.h" - -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "paddle/infrt/host_context/kernel_registry.h" -#include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/host_context/mlir_function_executable.h" -#include "paddle/infrt/tensor/dense_host_tensor.h" - -using infrt::host_context::Attribute; -using infrt::host_context::MlirFunctionExecutable; -using infrt::host_context::RemainingArguments; - -namespace infrt { -namespace kernel { -namespace { -class BenchmarkStats { - public: - BenchmarkStats(std::string name, - int num_warmup_runs, - int max_count, - std::chrono::microseconds benchmark_duration) - : name_{name}, - num_warmup_runs_{num_warmup_runs}, - max_count_{max_count}, - benchmark_duration_{benchmark_duration} {} - - void StartRun() { - ++cur_count_; - // Start recording CPU time. - cur_start_walltime_ = std::chrono::steady_clock::now(); - cur_start_cpu_ = std::clock(); - } - - void StopRun() { - // Do not collect the runtime statistics if we are still in the warm up - // period. - if (cur_count_ <= num_warmup_runs_) return; - - // Stop the CPU timer. - std::clock_t cur_stop_cpu_ = std::clock(); - - // Stop the wall clock timer. - auto cur_stop_walltime_ = std::chrono::steady_clock::now(); - - // Collect the wall clock duration. - auto duration_walltime_ = cur_stop_walltime_ - cur_start_walltime_; - run_times_walltime_.push_back(duration_walltime_); - - // Collect the CPU duration in microseconds. - // First cast to integer that represents microseconds with truncation, as - // does std::chrono::duration_cast. Then cast to std::chrono::microseconds. - std::clock_t duration_cpu_raw = cur_stop_cpu_ - cur_start_cpu_; - auto duration_cpu_ = static_cast( - static_cast(1e9 * duration_cpu_raw / CLOCKS_PER_SEC)); - - run_times_cpu_.push_back(duration_cpu_); - - total_duration_walltime_ += duration_walltime_; - total_duration_cpu_ += duration_cpu_; - } - // Return if we should we run more rounds. - bool MoreRun() const { - return cur_count_ < max_count_ + num_warmup_runs_ && - total_duration_walltime_ < benchmark_duration_; - } - - // Summarize the benchmark results. - void Summarize() { - std::sort(run_times_walltime_.begin(), run_times_walltime_.end()); - std::sort(run_times_cpu_.begin(), run_times_cpu_.end()); - - auto percentile = - [](double p, const std::vector &run_times) { - assert(p >= 0.0 && p <= 1.0); - return run_times[run_times.size() * p]; - }; - - // BM: prefix is added to make grepping results from lit output easier. - std::string prefix; - llvm::raw_string_ostream(prefix) << "BM:" << name_ << ':'; - auto cpu_utilization = - total_duration_cpu_.count() * 100.0 / total_duration_walltime_.count(); - - llvm::outs() << prefix << "Count: " << run_times_walltime_.size() << '\n'; - llvm::outs() << prefix - << "Duration(ns): " << total_duration_walltime_.count() - << '\n'; - llvm::outs() << prefix - << "Time Min(ns): " << run_times_walltime_.front().count() - << '\n'; - llvm::outs() << prefix - << "Time Max(ns): " << run_times_walltime_.back().count() - << '\n'; - llvm::outs() << prefix << "Time 50%(ns): " - << percentile(0.5, run_times_walltime_).count() << '\n'; - llvm::outs() << prefix << "Time 95%(ns): " - << percentile(0.95, run_times_walltime_).count() << '\n'; - llvm::outs() << prefix << "Time 99%(ns): " - << percentile(0.99, run_times_walltime_).count() << '\n'; - // Log CPU time statistics. - llvm::outs() << prefix - << "CPU Duration(ns): " << total_duration_cpu_.count() << '\n'; - llvm::outs() << prefix << "CPU Min(ns): " << run_times_cpu_.front().count() - << '\n'; - llvm::outs() << prefix << "CPU Max(ns): " << run_times_cpu_.back().count() - << '\n'; - llvm::outs() << prefix - << "CPU 50%(ns): " << percentile(0.5, run_times_cpu_).count() - << '\n'; - llvm::outs() << prefix - << "CPU 95%(ns): " << percentile(0.95, run_times_cpu_).count() - << '\n'; - llvm::outs() << prefix - << "CPU 99%(ns): " << percentile(0.99, run_times_cpu_).count() - << '\n'; - llvm::outs() << prefix << "CPU utilization(percent): " << cpu_utilization - << "\n"; - llvm::outs().flush(); - } - - private: - const std::string name_; - const int num_warmup_runs_; - const int max_count_; - int cur_count_ = 0; - const std::chrono::nanoseconds benchmark_duration_; - std::chrono::nanoseconds total_duration_walltime_{}; - std::chrono::nanoseconds total_duration_cpu_{}; - std::chrono::time_point cur_start_walltime_{}; - std::clock_t cur_start_cpu_; - std::vector run_times_walltime_; - // CPU run times in microseconds. - std::vector run_times_cpu_; -}; - -} // anonymous namespace - -// This op benchmarks the input function by running the function in a loop -// up to a max count or max time as specified in the function's attributes. -// -// Attributes: -// duration_secs: Benchmark duration in seconds. -// max_count: Max run count of input function. -// name: The name used to tag the benchmark results. -// num_warmup_runs: Number of warm up runs before benchmarking starts. -// fn: The input function to be benchmarked. -static void benchmark(RemainingArguments args, - host_context::RemainingResults results, - Attribute duration_secs, - Attribute max_count, - Attribute name, - Attribute num_warmup_runs, - Attribute fn) { - BenchmarkStats bm_stats{name.get(), - num_warmup_runs.get(), - max_count.get(), - std::chrono::seconds(duration_secs.get())}; - - while (bm_stats.MoreRun()) { - bm_stats.StartRun(); - fn.get()->Execute(args.values(), results.values(), true); - bm_stats.StopRun(); - } - bm_stats.Summarize(); -} - -// Just copy the input to the result. -tensor::DenseHostTensor ShadowCopyTensor(tensor::DenseHostTensor src) { - return src; -} - -void RegisterTestKernels(host_context::KernelRegistry *registry) { - registry->AddKernel("infrt.benchmark", INFRT_KERNEL(benchmark)); - registry->AddKernel("Infrt.test.shadow_copy_tensor", - INFRT_KERNEL(ShadowCopyTensor)); -} - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/test_kernels.h b/paddle/infrt/kernel/test_kernels.h deleted file mode 100644 index f5639ec1afa..00000000000 --- a/paddle/infrt/kernel/test_kernels.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -namespace infrt { -namespace host_context { - -struct KernelRegistry; - -} // namespace host_context -} // namespace infrt - -namespace infrt { -namespace kernel { - -/** - * Register all the test kernels to registry. - */ -void RegisterTestKernels(host_context::KernelRegistry* registry); - -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/paddle/CMakeLists.txt b/paddle/infrt/paddle/CMakeLists.txt deleted file mode 100644 index 5f894626f80..00000000000 --- a/paddle/infrt/paddle/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -proto_library(paddle_framework_proto SRCS framework.proto) - -add_subdirectory(cpp) -add_subdirectory(pb) - -core_gather_headers() - -gather_srcs(infrt_src SRCS model_parser.cc scope.cc tensor.cc) - -file( - GLOB includes - LIST_DIRECTORIES false - RELATIVE ${CMAKE_SOURCE_DIR} - *.h) - -foreach(header ${includes}) - set(core_includes - "${core_includes};${header}" - CACHE INTERNAL "") -endforeach() diff --git a/paddle/infrt/paddle/cpp/CMakeLists.txt b/paddle/infrt/paddle/cpp/CMakeLists.txt deleted file mode 100644 index 99477471084..00000000000 --- a/paddle/infrt/paddle/cpp/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -file( - GLOB includes - LIST_DIRECTORIES false - RELATIVE ${CMAKE_SOURCE_DIR} - *.h) - -foreach(header ${includes}) - set(core_includes - "${core_includes};${header}" - CACHE INTERNAL "") -endforeach() diff --git a/paddle/infrt/paddle/cpp/desc_api.h b/paddle/infrt/paddle/cpp/desc_api.h deleted file mode 100644 index 3b2dcb0018b..00000000000 --- a/paddle/infrt/paddle/cpp/desc_api.h +++ /dev/null @@ -1,233 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include -#include - -namespace infrt { -namespace paddle { -namespace cpp { - -/* - * Compatible interfaces for all the different kinds of XXXDesc. All the XXXDesc - * classes should implement this. - */ -class VarDescAPI { - public: - enum class Type { - // Pod Types - BOOL = 0, - INT16, - INT32, - INT64, - FP16, - FP32, - FP64, - // Tensor is used in C++. - SIZE_T, - UINT8, - INT8, - - // Other types that may need additional descriptions - LOD_TENSOR, - SELECTED_ROWS, - FEED_MINIBATCH, - FETCH_LIST, - STEP_SCOPES, - LOD_RANK_TABLE, - LOD_TENSOR_ARRAY, - PLACE_LIST, - READER, - // Any runtime decided variable type is raw - // raw variables should manage their own allocations - // in operators like nccl_op - RAW, - TUPLE - }; - - using VarDataType = Type; - - virtual ~VarDescAPI() = default; - - // Get var's name - virtual std::string Name() const = 0; - // Set var's name - virtual void SetName(std::string name) = 0; - // Get var's type - virtual Type GetType() const = 0; - // Set var's type - virtual void SetType(Type type) = 0; - // Tell whether var is persistable or not - virtual bool Persistable() const = 0; - // Set var to be persistable or not - virtual void SetPersistable(bool persistable) = 0; - // Get var's shape - virtual std::vector GetShape() const = 0; - // Set var's shape - virtual void SetShape(const std::vector& dims) = 0; -}; - -/* - * NOTE Some interfaces are weried, we remain them unchanged to keep compatible - * with framework::OpDesc in Fluid framework. - */ -class OpDescAPI { - public: - // The AttrType is used to make the proto::AttrType portable. - enum class AttrType { - INT = 0, - FLOAT = 1, - STRING = 2, - INTS = 3, - FLOATS = 4, - STRINGS = 5, - BOOLEAN = 6, - BOOLEANS = 7, - BLOCK = 8, - LONG = 9, - BLOCKS = 10, - LONGS = 11, - UNK, - }; - - virtual ~OpDescAPI() = default; - - /// Get operator's type. - virtual std::string Type() const = 0; - /// Set operator's type. - virtual void SetType(const std::string& type) = 0; - /// Get arguments given the parameter. - virtual std::vector Input(const std::string& param) const = 0; - /// Get parameters. - virtual std::vector InputArgumentNames() const = 0; - /// Get arguments given the parameter. - virtual std::vector Output(const std::string& param) const = 0; - /// Get parameters. - virtual std::vector OutputArgumentNames() const = 0; - /// Set a input given the parameter and arguments. - virtual void SetInput(const std::string& param, - const std::vector& args) = 0; - virtual void SetOutput(const std::string& param, - const std::vector& args) = 0; - /// Tell whether this desc has an attribute. - virtual bool HasAttr(const std::string& name) const = 0; - - /// Get the type of an attribute. - virtual AttrType GetAttrType(const std::string& name) const = 0; - - virtual std::vector AttrNames() const = 0; - - /// Set an attribute. - template - void SetAttr(const std::string& name, const T& v); - - /// Get an attribute. - template - T GetAttr(const std::string& name) const; - - std::string Repr() const { - std::stringstream ss; - ss << Type(); - ss << "("; - for (auto& arg : InputArgumentNames()) { - ss << arg << ":"; - for (auto val : Input(arg)) { - ss << val << " "; - } - } - ss << ") -> ("; - for (auto& arg : OutputArgumentNames()) { - ss << arg << ":"; - for (auto val : Output(arg)) { - ss << val << " "; - } - } - ss << ")"; - return ss.str(); - } -}; - -class BlockDescAPI { - public: - virtual ~BlockDescAPI() = default; - - virtual int32_t Idx() const = 0; - - virtual void SetIdx(int32_t idx) = 0; - - virtual int32_t ParentIdx() const = 0; - - virtual void SetParentIdx(int32_t idx) = 0; - - virtual size_t VarsSize() const = 0; - - virtual void ClearVars() = 0; - - // NOTE: This ugly method is used to compatible interfaces between cpp and - // pb/nb backends - // TODO(sangoly): refine this - template - T* GetVar(int32_t idx); - - template - T* AddVar(); - - virtual size_t OpsSize() const = 0; - - virtual void ClearOps() = 0; - - // NOTE: This ugly method is used to compatible interfaces between cpp and - // pb/nb backends - // TODO(sangoly): refine this - template - T* GetOp(int32_t idx); - - template - T* AddOp(); - - virtual int32_t ForwardBlockIdx() const = 0; - - virtual void SetForwardBlockIdx(int32_t idx) = 0; -}; - -class ProgramDescAPI { - public: - virtual ~ProgramDescAPI() = default; - - virtual size_t BlocksSize() const = 0; - - virtual void ClearBlocks() = 0; - - // NOTE: This ugly method is used to compatible interfaces between cpp and - // pb/nb backends - // TODO(sangoly): refine this - template - T* GetBlock(int32_t idx); - - template - T* AddBlock(); - - virtual bool HasVersion() const = 0; - - virtual int64_t Version() const = 0; - - virtual void SetVersion(int64_t version) = 0; -}; - -} // namespace cpp -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/framework.proto b/paddle/infrt/paddle/framework.proto deleted file mode 100644 index 9cf82a763f3..00000000000 --- a/paddle/infrt/paddle/framework.proto +++ /dev/null @@ -1,213 +0,0 @@ -/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -syntax = "proto2"; -package paddle.framework.proto; - -// Any incompatible changes to ProgramDesc and its dependencies should -// raise the version defined version.h. -// -// Serailization and Deserialization codes should be modified in a way -// that supports old versions following the version and compatibility policy. -message Version { optional int64 version = 1 [ default = 0 ]; } - -enum AttrType { - INT = 0; - FLOAT = 1; - STRING = 2; - INTS = 3; - FLOATS = 4; - STRINGS = 5; - BOOLEAN = 6; - BOOLEANS = 7; - BLOCK = 8; - LONG = 9; - BLOCKS = 10; - LONGS = 11; -} - -// OpDesc describes an instance of a C++ framework::OperatorBase -// derived class type. -message OpDesc { - - message Attr { - required string name = 1; - required AttrType type = 2; - optional int32 i = 3; - optional float f = 4; - optional string s = 5; - repeated int32 ints = 6; - repeated float floats = 7; - repeated string strings = 8; - optional bool b = 10; - repeated bool bools = 11; - optional int32 block_idx = 12; - optional int64 l = 13; - repeated int32 blocks_idx = 14; - repeated int64 longs = 15; - }; - - message Var { - required string parameter = 1; - repeated string arguments = 2; - }; - - required string type = 3; - repeated Var inputs = 1; - repeated Var outputs = 2; - repeated Attr attrs = 4; - optional bool is_target = 5 [ default = false ]; -}; - -// OpProto describes a C++ framework::OperatorBase derived class. -message OpProto { - - // VarProto describes the C++ type framework::Variable. - message Var { - required string name = 1; - required string comment = 2; - - optional bool duplicable = 3 [ default = false ]; - optional bool intermediate = 4 [ default = false ]; - optional bool dispensable = 5 [ default = false ]; - } - - // AttrProto describes the C++ type Attribute. - message Attr { - required string name = 1; - required AttrType type = 2; - required string comment = 3; - // If that attribute is generated, it means the Paddle third - // language binding has responsibility to fill that - // attribute. End-User should not set that attribute. - optional bool generated = 4 [ default = false ]; - } - - required string type = 1; - repeated Var inputs = 2; - repeated Var outputs = 3; - repeated Attr attrs = 4; - required string comment = 5; -} - -message VarType { - enum Type { - // Pod Types - BOOL = 0; - INT16 = 1; - INT32 = 2; - INT64 = 3; - FP16 = 4; - FP32 = 5; - FP64 = 6; - // Tensor is used in C++. - SIZE_T = 19; - UINT8 = 20; - INT8 = 21; - - // Other types that may need additional descriptions - LOD_TENSOR = 7; - SELECTED_ROWS = 8; - FEED_MINIBATCH = 9; - FETCH_LIST = 10; - STEP_SCOPES = 11; - LOD_RANK_TABLE = 12; - LOD_TENSOR_ARRAY = 13; - PLACE_LIST = 14; - READER = 15; - // Any runtime decided variable type is raw - // raw variables should manage their own allocations - // in operators like nccl_op - RAW = 17; - TUPLE = 18; - } - - required Type type = 1; - - message TensorDesc { - // Should only be PODType. Is enforced in C++ - required Type data_type = 1; - repeated int64 dims = 2; // [UNK, 640, 480] is saved as [-1, 640, 480] - } - optional TensorDesc selected_rows = 2; - - message LoDTensorDesc { - required TensorDesc tensor = 1; - optional int32 lod_level = 2 [ default = 0 ]; - } - optional LoDTensorDesc lod_tensor = 3; - - message LoDTensorArrayDesc { - required TensorDesc tensor = 1; - optional int32 lod_level = 2 [ default = 0 ]; - } - optional LoDTensorArrayDesc tensor_array = 4; - - message ReaderDesc { repeated LoDTensorDesc lod_tensor = 1; } - optional ReaderDesc reader = 5; - - message Tuple { repeated Type element_type = 1; } - optional Tuple tuple = 7; -} - -message VarDesc { - required string name = 1; - required VarType type = 2; - optional bool persistable = 3 [ default = false ]; - // True if the variable is an input data and - // have to check the feed data shape and dtype - optional bool need_check_feed = 4 [ default = false ]; -} - -message BlockDesc { - required int32 idx = 1; - required int32 parent_idx = 2; - repeated VarDesc vars = 3; - repeated OpDesc ops = 4; - optional int32 forward_block_idx = 5 [ default = -1 ]; -} - -// CompatibleInfo is used to determine if a feature is compatible and -// provides the information. -message CompatibleInfo { - enum Type { - COMPATIBLE = 0; - DEFINITELY_NOT = 1; - POSSIBLE = 2; - BUG_FIX = 3; - PRECISION_CHANGE = 4; - } - required string version = 1; - required Type type = 2; -} - -// In some cases, Paddle Fluid may perform operator definition iterations, -// and the operator uses OpCompatibleMap for compatibility testing. -message OpCompatibleMap { - message OpCompatiblePair { - required string op_name = 1; - required CompatibleInfo compatible_info = 2; - } - repeated OpCompatiblePair pair = 1; - optional string default_required_version = 2; -} - -// Please refer to -// https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/program.md -// for more details. -// TODO(panyx0718): A model can have multiple programs. Need a -// way to distinguish them. Maybe ID or name? -message ProgramDesc { - reserved 2; // For backward compatibility. - repeated BlockDesc blocks = 1; - optional Version version = 4; - optional OpCompatibleMap op_compatible_map = 3; -} diff --git a/paddle/infrt/paddle/model_parser.cc b/paddle/infrt/paddle/model_parser.cc deleted file mode 100644 index 6fc358a4c04..00000000000 --- a/paddle/infrt/paddle/model_parser.cc +++ /dev/null @@ -1,269 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/paddle/model_parser.h" - -#include -#include - -#include "paddle/infrt/common/common.h" -#include "paddle/infrt/common/string.h" -#include "paddle/infrt/common/target.h" -#include "paddle/infrt/common/type.h" - -#ifdef INFRT_WITH_PHI -#include "paddle/phi/common/data_type.h" -#endif - -namespace infrt { -namespace paddle { - -int SizeOfType(framework_proto::VarType::Type type) { - using Type = framework_proto::VarType::Type; - switch (static_cast(type)) { -#define DO(desc, type) \ - case Type::VarType_Type_##desc: \ - return sizeof(type); - DO(BOOL, bool); - DO(FP16, float); - DO(FP32, float); - DO(INT8, int8_t); - DO(INT16, int16_t); - DO(INT32, int); - DO(INT64, int64_t); -#undef DO - default: - LOG(FATAL) << "unknown data type " << type; - } - return -1; -} - -void TensorFromStream(std::istream &is, - _Tensor_ *tensor, - const common::Target &target) { - using Type = framework_proto::VarType::Type; - uint32_t version; - is.read(reinterpret_cast(&version), sizeof(version)); - CHECK_EQ(version, 0U) << "Only version 0 is supported"; - // read tensor desc - framework_proto::VarType::TensorDesc desc; - { - // int32_t size - // proto buffer - int32_t size; - is.read(reinterpret_cast(&size), sizeof(size)); - std::unique_ptr buf(new char[size]); - is.read(reinterpret_cast(buf.get()), size); - CHECK(desc.ParseFromArray(buf.get(), size)) << "Cannot parse tensor desc"; - } - - // read tensor - std::vector dims_vec; - std::copy( - desc.dims().begin(), desc.dims().end(), std::back_inserter(dims_vec)); - Shape dims(dims_vec); - tensor->Resize(dims); - void *buf; - size_t size = tensor->shape().numel() * SizeOfType(desc.data_type()); - // alllocate memory - if (target.arch == Target::Arch::X86) { - switch (static_cast(desc.data_type())) { -#define SET_TENSOR(desc, type, precision) \ - case Type::VarType_Type_##desc: \ - buf = tensor->mutable_data(target); \ - tensor->set_type(precision); \ - break - - SET_TENSOR(FP32, float, Float(32)); - SET_TENSOR(INT8, int8_t, Int(8)); - SET_TENSOR(INT16, int16_t, Int(16)); - SET_TENSOR(INT32, int32_t, Int(32)); - SET_TENSOR(INT64, int64_t, Int(64)); -#undef SET_TENSOR - default: - LOG(FATAL) << "unknown type " << desc.data_type(); - } - // tensor->set_persistable(true); - is.read(static_cast(buf), size); - } else if (target.arch == Target::Arch::NVGPU) { -#ifdef INFRT_WITH_CUDA - if (desc.data_type() != Type::VarType_Type_FP32) - LOG(FATAL) << "[CUDA] The type is not fp32!!"; - auto *data = tensor->mutable_data(target); - tensor->set_type(infrt::common::Float(32)); - std::vector temp(tensor->shape().numel()); - // LOG(INFO) <<"[CUDA] The tensor's size is "<< tensor->shape().numel(); - is.read(reinterpret_cast(temp.data()), size); - CUDA_CALL(cudaMemcpy(reinterpret_cast(data), - temp.data(), - tensor->shape().numel() * sizeof(float), - cudaMemcpyHostToDevice)); -#else - LOG(FATAL) << "To use CUDA backends, you need to set WITH_CUDA ON!"; -#endif - } else { - INFRT_NOT_IMPLEMENTED - } -} - -void LoadLoDTensor(std::istream &is, _Variable *var, const Target &target) { - auto &tensor = var->get(); - uint32_t version{}; - is.read(reinterpret_cast(&version), sizeof(version)); - VLOG(3) << "model version " << version; - - // Load LoD information - uint64_t lod_level{}; - is.read(reinterpret_cast(&lod_level), sizeof(lod_level)); - - for (uint64_t i = 0; i < lod_level; ++i) { - uint64_t size; - is.read(reinterpret_cast(&size), sizeof(size)); - std::vector tmp(size / sizeof(uint64_t)); - is.read(reinterpret_cast(tmp.data()), - static_cast(size)); - // lod[i] = tmp; - } - - TensorFromStream(is, tensor.operator->(), target); -} - -void ReadBinaryFile(const std::string &filename, std::string *contents) { - std::ifstream fin(filename, std::ios::in | std::ios::binary); - CHECK(fin.is_open()) << "Cannot open file: " << filename; - fin.seekg(0, std::ios::end); - auto size = fin.tellg(); - contents->clear(); - contents->resize(size); - fin.seekg(0, std::ios::beg); - fin.read(&(contents->at(0)), contents->size()); - fin.close(); -} - -std::unique_ptr LoadProgram( - const std::string &path, bool program_from_memory) { - std::unique_ptr main_program( - new framework_proto::ProgramDesc); - if (!program_from_memory) { - std::string desc_str; - ReadBinaryFile(path, &desc_str); - main_program->ParseFromString(desc_str); - } else { - main_program->ParseFromString(path); - } - return main_program; -} - -void LoadParams(const std::string &path) {} - -// Load directly to CPU, and latter transfer to other devices. -void LoadParam(const std::string &path, _Variable *out, const Target &target) { - std::ifstream fin(path, std::ios::binary); - CHECK(fin.is_open()) << "failed to open file " << path; - LoadLoDTensor(fin, out, target); -} - -#ifdef INFRT_WITH_PHI -namespace framework_proto = ::paddle::framework::proto; - -inline ::phi::DataType PhiDataType(framework_proto::VarType::Type type) { - using Type = framework_proto::VarType::Type; - switch (static_cast(type)) { - case Type::VarType_Type_BOOL: - return ::phi::DataType::BOOL; - case Type::VarType_Type_INT8: - return ::phi::DataType::INT8; - case Type::VarType_Type_UINT8: - return ::phi::DataType::UINT8; - case Type::VarType_Type_INT16: - return ::phi::DataType::INT16; - case Type::VarType_Type_INT32: - return ::phi::DataType::INT32; - case Type::VarType_Type_INT64: - return ::phi::DataType::INT64; - case Type::VarType_Type_SIZE_T: - return ::phi::DataType::UINT64; - case Type::VarType_Type_FP16: - return ::phi::DataType::FLOAT16; - case Type::VarType_Type_FP32: - return ::phi::DataType::FLOAT32; - case Type::VarType_Type_FP64: - return ::phi::DataType::FLOAT64; - default: - LOG(FATAL) << "unknown data type " << type; - } - return ::phi::DataType::UNDEFINED; -} - -inline void TensorFromStream(std::istream &is, - ::Tensor *tensor, - const ::phi::CPUContext &ctx) { - uint32_t version; - is.read(reinterpret_cast(&version), sizeof(version)); - CHECK_EQ(version, 0U); - framework_proto::VarType::TensorDesc desc; - { // int32_t size - // proto buffer - int32_t size = -1; - is.read(reinterpret_cast(&size), sizeof(size)); - CHECK_EQ(is.good(), true); - CHECK_GE(size, 0); - std::unique_ptr buf(new char[size]); - is.read(reinterpret_cast(buf.get()), size); - CHECK_EQ(desc.ParseFromArray(buf.get(), size), true); - } - { // read tensor - std::vector dims; - dims.reserve(static_cast(desc.dims().size())); - std::copy(desc.dims().begin(), desc.dims().end(), std::back_inserter(dims)); - tensor->Resize(::phi::make_ddim(dims)); - void *buf; - size_t size = tensor->numel() * SizeOfType(desc.data_type()); - ctx.HostAlloc(tensor, PhiDataType(desc.data_type()), size); - buf = tensor->data(); - is.read(static_cast(buf), size); - } -} - -void DeserializeFromStream(std::istream &is, - ::Tensor *tensor, - const ::phi::CPUContext &dev_ctx) { - { - // the 1st field, unit32_t version for LoDTensor - uint32_t version; - is.read(reinterpret_cast(&version), sizeof(version)); - CHECK_EQ(version, 0U); - } - { - // the 2st field, LoD information - uint64_t lod_level; - is.read(reinterpret_cast(&lod_level), sizeof(lod_level)); - auto &lod = *tensor->mutable_lod(); - lod.resize(lod_level); - for (uint64_t i = 0; i < lod_level; ++i) { - uint64_t size; - is.read(reinterpret_cast(&size), sizeof(size)); - std::vector tmp(size / sizeof(size_t)); - is.read(reinterpret_cast(tmp.data()), - static_cast(size)); - lod[i] = tmp; - } - } - // the 3st filed, Tensor - TensorFromStream(is, tensor, dev_ctx); -} -#endif - -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/model_parser.h b/paddle/infrt/paddle/model_parser.h deleted file mode 100644 index 39af7a91931..00000000000 --- a/paddle/infrt/paddle/model_parser.h +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include -#include - -#include "paddle/infrt/paddle/framework.pb.h" -#include "paddle/infrt/paddle/pb/block_desc.h" -#include "paddle/infrt/paddle/pb/op_desc.h" -#include "paddle/infrt/paddle/pb/program_desc.h" -#include "paddle/infrt/paddle/scope.h" -#include "paddle/infrt/paddle/tensor.h" - -#ifdef INFRT_WITH_PHI -#include "paddle/phi/backends/cpu/cpu_context.h" -#include "paddle/phi/core/dense_tensor.h" -#endif - -namespace infrt { -namespace paddle { -namespace framework_proto = ::paddle::framework::proto; - -// Read a __model__ file. -std::unique_ptr LoadProgram( - const std::string& path, bool program_from_memory = false); - -void LoadLoDTensor(std::istream& is, - _Variable* var, - const common::Target& target); - -// Read a single file containing all the parameters. -void LoadParams(const std::string& path); - -// Load a single parameter to an output tensor. -void LoadParam(const std::string& path, - _Variable* out, - const common::Target& target); - -// LoDTensor to ostream -void TensorToStream(std::ostream& os, const _Tensor_& tensor); -void TensorFromStream( - std::istream& is, - _Tensor_* tensor, - const common::Target& target = common::DefaultHostTarget()); -void ReadBinaryFile(const std::string& filename, std::string* contents); - -#ifdef INFRT_WITH_PHI -void DeserializeFromStream(std::istream& is, - ::Tensor* tensor, - const ::phi::CPUContext& dev_ctx); -#endif - -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/pb/CMakeLists.txt b/paddle/infrt/paddle/pb/CMakeLists.txt deleted file mode 100644 index 3614201a95f..00000000000 --- a/paddle/infrt/paddle/pb/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -gather_srcs(infrt_src SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc) - -file( - GLOB includes - LIST_DIRECTORIES false - RELATIVE ${CMAKE_SOURCE_DIR} - *.h) - -foreach(header ${includes}) - set(core_includes - "${core_includes};${header}" - CACHE INTERNAL "") -endforeach() diff --git a/paddle/infrt/paddle/pb/block_desc.cc b/paddle/infrt/paddle/pb/block_desc.cc deleted file mode 100644 index 5b28fa5464c..00000000000 --- a/paddle/infrt/paddle/pb/block_desc.cc +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/paddle/pb/block_desc.h" - -namespace infrt { -namespace paddle { -namespace pb { - -template <> -framework_proto::VarDesc* BlockDesc::GetVar( - int32_t idx) { - CHECK_LT(idx, static_cast(VarsSize())) << "idx >= vars.size()"; - return desc_->mutable_vars(idx); -} - -template <> -framework_proto::VarDesc* BlockDesc::AddVar() { - return desc_->add_vars(); -} - -template <> -framework_proto::OpDesc* BlockDesc::GetOp( - int32_t idx) { - CHECK_LT(idx, static_cast(OpsSize())) << "idx >= ops.size()"; - return desc_->mutable_ops(idx); -} - -template <> -framework_proto::OpDesc* BlockDesc::AddOp() { - return desc_->add_ops(); -} - -} // namespace pb -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/pb/block_desc.h b/paddle/infrt/paddle/pb/block_desc.h deleted file mode 100644 index c9e325699a4..00000000000 --- a/paddle/infrt/paddle/pb/block_desc.h +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -#include "paddle/infrt/paddle/cpp/desc_api.h" -#include "paddle/infrt/paddle/framework.pb.h" - -namespace infrt { -namespace paddle { -namespace pb { - -namespace framework_proto = ::paddle::framework::proto; - -class BlockDesc : public cpp::BlockDescAPI { - public: - BlockDesc() = delete; - - explicit BlockDesc(framework_proto::BlockDesc* desc) : desc_(desc) { - CHECK(desc_); - } - - framework_proto::BlockDesc* Proto() { return desc_; } - - const framework_proto::BlockDesc& ReadonlyProto() const { return *desc_; } - - int32_t Idx() const override { return desc_->idx(); } - - void SetIdx(int32_t idx) override { desc_->set_idx(idx); } - - int32_t ParentIdx() const override { return desc_->parent_idx(); } - - void SetParentIdx(int32_t idx) override { desc_->set_parent_idx(idx); } - - size_t VarsSize() const override { return desc_->vars_size(); } - - void ClearVars() override { desc_->clear_vars(); } - - template - T* GetVar(int32_t idx); - - template - T* AddVar(); - - size_t OpsSize() const override { return desc_->ops_size(); } - - void ClearOps() override { desc_->clear_ops(); } - - template - T* GetOp(int32_t idx); - - template - T* AddOp(); - - int32_t ForwardBlockIdx() const override { - return desc_->forward_block_idx(); - } - - void SetForwardBlockIdx(int32_t idx) override { - desc_->set_forward_block_idx(idx); - } - - private: - framework_proto::BlockDesc* desc_; // not_own -}; - -} // namespace pb -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/pb/op_desc.cc b/paddle/infrt/paddle/pb/op_desc.cc deleted file mode 100644 index 32dcefb1ac6..00000000000 --- a/paddle/infrt/paddle/pb/op_desc.cc +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/paddle/pb/op_desc.h" - -namespace infrt { -namespace paddle { -namespace pb { - -google::protobuf::internal::RepeatedPtrIterator -FindAttr(framework_proto::OpDesc *desc, const std::string &name) { - auto &xs = *desc->mutable_attrs(); - auto it = std::find_if( - xs.begin(), xs.end(), [&](const framework_proto::OpDesc_Attr &x) { - return x.name() == name; - }); - if (it == xs.end()) { - auto *attr = xs.Add(); - attr->set_name(name); - it = std::find_if( - xs.begin(), xs.end(), [&](const framework_proto::OpDesc_Attr &x) { - return x.name() == name; - }); - } - return it; -} - -#define SET_IMPL_ONE(T, ty__, pb_f__) \ - template <> \ - void OpDesc::SetAttr(const std::string &name, const T &v) { \ - auto it = FindAttr(desc_, name); \ - it->set_type(framework_proto::ty__); \ - it->set_##pb_f__(v); \ - } -SET_IMPL_ONE(int, INT, i); -SET_IMPL_ONE(float, FLOAT, f); -SET_IMPL_ONE(bool, BOOLEAN, b); -SET_IMPL_ONE(int64_t, LONG, l); - -template <> -void OpDesc::SetAttr>(const std::string &name, - const std::vector &v) { - auto it = FindAttr(desc_, name); - it->set_type(framework_proto::INTS); - it->clear_ints(); - for (auto &i : v) { - it->add_ints(i); - } -} - -template <> -void OpDesc::SetAttr(const std::string &name, - const std::string &v) { - auto it = FindAttr(desc_, name); - it->set_type(framework_proto::STRING); - it->set_s(v.c_str()); -} - -template <> -void OpDesc::SetAttr>(const std::string &name, - const std::vector &v) { - auto it = FindAttr(desc_, name); - it->set_type(framework_proto::FLOATS); - it->clear_floats(); - for (auto &i : v) { - it->add_floats(i); - } -} - -template <> -void OpDesc::SetAttr>( - const std::string &name, const std::vector &v) { - auto it = FindAttr(desc_, name); - it->set_type(framework_proto::STRINGS); - it->clear_strings(); - for (auto &i : v) { - it->add_strings(i); - } -} - -template <> -void OpDesc::SetAttr>(const std::string &name, - const std::vector &v) { - auto it = FindAttr(desc_, name); - it->set_type(framework_proto::LONGS); - it->clear_longs(); - for (auto &i : v) { - it->add_longs(i); - } -} -google::protobuf::internal::RepeatedPtrIterator< - const framework_proto::OpDesc_Attr> -GetFindAttr(const framework_proto::OpDesc &desc, const std::string &name) { - auto &xs = desc.attrs(); - auto it = std::find_if( - xs.begin(), xs.end(), [&](const framework_proto::OpDesc_Attr &x) { - return x.name() == name; - }); - return it; -} - -#define GET_ATTR_IMPL(T, pb_f__) \ - template <> \ - T OpDesc::GetAttr(const std::string &name) const { \ - auto it = GetFindAttr(*desc_, name); \ - return it->pb_f__(); \ - } - -#define GET_ATTRS_IMPL(T, pb_f__) \ - template <> \ - T OpDesc::GetAttr(const std::string &name) const { \ - auto it = GetFindAttr(*desc_, name); \ - T res; \ - for (const auto &v : it->pb_f__()) { \ - res.push_back(v); \ - } \ - return res; \ - } -GET_ATTR_IMPL(int32_t, i); -GET_ATTR_IMPL(int16_t, block_idx); -GET_ATTR_IMPL(float, f); -GET_ATTR_IMPL(bool, b); -GET_ATTR_IMPL(int64_t, l); -GET_ATTRS_IMPL(std::vector, ints); -GET_ATTRS_IMPL(std::vector, floats); -GET_ATTRS_IMPL(std::vector, strings); -GET_ATTR_IMPL(std::string, s); -GET_ATTRS_IMPL(std::vector, longs); - -} // namespace pb -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/pb/op_desc.h b/paddle/infrt/paddle/pb/op_desc.h deleted file mode 100644 index 2829f2aca2e..00000000000 --- a/paddle/infrt/paddle/pb/op_desc.h +++ /dev/null @@ -1,202 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -#include "paddle/infrt/paddle/cpp/desc_api.h" -#include "paddle/infrt/paddle/framework.pb.h" -#include "paddle/infrt/support/variant.h" - -namespace infrt { -namespace paddle { -namespace pb { - -namespace framework_proto = ::paddle::framework::proto; - -using Attribute = - Variant, std::vector>; -using VariableNameMap = std::map>; - -/* - * The lite::OpDesc, an light-weight implementation of wrapper of proto::OpDesc. - * Unlike the original one in framework::OpDesc, we remove the local members - * except the desc_, to avoid the inconsistent state, which is normal in the - * original interface and results in bugs. - */ -class OpDesc : public cpp::OpDescAPI { - public: - OpDesc() = delete; - - explicit OpDesc(framework_proto::OpDesc *desc) : desc_(desc) { CHECK(desc_); } - - framework_proto::OpDesc *Proto() { return desc_; } - const framework_proto::OpDesc &ReadonlyProto() const { return *desc_; } - - std::string Type() const override { return desc_->type(); } - - void SetType(const std::string &type) override { desc_->set_type(type); } - - // Get the arguments of parameter called `param` - std::vector Input(const std::string ¶m) const override { - return GetArguments(desc_->inputs(), param); - } - - std::vector InputArgumentNames() const override { - return GetArgumentNames(desc_->inputs()); - } - - void SetInput(const std::string ¶m, - const std::vector &args) override { - SetArgument(desc_->mutable_inputs(), param, args); - } - - std::vector Output(const std::string ¶m) const override { - return GetArguments(desc_->outputs(), param); - } - - std::vector OutputArgumentNames() const override { - return GetArgumentNames(desc_->outputs()); - } - - void SetOutput(const std::string ¶m, - const std::vector &args) override { - SetArgument(desc_->mutable_outputs(), param, args); - } - - bool HasAttr(const std::string &name) const override { - const auto &xs = desc_->attrs(); - auto it = std::find_if( - xs.begin(), xs.end(), [&](const framework_proto::OpDesc_Attr &x) { - return x.name() == name; - }); - return it != xs.end(); - } - - AttrType GetAttrType(const std::string &name) const override { - const auto &xs = desc_->attrs(); - auto it = std::find_if( - xs.begin(), xs.end(), [&](const framework_proto::OpDesc_Attr &x) { - return x.name() == name; - }); - CHECK(it != xs.end()); -#define DEF_ONE(type__) \ - case framework_proto::AttrType::type__: \ - return AttrType::type__; - - switch (it->type()) { - DEF_ONE(INT); - DEF_ONE(FLOAT); - DEF_ONE(STRING); - DEF_ONE(INTS); - DEF_ONE(FLOATS); - DEF_ONE(STRINGS); - DEF_ONE(BOOLEAN); - DEF_ONE(BOOLEANS); - DEF_ONE(BLOCK); - DEF_ONE(LONG); - DEF_ONE(BLOCKS); - DEF_ONE(LONGS); - default: - LOG(FATAL) << "Unknown attribute type"; - return static_cast(-1); - } -#undef DEF_ONE - } - - std::vector AttrNames() const override { - std::vector res; - const auto &xs = desc_->attrs(); - std::transform( - xs.begin(), - xs.end(), - std::back_inserter(res), - [](const framework_proto::OpDesc_Attr &x) { return x.name(); }); - return res; - } - - template - void SetAttr(const std::string &name, const T &v); - - template - T GetAttr(const std::string &name) const; - - private: - std::vector GetArguments( - const google::protobuf::RepeatedPtrField &xs, - const std::string ¶m) const { - std::vector res; - auto it = std::find_if( - xs.begin(), xs.end(), [&](const framework_proto::OpDesc_Var &it) { - return it.parameter() == param; - }); - CHECK(it != xs.end()); - - const auto &ys = it->arguments(); - std::transform(ys.begin(), - ys.end(), - std::back_inserter(res), - [](const std::string &x) { return x; }); - return res; - } - - void SetArgument( - google::protobuf::RepeatedPtrField *xs, - const std::string ¶m, - const std::vector &args) { - auto it = std::find_if( - xs->begin(), xs->end(), [&](const framework_proto::OpDesc_Var &it) { - return it.parameter() == param; - }); - if (it == xs->end()) { - auto *new_arg = xs->Add(); - new_arg->set_parameter(param); - for (const auto &arg : args) { - *new_arg->mutable_arguments()->Add() = arg; - } - } else { - it->mutable_arguments()->Clear(); - for (const auto &arg : args) { - *it->mutable_arguments()->Add() = arg; - } - } - } - - std::vector GetArgumentNames( - const google::protobuf::RepeatedPtrField &xs) - const { - std::vector res; - std::transform( - xs.begin(), - xs.end(), - std::back_inserter(res), - [](const framework_proto::OpDesc_Var &x) { return x.parameter(); }); - return res; - } - - private: - framework_proto::OpDesc *desc_; -}; - -template <> -void OpDesc::SetAttr(const std::string &name, - const std::string &v); - -template <> -void OpDesc::SetAttr>(const std::string &name, - const std::vector &v); - -} // namespace pb -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/pb/program_desc.cc b/paddle/infrt/paddle/pb/program_desc.cc deleted file mode 100644 index 9d725485a97..00000000000 --- a/paddle/infrt/paddle/pb/program_desc.cc +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/paddle/pb/program_desc.h" - -#include -#include - -namespace infrt { -namespace paddle { -namespace pb { - -template <> -framework_proto::BlockDesc* ProgramDesc::GetBlock( - int32_t idx) { - CHECK_LT(idx, static_cast(BlocksSize())) << "idx >= blocks.size()"; - return desc_->mutable_blocks(idx); -} - -template <> -framework_proto::BlockDesc* -ProgramDesc::AddBlock() { - return desc_->add_blocks(); -} - -} // namespace pb -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/pb/program_desc.h b/paddle/infrt/paddle/pb/program_desc.h deleted file mode 100644 index b1e64f8e866..00000000000 --- a/paddle/infrt/paddle/pb/program_desc.h +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include - -#include -#include - -#include "paddle/infrt/paddle/cpp/desc_api.h" -#include "paddle/infrt/paddle/framework.pb.h" - -namespace infrt { -namespace paddle { -namespace pb { -namespace framework_proto = ::paddle::framework::proto; - -class ProgramDesc : public cpp::ProgramDescAPI { - public: - ProgramDesc() = delete; - - explicit ProgramDesc(framework_proto::ProgramDesc *desc) : desc_(desc) { - CHECK(desc_); - } - - framework_proto::ProgramDesc *Proto() { return desc_; } - - const framework_proto::ProgramDesc &ReadonlyProto() const { return *desc_; } - - size_t BlocksSize() const override { return desc_->blocks_size(); } - - void ClearBlocks() override { desc_->clear_blocks(); } - - template - T *GetBlock(int32_t idx); - - template - T *AddBlock(); - - bool HasVersion() const override { return desc_->has_version(); } - - int64_t Version() const override { return desc_->version().version(); } - - void SetVersion(int64_t version) override { - desc_->mutable_version()->set_version(version); - } - - private: - framework_proto::ProgramDesc *desc_; // not_own -}; - -} // namespace pb -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/pb/var_desc.cc b/paddle/infrt/paddle/pb/var_desc.cc deleted file mode 100644 index 7ea2e24da34..00000000000 --- a/paddle/infrt/paddle/pb/var_desc.cc +++ /dev/null @@ -1,371 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/paddle/pb/var_desc.h" - -#include - -#include "paddle/infrt/paddle/cpp/desc_api.h" -#include "paddle/infrt/paddle/framework.pb.h" - -namespace infrt { -namespace paddle { -namespace pb { - -cpp::VarDescAPI::Type VarDesc::GetType() const { - auto type = desc_->type().type(); - -#define GET_TYPE_CASE_ITEM(type__) \ - case framework_proto::VarType::type__: \ - return cpp::VarDescAPI::Type::type__; - - switch (type) { - GET_TYPE_CASE_ITEM(LOD_TENSOR); - GET_TYPE_CASE_ITEM(LOD_TENSOR_ARRAY); - GET_TYPE_CASE_ITEM(LOD_RANK_TABLE); - GET_TYPE_CASE_ITEM(SELECTED_ROWS); - GET_TYPE_CASE_ITEM(FEED_MINIBATCH); - GET_TYPE_CASE_ITEM(FETCH_LIST); - GET_TYPE_CASE_ITEM(STEP_SCOPES); - GET_TYPE_CASE_ITEM(PLACE_LIST); - GET_TYPE_CASE_ITEM(READER); - default: - LOG(FATAL) << "Unknown var type"; - return VarDescAPI::Type(); - } -#undef GET_TYPE_CASE_ITEM -} - -void VarDesc::SetType(VarDescAPI::Type type) { -#define SET_TYPE_CASE_ITEM(type__) \ - case VarDescAPI::Type::type__: \ - desc_->mutable_type()->set_type(framework_proto::VarType::type__); \ - break; - - switch (type) { - SET_TYPE_CASE_ITEM(LOD_TENSOR); - SET_TYPE_CASE_ITEM(LOD_TENSOR_ARRAY); - SET_TYPE_CASE_ITEM(LOD_RANK_TABLE); - SET_TYPE_CASE_ITEM(SELECTED_ROWS); - SET_TYPE_CASE_ITEM(FEED_MINIBATCH); - SET_TYPE_CASE_ITEM(FETCH_LIST); - SET_TYPE_CASE_ITEM(STEP_SCOPES); - SET_TYPE_CASE_ITEM(PLACE_LIST); - SET_TYPE_CASE_ITEM(READER); - default: - LOG(FATAL) << "Unknown var type"; - } -#undef SET_TYPE_CASE_ITEM -} - -void VarDesc::SetShape(const std::vector &dims) { - VectorToRepeated(dims, mutable_tensor_desc()->mutable_dims()); -} - -void VarDesc::SetTensorDescNum(size_t num) { - switch (desc_->type().type()) { - case framework_proto::VarType::READER: { - auto *lod_tensors_ptr = - desc_->mutable_type()->mutable_reader()->mutable_lod_tensor(); - lod_tensors_ptr->Clear(); - for (size_t i = 0; i < num; ++i) { - lod_tensors_ptr->Add(); - } - return; - } break; - default: - LOG(FATAL) << "Setting 'sub_tensor_number' is not supported by the type " - "of var %s." - << this->Name(); - } -} - -size_t VarDesc::GetTensorDescNum() const { - switch (desc_->type().type()) { - case framework_proto::VarType::READER: - return desc_->type().reader().lod_tensor_size(); - break; - default: - LOG(FATAL) << "Getting 'sub_tensor_number' is not supported by the type " - "of var %s." - << this->Name(); - } - return 0; -} - -void VarDesc::SetShapes( - const std::vector> &multiple_dims) { - if (multiple_dims.size() != GetTensorDescNum()) { - VLOG(3) << "WARNING: The number of given shapes(" << multiple_dims.size() - << ") doesn't match the existing tensor number(" - << GetTensorDescNum() - << "). The Reader is going to be reinitialized."; - SetTensorDescNum(multiple_dims.size()); - } - std::vector tensors = - mutable_tensor_descs(); - for (size_t i = 0; i < multiple_dims.size(); ++i) { - VectorToRepeated(multiple_dims[i], tensors[i]->mutable_dims()); - } -} - -std::vector VarDesc::GetShape() const { - return RepeatedToVector(tensor_desc().dims()); -} - -std::vector> VarDesc::GetShapes() const { - std::vector descs = tensor_descs(); - std::vector> res; - res.reserve(descs.size()); - for (const auto &tensor_desc : descs) { - res.push_back(RepeatedToVector(tensor_desc.dims())); - } - return res; -} - -void VarDesc::SetDataType(VarDescAPI::VarDataType data_type) { -#define SET_DATA_TYPE_CASE_ITEM(type__) \ - case cpp::VarDescAPI::Type::type__: \ - mutable_tensor_desc()->set_data_type(framework_proto::VarType::type__); \ - break; - - switch (data_type) { - SET_DATA_TYPE_CASE_ITEM(BOOL); - SET_DATA_TYPE_CASE_ITEM(SIZE_T); - SET_DATA_TYPE_CASE_ITEM(UINT8); - SET_DATA_TYPE_CASE_ITEM(INT8); - SET_DATA_TYPE_CASE_ITEM(INT16); - SET_DATA_TYPE_CASE_ITEM(INT32); - SET_DATA_TYPE_CASE_ITEM(INT64); - SET_DATA_TYPE_CASE_ITEM(FP16); - SET_DATA_TYPE_CASE_ITEM(FP32); - SET_DATA_TYPE_CASE_ITEM(FP64); - default: - LOG(FATAL) << "Unknown var type: " << static_cast(data_type); - } -#undef SET_DATA_TYPE_CASE_ITEM -} - -void VarDesc::SetDataTypes( - const std::vector &multiple_data_type) { - if (multiple_data_type.size() != GetTensorDescNum()) { - VLOG(3) << "WARNING: The number of given data types(" - << multiple_data_type.size() - << ") doesn't match the existing tensor number(" - << GetTensorDescNum() - << "). The Reader is going to be reinitialized."; - SetTensorDescNum(multiple_data_type.size()); - } - std::vector tensor_descs = - mutable_tensor_descs(); - for (size_t i = 0; i < multiple_data_type.size(); ++i) { - tensor_descs[i]->set_data_type(multiple_data_type[i]); - } -} - -// proto::VarType::Type VarDesc::GetDataType() const { -// return tensor_desc().data_type(); -// } -cpp::VarDescAPI::VarDataType VarDesc::GetDataType() const { - CHECK(desc_->has_type()) << "The var's type hasn't been set."; - CHECK(desc_->type().has_type()) << "The var type hasn't been set."; - if (desc_->type().type() != framework_proto::VarType::LOD_TENSOR) { - return VarDescAPI::Type(); - } - auto type = tensor_desc().data_type(); -#define GET_DATA_TYPE_CASE_ITEM(type__) \ - case framework_proto::VarType::Type::VarType_Type_##type__: \ - return VarDescAPI::Type::type__ - - switch (type) { - GET_DATA_TYPE_CASE_ITEM(BOOL); - GET_DATA_TYPE_CASE_ITEM(SIZE_T); - GET_DATA_TYPE_CASE_ITEM(UINT8); - GET_DATA_TYPE_CASE_ITEM(INT8); - GET_DATA_TYPE_CASE_ITEM(INT16); - GET_DATA_TYPE_CASE_ITEM(INT32); - GET_DATA_TYPE_CASE_ITEM(INT64); - GET_DATA_TYPE_CASE_ITEM(FP16); - GET_DATA_TYPE_CASE_ITEM(FP32); - GET_DATA_TYPE_CASE_ITEM(FP64); - default: - LOG(FATAL) << "Unknown var type: " << static_cast(type); - return VarDescAPI::Type(); - } -#undef GET_DATA_TYPE_CASE_ITEM -} - -std::vector VarDesc::GetDataTypes() const { - std::vector descs = tensor_descs(); - std::vector res; - res.reserve(descs.size()); - for (const auto &tensor_desc : descs) { - res.push_back(tensor_desc.data_type()); - } - return res; -} - -void VarDesc::SetLoDLevel(int32_t lod_level) { - switch (desc_->type().type()) { - case framework_proto::VarType::LOD_TENSOR: - desc_->mutable_type()->mutable_lod_tensor()->set_lod_level(lod_level); - break; - case framework_proto::VarType::LOD_TENSOR_ARRAY: - desc_->mutable_type()->mutable_tensor_array()->set_lod_level(lod_level); - break; - default: - LOG(FATAL) - << "Setting 'lod_level' is not supported by the type of var %s." - << this->Name(); - } -} - -void VarDesc::SetLoDLevels(const std::vector &multiple_lod_level) { - if (multiple_lod_level.size() != GetTensorDescNum()) { - VLOG(3) << "WARNING: The number of given lod_levels(" - << multiple_lod_level.size() - << ") doesn't match the existing tensor number(" - << GetTensorDescNum() - << "). The Reader is going to be reinitialized."; - SetTensorDescNum(multiple_lod_level.size()); - } - switch (desc_->type().type()) { - case framework_proto::VarType::READER: { - size_t i = 0; - for (auto &lod_tensor : - *desc_->mutable_type()->mutable_reader()->mutable_lod_tensor()) { - lod_tensor.set_lod_level(multiple_lod_level[i++]); - } - } break; - default: - LOG(FATAL) - << "Setting 'lod_levels' is not supported by the type of var %s." - << this->Name(); - } -} - -int32_t VarDesc::GetLoDLevel() const { - switch (desc_->type().type()) { - case framework_proto::VarType::LOD_TENSOR: - return desc_->type().lod_tensor().lod_level(); - case framework_proto::VarType::LOD_TENSOR_ARRAY: - return desc_->type().tensor_array().lod_level(); - default: - LOG(FATAL) - << "Getting 'lod_level' is not supported by the type of var %s." - << this->Name(); - } - return 0; -} - -std::vector VarDesc::GetLoDLevels() const { - std::vector res; - switch (desc_->type().type()) { - case framework_proto::VarType::READER: - res.reserve(desc_->type().reader().lod_tensor_size()); - for (auto &lod_tensor : desc_->type().reader().lod_tensor()) { - res.push_back(lod_tensor.lod_level()); - } - return res; - break; - default: - LOG(FATAL) - << "Getting 'lod_levels' is not supported by the type of var %s." - << this->Name(); - } - return std::vector(); -} - -const framework_proto::VarType::TensorDesc &VarDesc::tensor_desc() const { - CHECK(desc_->has_type()) << "The var's type hasn't been set."; - CHECK(desc_->type().has_type()) << "The var type hasn't been set."; - switch (desc_->type().type()) { - case framework_proto::VarType::SELECTED_ROWS: - return desc_->type().selected_rows(); - case framework_proto::VarType::LOD_TENSOR: - return desc_->type().lod_tensor().tensor(); - case framework_proto::VarType::LOD_TENSOR_ARRAY: - return desc_->type().tensor_array().tensor(); - default: - LOG(FATAL) - << "Getting 'tensor_desc' is not supported by the type of var %s." - << this->Name(); - } - return framework_proto::VarDesc().type().lod_tensor().tensor(); -} - -std::vector VarDesc::tensor_descs() - const { - CHECK(desc_->has_type()) << "The var type hasn't been set."; - std::vector res; - res.reserve(GetTensorDescNum()); - switch (desc_->type().type()) { - case framework_proto::VarType::READER: - for (const auto &lod_tensor : desc_->type().reader().lod_tensor()) { - res.push_back(lod_tensor.tensor()); - } - return res; - default: - LOG(FATAL) - << "Getting 'tensor_descs' is not supported by the type of var " - "%s." - << this->Name(); - } - return std::vector(); -} - -framework_proto::VarType::TensorDesc *VarDesc::mutable_tensor_desc() { - CHECK(desc_->has_type()) << "The var type hasn't been set."; - CHECK(desc_->type().has_type()) << "The var type hasn't been set."; - switch (desc_->type().type()) { - case framework_proto::VarType::SELECTED_ROWS: - return desc_->mutable_type()->mutable_selected_rows(); - case framework_proto::VarType::LOD_TENSOR: - return desc_->mutable_type()->mutable_lod_tensor()->mutable_tensor(); - case framework_proto::VarType::LOD_TENSOR_ARRAY: - return desc_->mutable_type()->mutable_tensor_array()->mutable_tensor(); - default: - LOG(FATAL) << "Getting 'mutable_tensor_desc' is not supported by the " - "type of var " - "%s." - << this->Name(); - } - return nullptr; -} - -std::vector -VarDesc::mutable_tensor_descs() { - CHECK(desc_->has_type()) << "The var type hasn't been set."; - CHECK(desc_->type().has_type()) << "The var type hasn't been set."; - std::vector res; - res.reserve(GetTensorDescNum()); - switch (desc_->type().type()) { - case framework_proto::VarType::READER: - for (auto &lod_tensor : - *desc_->mutable_type()->mutable_reader()->mutable_lod_tensor()) { - res.push_back(lod_tensor.mutable_tensor()); - } - return res; - default: - LOG(FATAL) - << "Getting 'tensor_descs' is not supported by the type of var " - "%s." - << this->Name(); - } - return std::vector(); -} - -} // namespace pb -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/pb/var_desc.h b/paddle/infrt/paddle/pb/var_desc.h deleted file mode 100644 index 7215ba6bb6a..00000000000 --- a/paddle/infrt/paddle/pb/var_desc.h +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include - -#include -#include -#include - -#include "paddle/infrt/paddle/cpp/desc_api.h" -#include "paddle/infrt/paddle/framework.pb.h" - -namespace infrt { -namespace paddle { -namespace pb { -namespace framework_proto = ::paddle::framework::proto; - -// convert between std::vector and protobuf repeated. -template -inline std::vector RepeatedToVector( - const google::protobuf::RepeatedField &repeated_field) { - std::vector ret; - ret.reserve(repeated_field.size()); - std::copy( - repeated_field.begin(), repeated_field.end(), std::back_inserter(ret)); - return ret; -} - -template -inline void VectorToRepeated(const std::vector &vec, - RepeatedField *repeated_field) { - repeated_field->Clear(); - repeated_field->Reserve(vec.size()); - for (const auto &elem : vec) { - *repeated_field->Add() = elem; - } -} - -// Specialize vector. -template -inline void VectorToRepeated(const std::vector &vec, - RepeatedField *repeated_field) { - repeated_field->Clear(); - repeated_field->Reserve(vec.size()); - for (auto elem : vec) { - *repeated_field->Add() = elem; - } -} - -class VarDesc : public cpp::VarDescAPI { - public: - VarDesc() = delete; - - explicit VarDesc(framework_proto::VarDesc *desc) : desc_(desc) { - CHECK(desc_); - } - - ::paddle::framework::proto::VarDesc *Proto() { return desc_; } - const framework_proto::VarDesc &ReadonlyProto() const { return *desc_; } - - std::string Name() const override { return desc_->name(); } - - void SetName(std::string name) override { desc_->set_name(name); } - - void SetTensorDescNum(size_t num); - - size_t GetTensorDescNum() const; - - void SetShape(const std::vector &dims); - - void SetShapes(const std::vector> &multiple_dims); - - std::vector GetShape() const; - - std::vector> GetShapes() const; - - void SetDataType(VarDescAPI::VarDataType data_type); - - void SetDataTypes( - const std::vector &multiple_data_type); - - VarDescAPI::VarDataType GetDataType() const; - - std::vector GetDataTypes() const; - - void SetLoDLevel(int32_t lod_level); - - void SetLoDLevels(const std::vector &multiple_lod_level); - - int32_t GetLoDLevel() const; - - std::vector GetLoDLevels() const; - - VarDescAPI::Type GetType() const override; - - void SetType(VarDescAPI::Type type) override; - - bool Persistable() const override { return desc_->persistable(); } - - void SetPersistable(bool persistable) override { - desc_->set_persistable(persistable); - } - - private: - const framework_proto::VarType::TensorDesc &tensor_desc() const; - std::vector tensor_descs() const; - framework_proto::VarType::TensorDesc *mutable_tensor_desc(); - std::vector mutable_tensor_descs(); - - framework_proto::VarDesc *desc_; -}; - -} // namespace pb -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/scope.cc b/paddle/infrt/paddle/scope.cc deleted file mode 100644 index d7bab9f7495..00000000000 --- a/paddle/infrt/paddle/scope.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/paddle/scope.h" - -#include "paddle/infrt/common/common.h" - -namespace infrt { -namespace paddle { - -_Variable* Scope::FindVar(const std::string& name) const { - auto it = data_.find(name); - if (it != data_.end()) return it->second.get(); - return nullptr; -} - -Tensor Scope::GetTensor(const std::string& name) const { - CheckVarNameValid(name); - auto* var = FindVar(name); - CHECK(var) << "No variable called [" << name << "] found"; - return var->get(); -} - -std::vector Scope::var_names() const { - std::vector names; - for (auto& item : data_) { - names.push_back(item.first); - } - return names; -} - -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/scope.h b/paddle/infrt/paddle/scope.h deleted file mode 100644 index 1f81d0914df..00000000000 --- a/paddle/infrt/paddle/scope.h +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once -#include -#include -#include -#include - -#include "paddle/infrt/common/macros.h" -#include "paddle/infrt/paddle/tensor.h" -#include "paddle/infrt/support/variant.h" - -namespace infrt { -namespace paddle { - -using _Variable = Variant; - -struct _Tensor_; - -class Scope { - public: - static std::shared_ptr Create() { return std::make_shared(); } - - //! Get or create a variable. - template - _Variable* Var(const std::string& name); - - //! Find a variable, get null if not exists. - _Variable* FindVar(const std::string& name) const; - - Tensor GetTensor(const std::string& name) const; - - //! Get variable names. - std::vector var_names() const; - - Scope() = default; - - private: - std::unordered_map> data_; - - INFRT_DISALLOW_COPY_AND_ASSIGN(Scope); -}; - -template -_Variable* Scope::Var(const std::string& name) { - VLOG(4) << "Scope insert Var [" << name << "]"; - _Variable* x = FindVar(name); - if (x) return x; - auto* data = new _Variable(T()); - data_[name].reset(data); - return data; -} - -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/tensor.cc b/paddle/infrt/paddle/tensor.cc deleted file mode 100644 index 072701ee907..00000000000 --- a/paddle/infrt/paddle/tensor.cc +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "paddle/infrt/paddle/tensor.h" - -namespace infrt { -namespace paddle {} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/paddle/tensor.h b/paddle/infrt/paddle/tensor.h deleted file mode 100644 index 5c4458bb62d..00000000000 --- a/paddle/infrt/paddle/tensor.h +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include -#include -#include -#include - -#include "paddle/infrt/common/buffer.h" -#include "paddle/infrt/common/common.h" -#include "paddle/infrt/common/object.h" - -namespace infrt { -namespace paddle { -using common::Target; - -struct Shape { - using dim_t = int; - - Shape() = default; - explicit Shape(const std::vector& data) : data_(data) {} - - void SetData(const std::vector& data) { data_ = data; } - - const std::vector& data() const INFRT_RESULT_SHOULD_USE { - return data_; - } - std::vector& data() INFRT_RESULT_SHOULD_USE { return data_; } - size_t size() const INFRT_RESULT_SHOULD_USE { return data_.size(); } - uint32_t numel() const INFRT_RESULT_SHOULD_USE { - return std::accumulate( - data_.begin(), data_.end(), 1, [](dim_t a, dim_t b) { return a * b; }); - } - - private: - std::vector data_; -}; - -class _Tensor_ : public common::Object { - public: - _Tensor_() : buffer_(std::make_shared()) {} - - Shape& shape() { return shape_; } - - void Resize(const Shape& shape) { - shape_ = shape; - buffer_->data()->resize( - reinterpret_cast(shape.data().data()), - shape.size()); - } - - template - inline T* mutable_data(const Target& target) { - set_type(type_of()); - if (target == common::DefaultHostTarget()) { - int alignment = type_of().ElementOf().bits(); - buffer_->ResizeLazy(alignment, shape_.numel() * sizeof(T), target); - } else { - buffer_->ResizeLazy(shape_.numel() * sizeof(T), target); - } - return reinterpret_cast(buffer_->data()->memory); - } - - template - const T* data() const { - return reinterpret_cast(buffer_->data()->memory); - } - - const Type& type() { return type_; } - - void set_type(Type type) { type_ = type; } - const Type& type() const { return type_; } - - infrt_buffer_t* buffer() { return buffer_->data(); } - - const char* type_info() const override { return __type_info__; } - - private: - common::Type type_; - // A shared ptr to make it easier to share buffer between tensors. - std::shared_ptr buffer_; - Shape shape_; - - static constexpr const char* __type_info__ = "_frontend_tensor_"; -}; - -class Tensor : public Shared<_Tensor_> { - public: - Tensor() : Shared(new _Tensor_) {} - explicit Tensor(_Tensor_* x) : Shared(x) {} -}; - -} // namespace paddle -} // namespace infrt diff --git a/paddle/infrt/support/CMakeLists.txt b/paddle/infrt/support/CMakeLists.txt deleted file mode 100644 index 9bcce6cab36..00000000000 --- a/paddle/infrt/support/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -core_gather_headers() diff --git a/paddle/infrt/support/type_traits.h b/paddle/infrt/support/type_traits.h deleted file mode 100644 index 33a42fe37ea..00000000000 --- a/paddle/infrt/support/type_traits.h +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// This file defines type traits related utilities. - -#pragma once - -#include -#include -#include - -#include "llvm/ADT/STLExtras.h" - -namespace infrt { - -// Utility template for tag dispatching. -template -struct TypeTag {}; - -// This is the equivalent of std::void_t in C++17. -template -struct make_void { - typedef void type; -}; -template -using void_t = typename make_void::type; - -// The same as std::disjunction in C++17. -template -struct disjunction : std::false_type {}; -template -struct disjunction : B1 {}; -template -struct disjunction - : std::conditional_t> {}; - -// Check whether T may be a base class. -template -using MaybeBase = - llvm::conjunction, llvm::negation>>; - -// Find the index of a type in a tuple. -// -// Example: -// using Tuple = std::tuple; -// static_assert(TupleIndexOf::value == 0); -// static_assert(TupleIndexOf::value == 2); -template -struct TupleIndexOf; - -template -struct TupleIndexOf> - : std::integral_constant {}; - -template -struct TupleIndexOf> - : std::integral_constant>::value> { -}; - -template -struct TupleHasType; - -template -struct TupleHasType> - : disjunction...> {}; - -// The detector pattern in C++ that can be used for checking whether a type has -// a specific property, e.g. whether an internal type is present or whether a -// particular operation is valid. -// -// Sample usage: -// -// struct Foo { -// using difference_type = int; -// int get(); -// }; -// struct Bar {}; -// -// // Check whether a type T has an internal difference_type. -// template -// using diff_t = typename T::difference_type; -// -// static_assert(is_detected_v, "Foo has difference_type"); -// static_assert(!is_detected_v, "Bar has no difference_type"); -// -// // Check whether a type T has a get() member function. -// template -// using has_get_t = decltype(std::declval().get()); -// -// static_assert(is_detected_v, "Foo has get()"); -// static_assert(!is_detected_v, "Bar has no get()"); -// -// See https://en.cppreference.com/w/cpp/experimental/is_detected for details. - -namespace internal { - -// nonesuch is a class type used to indicate detection failure. -struct nonesuch { - ~nonesuch() = delete; - nonesuch(nonesuch const&) = delete; - void operator=(nonesuch const&) = delete; -}; - -template - class Op, - class... Args> -struct detector : std::false_type { - using value_t = std::false_type; - using type = Default; -}; - -template class Op, class... Args> -struct detector>, Op, Args...> { - using value_t = std::true_type; - using type = Op; -}; - -} // namespace internal - -template