【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;
}
相关推荐
不会代码的小猴8 小时前
Linux环境编程第六天笔记--system-V IPC
linux·笔记
乌恩大侠8 小时前
【笔记】USRP 5G 和 6G 参考架构
笔记·5g
biuyyyxxx8 小时前
Python自动化办公学习笔记(一) 工具安装&教程
笔记·python·学习·自动化
舟舟亢亢9 小时前
Java集合笔记总结
java·笔记
丝斯201110 小时前
AI学习笔记整理(66)——多模态大模型MOE-LLAVA
人工智能·笔记·学习
kida_yuan11 小时前
【Linux】运维实战笔记 — 我常用的方法与命令
linux·运维·笔记
laplace012311 小时前
Claude Skills 笔记整理
人工智能·笔记·agent·rag·skills
三块可乐两块冰11 小时前
【第二十八周】机器学习笔记二十九
笔记
血小板要健康12 小时前
Java基础常见面试题复习合集1
java·开发语言·经验分享·笔记·面试·学习方法
童话名剑12 小时前
情感分类与词嵌入除偏(吴恩达深度学习笔记)
笔记·深度学习·分类