【Cute学学习笔记】print_tensor打印error

经过实践,cute中使用print_tensor打印一个tensor,如果类型是half是会直接编译不过的:

cpp 复制代码
#include <cuda.h>
#include <stdlib.h>
#include <cute/tensor.hpp>

/*
    cute中的Tensor更多的是对Tensor进行分解和组合等操作,而这些操作多是对Layout的变换(只是逻辑层面的数据组织形式),底层的数据实体一般不变更。
    Tensor = Layout + storage
*/

// nvcc tensor.cu -arch=sm_89 -std=c++17 -I ../cutlass/include -I ../cutlass/tools/util/include --expt-relaxed-constexpr -cudart shared --cudadevrt none  -DDEBUG

using namespace cute;
using namespace std;

#define PRINT(name, content) \
    print(name);             \
    print(" : ");            \
    print(content);          \
    print("\n");

#define PRINTTENSOR(name, content) \
    print(name);                   \
    print(" : ");                  \
    print_tensor(content);         \
    print("\n");

template<typename T>
__global__ void handle_global_tensor(T *pointer)
{
    auto gshape = make_shape(Int<4>{}, Int<6>{});
    auto gstride = make_stride(Int<6>{}, Int<1>{});
    auto gtensor = make_tensor(make_gmem_ptr(pointer), make_layout(gshape, gstride));
    PRINTTENSOR("global tensor", gtensor);
}

int main()
{
    // register tensor
    // handle_regiser_tensor<<<1, 1>>>();

    // global memory tensor

    using T = half;

    T *pointer;
    int size = 4 * 6;
    cudaMalloc(&pointer, size * sizeof(T));
    T *cpointer = (T *)malloc(size * sizeof(T));
    for (int i = 0; i < size; i++)
    {
        cpointer[i] = (T)i;
    }
    cudaMemcpy(pointer, cpointer, size * sizeof(int), cudaMemcpyHostToDevice);
    handle_global_tensor<T><<<1, 1>>>(pointer);
    cudaDeviceSynchronize();
    return 0;
}

如果类型换成int或者float,是可以成功打印的:

cpp 复制代码
#include <cuda.h>
#include <stdlib.h>
#include <cute/tensor.hpp>

/*
    cute中的Tensor更多的是对Tensor进行分解和组合等操作,而这些操作多是对Layout的变换(只是逻辑层面的数据组织形式),底层的数据实体一般不变更。
    Tensor = Layout + storage
*/

// nvcc tensor.cu -arch=sm_89 -std=c++17 -I ../cutlass/include -I ../cutlass/tools/util/include --expt-relaxed-constexpr -cudart shared --cudadevrt none  -DDEBUG

using namespace cute;
using namespace std;

#define PRINT(name, content) \
    print(name);             \
    print(" : ");            \
    print(content);          \
    print("\n");

#define PRINTTENSOR(name, content) \
    print(name);                   \
    print(" : ");                  \
    print_tensor(content);         \
    print("\n");

template<typename T>
__global__ void handle_global_tensor(T *pointer)
{
    auto gshape = make_shape(Int<4>{}, Int<6>{});
    auto gstride = make_stride(Int<6>{}, Int<1>{});
    auto gtensor = make_tensor(make_gmem_ptr(pointer), make_layout(gshape, gstride));
    PRINTTENSOR("global tensor", gtensor);
}

int main()
{
    using T = float;

    T *pointer;
    int size = 4 * 6;
    cudaMalloc(&pointer, size * sizeof(T));
    T *cpointer = (T *)malloc(size * sizeof(T));
    for (int i = 0; i < size; i++)
    {
        cpointer[i] = (T)i;
    }
    cudaMemcpy(pointer, cpointer, size * sizeof(int), cudaMemcpyHostToDevice);
    handle_global_tensor<T><<<1, 1>>>(pointer);
    cudaDeviceSynchronize();
    return 0;
}

惊喜发现,不能直接用half,得用cute::half_t,这样的是可以打印的

cpp 复制代码
#include <cuda.h>
#include <stdlib.h>
#include <cute/tensor.hpp>

/*
    cute中的Tensor更多的是对Tensor进行分解和组合等操作,而这些操作多是对Layout的变换(只是逻辑层面的数据组织形式),底层的数据实体一般不变更。
    Tensor = Layout + storage
*/

// nvcc tensor.cu -arch=sm_89 -std=c++17 -I ../cutlass/include -I ../cutlass/tools/util/include --expt-relaxed-constexpr -cudart shared --cudadevrt none  -DDEBUG

using namespace cute;
using namespace std;

#define PRINT(name, content) \
    print(name);             \
    print(" : ");            \
    print(content);          \
    print("\n");

#define PRINTTENSOR(name, content) \
    print(name);                   \
    print(" : ");                  \
    print_tensor(content);         \
    print("\n");

template<typename T>
__global__ void handle_global_tensor(T *pointer)
{
    auto gshape = make_shape(Int<4>{}, Int<6>{});
    auto gstride = make_stride(Int<6>{}, Int<1>{});
    auto gtensor = make_tensor(make_gmem_ptr(pointer), make_layout(gshape, gstride));
    PRINTTENSOR("global tensor", gtensor);
}

int main()
{
    using T = cute::half_t;
    // using T = half;

    T *pointer;
    int size = 4 * 6;
    cudaMalloc(&pointer, size * sizeof(T));
    T *cpointer = (T *)malloc(size * sizeof(T));
    for (int i = 0; i < size; i++)
    {
        cpointer[i] = 1;
    }
    cudaMemcpy(pointer, cpointer, size * sizeof(T), cudaMemcpyHostToDevice);
    handle_global_tensor<T><<<1, 1>>>(pointer);
    cudaDeviceSynchronize();
    return 0;
}
相关推荐
2501_926978339 小时前
AGI 的四种瓶颈:资源型还是发现型--以及DSH的位置
人工智能·经验分享·笔记·ai写作
liuqs33210 小时前
nat123安卓和nat123全端口选型指南,映射工具怎么挑
网络·笔记
xian_wwq11 小时前
【学习笔记】光伏“四可”装置纵密模块--配置方法和原理
笔记·学习
日拱一卒的小田14 小时前
ZYNQ学习笔记3-ZYNQ的IIC控制器1
笔记·单片机·学习
不会代码的小猴15 小时前
C++新增关键字
开发语言·c++·笔记
LuminousCPP15 小时前
栈和队列专题(一):LeetCode 20. 有效的括号
数据结构·经验分享·笔记·leetcode·手写栈
小赵AI手记18 小时前
技术拆解(十七)具身智能:机器人动作生成为何走向Diffusion Policy?
人工智能·笔记·python·机器人
深蓝海拓18 小时前
基于socket的 TCP上位机通信内容解析程序
网络·笔记·python·学习·tcp/ip·plc
疯狂打码的少年18 小时前
【数据结构】二叉排序树(BST)的定义与操作
数据结构·笔记·算法
蒸蒸yyyyzwd18 小时前
后端面经学习笔记
笔记·学习