Skip to content

Commit

Permalink
zzf/add adaptor (DeepLink-org#440)
Browse files Browse the repository at this point in the history
* add adaptor
  • Loading branch information
zhangzefeng92 authored Oct 11, 2023
1 parent 61ddbb3 commit 5fd1071
Show file tree
Hide file tree
Showing 60 changed files with 265 additions and 435 deletions.
5 changes: 4 additions & 1 deletion diopi_test/python/conformance/conformance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def convert_input_tensors(function_paras: dict, test_tag: list, nhwc_list=[], dt
if glob_vars.four_bytes and (para in dtype_list) \
and tensor is not None and tensor.dtype == np.int64:
tensor = tensor.astype(np.int32)
if isinstance(function_paras['kwargs'][para], np.ndarray):

if isinstance(tensor, Tensor):
tensor = tensor.numpy()
if isinstance(tensor, np.ndarray):
ndim = tensor.ndim
if glob_vars.nhwc and (para in nhwc_list):
if ndim < glob_vars.nhwc_min_dim or ndim > 5:
Expand Down
32 changes: 22 additions & 10 deletions impl/ascend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,33 @@ endif()
file(GLOB_RECURSE IMPL_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} functions/*.cpp common/*.cpp)
list(APPEND IMPL_SRC ascend_tensor.cpp)

# adaptor
set(USE_ADAPTOR OFF)
if(EXISTS "${PROJECT_SOURCE_DIR}/convert_config.yaml")
set(USE_ADAPTOR ON)
endif()

if(USE_ADAPTOR)

file(GLOB ADAPTOR_TEMPLATE_CODE RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${ADAPTOR_DIR}/codegen/*.py)
add_custom_target(adaptor_gen_dependency DEPENDS ${ADAPTOR_TEMPLATE_CODE})

set(ADAPTOR_CSRC_PATH "${ADAPTOR_DIR}/csrc")
set(GEN_FILES ${ADAPTOR_CSRC_PATH}/diopi_adaptor.cpp ${ADAPTOR_CSRC_PATH}/impl_functions.hpp)
add_custom_target(adaptor_code_gen
COMMAND python3 ${ADAPTOR_DIR}/codegen/gen.py --diopi_dir=${CMAKE_SOURCE_DIR}/../ --output_dir=${ADAPTOR_CSRC_PATH} --config_device=ascend
BYPRODUCTS ${GEN_FILES}
DEPENDS adaptor_gen_dependency)
list(APPEND IMPL_SRC ${GEN_FILES} ${ADAPTOR_CSRC_PATH}/convert.cpp ${ADAPTOR_CSRC_PATH}/diopi_adaptor.cpp)
add_definitions(-DTEST_USE_ADAPTOR)
endif()

add_library(${DEVICEIMPL} SHARED ${IMPL_SRC})
set_target_properties(${DEVICEIMPL} PROPERTIES SUFFIX ".so")

target_link_libraries(${DEVICEIMPL} ascendcl acl_op_compiler)

if(EXISTS "${PROJECT_SOURCE_DIR}/convert_config.yaml")
set(ADAPTOR_GEN_PATH "${CMAKE_SOURCE_DIR}/../adaptor/codegen")
add_custom_target(adaptor_code_gen COMMAND python3 ${ADAPTOR_GEN_PATH}/gen.py
--diopi_dir=${CMAKE_SOURCE_DIR}/../ --output_dir=${ADAPTOR_GEN_PATH}/../csrc/
--config_device=ascend)
set(USE_ADAPTOR "true")
if(USE_ADAPTOR)
add_dependencies(${DEVICEIMPL} adaptor_code_gen)
add_definitions(-DTEST_USE_ADAPTOR)
else()
set(USE_ADAPTOR "false")
endif()

if (TEST)
Expand Down
8 changes: 4 additions & 4 deletions impl/ascend/common/acloprunner.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#ifndef IMPL_ASCEND_COMMON_ACLOPRUNNER_HPP_
#define IMPL_ASCEND_COMMON_ACLOPRUNNER_HPP_

#include <acl/acl.h>
#include <acl/acl_op.h>
#include <acl/acl_op_compiler.h>
#include <diopi/functions.h>
#include <stdint.h>

#include <algorithm>
Expand All @@ -21,7 +17,11 @@
#include <vector>

#include "../ascend_tensor.hpp"
#include "acl/acl.h"
#include "acl/acl_op.h"
#include "acl/acl_op_compiler.h"
#include "debug.hpp"
#include "impl_functions.hpp"
#include "utils.hpp"

namespace impl {
Expand Down
3 changes: 1 addition & 2 deletions impl/ascend/common/debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#ifndef IMPL_ASCEND_COMMON_DEBUG_HPP_
#define IMPL_ASCEND_COMMON_DEBUG_HPP_

#include <diopi/functions.h>

#include <algorithm>
#include <sstream>
#include <string>

#include "../ascend_tensor.hpp"
#include "impl_functions.hpp"

namespace impl {
namespace ascend {
Expand Down
3 changes: 1 addition & 2 deletions impl/ascend/common/generator_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
* @copyright (c) 2023, DeepLink.
*/

#include <diopi/functions.h>

#include <cstring>

#include "acloprunner.hpp"
#include "impl_functions.hpp"

namespace impl {
namespace ascend {
Expand Down
Empty file added impl/ascend/convert_config.yaml
Empty file.
60 changes: 28 additions & 32 deletions impl/ascend/functions/activation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,44 @@
* @copyright (c) 2023, DeepLink.
*/

#include <diopi/functions.h>

#include <set>

#include "../common/acloprunner.hpp"

namespace impl {
namespace ascend {
extern "C" {
DIOPI_API diopiError_t diopiRelu(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input) {

diopiError_t diopiRelu(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input) {
AclOpRunner<1, 1>("Relu", ctx).addInput(input).addOutput(out).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiReluInp(diopiContextHandle_t ctx, diopiTensorHandle_t input) {
diopiError_t diopiReluInp(diopiContextHandle_t ctx, diopiTensorHandle_t input) {
AclOpRunner<1, 1>("Relu", ctx).addInput(input).addOutput(input).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiSoftmax(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, int64_t dim) {
diopiError_t diopiSoftmax(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, int64_t dim) {
std::vector<int64_t> dimList = {dim};
AclOpRunner<1, 1>("SoftmaxV2", ctx).addInput(input).setAttr<int64_t>("axes", dimList).addOutput(out).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiSoftmaxBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t output, int64_t dim) {
diopiError_t diopiSoftmaxBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput, diopiConstTensorHandle_t output,
int64_t dim) {
std::vector<int64_t> dimList = {dim};
AclOpRunner<2, 1>("SoftmaxGrad", ctx).addInput(output).addInput(gradOutput).setAttr<int64_t>("axes", dimList).addOutput(gradInput).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiLogSoftmax(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, int64_t dim) {
diopiError_t diopiLogSoftmax(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, int64_t dim) {
std::vector<int64_t> dimList = {dim};
AclOpRunner<1, 1>("LogSoftmaxV2", ctx).addInput(input).setAttr("axes", dimList).addOutput(out).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiLogSoftmaxBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t output, int64_t dim) {
diopiError_t diopiLogSoftmaxBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t output, int64_t dim) {
diopiSize_t sumSize;
diopiGetTensorShape(gradOutput, &sumSize);
std::vector<int64_t> sumSizeVec(sumSize.data, sumSize.data + sumSize.len);
Expand All @@ -67,23 +65,22 @@ DIOPI_API diopiError_t diopiLogSoftmaxBackward(diopiContextHandle_t ctx, diopiTe
return diopiSuccess;
}

DIOPI_API diopiError_t diopiSilu(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input) {
diopiError_t diopiSilu(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input) {
AclOpRunner<1, 1>("Swish", ctx).addInput(input).setAttr<float>("scale", 1.0).addOutput(out).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiSiluInp(diopiContextHandle_t ctx, diopiTensorHandle_t input) { return diopiSilu(ctx, input, input); }
diopiError_t diopiSiluInp(diopiContextHandle_t ctx, diopiTensorHandle_t input) { return diopiSilu(ctx, input, input); }

DIOPI_API diopiError_t diopiSiluBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t input) {
diopiError_t diopiSiluBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput, diopiConstTensorHandle_t input) {
diopiTensorHandle_t out;
makeTensorLike(ctx, &out, input);
diopiSilu(ctx, out, input);
AclOpRunner<3, 1>("SwishGrad", ctx).addInput(gradOutput).addInput(input).addInput(out).addOutput(gradInput).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiSigmoid(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input) {
diopiError_t diopiSigmoid(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input) {
AclOpRunner<1, 1>("Sigmoid", ctx).addInput(input).addOutput(out).run();
return diopiSuccess;
}
Expand All @@ -96,28 +93,28 @@ DIOPI_API diopiError_t diopiSigmoidBackward(diopiContextHandle_t ctx, diopiTenso
return diopiSuccess;
}

DIOPI_API diopiError_t diopiGelu(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, const char* approximate) {
diopiError_t diopiGelu(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, const char* approximate) {
AclOpRunner<1, 1>("Gelu", ctx).addInput(input).addOutput(out).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiGeluBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t input, const char* approximate) {
diopiError_t diopiGeluBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput, diopiConstTensorHandle_t input,
const char* approximate) {
AclOpRunner<3, 1>("GeluGrad", ctx).addInput(gradOutput).addInput(input).addInput(gradOutput).addOutput(gradInput).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiLeakyRelu(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, const diopiScalar_t* negativeSlope) {
diopiError_t diopiLeakyRelu(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, const diopiScalar_t* negativeSlope) {
AclOpRunner<1, 1>("LeakyRelu", ctx).addInput(input).setAttr("negative_slope", getValue<float>(negativeSlope)).addOutput(out).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiLeakyReluInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, const diopiScalar_t* negativeSlope) {
diopiError_t diopiLeakyReluInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, const diopiScalar_t* negativeSlope) {
return diopiLeakyRelu(ctx, input, input, negativeSlope);
}

DIOPI_API diopiError_t diopiLeakyReluBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t input, const diopiScalar_t* negativeSlope, bool inputIsResult) {
diopiError_t diopiLeakyReluBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t input, const diopiScalar_t* negativeSlope, bool inputIsResult) {
AclOpRunner<2, 1>("LeakyReluGrad", ctx)
.addInput(gradOutput)
.addInput(input)
Expand All @@ -127,19 +124,18 @@ DIOPI_API diopiError_t diopiLeakyReluBackward(diopiContextHandle_t ctx, diopiTen
return diopiSuccess;
}

DIOPI_API diopiError_t diopiTanh(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input) {
diopiError_t diopiTanh(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input) {
AclOpRunner<1, 1>("Tanh", ctx).addInput(input).addOutput(out).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiTanhBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t output) {
diopiError_t diopiTanhBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput, diopiConstTensorHandle_t output) {
AclOpRunner<2, 1>("TanhGrad", ctx).addInput(output).addInput(gradOutput).addOutput(gradInput).run();
return diopiSuccess;
}

DIOPI_API diopiError_t diopiHardtanh(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, const diopiScalar_t* minVal,
const diopiScalar_t* maxVal) {
diopiError_t diopiHardtanh(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, const diopiScalar_t* minVal,
const diopiScalar_t* maxVal) {
diopiDtype_t dtype;
diopiGetTensorDtype(input, &dtype);
diopiScalar_t min = *minVal;
Expand All @@ -156,12 +152,12 @@ DIOPI_API diopiError_t diopiHardtanh(diopiContextHandle_t ctx, diopiTensorHandle
return diopiSuccess;
}

DIOPI_API diopiError_t diopiHardtanhInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, const diopiScalar_t* minVal, const diopiScalar_t* maxVal) {
diopiError_t diopiHardtanhInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, const diopiScalar_t* minVal, const diopiScalar_t* maxVal) {
return diopiHardtanh(ctx, input, input, minVal, maxVal);
}

DIOPI_API diopiError_t diopiHardtanhBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput,
diopiConstTensorHandle_t input, const diopiScalar_t* minVal, const diopiScalar_t* maxVal) {
diopiError_t diopiHardtanhBackward(diopiContextHandle_t ctx, diopiTensorHandle_t gradInput, diopiConstTensorHandle_t gradOutput, diopiConstTensorHandle_t input,
const diopiScalar_t* minVal, const diopiScalar_t* maxVal) {
diopiDtype_t inDtype;
diopiGetTensorDtype(input, &inDtype);
std::set<diopiDtype_t> supportDtypes{diopi_dtype_float16, diopi_dtype_float32};
Expand Down Expand Up @@ -192,6 +188,6 @@ DIOPI_API diopiError_t diopiHardtanhBackward(diopiContextHandle_t ctx, diopiTens
}
return diopiSuccess;
}
}

} // namespace ascend
} // namespace impl
14 changes: 4 additions & 10 deletions impl/ascend/functions/addcdiv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
* @copyright (c) 2023, DeepLink.
*/

#include <diopi/functions.h>

#include "../common/acloprunner.hpp"

namespace impl {
namespace ascend {

extern "C" {

DIOPI_API diopiError_t diopiAddcdiv(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, diopiConstTensorHandle_t tensor1,
diopiConstTensorHandle_t tensor2, const diopiScalar_t* value) {
diopiError_t diopiAddcdiv(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, diopiConstTensorHandle_t tensor1,
diopiConstTensorHandle_t tensor2, const diopiScalar_t* value) {
diopiTensorHandle_t trOther = nullptr;
diopiDtype_t dtype;
diopiGetTensorDtype(out, &dtype);
Expand All @@ -23,12 +19,10 @@ DIOPI_API diopiError_t diopiAddcdiv(diopiContextHandle_t ctx, diopiTensorHandle_
return diopiSuccess;
}

DIOPI_API diopiError_t diopiAddcdivInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, diopiConstTensorHandle_t tensor1, diopiConstTensorHandle_t tensor2,
const diopiScalar_t* value) {
diopiError_t diopiAddcdivInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, diopiConstTensorHandle_t tensor1, diopiConstTensorHandle_t tensor2,
const diopiScalar_t* value) {
return diopiAddcdiv(ctx, input, input, tensor1, tensor2, value);
}

} // extern "C"

} // namespace ascend
} // namespace impl
14 changes: 4 additions & 10 deletions impl/ascend/functions/addcmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
* @copyright (c) 2023, DeepLink.
*/

#include <diopi/functions.h>

#include "../common/acloprunner.hpp"

namespace impl {
namespace ascend {

extern "C" {

DIOPI_API diopiError_t diopiAddcmul(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, diopiConstTensorHandle_t tensor1,
diopiConstTensorHandle_t tensor2, const diopiScalar_t* value) {
diopiError_t diopiAddcmul(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, diopiConstTensorHandle_t tensor1,
diopiConstTensorHandle_t tensor2, const diopiScalar_t* value) {
diopiTensorHandle_t trOther = nullptr;
diopiDtype_t dtype;
diopiGetTensorDtype(out, &dtype);
Expand All @@ -23,12 +19,10 @@ DIOPI_API diopiError_t diopiAddcmul(diopiContextHandle_t ctx, diopiTensorHandle_
return diopiSuccess;
}

DIOPI_API diopiError_t diopiAddcmulInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, diopiConstTensorHandle_t tensor1, diopiConstTensorHandle_t tensor2,
const diopiScalar_t* value) {
diopiError_t diopiAddcmulInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, diopiConstTensorHandle_t tensor1, diopiConstTensorHandle_t tensor2,
const diopiScalar_t* value) {
return diopiAddcmul(ctx, input, input, tensor1, tensor2, value);
}

} // extern "C"

} // namespace ascend
} // namespace impl
5 changes: 1 addition & 4 deletions impl/ascend/functions/arange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
* @copyright (c) 2023, DeepLink.
*/

#include <diopi/functions.h>

#include "../common/acloprunner.hpp"

namespace impl {
namespace ascend {

extern "C" DIOPI_API diopiError_t diopiArange(diopiContextHandle_t ctx, diopiTensorHandle_t out, const diopiScalar_t* start, const diopiScalar_t* end,
const diopiScalar_t* step) {
diopiError_t diopiArange(diopiContextHandle_t ctx, diopiTensorHandle_t out, const diopiScalar_t* start, const diopiScalar_t* end, const diopiScalar_t* step) {
AclOpRunner<3, 1>("Range", ctx).addConstInput(*start).addConstInput(*end).addConstInput(*step).addOutput(out).run();
return diopiSuccess;
}
Expand Down
4 changes: 1 addition & 3 deletions impl/ascend/functions/argmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
* @copyright (c) 2023, DeepLink.
*/

#include <diopi/functions.h>

#include "../common/acloprunner.hpp"

namespace impl {
namespace ascend {

extern "C" diopiError_t diopiArgmax(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, const int64_t* dim, bool keepdim) {
diopiError_t diopiArgmax(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, const int64_t* dim, bool keepdim) {
AclOpRunner<2, 1>("ArgMaxV2", ctx).addInput(input).addConstInput(*dim, diopi_dtype_int64).setAttr("keep_dims", keepdim).addOutput(out).run();
return diopiSuccess;
}
Expand Down
Loading

0 comments on commit 5fd1071

Please sign in to comment.