C++简易版Tensor实现方法详解

 更新时间:2022年8月11日 13:47  点击:333 作者:AI_潜行者

基础知识铺垫

  • 缺省参数
  • 异常处理
  • 如果有模板元编程经验更好
  • std::memset、std::fill、std::fill_n、std::memcpy

std::memset 的内存填充单位固定为字节(char),所以不能应用与double,非char类型只适合置0。

std::fill 和 std::fill_n 则可以对指定类型进行内存填充,更加通用。

std::memcpy 则可以讲内存中排列好的数据拷贝过去,不同位置可填充不同值。

double dp[505];
std::memset(dp, -1.0, 505 * sizeof(double));//错误的 ★★★,memset的单位是字节(char),我们需要的是fill

double dp[505];
std::fill(dp, dp + 505, -1.0);
std::fill(std::begin(dp), std::end(dp), -1.0);
std::fill_n(dp, 505, -1.0);

double dp[505];
double data[5] = {11,22,33,44,55};
std::memcpy(dp, data, 5 * sizeof(double))

内存管理 allocate

在c++11中引入了智能指针这个概念,这个非常好,但是有一个问题显然被忘记了,如何动态创建智能指针数组,在c++11中没有提供直接的函数。换句话说,创建智能指针的make_shared,不支持创建数组。那在c++11中如何创建一个智能指针数组呢?只能自己封装或者变通实现,在c++14后可以支持构造函数创建智能指针数组,可这仍然不太符合技术规范发展的一致性,可继承性。

共享指针share_ptr 和 唯一指针unique_ptr 可能并不是一个很完整的方式,因为默认情况下需要开发人员手动的指定 delete handler。 但是只需要简单的封装一下就可以是更智能的方式,就是自动生成 delete handler。并且不必使用new(或者其他的指针形式)作为构造参数,而是直接通过 allocate 和 construct 两种形式,最抽象简单直观的方式得到想要的。

shared_ptr<T> pt0(new T());// 将会自动采用 std::default_delete
shared_ptr<int> p1 = make_shared<int>();
//指定 default_delete 作为释放规则
std::shared_ptr<int> p6(new int[10], std::default_delete<int[]>());
//自定义释放规则
void deleteInt(int*p) { delete []p; }
std::shared_ptr<int> p3(new int[10], deleteInt);

我们期待的规范后是这样使用的:不用考虑 释放规则,而且分为 allocate 和 construct 两种形式。

auto uptr = Alloc::unique_allocate<Foo>(sizeof(Foo));
auto sptr = Alloc::shared_allocate<Foo>(sizeof(Foo));
auto uptr = Alloc::unique_construct<Foo>();
auto sptr = Alloc::shared_construct<Foo>('6', '7');

allocator.h

#ifndef UTILS_ALLOCATOR_H
#define UTILS_ALLOCATOR_H
#include <cstdlib>
#include <map>
#include <memory>
#include <utility>
#include "base_config.h"
namespace st {
// 工具类(单例)
class Alloc {
public:
    // allocate 删除器
    class trivial_delete_handler {
    public:
        trivial_delete_handler(index_t size_) : size(size_) {}
        void operator()(void* ptr) { deallocate(ptr, size); }
    private:
        index_t size;
    };
    // construct 删除器
    template<typename T>
    class nontrivial_delete_handler {
    public:
        void operator()(void* ptr) {
            static_cast<T*>(ptr)->~T();
            deallocate(ptr, sizeof(T));
        }
    };
    // unique_ptr :对应 allocate
    template<typename T> 
    using TrivialUniquePtr = std::unique_ptr<T, trivial_delete_handler>;
    // unique_ptr :对应 construct
    template<typename T>
    using NontrivialUniquePtr = std::unique_ptr<T, nontrivial_delete_handler<T>>;
    // I know it's weird here. The type has been already passed in as T, but the
    // function parameter still need the number of bytes, instead of objects.
    // And their relationship is 
    //          nbytes = nobjects * sizeof(T).
    // Check what I do in "tensor/storage.cpp", and you'll understand.
    // Or maybe changing the parameter here and doing some extra work in 
    // "tensor/storage.cpp" is better.
    // 共享指针 allocate
    // 目的:自动生成 delete handler
    template<typename T> 
    static std::shared_ptr<T> shared_allocate(index_t nbytes) {
        void* raw_ptr = allocate(nbytes);
        return std::shared_ptr<T>(
            static_cast<T*>(raw_ptr),
            trivial_delete_handler(nbytes)
        );
    }
    // 唯一指针 allocate
    // 目的:自动生成 delete handler
    template<typename T>
    static TrivialUniquePtr<T> unique_allocate(index_t nbytes) {
        //开辟 内存
        void* raw_ptr = allocate(nbytes); 
        //返回 unique_ptr(自动生成了删除器)
        return TrivialUniquePtr<T>(
            static_cast<T*>(raw_ptr),
            trivial_delete_handler(nbytes)
        );
    }
    // 共享指针 construct
    // 目的:自动生成 delete handler
    template<typename T, typename... Args>
    static std::shared_ptr<T> shared_construct(Args&&... args) {
        void* raw_ptr = allocate(sizeof(T));
        new(raw_ptr) T(std::forward<Args>(args)...); 
        return std::shared_ptr<T>(
            static_cast<T*>(raw_ptr),
            nontrivial_delete_handler<T>()
        );
    }
    // 唯一指针 construct
    // 目的:自动生成 delete handler
    template<typename T, typename... Args>
    static NontrivialUniquePtr<T> unique_construct(Args&&... args) {
        void* raw_ptr = allocate(sizeof(T));
        new(raw_ptr) T(std::forward<Args>(args)...);
        return NontrivialUniquePtr<T>(
            static_cast<T*>(raw_ptr),
            nontrivial_delete_handler<T>()
        );
    }
    static bool all_clear(void);
private:
    Alloc() = default;
    ~Alloc(){ 
        /* release unique ptr, the map will not do destruction!!! */
        for (auto iter = cache_.begin(); iter != cache_.end(); ++iter) {  iter->second.release(); }
    }
    static Alloc& self(); // 单例
    static void* allocate(index_t size);
    static void deallocate(void* ptr, index_t size);
    static index_t allocate_memory_size;
    static index_t deallocate_memory_size;
    struct free_deletor {
        void operator()(void* ptr) { std::free(ptr); }
    };
    // multimap 允许容器有重复的 key 值
    // 保留开辟过又释放掉的堆内存,再次使用的时候可重复使用(省略了查找可用堆内存的操作)
    std::multimap<index_t, std::unique_ptr<void, free_deletor>> cache_;
};
} // namespace st
#endif

allocator.cpp

#include "allocator.h"
#include "exception.h"
#include <iostream>
namespace st {
index_t Alloc::allocate_memory_size = 0;
index_t Alloc::deallocate_memory_size = 0;
Alloc& Alloc::self() {
    static Alloc alloc;
    return alloc;
}
void* Alloc::allocate(index_t size) {
    auto iter = self().cache_.find(size);
    void* res;
    if(iter != self().cache_.end()) {
        // 临时:为什么要这么做?找到了为社么要删除
        res = iter->second.release();//释放指针指向内存
        self().cache_.erase(iter);//擦除
    } else {
        res = std::malloc(size); 
        CHECK_NOT_NULL(res, "failed to allocate %d memory.", size);
    }
    allocate_memory_size += size;
    return res;
}
void Alloc::deallocate(void* ptr, index_t size) {
    deallocate_memory_size += size;
    // 本质上是保留保留 堆内存中的位置,下一次可直接使用,而不是重新开辟
    self().cache_.emplace(size, ptr); // 插入
}
bool Alloc::all_clear() {
    return allocate_memory_size == deallocate_memory_size;
}
} // namespace st

使用:封装成 unique_allocate、unique_construct、share_allocate、share_construct 的目的就是对 share_ptr 和 unique_ptr 的生成自动赋予其对应的 delete handler。

struct Foo {
    static int ctr_call_counter;
    static int dectr_call_counter;
    char x_;
    char y_;
    Foo() { ++ctr_call_counter; }
    Foo(char x, char y) : x_(x), y_(y) { ++ctr_call_counter; }
    ~Foo() { ++dectr_call_counter; }
};
int Foo::ctr_call_counter = 0;
int Foo::dectr_call_counter = 0;
void test_Alloc() {
    using namespace st;
    // allocate 开辟空间
    // construct 开辟空间 + 赋值
    void* ptr;
    {//
        auto uptr = Alloc::unique_allocate<Foo>(sizeof(Foo));
        CHECK_EQUAL(Foo::ctr_call_counter, 0, "check 1");
        ptr = uptr.get();
    }
    CHECK_EQUAL(Foo::dectr_call_counter, 0, "check 1");
    {
        auto sptr = Alloc::shared_allocate<Foo>(sizeof(Foo));
        // The strategy of allocator.
        CHECK_EQUAL(ptr, static_cast<void*>(sptr.get()), "check 2");
    }
    {
        auto uptr = Alloc::unique_construct<Foo>();
        CHECK_EQUAL(Foo::ctr_call_counter, 1, "check 3");
        CHECK_EQUAL(ptr, static_cast<void*>(uptr.get()), "check 3");
    }
    CHECK_EQUAL(Foo::dectr_call_counter, 1, "check 3");
    {
        auto sptr = Alloc::shared_construct<Foo>('6', '7');
        CHECK_EQUAL(Foo::ctr_call_counter, 2, "check 4");
        CHECK_TRUE(sptr->x_ == '6' && sptr->y_ == '7', "check 4");
        CHECK_EQUAL(ptr, static_cast<void*>(sptr.get()), "check 4");
    }
    CHECK_EQUAL(Foo::dectr_call_counter, 2, "check 4");
}

实现Tensor需要准备shape和storage

shape 管理形状,每一个Tensor的形状都是唯一的(采用 unique_ptr管理数据),见array.h 个 shape.h。

storage:管理数据,不同的Tensor的数据可能是同一份数据(share_ptr管理数据),见stroage.h。

array.h

#ifndef UTILS_ARRAY_H
#define UTILS_ARRAY_H
#include <initializer_list>
#include <memory>
#include <cstring>
#include <iostream>
// utils
#include "base_config.h"
#include "allocator.h"
namespace st {
	// 应用是 tensor 的 shape, shape 是唯一的, 所以用 unique_ptr
	// 临时:实际上并不是很完善,目前的样子有点对不起这个 Dynamic 单词
	template<typename Dtype>
	class DynamicArray {
	public:
	    explicit DynamicArray(index_t size) 
	            : size_(size),
	              dptr_(Alloc::unique_allocate<Dtype>(size_ * sizeof(Dtype))) {
	    }
	    DynamicArray(std::initializer_list<Dtype> data) 
	            : DynamicArray(data.size()) {
	        auto ptr = dptr_.get();
	        for(auto d: data) {
	            *ptr = d;
	            ++ptr;
	        }
	    }
	    DynamicArray(const DynamicArray<Dtype>& other) 
	            : DynamicArray(other.size()) {
	        std::memcpy(dptr_.get(), other.dptr_.get(), size_ * sizeof(Dtype));
	    }
	    DynamicArray(const Dtype* data, index_t size) 
	            : DynamicArray(size) {
	        std::memcpy(dptr_.get(), data, size_ * sizeof(Dtype));
	    }
	    explicit DynamicArray(DynamicArray<Dtype>&& other) = default;
	    ~DynamicArray() = default;
	    Dtype& operator[](index_t idx) { return dptr_.get()[idx]; }
	    Dtype operator[](index_t idx) const { return dptr_.get()[idx]; }
	    index_t size() const { return size_; }
	    // 注意 std::memset 的单位是字节(char),若不是char类型,只用来置0,否则结果错误
	    // 临时:std::memset 对非char类型只适合内存置0,如果想要更加通用,不妨考虑一下 std::fill 和 std::fill_n
	    void memset(int value) const { std::memset(dptr_.get(), value, size_ * sizeof(Dtype)); } //原
	    void fill(int value) const (std::fill_n, size_, value); //改:见名知意
	private:
	    index_t size_;
	    Alloc::TrivialUniquePtr<Dtype> dptr_;
	};
} // namespace st
#endif

stroage.h

#ifndef TENSOR_STORAGE_H
#define TENSOR_STORAGE_H
#include <memory>
#include "base_config.h"
#include "allocator.h"
namespace st {
    namespace nn {
        class InitializerBase;
        class OptimizerBase;
    }
    class Storage {
    public:
        explicit Storage(index_t size);
        Storage(const Storage& other, index_t offset); //观察:offset 具体应用?bptr_数据依然是同一份,只是dptr_指向位置不同,这是关于pytorch的clip,切片等操作的设计方法
        Storage(index_t size, data_t value);
        Storage(const data_t* data, index_t size);
        explicit Storage(const Storage& other) = default;//复制构造(因为数据都是指针形式,所以直接默认就行)
        explicit Storage(Storage&& other) = default;//移动构造(因为数据都是指针形式,所以直接默认就行)
        ~Storage() = default;
        Storage& operator=(const Storage& other) = delete;
        // inline function
        data_t operator[](index_t idx) const { return dptr_[idx]; }
        data_t& operator[](index_t idx) { return dptr_[idx]; }
        index_t offset(void) const { return dptr_ - bptr_->data_; }//
        index_t version(void) const { return bptr_->version_; }//
        void increment_version(void) const { ++bptr_->version_; }//???
        // friend function
        friend class nn::InitializerBase;
        friend class nn::OptimizerBase;
    public:
        index_t size_;
    private:
        struct Vdata {
            index_t version_; //???
            data_t data_[1]; //永远指向数据头
        };
        std::shared_ptr<Vdata> bptr_;  // base pointer, share_ptr 的原因是不同的tensor可能指向的是storage数据
        data_t* dptr_;  // data pointer, 指向 Vdata 中的 data_, 他是移动的(游标)
    };
}  // namespace st
#endif

storage.cpp

#include <iostream>
#include <cstring>
#include <algorithm>
#include "storage.h"
namespace st {
    Storage::Storage(index_t size)
        : bptr_(Alloc::shared_allocate<Vdata>(size * sizeof(data_t) + sizeof(index_t))),
        dptr_(bptr_->data_)
    {
        bptr_->version_ = 0;
        this->size_ = size;
    }
    Storage::Storage(const Storage& other, index_t offset)
        : bptr_(other.bptr_),
        dptr_(other.dptr_ + offset)
    {
        this->size_ = other.size_;
    }
    Storage::Storage(index_t size, data_t value)
        : Storage(size) {
        //std::memset(dptr_, value, size * sizeof(data_t)); // 临时
        std::fill_n(dptr_, size, value);
    }
    Storage::Storage(const data_t* data, index_t size)
        : Storage(size) {
        std::memcpy(dptr_, data, size * sizeof(data_t));
    }
}  // namespace st

shape.h

#ifndef TENSOR_SHAPE_H
#define TENSOR_SHAPE_H
#include <initializer_list>
#include <ostream>
#include "base_config.h"
#include "allocator.h"
#include "array.h"
namespace st {
    class Shape {
    public:
        // constructor
        Shape(std::initializer_list<index_t> dims);
        Shape(const Shape& other, index_t skip);
        Shape(index_t* dims, index_t dim);
        Shape(IndexArray&& shape);
        Shape(const Shape& other) = default;
        Shape(Shape&& other) = default;
        ~Shape() = default;
        // method
        index_t dsize() const;
        index_t subsize(index_t start_dim, index_t end_dim) const;
        index_t subsize(index_t start_dim) const;
        bool operator==(const Shape& other) const;
        // inline function
        index_t ndim(void) const { return dims_.size(); }
        index_t operator[](index_t idx) const { return dims_[idx]; }
        index_t& operator[](index_t idx) { return dims_[idx]; }
        operator const IndexArray() const { return dims_; }
        // friend function
        friend std::ostream& operator<<(std::ostream& out, const Shape& s);
    private:
        IndexArray dims_; // IndexArray 就是(DynamicArray)
    };
}  // namespace st
#endif

shape.cpp

#include "shape.h"
namespace st {
    Shape::Shape(std::initializer_list<index_t> dims) : dims_(dims) {}
    Shape::Shape(const Shape& other, index_t skip) : dims_(other.ndim() - 1) {
        int i = 0;
        for (; i < skip; ++i)
            dims_[i] = other.dims_[i];
        for (; i < dims_.size(); ++i)
            dims_[i] = other.dims_[i + 1];
    }
    Shape::Shape(index_t* dims, index_t dim_) : dims_(dims, dim_) {}
    Shape::Shape(IndexArray&& shape) : dims_(std::move(shape)) {}
    index_t Shape::dsize() const {
        int res = 1;
        for (int i = 0; i < dims_.size(); ++i)
            res *= dims_[i];
        return res;
    }
    index_t Shape::subsize(index_t start_dim, index_t end_dim) const {
        int res = 1;
        for (; start_dim < end_dim; ++start_dim)
            res *= dims_[start_dim];
        return res;
    }
    index_t Shape::subsize(index_t start_dim) const {
        return subsize(start_dim, dims_.size());
    }
    bool Shape::operator==(const Shape& other) const {
        if (this->ndim() != other.ndim()) return false;
        index_t i = 0;
        for (; i < dims_.size() && dims_[i] == other.dims_[i]; ++i)
            ;
        return i == dims_.size();
    }
    std::ostream& operator<<(std::ostream& out, const Shape& s) {
        out << '(' << s[0];
        for (int i = 1; i < s.ndim(); ++i)
            out << ", " << s[i];
        out << ")";
        return out;
    }
}  // namespace st

Tensor的设计方法(基础)

知识准备:继承、指针类、奇异递归模板(静态多态)、表达式模板、Impl设计模式(声明实现分离)、友元类、模板特化。

tensor的设计采用的 impl 方法(声明和实现分离), 采用了奇异递归模板(静态多态),Tensor本身管理Tensor的张量运算,Exp则管理引用计数、梯度计数(反向求导,梯度更新时需要用到)的运算。

一共5个类:Tensor,TensorImpl,Exp,ExpImpl,ExpImplPtr,他们之间的关系由下图体现。

先上图:

代码:

// 代码比较多,就不放在这了,参看源码结合注释理解

Tensor的设计方法(更进一步)

Tensor 数据内存分布管理

Tensor的数据只有独一份,那么Tensor的各种操作 transpose,purmute,slice,等等,难道都要生出一个新的 tensor 和对应新的数据吗?当然不可能,能用一份数据的绝不用两份!tensor 数据的描述主要有 size(总数数据量),offset(此 tensor 相对于原始base数据的一个偏移量) ndim(几个维度),shape(每个维度映射的个数),stride(每个维度中数据的索引步长),stride 和 shape是 一 一 对应的,通过这个stride的索引公式,我们就可以用一份数据幻化出不同的tensor表象了。解析如下图

permute(轴换位置):shape 和 stride 调换序列一致即可。

transpose(指定两个轴换位置,转置):同上,与permute一致。

slice(切片):在原始数据上增加了一个偏移量。Tensor中的数据部分Storage中有一个bptr_(管理原始数据)和dptr_(管理当前tensor的数据指向)。

unsqueese(升维):指定dim加一个维度,且shape值为1,stride值则根据shape的subsize(dim)算出即可。

squeese(降维):dim为1的将自动消失并降维,shape 和 stride 对应位子都会去掉。

view(变形):目前是只支持连续数据分布且数据的size总和不变的情况,比如permute、transpose就会破坏这种连续。slice就会破坏数据size不一致。

到此这篇关于C++简易版Tensor实现方法详解的文章就介绍到这了,更多相关C++简易版Tensor内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://blog.csdn.net/weixin_40011280/article/details/126166

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • Pytorch之扩充tensor的操作

    这篇文章主要介绍了Pytorch之扩充tensor的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-05
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 基于C++中常见编译错误的总结详解

    本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25