chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
|
||||
namespace ap {
|
||||
|
||||
using adt::errors::AttributeError;
|
||||
using adt::errors::Error;
|
||||
using adt::errors::IndexError;
|
||||
using adt::errors::InvalidArgumentError;
|
||||
using adt::errors::NameError;
|
||||
using adt::errors::RuntimeError;
|
||||
using adt::errors::SyntaxError;
|
||||
using adt::errors::TypeError;
|
||||
using adt::errors::ValueError;
|
||||
|
||||
template <typename T>
|
||||
using Result = adt::Result<T>;
|
||||
} // namespace ap
|
||||
@@ -0,0 +1,267 @@
|
||||
// Copyright (c) 2024 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 <fstream>
|
||||
#include "paddle/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/code_module/func_declare.h"
|
||||
#include "paddle/ap/include/code_module/project.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
struct ApiWrapperProjectMaker {
|
||||
adt::Result<Project> Make(const std::vector<FuncDeclare>& func_declares) {
|
||||
ADT_LET_CONST_REF(nested_files, MakeNestedFiles(func_declares));
|
||||
ADT_LET_CONST_REF(compile_cmd, GetCompileCmd());
|
||||
ADT_LET_CONST_REF(so_relative_path, GetSoRelativePath());
|
||||
axpr::AttrMap<axpr::SerializableValue> others;
|
||||
return Project{nested_files, compile_cmd, so_relative_path, others};
|
||||
}
|
||||
|
||||
adt::Result<Directory<File>> MakeNestedFiles(
|
||||
const std::vector<FuncDeclare>& func_declares) {
|
||||
Directory<File> directory;
|
||||
ADT_LET_CONST_REF(file_content,
|
||||
GenerateApiWrapperCFileContent(func_declares));
|
||||
directory.dentry2file->Set("api_wrapper.c", FileContent{file_content});
|
||||
return directory;
|
||||
}
|
||||
|
||||
adt::Result<std::string> GenerateApiWrapperCFileContent(
|
||||
const std::vector<FuncDeclare>& func_declares) {
|
||||
std::ostringstream ss;
|
||||
ss << "#include <stdint.h>" << std::endl << std::endl;
|
||||
for (const auto& func_declare : func_declares) {
|
||||
ADT_RETURN_IF_ERR(GenerateCCode4FuncDeclare(&ss, func_declare));
|
||||
ss << std::endl;
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
adt::Result<adt::Ok> GenerateCCode4FuncDeclare(
|
||||
std::ostringstream* ss, const FuncDeclare& func_declare) {
|
||||
(*ss) << "void " << func_declare->func_id
|
||||
<< "(void* ret, void* f, void** args) {" << std::endl;
|
||||
ADT_LET_CONST_REF(func_ptr_var, DeclareFuncPtrType(func_declare, "func"));
|
||||
(*ss) << " " << func_ptr_var << " = f"
|
||||
<< ";\n";
|
||||
ADT_LET_CONST_REF(func_call_str,
|
||||
GenerateFuncCall(func_declare, "func", "args"));
|
||||
if (IsVoidRet(func_declare)) {
|
||||
(*ss) << " " << func_call_str << ";\n";
|
||||
} else {
|
||||
ADT_LET_CONST_REF(ret_type, GenCode4ArgType(func_declare->ret_type));
|
||||
(*ss) << " *(" << ret_type << "*)ret = " << func_call_str << ";\n";
|
||||
}
|
||||
(*ss) << "}\n" << std::endl;
|
||||
return adt::Ok{};
|
||||
}
|
||||
|
||||
bool IsVoidRet(const FuncDeclare& func_declare) {
|
||||
return func_declare->ret_type.Match(
|
||||
[&](const axpr::DataType& data_type) -> bool {
|
||||
return data_type.template Has<axpr::CppDataType<adt::Undefined>>();
|
||||
},
|
||||
[&](const axpr::PointerType& pointer_type) -> bool { return false; });
|
||||
}
|
||||
|
||||
adt::Result<std::string> GenerateFuncCall(const FuncDeclare& func_declare,
|
||||
const std::string& func_var_name,
|
||||
const std::string& args_var_name) {
|
||||
std::ostringstream ss;
|
||||
ss << func_var_name << "(";
|
||||
for (size_t i = 0; i < func_declare->arg_types->size(); ++i) {
|
||||
if (i > 0) {
|
||||
ss << ", ";
|
||||
}
|
||||
const auto& arg_type = func_declare->arg_types->at(i);
|
||||
ADT_LET_CONST_REF(arg_type_str, GenCode4ArgType(arg_type));
|
||||
ss << "*(" << arg_type_str << "*)" << args_var_name << "[" << i << "]";
|
||||
}
|
||||
ss << ")";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
adt::Result<std::string> DeclareFuncPtrType(const FuncDeclare& func_declare,
|
||||
const std::string& func_name) {
|
||||
std::ostringstream ss;
|
||||
ADT_LET_CONST_REF(ret_type, GenCode4ArgType(func_declare->ret_type));
|
||||
ss << ret_type << "(*" << func_name << ")(";
|
||||
int i = 0;
|
||||
for (const auto& arg_type : *func_declare->arg_types) {
|
||||
if (i++ > 0) {
|
||||
ss << ", ";
|
||||
}
|
||||
ADT_LET_CONST_REF(arg_type_str, GenCode4ArgType(arg_type));
|
||||
ss << arg_type_str;
|
||||
}
|
||||
ss << ")";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
adt::Result<std::string> GenCode4ArgType(const ArgType& arg_type) {
|
||||
return arg_type.Match(
|
||||
[&](const axpr::DataType& data_type) -> adt::Result<std::string> {
|
||||
return GenCode4DataType(data_type);
|
||||
},
|
||||
[&](const axpr::PointerType& pointer_type) -> adt::Result<std::string> {
|
||||
return GenCode4PointerType(pointer_type);
|
||||
});
|
||||
}
|
||||
|
||||
adt::Result<std::string> GenCode4DataType(const axpr::DataType& data_type) {
|
||||
using RetT = adt::Result<std::string>;
|
||||
return data_type.Match(
|
||||
[&](axpr::CppDataType<bool>) -> RetT { return "bool"; },
|
||||
[&](axpr::CppDataType<int8_t>) -> RetT { return "int8_t"; },
|
||||
[&](axpr::CppDataType<uint8_t>) -> RetT { return "uint8_t"; },
|
||||
[&](axpr::CppDataType<int16_t>) -> RetT { return "int16_t"; },
|
||||
[&](axpr::CppDataType<uint16_t>) -> RetT { return "uint16_t"; },
|
||||
[&](axpr::CppDataType<int32_t>) -> RetT { return "int32_t"; },
|
||||
[&](axpr::CppDataType<uint32_t>) -> RetT { return "uint32_t"; },
|
||||
[&](axpr::CppDataType<int64_t>) -> RetT { return "int64_t"; },
|
||||
[&](axpr::CppDataType<uint64_t>) -> RetT { return "uint64_t"; },
|
||||
[&](axpr::CppDataType<float>) -> RetT { return "float"; },
|
||||
[&](axpr::CppDataType<double>) -> RetT { return "double"; },
|
||||
[&](axpr::CppDataType<axpr::bfloat16>) -> RetT {
|
||||
return adt::errors::TypeError{
|
||||
"bfloat16 is not supported in SO function calls; use float or "
|
||||
"half "
|
||||
"(if available) as an alternative"};
|
||||
},
|
||||
[&](axpr::CppDataType<axpr::float8_e4m3fn>) -> RetT {
|
||||
return adt::errors::TypeError{
|
||||
"float8_e4m3fn is not supported in SO function calls; consider "
|
||||
"using "
|
||||
"higher-precision floating-point types"};
|
||||
},
|
||||
[&](axpr::CppDataType<axpr::float8_e5m2>) -> RetT {
|
||||
return adt::errors::TypeError{
|
||||
"float8_e5m2 is not supported in SO function calls; consider "
|
||||
"using "
|
||||
"higher-precision floating-point types"};
|
||||
},
|
||||
[&](axpr::CppDataType<axpr::float16>) -> RetT {
|
||||
return adt::errors::TypeError{
|
||||
"float16 (half precision) is not supported in SO function calls; "
|
||||
"use "
|
||||
"float instead if possible"};
|
||||
},
|
||||
[&](axpr::CppDataType<axpr::complex64>) -> RetT {
|
||||
return adt::errors::TypeError{
|
||||
"complex64 is not supported in SO function calls; decompose into "
|
||||
"real and imaginary parts manually"};
|
||||
},
|
||||
[&](axpr::CppDataType<axpr::complex128>) -> RetT {
|
||||
return adt::errors::TypeError{
|
||||
"complex128 is not supported in SO function calls; handle "
|
||||
"complex "
|
||||
"arithmetic explicitly"};
|
||||
},
|
||||
[&](axpr::CppDataType<axpr::pstring>) -> RetT {
|
||||
return adt::errors::TypeError{
|
||||
"pstring is not supported in SO function calls; use const char* "
|
||||
"or "
|
||||
"void* with length metadata instead"};
|
||||
},
|
||||
[&](axpr::CppDataType<adt::Undefined>) -> RetT { return "void"; });
|
||||
}
|
||||
|
||||
adt::Result<std::string> GenCode4PointerType(
|
||||
const axpr::PointerType& pointer_type) {
|
||||
using RetT = adt::Result<std::string>;
|
||||
return pointer_type.Match(
|
||||
[&](axpr::CppPointerType<bool*>) -> RetT { return "bool*"; },
|
||||
[&](axpr::CppPointerType<int8_t*>) -> RetT { return "int8_t*"; },
|
||||
[&](axpr::CppPointerType<uint8_t*>) -> RetT { return "uint8_t*"; },
|
||||
[&](axpr::CppPointerType<int16_t*>) -> RetT { return "int16_t*"; },
|
||||
[&](axpr::CppPointerType<uint16_t*>) -> RetT { return "uint16_t*"; },
|
||||
[&](axpr::CppPointerType<int32_t*>) -> RetT { return "int32_t*"; },
|
||||
[&](axpr::CppPointerType<uint32_t*>) -> RetT { return "uint32_t*"; },
|
||||
[&](axpr::CppPointerType<int64_t*>) -> RetT { return "int64_t*"; },
|
||||
[&](axpr::CppPointerType<uint64_t*>) -> RetT { return "uint64_t*"; },
|
||||
[&](axpr::CppPointerType<float*>) -> RetT { return "float*"; },
|
||||
[&](axpr::CppPointerType<double*>) -> RetT { return "double*"; },
|
||||
[&](axpr::CppPointerType<axpr::bfloat16*>) -> RetT { return "void*"; },
|
||||
[&](axpr::CppPointerType<axpr::float8_e4m3fn*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<axpr::float8_e5m2*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<axpr::float16*>) -> RetT { return "void*"; },
|
||||
[&](axpr::CppPointerType<axpr::complex64*>) -> RetT { return "void*"; },
|
||||
[&](axpr::CppPointerType<axpr::complex128*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<axpr::pstring*>) -> RetT { return "void*"; },
|
||||
[&](axpr::CppPointerType<void*>) -> RetT { return "void*"; },
|
||||
[&](axpr::CppPointerType<const bool*>) -> RetT { return "bool*"; },
|
||||
[&](axpr::CppPointerType<const int8_t*>) -> RetT { return "int8_t*"; },
|
||||
[&](axpr::CppPointerType<const uint8_t*>) -> RetT {
|
||||
return "uint8_t*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const int16_t*>) -> RetT {
|
||||
return "int16_t*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const uint16_t*>) -> RetT {
|
||||
return "uint16_t*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const int32_t*>) -> RetT {
|
||||
return "int32_t*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const uint32_t*>) -> RetT {
|
||||
return "uint32_t*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const int64_t*>) -> RetT {
|
||||
return "int64_t*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const uint64_t*>) -> RetT {
|
||||
return "uint64_t*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const float*>) -> RetT { return "float*"; },
|
||||
[&](axpr::CppPointerType<const double*>) -> RetT { return "double*"; },
|
||||
[&](axpr::CppPointerType<const axpr::bfloat16*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const axpr::float8_e4m3fn*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const axpr::float8_e5m2*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const axpr::float16*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const axpr::complex64*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const axpr::complex128*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const axpr::pstring*>) -> RetT {
|
||||
return "void*";
|
||||
},
|
||||
[&](axpr::CppPointerType<const void*>) -> RetT { return "void*"; });
|
||||
}
|
||||
|
||||
adt::Result<std::string> GetCompileCmd() {
|
||||
return "gcc -fPIC -shared api_wrapper.c -o api_wrapper.so";
|
||||
}
|
||||
|
||||
adt::Result<std::string> GetSoRelativePath() { return "api_wrapper.so"; }
|
||||
};
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/axpr/value_method_class.h"
|
||||
#include "paddle/ap/include/code_module/adt.h"
|
||||
#include "paddle/ap/include/code_module/data_type.h"
|
||||
|
||||
namespace phi {
|
||||
|
||||
class DenseTensor;
|
||||
|
||||
}
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
using ap::axpr::DataType;
|
||||
using ap::axpr::MethodClass;
|
||||
using ap::axpr::PointerType;
|
||||
|
||||
using ArgTypeImpl = std::variant<DataType, PointerType>;
|
||||
|
||||
struct ArgType : public ArgTypeImpl {
|
||||
using ArgTypeImpl::ArgTypeImpl;
|
||||
ADT_DEFINE_VARIANT_METHODS(ArgTypeImpl);
|
||||
|
||||
const char* Name() const {
|
||||
return Match([](const auto& impl) { return impl.Name(); });
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool IsType() const {
|
||||
if constexpr (std::is_pointer_v<T>) {
|
||||
const auto& pointer_type = this->template TryGet<ap::axpr::PointerType>();
|
||||
if (pointer_type.HasError()) {
|
||||
return false;
|
||||
}
|
||||
return pointer_type.GetOkValue()
|
||||
.template Has<ap::axpr::CppPointerType<T>>();
|
||||
} else {
|
||||
const auto& data_type = this->template TryGet<ap::axpr::DataType>();
|
||||
if (data_type.HasError()) {
|
||||
return false;
|
||||
}
|
||||
return data_type.GetOkValue().template Has<ap::axpr::CppDataType<T>>();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename ValueT>
|
||||
adt::Result<ValueT> CastTo() const {
|
||||
return Match([](const auto& impl) -> adt::Result<ValueT> { return impl; });
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ValueT>
|
||||
Result<ArgType> CastToArgType(const ValueT& val) {
|
||||
return val.Match(
|
||||
[&](const DataType& atype) -> Result<ArgType> { return ArgType{atype}; },
|
||||
[&](const PointerType& ptype) -> Result<ArgType> {
|
||||
return ArgType{ptype};
|
||||
},
|
||||
[&](const auto&) -> Result<ArgType> {
|
||||
return adt::errors::TypeError{std::string() +
|
||||
"CastToArgType failed. expected types: "
|
||||
"(DataType, PointerType), actual type: " +
|
||||
axpr::GetTypeName(val)};
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/builtin_frame_util.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/code_module_method_class.h"
|
||||
#include "paddle/ap/include/code_module/directory_method_class.h"
|
||||
#include "paddle/ap/include/code_module/file_content_method_class.h"
|
||||
#include "paddle/ap/include/code_module/func_declare_method_class.h"
|
||||
#include "paddle/ap/include/code_module/package_method_class.h"
|
||||
#include "paddle/ap/include/code_module/project_method_class.h"
|
||||
#include "paddle/ap/include/code_module/soft_link_method_class.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
template <typename ValueT, typename DoEachT>
|
||||
void VisitEachBuiltinFrameAttr(const DoEachT& DoEach) {
|
||||
DoEach(GetFileContentClass());
|
||||
DoEach(GetSoftLinkClass());
|
||||
DoEach(GetDirectoryClass());
|
||||
DoEach(GetProjectClass());
|
||||
DoEach(GetPackageClass());
|
||||
DoEach(GetFuncDeclareClass());
|
||||
DoEach(GetCodeModuleClass());
|
||||
}
|
||||
|
||||
template <typename ValueT>
|
||||
axpr::AttrMap<ValueT> MakeBuiltinFrameAttrMap() {
|
||||
axpr::AttrMap<ValueT> attr_map;
|
||||
axpr::VisitEachBuiltinFrameAttr<ValueT>(
|
||||
[&](const std::string& k, const ValueT& v) { attr_map->Set(k, v); });
|
||||
VisitEachBuiltinFrameAttr<ValueT>([&](const auto& cls) {
|
||||
attr_map->Set(cls.Name(), cls);
|
||||
attr_map->Set(std::string("__builtin__") + cls.Name(), cls);
|
||||
});
|
||||
return attr_map;
|
||||
}
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/type.h"
|
||||
#include "paddle/ap/include/code_module/adt.h"
|
||||
#include "paddle/ap/include/code_module/arg_type.h"
|
||||
#include "paddle/ap/include/code_module/data_type.h"
|
||||
#include "paddle/ap/include/code_module/func_declare.h"
|
||||
#include "paddle/ap/include/code_module/source_code.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
struct CodeModuleImpl {
|
||||
adt::List<FuncDeclare> func_declares;
|
||||
SourceCode source_code;
|
||||
|
||||
bool operator==(const CodeModuleImpl& other) const {
|
||||
return other.func_declares == this->func_declares &&
|
||||
other.source_code == this->source_code;
|
||||
}
|
||||
};
|
||||
ADT_DEFINE_RC(CodeModule, CodeModuleImpl);
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/method_class.h"
|
||||
#include "paddle/ap/include/axpr/naive_class_ops.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/code_module.h"
|
||||
#include "paddle/ap/include/code_module/func_declare.h"
|
||||
#include "paddle/ap/include/code_module/source_code.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
axpr::TypeImpl<axpr::BuiltinClassInstance<axpr::Value>> GetCodeModuleClass();
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2024 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/common/data_type.h"
|
||||
#include "paddle/phi/common/pstring.h"
|
||||
|
||||
namespace ap {
|
||||
|
||||
using complex64 = ::phi::dtype::complex<float>;
|
||||
using complex128 = ::phi::dtype::complex<double>;
|
||||
using float16 = ::phi::dtype::float16;
|
||||
using bfloat16 = ::phi::dtype::bfloat16;
|
||||
using float8_e4m3fn = ::phi::dtype::float8_e4m3fn;
|
||||
using float8_e5m2 = ::phi::dtype::float8_e5m2;
|
||||
using pstring = ::phi::dtype::pstring;
|
||||
|
||||
#define AP_FOR_EACH_INT_TYPE(_) \
|
||||
_(int8) \
|
||||
_(uint8) \
|
||||
_(int16) \
|
||||
_(uint16) \
|
||||
_(int32) \
|
||||
_(uint32) \
|
||||
_(int64) \
|
||||
_(uint64)
|
||||
|
||||
} // namespace ap
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/attr_map.h"
|
||||
#include "paddle/ap/include/axpr/type.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
template <typename FileT>
|
||||
struct Directory {
|
||||
axpr::AttrMap<FileT> dentry2file;
|
||||
|
||||
bool operator==(const Directory& other) const {
|
||||
return this->dentry2file == other.dentry2file;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/method_class.h"
|
||||
#include "paddle/ap/include/axpr/naive_class_ops.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/directory.h"
|
||||
#include "paddle/ap/include/code_module/file.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
axpr::TypeImpl<axpr::BuiltinClassInstance<axpr::Value>> GetDirectoryClass();
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/type.h"
|
||||
#include "paddle/ap/include/code_module/directory.h"
|
||||
#include "paddle/ap/include/code_module/file_content.h"
|
||||
#include "paddle/ap/include/code_module/soft_link.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
template <typename FileT>
|
||||
using FileImpl = std::variant<FileContent, SoftLink, Directory<FileT>>;
|
||||
|
||||
struct File : public FileImpl<File> {
|
||||
using FileImpl<File>::FileImpl;
|
||||
ADT_DEFINE_VARIANT_METHODS(FileImpl<File>);
|
||||
|
||||
static adt::Result<File> CastFromAxprValue(const axpr::Value& val) {
|
||||
if (val.template CastableTo<FileContent>()) {
|
||||
ADT_LET_CONST_REF(file_content, val.template CastTo<FileContent>());
|
||||
return file_content;
|
||||
}
|
||||
if (val.template CastableTo<SoftLink>()) {
|
||||
ADT_LET_CONST_REF(soft_link, val.template CastTo<SoftLink>());
|
||||
return soft_link;
|
||||
}
|
||||
if (val.template CastableTo<Directory<File>>()) {
|
||||
ADT_LET_CONST_REF(directory, val.template CastTo<Directory<File>>());
|
||||
return directory;
|
||||
}
|
||||
return adt::errors::TypeError{"File::CastFromAxprValue() failed."};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/type.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
struct FileContentImpl {
|
||||
std::string file_content;
|
||||
|
||||
bool operator==(const FileContentImpl& other) const {
|
||||
return this->file_content == other.file_content;
|
||||
}
|
||||
};
|
||||
ADT_DEFINE_RC(FileContent, FileContentImpl);
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/method_class.h"
|
||||
#include "paddle/ap/include/axpr/naive_class_ops.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/file_content.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
axpr::TypeImpl<axpr::BuiltinClassInstance<axpr::Value>> GetFileContentClass();
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/type.h"
|
||||
#include "paddle/ap/include/code_module/adt.h"
|
||||
#include "paddle/ap/include/code_module/arg_type.h"
|
||||
#include "paddle/ap/include/code_module/data_type.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
using FuncId = std::string;
|
||||
|
||||
struct FuncDeclareImpl {
|
||||
ArgType ret_type;
|
||||
FuncId func_id;
|
||||
adt::List<ArgType> arg_types;
|
||||
|
||||
bool operator==(const FuncDeclareImpl& other) const {
|
||||
return other.func_id == this->func_id && other.ret_type == this->ret_type &&
|
||||
other.arg_types == this->arg_types;
|
||||
}
|
||||
};
|
||||
ADT_DEFINE_RC(FuncDeclare, FuncDeclareImpl);
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/method_class.h"
|
||||
#include "paddle/ap/include/axpr/naive_class_ops.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/func_declare.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
axpr::TypeImpl<axpr::BuiltinClassInstance<axpr::Value>> GetFuncDeclareClass();
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,86 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/code_module/api_wrapper_project_maker.h"
|
||||
#include "paddle/ap/include/code_module/code_module.h"
|
||||
#include "paddle/ap/include/code_module/package.h"
|
||||
#include "paddle/ap/include/code_module/project_compile_helper.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
class ModuleCompileHelper {
|
||||
std::string workspace_dir_;
|
||||
std::string relative_dir_in_workspace_;
|
||||
|
||||
public:
|
||||
ModuleCompileHelper(const std::string& workspace_dir,
|
||||
const std::string& relative_dir_in_workspace)
|
||||
: workspace_dir_(workspace_dir),
|
||||
relative_dir_in_workspace_(relative_dir_in_workspace) {}
|
||||
|
||||
adt::Result<CodeModule> CompileProjectModuleToPackageModule(
|
||||
const CodeModule& project_module) const {
|
||||
ADT_CHECK(project_module->source_code.template Has<Project>());
|
||||
const auto& func_declares = project_module->func_declares.vector();
|
||||
ADT_LET_CONST_REF(
|
||||
api_wrapper_project,
|
||||
code_module::ApiWrapperProjectMaker{}.Make(func_declares));
|
||||
ADT_LET_CONST_REF(main_project, GetMainProject(project_module));
|
||||
code_module::ProjectCompileHelper api_wrapper_compile_helper(
|
||||
GetApiWrapperProjectAbsoluteDir(), api_wrapper_project);
|
||||
code_module::ProjectCompileHelper main_compile_helper(
|
||||
GetMainProjectAbsoluteDir(), main_project);
|
||||
ADT_RETURN_IF_ERR(api_wrapper_compile_helper.DumpNestedFilesToFs());
|
||||
ADT_RETURN_IF_ERR(main_compile_helper.DumpNestedFilesToFs());
|
||||
ADT_RETURN_IF_ERR(api_wrapper_compile_helper.Compile());
|
||||
ADT_RETURN_IF_ERR(main_compile_helper.Compile());
|
||||
std::string api_wrapper_so_relative_path =
|
||||
GetApiWrapperProjectRelativeDir() + "/" +
|
||||
api_wrapper_project->so_relative_path;
|
||||
std::string main_so_relative_path =
|
||||
GetMainProjectRelativeDir() + "/" + main_project->so_relative_path;
|
||||
Package ret_package{
|
||||
/*nested_files=*/Directory<File>{},
|
||||
/*api_wrapper_so_relative_path=*/api_wrapper_so_relative_path,
|
||||
/*main_so_relative_path=*/main_so_relative_path,
|
||||
/*others=*/axpr::AttrMap<axpr::SerializableValue>{}};
|
||||
return CodeModule{project_module->func_declares, ret_package};
|
||||
}
|
||||
|
||||
private:
|
||||
adt::Result<code_module::Project> GetMainProject(
|
||||
const code_module::CodeModule& code_module) const {
|
||||
return code_module->source_code.template TryGet<code_module::Project>();
|
||||
}
|
||||
|
||||
std::string GetApiWrapperProjectAbsoluteDir() const {
|
||||
return workspace_dir_ + "/" + GetApiWrapperProjectRelativeDir();
|
||||
}
|
||||
|
||||
std::string GetMainProjectAbsoluteDir() const {
|
||||
return workspace_dir_ + "/" + GetMainProjectRelativeDir();
|
||||
}
|
||||
|
||||
std::string GetApiWrapperProjectRelativeDir() const {
|
||||
return relative_dir_in_workspace_ + "/api_wrapper/";
|
||||
}
|
||||
std::string GetMainProjectRelativeDir() const {
|
||||
return relative_dir_in_workspace_ + "/main/";
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,154 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/builtin_serializable_attr_map_to_axpr_helper.h"
|
||||
#include "paddle/ap/include/axpr/lambda_expr_builder.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/code_module.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
struct ModuleToAxprHelper {
|
||||
using AnfExpr = axpr::AnfExpr;
|
||||
|
||||
adt::Result<AnfExpr> ConvertModuleToAnfExpr(axpr::LetContext* ctx,
|
||||
const CodeModule& m) const {
|
||||
return ConvertModuleToAnfExprImpl(ctx, m);
|
||||
}
|
||||
|
||||
adt::Result<AnfExpr> ConvertModuleToAnfExpr(const CodeModule& m) const {
|
||||
auto ConstructLambdaBody = [&](auto& ctx) -> adt::Result<AnfExpr> {
|
||||
return ConvertModuleToAnfExprImpl(&ctx, m);
|
||||
};
|
||||
return ap::axpr::LambdaExprBuilder{}.TryLambda({}, ConstructLambdaBody);
|
||||
}
|
||||
|
||||
private:
|
||||
adt::Result<AnfExpr> ConvertModuleToAnfExprImpl(axpr::LetContext* ctx,
|
||||
const CodeModule& m) const {
|
||||
auto ConvertArgType = [&](auto& ctx, const auto& arg_type) -> AnfExpr {
|
||||
return arg_type.Match(
|
||||
[&](const ap::axpr::DataType& data_type) -> AnfExpr {
|
||||
const auto& var = ctx->Var("DataType").Attr(data_type.Name());
|
||||
return ap::axpr::tVar<std::string>{var.name()};
|
||||
},
|
||||
[&](const ap::axpr::PointerType& pointer_type) -> AnfExpr {
|
||||
const auto& var = ctx->Var("PointerType").Attr(pointer_type.Name());
|
||||
return ap::axpr::tVar<std::string>{var.name()};
|
||||
});
|
||||
};
|
||||
auto ConvertFuncDeclareCall = [&](auto& ctx,
|
||||
const auto& func_declare) -> AnfExpr {
|
||||
const auto& ret_val_anf_expr =
|
||||
ConvertArgType(ctx, func_declare->ret_type);
|
||||
const auto& func_name = ctx->String(func_declare->func_id);
|
||||
std::vector<AnfExpr> elts;
|
||||
elts.reserve(func_declare->arg_types->size());
|
||||
for (const auto& arg_type : *func_declare->arg_types) {
|
||||
elts.emplace_back(ConvertArgType(ctx, arg_type));
|
||||
}
|
||||
const auto& arg_type_anf_expr = ctx->Call(ap::axpr::kBuiltinList(), elts);
|
||||
return ctx->Call(
|
||||
"FuncDeclare", ret_val_anf_expr, func_name, arg_type_anf_expr);
|
||||
};
|
||||
auto ConvertFuncDeclareList = [&](auto& ctx) -> AnfExpr {
|
||||
std::vector<AnfExpr> elts;
|
||||
elts.reserve(m->func_declares->size());
|
||||
for (const auto& func_declare : *m->func_declares) {
|
||||
elts.emplace_back(ConvertFuncDeclareCall(ctx, func_declare));
|
||||
}
|
||||
return ctx->Call(ap::axpr::kBuiltinList(), elts);
|
||||
};
|
||||
auto ConvertSourceCodeConstruction =
|
||||
[&](auto* ctx) -> adt::Result<AnfExpr> {
|
||||
return m->source_code.Match(
|
||||
[&](const ap::code_module::Project& project) -> adt::Result<AnfExpr> {
|
||||
return ConvertProjectConstruct(ctx, project);
|
||||
},
|
||||
[&](const ap::code_module::Package& package) -> adt::Result<AnfExpr> {
|
||||
return ConvertPackageConstruct(ctx, package);
|
||||
});
|
||||
};
|
||||
const auto& declare = ConvertFuncDeclareList(ctx);
|
||||
ADT_LET_CONST_REF(source_code, ConvertSourceCodeConstruction(ctx));
|
||||
return ctx->Call("CodeModule", declare, source_code);
|
||||
}
|
||||
|
||||
adt::Result<AnfExpr> ConvertProjectConstruct(
|
||||
ap::axpr::LetContext* ctx,
|
||||
const ap::code_module::Project& project) const {
|
||||
const auto& attrs = project->others;
|
||||
ADT_LET_CONST_REF(others_anf_expr,
|
||||
GetCodeFromBuiltinSerializableAttrMap(ctx, attrs));
|
||||
std::map<std::string, AnfExpr> kwargs{
|
||||
{"nested_files", ConvertProjectNestedFiles(ctx, project->nested_files)},
|
||||
{"compile_cmd", AnfExpr{ctx->String(project->compile_cmd)}},
|
||||
{"so_relative_path", AnfExpr{ctx->String(project->so_relative_path)}},
|
||||
{"others", others_anf_expr},
|
||||
};
|
||||
return ctx->Apply("Project", {}, kwargs);
|
||||
}
|
||||
|
||||
adt::Result<AnfExpr> ConvertPackageConstruct(
|
||||
ap::axpr::LetContext* ctx,
|
||||
const ap::code_module::Package& package) const {
|
||||
const auto& attrs = package->others;
|
||||
ADT_LET_CONST_REF(others_anf_expr,
|
||||
GetCodeFromBuiltinSerializableAttrMap(ctx, attrs));
|
||||
const auto& api_so_path = package->api_wrapper_so_relative_path;
|
||||
const auto& main_so_path = package->main_so_relative_path;
|
||||
std::map<std::string, AnfExpr> kwargs{
|
||||
{"nested_files", ConvertProjectNestedFiles(ctx, package->nested_files)},
|
||||
{"api_wrapper_so_relative_path", AnfExpr{ctx->String(api_so_path)}},
|
||||
{"main_so_relative_path", AnfExpr{ctx->String(main_so_path)}},
|
||||
{"others", others_anf_expr},
|
||||
};
|
||||
return ctx->Apply("Package", {}, kwargs);
|
||||
}
|
||||
|
||||
AnfExpr ConvertProjectNestedFiles(ap::axpr::LetContext* ctx,
|
||||
const ap::code_module::File& file) const {
|
||||
return file.Match(
|
||||
[&](const ap::code_module::FileContent& file_content) -> AnfExpr {
|
||||
const auto& str = file_content->file_content;
|
||||
return ctx->Var("Project").Attr("FileContent").Call(ctx->String(str));
|
||||
},
|
||||
[&](const ap::code_module::SoftLink& soft_link) -> AnfExpr {
|
||||
const auto& str = soft_link->target_relative_path;
|
||||
return ctx->Var("Project").Attr("SoftLink").Call(ctx->String(str));
|
||||
},
|
||||
[&](const ap::code_module::Directory<ap::code_module::File>& dir)
|
||||
-> AnfExpr {
|
||||
std::vector<AnfExpr> args;
|
||||
for (const auto& [k, v] : dir.dentry2file->storage) {
|
||||
const auto& v_anf_expr = ConvertProjectNestedFiles(ctx, v);
|
||||
args.emplace_back(ctx->Call(
|
||||
ap::axpr::kBuiltinList(), ctx->String(k), v_anf_expr));
|
||||
}
|
||||
return ctx->Apply(ctx->Var("Project").Attr("Directory"), args);
|
||||
});
|
||||
}
|
||||
|
||||
adt::Result<AnfExpr> GetCodeFromBuiltinSerializableAttrMap(
|
||||
ap::axpr::LetContext* ctx,
|
||||
const ap::axpr::AttrMap<ap::axpr::SerializableValue>& attr_map) const {
|
||||
return axpr::BuiltinSerializableAttrMapToAxprHelper{}.Convert(ctx,
|
||||
attr_map);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/attr_map.h"
|
||||
#include "paddle/ap/include/axpr/serializable_value.h"
|
||||
#include "paddle/ap/include/axpr/type.h"
|
||||
#include "paddle/ap/include/code_module/adt.h"
|
||||
#include "paddle/ap/include/code_module/arg_type.h"
|
||||
#include "paddle/ap/include/code_module/data_type.h"
|
||||
#include "paddle/ap/include/code_module/file.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
struct PackageImpl {
|
||||
Directory<File> nested_files;
|
||||
std::string api_wrapper_so_relative_path;
|
||||
std::string main_so_relative_path;
|
||||
axpr::AttrMap<axpr::SerializableValue> others;
|
||||
|
||||
bool operator==(const PackageImpl& other) const { return this == &other; }
|
||||
};
|
||||
ADT_DEFINE_RC(Package, PackageImpl);
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/method_class.h"
|
||||
#include "paddle/ap/include/axpr/naive_class_ops.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/file.h"
|
||||
#include "paddle/ap/include/code_module/package.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
axpr::TypeImpl<axpr::BuiltinClassInstance<axpr::Value>> GetPackageClass();
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/attr_map.h"
|
||||
#include "paddle/ap/include/axpr/serializable_value.h"
|
||||
#include "paddle/ap/include/axpr/type.h"
|
||||
#include "paddle/ap/include/code_module/adt.h"
|
||||
#include "paddle/ap/include/code_module/arg_type.h"
|
||||
#include "paddle/ap/include/code_module/data_type.h"
|
||||
#include "paddle/ap/include/code_module/file.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
struct ProjectImpl {
|
||||
Directory<File> nested_files;
|
||||
std::string compile_cmd;
|
||||
std::string so_relative_path;
|
||||
axpr::AttrMap<axpr::SerializableValue> others;
|
||||
|
||||
bool operator==(const ProjectImpl& other) const { return this == &other; }
|
||||
};
|
||||
ADT_DEFINE_RC(Project, ProjectImpl);
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,130 @@
|
||||
// Copyright (c) 2024 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 <sys/wait.h>
|
||||
#include <fstream>
|
||||
#include "paddle/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/code_module/project.h"
|
||||
#include "paddle/ap/include/env/ap_path.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
struct ProjectCompileHelper {
|
||||
ProjectCompileHelper(const std::string& workspace_dir_val,
|
||||
const Project& project_val)
|
||||
: workspace_dir(workspace_dir_val), project(project_val) {}
|
||||
|
||||
adt::Result<adt::Ok> DumpNestedFilesToFs() {
|
||||
return DumpNestedFilesToFs(this->project->nested_files, "");
|
||||
}
|
||||
|
||||
adt::Result<adt::Ok> Compile() {
|
||||
int ret_code = 0;
|
||||
std::string change_dir_cmd = std::string() + "cd " + this->workspace_dir;
|
||||
std::string compile_cmd =
|
||||
change_dir_cmd + "; " + this->project->compile_cmd;
|
||||
ret_code = WEXITSTATUS(std::system(compile_cmd.c_str()));
|
||||
ADT_CHECK(ret_code == 0) << adt::errors::RuntimeError{
|
||||
std::string() + "system() failed. ret_code: " +
|
||||
std::to_string(ret_code) + ", compile_cmd: " + compile_cmd};
|
||||
return adt::Ok{};
|
||||
}
|
||||
|
||||
std::string GetSoPath() {
|
||||
return this->workspace_dir + "/" + this->project->so_relative_path;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string workspace_dir;
|
||||
Project project;
|
||||
|
||||
adt::Result<adt::Ok> DumpNestedFilesToFs(
|
||||
const Directory<File>& directory, const std::string& relative_dir_path) {
|
||||
std::string dir_path = this->workspace_dir + "/" + relative_dir_path;
|
||||
std::string cmd = std::string() + "mkdir -p " + dir_path;
|
||||
int ret = std::system(cmd.c_str());
|
||||
ADT_CHECK(ret != -1 && WIFEXITED(ret) && WEXITSTATUS(ret) == 0)
|
||||
<< adt::errors::RuntimeError{std::string() +
|
||||
"mkdir failed. dir_path: " + dir_path +
|
||||
", system return: " + std::to_string(ret)};
|
||||
using Ok = adt::Result<adt::Ok>;
|
||||
for (const auto& [dentry, file] : directory.dentry2file->storage) {
|
||||
ADT_RETURN_IF_ERR(file.Match(
|
||||
[&](const FileContent& file_content) -> Ok {
|
||||
return DumpFileContentToFs(file_content,
|
||||
relative_dir_path + "/" + dentry);
|
||||
},
|
||||
[&](const SoftLink& soft_link) -> Ok {
|
||||
return DumpSoftLinkToFs(soft_link,
|
||||
relative_dir_path + "/" + dentry);
|
||||
},
|
||||
[&](const Directory<File>& sub_dir) -> Ok {
|
||||
return DumpNestedFilesToFs(sub_dir,
|
||||
relative_dir_path + "/" + dentry);
|
||||
}));
|
||||
}
|
||||
return adt::Ok{};
|
||||
}
|
||||
|
||||
adt::Result<adt::Ok> DumpFileContentToFs(
|
||||
const FileContent& file_content, const std::string& relative_file_path) {
|
||||
std::string file_path = this->workspace_dir + "/" + relative_file_path;
|
||||
std::ofstream of{file_path};
|
||||
ADT_CHECK(of.is_open()) << adt::errors::RuntimeError{
|
||||
std::string() + "file open failed. file_path: " + file_path};
|
||||
of << file_content->file_content;
|
||||
of.close();
|
||||
return adt::Ok{};
|
||||
}
|
||||
|
||||
adt::Result<adt::Ok> DumpSoftLinkToFs(const SoftLink& soft_link,
|
||||
const std::string& relative_link_path) {
|
||||
std::string link = this->workspace_dir + "/" + relative_link_path;
|
||||
std::optional<std::string> target_path;
|
||||
auto FindExistedSourcePath =
|
||||
[&](const auto& prefix) -> adt::Result<adt::LoopCtrl> {
|
||||
std::string cur_target_path =
|
||||
std::string() + prefix + "/" + soft_link->target_relative_path;
|
||||
if (FileExists(cur_target_path)) {
|
||||
target_path = cur_target_path;
|
||||
return adt::Break{};
|
||||
} else {
|
||||
return adt::Continue{};
|
||||
}
|
||||
};
|
||||
ADT_RETURN_IF_ERR(env::VisitEachApPath(FindExistedSourcePath));
|
||||
ADT_CHECK(target_path.has_value()) << adt::errors::RuntimeError{
|
||||
std::string() +
|
||||
"link failed. relative_path: " + soft_link->target_relative_path};
|
||||
std::string cmd =
|
||||
std::string() + "ln -s " + target_path.value() + " " + link;
|
||||
ADT_CHECK(WEXITSTATUS(std::system(cmd.c_str())) == 0);
|
||||
return adt::Ok{};
|
||||
}
|
||||
|
||||
bool FileExists(const std::string& filepath) {
|
||||
std::fstream fp;
|
||||
fp.open(filepath, std::fstream::in);
|
||||
if (fp.is_open()) {
|
||||
fp.close();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/method_class.h"
|
||||
#include "paddle/ap/include/axpr/naive_class_ops.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/directory.h"
|
||||
#include "paddle/ap/include/code_module/directory_method_class.h"
|
||||
#include "paddle/ap/include/code_module/file.h"
|
||||
#include "paddle/ap/include/code_module/file_content_method_class.h"
|
||||
#include "paddle/ap/include/code_module/project.h"
|
||||
#include "paddle/ap/include/code_module/soft_link_method_class.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
axpr::TypeImpl<axpr::BuiltinClassInstance<axpr::Value>> GetProjectClass();
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2024 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 ap::code_module {
|
||||
|
||||
class RtModule {
|
||||
public:
|
||||
virtual ~RtModule() = default;
|
||||
|
||||
protected:
|
||||
RtModule() = default;
|
||||
};
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/type.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
struct SoftLinkImpl {
|
||||
std::string target_relative_path;
|
||||
|
||||
bool operator==(const SoftLinkImpl& other) const {
|
||||
return this->target_relative_path == other.target_relative_path;
|
||||
}
|
||||
};
|
||||
ADT_DEFINE_RC(SoftLink, SoftLinkImpl);
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/method_class.h"
|
||||
#include "paddle/ap/include/axpr/naive_class_ops.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/soft_link.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
axpr::TypeImpl<axpr::BuiltinClassInstance<axpr::Value>> GetSoftLinkClass();
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/code_module/package.h"
|
||||
#include "paddle/ap/include/code_module/project.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
using SourceCodeImpl = std::variant<Project, Package>;
|
||||
|
||||
struct SourceCode : public SourceCodeImpl {
|
||||
using SourceCodeImpl::SourceCodeImpl;
|
||||
ADT_DEFINE_VARIANT_METHODS(SourceCodeImpl);
|
||||
|
||||
static adt::Result<SourceCode> CastFromAxprValue(const axpr::Value& val) {
|
||||
if (val.template CastableTo<Project>()) {
|
||||
ADT_LET_CONST_REF(project, val.template CastTo<Project>());
|
||||
return project;
|
||||
}
|
||||
if (val.template CastableTo<Package>()) {
|
||||
ADT_LET_CONST_REF(package, val.template CastTo<Package>());
|
||||
return package;
|
||||
}
|
||||
return adt::errors::TypeError{"SourceCode::CastFromAxprValue() failed"};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2024 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/ap/include/adt/adt.h"
|
||||
#include "paddle/ap/include/axpr/data_type.h"
|
||||
#include "paddle/ap/include/axpr/pointer_type.h"
|
||||
#include "paddle/ap/include/axpr/value.h"
|
||||
#include "paddle/ap/include/code_module/adt.h"
|
||||
#include "paddle/ap/include/code_module/code_module.h"
|
||||
#include "paddle/ap/include/code_module/data_type.h"
|
||||
#include "paddle/ap/include/code_module/func_declare.h"
|
||||
|
||||
namespace ap::code_module {
|
||||
|
||||
using axpr::Value;
|
||||
|
||||
} // namespace ap::code_module
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2024 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/ap/include/axpr/data_type_method_class.h"
|
||||
#include "paddle/ap/include/axpr/pointer_type_method_class.h"
|
||||
#include "paddle/ap/include/axpr/value_method_class.h"
|
||||
Reference in New Issue
Block a user