support ascend (#165)

* fix

* fix code

* fix format

* fix format

* fix

* fix

* addAbs

* more Unary

* add kernels

* fix concat&pooling test code

* add softmax/element_wise kernel

* fix format

* add reshape

* support for llama

* add maxpooling & flatten

* add conv_transpose&&native maxpooling

* add conv_transpose

* add communication operator

* fix

* style: fix format

* style: fix format

* add depthTospace&&resize

* add layernorm

* format

* add gemm

* add leakyRelu op

* modified format

* modified onnx leakyrelu alpha

* modified batchnorm

* fix gemm & avgpooling

* fix: onnx resize op input is none bug

* add instancenorm, use layernorm replace instance, error

* modiefied format, replace layernorm as instancenorm

* fix: onnx resize op input is none bug

* add pad2d kernel

* modified format

* fix op

* fix resize

* remove sync in op

* Update INSTALL_GUIDE_CN.md for ASCEND

* Update env.sh

* format

* fix test_resize

* fix resize

* fix test_resize_

* fix test_resize_

* add HcclCommDestroy && use default context

* install onnxtuntime

* install onnx-simplifier

* install numpy

* fix bug after merge

* remove CHECK_RET&LOG_PRINT

* fix test_ascend_layernorm

* fix test_cuda_resize

* fix test_ascend_*

* fix format

---------

Co-authored-by: Haojie Wang <haojie0429@gmail.com>
Co-authored-by: wanghailu <wanghailu@qiyuanlab.com>
Co-authored-by: OdinaryWord <sx-hz@163.com>
Co-authored-by: xgqdut2016 <kenan_gewei@163.com>
Co-authored-by: zhangyunze <z13785159769@163.com>
Co-authored-by: Songxin <sx-hz@hotmail.com>
Co-authored-by: zhangyue <138768300+zhangyue207@users.noreply.github.com>
Co-authored-by: zhangyue <zhangyue@qiyuanlab.com>
Co-authored-by: sx941227 <14507528+sx941227@user.noreply.gitee.com>
Co-authored-by: zhangyunze <zhangyunze@qiyuanlab.com>
Co-authored-by: Chenjie Duan <44265800+kilinchange@users.noreply.github.com>
This commit is contained in:
Hardy 2024-08-20 22:09:33 +08:00 committed by GitHub
parent 9c0749d1e6
commit 8b61b0e397
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
78 changed files with 4803 additions and 85 deletions

2
.gitignore vendored
View File

@ -44,3 +44,5 @@ build_debug/
*.onnx
*.pb
*.npy
*.swp

View File

@ -2,6 +2,7 @@
option(USE_CUDA "Support CUDA GPU" OFF)
option(USE_BANG "Support BANG MLU" OFF)
option(USE_KUNLUN "Support KUNLUN XPU" OFF)
option(USE_ASCEND "Support HUAWEI ASCEND" OFF)
option(USE_INTELCPU "Support INTELCPU" OFF)
option(USE_BACKTRACE "Print backtrace on exception and segmentation fault" ON)
option(USE_PROTOBUF "Serialize and deserialize tensors" OFF)
@ -151,6 +152,11 @@ if(USE_KUNLUN)
list (APPEND SRC ${SRC_KUNLUN})
endif()
if(USE_ASCEND)
file(GLOB_RECURSE SRC_ASCEND src/ascend/*.cc src/kernels/ascend/*.cc )
list (APPEND SRC ${SRC_ASCEND})
endif()
if(USE_INTELCPU)
file(GLOB_RECURSE SRC_INTELCPU src/intelcpu/*.cc src/kernels/intelcpu/*.cc )
list (APPEND SRC ${SRC_INTELCPU})
@ -297,6 +303,7 @@ if(USE_KUNLUN)
else()
set(TARGET_CPU_ARCH $ENV{TARGET_CPU_ARCH} CACHE STRING "Target CPU ARCH")
endif()
message(STATUS "TARGET_CPU_ARCH: ${TARGET_CPU_ARCH}")
if (BUILD_DIST)
@ -309,6 +316,42 @@ if(USE_KUNLUN)
target_link_libraries(InfiniTensor ${KUNLUN_RT} ${KUNLUN_DNN} stdc++)
endif()
if(USE_ASCEND)
add_compile_definitions(USE_ASCEND=1)
if ((NOT DEFINED ASCEND_HOME) AND (NOT DEFINED ENV{ASCEND_HOME}))
message(FATAL_ERROR "ASCEND_HOME is not defined from cmake or env")
elseif (DEFINED ASCEND_HOME)
set(ASCEND_HOME ${ASCEND_HOME} CACHE STRING "ASCEND_HOME directory for Ascend development")
else()
set(ASCEND_HOME $ENV{ASCEND_HOME} CACHE STRING "ASCEND_HOME directory for Ascend development")
endif()
message(STATUS "ASCEND_HOME: ${ASCEND_HOME}")
include_directories("${ASCEND_HOME}/include/")
include_directories("${ASCEND_HOME}/include/aclnn")
find_library(ASCEND_CL libascendcl.so "${ASCEND_HOME}/lib64")
find_library(ASCEND_BASE libnnopbase.so "${ASCEND_HOME}/lib64")
find_library(ASCEND_DNN libopapi.so "${ASCEND_HOME}/lib64")
find_library(ASCEND_HCCL libhccl.so "${ASCEND_HOME}/lib64")
find_library(ASCEND_HAL libascend_hal.so "${ASCEND_HOME}/../../driver/lib64/driver")
# find_library(ASCEND_RT libruntime.so "${ASCEND_HOME}/lib64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lstdc++ -Wall -Werror")
if ((NOT DEFINED TARGET_CPU_ARCH) AND (NOT DEFINED ENV{TARGET_CPU_ARCH}))
execute_process(COMMAND uname -m OUTPUT_VARIABLE _uname_m OUTPUT_STRIP_TRAILING_WHITESPACE)
set(TARGET_CPU_ARCH "${_uname_m}" CACHE STRING "Target CPU ARCH")
elseif(DEFINED TARGET_CPU_ARCH)
set(TARGET_CPU_ARCH ${TARGET_CPU_ARCH} CACHE STRING "Target CPU ARCH")
else()
set(TARGET_CPU_ARCH $ENV{TARGET_CPU_ARCH} CACHE STRING "Target CPU ARCH")
endif()
message(STATUS "TARGET_CPU_ARCH: ${TARGET_CPU_ARCH}")
target_link_libraries(InfiniTensor ${ASCEND_HAL} ${ASCEND_CL} ${ASCEND_BASE} ${ASCEND_DNN} ${ASCEND_HCCL} stdc++)
if (BUILD_DIST)
message(STATUS "Add BUILD_DIST, use HCCL with ASCEND")
add_compile_definitions(INFINI_USE_HCCL=1)
endif()
endif()
# # Python bindings
# pybind11_add_module(infini MODULE ${FFI})
# target_link_libraries(infini PRIVATE infini_cpp)
@ -346,6 +389,9 @@ if(BUILD_TEST)
build_test(test/kernels/kunlun/*.cc)
build_test(test/kunlun/*.cc)
endif()
if (USE_ASCEND)
build_test(test/kernels/ascend/*.cc)
endif()
if (USE_INTELCPU)
build_test(test/kernels/intelcpu/*.cc)
endif()

View File

@ -4,6 +4,7 @@ TYPE ?= Release
CUDA ?= OFF
BANG ?= OFF
KUNLUN ?= OFF
ASCEND ?= OFF
INTELCPU ?= off
BACKTRACE ?= ON
TEST ?= ON
@ -29,6 +30,7 @@ CMAKE_OPT = -DCMAKE_BUILD_TYPE=$(TYPE)
CMAKE_OPT += -DUSE_CUDA=$(CUDA)
CMAKE_OPT += -DUSE_BANG=$(BANG)
CMAKE_OPT += -DUSE_KUNLUN=$(KUNLUN)
CMAKE_OPT += -DUSE_ASCEND=$(ASCEND)
CMAKE_OPT += -DUSE_BACKTRACE=$(BACKTRACE)
CMAKE_OPT += -DBUILD_TEST=$(TEST)
CMAKE_OPT += -DBUILD_DIST=$(DIST)

View File

@ -14,6 +14,7 @@
| -------- | ------------ | ----------- | ---------- |
| X86-64 | Nvidia GPU | Ubuntu-22.04 | Yes |
| X86-64 | Cambricon MLU | Ubuntu-22.04 | Yes |
| arm64 | Ascend NPU |OpenEuler-22.03| Yes |
推荐使用 X86-64 机器以及 Ubuntu-22.04,本文以此环境为例。
@ -68,6 +69,20 @@
我们强烈建议您规范安装,统一到一个目录下,以免不必要的麻烦。另外请注意,由于 MLU 上层软件建设适配程度有限,如您在其覆盖的机器,操作系统之外运行,需要在安装驱动之后使用上层软件的 Docker。
- 如您的第三方加速卡为昇腾 NPU请参考昇腾官方文档进行
> [驱动及CANN安装](https://www.hiascend.com/document/detail/zh/canncommercial/80RC1/quickstart/quickstart/quickstart_18_0006.html)
> 安装完成后请进行相应的环境变量配置,将可执行文件目录与库目录添加到操作系统识别的路径中,例如
>
> ```bash
> # 将如下内容写入到你的 bashrc 文件并 source 该文件
> export ASCEND_HOME=/usr/local/Ascend/ascend-toolkit/latest
> source /usr/local/Ascend/ascend-toolkit/set_env.sh
> # 如您不方便将上述环境变量配置到 bashrc 文件中进行长期使用,你也可以在我们提供的 env.sh 文件中进行正确配置并激活,作为临时使用
> source env.sh
> ```
我们强烈建议您规范安装,统一到一个目录下,以免不必要的麻烦。
4. 确认您安装了 makebuild-essential python-is-python3 python-dev-is-python3 python3-pip libdw-dev如您的机器没有上述基础依赖请自行按需安装。
- 在使用 apt-get 工具情况下,您可以这样执行
@ -140,6 +155,13 @@
make install-python KUNLUN=ON
```
编译 CPU 部分,同时编译昇腾 NPU 部分:
```bash
export ASCEND_HOME=/path/to/your/ascend_home
make install-python ASCEND=ON
```
3. 使用方法
安装成功后,您就可以使用本项目的 Python 接口进行编码并运行。具体使用方式可以参考项目样例代码 example/Resnet/resnet.py 以及用户使用手册

View File

@ -15,6 +15,7 @@
| -------- | ------------ | ----------- | ---------- |
| X86-64 | Nvidia GPU | Ubuntu-22.04 | Yes |
| X86-64 | Cambricon MLU | Ubuntu-22.04 | Yes |
| arm64 | Ascend NPU |OpenEuler-22.03| Yes |
## 神经网络支持

View File

@ -28,6 +28,7 @@
- `CUDA`:是否编译 CUDA 后端,默认为 `OFF``ON` 打开
- `BANG`:是否编译寒武纪后端,默认为 `OFF``ON` 打开
- `KUNLUN`:是否编译昆仑后端,默认为 `OFF``ON` 打开
- `ASCEND`:是否编译华为后端,默认为 `OFF``ON` 打开
- `BACKTRACE`:是否启用栈回溯,默认为 `ON``OFF` 关闭,建议调试时打开
- `TEST`:是否编译 `googletest`,默认为 `ON``OFF` 关闭,只有 `test-cpp` 时必要

30
env.sh
View File

@ -36,3 +36,33 @@ export LD_LIBRARY_PATH="${NEUWARE_HOME}/lib64:${LD_LIBRARY_PATH}"
# ├── version
# └── XTDK
export KUNLUN_HOME=/usr/local/xpu
# 配置华为ASCEND NPU 的 HOME 路径,请注意 /usr/local/Ascend/ascend-toolkit/latest 是华为ASCEND 软件栈提供的软件包路径。
# 如若用户有其他的路径安装方式,请自行配置正确的路径。
# 这里是 ascend 目录下一个可能的结构图,请参考。
# .
# ├── aarch64-linux
# ├── acllib
# ├── arm64-linux
# ├── atc
# ├── bin
# ├── compiler
# ├── conf
# ├── fwkacllib
# ├── hccl
# ├── include
# ├── lib64
# ├── mindstudio-toolkit
# ├── opp
# ├── opp_kernel
# ├── ops
# ├── pyACL
# ├── python
# ├── runtime
# ├── test-ops
# ├── toolkit
# └── tools
export ASCEND_HOME=/usr/local/Ascend/ascend-toolkit/latest
source /usr/local/Ascend/ascend-toolkit/set_env.sh
source /usr/local/Ascend/toolbox/set_env.sh

@ -1 +1 @@
Subproject commit 51d3105277f3774ed31c02ed4cd11fa92925af77
Subproject commit b896cec2dba5b8522b141ac4f89eb43074ee1b98

View File

@ -0,0 +1,198 @@
import argparse
import os
import time
import multiprocessing as mp
from pyinfinitensor.onnx import OnnxStub, backend
import onnx
from onnx.shape_inference import infer_shapes_path
import numpy as np
from parallel_opt import parallel_model
import acl
def parse_args():
parser = argparse.ArgumentParser(description="launch distributed infinitensor")
parser.add_argument("--num_nodes", type=int, default=1, help="number of nodes")
parser.add_argument(
"--nproc_per_node", type=int, default=2, help="number of processes per node"
)
parser.add_argument(
"--name", type=str, default="test", help="name of this instance."
)
parser.add_argument(
"--model", type=str, default="/data/onnx_models/llama2/llama_bs1_seq1024.onnx",
help="path to the ONNX model file."
)
parser.add_argument("--batch_size", type=int, default=1, help="batch size.")
parser.add_argument("--length", type=int, default=1, help="sequence length.")
parser.add_argument(
"--gen_std",
default=False,
action="store_true",
help="whether to generate the standard results.",
)
args = parser.parse_args()
print("arg setting: ", args)
return (
args.num_nodes,
args.nproc_per_node,
args.name,
args.model,
args.batch_size,
args.length,
args.gen_std,
)
def run_model(model, runtime, world_size=1, rank=0, n=10):
stub = OnnxStub(model, runtime)
load_inputs(stub, world_size, rank)
# stub.tune()
stub.run()
# get outputs
time.sleep(0.01)
outputs = next(stub.outputs.values().__iter__()).copyout_numpy()
# bench
begin = time.time()
for _ in range(n):
stub.run()
end = time.time()
avg_time = (end - begin) / n
print(f"average time: {avg_time}")
return outputs
def run_and_compare(name, model, runtime, world_size=1, rank = 0):
results = np.load(f"./data/output.npy")
outputs = run_model(model, runtime, world_size, rank)
print("answer argmax:", np.argmax(results))
print("output argmax:", np.argmax(outputs))
#np.testing.assert_allclose(outputs, results, rtol=1e-3, atol=1e-3)
getDiff(results, outputs)
def start_worker(
name: str, world_size: int, rank: int, local_rank: int, model: onnx.ModelProto
):
dist_name = name + "_dist"
model = parallel_model(model, world_size, rank)
extern_path = f"./{dist_name}_rank{rank}.pb"
if os.path.exists(extern_path):
os.remove(extern_path)
onnx.save_model(
model,
f"./{dist_name}_rank{rank}.onnx",
save_as_external_data=True,
location=extern_path,
)
infer_shapes_path(f"./{dist_name}_rank{rank}.onnx")
runtime = backend.ASCENDRuntime(local_rank)
# print("init comm")
runtime.init_comm(
dist_name,
world_size,
rank,
)
run_and_compare(name, model, runtime, world_size, rank)
def start_single(name, model):
runtime = backend.ASCENDRuntime(0)
run_and_compare(name, model, runtime)
def generate_input_output(model):
os.makedirs(os.path.dirname("./data/"), exist_ok=True)
runtime = backend.ASCENDRuntime(0)
stub = OnnxStub(model, runtime)
position_id = 0
for i, (name, tensor) in enumerate(stub.inputs.items()):
input = tensor.copyout_numpy()
if np.issubdtype(input.dtype, np.integer):
if input.size == 1:
# input = np.array([position_id])
input = np.random.randint(0,2,size=input.shape, dtype=input.dtype)
else:
input = np.random.randint(0,2,size=input.shape, dtype=input.dtype)
elif input.dtype == np.bool_:
input = np.random.randint(0,2,size=input.shape) > 0
else:
if i == 0:
input = np.ones(input.shape).astype(input.dtype)
position_id = input.shape[-1] - 1
else:
input = np.random.rand(*input.shape).astype(input.dtype)
tensor.copyin_numpy(input)
np.save(f"./data/input_{i}", input)
stub.run()
time.sleep(0.01)
output = next(stub.outputs.values().__iter__()).copyout_numpy()
if np.isnan(output).any():
print("Nan in output")
np.save(f"./data/output", output)
def load_inputs(stub, world_size=1, rank=0):
for i, (name, tensor) in enumerate(stub.inputs.items()):
input = np.load(f"./data/input_{i}.npy")
if all(x == y for x,y in zip(input.shape,tensor.shape())):
tensor.copyin_numpy(input)
else:
tensor.copyin_numpy(np.hsplit(input, world_size)[rank])
def getDiff(base, test):
absolute_diff = np.abs(np.subtract(base, test))
max_absolute_diff = np.max(absolute_diff)
baseCopy = base.astype(np.float64).ravel()
testCopy = test.astype(np.float64).ravel()
upValue = np.sum(np.abs(baseCopy - testCopy))
downValue = np.sum(np.abs(baseCopy)) + np.float64(1e-9)
max_relative_diff = upValue / downValue
print(f"Max absolute difference: {max_absolute_diff}\n"
f"Max relative difference: {max_relative_diff}")
return max_absolute_diff, max_relative_diff
def main():
acl.init()
nnodes, nproc_per_node, name, model_path, bs, length, gen_std = parse_args()
model = onnx.load(model_path)
# generate standart output
if gen_std:
print("Generate inputs and outputs.")
p = mp.Process(target=generate_input_output, args=[model])
p.start()
p.join()
return
# run single process.
# use standalone process to isolate cuda.
print("run model by single MLU.")
p = mp.Process(target=start_single, args=(name, model))
p.start()
p.join()
# run distributed parallel.
world_size = nnodes * nproc_per_node
print(f"run model by {world_size} MLUs in parallel.")
workers = [
mp.Process(
target=start_worker,
args=(name, world_size, rank, rank % nproc_per_node, model),
)
for rank in range(world_size)
]
for w in workers:
w.start()
for w in workers:
w.join()
if __name__ == "__main__":
main()

View File

@ -0,0 +1,37 @@
#pragma once
#include "acl/acl.h"
#include "acl/acl_op.h"
#include "core/common.h"
#define checkASCENDError(call) \
{ \
auto err = call; \
if (ACL_SUCCESS != err) { \
fprintf(stderr, "ASCEND error in %s:%i : .\n", __FILE__, \
__LINE__); \
exit(EXIT_FAILURE); \
} \
}
#define checkHCCLError(call) \
{ \
auto err = call; \
if (HCCL_SUCCESS != err) { \
fprintf(stderr, "HCCL error in %s:%i : .\n", __FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} \
}
#define GetRecentErrMsg() \
{ \
auto tmp_err_msg = aclGetRecentErrMsg(); \
if (tmp_err_msg != NULL) { \
printf(" ERROR Message : %s \n ", tmp_err_msg); \
} \
}
namespace infini {
using ASCENDPtr = void *;
} // namespace infini

View File

@ -0,0 +1,23 @@
#pragma once
#include "ascend/ascend_runtime.h"
#include "core/kernel.h"
namespace infini {
class ASCENDKernelWithoutConfig : public Kernel {
public:
virtual void compute(const Operator &op, const PerfRecord &record,
const RuntimeObj *context) const {
compute(op, context);
}
virtual void compute(const Operator &op,
const RuntimeObj *context) const = 0;
// Premise: op is idempotent since it is called multiple times.
virtual PerfRecord tune(const Operator &op,
const RuntimeObj *_context) const {
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
return make_ref<PerfRecordObj>(timeit([&]() { compute(op, _context); },
[&]() { context->sync(); }));
}
};
} // namespace infini

View File

@ -0,0 +1,82 @@
#pragma once
#include "ascend/ascend_common.h"
#include "core/runtime.h"
namespace infini {
class ASCENDRuntimeObj : public RuntimeObj {
private:
aclrtContext context;
aclrtStream stream;
std::unique_ptr<CommunicatorObj> comm;
ASCENDPtr workspace = nullptr;
uint64_t workspaceSize;
public:
ASCENDRuntimeObj(int deviceId = 0) : RuntimeObj(Device::ASCEND, deviceId) {
// auto ret = aclInit(nullptr);
// CHECK_RET(ret == ACL_SUCCESS,
// LOG_PRINT("aclInit failed. ERROR: %d\n", ret));
auto ret = aclrtSetDevice(deviceId);
checkASCENDError(ret);
ret = aclrtCreateStream(&stream);
checkASCENDError(ret);
// 10GB for Longformer
// size_t longformerNum = 3lu * (1 << 30);
workspaceSize = 3ll * (1 << 30); // 3 GB
workspace = alloc(workspaceSize);
}
virtual ~ASCENDRuntimeObj() {
dealloc(workspace);
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
// aclFinalize();
}
string toString() const override;
void run(const Graph &graph, bool tune = false,
bool profiling = false) const;
void sync() const;
ASCENDPtr alloc(size_t size) override {
void *ptr;
checkASCENDError(
aclrtMalloc((void **)&ptr, size, ACL_MEM_MALLOC_HUGE_FIRST));
return ptr;
}
void dealloc(void *ptr) override { checkASCENDError(aclrtFree(ptr)); }
aclrtStream ASCENDHandle() const { return stream; }
ASCENDPtr getWorkspace(uint64_t size) const {
IT_ASSERT(size <= workspaceSize);
return workspace;
}
void copyBlobFromCPU(void *dst, const void *src,
size_t bytes) const override {
aclrtMemcpy(dst, bytes, const_cast<void *>(src), bytes,
ACL_MEMCPY_HOST_TO_DEVICE);
}
void copyBlobToCPU(void *dst, const void *src,
size_t bytes) const override {
aclrtMemcpy(dst, bytes, const_cast<void *>(src), bytes,
ACL_MEMCPY_DEVICE_TO_HOST);
}
void copyBlobInsideRuntime(void *dst, const void *src,
size_t bytes) const override {
aclrtMemcpy(dst, bytes, const_cast<void *>(src), bytes,
ACL_MEMCPY_DEVICE_TO_DEVICE);
}
void initComm(const string &name, int worldSize, int rank) final;
CommunicatorObj &getCommunicator() const override { return *comm; }
private:
void runWithoutSync(const Graph &graph, bool tune, bool profiling) const;
};
} // namespace infini

View File

@ -0,0 +1,72 @@
#pragma once
#include "ascend/ascend_common.h"
#include "core/communicator.h"
#include "hccl/hccl.h"
#include "hccl/hccl_types.h"
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <thread>
#include <vector>
namespace infini {
class HcclCommunicatorObj final : public CommunicatorObj {
private:
HcclComm comm;
public:
HcclCommunicatorObj(const string &name, int worldSize, int rank)
: CommunicatorObj(worldSize, rank) {
const std::string filePath("./" + name + "_hccl_id.bin");
int devId = rank;
int devCount = worldSize;
// get rootInfo in rootRank
HcclRootInfo rootInfo;
int32_t rootRank = 0;
if (devId == rootRank) {
checkHCCLError(HcclGetRootInfo(&rootInfo));
std::ofstream ofs(filePath, std::ios::binary);
ofs.write((char *)&rootInfo, sizeof(HcclRootInfo));
} else {
auto begin = std::chrono::steady_clock::now();
while (!std::filesystem::exists(filePath)) {
auto now = std::chrono::steady_clock::now();
_IT_ASSERT_2(now < begin + std::chrono::seconds(10),
"time limit (10s) exceeded.");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::ifstream ifs(filePath, std::ios::binary);
ifs.read((char *)&rootInfo, sizeof(HcclRootInfo));
}
auto ret = HcclCommInitRootInfo(uint32_t(devCount), &rootInfo,
uint32_t(devId), &comm);
checkHCCLError(ret);
if (rank == 0) {
std::filesystem::remove(filePath);
}
}
// Get the actual ncclComm_t
HcclComm getHcclComm() { return comm; }
~HcclCommunicatorObj() final {
auto ret = HcclCommDestroy(comm);
checkHCCLError(ret);
}
virtual string toString() const final {
std::ostringstream oss;
oss << "HCCL communicator";
return oss.str();
}
};
} // namespace infini

View File

@ -93,4 +93,6 @@ double timeit(
const std::function<void(void)> &sync = []() {}, int warmupRounds = 10,
int timingRounds = 10);
std::vector<int64_t> castTo64(std::vector<int> const &v32);
} // namespace infini

View File

@ -38,6 +38,8 @@ class GraphHandlerObj {
float momentum, float eps, bool training);
Tensor layerNormalization(Tensor input, Tensor scale, Tensor output,
Tensor bias, float eps, int axis, int stash_type);
Tensor instanceNormalization(Tensor input, Tensor output, Tensor scale,
Tensor bias, float eps);
Tensor rmsNorm(Tensor input, Tensor weight, Tensor output);
Tensor maxPool(Tensor input, Tensor output, int kh, int kw, int dh, int dw,
@ -77,7 +79,7 @@ class GraphHandlerObj {
Tensor reshape(Tensor data, Tensor reshaped, Shape shape);
Tensor resize(Tensor input, Tensor output,
const std::optional<vector<int>> &axes, Tensor sizes,
Tensor scales, Tensor roi, vector<uint32_t> sizes_,
Tensor scales, Tensor roi, vector<int64_t> sizes_,
vector<float> scales_, vector<float> roi_, string mode,
string ratioPolicy, string nearestMode,
string coordTransMode);

View File

@ -1,4 +1,4 @@
#pragma once
#pragma once
#ifndef OP_TYPE_H
#define OP_TYPE_H
@ -261,6 +261,7 @@ struct OpType {
enum class ActType {
None,
Relu,
LeakyRelu,
Sigmoid,
Tanh,
};

View File

@ -32,7 +32,7 @@ using OpLists = list<Operator>;
using VType = uint32_t;
enum class Device { CPU = 1, CUDA, BANG, INTELCPU, KUNLUN };
enum class Device { CPU = 1, CUDA, BANG, INTELCPU, KUNLUN, ASCEND };
/***************** Forward declaration end *****************/
class RuntimeObj : public std::enable_shared_from_this<RuntimeObj> {
@ -75,6 +75,7 @@ class RuntimeObj : public std::enable_shared_from_this<RuntimeObj> {
bool isCuda() const { return device == Device::CUDA; }
bool isBang() const { return device == Device::BANG; }
bool isKUNLUN() const { return device == Device::KUNLUN; }
bool isAscend() const { return device == Device::ASCEND; }
void copyBlob(const TensorObj *dst, const TensorObj *src) const;
// TODO: unify these copy APIs
virtual void copyBlobFromCPU(void *dst, const void *src,

View File

@ -0,0 +1,24 @@
#pragma once
#include "core/operator.h"
namespace infini {
class InstanceNormObj : public OperatorObj {
public:
InstanceNormObj(GraphObj *graph, Tensor input, Tensor output, Tensor scale,
Tensor bias, float eps = 1e-5);
OP_CLONE(InstanceNormObj);
optional<vector<Shape>> inferShape(const TensorVec &inputs) override;
std::string toString() const override;
int numInputs() const override { return inputs.size(); }
int numOutputs() const override { return outputs.size(); }
float getEps() const { return eps; }
private:
vector<int> getWorkloadVector() const override;
vector<int> getOpAttrVector() const override;
vector<DataType> inferDataType(const TensorVec &inputs) const override;
float eps;
};
} // namespace infini

View File

@ -85,6 +85,7 @@ class ResizeObj : public OperatorObj {
vector<int> axes;
vector<float> scales;
vector<float> roi;
bool isGivenSizes = false;
ECoordinateTransMode coMode; // compute src coordinate from dst coordinate
ECoeffMode mode; // coeff mode,for computing dst value from coordinate src
@ -139,9 +140,8 @@ class ResizeObj : public OperatorObj {
} else
return 0;
}
bool isResizeBySizes() const {
return ratioPolicy != EKeepAspectRatioPolicy::none;
}
void setGivenSizes(bool val) { isGivenSizes = val; }
bool isResizeBySizes() const { return isGivenSizes; }
private:
vector<int> getWorkloadVector() const override;

View File

@ -191,7 +191,7 @@ class OnnxStub:
node,
{
"dilations": [1, 1],
"pads": [0, 0],
"pads": [0, 0, 0, 0],
"strides": [1, 1],
"output_padding": [0, 0],
},
@ -200,8 +200,52 @@ class OnnxStub:
attributes[name]
for name in ["dilations", "pads", "strides", "output_padding"]
)
if p[0] != p[2] or p[1] != p[3]:
adapt = "{}-adapt".format(node.output[0])
tensors[adapt] = self.handler.pad(
tensors[node.input[0]], None, p, [-2, -1]
)
p = [0, 0, 0, 0]
else:
adapt = node.input[0]
if len(node.input) > 2:
bias = "{}-bias".format(node.output[0])
reshape = "{}-reshape".format(node.output[0])
tensors[bias] = self.handler.convTransposed2d(
tensors[adapt],
tensors[node.input[1]],
None,
p[0],
p[1],
s[0],
s[1],
d[0],
d[1],
op[0],
op[1],
)
tensors[reshape] = self.handler.reshape(
tensors[node.input[2]],
None,
[
1,
reduce(
lambda acc, x: acc * x,
tensors[node.input[2]].shape(),
),
1,
1,
],
)
tensors[node.output[0]] = self.handler.add(
tensors[bias],
tensors[reshape],
tensors.get(node.output[0]),
)
else:
tensors[node.output[0]] = self.handler.convTransposed2d(
tensors[node.input[0]],
tensors[adapt],
tensors[node.input[1]],
tensors.get(node.output[0]),
p[0],
@ -286,6 +330,21 @@ class OnnxStub:
axis,
stash_type,
)
elif node.op_type == "InstanceNormalization":
(input, scale, bias) = (tensors[node.input[i]] for i in [0, 1, 2])
output = tensors.get(node.output[0])
tensors[node.output[0]] = self.handler.instanceNormalization(
input,
output,
scale,
bias,
next(
(attr.f for attr in node.attribute if attr.name == "epsilon"),
1e-5,
),
)
elif node.op_type == "RMSNorm":
tensors[node.output[0]] = self.handler.RMSNorm(
tensors[node.input[0]],
@ -557,16 +616,6 @@ class OnnxStub:
tensors[node.input[1]],
tensors.get(node.output[0]),
)
elif node.op_type == "LeakyRelu":
tensors[node.output[0]] = self.handler.leakyRelu(
tensors[node.input[0]],
tensors.get(node.output[0]),
next(
(attr.f for attr in node.attribute if attr.name == "alpha"),
0.01,
),
)
elif node.op_type == "Clip":
tensors[node.output[0]] = self.handler.clip(
tensors[node.input[0]],
@ -625,7 +674,7 @@ class OnnxStub:
"cubic_coeff_a": -0.75,
"exclude_outside": 0,
"extrapolation_value": 0.0,
"keep_aspect_ratio_policy": "none",
"keep_aspect_ratio_policy": "stretch",
"mode": "nearest",
"nearest_mode": "none",
},

View File

@ -11,7 +11,7 @@ proj_path = Path(sys.path[0]).parent
def format_file(file):
file = Path(proj_path.joinpath(file))
if file.suffix in c_style_file:
run(f"clang-format-14 -style=file -i {file}", cwd=proj_path, shell=True)
run(f"clang-format -style=file -i {file}", cwd=proj_path, shell=True)
run(f"git add {file}", cwd=proj_path, shell=True)
elif file.suffix == py_file:
run(f"black {file}", cwd=proj_path, shell=True)

View File

@ -0,0 +1,73 @@
#include "ascend/ascend_runtime.h"
#include "core/kernel.h"
#include "core/perf_engine.h"
#ifdef INFINI_USE_HCCL
#include "ascend/hccl_communicator.h"
#endif
namespace infini {
void ASCENDRuntimeObj::runWithoutSync(const Graph &graph, bool tune = false,
bool profiling = false) const {
const auto &kernelRegistry = KernelRegistry::getInstance();
auto &perfEngine = PerfEngine::getInstance();
double totalTime = 0;
std::map<OpType, double> opTime;
std::map<OpType, int> opCnt;
for (auto &op : graph->getOperators()) {
// HACK: set correct data type
auto kernelAttrs = KernelAttrs{device, op->getOpType().underlying()};
Kernel *kernel = kernelRegistry.getKernel(kernelAttrs);
auto perfKey = PerfEngine::Key{kernelAttrs, op->getOpPerfKey()};
auto perfData = perfEngine.getPerfData(perfKey);
if (!perfData && !tune) {
kernel->compute(op, this);
continue;
}
PerfRecord record;
if (!perfData) {
record = kernel->tune(op, this);
perfEngine.setPerfData(perfKey, record);
} else
record = perfData;
double t = record->time;
totalTime += t;
if (profiling) {
double t = timeit([&]() { kernel->compute(op, record, this); },
[&]() { sync(); }, 1, 1);
op->print();
printf(" op_time on kunlun xpu %lf\n", t);
totalTime += t;
opTime[op->getOpType()] += t;
opCnt[op->getOpType()]++;
}
}
}
void ASCENDRuntimeObj::run(const Graph &graph, bool tune,
bool profiling) const {
if (profiling)
IT_TODO_HALT();
runWithoutSync(graph, tune, profiling);
sync();
}
void ASCENDRuntimeObj::sync() const { aclrtSynchronizeStream(stream); }
string ASCENDRuntimeObj::toString() const { return "ASCEND Runtime"; }
void ASCENDRuntimeObj::initComm(const string &name, int worldSize, int rank) {
IT_ASSERT(worldSize > 0);
IT_ASSERT(rank >= 0);
IT_ASSERT(rank < worldSize);
IT_ASSERT(!comm) << "communicator is already initialized.";
#ifdef INFINI_USE_HCCL
comm = std::make_unique<HcclCommunicatorObj>(name, worldSize, rank);
#else
IT_TODO_HALT_MSG("Not compiled with CNCL.");
#endif
}
} // namespace infini

View File

@ -21,4 +21,17 @@ double timeit(const std::function<void()> &func,
timingRounds;
}
// transform vector<int> to vector<int64_t>
std::vector<int64_t> castTo64(std::vector<int> const &v32) {
if (v32.size() == 0) {
std::vector<int64_t> v64(1, 1);
return v64;
}
std::vector<int64_t> v64(v32.size(), 1);
for (size_t i = 0; i < v32.size(); ++i) {
v64[i] = int64_t(v32[i]);
}
return v64;
}
} // namespace infini

View File

@ -1,4 +1,4 @@
#include "core/graph_handler.h"
#include "core/graph_handler.h"
#include "operators/all_gather.h"
#include "operators/all_reduce.h"
#include "operators/attention_kvcache.h"
@ -9,6 +9,7 @@
#include "operators/element_wise.h"
#include "operators/expand.h"
#include "operators/gather.h"
#include "operators/instance_norm.h"
#include "operators/layer_norm.h"
#include "operators/lrn.h"
#include "operators/matmul.h"
@ -135,6 +136,21 @@ Tensor GraphHandlerObj::layerNormalization(Tensor input, Tensor scale,
}
}
Tensor GraphHandlerObj::instanceNormalization(Tensor input, Tensor output,
Tensor scale, Tensor bias,
float eps) {
if (output) {
g->addOpWithOutputs<InstanceNormObj>(
std::move(input), output, std::move(scale), std::move(bias), eps);
return output;
} else {
return g
->addOp<InstanceNormObj>(std::move(input), output, std::move(scale),
std::move(bias), eps)
->getOutput();
}
}
Tensor GraphHandlerObj::rmsNorm(Tensor input, Tensor weight, Tensor output) {
if (output) {
g->addOpWithOutputs<RMSNormObj>(std::move(input), std::move(weight),
@ -295,13 +311,13 @@ Tensor GraphHandlerObj::reshape(Tensor data, Tensor reshaped, Shape shape) {
Tensor GraphHandlerObj::resize(Tensor input, Tensor output,
const std::optional<vector<int>> &axes,
Tensor sizes, Tensor scales, Tensor roi,
vector<uint32_t> sizes_, vector<float> scales_,
vector<int64_t> sizes_, vector<float> scales_,
vector<float> roi_, string mode,
string ratioPolicy, string nearestMode,
string coordTransMode) {
if (sizes_.size() > 0) {
sizes->dataMalloc();
sizes->copyin<uint32_t>(sizes_);
sizes->copyin<int64_t>(sizes_);
}
if (scales_.size() > 0) {
scales->dataMalloc();

View File

@ -30,6 +30,9 @@
#ifdef USE_KUNLUN
#include "kunlun/kunlun_runtime.h"
#endif
#ifdef USE_ASCEND
#include "ascend/ascend_runtime.h"
#endif
#ifdef USE_INTELCPU
#include "intelcpu/mkl_runtime.h"
#include "intelcpu/operator_timer.h"
@ -65,6 +68,7 @@ void export_values(py::module &m) {
py::enum_<ActType>(m, "ActType")
.value("Linear", ActType::None) // `None` is Python keyword
.VALUE(ActType, Relu)
.VALUE(ActType, LeakyRelu)
.VALUE(ActType, Sigmoid)
.VALUE(ActType, Tanh)
.export_values();
@ -102,9 +106,9 @@ void export_values(py::module &m) {
.VALUE(OpType, BatchNormalization)
.VALUE(OpType, Softmax)
.VALUE(OpType, Relu)
.VALUE(OpType, LeakyRelu)
.VALUE(OpType, Gelu)
.VALUE(OpType, PRelu)
.VALUE(OpType, LeakyRelu)
.VALUE(OpType, Sigmoid)
.VALUE(OpType, Tanh)
.VALUE(OpType, HardSigmoid)
@ -177,6 +181,12 @@ static Ref<KUNLUNRuntimeObj> kunlun_runtime() {
}
#endif
#ifdef USE_ASCEND
static Ref<ASCENDRuntimeObj> ascend_runtime() {
return make_ref<ASCENDRuntimeObj>();
}
#endif
#ifdef USE_INTELCPU
static Ref<RuntimeObj> intelcpu_runtime() { return make_ref<MklRuntimeObj>(); }
#endif
@ -355,6 +365,10 @@ void export_functions(py::module &m) {
#ifdef USE_KUNLUN
.FUNCTION(kunlun_runtime)
#endif
#ifdef USE_ASCEND
.FUNCTION(ascend_runtime)
#endif
.FUNCTION(conv_attrs_of)
.FUNCTION(conv_trans_attrs_of)
.FUNCTION(matmul_attrs_of)
@ -442,6 +456,14 @@ void init_graph_builder(py::module &m) {
.def(py::init<int>(), py::arg("device") = 0)
.def("init_comm", &KUNLUNRuntimeObj::initComm);
#endif
#ifdef USE_ASCEND
py::class_<ASCENDRuntimeObj, std::shared_ptr<ASCENDRuntimeObj>, RuntimeObj>(
m, "ASCENDRuntime")
.def(py::init<int>(), py::arg("device") = 0)
.def("init_comm", &ASCENDRuntimeObj::initComm);
;
#endif
py::class_<TensorObj, std::shared_ptr<TensorObj>>(m, "Tensor",
py::buffer_protocol())
.def("fuid", &TensorObj::getFuid, policy::automatic)
@ -516,6 +538,8 @@ void init_graph_builder(py::module &m) {
.def("matmul", &Handler::matmul, policy::move)
.def("batchNormalization", &Handler::batchNormalization, policy::move)
.def("layerNormalization", &Handler::layerNormalization, policy::move)
.def("instanceNormalization", &Handler::instanceNormalization,
policy::move)
.def("RMSNorm", &Handler::rmsNorm, policy::move)
.def("maxPool", &Handler::maxPool, policy::move)
.def("avgPool", &Handler::avgPool, policy::move)
@ -528,6 +552,7 @@ void init_graph_builder(py::module &m) {
.def("min", &Handler::min, policy::move)
.def("max", &Handler::max, policy::move)
.def("relu", &Handler::relu, policy::move)
.def("leakyRelu", &Handler::leakyRelu, policy::move)
.def("silu", &Handler::silu, policy::move)
.def("gelu", &Handler::gelu, policy::move)
.def("sigmoid", &Handler::sigmoid, policy::move)
@ -542,7 +567,6 @@ void init_graph_builder(py::module &m) {
.def("identity", &Handler::identity, policy::move)
.def("flatten", &Handler::flatten, policy::move)
.def("pRelu", &Handler::pRelu, policy::move)
.def("leakyRelu", &Handler::leakyRelu, policy::move)
.def("clip", &Handler::clip, policy::move)
.def("transpose", &Handler::transpose, policy::move)
.def("depthToSpace", &Handler::depthToSpace, policy::move)

View File

@ -0,0 +1,50 @@
#ifdef INFINI_USE_HCCL
#include "operators/all_gather.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
#include "ascend/hccl_communicator.h"
#include "hccl/hccl.h"
#include "hccl/hccl_types.h"
namespace infini {
class AllGatherHCCL : public ASCENDKernelWithoutConfig {
public:
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<AllGatherObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
int world_size = op->getWorldSize();
// Check if world size info in operator matches runtime
IT_ASSERT(world_size == context->getCommunicator().getWorldSize());
void *input = op->getInputs(0)->getRawDataPtr<void *>();
ASCENDPtr output_temp =
context->getWorkspace(op->getInputs(0)->getBytes() * world_size);
IT_ASSERT(op->getDType() == DataType::Float32);
size_t bytes = op->getInputs(0)->getBytes();
size_t count = bytes / op->getDType().getSize();
HcclComm comm =
dynamic_cast<HcclCommunicatorObj &>(context->getCommunicator())
.getHcclComm();
checkHCCLError(HcclAllGather(input, output_temp, uint64_t(count),
HCCL_DATA_TYPE_FP32, comm,
context->ASCENDHandle()));
checkASCENDError(aclrtSynchronizeStream(context->ASCENDHandle()));
for (int i = 0; i < world_size; ++i) {
Tensor output = op->getOutput(i);
context->copyBlobInsideRuntime(
output->getRawDataPtr<float *>(),
static_cast<float *>(output_temp) + i * count, bytes);
}
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::AllGather, AllGatherHCCL,
"AllGather_HCCL_ASCEND");
} // namespace infini
#endif

View File

@ -0,0 +1,57 @@
#ifdef INFINI_USE_HCCL
#include "operators/all_reduce.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
#include "ascend/hccl_communicator.h"
#include "hccl/hccl.h"
#include "hccl/hccl_types.h"
namespace infini {
class AllReduceHCCL : public ASCENDKernelWithoutConfig {
public:
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<AllReduceBaseObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
void *sendBuff = op->getInputs(0)->getRawDataPtr<void *>();
void *recvBuff = op->getOutput()->getRawDataPtr<void *>();
size_t count = op->getInputs(0)->size();
HcclComm comm =
dynamic_cast<HcclCommunicatorObj &>(context->getCommunicator())
.getHcclComm();
checkHCCLError(HcclAllReduce(sendBuff, recvBuff, count,
HCCL_DATA_TYPE_FP32, getRedOp(), comm,
context->ASCENDHandle()));
checkASCENDError(aclrtSynchronizeStream(context->ASCENDHandle()));
}
virtual HcclReduceOp getRedOp() const = 0;
};
class AllReduceSumHCCL : public AllReduceHCCL {
HcclReduceOp getRedOp() const override { return HCCL_REDUCE_SUM; }
};
class AllReduceProdHCCL : public AllReduceHCCL {
HcclReduceOp getRedOp() const override { return HCCL_REDUCE_PROD; }
};
class AllReduceMinHCCL : public AllReduceHCCL {
HcclReduceOp getRedOp() const override { return HCCL_REDUCE_MIN; }
};
class AllReduceMaxHCCL : public AllReduceHCCL {
HcclReduceOp getRedOp() const override { return HCCL_REDUCE_MAX; }
};
REGISTER_KERNEL(Device::ASCEND, OpType::AllReduceSum, AllReduceSumHCCL,
"AllReduce_Sum_HCCL_ASCEND");
REGISTER_KERNEL(Device::ASCEND, OpType::AllReduceProd, AllReduceProdHCCL,
"AllReduce_Prod_HCCL_ASCEND");
REGISTER_KERNEL(Device::ASCEND, OpType::AllReduceMin, AllReduceMinHCCL,
"AllReduce_Min_HCCL_ASCEND");
REGISTER_KERNEL(Device::ASCEND, OpType::AllReduceMax, AllReduceMaxHCCL,
"AllReduce_Max_HCCL_ASCEND");
} // namespace infini
#endif

View File

@ -0,0 +1,99 @@
#include "operators/batch_norm.h"
#include "aclnnop/level2/aclnn_batch_norm.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class BatchNormAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<BatchNormObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const inData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const outData = (op->getOutput()->getRawDataPtr<void *>());
void *const meanData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const varData = (op->getInputs(2)->getRawDataPtr<void *>());
void *const scaleData = (op->getInputs(3)->getRawDataPtr<void *>());
void *const biasData = (op->getInputs(4)->getRawDataPtr<void *>());
auto inD = op->getInputs(0)->getDims();
auto inS = op->getInputs(0)->getStride();
auto paraD = op->getInputs(1)->getDims();
auto paraS = op->getInputs(1)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
std::vector<int64_t> inputDim = castTo64(inD);
std::vector<int64_t> inputStride = castTo64(inS);
std::vector<int64_t> paraDim = castTo64(paraD);
std::vector<int64_t> paraStride = castTo64(paraS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
auto inputTensor =
aclCreateTensor(inputDim.data(), inputDim.size(), ACL_FLOAT,
inputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
inputDim.data(), inputDim.size(), inData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
outputDim.data(), outputDim.size(), outData);
auto meanTensor = aclCreateTensor(
paraDim.data(), paraDim.size(), ACL_FLOAT, paraStride.data(), 0,
aclFormat::ACL_FORMAT_ND, paraDim.data(), paraDim.size(), meanData);
auto varTensor = aclCreateTensor(
paraDim.data(), paraDim.size(), ACL_FLOAT, paraStride.data(), 0,
aclFormat::ACL_FORMAT_ND, paraDim.data(), paraDim.size(), varData);
auto scaleTensor =
aclCreateTensor(paraDim.data(), paraDim.size(), ACL_FLOAT,
paraStride.data(), 0, aclFormat::ACL_FORMAT_ND,
paraDim.data(), paraDim.size(), scaleData);
auto biasTensor = aclCreateTensor(
paraDim.data(), paraDim.size(), ACL_FLOAT, paraStride.data(), 0,
aclFormat::ACL_FORMAT_ND, paraDim.data(), paraDim.size(), biasData);
auto savemeanTensor =
aclCreateTensor(paraDim.data(), paraDim.size(), ACL_FLOAT,
paraStride.data(), 0, aclFormat::ACL_FORMAT_ND,
paraDim.data(), paraDim.size(), scaleData);
auto saveinvstdTensor = aclCreateTensor(
paraDim.data(), paraDim.size(), ACL_FLOAT, paraStride.data(), 0,
aclFormat::ACL_FORMAT_ND, paraDim.data(), paraDim.size(), biasData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnBatchNormGetWorkspaceSize(
inputTensor, scaleTensor, biasTensor, meanTensor, varTensor, false,
op->getMomentum(), op->getEps(), outputTensor, savemeanTensor,
saveinvstdTensor, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnBatchNorm(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputTensor);
aclDestroyTensor(outputTensor);
aclDestroyTensor(meanTensor);
aclDestroyTensor(varTensor);
aclDestroyTensor(scaleTensor);
aclDestroyTensor(biasTensor);
aclDestroyTensor(savemeanTensor);
aclDestroyTensor(saveinvstdTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::BatchNormalization, BatchNormAclnn,
"batchnorm_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,74 @@
#include "operators/concat.h"
#include "aclnnop/level2/aclnn_cat.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class ConcatAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<ConcatObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
int dim = op->getDim();
int num = op->numInputs();
std::vector<aclTensor *> inputsData{};
for (int i = 0; i < num; ++i) {
auto inD = op->getInputs(i)->getDims();
auto inS = op->getInputs(i)->getStride();
std::vector<int64_t> inputDim = castTo64(inD);
std::vector<int64_t> inputStride = castTo64(inS);
void *const inData = (op->getInputs(i)->getRawDataPtr<void *>());
auto tmpTensor =
aclCreateTensor(inputDim.data(), inputDim.size(), ACL_FLOAT,
inputStride.data(), 0, aclFormat::ACL_FORMAT_ND,
inputDim.data(), inputDim.size(), inData);
inputsData.push_back(tmpTensor);
}
aclTensorList *tensorList =
aclCreateTensorList(inputsData.data(), inputsData.size());
void *const outData = (op->getOutput()->getRawDataPtr<void *>());
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_ND,
outputDim.data(), outputDim.size(), outData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnCatGetWorkspaceSize(
tensorList, int64_t(dim), outputTensor, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnCat(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensorList(tensorList);
aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Concat, ConcatAclnn,
"concat_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,92 @@
#include "operators/conv.h"
#include "aclnnop/level2/aclnn_convolution.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class ConvAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<ConvObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
const auto [ph, pw, sh, sw, dh, dw] = op->getPadStrideDilation();
const auto [n, c, h, w, f, r, s] = op->getNCHWFRS();
const int cpg = op->getChannelPerGroup();
const int g = c / cpg;
std::vector<int64_t> pads = {ph, pw};
std::vector<int64_t> stride = {sh, sw};
std::vector<int64_t> dilation = {dh, dw};
std::vector<int64_t> outputPadding = {sh - 1, sw - 1};
aclIntArray *convpads = aclCreateIntArray(pads.data(), pads.size());
aclIntArray *convstride =
aclCreateIntArray(stride.data(), stride.size());
aclIntArray *convdilation =
aclCreateIntArray(dilation.data(), dilation.size());
aclIntArray *convOutputpadding =
aclCreateIntArray(outputPadding.data(), outputPadding.size());
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const bData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto inputD = op->getInputs(0)->getDims();
auto inputS = op->getInputs(0)->getStride();
auto weightD = op->getInputs(1)->getDims();
auto weightS = op->getInputs(1)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
std::vector<int64_t> inputDim = castTo64(inputD);
std::vector<int64_t> inputStride = castTo64(inputS);
std::vector<int64_t> weightDim = castTo64(weightD);
std::vector<int64_t> weightStride = castTo64(weightS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
auto inputTensor =
aclCreateTensor(inputDim.data(), inputDim.size(), ACL_FLOAT,
inputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
inputDim.data(), inputDim.size(), aData);
auto weightTensor =
aclCreateTensor(weightDim.data(), weightDim.size(), ACL_FLOAT,
weightStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
weightDim.data(), weightDim.size(), bData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
outputDim.data(), outputDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnConvolutionGetWorkspaceSize(
inputTensor, weightTensor, nullptr, convstride, convpads,
convdilation, false, convOutputpadding, int64_t(g), outputTensor,
int8_t(1), &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnConvolution(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputTensor);
aclDestroyTensor(weightTensor);
aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Conv, ConvAclnn, "conv_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,94 @@
#include "aclnnop/level2/aclnn_convolution.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
#include "operators/conv.h"
namespace infini {
class ConvTransAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<ConvTransposed2dObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
const auto [ph, pw, sh, sw, dh, dw] = op->getPadStrideDilation();
const auto [oph, opw] = op->getOutputPadding();
const auto [n, c, h, w, f, r, s] = op->getNCHWFRS();
const int cpg = op->getChannelPerGroup();
const int g = c / cpg;
std::vector<int64_t> pads = {ph, pw};
std::vector<int64_t> stride = {sh, sw};
std::vector<int64_t> dilation = {dh, dw};
std::vector<int64_t> outputPadding = {oph, opw};
aclIntArray *convpads = aclCreateIntArray(pads.data(), pads.size());
aclIntArray *convstride =
aclCreateIntArray(stride.data(), stride.size());
aclIntArray *convdilation =
aclCreateIntArray(dilation.data(), dilation.size());
aclIntArray *convOutputpadding =
aclCreateIntArray(outputPadding.data(), outputPadding.size());
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const bData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto inputD = op->getInputs(0)->getDims();
auto inputS = op->getInputs(0)->getStride();
auto weightD = op->getInputs(1)->getDims();
auto weightS = op->getInputs(1)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
std::vector<int64_t> inputDim = castTo64(inputD);
std::vector<int64_t> inputStride = castTo64(inputS);
std::vector<int64_t> weightDim = castTo64(weightD);
std::vector<int64_t> weightStride = castTo64(weightS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
auto inputTensor =
aclCreateTensor(inputDim.data(), inputDim.size(), ACL_FLOAT,
inputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
inputDim.data(), inputDim.size(), aData);
auto weightTensor =
aclCreateTensor(weightDim.data(), weightDim.size(), ACL_FLOAT,
weightStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
weightDim.data(), weightDim.size(), bData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
outputDim.data(), outputDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnConvolutionGetWorkspaceSize(
inputTensor, weightTensor, nullptr, convstride, convpads,
convdilation, true, convOutputpadding, int64_t(g), outputTensor,
int8_t(1), &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnConvolution(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputTensor);
aclDestroyTensor(weightTensor);
aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::ConvTranspose, ConvTransAclnn,
"ConvTrans_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,219 @@
#include "operators/element_wise.h"
#include "aclnnop/aclnn_maximum.h"
#include "aclnnop/level2/aclnn_add.h"
#include "aclnnop/level2/aclnn_div.h"
#include "aclnnop/level2/aclnn_mul.h"
#include "aclnnop/level2/aclnn_pow_tensor_tensor.h"
#include "aclnnop/level2/aclnn_sub.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
#define DEFINE_ELEMENT_WISE_Aclnn(prefix) \
class prefix##Aclnn : public ASCENDKernelWithoutConfig { \
void compute(const Operator &_op, \
const RuntimeObj *_context) const override { \
auto op = as<ElementWiseObj>(_op); \
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context); \
IT_ASSERT(op->getDType() == DataType::Float32); \
\
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>()); \
void *const bData = (op->getInputs(1)->getRawDataPtr<void *>()); \
void *const cData = (op->getOutput()->getRawDataPtr<void *>()); \
\
auto a = op->getInputs(0)->getDims(); \
auto aS = op->getInputs(0)->getStride(); \
auto b = op->getInputs(1)->getDims(); \
auto bS = op->getInputs(1)->getStride(); \
auto c = op->getOutput()->getDims(); \
auto cS = op->getOutput()->getStride(); \
\
std::vector<int64_t> aDim = castTo64(a); \
std::vector<int64_t> aStride = castTo64(aS); \
std::vector<int64_t> bDim = castTo64(b); \
std::vector<int64_t> bStride = castTo64(bS); \
std::vector<int64_t> cDim = castTo64(c); \
std::vector<int64_t> cStride = castTo64(cS); \
\
auto inputA = aclCreateTensor( \
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0, \
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData); \
auto inputB = aclCreateTensor( \
bDim.data(), bDim.size(), ACL_FLOAT, bStride.data(), 0, \
aclFormat::ACL_FORMAT_ND, bDim.data(), bDim.size(), bData); \
auto output = aclCreateTensor( \
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0, \
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData); \
\
uint64_t workspaceSize = 0; \
aclOpExecutor *executor; \
\
auto ret = aclnn##prefix##GetWorkspaceSize( \
inputA, inputB, output, &workspaceSize, &executor); \
checkASCENDError(ret); \
void *workspaceAddr = nullptr; \
if (workspaceSize > 0) { \
workspaceAddr = context->getWorkspace(workspaceSize); \
} \
\
ret = aclnn##prefix(workspaceAddr, workspaceSize, executor, \
context->ASCENDHandle()); \
checkASCENDError(ret); \
\
aclDestroyTensor(inputA); \
aclDestroyTensor(inputB); \
aclDestroyTensor(output); \
\
return; \
} \
};
class AddAclnn : public ASCENDKernelWithoutConfig {
virtual tuple<float, float, float> getAlphBeta() const {
return {1.f, 1.f, 0.f};
}
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<ElementWiseObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const bData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto b = op->getInputs(1)->getDims();
auto bS = op->getInputs(1)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> bDim = castTo64(b);
std::vector<int64_t> bStride = castTo64(bS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto inputB = aclCreateTensor(
bDim.data(), bDim.size(), ACL_FLOAT, bStride.data(), 0,
aclFormat::ACL_FORMAT_ND, bDim.data(), bDim.size(), bData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
auto [aAlpha, bAlpha, beta] = getAlphBeta();
auto alpha = aclCreateScalar(&bAlpha, ACL_FLOAT);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnAddGetWorkspaceSize(inputA, inputB, alpha, output,
&workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnAdd(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyTensor(inputB);
aclDestroyScalar(alpha);
aclDestroyTensor(output);
return;
}
};
class SubAclnn : public ASCENDKernelWithoutConfig {
virtual tuple<float, float, float> getAlphBeta() const {
return {1.f, 1.f, 0.f};
}
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<ElementWiseObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const bData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto b = op->getInputs(1)->getDims();
auto bS = op->getInputs(1)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> bDim = castTo64(b);
std::vector<int64_t> bStride = castTo64(bS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto inputB = aclCreateTensor(
bDim.data(), bDim.size(), ACL_FLOAT, bStride.data(), 0,
aclFormat::ACL_FORMAT_ND, bDim.data(), bDim.size(), bData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
auto [aAlpha, bAlpha, beta] = getAlphBeta();
auto alpha = aclCreateScalar(&bAlpha, ACL_FLOAT);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnSubGetWorkspaceSize(inputA, inputB, alpha, output,
&workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnSub(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyTensor(inputB);
aclDestroyScalar(alpha);
aclDestroyTensor(output);
return;
}
};
DEFINE_ELEMENT_WISE_Aclnn(PowTensorTensor);
DEFINE_ELEMENT_WISE_Aclnn(Div);
DEFINE_ELEMENT_WISE_Aclnn(Mul);
DEFINE_ELEMENT_WISE_Aclnn(Maximum);
REGISTER_KERNEL(Device::ASCEND, OpType::Pow, PowTensorTensorAclnn,
"pow_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Div, DivAclnn, "div_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Mul, MulAclnn, "mul_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Add, AddAclnn, "add_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Sub, SubAclnn, "sub_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Max, MaximumAclnn, "max_ASCEND_float");
// REGISTER_KERNEL(Device::ASCEND, OpType::Abs, AbsAclnn, "abs_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,78 @@
#include "operators/gather.h"
#include "aclnnop/level2/aclnn_gather_v2.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class GatherAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<GatherObj>(_op);
IT_ASSERT(op->getInputs(1)->getDType() == DataType::Int32 ||
op->getInputs(1)->getDType() == DataType::Int64);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const bData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
int64_t axis = int64_t(op->getAxis());
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto b = op->getInputs(1)->getDims();
auto bS = op->getInputs(1)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
if (b.size() == 0) {
c.insert(c.begin() + axis, 1);
cS.insert(cS.begin() + axis, axis > 0 ? cS[axis - 1] : cS[0]);
}
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> bDim = castTo64(b);
std::vector<int64_t> bStride = castTo64(bS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto inputB = aclCreateTensor(
bDim.data(), bDim.size(), ACL_INT64, bStride.data(), 0,
aclFormat::ACL_FORMAT_ND, bDim.data(), bDim.size(), bData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnGatherV2GetWorkspaceSize(inputA, axis, inputB, output,
&workspaceSize, &executor);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
checkASCENDError(ret);
ret = aclnnGatherV2(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyTensor(inputB);
aclDestroyTensor(output);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Gather, GatherAclnn,
"gather_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,104 @@
#include "operators/instance_norm.h"
#include "aclnnop/level2/aclnn_layer_norm.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
#include "operators/gather.h"
namespace infini {
class InstanceNormAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<InstanceNormObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const inputData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const weightData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const outputData = (op->getOutput()->getRawDataPtr<void *>());
auto inputD = op->getInputs(0)->getDims();
auto inputS = op->getInputs(0)->getStride();
auto weightD = op->getInputs(1)->getDims();
auto weightS = op->getInputs(1)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
double eps = static_cast<double>(op->getEps());
std::vector<int64_t> inputDim = castTo64(inputD);
std::vector<int64_t> inputStride = castTo64(inputS);
std::vector<int64_t> weightDim = castTo64(weightD);
std::vector<int64_t> weightStride = castTo64(weightS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
auto axis = 3;
auto rank = static_cast<int>(inputDim.size());
std::vector<int64_t> normalizedShape(rank - axis, 0);
for (auto i = rank; i > axis; --i) {
normalizedShape[i - 1 - axis] = inputDim[i - 1];
}
auto inputTensor =
aclCreateTensor(inputDim.data(), inputDim.size(), ACL_FLOAT,
inputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
inputDim.data(), inputDim.size(), inputData);
auto weightTensor =
aclCreateTensor(weightDim.data(), weightDim.size(), ACL_FLOAT,
weightStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
weightDim.data(), weightDim.size(), weightData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
outputDim.data(), outputDim.size(), outputData);
auto *normArray =
aclCreateIntArray(normalizedShape.data(), normalizedShape.size());
aclTensor *biasTensor = NULL;
if (op->numInputs() == 3) {
void *const biasData = (op->getInputs(2)->getRawDataPtr<void *>());
auto biasD = op->getInputs(2)->getDims();
auto biasS = op->getInputs(2)->getStride();
std::vector<int64_t> biasDim = castTo64(biasD);
std::vector<int64_t> biasStride = castTo64(biasS);
biasTensor = aclCreateTensor(
biasDim.data(), biasDim.size(), ACL_FLOAT, biasStride.data(), 0,
aclFormat::ACL_FORMAT_NCHW, biasDim.data(), biasDim.size(),
biasData);
}
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnLayerNormGetWorkspaceSize(
inputTensor, normArray, weightTensor, biasTensor, eps, outputTensor,
NULL, NULL, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnLayerNorm(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputTensor);
aclDestroyTensor(weightTensor);
aclDestroyIntArray(normArray);
aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::InstanceNormalization,
InstanceNormAclnn, "InstanceNorm_ASCEND");
} // namespace infini

View File

@ -0,0 +1,103 @@
#include "operators/layer_norm.h"
#include "aclnnop/level2/aclnn_layer_norm.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
#include "operators/gather.h"
namespace infini {
class LayerNormAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<LayerNormObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const inputData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const weightData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const outputData = (op->getOutput()->getRawDataPtr<void *>());
auto inputD = op->getInputs(0)->getDims();
auto inputS = op->getInputs(0)->getStride();
auto weightD = op->getInputs(1)->getDims();
auto weightS = op->getInputs(1)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
double eps = static_cast<double>(op->getEps());
std::vector<int64_t> inputDim = castTo64(inputD);
std::vector<int64_t> inputStride = castTo64(inputS);
std::vector<int64_t> weightDim = castTo64(weightD);
std::vector<int64_t> weightStride = castTo64(weightS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
auto axis = op->getAxis();
auto rank = static_cast<int>(inputDim.size());
std::vector<int64_t> normalizedShape(rank - axis, 0);
for (auto i = rank; i > axis; --i) {
normalizedShape[i - 1 - axis] = inputDim[i - 1];
}
auto inputTensor =
aclCreateTensor(inputDim.data(), inputDim.size(), ACL_FLOAT,
inputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
inputDim.data(), inputDim.size(), inputData);
auto weightTensor =
aclCreateTensor(weightDim.data(), weightDim.size(), ACL_FLOAT,
weightStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
weightDim.data(), weightDim.size(), weightData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
outputDim.data(), outputDim.size(), outputData);
auto *normArray =
aclCreateIntArray(normalizedShape.data(), normalizedShape.size());
aclTensor *biasTensor = NULL;
if (op->numInputs() == 3) {
void *const biasData = (op->getInputs(2)->getRawDataPtr<void *>());
auto biasD = op->getInputs(2)->getDims();
auto biasS = op->getInputs(2)->getStride();
std::vector<int64_t> biasDim = castTo64(biasD);
std::vector<int64_t> biasStride = castTo64(biasS);
biasTensor = aclCreateTensor(
biasDim.data(), biasDim.size(), ACL_FLOAT, biasStride.data(), 0,
aclFormat::ACL_FORMAT_NCHW, biasDim.data(), biasDim.size(),
biasData);
}
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnLayerNormGetWorkspaceSize(
inputTensor, normArray, weightTensor, biasTensor, eps, outputTensor,
NULL, NULL, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnLayerNorm(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputTensor);
aclDestroyTensor(weightTensor);
aclDestroyIntArray(normArray);
aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::LayerNormalization, LayerNormAclnn,
"LayerNorm_ASCEND");
} // namespace infini

View File

@ -0,0 +1,118 @@
#include "operators/matmul.h"
#include "aclnnop/level2/aclnn_gemm.h"
#include "aclnnop/level2/aclnn_matmul.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class MatmulAclnn : public ASCENDKernelWithoutConfig {
// unsupport trans for "gemm" whithou biasInput
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<MatmulObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
auto input_num = op->numInputs();
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const bData = (op->getInputs(1)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
void *biasData = NULL;
if (input_num > 2) {
biasData = (op->getInputs(2)->getRawDataPtr<void *>());
}
auto selfD = op->getInputs(0)->getDims();
auto selfS = op->getInputs(0)->getStride();
auto matD = op->getInputs(1)->getDims();
auto matS = op->getInputs(1)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
std::vector<int> biasD;
std::vector<int> biasS;
if (input_num > 2) {
biasD = op->getInputs(2)->getDims();
biasS = op->getInputs(2)->getStride();
}
std::vector<int64_t> selfDim = castTo64(selfD);
std::vector<int64_t> selfStride = castTo64(selfS);
std::vector<int64_t> matDim = castTo64(matD);
std::vector<int64_t> matStride = castTo64(matS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
std::vector<int64_t> biasDim;
std::vector<int64_t> biasStride;
if (input_num > 2) {
biasDim = castTo64(biasD);
biasStride = castTo64(biasS);
}
auto selfTensor = aclCreateTensor(
selfDim.data(), selfDim.size(), ACL_FLOAT, selfStride.data(), 0,
aclFormat::ACL_FORMAT_ND, selfDim.data(), selfDim.size(), aData);
auto matTensor = aclCreateTensor(
matDim.data(), matDim.size(), ACL_FLOAT, matStride.data(), 0,
aclFormat::ACL_FORMAT_ND, matDim.data(), matDim.size(), bData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_ND,
outputDim.data(), outputDim.size(), cData);
aclTensor *biasTensor = NULL;
if (input_num > 2) {
biasTensor =
aclCreateTensor(biasDim.data(), biasDim.size(), ACL_FLOAT,
biasStride.data(), 0, aclFormat::ACL_FORMAT_ND,
biasDim.data(), biasDim.size(), biasData);
}
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
if (input_num > 2) {
float alpha = 1.0;
float beta = 1.0;
int32_t transA = op->getTransA();
int32_t transB = op->getTransB();
auto ret = aclnnGemmGetWorkspaceSize(
selfTensor, matTensor, biasTensor, alpha, beta, int64_t(transA),
int64_t(transB), outputTensor, 1, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnGemm(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
} else {
auto ret =
aclnnMatmulGetWorkspaceSize(selfTensor, matTensor, outputTensor,
1, &workspaceSize, &executor);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
checkASCENDError(ret);
ret = aclnnMatmul(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
}
// aclDestroyTensor(selfTensor);
// aclDestroyTensor(matTensor);
// aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::MatMul, MatmulAclnn,
"matmul_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,83 @@
#include "aclnnop/aclnn_constant_pad_nd.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
#include "operators/pad.h"
namespace infini {
class PadAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<PadObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto inputD = op->getInputs(0)->getDims();
auto inputS = op->getInputs(0)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
std::vector<int64_t> inputDim = castTo64(inputD);
std::vector<int64_t> inputStride = castTo64(inputS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
auto inputTensor = aclCreateTensor(
inputDim.data(), inputDim.size(), ACL_FLOAT, inputStride.data(), 0,
aclFormat::ACL_FORMAT_ND, inputDim.data(), inputDim.size(), aData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_ND,
outputDim.data(), outputDim.size(), cData);
std::vector<int> intPads = op->getPads();
std::size_t length = intPads.size();
std::vector<int64_t> pads(length);
std::size_t halfLen = length / 2;
for (std::size_t i = 0; i < halfLen; ++i) {
pads[2 * i] = intPads[halfLen - i - 1];
pads[2 * i + 1] = intPads[2 * halfLen - i - 1];
}
std::cout << "pads = " << vecToString(pads) << std::endl;
aclIntArray *padding = aclCreateIntArray(pads.data(), length);
float valueValue = 0.0f;
auto value = aclCreateScalar(&valueValue, ACL_FLOAT);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnConstantPadNdGetWorkspaceSize(
inputTensor, padding, value, outputTensor, &workspaceSize,
&executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnConstantPadNd(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputTensor);
aclDestroyIntArray(padding);
aclDestroyScalar(value);
aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Pad, PadAclnn, "pad_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,154 @@
#include "operators/pooling.h"
#include "aclnnop/level2/aclnn_avgpool2d.h"
#include "aclnnop/level2/aclnn_max_pool.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class AvgPooling : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<PoolingObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto [n, c, h, w, kh, kw] = op->getNCHWRS();
auto [ph, pw, sh, sw, dh, dw] = op->getPadStrideDilation();
std::vector<int64_t> ksize = {kh, kw};
std::vector<int64_t> stride = {sh, sw};
std::vector<int64_t> pad = {ph, pw};
int64_t divisorOverride = 0;
auto selfD = op->getInputs(0)->getDims();
auto selfS = op->getInputs(0)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
std::vector<int64_t> selfDim = castTo64(selfD);
std::vector<int64_t> selfStride = castTo64(selfS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
aclIntArray *kernelSize = aclCreateIntArray(ksize.data(), ksize.size());
aclIntArray *strides = aclCreateIntArray(stride.data(), stride.size());
aclIntArray *paddings = aclCreateIntArray(pad.data(), pad.size());
auto selfTensor = aclCreateTensor(
selfDim.data(), selfDim.size(), ACL_FLOAT, selfStride.data(), 0,
aclFormat::ACL_FORMAT_NCHW, selfDim.data(), selfDim.size(), aData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
outputDim.data(), outputDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnAvgPool2dGetWorkspaceSize(
selfTensor, kernelSize, strides, paddings, false, true,
divisorOverride, int8_t(0), outputTensor, &workspaceSize,
&executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnAvgPool2d(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(selfTensor);
aclDestroyIntArray(kernelSize);
aclDestroyIntArray(strides);
aclDestroyIntArray(paddings);
aclDestroyTensor(outputTensor);
return;
}
};
class MaxPooling : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<PoolingObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto [n, c, h, w, kh, kw] = op->getNCHWRS();
auto [ph, pw, sh, sw, dh, dw] = op->getPadStrideDilation();
int64_t ceilMode = int64_t(op->getCeilMode());
std::vector<int64_t> ksize = {kh, kw};
std::vector<int64_t> stride = {sh, sw};
std::vector<int64_t> pad = {ph, pw};
std::vector<int64_t> dilation = {dh, dw};
auto selfD = op->getInputs(0)->getDims();
auto selfS = op->getInputs(0)->getStride();
auto outD = op->getOutput()->getDims();
auto outS = op->getOutput()->getStride();
std::vector<int64_t> selfDim = castTo64(selfD);
std::vector<int64_t> selfStride = castTo64(selfS);
std::vector<int64_t> outputDim = castTo64(outD);
std::vector<int64_t> outputStride = castTo64(outS);
aclIntArray *kernelSize = aclCreateIntArray(ksize.data(), ksize.size());
aclIntArray *strides = aclCreateIntArray(stride.data(), stride.size());
aclIntArray *paddings = aclCreateIntArray(pad.data(), pad.size());
aclIntArray *dilations =
aclCreateIntArray(dilation.data(), dilation.size());
auto selfTensor = aclCreateTensor(
selfDim.data(), selfDim.size(), ACL_FLOAT, selfStride.data(), 0,
aclFormat::ACL_FORMAT_NCHW, selfDim.data(), selfDim.size(), aData);
auto outputTensor =
aclCreateTensor(outputDim.data(), outputDim.size(), ACL_FLOAT,
outputStride.data(), 0, aclFormat::ACL_FORMAT_NCHW,
outputDim.data(), outputDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnMaxPoolGetWorkspaceSize(
selfTensor, kernelSize, strides, 0, paddings, dilations, ceilMode,
outputTensor, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnMaxPool(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(selfTensor);
aclDestroyIntArray(kernelSize);
aclDestroyIntArray(strides);
aclDestroyIntArray(paddings);
aclDestroyIntArray(dilations);
aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::MaxPool, MaxPooling,
"maxpooling_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::AveragePool, AvgPooling,
"avgpooling_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,50 @@
#ifdef INFINI_USE_HCCL
#include "operators/recv.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
#include "ascend/hccl_communicator.h"
#include "hccl/hccl.h"
#include "hccl/hccl_types.h"
namespace infini {
class RecvHCCL : public ASCENDKernelWithoutConfig {
public:
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<RecvObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *output = op->getOutput(0)->getRawDataPtr<void *>();
IT_ASSERT(op->getDType() == DataType::Float32);
const auto shape = op->getShape();
int nDims = shape.size();
int outputCount = 1;
for (int i = 0; i < nDims; i++) {
outputCount *= shape[i];
}
HcclComm comm =
dynamic_cast<HcclCommunicatorObj &>(context->getCommunicator())
.getHcclComm();
uint32_t rank;
checkHCCLError(HcclGetRankId(comm, &rank));
int source = op->getSourceRank();
int destination = op->getDestinationRank();
if (int(rank) == destination) {
checkHCCLError(HcclRecv(output, uint64_t(outputCount),
HCCL_DATA_TYPE_FP32, uint32_t(source), comm,
context->ASCENDHandle()));
}
checkASCENDError(aclrtSynchronizeStream(context->ASCENDHandle()));
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Recv, RecvHCCL, "Recv_HCCL_ASCEND");
} // namespace infini
#endif

View File

@ -0,0 +1,131 @@
#include "operators/reduce.h"
#include "aclnnop/aclnn_mean.h"
#include "aclnnop/aclnn_reduce_sum.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class MeanAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<ReduceBaseObj>(_op);
IT_ASSERT(op->getDType() == DataType::Float32);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto axes_set = op->getAxes();
std::vector<int> axes;
axes.assign(axes_set.begin(), axes_set.end());
bool KeepDim = op->getKeepDims();
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
std::vector<int64_t> axes_64 = castTo64(axes);
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
aclIntArray *dim = aclCreateIntArray(axes_64.data(), axes_64.size());
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnMeanV2GetWorkspaceSize(
inputA, dim, KeepDim, true, output, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnMeanV2(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyIntArray(dim);
aclDestroyTensor(output);
return;
}
};
class ReduceSumAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<ReduceBaseObj>(_op);
IT_ASSERT(op->getDType() == DataType::Float32);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto axes_set = op->getAxes();
std::vector<int> axes;
axes.assign(axes_set.begin(), axes_set.end());
bool KeepDim = op->getKeepDims();
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
std::vector<int64_t> axes_64 = castTo64(axes);
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
aclIntArray *dim = aclCreateIntArray(axes_64.data(), axes_64.size());
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnReduceSumGetWorkspaceSize(
inputA, dim, KeepDim, ACL_FLOAT, output, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnReduceSum(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyIntArray(dim);
aclDestroyTensor(output);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::ReduceMean, MeanAclnn,
"reduceMean_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::ReduceSum, ReduceSumAclnn,
"reduceSum_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,62 @@
#include "operators/reshape.h"
#include "aclnnop/level2/aclnn_copy.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class CopyAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &op,
const RuntimeObj *_context) const override {
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto aD = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
std::vector<int64_t> aDim = castTo64(aD);
std::vector<int64_t> aStride = castTo64(aS);
auto srcTensor = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto outputTensor = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnInplaceCopyGetWorkspaceSize(outputTensor, srcTensor,
&workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnInplaceCopy(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(srcTensor);
aclDestroyTensor(outputTensor);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Reshape, CopyAclnn,
"reshape_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Unsqueeze, CopyAclnn,
"unsqueeze_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Squeeze, CopyAclnn,
"squeeze_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Flatten, CopyAclnn,
"Flatten_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Identity, CopyAclnn,
"Identity_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,82 @@
#include "operators/resize.h"
#include "aclnnop/level2/aclnn_resize.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class ResizeAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<ResizeObj>(_op);
IT_ASSERT(op->getDType() == DataType::Float32);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
int nDims = op->getInputs(0)->getRank();
if (nDims > 4)
IT_TODO_HALT();
vector<float> scalesData = op->getScales();
const char *mode;
switch (op->getMode()) {
case ResizeObj::ECoeffMode::nearest:
mode = "nearest";
break;
case ResizeObj::ECoeffMode::linear:
mode = "bilinear";
break;
default:
IT_TODO_HALT();
}
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
auto self = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_NCHW, aDim.data(), aDim.size(), aData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_NCHW, cDim.data(), cDim.size(), cData);
aclFloatArray *scales = nullptr;
scales = aclCreateFloatArray(scalesData.data(), scalesData.size());
assert(scales != nullptr);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnResizeGetWorkspaceSize(self, scales, mode, output,
&workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnResize(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(self);
aclDestroyFloatArray(scales);
aclDestroyTensor(output);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Resize, ResizeAclnn, "Resize_ASCEND");
} // namespace infini

View File

@ -0,0 +1,45 @@
#ifdef INFINI_USE_HCCL
#include "operators/send.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
#include "ascend/hccl_communicator.h"
#include "hccl/hccl.h"
#include "hccl/hccl_types.h"
namespace infini {
class SendHCCL : public ASCENDKernelWithoutConfig {
public:
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<SendObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
void *input = op->getInputs(0)->getRawDataPtr<void *>();
IT_ASSERT(op->getDType() == DataType::Float32);
int inputCount =
op->getInputs(0)->getBytes() / op->getDType().getSize();
HcclComm comm =
dynamic_cast<HcclCommunicatorObj &>(context->getCommunicator())
.getHcclComm();
uint32_t rank;
checkHCCLError(HcclGetRankId(comm, &rank));
int source = op->getSourceRank();
int destination = op->getDestinationRank();
if (int(rank) == source) {
checkHCCLError(HcclSend(input, uint64_t(inputCount),
HCCL_DATA_TYPE_FP32, uint32_t(destination),
comm, context->ASCENDHandle()));
}
checkASCENDError(aclrtSynchronizeStream(context->ASCENDHandle()));
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Send, SendHCCL, "Send_HCCL_ASCEND");
} // namespace infini
#endif

View File

@ -0,0 +1,84 @@
#include "operators/slice.h"
#include "aclnnop/aclnn_slice_v2.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class SliceAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<SliceObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto starts_32 = op->getStarts();
auto ends_32 = op->getEnds();
auto steps_32 = op->getSteps();
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
std::vector<int64_t> starts_64 = castTo64(starts_32);
std::vector<int64_t> ends_64 = castTo64(ends_32);
std::vector<int64_t> steps_64 = castTo64(steps_32);
vector<int64_t> axes_64 = vector<int64_t>(starts_32.size(), 0);
for (int i = 0; i < int(starts_32.size()); i++) {
axes_64[i] = i;
}
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
aclIntArray *starts =
aclCreateIntArray(starts_64.data(), starts_64.size());
aclIntArray *ends = aclCreateIntArray(ends_64.data(), ends_64.size());
aclIntArray *steps =
aclCreateIntArray(steps_64.data(), steps_64.size());
aclIntArray *axes = aclCreateIntArray(axes_64.data(), axes_64.size());
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret =
aclnnSliceV2GetWorkspaceSize(inputA, starts, ends, axes, steps,
output, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnSliceV2(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyIntArray(starts);
aclDestroyIntArray(ends);
aclDestroyIntArray(axes);
aclDestroyIntArray(steps);
aclDestroyTensor(output);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Slice, SliceAclnn,
"slice_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,61 @@
#include "operators/softmax.h"
#include "aclnnop/level2/aclnn_softmax.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class SoftmaxAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<SoftmaxObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
int64_t axis = int64_t(op->getAxis());
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
auto input = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnSoftmaxGetWorkspaceSize(input, axis, output,
&workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnSoftmax(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(input);
aclDestroyTensor(output);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Softmax, SoftmaxAclnn,
"softmax_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,75 @@
#include "operators/split.h"
#include "aclnnop/aclnn_split_tensor.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class SplitAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<SplitObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
int64_t dim = op->getDim();
int num = op->numOutputs();
int dimSize = a.at(op->getDim());
uint64_t splitSections = dimSize / num;
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
std::vector<aclTensor *> outputsData{};
for (int i = 0; i < num; ++i) {
auto c = op->getOutput(i)->getDims();
auto cS = op->getOutput(i)->getStride();
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
void *const cData = (op->getOutput(i)->getRawDataPtr<void *>());
aclTensor *tmpTensor = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
outputsData.push_back(tmpTensor);
}
aclTensorList *tensorList =
aclCreateTensorList(outputsData.data(), outputsData.size());
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnSplitTensorGetWorkspaceSize(
inputA, splitSections, dim, tensorList, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnSplitTensor(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyTensorList(tensorList);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Split, SplitAclnn,
"split_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,136 @@
#include "operators/transpose.h"
#include "aclnnop/level2/aclnn_permute.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class PermuteAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<TransposeObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto a = op->getInputs(0)->getDims();
auto aS = op->getInputs(0)->getStride();
auto c = op->getOutput()->getDims();
auto cS = op->getOutput()->getStride();
std::vector<int64_t> aDim = castTo64(a);
std::vector<int64_t> aStride = castTo64(aS);
std::vector<int64_t> cDim = castTo64(c);
std::vector<int64_t> cStride = castTo64(cS);
auto _permute = op->getPermute();
std::vector<int64_t> permute = castTo64(_permute);
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
aclIntArray *dims = aclCreateIntArray(permute.data(), permute.size());
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnPermuteGetWorkspaceSize(inputA, dims, output,
&workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnPermute(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyIntArray(dims);
aclDestroyTensor(output);
return;
}
};
class DepthToSpaceAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<DepthToSpaceObj>(_op);
IT_ASSERT(op->getDType() == DataType::Float32);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto getStride = [](std::vector<int> Dim) {
Shape stride(Dim.size());
ShapeElem p = 1;
for (auto i = Dim.size(); i > 0; --i) {
stride[i - 1] = p;
p = p * Dim[i - 1];
}
return stride;
};
auto reshapeDim = op->getReshapeDim();
auto reshapeStride = getStride(reshapeDim);
auto transposeDim = op->getTransposeDim();
auto transposeStride = getStride(transposeDim);
std::vector<int64_t> aDim = castTo64(reshapeDim);
std::vector<int64_t> aStride = castTo64(reshapeStride);
std::vector<int64_t> cDim = castTo64(transposeDim);
std::vector<int64_t> cStride = castTo64(transposeStride);
auto mode = op->getMode();
std::vector<int64_t> permute;
if (mode == 0) {
permute = {0, 3, 4, 1, 5, 2};
} else {
permute = {0, 1, 4, 2, 5, 3};
}
auto inputA = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
aclIntArray *dims = aclCreateIntArray(permute.data(), permute.size());
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret = aclnnPermuteGetWorkspaceSize(inputA, dims, output,
&workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnPermute(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(inputA);
aclDestroyIntArray(dims);
aclDestroyTensor(output);
return;
}
};
REGISTER_KERNEL(Device::ASCEND, OpType::Transpose, PermuteAclnn,
"transpose_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::DepthToSpace, DepthToSpaceAclnn,
"DepthToSpace_ASCEND_float");
} // namespace infini

258
src/kernels/ascend/unary.cc Normal file
View File

@ -0,0 +1,258 @@
#include "operators/unary.h"
#include "aclnnop/aclnn_erf.h"
#include "aclnnop/level2/aclnn_abs.h"
#include "aclnnop/level2/aclnn_acos.h"
#include "aclnnop/level2/aclnn_atan.h"
#include "aclnnop/level2/aclnn_ceil.h"
#include "aclnnop/level2/aclnn_cos.h"
#include "aclnnop/level2/aclnn_exp.h"
#include "aclnnop/level2/aclnn_floor.h"
#include "aclnnop/level2/aclnn_gelu.h"
#include "aclnnop/level2/aclnn_hardswish.h"
#include "aclnnop/level2/aclnn_leaky_relu.h"
#include "aclnnop/level2/aclnn_neg.h"
#include "aclnnop/level2/aclnn_reciprocal.h"
#include "aclnnop/level2/aclnn_relu.h"
#include "aclnnop/level2/aclnn_round.h"
#include "aclnnop/level2/aclnn_sigmoid.h"
#include "aclnnop/level2/aclnn_sin.h"
#include "aclnnop/level2/aclnn_sqrt.h"
#include "aclnnop/level2/aclnn_tanh.h"
#include "ascend/ascend_kernel_without_config.h"
#include "ascend/ascend_runtime.h"
namespace infini {
class ReluAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<UnaryObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto a = op->getInputs(0)->getDims();
std::vector<int64_t> aDim(a.size(), 1);
for (size_t i = 0; i < a.size(); ++i) {
aDim[i] = int64_t(a[i]);
}
auto aS = op->getInputs(0)->getStride();
std::vector<int64_t> aStride(aS.size(), 1);
for (size_t i = 0; i < aS.size(); ++i) {
aStride[i] = int64_t(aS[i]);
}
auto c = op->getInputs(0)->getDims();
std::vector<int64_t> cDim(c.size(), 1);
for (size_t i = 0; i < c.size(); ++i) {
cDim[i] = int64_t(c[i]);
}
auto cS = op->getInputs(0)->getStride();
std::vector<int64_t> cStride(cS.size(), 1);
for (size_t i = 0; i < cS.size(); ++i) {
cStride[i] = int64_t(cS[i]);
}
auto input = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
auto ret =
aclnnReluGetWorkspaceSize(input, output, &workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnRelu(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(input);
aclDestroyTensor(output);
return;
}
};
class LeakyReluAclnn : public ASCENDKernelWithoutConfig {
void compute(const Operator &_op,
const RuntimeObj *_context) const override {
auto op = as<LeakyReluObj>(_op);
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context);
IT_ASSERT(op->getDType() == DataType::Float32);
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>());
void *const cData = (op->getOutput()->getRawDataPtr<void *>());
auto a = op->getInputs(0)->getDims();
std::vector<int64_t> aDim(a.size(), 1);
for (size_t i = 0; i < a.size(); ++i) {
aDim[i] = int64_t(a[i]);
}
auto aS = op->getInputs(0)->getStride();
std::vector<int64_t> aStride(aS.size(), 1);
for (size_t i = 0; i < aS.size(); ++i) {
aStride[i] = int64_t(aS[i]);
}
auto c = op->getInputs(0)->getDims();
std::vector<int64_t> cDim(c.size(), 1);
for (size_t i = 0; i < c.size(); ++i) {
cDim[i] = int64_t(c[i]);
}
auto cS = op->getInputs(0)->getStride();
std::vector<int64_t> cStride(cS.size(), 1);
for (size_t i = 0; i < cS.size(); ++i) {
cStride[i] = int64_t(cS[i]);
}
auto input = aclCreateTensor(
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0,
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData);
auto output = aclCreateTensor(
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0,
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
float negativeSlopeValue = op->getAlpha();
aclScalar *negativeSlope = nullptr;
negativeSlope =
aclCreateScalar(&negativeSlopeValue, aclDataType::ACL_FLOAT);
auto ret = aclnnLeakyReluGetWorkspaceSize(input, negativeSlope, output,
&workspaceSize, &executor);
checkASCENDError(ret);
void *workspaceAddr = nullptr;
if (workspaceSize > 0) {
workspaceAddr = context->getWorkspace(workspaceSize);
}
ret = aclnnLeakyRelu(workspaceAddr, workspaceSize, executor,
context->ASCENDHandle());
checkASCENDError(ret);
aclDestroyTensor(input);
aclDestroyScalar(negativeSlope);
aclDestroyTensor(output);
return;
}
};
#define DEFINE_UNARY_Aclnn(prefix) \
class prefix##Aclnn : public ASCENDKernelWithoutConfig { \
void compute(const Operator &_op, \
const RuntimeObj *_context) const override { \
auto op = as<UnaryObj>(_op); \
auto context = dynamic_cast<const ASCENDRuntimeObj *>(_context); \
IT_ASSERT(op->getDType() == DataType::Float32); \
\
void *const aData = (op->getInputs(0)->getRawDataPtr<void *>()); \
void *const cData = (op->getOutput()->getRawDataPtr<void *>()); \
\
auto a = op->getInputs(0)->getDims(); \
std::vector<int64_t> aDim(a.size(), 1); \
for (size_t i = 0; i < a.size(); ++i) { \
aDim[i] = int64_t(a[i]); \
} \
auto aS = op->getInputs(0)->getStride(); \
std::vector<int64_t> aStride(aS.size(), 1); \
for (size_t i = 0; i < aS.size(); ++i) { \
aStride[i] = int64_t(aS[i]); \
} \
auto c = op->getInputs(0)->getDims(); \
std::vector<int64_t> cDim(c.size(), 1); \
for (size_t i = 0; i < c.size(); ++i) { \
cDim[i] = int64_t(c[i]); \
} \
auto cS = op->getInputs(0)->getStride(); \
std::vector<int64_t> cStride(cS.size(), 1); \
for (size_t i = 0; i < cS.size(); ++i) { \
cStride[i] = int64_t(cS[i]); \
} \
\
auto input = aclCreateTensor( \
aDim.data(), aDim.size(), ACL_FLOAT, aStride.data(), 0, \
aclFormat::ACL_FORMAT_ND, aDim.data(), aDim.size(), aData); \
auto output = aclCreateTensor( \
cDim.data(), cDim.size(), ACL_FLOAT, cStride.data(), 0, \
aclFormat::ACL_FORMAT_ND, cDim.data(), cDim.size(), cData); \
\
uint64_t workspaceSize = 0; \
aclOpExecutor *executor; \
\
auto ret = aclnn##prefix##GetWorkspaceSize( \
input, output, &workspaceSize, &executor); \
checkASCENDError(ret); \
void *workspaceAddr = nullptr; \
if (workspaceSize > 0) { \
workspaceAddr = context->getWorkspace(workspaceSize); \
} \
\
ret = aclnn##prefix(workspaceAddr, workspaceSize, executor, \
context->ASCENDHandle()); \
checkASCENDError(ret); \
\
aclDestroyTensor(input); \
aclDestroyTensor(output); \
\
return; \
} \
};
DEFINE_UNARY_Aclnn(Abs);
DEFINE_UNARY_Aclnn(Sigmoid);
DEFINE_UNARY_Aclnn(Hardswish);
DEFINE_UNARY_Aclnn(Gelu);
DEFINE_UNARY_Aclnn(Tanh);
DEFINE_UNARY_Aclnn(Sin);
DEFINE_UNARY_Aclnn(Cos);
DEFINE_UNARY_Aclnn(Acos);
DEFINE_UNARY_Aclnn(Atan);
DEFINE_UNARY_Aclnn(Ceil);
DEFINE_UNARY_Aclnn(Floor);
DEFINE_UNARY_Aclnn(Exp);
DEFINE_UNARY_Aclnn(Neg);
DEFINE_UNARY_Aclnn(Reciprocal);
DEFINE_UNARY_Aclnn(Sqrt);
DEFINE_UNARY_Aclnn(Round);
DEFINE_UNARY_Aclnn(Erf);
REGISTER_KERNEL(Device::ASCEND, OpType::Relu, ReluAclnn, "relu_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::LeakyRelu, LeakyReluAclnn,
"leakyrelu_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Abs, AbsAclnn, "abs_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Sigmoid, SigmoidAclnn,
"sigmoid_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::HardSwish, HardswishAclnn,
"hardswish_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Tanh, TanhAclnn, "tanh_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Gelu, GeluAclnn, "gelu_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Sin, SinAclnn, "sin_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Cos, CosAclnn, "cos_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Acos, AcosAclnn, "acos_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Atan, AtanAclnn, "atan_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Neg, NegAclnn, "neg_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Ceil, CeilAclnn, "ceil_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Floor, FloorAclnn,
"floor_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Exp, ExpAclnn, "exp_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Reciprocal, ReciprocalAclnn,
"reciprocal_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Sqrt, SqrtAclnn, "sqrt_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Round, RoundAclnn,
"round_ASCEND_float");
REGISTER_KERNEL(Device::ASCEND, OpType::Erf, ErfAclnn, "erf_ASCEND_float");
} // namespace infini

View File

@ -0,0 +1,48 @@
#include "operators/instance_norm.h"
#include "utils/operator_utils.h"
namespace infini {
InstanceNormObj::InstanceNormObj(GraphObj *graph, Tensor input, Tensor output,
Tensor scale, Tensor bias, float eps)
: OperatorObj(OpType::InstanceNormalization, TensorVec{input, scale, bias},
{output}),
eps(eps) {
IT_ASSERT(checkValid(graph));
}
optional<vector<Shape>> InstanceNormObj::inferShape(const TensorVec &inputs) {
return {{inputs[0]->getDims()}};
}
vector<DataType> InstanceNormObj::inferDataType(const TensorVec &inputs) const {
return {inputs[0]->getDType()};
}
std::string InstanceNormObj::toString() const {
std::ostringstream os;
os << "InstanceNormalization[" << getGuid() << "]";
os << "(";
os << vecToString(inputs[0]->getDims()) << ",";
os << "eps=" << eps << ",";
os << "input=" << inputs[0]->getGuid() << ",";
os << "scale=" << inputs[1]->getGuid() << ",";
os << "bias=" << inputs[2]->getGuid() << ",";
os << "output=";
for (auto output : outputs)
os << output->getGuid() << ",";
return os.str();
}
vector<int> InstanceNormObj::getWorkloadVector() const {
vector<int> ret = inputs[0]->getDims();
ret.emplace(ret.begin(), type.underlying());
return ret;
}
vector<int> InstanceNormObj::getOpAttrVector() const {
return {type.underlying()};
}
} // namespace infini

View File

@ -33,10 +33,11 @@ void ResizeObj::init(const Tensor &input, const Tensor &sizes,
// inputs of operator must not be nullptr, due to the check in
// OperatorObj::OperatorObj
if (nullptr != sizes) {
IT_ASSERT(isResizeBySizes());
setGivenSizes(true);
inputs.push_back(sizes);
InitBySizes(input, sizes, axes);
} else if (nullptr != scales) {
setGivenSizes(false);
inputs.push_back(scales);
InitByScales(input, scales, axes);
}
@ -101,8 +102,9 @@ void ResizeObj::InitBySizes(Tensor input, Tensor sizes,
// copy sizes data to host.
IT_ASSERT(sizes->getDataBlob() != nullptr);
Runtime runtime = NativeCpuRuntimeObj::getInstance();
std::shared_ptr<int> dataObj((int *)runtime->alloc(sizes->getBytes()),
[&](int *p) { runtime->dealloc(p); });
std::shared_ptr<int64_t> dataObj(
(int64_t *)runtime->alloc(sizes->getBytes()),
[&](int64_t *p) { runtime->dealloc(p); });
auto data = dataObj.get();
sizes->getRuntime()->copyBlobToCPU(
(void *)data, sizes->getRawDataPtr<void *>(), sizes->getBytes());
@ -193,7 +195,7 @@ vector<DataType> ResizeObj::inferDataType(const TensorVec &inputs) const {
}
if (isResizeBySizes()) {
auto sizes = inputs[1];
IT_ASSERT(sizes && sizes->getDType() == DataType::UInt32);
IT_ASSERT(sizes && sizes->getDType() == DataType::Int64);
} else {
auto scales = inputs[1];
IT_ASSERT(scales && scales->getDType() == DataType::Float32);

View File

@ -300,7 +300,8 @@ std::string LeakyReluObj::toString() const {
os << "(";
os << vecToString(inputs[0]->getDims()) << ",";
os << "input=" << inputs[0]->getGuid() << ",";
os << "output=" << outputs[0]->getGuid() << ")";
os << "output=" << outputs[0]->getGuid() << ",";
os << "alpha=" << alphaValue << ")";
return os.str();
}

View File

@ -104,6 +104,8 @@ std::string device_to_str(Device device) {
return "INTELCPU";
case Device::KUNLUN:
return "KUNLUN";
case Device::ASCEND:
return "ASCEND";
default:
IT_TODO_HALT();
}

View File

@ -0,0 +1,55 @@
#ifdef INFINI_USE_HCCL
#include "ascend/ascend_runtime.h"
#include "ascend/hccl_communicator.h"
#include "core/graph.h"
#include "core/runtime.h"
#include "operators/all_gather.h"
#include "test.h"
#include <thread>
static int WORLD_SIZE = 2;
namespace infini {
void allGather(const string taskName, int deviceID, vector<float> data,
vector<vector<float>> ans) {
// Create Runtimes and initiate communication
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
Runtime ascnedRuntime = make_ref<ASCENDRuntimeObj>(deviceID);
ascnedRuntime->initComm(taskName, WORLD_SIZE, deviceID);
// Create Graph and insert allReduce operation
Graph g = make_ref<GraphObj>(ascnedRuntime);
auto input =
g->addTensor(Shape{static_cast<int>(data.size())}, DataType::Float32);
auto op = g->addOp<AllGatherObj>(input, std::nullopt, WORLD_SIZE);
// Copy data from CPU to MLU
g->dataMalloc();
input->copyin(data);
// Run operation
ascnedRuntime->run(g);
// Copy output from MLU to CPU
for (int i = 0; i < WORLD_SIZE; ++i) {
auto result = op->getOutputs()[i]->clone(cpuRuntime);
EXPECT_TRUE(result->equalData(ans[i]));
}
}
TEST(ASCEND_AllGather, run) {
aclInit(nullptr);
vector<float> data[4] = {{2., 3.}, {5., 6.}, {7., 8.}, {9., 10.}};
vector<vector<float>> ans = {{2., 3.}, {5., 6.}, {7., 8.}, {9., 10.}};
std::vector<std::thread> threads;
for (int gpu = 0; gpu < WORLD_SIZE; ++gpu) {
threads.emplace_back(allGather, "test_all_gather", gpu, data[gpu], ans);
}
for (auto &thread : threads) {
thread.join();
}
aclFinalize();
}
} // namespace infini
#endif

View File

@ -0,0 +1,120 @@
#ifdef INFINI_USE_HCCL
#include "ascend/ascend_runtime.h"
#include "ascend/hccl_communicator.h"
#include "core/graph.h"
#include "core/runtime.h"
#include "operators/all_reduce.h"
#include "test.h"
#include <thread>
static int WORLD_SIZE = 2;
namespace infini {
template <typename OperatorObj>
void allReduce(const string taskName, int deviceID, vector<float> data,
vector<float> ans) {
// Create Runtimes and initiate communication
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
Runtime ascendRuntime = make_ref<ASCENDRuntimeObj>(deviceID);
ascendRuntime->initComm(taskName, WORLD_SIZE, deviceID);
// Create Graph and insert allReduce operation
Graph g = make_ref<GraphObj>(ascendRuntime);
auto input =
g->addTensor(Shape{static_cast<int>(data.size())}, DataType::Float32);
auto op = g->addOp<OperatorObj>(input, nullptr);
// Copy data from CPU to GPU
g->dataMalloc();
input->copyin(data);
// Run operation
ascendRuntime->run(g);
// Copy output from GPU to CPU
auto result = op->getOutput()->clone(cpuRuntime);
EXPECT_TRUE(result->equalData(ans));
}
// TEST(ASCEND_AllReduce, sum) {
// aclInit(nullptr);
// vector<float> data[2] = {{2., 3.}, {5., 6.}};
// vector<float> ans = {7., 9.};
//
// std::vector<std::thread> threads;
// for (int gpu = 0; gpu < WORLD_SIZE; ++gpu) {
// threads.emplace_back(allReduce<AllReduceSumObj>,
// "test_allreduce_sum",
// gpu, data[gpu], ans);
// }
// for (auto &thread : threads) {
// thread.join();
// }
// aclFinalize();
// }
// TEST(ASCEND_AllReduce, prod) {
// aclInit(nullptr);
// vector<float> data[2] = {{2., 3.}, {5., 6.}};
// vector<float> ans = {10., 18.};
//
// std::vector<std::thread> threads;
// for (int gpu = 0; gpu < WORLD_SIZE; ++gpu) {
// threads.emplace_back(allReduce<AllReduceProdObj>,
// "test_allreduce_prod",
// gpu, data[gpu], ans);
// }
// for (auto &thread : threads) {
// thread.join();
// }
// aclFinalize();
// }
// TEST(ASCEND_AllReduce, min) {
// aclInit(nullptr);
// vector<float> data[2] = {{2., 3.}, {5., 6.}};
// vector<float> ans = {2., 3.};
//
// std::vector<std::thread> threads;
// for (int gpu = 0; gpu < WORLD_SIZE; ++gpu) {
// threads.emplace_back(allReduce<AllReduceMinObj>,
// "test_allreduce_min",
// gpu, data[gpu], ans);
// }
// for (auto &thread : threads) {
// thread.join();
// }
// aclFinalize();
// }
TEST(ASCEND_AllReduce, max) {
aclInit(nullptr);
vector<float> data[2] = {{2., 3.}, {5., 6.}};
vector<float> ans = {5., 6.};
std::vector<std::thread> threads;
for (int gpu = 0; gpu < WORLD_SIZE; ++gpu) {
threads.emplace_back(allReduce<AllReduceMaxObj>, "test_allreduce_max",
gpu, data[gpu], ans);
}
for (auto &thread : threads) {
thread.join();
}
aclFinalize();
}
// TEST(ASCEND_AllReduce, avg) {
// vector<float> data[2] = {{2., 3.}, {5., 6.}};
// vector<float> ans = {3.5, 4.5};
//
// std::vector<std::thread> threads;
// for (int gpu = 0; gpu < WORLD_SIZE; ++gpu) {
// threads.emplace_back(allReduce<AllReduceAvgObj>,
// "test_allreduce_avg",
// gpu, data[gpu], ans);
// }
// for (auto &thread : threads) {
// thread.join();
// }
// }
} // namespace infini
#endif

View File

@ -0,0 +1,58 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/batch_norm.h"
#include "test.h"
namespace infini {
TEST(ascend_BatchNorm, run) {
aclInit(nullptr);
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build cpu graph
Graph gCpu = make_ref<GraphObj>(cpuRuntime);
auto iCpu = gCpu->addTensor(Shape{1, 3, 2, 2}, DataType::Float32);
auto meanCpu = gCpu->addTensor(Shape{3}, DataType::Float32);
auto varCpu = gCpu->addTensor(Shape{3}, DataType::Float32);
auto scaleCpu = gCpu->addTensor(Shape{3}, DataType::Float32);
auto biasCpu = gCpu->addTensor(Shape{3}, DataType::Float32);
// Build input data on CPU
gCpu->dataMalloc();
iCpu->setData(IncrementalGenerator());
meanCpu->copyin(vector<float>{1, 6, 9});
varCpu->copyin(vector<float>{4, 1, 9});
scaleCpu->setData(OneGenerator());
biasCpu->setData(ZeroGenerator());
// Build CUDA graph
Graph g = make_ref<GraphObj>(npuRuntime);
auto i = g->cloneTensor(iCpu);
auto mean = g->cloneTensor(meanCpu);
auto var = g->cloneTensor(varCpu);
auto scale = g->cloneTensor(scaleCpu);
auto bias = g->cloneTensor(biasCpu);
auto op =
g->addOp<BatchNormObj>(i, nullptr, mean, var, scale, bias, 0.9, 0);
// allocate CUDA memory
g->dataMalloc();
// Execute on CUDA
npuRuntime->run(g);
// clone CUDA output to CPU
auto o = op->getOutput();
auto ocpu = o->clone(cpuRuntime);
// check results on CPU
EXPECT_TRUE(ocpu->equalData(vector<float>{
-0.5, 0, 0.5, 1, -2, -1, 0, 1, -0.333333, 0, 0.333333, 0.666667}));
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,65 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/concat.h"
#include "test.h"
namespace infini {
template <class T>
void testConcat(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu1 =
make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu1->dataMalloc();
inputCpu1->setData(generator);
Tensor inputCpu2 =
make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu2->dataMalloc();
inputCpu2->setData(generator);
Tensor inputCpu3 =
make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu3->dataMalloc();
inputCpu3->setData(generator);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu1 = npuGraph->cloneTensor(inputCpu1);
auto inputNpu2 = npuGraph->cloneTensor(inputCpu2);
auto inputNpu3 = npuGraph->cloneTensor(inputCpu3);
auto npuOp = npuGraph->addOp<T>(TensorVec{inputNpu1, inputNpu2, inputNpu3},
nullptr, 2);
npuGraph->dataMalloc();
inputNpu1->setData(generator);
inputNpu2->setData(generator);
inputNpu3->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
inputCpu1->print();
inputCpu1->printData();
inputCpu2->print();
inputCpu2->printData();
inputCpu3->print();
inputCpu3->printData();
outputNpu2Cpu->print();
outputNpu2Cpu->printData();
EXPECT_TRUE(1);
}
TEST(ascend_Concat, run) {
aclInit(nullptr);
testConcat<ConcatObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,60 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/conv.h"
#include "test.h"
namespace infini {
template <class T>
void testConv(const std::function<void(void *, size_t, DataType)> &generatorA,
const std::function<void(void *, size_t, DataType)> &generatorB,
const Shape &shapeA, const Shape &shapeB) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu1 =
make_ref<TensorObj>(shapeA, DataType::Float32, cpuRuntime);
Tensor inputCpu2 =
make_ref<TensorObj>(shapeB, DataType::Float32, cpuRuntime);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu1 = npuGraph->cloneTensor(inputCpu1);
auto inputNpu2 = npuGraph->cloneTensor(inputCpu2);
auto npuOp =
npuGraph->addOp<T>(inputNpu1, inputNpu2, nullptr, 1, 1, 1, 1, 1, 1);
npuGraph->dataMalloc();
inputNpu1->setData(generatorA);
inputNpu2->setData(generatorB);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// CPU
Graph cpuGraph = make_ref<GraphObj>(cpuRuntime);
cpuGraph->addTensor(inputCpu1);
cpuGraph->addTensor(inputCpu2);
auto cpuOp =
cpuGraph->addOp<T>(inputCpu1, inputCpu2, nullptr, 1, 1, 1, 1, 1, 1);
cpuGraph->dataMalloc();
inputCpu1->setData(generatorA);
inputCpu2->setData(generatorB);
cpuRuntime->run(cpuGraph);
auto outputCpu = cpuOp->getOutput();
// Check
// outputCpu->printData();
// outputNpu2Cpu->printData();
EXPECT_TRUE(outputCpu->equalData(outputNpu2Cpu, 1e-3));
}
TEST(ascend_Conv, run) {
aclInit(nullptr);
testConv<ConvObj>(IncrementalGenerator(), IncrementalGenerator(),
Shape{1, 3, 128, 128}, Shape{2, 3, 3, 3});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,58 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/conv.h"
#include "test.h"
namespace infini {
void testConvTransposedAclnn(
const std::function<void(void *, size_t, DataType)> &generator,
std::vector<float> ansVec) {
const auto &[N, C, H, W, F, R, S] = tuple{1, 1, 2, 2, 1, 4, 4};
const int stride = 1, padding = 0, dilation = 1;
// Construct Runtime and graph for CPU and CUDA
Runtime cpu = NativeCpuRuntimeObj::getInstance(); // CPUruntime is singleton
Graph gCpu = make_ref<GraphObj>(cpu);
Runtime npu = make_ref<ASCENDRuntimeObj>();
Graph gNpu = make_ref<GraphObj>(npu);
// Set input data on CPU in a CPU Graph
Tensor i0Cpu = gCpu->addTensor({N, F, H, H}, DataType::Float32);
Tensor w0Cpu = gCpu->addTensor({F, C, R, S}, DataType::Float32);
// Malloc data for all tensors in a graph. Do we need implicit allocation?
gCpu->dataMalloc();
i0Cpu->setData(generator);
w0Cpu->setData(generator);
// Copy input tensors from CPU to CUDA
Tensor i0Npu = gNpu->cloneTensor(i0Cpu);
Tensor w0Npu = gNpu->cloneTensor(w0Cpu);
// Build CUDA graph
auto conv = gNpu->addOp<ConvTransposed2dObj>(i0Npu, w0Npu, nullptr, padding,
padding, stride, stride,
dilation, dilation);
gNpu->dataMalloc();
i0Npu->setData(generator);
w0Npu->setData(generator);
// Execute on CUDA
npu->run(gNpu);
// copy output from CUDA to CPU
auto o0Cpu = gCpu->cloneTensor(conv->getOutput());
// check results on CPU
o0Cpu->printData();
EXPECT_TRUE(o0Cpu->equalData(ansVec));
}
TEST(ascend_ConvTransposed, run) {
aclInit(nullptr);
testConvTransposedAclnn(
IncrementalGenerator(),
std::vector<float>{0., 0., 1., 2., 3., 0., 6., 12., 18.,
16., 8., 30., 36., 42., 32., 16., 54., 60.,
66., 48., 24., 62., 67., 72., 45.});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,69 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/element_wise.h"
#include "test.h"
namespace infini {
template <class T>
void testElementWise(
const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape0, const Shape &shape) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu1 =
make_ref<TensorObj>(shape0, DataType::Float32, cpuRuntime);
Tensor inputCpu2 =
make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu1->dataMalloc();
inputCpu2->dataMalloc();
inputCpu1->setData(generator);
inputCpu2->setData(generator);
inputCpu1->print();
inputCpu1->printData();
inputCpu2->print();
inputCpu2->printData();
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu1 = npuGraph->cloneTensor(inputCpu1);
auto inputNpu2 = npuGraph->cloneTensor(inputCpu2);
auto npuOp = npuGraph->addOp<T>(inputNpu1, inputNpu2, nullptr);
npuGraph->dataMalloc();
inputNpu1->setData(generator);
inputNpu2->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
outputNpu2Cpu->print();
outputNpu2Cpu->printData();
EXPECT_TRUE(1);
}
TEST(ascend_ElementWise, run) {
aclInit(nullptr);
testElementWise<PowObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
Shape{1, 2, 2, 3});
testElementWise<AddObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
Shape{1, 2, 2, 3});
testElementWise<SubObj>(IncrementalGenerator(), Shape{1, 1, 48, 48},
Shape{1, 1, 1, 1});
testElementWise<MaximumObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
Shape{1, 2, 2, 3});
testElementWise<DivObj>(IncrementalGenerator(), Shape{1},
Shape{1, 2, 2, 3});
testElementWise<MulObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
Shape{1, 2, 2, 3});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,42 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/unary.h"
#include "test.h"
namespace infini {
template <class T>
void testErf(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto ascendRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu->dataMalloc();
inputCpu->setData(generator);
// Npu
Graph npuGraph = make_ref<GraphObj>(ascendRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp = npuGraph->addOp<T>(inputNpu, nullptr);
npuGraph->dataMalloc();
ascendRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
inputCpu->printData();
outputNpu2Cpu->printData();
EXPECT_TRUE(1);
}
TEST(ascend_Erf, run) {
aclInit(nullptr);
testErf<ErfObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,71 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/gather.h"
#include "test.h"
namespace infini {
TEST(ascend_Gather, run) {
aclInit(nullptr);
{
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu =
make_ref<TensorObj>(Shape{3, 3}, DataType::Float32, cpuRuntime);
Tensor indexCpu =
make_ref<TensorObj>(Shape{1}, DataType::Int64, cpuRuntime);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto indexNpu = npuGraph->cloneTensor(indexCpu);
auto npuOp = npuGraph->addOp<GatherObj>(inputNpu, indexNpu, nullptr, 1);
npuGraph->dataMalloc();
inputNpu->setData(IncrementalGenerator());
indexNpu->copyin(vector<int64_t>{0});
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
outputNpu2Cpu->print();
outputNpu2Cpu->printData();
// Check
EXPECT_TRUE(outputNpu2Cpu->equalData(vector<float>{0, 3, 6}));
}
{
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu =
make_ref<TensorObj>(Shape{3, 2}, DataType::Float32, cpuRuntime);
Tensor indexCpu =
make_ref<TensorObj>(Shape{2, 2}, DataType::Int64, cpuRuntime);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto indexNpu = npuGraph->cloneTensor(indexCpu);
auto npuOp = npuGraph->addOp<GatherObj>(inputNpu, indexNpu, nullptr, 0);
npuGraph->dataMalloc();
inputNpu->copyin(std::vector<float>{1.0, 1.2, 2.3, 3.4, 4.5, 5.7});
indexNpu->copyin(vector<int64_t>{0, 1, 1, 2});
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
EXPECT_TRUE(outputNpu2Cpu->equalData(
vector<float>{1.0, 1.2, 2.3, 3.4, 2.3, 3.4, 4.5, 5.7}));
}
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,74 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/instance_norm.h"
#include "test.h"
namespace infini {
void test_instancenormFp32(const Shape &inputShape,
const vector<float> &inputData,
const Shape &scaleShape,
const vector<float> &scaleData, float eps,
const vector<float> &ExpectData,
const Shape &biasShape,
const vector<float> &biasData) {
Runtime runtime = NativeCpuRuntimeObj::getInstance();
Graph gCpu = make_ref<GraphObj>(runtime);
auto bias = gCpu->addTensor(biasShape, DataType::Float32);
auto input = gCpu->addTensor(inputShape, DataType::Float32);
auto scale = gCpu->addTensor(scaleShape, DataType::Float32);
auto ExpectOutput = gCpu->addTensor(inputShape, DataType::Float32);
gCpu->dataMalloc();
bias->copyin(biasData); //
// bias->printData();
input->copyin(inputData);
scale->copyin(scaleData); //
ExpectOutput->copyin(ExpectData);
auto ascendRuntime = make_ref<ASCENDRuntimeObj>();
Graph gAscend = make_ref<GraphObj>(ascendRuntime);
auto biasNpu = gAscend->cloneTensor(bias);
auto inputNpu = gAscend->cloneTensor(input);
auto scaleNpu = gAscend->cloneTensor(scale);
// gCpu->cloneTensor(biasNpu)->printData();
auto op =
gAscend->addOp<InstanceNormObj>(inputNpu, nullptr, scaleNpu, biasNpu,
eps); // InstancenormObj
gAscend->dataMalloc();
biasNpu->copyin(biasData);
// gCpu->cloneTensor(biasNpu)->printData();
inputNpu->copyin(inputData);
scaleNpu->copyin(scaleData);
ascendRuntime->run(gAscend);
auto oCpu = gCpu->cloneTensor(op->getOutput()); // move Data from npu to cpu
oCpu->printData(); //->printData
EXPECT_TRUE(oCpu->equalData(ExpectOutput, 1e-4));
}
TEST(CUDA_InstancenormFp32, run) {
aclInit(nullptr);
test_instancenormFp32(
Shape{2, 3, 2, 3},
vector<float>{0., 1., 2., 3., 4., 5., 6., 7., 8.,
9., 10., 11., 12., 13., 14., 15., 16., 17.,
18., 19., 20., 21., 22., 23., 24., 25., 26.,
27., 28., 29., 30., 31., 32., 33., 34., 35.},
Shape{3}, vector<float>{0.3, 0.2, 0.5}, 1e-5,
vector<float>{
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678},
Shape{3}, vector<float>{0, 0, 0});
aclFinalize();
} // python output
} // namespace infini

View File

@ -0,0 +1,143 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/layer_norm.h"
#include "test.h"
namespace infini {
void test_layernormFp32(
const Shape &inputShape, const vector<float> &inputData,
const Shape &scaleShape, const vector<float> &scaleData, float eps,
int axis, int stash_type, const vector<float> &ExpectData,
const std::optional<Shape> &bShape = std::nullopt,
const std::optional<std::vector<float>> &biasData = std::nullopt) {
Runtime runtime = NativeCpuRuntimeObj::getInstance();
Graph gCpu = make_ref<GraphObj>(runtime);
if (bShape.has_value() && biasData.has_value()) {
Shape biasShape = *bShape;
auto bias = gCpu->addTensor(biasShape, DataType::Float32);
auto input = gCpu->addTensor(inputShape, DataType::Float32);
auto scale = gCpu->addTensor(scaleShape, DataType::Float32);
auto ExpectOutput = gCpu->addTensor(inputShape, DataType::Float32);
gCpu->dataMalloc();
bias->copyin(*biasData);
// bias->printData();
input->copyin(inputData);
scale->copyin(scaleData);
ExpectOutput->copyin(ExpectData);
auto ascendRuntime = make_ref<ASCENDRuntimeObj>();
Graph gAscend = make_ref<GraphObj>(ascendRuntime);
auto biasNpu = gAscend->cloneTensor(bias);
auto inputNpu = gAscend->cloneTensor(input);
auto scaleNpu = gAscend->cloneTensor(scale);
auto op =
gAscend->addOp<LayerNormObj>(inputNpu, scaleNpu, nullptr, biasNpu,
eps, axis, stash_type); // LayernormObj
gAscend->dataMalloc();
biasNpu->copyin(*biasData);
// gCpu->cloneTensor(biasNpu)->printData();
inputNpu->copyin(inputData);
scaleNpu->copyin(scaleData);
ascendRuntime->run(gAscend);
auto oCpu =
gCpu->cloneTensor(op->getOutput()); // move Data from npu to cpu
oCpu->printData(); //->printData
EXPECT_TRUE(oCpu->equalData(ExpectOutput, 1e-4));
} else {
auto input = gCpu->addTensor(inputShape, DataType::Float32);
auto scale = gCpu->addTensor(scaleShape, DataType::Float32);
auto ExpectOutput = gCpu->addTensor(inputShape, DataType::Float32);
gCpu->dataMalloc();
input->copyin(inputData);
scale->copyin(scaleData); //
ExpectOutput->copyin(ExpectData);
auto ascendRuntime = make_ref<ASCENDRuntimeObj>();
Graph gAscend = make_ref<GraphObj>(ascendRuntime);
auto inputNpu = gAscend->cloneTensor(input);
auto scaleNpu = gAscend->cloneTensor(scale);
auto op =
gAscend->addOp<LayerNormObj>(inputNpu, scaleNpu, nullptr, nullptr,
eps, axis, stash_type); // LayernormObj
gAscend->dataMalloc();
inputNpu->copyin(inputData);
scaleNpu->copyin(scaleData);
ascendRuntime->run(gAscend);
auto oCpu =
gCpu->cloneTensor(op->getOutput()); // move Data from npu to cpu
oCpu->printData(); //->printData
EXPECT_TRUE(oCpu->equalData(ExpectOutput, 1e-4));
// EXPECT_TRUE(oCpu->equalData(ExpectData));
}
}
TEST(CUDA_LayernormFp32, run) {
aclInit(nullptr);
test_layernormFp32(
Shape{2, 3, 2, 3},
vector<float>{0., 1., 2., 3., 4., 5., 6., 7., 8.,
9., 10., 11., 12., 13., 14., 15., 16., 17.,
18., 19., 20., 21., 22., 23., 24., 25., 26.,
27., 28., 29., 30., 31., 32., 33., 34., 35.},
Shape{3}, vector<float>{0.3, 0.2, 0.5}, 1e-5, 3, 1,
vector<float>{
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207, 0.0000000, 0.6123678},
Shape{3}, vector<float>{0, 0, 0});
test_layernormFp32(
Shape{2, 3, 2, 3},
vector<float>{0., 1., 2., 3., 4., 5., 6., 7., 8.,
9., 10., 11., 12., 13., 14., 15., 16., 17.,
18., 19., 20., 21., 22., 23., 24., 25., 26.,
27., 28., 29., 30., 31., 32., 33., 34., 35.},
Shape{3}, vector<float>{0.3, 0.2, 0.5}, 1e-5, 3, 1,
vector<float>{
-0.0674207, 0.2000000, 1.1123679, -0.0674207, 0.2000000, 1.1123679,
-0.0674207, 0.2000000, 1.1123679, -0.0674207, 0.2000000, 1.1123679,
-0.0674207, 0.2000000, 1.1123679, -0.0674207, 0.2000000, 1.1123679,
-0.0674207, 0.2000000, 1.1123679, -0.0674207, 0.2000000, 1.1123679,
-0.0674207, 0.2000000, 1.1123679, -0.0674207, 0.2000000, 1.1123679,
-0.0674207, 0.2000000, 1.1123679, -0.0674207, 0.2000000, 1.1123679},
Shape{3}, vector<float>{0.3, 0.2, 0.5});
test_layernormFp32(
Shape{2, 3, 2, 3},
vector<float>{0., 1., 2., 3., 4., 5., 6., 7., 8.,
9., 10., 11., 12., 13., 14., 15., 16., 17.,
18., 19., 20., 21., 22., 23., 24., 25., 26.,
27., 28., 29., 30., 31., 32., 33., 34., 35.},
Shape{3}, vector<float>{0.3, 0.2, 0.5}, 1e-5, 3, 1,
vector<float>{-0.3674207, 0.0000000, 0.6123678, -0.3674207,
0.0000000, 0.6123678, -0.3674207, 0.0000000,
0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207,
0.0000000, 0.6123678, -0.3674207, 0.0000000,
0.6123678, -0.3674207, 0.0000000, 0.6123678,
-0.3674207, 0.0000000, 0.6123678, -0.3674207,
0.0000000, 0.6123678, -0.3674207, 0.0000000,
0.6123678, -0.3674207, 0.0000000, 0.6123678});
aclFinalize();
} // python output
} // namespace infini

View File

@ -0,0 +1,59 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/matmul.h"
#include "test.h"
namespace infini {
template <class T>
void testMatmul(const std::function<void(void *, size_t, DataType)> &generatorA,
const std::function<void(void *, size_t, DataType)> &generatorB,
bool transA, bool transB, const Shape &shapeA,
const Shape &shapeB) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu1 =
make_ref<TensorObj>(shapeA, DataType::Float32, cpuRuntime);
Tensor inputCpu2 =
make_ref<TensorObj>(shapeB, DataType::Float32, cpuRuntime);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu1 = npuGraph->cloneTensor(inputCpu1);
auto inputNpu2 = npuGraph->cloneTensor(inputCpu2);
auto npuOp = npuGraph->addOp<T>(inputNpu1, inputNpu2, nullptr);
npuGraph->dataMalloc();
inputNpu1->setData(generatorA);
inputNpu2->setData(generatorB);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// CPU
Graph cpuGraph = make_ref<GraphObj>(cpuRuntime);
auto cpuOp = cpuGraph->addOp<T>(inputCpu1, inputCpu2, nullptr);
cpuGraph->addTensor(inputCpu1);
cpuGraph->addTensor(inputCpu2);
cpuGraph->dataMalloc();
inputCpu1->setData(generatorA);
inputCpu2->setData(generatorB);
cpuRuntime->run(cpuGraph);
auto outputCpu = cpuOp->getOutput();
// Check
EXPECT_TRUE(outputCpu->equalData(outputNpu2Cpu));
}
TEST(ascend_Matmul, run) {
aclInit(nullptr);
testMatmul<MatmulObj>(IncrementalGenerator(), IncrementalGenerator(), false,
false, Shape{1, 2, 3}, Shape{1, 3, 4});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,49 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/pad.h"
#include "test.h"
namespace infini {
template <class T>
void testPad(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu->dataMalloc();
inputCpu->setData(generator);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp = npuGraph->addOp<T>(inputNpu, nullptr, vector<int>{1, 1, 1, 1},
vector<int>{0, 3});
npuGraph->dataMalloc();
inputNpu->setData(generator);
std::cout << npuOp->toString() << std::endl;
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
inputCpu->print();
inputCpu->printData();
outputNpu2Cpu->print();
outputNpu2Cpu->printData();
EXPECT_TRUE(1);
}
TEST(ascend_Pad, run) {
aclInit(nullptr);
testPad<PadObj>(IncrementalGenerator(), Shape{1, 1, 2, 3});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,48 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/pooling.h"
#include "test.h"
namespace infini {
template <class T, typename std::enable_if<std::is_base_of<PoolingObj, T>{},
int>::type = 0>
void testPooling(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu->dataMalloc();
inputCpu->setData(generator);
// GPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp =
npuGraph->addOp<T>(inputNpu, nullptr, 3, 3, 1, 1, 1, 1, 2, 2, 0);
// npuGraph->addOp<T>(inputNpu, nullptr, 2, 2, 1, 1, 0, 0, 1, 1, 0);
npuGraph->dataMalloc();
inputNpu->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
inputCpu->printData();
outputNpu2Cpu->printData();
EXPECT_TRUE(1);
}
TEST(cnnl_Pooling, run) {
aclInit(nullptr);
testPooling<MaxPoolObj>(IncrementalGenerator(), Shape{1, 3, 5, 5});
testPooling<AvgPoolObj>(IncrementalGenerator(), Shape{1, 2, 5, 5});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,84 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/reduce.h"
#include "test.h"
namespace infini {
template <typename ReduceObjT>
void test_reduce(const Shape &shape, const vector<float> &data,
const optional<const vector<int>> &axes, bool keepDims,
const vector<float> &ExpectData) {
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
// Build NPU graph
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto op = npuGraph->addOp<ReduceObjT>(inputNpu, nullptr, axes, keepDims);
// allocate NPU memory
npuGraph->dataMalloc();
inputNpu->copyin(data);
// Execute on NPU
npuRuntime->run(npuGraph);
// clone NPU output to CPU
auto outputNpu = op->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// check results on CPU
EXPECT_TRUE(outputNpu2Cpu->equalData(ExpectData));
}
TEST(ascend_ReduceMean, run) {
aclInit(nullptr);
test_reduce<ReduceMeanObj>(
Shape{3, 2, 2}, vector<float>{5, 1, 20, 2, 30, 1, 40, 2, 55, 1, 60, 2},
std::nullopt, true, vector<float>{18.25});
test_reduce<ReduceMeanObj>(
Shape{1, 3, 2, 2, 1},
vector<float>{5, 1, 20, 2, 30, 1, 40, 2, 55, 1, 60, 2}, std::nullopt,
false, vector<float>{18.25});
test_reduce<ReduceMeanObj>(
Shape{2, 3, 2, 2},
vector<float>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23},
vector<int>{1, 2}, false, vector<float>{5, 6, 17, 18});
test_reduce<ReduceMeanObj>(
Shape{2, 3, 2, 2, 1},
vector<float>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23},
vector<int>{1, 2}, true, vector<float>{5, 6, 17, 18});
aclFinalize();
}
TEST(ascend_ReduceSum, run) {
test_reduce<ReduceSumObj>(Shape{3, 2, 2},
vector<float>{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
std::nullopt, true, vector<float>{12});
test_reduce<ReduceSumObj>(Shape{1, 3, 2, 2, 1},
vector<float>{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
std::nullopt, false, vector<float>{12});
test_reduce<ReduceSumObj>(
Shape{2, 3, 2, 2},
vector<float>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23},
vector<int>{1, 2}, false, vector<float>{30, 36, 102, 108});
test_reduce<ReduceSumObj>(
Shape{2, 3, 2, 2, 1},
vector<float>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23},
vector<int>{1, 2}, true, vector<float>{30, 36, 102, 108});
}
} // namespace infini

View File

@ -0,0 +1,84 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/reshape.h"
#include "operators/squeeze.h"
#include "operators/unsqueeze.h"
#include "test.h"
namespace infini {
template <class T>
void testReshape(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape, const Shape &outputShape) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu->dataMalloc();
inputCpu->setData(generator);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp = npuGraph->addOp<T>(inputNpu, nullptr, outputShape);
npuGraph->dataMalloc();
inputNpu->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
inputCpu->print();
inputCpu->printData();
outputNpu2Cpu->print();
outputNpu2Cpu->printData();
EXPECT_TRUE(inputCpu->equalData(outputNpu2Cpu, 1e-3));
}
void testFlatten(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape, int axis) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu->dataMalloc();
inputCpu->setData(generator);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp = npuGraph->addOp<FlattenObj>(inputNpu, nullptr, axis);
npuGraph->dataMalloc();
inputNpu->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
inputCpu->print();
inputCpu->printData();
outputNpu2Cpu->print();
outputNpu2Cpu->printData();
EXPECT_TRUE(inputCpu->equalData(outputNpu2Cpu, 1e-3));
}
TEST(ascend_Unary, run) {
aclInit(nullptr);
testReshape<ReshapeObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
Shape{1, 2, 6});
testReshape<SqueezeObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
Shape{0});
testReshape<UnsqueezeObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
Shape{4});
testFlatten(IncrementalGenerator(), Shape{1, 2, 2, 3}, 2);
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,69 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/resize.h"
#include "test.h"
namespace infini {
TEST(Resize, Ascend_downsample_scales_nearest) {
aclInit(nullptr);
{
Runtime runtime = NativeCpuRuntimeObj::getInstance();
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 2, 4}, DataType::Float32);
auto scales = gCpu->addTensor({4}, DataType::Float32);
gCpu->dataMalloc();
input->copyin(vector<float>{1, 2, 3, 4, 5, 6, 7, 8});
scales->copyin(vector<float>{1, 1, 0.6, 0.6});
auto ascendRuntime = make_ref<ASCENDRuntimeObj>();
Graph gNpu = make_ref<GraphObj>(ascendRuntime);
auto inputNpu = gNpu->cloneTensor(input);
auto scalesNpu = gNpu->cloneTensor(scales);
auto op = gNpu->addOp<ResizeObj>(inputNpu, nullptr, std::nullopt,
nullptr, scalesNpu, nullptr);
gNpu->dataMalloc();
inputNpu->copyin(vector<float>{1, 2, 3, 4, 5, 6, 7, 8});
scalesNpu->copyin(vector<float>{1, 1, 0.6, 0.6});
ascendRuntime->run(gNpu);
// copy output from NPU to CPU
auto oCpu = gCpu->cloneTensor(op->getOutput(0));
EXPECT_TRUE(oCpu->equalData(vector<float>{1, 3}));
}
{
Runtime runtime = NativeCpuRuntimeObj::getInstance();
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 2, 2}, DataType::Float32);
auto scales = gCpu->addTensor({4}, DataType::Float32);
gCpu->dataMalloc();
input->copyin(vector<float>{1, 2, 3, 4});
scales->copyin(vector<float>{1, 1, 2, 3});
auto ascendRuntime = make_ref<ASCENDRuntimeObj>();
Graph gNpu = make_ref<GraphObj>(ascendRuntime);
auto inputNpu = gNpu->cloneTensor(input);
auto scalesNpu = gNpu->cloneTensor(scales);
auto op = gNpu->addOp<ResizeObj>(inputNpu, nullptr, std::nullopt,
nullptr, scalesNpu, nullptr);
gNpu->dataMalloc();
inputNpu->copyin(vector<float>{1, 2, 3, 4});
scalesNpu->copyin(vector<float>{1, 1, 2, 3});
ascendRuntime->run(gNpu);
// copy output from NPU to CPU
auto oCpu = gCpu->cloneTensor(op->getOutput(0));
EXPECT_TRUE(
oCpu->equalData(vector<float>{1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2,
3, 3, 3, 4, 4, 4, 3, 3, 3, 4, 4, 4}));
}
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,75 @@
#ifdef INFINI_USE_HCCL
#include "ascend/ascend_runtime.h"
#include "ascend/hccl_communicator.h"
#include "core/graph.h"
#include "core/runtime.h"
#include "operators/recv.h"
#include "operators/send.h"
#include "test.h"
#include <thread>
namespace infini {
void sendrecv(const string taskName, int deviceID, vector<float> data,
const Shape &dataShape, int WORLD_SIZE, int source,
int destination) {
// Create Runtimes and initiate communication
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
Runtime ascnedRuntime = make_ref<ASCENDRuntimeObj>(deviceID);
ascnedRuntime->initComm(taskName, WORLD_SIZE, deviceID);
if (deviceID == source) {
Graph gSend = make_ref<GraphObj>(ascnedRuntime);
auto input = gSend->addTensor(Shape{static_cast<int>(data.size())},
DataType::Float32);
auto opSend =
gSend->addOp<SendObj>(input, source, destination, nullptr);
// Copy data from CPU to GPU
gSend->dataMalloc();
input->copyin(data);
ascnedRuntime->run(gSend);
}
// ----------------
if (deviceID == destination) {
Graph gRecv = make_ref<GraphObj>(ascnedRuntime);
int outputType = 1;
// auto input =
// gRecv->addTensor(Shape{static_cast<int>(data.size())},DataType::Float32);
auto opRecv = gRecv->addOp<RecvObj>(nullptr, source, destination,
dataShape, outputType, nullptr);
gRecv->dataMalloc();
ascnedRuntime->run(gRecv);
auto result = opRecv->getOutput()->clone(cpuRuntime);
EXPECT_TRUE(result->equalData(data));
}
}
TEST(ASCEND_SendRecv1, run) {
// Only 1 device gets data. Every rank should have the same data after
// sendrecv.
aclInit(nullptr);
{
vector<float> data = {2., 3., 5., 6.};
int WORLD_SIZE = 4;
int source = 0;
int destination = 2;
std::vector<std::thread> threads;
for (int gpu = 0; gpu < WORLD_SIZE; ++gpu) {
threads.emplace_back(sendrecv, "test_sendrecv", gpu, data,
Shape{2, 2}, WORLD_SIZE, source, destination);
}
for (auto &thread : threads) {
thread.join();
}
}
aclFinalize();
}
} // namespace infini
#endif

View File

@ -0,0 +1,41 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/slice.h"
#include "test.h"
namespace infini {
TEST(ascend_Unary, run) {
aclInit(nullptr);
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu =
make_ref<TensorObj>(Shape{3, 2, 1, 5}, DataType::Float32, cpuRuntime);
// inputCpu->dataMalloc();
// inputCpu->setData(IncrementalGenerator());
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp = npuGraph->addOp<SliceObj>(inputNpu, nullptr, vector<int>{1, 1},
vector<int>{2, 5}, vector<int>{0, 3},
std::nullopt);
npuGraph->dataMalloc();
inputNpu->setData(IncrementalGenerator());
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
EXPECT_TRUE(outputNpu2Cpu->equalData(
vector<float>{11, 12, 13, 14, 16, 17, 18, 19}));
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,61 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/softmax.h"
#include "test.h"
namespace infini {
template <class T>
void testSoftmax(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape, int axis, vector<float> Out) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu1 =
make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu1->dataMalloc();
// inputCpu1->setData(generator);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu1 = npuGraph->cloneTensor(inputCpu1);
auto npuOp = npuGraph->addOp<T>(inputNpu1, nullptr, axis);
npuGraph->dataMalloc();
inputNpu1->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
EXPECT_TRUE(outputNpu2Cpu->equalData(Out));
}
TEST(ascend_ElementWise, run) {
aclInit(nullptr);
testSoftmax<SoftmaxObj>(
IncrementalGenerator(), Shape{2, 2, 2, 2}, 1,
vector<float>{0.0179862, 0.0179862, 0.0179862, 0.0179862, 0.9820138,
0.9820138, 0.9820138, 0.9820138, 0.0179862, 0.0179862,
0.0179862, 0.0179862, 0.9820138, 0.9820138, 0.9820138,
0.9820138});
testSoftmax<SoftmaxObj>(
IncrementalGenerator(), Shape{2, 2, 2, 2}, 2,
vector<float>{0.1192029, 0.1192029, 0.8807971, 0.8807971, 0.1192029,
0.1192029, 0.8807971, 0.8807971, 0.1192029, 0.1192029,
0.8807971, 0.8807971, 0.1192029, 0.1192029, 0.8807971,
0.8807971});
testSoftmax<SoftmaxObj>(
IncrementalGenerator(), Shape{2, 2, 2, 2}, 3,
vector<float>{0.2689414, 0.7310586, 0.2689414, 0.7310586, 0.2689414,
0.7310586, 0.2689414, 0.7310586, 0.2689414, 0.7310586,
0.2689414, 0.7310586, 0.2689414, 0.7310586, 0.2689414,
0.7310586});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,50 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/split.h"
#include "test.h"
namespace infini {
template <class T>
void testSplit(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu->dataMalloc();
inputCpu->setData(generator);
// GPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto gpuOp = npuGraph->addOp<T>(inputNpu, std::nullopt, 3, 3);
npuGraph->dataMalloc();
inputNpu->setData(generator);
npuRuntime->run(npuGraph);
auto o0Cpu = gpuOp->getOutput(0)->clone(cpuRuntime);
auto o1Cpu = gpuOp->getOutput(1)->clone(cpuRuntime);
auto o2Cpu = gpuOp->getOutput(2)->clone(cpuRuntime);
// Check
inputCpu->print();
inputCpu->printData();
o0Cpu->print();
o0Cpu->printData();
o1Cpu->print();
o1Cpu->printData();
o2Cpu->print();
o2Cpu->printData();
EXPECT_TRUE(1);
}
TEST(ascend_Split, run) {
aclInit(nullptr);
testSplit<SplitObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,49 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/transpose.h"
#include "test.h"
namespace infini {
template <class T>
void testTranspose(
const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape, const Shape &permute) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
inputCpu->dataMalloc();
inputCpu->setData(generator);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp = npuGraph->addOp<T>(inputNpu, nullptr, permute);
npuGraph->dataMalloc();
inputNpu->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
inputCpu->print();
inputCpu->printData();
outputNpu2Cpu->print();
outputNpu2Cpu->printData();
EXPECT_TRUE(1);
}
TEST(ascend_Unary, run) {
aclInit(nullptr);
testTranspose<TransposeObj>(IncrementalGenerator(), Shape{1, 1, 2, 3},
vector<int>{0, 1, 3, 2});
aclFinalize();
}
} // namespace infini

View File

@ -0,0 +1,151 @@
#include "ascend/ascend_runtime.h"
#include "core/graph.h"
#include "core/kernel.h"
#include "core/runtime.h"
#include "operators/unary.h"
#include "test.h"
namespace infini {
template <class T>
void testUnary(const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp = npuGraph->addOp<T>(inputNpu, nullptr);
npuGraph->dataMalloc();
inputNpu->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// CPU
Graph cpuGraph = make_ref<GraphObj>(cpuRuntime);
auto cpuOp = cpuGraph->addOp<T>(inputCpu, nullptr);
cpuGraph->addTensor(inputCpu);
cpuGraph->dataMalloc();
inputCpu->setData(generator);
cpuRuntime->run(cpuGraph);
auto outputCpu = cpuOp->getOutput();
// Check
EXPECT_TRUE(outputCpu->equalData(outputNpu2Cpu, 1e-3));
}
template <class T>
void testUnarywithExpectData(
const std::function<void(void *, size_t, DataType)> &generator,
const Shape &shape, const vector<float> &ExpectData) {
// Runtime
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
Graph gCpu = make_ref<GraphObj>(cpuRuntime);
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
// Build input data on CPU
Tensor inputCpu = make_ref<TensorObj>(shape, DataType::Float32, cpuRuntime);
auto ExpectOutput = gCpu->addTensor(shape, DataType::Float32);
gCpu->dataMalloc();
ExpectOutput->copyin(ExpectData);
// NPU
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
auto inputNpu = npuGraph->cloneTensor(inputCpu);
auto npuOp = npuGraph->addOp<T>(inputNpu, nullptr);
npuGraph->dataMalloc();
inputNpu->setData(generator);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// CPU
// Graph cpuGraph = make_ref<GraphObj>(cpuRuntime);
// auto cpuOp = cpuGraph->addOp<T>(inputCpu, nullptr);
// cpuGraph->addTensor(inputCpu);
// cpuGraph->dataMalloc();
// inputCpu->setData(generator);
// cpuRuntime->run(cpuGraph);
// auto outputCpu = cpuOp->getOutput();
// Check
outputNpu2Cpu->printData();
EXPECT_TRUE(outputNpu2Cpu->equalData(ExpectOutput, 1e-4));
}
void testLeakyRelu(const Shape &shape, const vector<float> &inputData,
const vector<float> &ExpectData, float alpha) {
Runtime cpuRuntime = NativeCpuRuntimeObj::getInstance();
Runtime runtime = NativeCpuRuntimeObj::getInstance();
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor(shape, DataType::Float32);
gCpu->dataMalloc();
input->copyin(inputData);
auto npuRuntime = make_ref<ASCENDRuntimeObj>();
Graph npuGraph = make_ref<GraphObj>(npuRuntime);
// NPU
auto inputNpu = npuGraph->cloneTensor(input);
auto npuOp = npuGraph->addOp<LeakyReluObj>(inputNpu, nullptr, alpha);
npuGraph->dataMalloc();
inputNpu->copyin(inputData);
npuRuntime->run(npuGraph);
auto outputNpu = npuOp->getOutput();
auto outputNpu2Cpu = outputNpu->clone(cpuRuntime);
// Check
EXPECT_TRUE(outputNpu2Cpu->equalData(ExpectData));
}
TEST(ascend_Unary, run) {
aclInit(nullptr);
testLeakyRelu(Shape{1, 2, 2, 3},
vector<float>{-6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6},
vector<float>{-0.0600, -0.0500, -0.0400, -0.0300, -0.0200,
-0.0100, 1.0000, 2.0000, 3.0000, 4.0000, 5.0000,
6.0000},
0.01);
testUnary<ReluObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<AbsObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<SigmoidObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<HardSwishObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<TanhObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<SinObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<GeluObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<CosObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<ACosObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<ATanObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<NegObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnary<SqrtObj>(IncrementalGenerator(), Shape{1, 2, 2, 3});
testUnarywithExpectData<CeilObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
vector<float>{0.0, 1.0, 2.0, 3.0, 4.0, 5.0,
6.0, 7.0, 8.0, 9.0, 10.0,
11.0});
testUnarywithExpectData<FloorObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
vector<float>{0.0, 1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0, 9.0,
10.0, 11.0});
testUnarywithExpectData<ExpObj>(
IncrementalGenerator(), Shape{1, 2, 2, 3},
vector<float>{1.0, 2.71828, 7.38906, 20.0855, 54.5981, 148.413, 403.429,
1096.63, 2980.96, 8103.08, 22026.5, 59874.1});
testUnarywithExpectData<ReciprocalObj>(
IncrementalGenerator(), Shape{1, 2, 2, 3},
vector<float>{std::numeric_limits<float>::infinity(), 1, 0.5, 0.333333,
0.25, 0.2, 0.166667, 0.142857, 0.125, 0.111111, 0.1,
0.0909091});
testUnarywithExpectData<RoundObj>(IncrementalGenerator(), Shape{1, 2, 2, 3},
vector<float>{0.0, 1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0, 9.0,
10.0, 11.0});
aclFinalize();
}
} // namespace infini

View File

@ -11,10 +11,10 @@ TEST(Resize, Cuda_downsample_sizes_nearest) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 2, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({4}, DataType::UInt32);
auto sizes = gCpu->addTensor({4}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(vector<float>{1, 2, 3, 4, 5, 6, 7, 8});
sizes->copyin(vector<uint32_t>{1, 1, 1, 3});
sizes->copyin(vector<int64_t>{1, 1, 1, 3});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -26,7 +26,7 @@ TEST(Resize, Cuda_downsample_sizes_nearest) {
ResizeObj::EKeepAspectRatioPolicy::stretch);
gCuda->dataMalloc();
inputCuda->copyin(vector<float>{1, 2, 3, 4, 5, 6, 7, 8});
sizesCuda->copyin(vector<uint32_t>{1, 1, 1, 3});
sizesCuda->copyin(vector<int64_t>{1, 1, 1, 3});
cudaRuntime->run(gCuda);
@ -40,10 +40,10 @@ TEST(Resize, Cuda_upsample_sizes_nearest_notlarger) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 2, 2}, DataType::Float32);
auto sizes = gCpu->addTensor({2}, DataType::UInt32);
auto sizes = gCpu->addTensor({2}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(vector<float>{1, 2, 3, 4});
sizes->copyin(vector<uint32_t>{7, 8});
sizes->copyin(vector<int64_t>{7, 8});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -57,7 +57,7 @@ TEST(Resize, Cuda_upsample_sizes_nearest_notlarger) {
ResizeObj::ECoordinateTransMode::halfPixel);
gCuda->dataMalloc();
inputCuda->copyin(vector<float>{1, 2, 3, 4});
sizesCuda->copyin(vector<uint32_t>{7, 8});
sizesCuda->copyin(vector<int64_t>{7, 8});
cudaRuntime->run(gCuda);
// copy output from CUDA to CPU
@ -73,10 +73,10 @@ TEST(Resize, Cuda_upsample_sizes_nearest_notsmaller) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 2, 2}, DataType::Float32);
auto sizes = gCpu->addTensor({2}, DataType::UInt32);
auto sizes = gCpu->addTensor({2}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(vector<float>{1, 2, 3, 4});
sizes->copyin(vector<uint32_t>{7, 8});
sizes->copyin(vector<int64_t>{7, 8});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -90,7 +90,7 @@ TEST(Resize, Cuda_upsample_sizes_nearest_notsmaller) {
ResizeObj::ECoordinateTransMode::halfPixel);
gCuda->dataMalloc();
inputCuda->copyin(vector<float>{1, 2, 3, 4});
sizesCuda->copyin(vector<uint32_t>{7, 8});
sizesCuda->copyin(vector<int64_t>{7, 8});
cudaRuntime->run(gCuda);
// copy output from CUDA to CPU
@ -106,11 +106,11 @@ TEST(Resize, Cuda_upsample_sizes_nearest_ceil_half_pixel) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 4, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({4}, DataType::UInt32);
auto sizes = gCpu->addTensor({4}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizes->copyin(vector<uint32_t>{1, 1, 8, 8});
sizes->copyin(vector<int64_t>{1, 1, 8, 8});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -125,7 +125,7 @@ TEST(Resize, Cuda_upsample_sizes_nearest_ceil_half_pixel) {
gCuda->dataMalloc();
inputCuda->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizesCuda->copyin(vector<uint32_t>{1, 1, 8, 8});
sizesCuda->copyin(vector<int64_t>{1, 1, 8, 8});
cudaRuntime->run(gCuda);
// copy output from CUDA to CPU
@ -143,11 +143,11 @@ TEST(Resize, Cuda_upsample_sizes_nearest_floor_align_corners) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 4, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({2}, DataType::UInt32);
auto sizes = gCpu->addTensor({2}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizes->copyin(vector<uint32_t>{8, 8});
sizes->copyin(vector<int64_t>{8, 8});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -162,7 +162,7 @@ TEST(Resize, Cuda_upsample_sizes_nearest_floor_align_corners) {
gCuda->dataMalloc();
inputCuda->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizesCuda->copyin(vector<uint32_t>{8, 8});
sizesCuda->copyin(vector<int64_t>{8, 8});
cudaRuntime->run(gCuda);
// copy output from CUDA to CPU
@ -180,11 +180,11 @@ TEST(Resize, Cuda_upsample_sizes_nearest_round_prefer_ceil_asymmetri) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 4, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({4}, DataType::UInt32);
auto sizes = gCpu->addTensor({4}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizes->copyin(vector<uint32_t>{1, 1, 8, 8});
sizes->copyin(vector<int64_t>{1, 1, 8, 8});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -199,7 +199,7 @@ TEST(Resize, Cuda_upsample_sizes_nearest_round_prefer_ceil_asymmetri) {
gCuda->dataMalloc();
inputCuda->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizesCuda->copyin(vector<uint32_t>{1, 1, 8, 8});
sizesCuda->copyin(vector<int64_t>{1, 1, 8, 8});
cudaRuntime->run(gCuda);
@ -421,11 +421,11 @@ TEST(Resize, Cuda_downsample_sizes_linear_pytorchhalfpixel) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 4, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({4}, DataType::UInt32);
auto sizes = gCpu->addTensor({4}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizes->copyin(vector<uint32_t>{1, 1, 3, 1});
sizes->copyin(vector<int64_t>{1, 1, 3, 1});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -440,7 +440,7 @@ TEST(Resize, Cuda_downsample_sizes_linear_pytorchhalfpixel) {
gCuda->dataMalloc();
inputCuda->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizesCuda->copyin(vector<uint32_t>{1, 1, 3, 1});
sizesCuda->copyin(vector<int64_t>{1, 1, 3, 1});
cudaRuntime->run(gCuda);
// copy output from CUDA to CPU
@ -453,12 +453,12 @@ TEST(Resize, Cuda_tf_crop_and_resize) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 4, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({4}, DataType::UInt32);
auto sizes = gCpu->addTensor({4}, DataType::Int64);
auto roi = gCpu->addTensor({8}, DataType::Float32);
gCpu->dataMalloc();
input->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizes->copyin(vector<uint32_t>{1, 1, 3, 3});
sizes->copyin(vector<int64_t>{1, 1, 3, 3});
roi->copyin(vector<float>{0, 0, 0.4, 0.6, 1, 1, 0.6, 0.8});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
@ -475,7 +475,7 @@ TEST(Resize, Cuda_tf_crop_and_resize) {
gCuda->dataMalloc();
inputCuda->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizesCuda->copyin(vector<uint32_t>{1, 1, 3, 3});
sizesCuda->copyin(vector<int64_t>{1, 1, 3, 3});
roiCuda->copyin(vector<float>{0, 0, 0.4, 0.6, 1, 1, 0.6, 0.8});
cudaRuntime->run(gCuda);
@ -490,12 +490,12 @@ TEST(Resize, Cuda_tf_crop_and_resize_axes_3_2) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 4, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({2}, DataType::UInt32);
auto sizes = gCpu->addTensor({2}, DataType::Int64);
auto roi = gCpu->addTensor({4}, DataType::Float32);
gCpu->dataMalloc();
input->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizes->copyin(vector<uint32_t>{3, 3});
sizes->copyin(vector<int64_t>{3, 3});
roi->copyin(vector<float>{0.6, 0.4, 0.8, 0.6});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
@ -512,7 +512,7 @@ TEST(Resize, Cuda_tf_crop_and_resize_axes_3_2) {
gCuda->dataMalloc();
inputCuda->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizesCuda->copyin(vector<uint32_t>{3, 3});
sizesCuda->copyin(vector<int64_t>{3, 3});
roiCuda->copyin(vector<float>{0.6, 0.4, 0.8, 0.6});
cudaRuntime->run(gCuda);
@ -715,11 +715,11 @@ TEST(Resize, Cuda_downsample_sizes_cubic) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 4, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({4}, DataType::UInt32);
auto sizes = gCpu->addTensor({4}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizes->copyin(vector<uint32_t>{1, 1, 3, 3});
sizes->copyin(vector<int64_t>{1, 1, 3, 3});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -733,7 +733,7 @@ TEST(Resize, Cuda_downsample_sizes_cubic) {
gCuda->dataMalloc();
inputCuda->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizesCuda->copyin(vector<uint32_t>{1, 1, 3, 3});
sizesCuda->copyin(vector<int64_t>{1, 1, 3, 3});
cudaRuntime->run(gCuda);
// copy output from CUDA to CPU
@ -753,11 +753,11 @@ TEST(Resize, Cuda_upsample_sizes_cubic) {
Graph gCpu = make_ref<GraphObj>(runtime);
auto input = gCpu->addTensor({1, 1, 4, 4}, DataType::Float32);
auto sizes = gCpu->addTensor({4}, DataType::UInt32);
auto sizes = gCpu->addTensor({4}, DataType::Int64);
gCpu->dataMalloc();
input->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizes->copyin(vector<uint32_t>{1, 1, 9, 10});
sizes->copyin(vector<int64_t>{1, 1, 9, 10});
auto cudaRuntime = make_ref<CudaRuntimeObj>();
Graph gCuda = make_ref<GraphObj>(cudaRuntime);
@ -771,7 +771,7 @@ TEST(Resize, Cuda_upsample_sizes_cubic) {
gCuda->dataMalloc();
inputCuda->copyin(
vector<float>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
sizesCuda->copyin(vector<uint32_t>{1, 1, 9, 10});
sizesCuda->copyin(vector<int64_t>{1, 1, 9, 10});
cudaRuntime->run(gCuda);
// copy output from CUDA to CPU
auto oCpu = gCpu->cloneTensor(op->getOutput(0));

View File

@ -10,9 +10,9 @@ TEST(Resize, ShapeInference) {
{
Graph g = make_ref<GraphObj>(cpuRuntime);
Tensor i = g->addTensor({1, 1, 2, 4}, DataType::UInt32);
Tensor sizes = g->addTensor({4}, DataType::UInt32);
Tensor sizes = g->addTensor({4}, DataType::Int64);
sizes->dataMalloc();
sizes->copyin(vector<uint32_t>{1, 1, 1, 3});
sizes->copyin(vector<int64_t>{1, 1, 1, 3});
auto op = g->addOp<ResizeObj>(
i, nullptr, std::nullopt, sizes, nullptr, nullptr,
ResizeObj::EKeepAspectRatioPolicy::stretch);
@ -22,9 +22,9 @@ TEST(Resize, ShapeInference) {
{
Graph g = make_ref<GraphObj>(cpuRuntime);
Tensor i = g->addTensor({1, 1, 2, 4}, DataType::UInt32);
Tensor sizes = g->addTensor({2}, DataType::UInt32);
Tensor sizes = g->addTensor({2}, DataType::Int64);
sizes->dataMalloc();
sizes->copyin(vector<uint32_t>{1, 3});
sizes->copyin(vector<int64_t>{1, 3});
auto op = g->addOp<ResizeObj>(
i, nullptr, vector<int>{2, 3}, sizes, nullptr, nullptr,
ResizeObj::EKeepAspectRatioPolicy::stretch);
@ -34,9 +34,9 @@ TEST(Resize, ShapeInference) {
{
Graph g = make_ref<GraphObj>(cpuRuntime);
Tensor i = g->addTensor({1, 3, 2, 4}, DataType::UInt32);
Tensor sizes = g->addTensor({2}, DataType::UInt32);
Tensor sizes = g->addTensor({2}, DataType::Int64);
sizes->dataMalloc();
sizes->copyin(vector<uint32_t>{7, 8});
sizes->copyin(vector<int64_t>{7, 8});
auto op = g->addOp<ResizeObj>(
i, nullptr, vector<int>{2, 3}, sizes, nullptr, nullptr,
ResizeObj::EKeepAspectRatioPolicy::notLarger);
@ -46,9 +46,9 @@ TEST(Resize, ShapeInference) {
{
Graph g = make_ref<GraphObj>(cpuRuntime);
Tensor i = g->addTensor({1, 3, 2, 4}, DataType::UInt32);
Tensor sizes = g->addTensor({3}, DataType::UInt32);
Tensor sizes = g->addTensor({3}, DataType::Int64);
sizes->dataMalloc();
sizes->copyin(vector<uint32_t>{2, 6, 8});
sizes->copyin(vector<int64_t>{2, 6, 8});
auto op = g->addOp<ResizeObj>(
i, nullptr, vector<int>{1, 2, 3}, sizes, nullptr, nullptr,
ResizeObj::EKeepAspectRatioPolicy::notSmaller);