Unverified Commit 85bc98ac authored by qinyiqun's avatar qinyiqun Committed by GitHub
Browse files

ISSUE/628 适配QY C610 GPU,增加编译选项,适配已有算子。添加bge类模型所需的算子, (#629)



* ISSUE/628 适配QY C610 GPU,增加编译选项,适配已有算子。添加bge类模型所需的算子,包括gelu,layer_norm,lp_norm(支持l1,l2 norm),relu,softmax,tanh。

---------
Co-authored-by: default avatarxgqdut2016 <kenan_gewei@163.com>
Co-authored-by: default avatarxgqdut2016 <140036308+xgqdut2016@users.noreply.github.com>
parent 7c397dd2
#ifndef __TANH_CUDA_H__
#define __TANH_CUDA_H__
#include <cmath>
#include <cuda_bf16.h>
#include <cuda_fp16.h>
namespace op::tanh::cuda {
typedef struct TanhOp {
static constexpr size_t num_inputs = 1;
__device__ __forceinline__ float tanh_f32_func(float x) const {
return tanhf(x);
}
template <typename T>
__device__ __forceinline__ T operator()(const T &input) const {
if constexpr (std::is_same_v<T, half2>) {
float2 vf = __half22float2(input);
float2 vr = make_float2(tanh_f32_func(vf.x), tanh_f32_func(vf.y));
return __float22half2_rn(vr);
} else if constexpr (std::is_same_v<T, half>) {
float xf = __half2float(input);
float yf = tanh_f32_func(xf);
return __float2half_rn(yf);
} else if constexpr (std::is_same_v<T, cuda_bfloat162>) {
float f0 = __bfloat162float(__low2bfloat16(input));
float f1 = __bfloat162float(__high2bfloat16(input));
float r0 = tanh_f32_func(f0);
float r1 = tanh_f32_func(f1);
return __floats2bfloat162_rn(r0, r1);
} else if constexpr (std::is_same_v<T, cuda_bfloat16>) {
float xf = __bfloat162float(input);
float rf = tanh_f32_func(xf);
return __float2bfloat16_rn(rf);
} else if constexpr (std::is_same_v<T, float>) {
return tanh_f32_func(input);
} else if constexpr (std::is_same_v<T, double>) {
return std::tanh(input);
} else {
return std::tanh(input);
}
}
} TanhOp;
} // namespace op::tanh::cuda
#endif // __TANH_CUDA_H__
#ifndef __TANH_METAX_API_H__
#define __TANH_METAX_API_H__
#include "../../../elementwise/metax/elementwise_metax_api.h"
ELEMENTWISE_DESCRIPTOR(tanh, metax)
#endif // __TANH_METAX_API_H__
#include "tanh_metax.h"
#include "../../../elementwise/metax/elementwise_metax.h"
#include "../cuda/kernel.cuh"
namespace op::tanh::metax {
Descriptor::~Descriptor() = default;
infiniStatus_t Descriptor::create(
infiniopHandle_t handle_,
Descriptor **desc_ptr,
infiniopTensorDescriptor_t out_desc,
std::vector<infiniopTensorDescriptor_t> input_desc_vec) {
auto handle = reinterpret_cast<device::metax::Handle *>(handle_);
auto dtype = out_desc->dtype();
const auto &input_desc = input_desc_vec.at(0);
const auto &output_shape = out_desc->shape();
const auto &input_shape = input_desc->shape();
CHECK_DTYPE(dtype, INFINI_DTYPE_F16, INFINI_DTYPE_F32, INFINI_DTYPE_F64, INFINI_DTYPE_BF16);
CHECK_SAME_SHAPE(output_shape, input_shape);
// create CUDA elementwise descriptor
CREATE_ELEMENTWISE_METAX_DESCRIPTOR(handle, dtype, out_desc, input_desc_vec)
return INFINI_STATUS_SUCCESS;
}
infiniStatus_t Descriptor::calculate(
void *workspace,
size_t workspace_size,
void *output,
std::vector<const void *> inputs,
void *stream) const {
if (workspace_size < _workspace_size) {
return INFINI_STATUS_INSUFFICIENT_WORKSPACE;
}
switch (_dtype) {
case INFINI_DTYPE_F16:
return _device_info->calculate<256, cuda::TanhOp, half>(_info, workspace, output, inputs, stream);
case INFINI_DTYPE_BF16:
return _device_info->calculate<256, cuda::TanhOp, cuda_bfloat16>(_info, workspace, output, inputs, stream);
case INFINI_DTYPE_F32:
return _device_info->calculate<256, cuda::TanhOp, float>(_info, workspace, output, inputs, stream);
case INFINI_DTYPE_F64:
return _device_info->calculate<256, cuda::TanhOp, double>(_info, workspace, output, inputs, stream);
default:
return INFINI_STATUS_BAD_TENSOR_DTYPE;
}
return INFINI_STATUS_SUCCESS;
}
} // namespace op::tanh::metax
#include "../../../elementwise/nvidia/elementwise_nvidia.cuh"
#include "../cuda/kernel.cuh"
#include "tanh_nvidia.cuh"
namespace op::tanh::nvidia {
Descriptor::~Descriptor() = default;
infiniStatus_t Descriptor::create(
infiniopHandle_t handle_,
Descriptor **desc_ptr,
infiniopTensorDescriptor_t out_desc,
std::vector<infiniopTensorDescriptor_t> input_desc_vec) {
auto handle = reinterpret_cast<device::nvidia::Handle *>(handle_);
auto dtype = out_desc->dtype();
const auto &input_desc = input_desc_vec.at(0);
const auto &output_shape = out_desc->shape();
const auto &input_shape = input_desc->shape();
CHECK_DTYPE(dtype, INFINI_DTYPE_F16, INFINI_DTYPE_F32, INFINI_DTYPE_F64, INFINI_DTYPE_BF16);
CHECK_SAME_SHAPE(output_shape, input_shape);
// create CUDA elementwise descriptor
CREATE_ELEMENTWISE_CUDA_DESCRIPTOR(handle, dtype, out_desc, input_desc_vec)
return INFINI_STATUS_SUCCESS;
}
infiniStatus_t Descriptor::calculate(
void *workspace,
size_t workspace_size,
void *output,
std::vector<const void *> inputs,
void *stream) const {
if (workspace_size < _workspace_size) {
return INFINI_STATUS_INSUFFICIENT_WORKSPACE;
}
switch (_dtype) {
case INFINI_DTYPE_F16:
return _device_info->calculate<256, cuda::TanhOp, half>(_info, workspace, output, inputs, stream);
case INFINI_DTYPE_BF16:
return _device_info->calculate<256, cuda::TanhOp, cuda_bfloat16>(_info, workspace, output, inputs, stream);
case INFINI_DTYPE_F32:
return _device_info->calculate<256, cuda::TanhOp, float>(_info, workspace, output, inputs, stream);
case INFINI_DTYPE_F64:
return _device_info->calculate<256, cuda::TanhOp, double>(_info, workspace, output, inputs, stream);
default:
return INFINI_STATUS_BAD_TENSOR_DTYPE;
}
return INFINI_STATUS_SUCCESS;
}
} // namespace op::tanh::nvidia
#ifndef __TANH_CUDA_API_H__
#define __TANH_CUDA_API_H__
#include "../../../elementwise/nvidia/elementwise_nvidia_api.cuh"
ELEMENTWISE_DESCRIPTOR(tanh, nvidia)
#endif // __TANH_CUDA_API_H__
#include "../../operator.h"
#include "../../handle.h"
#include "infiniop/ops/tanh.h"
#ifdef ENABLE_CPU_API
#include "cpu/tanh_cpu.h"
#endif
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_ILUVATAR_API) || defined(ENABLE_QY_API)
#include "nvidia/tanh_nvidia.cuh"
#endif
#ifdef ENABLE_METAX_API
#include "metax/tanh_metax.h"
#endif
__C infiniStatus_t infiniopCreateTanhDescriptor(
infiniopHandle_t handle,
infiniopTanhDescriptor_t *desc_ptr,
infiniopTensorDescriptor_t output_desc,
infiniopTensorDescriptor_t input_desc) {
#define CREATE(CASE, NAMESPACE) \
case CASE: \
return op::tanh::NAMESPACE::Descriptor::create( \
handle, \
reinterpret_cast<op::tanh::NAMESPACE::Descriptor **>(desc_ptr), \
output_desc, \
{input_desc})
switch (handle->device) {
#ifdef ENABLE_CPU_API
CREATE(INFINI_DEVICE_CPU, cpu);
#endif
#ifdef ENABLE_NVIDIA_API
CREATE(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_ILUVATAR_API
CREATE(INFINI_DEVICE_ILUVATAR, nvidia);
#endif
#ifdef ENABLE_QY_API
CREATE(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
CREATE(INFINI_DEVICE_METAX, metax);
#endif
default:
return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
}
#undef CREATE
}
__C infiniStatus_t infiniopGetTanhWorkspaceSize(infiniopTanhDescriptor_t desc, size_t *size) {
#define GET(CASE, NAMESPACE) \
case CASE: \
*size = reinterpret_cast<op::tanh::NAMESPACE::Descriptor *>(desc)->workspaceSize(); \
return INFINI_STATUS_SUCCESS;
switch (desc->device_type) {
#ifdef ENABLE_CPU_API
GET(INFINI_DEVICE_CPU, cpu);
#endif
#ifdef ENABLE_NVIDIA_API
GET(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_ILUVATAR_API
GET(INFINI_DEVICE_ILUVATAR, nvidia);
#endif
#ifdef ENABLE_QY_API
GET(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
GET(INFINI_DEVICE_METAX, metax);
#endif
default:
return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
}
#undef GET
return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
}
__C infiniStatus_t infiniopTanh(
infiniopTanhDescriptor_t desc,
void *workspace,
size_t workspace_size,
void *output,
const void *input,
void *stream) {
#define CALCULATE(CASE, NAMESPACE) \
case CASE: \
return reinterpret_cast<const op::tanh::NAMESPACE::Descriptor *>(desc) \
->calculate(workspace, workspace_size, output, {input}, stream)
switch (desc->device_type) {
#ifdef ENABLE_CPU_API
CALCULATE(INFINI_DEVICE_CPU, cpu);
#endif
#ifdef ENABLE_NVIDIA_API
CALCULATE(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_ILUVATAR_API
CALCULATE(INFINI_DEVICE_ILUVATAR, nvidia);
#endif
#ifdef ENABLE_QY_API
CALCULATE(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
CALCULATE(INFINI_DEVICE_METAX, metax);
#endif
default:
return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
}
#undef CALCULATE
}
__C infiniStatus_t
infiniopDestroyTanhDescriptor(infiniopTanhDescriptor_t desc) {
#define DELETE(CASE, NAMESPACE) \
case CASE: \
delete reinterpret_cast<const op::tanh::NAMESPACE::Descriptor *>(desc); \
return INFINI_STATUS_SUCCESS
switch (desc->device_type) {
#ifdef ENABLE_CPU_API
DELETE(INFINI_DEVICE_CPU, cpu);
#endif
#ifdef ENABLE_NVIDIA_API
DELETE(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_ILUVATAR_API
DELETE(INFINI_DEVICE_ILUVATAR, nvidia);
#endif
#ifdef ENABLE_QY_API
DELETE(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
DELETE(INFINI_DEVICE_METAX, metax);
#endif
default:
return INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED;
}
#undef DELETE
}
#ifdef ENABLE_NVIDIA_API
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_QY_API)
#include "../../../devices/nvidia/nvidia_common.cuh"
#include "../../../devices/nvidia/nvidia_kernel_common.cuh"
......
......@@ -5,7 +5,7 @@
#ifdef ENABLE_CPU_API
#include "cpu/topkrouter_cpu.h"
#endif
#if defined(ENABLE_NVIDIA_API)
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_QY_API)
#include "nvidia/topkrouter_nvidia.cuh"
#endif
......@@ -23,6 +23,9 @@ __C infiniStatus_t infiniopCreateTopkrouterDescriptor(infiniopHandle_t handle, i
#endif
#ifdef ENABLE_NVIDIA_API
CREATE(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_QY_API
CREATE(INFINI_DEVICE_QY, nvidia);
#endif
}
......@@ -43,6 +46,9 @@ __C infiniStatus_t infiniopGetTopkrouterWorkspaceSize(infiniopTopkrouterDescript
#endif
#ifdef ENABLE_NVIDIA_API
GET(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_QY_API
GET(INFINI_DEVICE_QY, nvidia);
#endif
}
......@@ -66,6 +72,9 @@ __C infiniStatus_t infiniopTopkrouter(infiniopTopkrouterDescriptor_t desc, void
#endif
#ifdef ENABLE_NVIDIA_API
CALCULATE(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_QY_API
CALCULATE(INFINI_DEVICE_QY, nvidia);
#endif
}
......@@ -86,6 +95,9 @@ __C infiniStatus_t infiniopDestroyTopkrouterDescriptor(infiniopTopkrouterDescrip
#endif
#ifdef ENABLE_NVIDIA_API
DESTROY(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_QY_API
DESTROY(INFINI_DEVICE_QY, nvidia);
#endif
}
......
......@@ -5,7 +5,7 @@
#ifdef ENABLE_CPU_API
#include "cpu/topksoftmax_cpu.h"
#endif
#if defined(ENABLE_NVIDIA_API)
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_QY_API)
#include "nvidia/topksoftmax_nvidia.cuh"
#endif
#ifdef ENABLE_METAX_API
......@@ -28,6 +28,9 @@ __C infiniStatus_t infiniopCreateTopksoftmaxDescriptor(infiniopHandle_t handle,
#ifdef ENABLE_NVIDIA_API
CREATE(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_QY_API
CREATE(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
CREATE(INFINI_DEVICE_METAX, metax);
#endif
......@@ -52,6 +55,9 @@ __C infiniStatus_t infiniopGetTopksoftmaxWorkspaceSize(infiniopTopksoftmaxDescri
#ifdef ENABLE_NVIDIA_API
GET(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_QY_API
GET(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
GET(INFINI_DEVICE_METAX, metax);
#endif
......@@ -81,6 +87,9 @@ __C infiniStatus_t infiniopTopksoftmax(infiniopTopksoftmaxDescriptor_t desc, voi
#ifdef ENABLE_NVIDIA_API
CALCULATE(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_QY_API
CALCULATE(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
CALCULATE(INFINI_DEVICE_METAX, metax);
#endif
......@@ -105,6 +114,9 @@ __C infiniStatus_t infiniopDestroyTopksoftmaxDescriptor(infiniopTopksoftmaxDescr
#ifdef ENABLE_NVIDIA_API
DESTROY(INFINI_DEVICE_NVIDIA, nvidia);
#endif
#ifdef ENABLE_QY_API
DESTROY(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
DESTROY(INFINI_DEVICE_METAX, metax);
#endif
......
......@@ -5,7 +5,7 @@
#ifdef ENABLE_CPU_API
#include "cpu/zeros_cpu.h"
#endif
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_ILUVATAR_API)
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_ILUVATAR_API) || defined(ENABLE_QY_API)
#include "nvidia/zeros_nvidia.cuh"
#endif
#ifdef ENABLE_METAX_API
......@@ -40,6 +40,9 @@ __C infiniStatus_t infiniopCreateZerosDescriptor(
#ifdef ENABLE_ILUVATAR_API
CREATE(INFINI_DEVICE_ILUVATAR, nvidia);
#endif
#ifdef ENABLE_QY_API
CREATE(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
CREATE(INFINI_DEVICE_METAX, metax);
#endif
......@@ -70,6 +73,9 @@ __C infiniStatus_t infiniopGetZerosWorkspaceSize(infiniopZerosDescriptor_t desc,
#ifdef ENABLE_ILUVATAR_API
GET(INFINI_DEVICE_ILUVATAR, nvidia);
#endif
#ifdef ENABLE_QY_API
GET(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
GET(INFINI_DEVICE_METAX, metax);
#endif
......@@ -108,6 +114,9 @@ __C infiniStatus_t infiniopZeros(
#ifdef ENABLE_ILUVATAR_API
CALCULATE(INFINI_DEVICE_ILUVATAR, nvidia);
#endif
#ifdef ENABLE_QY_API
CALCULATE(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
CALCULATE(INFINI_DEVICE_METAX, metax);
#endif
......@@ -140,6 +149,9 @@ infiniopDestroyZerosDescriptor(infiniopZerosDescriptor_t desc) {
#ifdef ENABLE_ILUVATAR_API
DELETE(INFINI_DEVICE_ILUVATAR, nvidia);
#endif
#ifdef ENABLE_QY_API
DELETE(INFINI_DEVICE_QY, nvidia);
#endif
#ifdef ENABLE_METAX_API
DELETE(INFINI_DEVICE_METAX, metax);
#endif
......
#ifndef __INFINIOP_REDUCE_CUDA_H__
#define __INFINIOP_REDUCE_CUDA_H__
#include <cub/block/block_reduce.cuh>
/*
* Device functions for reduction operations on CUDA.
*
......
......@@ -20,6 +20,7 @@ void printUsage() {
<< " metax" << std::endl
<< " moore" << std::endl
<< " iluvatar" << std::endl
<< " qy" << std::endl
<< " kunlun" << std::endl
<< " hygon" << std::endl
<< std::endl;
......@@ -51,6 +52,7 @@ ParsedArgs parseArgs(int argc, char *argv[]) {
else PARSE_DEVICE("--metax", INFINI_DEVICE_METAX)
else PARSE_DEVICE("--moore", INFINI_DEVICE_MOORE)
else PARSE_DEVICE("--iluvatar", INFINI_DEVICE_ILUVATAR)
else PARSE_DEVICE("--qy", INFINI_DEVICE_QY)
else PARSE_DEVICE("--kunlun", INFINI_DEVICE_KUNLUN)
else PARSE_DEVICE("--hygon", INFINI_DEVICE_HYGON)
else {
......
......@@ -3,7 +3,7 @@
#include "../infinirt_impl.h"
namespace infinirt::cuda {
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_ILUVATAR_API) || defined(ENABLE_HYGON_API)
#if defined(ENABLE_NVIDIA_API) || defined(ENABLE_ILUVATAR_API) || defined(ENABLE_QY_API) || defined(ENABLE_HYGON_API)
INFINIRT_DEVICE_API_IMPL
#else
INFINIRT_DEVICE_API_NOOP
......
......@@ -23,7 +23,7 @@ __C infiniStatus_t infinirtGetAllDeviceCount(int *count_array) {
return INFINI_STATUS_NULL_POINTER;
}
for (size_t i = 0; i < INFINI_DEVICE_TYPE_COUNT; i++) {
if (i == INFINI_DEVICE_ILUVATAR || i == INFINI_DEVICE_HYGON) {
if (i == INFINI_DEVICE_ILUVATAR || i == INFINI_DEVICE_QY || i == INFINI_DEVICE_KUNLUN || i == INFINI_DEVICE_HYGON) {
count_array[i] = 0;
continue;
}
......@@ -77,6 +77,9 @@ __C infiniStatus_t infinirtGetDevice(infiniDevice_t *device_ptr, int *device_id_
case INFINI_DEVICE_ILUVATAR: \
_status = infinirt::cuda::API PARAMS; \
break; \
case INFINI_DEVICE_QY: \
_status = infinirt::cuda::API PARAMS; \
break; \
case INFINI_DEVICE_HYGON: \
_status = infinirt::cuda::API PARAMS; \
break; \
......
......@@ -68,10 +68,11 @@ _TOLERANCE_MAP = {
# Helper Functions
# ==============================================================================
def load_binary_with_torch(filename, dtype, shape):
"""使用 torch.frombuffer 读取二进制文件"""
torch_dtype = to_torch_dtype(dtype)
with open(filename, 'rb') as f:
with open(filename, "rb") as f:
data = f.read()
return torch.frombuffer(data, dtype=torch_dtype).reshape(shape)
......@@ -80,19 +81,23 @@ def load_binary_with_torch(filename, dtype, shape):
# Test Methods
# ==============================================================================
def test_basic_print(device, test_case, dtype, config):
"""测试基本的 debug 打印功能"""
test_name, shape = test_case.args
print(f"Testing Basic Print on {InfiniDeviceNames[device]} with "
f"shape:{shape}, dtype:{dtype}")
print(
f"Testing Basic Print on {InfiniDeviceNames[device]} with "
f"shape:{shape}, dtype:{dtype}"
)
device_str = torch_device_map[device]
torch_dtype = to_torch_dtype(dtype)
# 创建测试张量
torch_tensor = torch.arange(1, int(np.prod(shape)) + 1,
dtype=torch_dtype, device=device_str).reshape(shape)
torch_tensor = torch.arange(
1, int(np.prod(shape)) + 1, dtype=torch_dtype, device=device_str
).reshape(shape)
infini_tensor = create_infinicore_tensor(torch_tensor, device_str)
......@@ -106,15 +111,18 @@ def test_binary_save(device, test_case, dtype, config):
"""测试二进制格式保存"""
test_name, shape = test_case.args
print(f"Testing Binary Save on {InfiniDeviceNames[device]} with "
f"shape:{shape}, dtype:{dtype}")
print(
f"Testing Binary Save on {InfiniDeviceNames[device]} with "
f"shape:{shape}, dtype:{dtype}"
)
device_str = torch_device_map[device]
torch_dtype = to_torch_dtype(dtype)
# 创建测试张量
torch_tensor = torch.arange(1, int(np.prod(shape)) + 1,
dtype=torch_dtype, device=device_str).reshape(shape)
torch_tensor = torch.arange(
1, int(np.prod(shape)) + 1, dtype=torch_dtype, device=device_str
).reshape(shape)
infini_tensor = create_infinicore_tensor(torch_tensor, device_str)
......@@ -128,8 +136,9 @@ def test_binary_save(device, test_case, dtype, config):
# 验证文件大小
expected_size = int(np.prod(shape)) * torch_tensor.element_size()
actual_size = os.path.getsize(bin_file)
assert actual_size == expected_size, \
f"Binary file size mismatch: {actual_size} vs {expected_size}"
assert (
actual_size == expected_size
), f"Binary file size mismatch: {actual_size} vs {expected_size}"
# 使用 torch.frombuffer 读取并验证
loaded_tensor = load_binary_with_torch(bin_file, dtype, shape)
......@@ -139,9 +148,12 @@ def test_binary_save(device, test_case, dtype, config):
loaded_tensor_cpu = loaded_tensor.cpu()
tolerance = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 1e-5})
assert torch.allclose(loaded_tensor_cpu, torch_tensor_cpu,
atol=tolerance["atol"], rtol=tolerance["rtol"]), \
f"Binary data mismatch"
assert torch.allclose(
loaded_tensor_cpu,
torch_tensor_cpu,
atol=tolerance["atol"],
rtol=tolerance["rtol"],
), f"Binary data mismatch"
# 清理
os.remove(bin_file)
......@@ -152,15 +164,18 @@ def test_multidimensional(device, test_case, dtype, config):
"""测试多维张量"""
test_name, shape = test_case.args
print(f"Testing Multidimensional on {InfiniDeviceNames[device]} with "
f"shape:{shape}, dtype:{dtype}")
print(
f"Testing Multidimensional on {InfiniDeviceNames[device]} with "
f"shape:{shape}, dtype:{dtype}"
)
device_str = torch_device_map[device]
torch_dtype = to_torch_dtype(dtype)
# 创建多维张量
torch_tensor = torch.arange(1, int(np.prod(shape)) + 1,
dtype=torch_dtype, device=device_str).reshape(shape)
torch_tensor = torch.arange(
1, int(np.prod(shape)) + 1, dtype=torch_dtype, device=device_str
).reshape(shape)
infini_tensor = create_infinicore_tensor(torch_tensor, device_str)
......@@ -179,9 +194,12 @@ def test_multidimensional(device, test_case, dtype, config):
loaded_tensor_cpu = loaded_tensor.cpu()
tolerance = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 1e-5})
assert torch.allclose(loaded_tensor_cpu, torch_tensor_cpu,
atol=tolerance["atol"], rtol=tolerance["rtol"]), \
f"Multidimensional data mismatch"
assert torch.allclose(
loaded_tensor_cpu,
torch_tensor_cpu,
atol=tolerance["atol"],
rtol=tolerance["rtol"],
), f"Multidimensional data mismatch"
# 清理
os.remove(bin_file)
......@@ -202,8 +220,9 @@ def test_non_contiguous_stride(device, test_case, dtype, config):
# 创建连续张量
print(f"\nStep 1: Creating contiguous tensor...")
torch_tensor_orig = torch.arange(1, int(np.prod(shape)) + 1,
dtype=torch_dtype, device=device_str).reshape(shape)
torch_tensor_orig = torch.arange(
1, int(np.prod(shape)) + 1, dtype=torch_dtype, device=device_str
).reshape(shape)
print(f" Original shape: {torch_tensor_orig.shape}")
print(f" Original stride: {torch_tensor_orig.stride()}")
print(f" Is contiguous: {torch_tensor_orig.is_contiguous()}")
......@@ -222,8 +241,7 @@ def test_non_contiguous_stride(device, test_case, dtype, config):
# permute([1, 0]) 相当于 transpose,会创建非连续的内存布局
infini_tensor_orig = create_infinicore_tensor(torch_tensor_orig, device_str)
infini_tensor_t = infini_tensor_orig.as_strided(
list(torch_tensor_t.shape),
list(torch_tensor_t.stride())
list(torch_tensor_t.shape), list(torch_tensor_t.stride())
)
print(f"\nStep 3: InfiniCore tensor after permute:")
......@@ -249,11 +267,14 @@ def test_non_contiguous_stride(device, test_case, dtype, config):
expected_size = int(np.prod(torch_tensor_t.shape)) * torch_tensor_t.element_size()
print(f"\nFile size check:")
print(f" Expected: {expected_size} bytes ({int(np.prod(torch_tensor_t.shape))} elements)")
print(
f" Expected: {expected_size} bytes ({int(np.prod(torch_tensor_t.shape))} elements)"
)
print(f" Actual: {actual_size} bytes")
assert actual_size == expected_size, \
f"File size mismatch: {actual_size} vs {expected_size}"
assert (
actual_size == expected_size
), f"File size mismatch: {actual_size} vs {expected_size}"
print(f" ✓ File size is correct")
# 读取并验证数据
......@@ -267,12 +288,17 @@ def test_non_contiguous_stride(device, test_case, dtype, config):
print(f" Expected (first 2 rows):\n{torch_tensor_cpu[:2]}")
print(f" Got (first 2 rows):\n{loaded_tensor_cpu[:2]}")
assert torch.allclose(loaded_tensor_cpu, torch_tensor_cpu,
atol=tolerance["atol"], rtol=tolerance["rtol"]), \
f"Data verification failed: loaded data doesn't match expected"
assert torch.allclose(
loaded_tensor_cpu,
torch_tensor_cpu,
atol=tolerance["atol"],
rtol=tolerance["rtol"],
), f"Data verification failed: loaded data doesn't match expected"
print(f"\n✓ Binary format: Data matches perfectly!")
print(f" Binary format correctly handles non-contiguous memory layout using stride")
print(
f" Binary format correctly handles non-contiguous memory layout using stride"
)
# 清理
os.remove(bin_file)
......@@ -280,7 +306,9 @@ def test_non_contiguous_stride(device, test_case, dtype, config):
print(f"\n{'='*70}")
print(f"Non-Contiguous Memory Layout Test Summary:")
print(f" ✅ Binary format (.bin): NOW supports non-contiguous memory!")
print(f" Performance: Contiguous tensors use fast path, non-contiguous use stride-based writing")
print(
f" Performance: Contiguous tensors use fast path, non-contiguous use stride-based writing"
)
print(f"{'='*70}\n")
......@@ -368,10 +396,12 @@ def test_large_scale_binary_performance(device, test_case, dtype, config):
sample_size = min(1000, num_elements)
tolerance = _TOLERANCE_MAP.get(dtype, {"atol": 0, "rtol": 1e-5})
assert torch.allclose(loaded_tensor_cpu.flatten()[:sample_size],
assert torch.allclose(
loaded_tensor_cpu.flatten()[:sample_size],
torch_tensor_cpu.flatten()[:sample_size],
atol=tolerance["atol"], rtol=tolerance["rtol"]), \
f"Data verification failed (sampled first {sample_size} elements)"
atol=tolerance["atol"],
rtol=tolerance["rtol"],
), f"Data verification failed (sampled first {sample_size} elements)"
print(f" Data verification: ✓ (sampled first {sample_size} elements)")
......@@ -383,7 +413,9 @@ def test_large_scale_binary_performance(device, test_case, dtype, config):
print(f" File size: {actual_size_mb:.2f} MB")
print(f" Write time: {save_time:.4f} sec → {write_throughput_mbps:.2f} MB/s")
print(f" Read time: {read_time:.4f} sec → {read_throughput_mbps:.2f} MB/s")
print(f" Speed ratio (Read/Write): {read_throughput_mbps/write_throughput_mbps:.2f}x")
print(
f" Speed ratio (Read/Write): {read_throughput_mbps/write_throughput_mbps:.2f}x"
)
print(f"{'='*70}")
# 清理
......@@ -395,6 +427,7 @@ def test_large_scale_binary_performance(device, test_case, dtype, config):
# Main Execution Function
# ==============================================================================
def main():
args = get_args()
......@@ -435,7 +468,9 @@ def main():
print(f"{'='*60}")
non_contiguous_runner = TestRunner(_NON_CONTIGUOUS_TEST_CASES, config)
non_contiguous_passed = non_contiguous_runner.run_tests(devices, test_non_contiguous_stride)
non_contiguous_passed = non_contiguous_runner.run_tests(
devices, test_non_contiguous_stride
)
all_passed = all_passed and non_contiguous_passed
# 运行大规模性能测试
......@@ -444,7 +479,9 @@ def main():
print(f"{'='*60}")
large_scale_runner = TestRunner(_LARGE_SCALE_TEST_CASES, config)
large_scale_passed = large_scale_runner.run_tests(devices, test_large_scale_binary_performance)
large_scale_passed = large_scale_runner.run_tests(
devices, test_large_scale_binary_performance
)
all_passed = all_passed and large_scale_passed
# 打印总结
......
......@@ -23,6 +23,7 @@ def get_supported_hardware_platforms():
("--moore", "Moore Threads GPUs (requires torch_musa)"),
("--kunlun", "Kunlun XPUs (requires torch_xmlir)"),
("--hygon", "Hygon DCUs"),
("--qy", "QY GPUs"),
]
......@@ -195,6 +196,15 @@ def get_test_devices(args):
except ImportError:
print("Warning: Hygon DCU support not available")
if args.qy:
try:
# Iluvatar GPU detection
import torch
devices_to_test.append(InfiniDeviceEnum.QY)
except ImportError:
print("Warning: QY GPU support not available")
# Default to CPU if no devices specified
if not devices_to_test:
devices_to_test = [InfiniDeviceEnum.CPU]
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment