未验证 提交 b77e20ac 编写于 作者: 石晓伟 提交者: GitHub

add the map for dense tensor, test=develop (#40665)

上级 78fad09b
...@@ -129,7 +129,7 @@ class PredictExecutor : public MlirToRuntimeTranslator { ...@@ -129,7 +129,7 @@ class PredictExecutor : public MlirToRuntimeTranslator {
auto arg = predict_func.getArgument(i); auto arg = predict_func.getArgument(i);
auto type = arg.getType(); auto type = arg.getType();
// this param is TensorMap // this param is TensorMap
if (type.isa<infrt::DenseTensorMapType>()) { if (type.isa<infrt::DenseHostTensorMapType>()) {
auto* value = new host_context::Value(std::move(*map)); auto* value = new host_context::Value(std::move(*map));
arguments_.push_back(value); arguments_.push_back(value);
AddValue(predict_func.getArgument(i), value); AddValue(predict_func.getArgument(i), value);
......
...@@ -106,7 +106,7 @@ def LoadParamsOp : DT_Op<"load_params", [NoSideEffect]> { ...@@ -106,7 +106,7 @@ def LoadParamsOp : DT_Op<"load_params", [NoSideEffect]> {
// input path of model params. // input path of model params.
let arguments = (ins StrAttr:$path); let arguments = (ins StrAttr:$path);
let results = (outs DenseTensorMap:$out); let results = (outs DenseHostTensorMap:$out);
let assemblyFormat = "`(``)`attr-dict"; let assemblyFormat = "`(``)`attr-dict";
} }
...@@ -121,7 +121,7 @@ def TensorMapGetTensorOp : DT_Op<"tensor_map_get_tensor", [NoSideEffect]> { ...@@ -121,7 +121,7 @@ def TensorMapGetTensorOp : DT_Op<"tensor_map_get_tensor", [NoSideEffect]> {
// input path of model params. // input path of model params.
let arguments = (ins let arguments = (ins
DenseTensorMap:$map, DenseHostTensorMap:$map,
StrAttr:$name StrAttr:$name
); );
let results = (outs DenseTensor:$output); let results = (outs DenseTensor:$output);
...@@ -136,7 +136,7 @@ def TensorMapGetSizeOp : DT_Op<"tensor_map_get_size", [NoSideEffect]> { ...@@ -136,7 +136,7 @@ def TensorMapGetSizeOp : DT_Op<"tensor_map_get_size", [NoSideEffect]> {
An operation that get the size of a TensorMap. An operation that get the size of a TensorMap.
}]; }];
let arguments = (ins DenseTensorMap:$map); let arguments = (ins DenseHostTensorMap:$map);
let results = (outs I32:$size); let results = (outs I32:$size);
let assemblyFormat = "`(` $map `)` attr-dict `->` type($size)"; let assemblyFormat = "`(` $map `)` attr-dict `->` type($size)";
} }
......
...@@ -83,7 +83,7 @@ def DenseTensor : Infrt_Type<"DenseTensor"> { ...@@ -83,7 +83,7 @@ def DenseTensor : Infrt_Type<"DenseTensor"> {
); );
} }
def DenseTensorMap : Infrt_Type<"DenseTensorMap"> { def DenseHostTensorMap : Infrt_Type<"DenseHostTensorMap"> {
let summary = "infrt dense tensor map"; let summary = "infrt dense tensor map";
let description = [{dense_tensor map}]; let description = [{dense_tensor map}];
let parameters = (ins); let parameters = (ins);
......
...@@ -91,7 +91,7 @@ mlir::Type InfrtDialect::parseType(::mlir::DialectAsmParser &parser) const { ...@@ -91,7 +91,7 @@ mlir::Type InfrtDialect::parseType(::mlir::DialectAsmParser &parser) const {
parser.getContext(), shape, elementType, lod_level); parser.getContext(), shape, elementType, lod_level);
} }
if (keyword == "dense_tensor_map") { if (keyword == "dense_tensor_map") {
return DenseTensorMapType::get(parser.getContext()); return DenseHostTensorMapType::get(parser.getContext());
} }
if (keyword == "dense_tensor") { if (keyword == "dense_tensor") {
// parse DenseTensor, for example: !i=Infrt.tensor<X86, CUDA, F32> // parse DenseTensor, for example: !i=Infrt.tensor<X86, CUDA, F32>
...@@ -162,7 +162,7 @@ void InfrtDialect::printType(::mlir::Type type, ...@@ -162,7 +162,7 @@ void InfrtDialect::printType(::mlir::Type type,
<< lod_tensor_type.getLod_level() << ">"; << lod_tensor_type.getLod_level() << ">";
return; return;
} }
if (type.isa<infrt::DenseTensorMapType>()) { if (type.isa<infrt::DenseHostTensorMapType>()) {
os << "dense_tensor_map"; os << "dense_tensor_map";
return; return;
} }
...@@ -180,12 +180,6 @@ void InfrtDialect::printType(::mlir::Type type, ...@@ -180,12 +180,6 @@ void InfrtDialect::printType(::mlir::Type type,
os << "tensor_list"; os << "tensor_list";
return; return;
} }
// print DenseTensorType, for example: !infrt.dense_tensor<CPU, FP32, NCHW>
if (type.isa<DenseTensorMapType>()) {
os << "dense_tensor_map";
return;
}
llvm_unreachable("unknown infrt type."); llvm_unreachable("unknown infrt type.");
} }
......
...@@ -23,16 +23,16 @@ ...@@ -23,16 +23,16 @@
namespace infrt { namespace infrt {
phi::Backend ConvertTargetToPhi(TargetType target); ::phi::Backend ConvertTargetToPhi(TargetType target);
TargetType ConvertTargetFromPhi(phi::Backend backend); TargetType ConvertTargetFromPhi(::phi::Backend backend);
phi::DataType ConvertPrecisionToPhi(PrecisionType precision); ::phi::DataType ConvertPrecisionToPhi(PrecisionType precision);
PrecisionType ConvertPrecisionFromPhi(phi::DataType datatype); PrecisionType ConvertPrecisionFromPhi(::phi::DataType datatype);
phi::DataLayout ConvertLayoutToPhi(LayoutType layout); ::phi::DataLayout ConvertLayoutToPhi(LayoutType layout);
LayoutType ConvertLayoutFromPhi(phi::DataLayout layout); LayoutType ConvertLayoutFromPhi(::phi::DataLayout layout);
phi::KernelKey ConvertPlaceToPhi(const Place& place); ::phi::KernelKey ConvertPlaceToPhi(const Place& place);
Place ConvertPlaceFromPhi(phi::TensorArgDef tensor_arg); Place ConvertPlaceFromPhi(::phi::TensorArgDef tensor_arg);
} // namespace infrt } // namespace infrt
...@@ -37,4 +37,8 @@ def Allocator : PHI_Type<"Allocator"> { ...@@ -37,4 +37,8 @@ def Allocator : PHI_Type<"Allocator"> {
let assemblyFormat = "`<` $target `>`"; let assemblyFormat = "`<` $target `>`";
} }
def PD_DenseTensorMap : PHI_Type<"DenseTensorMap"> {
let mnemonic = "dense_tensor_map";
}
#endif #endif
...@@ -51,12 +51,46 @@ class CreateContextOp<string target> ...@@ -51,12 +51,46 @@ class CreateContextOp<string target>
let results = (outs Context:$output); 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_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_CreateCPUDenseTensorOp : CreateDenseTensorOp<"cpu">;
def PDT_CreateGPUDenseTensorOp : CreateDenseTensorOp<"gpu">; def PDT_CreateGPUDenseTensorOp : CreateDenseTensorOp<"gpu">;
def PDT_FillDenseTensorOp_f32 : FillDenseTensorOp<F32ArrayAttr, "f32">; def PDT_FillDenseTensorOp_f32 : FillDenseTensorOp<F32ArrayAttr, "f32">;
def PDT_CreateCPUContextOp : CreateContextOp<"cpu">; def PDT_CreateCPUContextOp : CreateContextOp<"cpu">;
def PDT_CreateGPUContextOp : CreateContextOp<"gpu">; def PDT_CreateGPUContextOp : CreateContextOp<"gpu">;
def PDT_PrintDenseTensor : PrintDenseTensorOp; def PDT_PrintDenseTensor : PrintDenseTensorOp;
def PDT_TensorMapGetTensorOp: TensorMapGetTensorOp;
def FakeKernelOp : PDT_Op<"fake_phi_kernel"> { def FakeKernelOp : PDT_Op<"fake_phi_kernel"> {
let arguments = (ins Context:$dev_ctx, DenseTensor:$x, DenseTensor:$y, BoolAttr:$transpose_x, BoolAttr:$transpose_y); let arguments = (ins Context:$dev_ctx, DenseTensor:$x, DenseTensor:$y, BoolAttr:$transpose_x, BoolAttr:$transpose_y);
......
...@@ -351,18 +351,26 @@ bool MlirToRuntimeTranslator::EmitGeneralOp( ...@@ -351,18 +351,26 @@ bool MlirToRuntimeTranslator::EmitGeneralOp(
auto attrs = op->getAttrs(); auto attrs = op->getAttrs();
// MLIR's underlying attr storage type is `Builtin_Dictionary`, and its // MLIR's underlying attr storage type is `Builtin_Dictionary`, and its
// elements // elements are sorted by name. The following code adapts the order of
// are sorted by name. The following code adapts the order of function // function signatures of the phi operator library.
// signatures
// of the phi operator library.
llvm::SmallVector<Value*, 4> tmp; llvm::SmallVector<Value*, 4> tmp;
tmp.resize(attrs.size()); tmp.resize(attrs.size());
const std::string& kernel_name = op->getName().getStringRef().str(); const std::string& kernel_name = op->getName().getStringRef().str();
const auto& attr_names = kernel_registry.GetAttrNameList(kernel_name); const auto& attr_names = kernel_registry.GetAttrNameList(kernel_name);
if (attrs.size() && attr_names.empty()) { if (attrs.size()) {
if (attr_names.empty()) {
LOG(WARNING) << "The kernel `" << kernel_name LOG(WARNING) << "The kernel `" << kernel_name
<< "` has no specified attr order."; << "` has not been registered with "
"`KernelRegistry::AddKernelWithAttrs()`.";
} 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, auto get_offset = [](const char* attr,
const std::vector<const char*>& names, const std::vector<const char*>& names,
const std::string& kernel_name) -> int { const std::string& kernel_name) -> int {
...@@ -385,7 +393,7 @@ bool MlirToRuntimeTranslator::EmitGeneralOp( ...@@ -385,7 +393,7 @@ bool MlirToRuntimeTranslator::EmitGeneralOp(
} else { } else {
offset = i; offset = i;
} }
CHECK_NE(offset, -1); CHECK_GT(offset, -1);
if (auto v = EmitAttribute<int32_t>(attr.getValue())) { if (auto v = EmitAttribute<int32_t>(attr.getValue())) {
tmp[offset] = new Value(*v); tmp[offset] = new Value(*v);
} else if (auto v = EmitAttribute<int64_t>(attr.getValue())) { } else if (auto v = EmitAttribute<int64_t>(attr.getValue())) {
......
...@@ -79,7 +79,7 @@ mlir::FuncOp MLIRModelGenImpl::UpdateModelModule( ...@@ -79,7 +79,7 @@ mlir::FuncOp MLIRModelGenImpl::UpdateModelModule(
llvm::SmallVector<mlir::Type, 4> MLIRModelGenImpl::GetModelInputsType( llvm::SmallVector<mlir::Type, 4> MLIRModelGenImpl::GetModelInputsType(
const infrt::paddle::framework_proto::ProgramDesc &program) { const infrt::paddle::framework_proto::ProgramDesc &program) {
llvm::SmallVector<mlir::Type, 4> operandTypes; llvm::SmallVector<mlir::Type, 4> operandTypes;
operandTypes.push_back(infrt::DenseTensorMapType::get(context_)); operandTypes.push_back(infrt::DenseHostTensorMapType::get(context_));
for (auto &op_desc : main_block_.ops()) { for (auto &op_desc : main_block_.ops()) {
if (op_desc.type() != "feed") continue; if (op_desc.type() != "feed") continue;
for (int var_idx = 0; var_idx < op_desc.outputs_size(); ++var_idx) { for (int var_idx = 0; var_idx < op_desc.outputs_size(); ++var_idx) {
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#ifdef INFRT_WITH_PHI #ifdef INFRT_WITH_PHI
#include "paddle/infrt/backends/host/phi_allocator.h" #include "paddle/infrt/backends/host/phi_allocator.h"
#include "paddle/infrt/backends/host/phi_context.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/backends/all_context.h"
#include "paddle/phi/common/backend.h" #include "paddle/phi/common/backend.h"
#include "paddle/phi/common/data_type.h" #include "paddle/phi/common/data_type.h"
...@@ -84,22 +85,23 @@ using ValueVariantType = ...@@ -84,22 +85,23 @@ using ValueVariantType =
#ifdef INFRT_WITH_GPU #ifdef INFRT_WITH_GPU
backends::GpuPhiContext, backends::GpuPhiContext,
::phi::GPUContext, ::phi::GPUContext,
#endif #endif // INFRT_WITH_GPU
::phi::CPUContext, ::phi::CPUContext,
std::vector<const phi::DenseTensor*>, std::vector<const ::phi::DenseTensor*>,
std::vector<phi::DenseTensor*>, std::vector<::phi::DenseTensor*>,
paddle::experimental::ScalarBase<phi::DenseTensor>, paddle::experimental::ScalarBase<::phi::DenseTensor>,
paddle::experimental::ScalarArrayBase<phi::DenseTensor>, paddle::experimental::ScalarArrayBase<::phi::DenseTensor>,
std::vector<phi::MetaTensor*>, std::vector<::phi::MetaTensor*>,
phi::MetaConfig, ::phi::MetaConfig,
paddle::experimental::Backend, paddle::experimental::Backend,
paddle::experimental::DataLayout, paddle::experimental::DataLayout,
paddle::experimental::DataType, paddle::experimental::DataType,
::infrt::phi::DenseTensorMap,
#endif // INFRT_WITH_PHI
#ifdef INFRT_WITH_TRT #ifdef INFRT_WITH_TRT
::infrt::backends::tensorrt::TrtEngine, ::infrt::backends::tensorrt::TrtEngine,
::infrt::kernel::tensorrt::MlirOperationWithInfrtSymbol, ::infrt::kernel::tensorrt::MlirOperationWithInfrtSymbol,
#endif // INFRT_WITH_TRT #endif // INFRT_WITH_TRT
#endif
std::vector<int16_t>, std::vector<int16_t>,
std::vector<int32_t>, std::vector<int32_t>,
std::vector<int64_t>, std::vector<int64_t>,
...@@ -136,6 +138,7 @@ class Value : public common::Object { ...@@ -136,6 +138,7 @@ class Value : public common::Object {
explicit Value(tensor::DenseHostTensor&& x) : data(std::move(x)) {} explicit Value(tensor::DenseHostTensor&& x) : data(std::move(x)) {}
explicit Value(MlirFunctionExecutable* x) : data(x) {} explicit Value(MlirFunctionExecutable* x) : data(x) {}
#ifdef INFRT_WITH_PHI #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(::phi::CPUContext&& x) : data(std::move(x)) {}
explicit Value(backends::CpuPhiContext&& x) : data(std::move(x)) {} explicit Value(backends::CpuPhiContext&& x) : data(std::move(x)) {}
#ifdef INFRT_WITH_GPU #ifdef INFRT_WITH_GPU
......
...@@ -13,8 +13,11 @@ ...@@ -13,8 +13,11 @@
// limitations under the License. // limitations under the License.
#include "paddle/infrt/kernel/phi/dense_tensor_kernels.h" #include "paddle/infrt/kernel/phi/dense_tensor_kernels.h"
#include "paddle/infrt/common/string.h"
#include "paddle/infrt/dialect/phi/data_type.h" #include "paddle/infrt/dialect/phi/data_type.h"
#include "paddle/infrt/kernel/phi/context_kernels.h" #include "paddle/infrt/kernel/phi/context_kernels.h"
#include "paddle/infrt/paddle/model_parser.h"
#include "paddle/infrt/paddle/scope.h"
#include "paddle/phi/backends/all_context.h" #include "paddle/phi/backends/all_context.h"
#include "paddle/phi/common/place.h" #include "paddle/phi/common/place.h"
...@@ -22,6 +25,18 @@ ...@@ -22,6 +25,18 @@
#include <cuda_runtime.h> #include <cuda_runtime.h>
#endif #endif
namespace paddle {
namespace platform {
using DeviceContext = ::phi::DeviceContext;
} // namespace platform
namespace framework {
using LoDTensor = ::phi::DenseTensor;
void DeserializeFromStream(std::istream& is,
LoDTensor* tensor,
const platform::DeviceContext& dev_ctx);
}
} // namespace paddle
namespace infrt { namespace infrt {
namespace kernel { namespace kernel {
namespace phi { namespace phi {
...@@ -130,6 +145,89 @@ void PrintDenseTensor(::phi::DenseTensor* dense_tensor) { ...@@ -130,6 +145,89 @@ void PrintDenseTensor(::phi::DenseTensor* dense_tensor) {
std::cout << "]\n"; std::cout << "]\n";
#undef PRINT_META_DATA #undef PRINT_META_DATA
} }
::infrt::phi::DenseTensorMap LoadParams(
host_context::Attribute<std::string> path) {
const auto& file_path = path.get();
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);
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<::phi::DenseTensor> tensor{
std::make_unique<::phi::DenseTensor>()};
::phi::CPUContext ctx;
::paddle::framework::DeserializeFromStream(
param_file, tensor.get(), ctx);
map.SetDenseTensor(var.name(), std::move(tensor));
} break;
default: {
LOG(WARNING) << "Var `" << var.name() << "` type `"
<< static_cast<int>(var.type().type())
<< "` has not been supported now.";
}
}
}
return map;
}
::infrt::phi::DenseTensorMap LoadCombinedParams(
host_context::Attribute<std::string> model_path,
host_context::Attribute<std::string> params_path) {
const auto& model = model_path.get();
std::cout << "loading params from: " << model << std::endl;
::infrt::phi::DenseTensorMap map;
auto pb_proto_prog = paddle::LoadProgram(model);
auto main_block = pb_proto_prog->blocks(0);
std::ifstream param_file(params_path.get(), std::ios::binary);
std::set<std::string> 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.");
}
}
for (auto& var : tmp) {
std::unique_ptr<::phi::DenseTensor> tensor{
std::make_unique<::phi::DenseTensor>()};
::phi::CPUContext ctx;
::paddle::framework::DeserializeFromStream(param_file, tensor.get(), ctx);
map.SetDenseTensor(var, std::move(tensor));
}
return map;
}
::phi::DenseTensor TensorMapGetTensor(
const ::infrt::phi::DenseTensorMap& map,
host_context::Attribute<std::string> name) {
auto* tensor = map.GetDenseTensor(name.get());
CHECK(tensor);
return *tensor;
}
int32_t TensorMapGetSize(const ::infrt::phi::DenseTensorMap& map) {
return map.size();
}
} // namespace phi } // namespace phi
} // namespace kernel } // namespace kernel
} // namespace infrt } // namespace infrt
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "paddle/infrt/backends/host/phi_allocator.h" #include "paddle/infrt/backends/host/phi_allocator.h"
#include "paddle/infrt/dialect/infrt/common/types.h" #include "paddle/infrt/dialect/infrt/common/types.h"
#include "paddle/infrt/host_context/kernel_utils.h" #include "paddle/infrt/host_context/kernel_utils.h"
#include "paddle/infrt/tensor/phi/tensor_map.h"
#include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/dense_tensor.h"
namespace infrt { namespace infrt {
...@@ -41,6 +42,19 @@ void FillDenseTensorF32(::phi::DenseTensor* dense_tensor, ...@@ -41,6 +42,19 @@ void FillDenseTensorF32(::phi::DenseTensor* dense_tensor,
host_context::Attribute<std::vector<float>> values); host_context::Attribute<std::vector<float>> values);
void PrintDenseTensor(::phi::DenseTensor* dense_tensor); void PrintDenseTensor(::phi::DenseTensor* dense_tensor);
infrt::phi::DenseTensorMap LoadParams(
host_context::Attribute<std::string> path);
::phi::DenseTensor TensorMapGetTensor(
const ::infrt::phi::DenseTensorMap& map,
host_context::Attribute<std::string> name);
::infrt::phi::DenseTensorMap LoadCombinedParams(
host_context::Attribute<std::string> model_path,
host_context::Attribute<std::string> params_path);
int32_t TensorMapGetSize(const ::infrt::phi::DenseTensorMap& map);
} // namespace phi } // namespace phi
} // namespace kernel } // namespace kernel
} // namespace infrt } // namespace infrt
...@@ -37,15 +37,16 @@ TEST(utils, registry) { ...@@ -37,15 +37,16 @@ TEST(utils, registry) {
CHECK_EQ(count, 2U); CHECK_EQ(count, 2U);
} }
class FancyAllocator : public phi::Allocator { class FancyAllocator : public ::phi::Allocator {
public: public:
static void Delete(phi::Allocation* allocation) { static void Delete(::phi::Allocation* allocation) {
::operator delete(allocation->ptr()); ::operator delete(allocation->ptr());
} }
AllocationPtr Allocate(size_t bytes_size) override { AllocationPtr Allocate(size_t bytes_size) override {
void* data = ::operator new(bytes_size); void* data = ::operator new(bytes_size);
auto* allocation = new phi::Allocation(data, bytes_size, phi::CPUPlace()); auto* allocation =
new ::phi::Allocation(data, bytes_size, ::phi::CPUPlace());
return AllocationPtr(allocation, Delete); return AllocationPtr(allocation, Delete);
} }
}; };
...@@ -56,20 +57,20 @@ TEST(ElementwiseAdd, launcher_registry) { ...@@ -56,20 +57,20 @@ TEST(ElementwiseAdd, launcher_registry) {
ASSERT_GE(registry.size(), 1UL); ASSERT_GE(registry.size(), 1UL);
auto creator = registry.GetKernel("phi_cpu.add.float32.any"); auto creator = registry.GetKernel("phi_cpu.add.float32.any");
const phi::DDim dims({1, 2}); const ::phi::DDim dims({1, 2});
const phi::DataType dtype{phi::DataType::FLOAT32}; const ::phi::DataType dtype{::phi::DataType::FLOAT32};
const phi::DataLayout layout{phi::DataLayout::NHWC}; const ::phi::DataLayout layout{::phi::DataLayout::NHWC};
const phi::LoD lod{}; const ::phi::LoD lod{};
phi::DenseTensorMeta meta(dtype, dims, layout, lod); ::phi::DenseTensorMeta meta(dtype, dims, layout, lod);
auto fancy_allocator = std::unique_ptr<phi::Allocator>(new FancyAllocator); auto fancy_allocator = std::unique_ptr<::phi::Allocator>(new FancyAllocator);
auto* alloc = fancy_allocator.get(); auto* alloc = fancy_allocator.get();
phi::DenseTensor a(alloc, meta); ::phi::DenseTensor a(alloc, meta);
phi::DenseTensor b(alloc, meta); ::phi::DenseTensor b(alloc, meta);
phi::DenseTensor c(alloc, meta); ::phi::DenseTensor c(alloc, meta);
auto place = phi::CPUPlace(); auto place = ::phi::CPUPlace();
float* a_data = a.mutable_data<float>(place); float* a_data = a.mutable_data<float>(place);
float* b_data = b.mutable_data<float>(place); float* b_data = b.mutable_data<float>(place);
float* c_data = c.mutable_data<float>(place); float* c_data = c.mutable_data<float>(place);
...@@ -78,7 +79,7 @@ TEST(ElementwiseAdd, launcher_registry) { ...@@ -78,7 +79,7 @@ TEST(ElementwiseAdd, launcher_registry) {
b_data[i] = 2.f; b_data[i] = 2.f;
} }
phi::CPUContext context; ::phi::CPUContext context;
context.SetAllocator(alloc); context.SetAllocator(alloc);
context.Init(); context.Init();
......
...@@ -53,6 +53,19 @@ void RegisterPhiKernels(host_context::KernelRegistry* registry) { ...@@ -53,6 +53,19 @@ void RegisterPhiKernels(host_context::KernelRegistry* registry) {
INFRT_KERNEL(infrt::kernel::phi::CreateGPUDenseTensor), INFRT_KERNEL(infrt::kernel::phi::CreateGPUDenseTensor),
{"dims", "lod", "layout", "precision"}); {"dims", "lod", "layout", "precision"});
#endif #endif
registry->AddKernelWithAttrs("phi_dt.load_params",
INFRT_KERNEL(infrt::kernel::phi::LoadParams),
{"path"});
registry->AddKernelWithAttrs(
"phi_dt.load_combined_params",
INFRT_KERNEL(infrt::kernel::phi::LoadCombinedParams),
{"model_path", "params_path"});
registry->AddKernelWithAttrs(
"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 kernel
......
...@@ -68,14 +68,14 @@ int32_t TensorMapGetSize(TensorMap map) { return map.size(); } ...@@ -68,14 +68,14 @@ int32_t TensorMapGetSize(TensorMap map) { return map.size(); }
// TODO(wilber): Maybe we should place TensorList type in dt dialect. // TODO(wilber): Maybe we should place TensorList type in dt dialect.
#ifdef INFRT_WITH_PHI #ifdef INFRT_WITH_PHI
phi::DenseTensor TensorListGetTensor(std::vector<phi::DenseTensor *> list, ::phi::DenseTensor TensorListGetTensor(std::vector<::phi::DenseTensor *> list,
Attribute<int32_t> idx) { Attribute<int32_t> idx) {
CHECK_LT(idx.get(), static_cast<int>(list.size())) CHECK_LT(idx.get(), static_cast<int>(list.size()))
<< "idx should less than list size"; << "idx should less than list size";
return *list[idx.get()]; return *list[idx.get()];
} }
int32_t TensorListGetSize(const std::vector<phi::DenseTensor *> &list) { int32_t TensorListGetSize(const std::vector<::phi::DenseTensor *> &list) {
return list.size(); return list.size();
} }
#endif #endif
......
core_gather_headers() core_gather_headers()
add_subdirectory(phi)
gather_srcs(infrt_src SRCS gather_srcs(infrt_src SRCS
tensor_map.cc tensor_map.cc
tensor_metadata.cc tensor_metadata.cc
......
gather_srcs(infrt_src SRCS
tensor_map.cc
)
// 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/tensor/phi/tensor_map.h"
#include "llvm/Support/ErrorHandling.h"
namespace infrt {
namespace phi {
void DenseTensorMap::SetDenseTensor(
const std::string& name, std::unique_ptr<::phi::DenseTensor>&& tensor) {
std::lock_guard<std::mutex> lock(mu_);
auto it = map_.emplace(std::make_pair(name, std::move(tensor)));
if (!it.second) {
llvm_unreachable("dense tensor map insert failed.");
}
}
::phi::DenseTensor* DenseTensorMap::GetDenseTensor(
const std::string& name) const {
std::lock_guard<std::mutex> lock(mu_);
auto it = map_.find(name);
if (it != map_.end()) {
return it->second.get();
}
LOG(WARNING) << "can not find `" << name << "` in the tensor map.";
return nullptr;
}
size_t DenseTensorMap::size() const {
std::lock_guard<std::mutex> lock(mu_);
return map_.size();
}
} // namespace phi
} // namespace infrt
// 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/phi/core/dense_tensor.h"
namespace infrt {
namespace phi {
class DenseTensorMap {
public:
DenseTensorMap() = default;
DenseTensorMap(DenseTensorMap&& other) : map_(std::move(other.map_)) {}
void SetDenseTensor(const std::string& name,
std::unique_ptr<::phi::DenseTensor>&& tensor);
::phi::DenseTensor* GetDenseTensor(const std::string& name) const;
size_t size() const;
private:
mutable std::mutex mu_;
std::unordered_map<std::string, std::unique_ptr<::phi::DenseTensor>> map_;
};
} // namespace phi
} // namespace infrt
...@@ -12,3 +12,30 @@ func @load_tensor_map() { ...@@ -12,3 +12,30 @@ func @load_tensor_map() {
infrt.return infrt.return
} }
func @load_phi_tensor_map() {
%map = phi_dt.load_params(){path="@CMAKE_BINARY_DIR@/multi_fc_model"}
%size = phi_dt.tensor_map_get_size(%map) -> i32
infrt.print.i32 %size
%a = phi_dt.tensor_map_get_tensor(%map) {name="fc_bias"} -> !infrt.dense_tensor<CPU, FP32, NCHW>
// CHECK: dense_tensor: shape=shape[2], value=[0,0]
phi_dt.print_tensor (%a : !infrt.dense_tensor<CPU, FP32, NCHW>)
infrt.return
}
func @load_combined_phi_tensor_map() {
%map = phi_dt.load_combined_params(){model_path="@CMAKE_BINARY_DIR@/multi_fc_model/fc.pdmodel",
params_path="@CMAKE_BINARY_DIR@/multi_fc_model/fc.pdiparams"}
%size = phi_dt.tensor_map_get_size(%map) -> i32
infrt.print.i32 %size
%a = phi_dt.tensor_map_get_tensor(%map) {name="fc_bias"} -> !infrt.dense_tensor<CPU, FP32, NCHW>
// CHECK: dense_tensor: shape=shape[2], value=[0,0]
phi_dt.print_tensor (%a : !infrt.dense_tensor<CPU, FP32, NCHW>)
infrt.return
}
...@@ -52,4 +52,7 @@ loss = exe = fluid.Executor(cpu) ...@@ -52,4 +52,7 @@ loss = exe = fluid.Executor(cpu)
exe.run(fluid.default_startup_program()) exe.run(fluid.default_startup_program())
fluid.io.save_inference_model("./multi_fc_model", [a.name], [fc_out], exe) fluid.io.save_inference_model("./multi_fc_model", [a.name], [fc_out], exe)
fluid.io.save_inference_model("./multi_fc_model", [a.name], [fc_out], exe, None,
"fc.pdmodel", "fc.pdiparams")
print('output name', fc_out.name) print('output name', fc_out.name)
...@@ -49,7 +49,7 @@ all_ir_name=`grep -Eo "PDTCPU_Kernel<.*\"" paddle/infrt/dialect/phi/ir/phi_cpu_k ...@@ -49,7 +49,7 @@ all_ir_name=`grep -Eo "PDTCPU_Kernel<.*\"" paddle/infrt/dialect/phi/ir/phi_cpu_k
for ir in $all_ir_name for ir in $all_ir_name
do do
attr_name=`grep "<\"$ir" -A 3 paddle/infrt/dialect/phi/ir/phi_cpu_kernels.td | grep -Eo "Attr:.*)" \ attr_name=`grep "<\"$ir" -A 3 paddle/infrt/dialect/phi/ir/phi_cpu_kernels.td | grep -Eo "Attr:.*)" \
| awk '{gsub(/F32Attr/,"");gsub(/F64Attr/,"");gsub(/StrAttr/,"");gsub(/BOOLAttr/,""); \ | awk '{gsub(/F32Attr/,"");gsub(/F64Attr/,"");gsub(/StrAttr/,"");gsub(/BoolAttr/,""); \
gsub(/SI1Attr/,"");gsub(/SI8Attr/,"");gsub(/SI16Attr/,"");gsub(/SI32Attr/,"");gsub(/SI64Attr/,""); \ gsub(/SI1Attr/,"");gsub(/SI8Attr/,"");gsub(/SI16Attr/,"");gsub(/SI32Attr/,"");gsub(/SI64Attr/,""); \
gsub(/UI1Attr/,"");gsub(/UI8Attr/,"");gsub(/I16Attr/,"");gsub(/I32Attr/,"");gsub(/I64Attr/,""); \ gsub(/UI1Attr/,"");gsub(/UI8Attr/,"");gsub(/I16Attr/,"");gsub(/I32Attr/,"");gsub(/I64Attr/,""); \
gsub(/I1Attr/,"");gsub(/I8Attr/,"");gsub(/UI16Attr/,"");gsub(/UI32Attr/,"");gsub(/UI64Attr/,""); \ gsub(/I1Attr/,"");gsub(/I8Attr/,"");gsub(/UI16Attr/,"");gsub(/UI32Attr/,"");gsub(/UI64Attr/,""); \
...@@ -62,7 +62,7 @@ all_ir_name=`grep -Eo "PDTGPU_Kernel<.*\"" paddle/infrt/dialect/phi/ir/phi_gpu_k ...@@ -62,7 +62,7 @@ all_ir_name=`grep -Eo "PDTGPU_Kernel<.*\"" paddle/infrt/dialect/phi/ir/phi_gpu_k
for ir in $all_ir_name for ir in $all_ir_name
do do
attr_name=`grep "<\"$ir" -A 3 paddle/infrt/dialect/phi/ir/phi_gpu_kernels.td | grep -Eo "Attr:.*)" \ attr_name=`grep "<\"$ir" -A 3 paddle/infrt/dialect/phi/ir/phi_gpu_kernels.td | grep -Eo "Attr:.*)" \
| awk '{gsub(/F32Attr/,"");gsub(/F64Attr/,"");gsub(/StrAttr/,"");gsub(/BOOLAttr/,""); \ | awk '{gsub(/F32Attr/,"");gsub(/F64Attr/,"");gsub(/StrAttr/,"");gsub(/BoolAttr/,""); \
gsub(/SI1Attr/,"");gsub(/SI8Attr/,"");gsub(/SI16Attr/,"");gsub(/SI32Attr/,"");gsub(/SI64Attr/,""); \ gsub(/SI1Attr/,"");gsub(/SI8Attr/,"");gsub(/SI16Attr/,"");gsub(/SI32Attr/,"");gsub(/SI64Attr/,""); \
gsub(/UI1Attr/,"");gsub(/UI8Attr/,"");gsub(/I16Attr/,"");gsub(/I32Attr/,"");gsub(/I64Attr/,""); \ gsub(/UI1Attr/,"");gsub(/UI8Attr/,"");gsub(/I16Attr/,"");gsub(/I32Attr/,"");gsub(/I64Attr/,""); \
gsub(/I1Attr/,"");gsub(/I8Attr/,"");gsub(/UI16Attr/,"");gsub(/UI32Attr/,"");gsub(/UI64Attr/,""); \ gsub(/I1Attr/,"");gsub(/I8Attr/,"");gsub(/UI16Attr/,"");gsub(/UI32Attr/,"");gsub(/UI64Attr/,""); \
......
...@@ -133,11 +133,11 @@ namespace kernel { ...@@ -133,11 +133,11 @@ namespace kernel {
def gen_context(val): def gen_context(val):
if val == "CPU": if val == "CPU":
return "phi::CPUContext", "phi_cpu" return "::phi::CPUContext", "phi_cpu"
elif val == "GPU": elif val == "GPU":
return "phi::GPUContext", "phi_gpu" return "::phi::GPUContext", "phi_gpu"
# elif val == "XPU": # elif val == "XPU":
# return "phi::XPUContext", "phi_xpu" # return "::phi::XPUContext", "phi_xpu"
else: else:
# raise Exception(f"Unknown context type {val}") # raise Exception(f"Unknown context type {val}")
return "", "" return "", ""
...@@ -157,12 +157,12 @@ def gen_kernel_func(val, ctx_name, dtype_name): ...@@ -157,12 +157,12 @@ def gen_kernel_func(val, ctx_name, dtype_name):
ed = val.index('>') ed = val.index('>')
func_name = val[:st] func_name = val[:st]
template_name = val[st + 1:ed] template_name = val[st + 1:ed]
if 'phi::' in template_name: if '::phi::' in template_name:
return "&phi::" + val return "&::phi::" + val
else: else:
return "&phi::" + func_name + "<phi::" + template_name + ">" return "&::phi::" + func_name + "<::phi::" + template_name + ">"
else: else:
return "&phi::" + val + "<" + dtype_name + ", " + ctx_name + ">" return "&::phi::" + val + "<" + dtype_name + ", " + ctx_name + ">"
def gen_dtype(vals: List[str]): def gen_dtype(vals: List[str]):
...@@ -227,7 +227,7 @@ def gen_register_code_info(item: List[str], attr_data: Dict[str, List[str]]): ...@@ -227,7 +227,7 @@ def gen_register_code_info(item: List[str], attr_data: Dict[str, List[str]]):
return "" return ""
item[2] = gen_layout(item[2]) item[2] = gen_layout(item[2])
ir_dtypes, origin_dtypes = gen_dtype(item[4:-1]) ir_dtypes, origin_dtypes = gen_dtype(item[4:-1])
infer_shape_func = "&phi::" + item[-1] infer_shape_func = "&::phi::" + item[-1]
res = "" res = ""
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册