[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/TensorArray/TensorArray/master/src/tensor-array/core/tensor.hh [Back]  [Original]

/*
Copyright 2024 TensorArray-Creators

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include 
#include 
#include 
#include "tensorbase.hh"

#pragma once

#define USING_DATA_TYPE_FLOAT() (float)(double)
#define USING_DATA_TYPE_SINT() (int8_t)(int16_t)(int32_t)(int64_t)
#define USING_DATA_TYPE_UINT() (uint8_t)(uint16_t)(uint32_t)(uint64_t)
#define USING_DATA_TYPE USING_DATA_TYPE_SINT() USING_DATA_TYPE_UINT() USING_DATA_TYPE_FLOAT()

#define LOOP(seq) END(A seq)
#define BODY(x) ADD_CODE(x)
#define A(x) BODY(x) B
#define B(x) BODY(x) A
#define A_END
#define B_END
#define END(...) END_(__VA_ARGS__)
#define END_(...) __VA_ARGS__##_END

#ifdef _WIN32
#ifdef TENSOR_ARRAY_CORE_EXPORTS
#define TENSOR_ARRAY_API __declspec(dllexport)
#else
#define TENSOR_ARRAY_API __declspec(dllimport)
#endif
#else
#define TENSOR_ARRAY_API
#endif

namespace tensor_array
{
	namespace value
	{
		extern TENSOR_ARRAY_API bool use_grad;

#ifdef TENSOR_CONTENT
        void* create_mem_101(std::size_t s, const void* dat);
        class DataBuffer
        {
        public:
            template
            constexpr DataBuffer(const T(&data)) :
                data(create_mem_101(sizeof(T), &data)),
                data_size(sizeof(T))
            {
                static_assert(std::is_trivially_copyable_v, "Requied default constructor");
            }
            template
            constexpr DataBuffer(const std::initializer_list &data) :
                data(create_mem_101(sizeof(T) * data.size(), data.begin())),
                data_size(sizeof(T) * data.size())
            {
                static_assert(std::is_trivially_copyable_v, "Requied default constructor");
            }
            DataBuffer();
            DataBuffer(std::nullptr_t);
            DataBuffer(const DataBuffer&);
            ~DataBuffer();
            const void* const& get_data() const;
            const std::size_t& get_data_size() const;
            DataBuffer& operator=(const DataBuffer&);
            friend bool operator==(const DataBuffer&, const DataBuffer&);
        private:
            const void* data;
            std::size_t data_size;
        };

        class Derivation;
#endif

        struct dimension
        {
            unsigned int x = 1U, y = 1U, z = 1U;
        };

        struct ConvolutionParameter
        {
            dimension
                input,
                kernel,
                strides,
                dilation;
        };

        class Tensor;

        TENSOR_ARRAY_API Tensor tensor_rand(const std::initializer_list&, unsigned int = std::rand());

        TENSOR_ARRAY_API std::pair tensor_broadcasting(const Tensor&, const Tensor&, unsigned char = 0, unsigned char = 0);

        /**
         * \brief Dynamic derivative tensor.
         * \brief This class use to calculate the tensor.
         */
        class TENSOR_ARRAY_API Tensor
        {
        public:
            /**
             * \brief Create an empty tensor.
             */
            constexpr Tensor() = default;

            /**
             * \brief Base Tensor copy.
             */
            Tensor(const TensorBase&);

            /**
             * \brief Base Tensor move.
             */
            Tensor(TensorBase&&);

            ~Tensor();

            friend class WeakTensor;
            friend struct std::hash;
            friend struct std::equal_to;

            /**
             * \brief This class can iterate copy child tensor by index and derivate to parent tensor,
             */
            class TENSOR_ARRAY_API Iterator
            {
            public:
                using iterator_category = std::forward_iterator_tag;
                using difference_type = std::ptrdiff_t;
                using value_type = Tensor;
                using reference = value_type;
                using reference_left = const value_type&;
                Iterator(reference_left, unsigned int);
                reference operator*() const;
                Iterator& operator++();
                Iterator& operator--();
                Iterator operator++(int);
                Iterator operator--(int);
                friend bool TENSOR_ARRAY_API operator==(const Iterator&, const Iterator&);
                friend bool TENSOR_ARRAY_API operator!=(const Iterator&, const Iterator&);
            private:
                unsigned long long index;
                reference_left ref;
            };
            struct Slice
            {
                int begin = 0;
                int end = -1;
                int strides = 1;
            };
            Tensor clone() const;
            void save(const char*) const;
            int real_index(int) const;
            Slice correct_slice(const Slice&) const;
            bool& multithread_derive();
            long tensor_use_count();
            void calc_grad();
            Iterator begin() const;
            Iterator end() const;
            Iterator cbegin() const;
            Iterator cend() const;
            const TensorBase& get_buffer() const;
            Tensor padding(unsigned int);
            Tensor loss(const Tensor&) const;
            Tensor mean(unsigned char) const;
            Tensor variance(unsigned char) const;
            Tensor mean(const std::initializer_list&) const;
            Tensor variance(const std::initializer_list&) const;
            Tensor mean(const std::vector&) const;
            Tensor variance(const std::vector&) const;
            Tensor value_scalar() const;
            Tensor get_grad() const;
            Tensor expand(unsigned char, unsigned int);
            Tensor reshape(const std::initializer_list&) const;
            Tensor reshape(const std::vector&) const;
            Tensor tensor_cast(const std::type_info&) const;
            Tensor conv_padding(const dimension&) const;
            Tensor transpose(unsigned char, unsigned char) const;
            std::pair max(unsigned char = 0) const;
            std::pair min(unsigned char = 0) const;
#ifdef TENSOR_CONTENT
#endif
            bool has_tensor() const;
            template
            operator T () const;


            Tensor unslice(const std::initializer_list&, const std::initializer_list&) const;

            /**
             * \brief Array Operator.
             * You can chain tensor array operator to a scalar.
             * \param pos Position of this tensor.
             * \return
             * Tensor
             */
            Tensor operator[](unsigned int) const;

            Tensor operator[](const std::initializer_list&) const;

            Tensor operator+() const;

            Tensor operator-() const;

            Tensor& operator+=(const Tensor&);

            Tensor& operator-=(const Tensor&);

            Tensor& operator*=(const Tensor&);

            Tensor& operator/=(const Tensor&);

            friend TENSOR_ARRAY_API Tensor operator>(const Tensor&, const Tensor&);
            friend TENSOR_ARRAY_API Tensor operator()), value);
        }

        template
        inline Tensor zeros(const std::initializer_list& shape_list)
        {
            return TensorBase(typeid(T), shape_list);
        }

        template
        inline Tensor zeros(const std::vector& shape_vector)
        {
            return zeros(wrapper::initializer_wrapper(shape_vector.begin().operator->(), shape_vector.end().operator->()));
        }
}
}

template
struct std::hash
{
    inline std::size_t operator()(const tensor_array::value::Tensor& t) const
    {
        return std::hash()(t.tensor_data);
    }
};

template
struct std::equal_to
{
    inline std::size_t operator()(const tensor_array::value::Tensor& a, const tensor_array::value::Tensor& b) const
    {
        return std::equal_to()(a.tensor_data, b.tensor_data);
    }
};

#undef LOOP
#undef BODY
#undef A
#undef B
#undef A_END
#undef B_END
#undef END
#undef END_

#undef USING_DATA_TYPE
#undef USING_DATA_TYPE_FLOAT
#undef USING_DATA_TYPE_SINT
#undef USING_DATA_TYPE_UINT

#undef TENSOR_ARRAY_API

Web Proxy Viewer  |  New URL  |  Original Page