ソースを参照

add more arithmetic functions

JasonWang 7 年 前
コミット
fb32b00b9f

+ 2 - 2
traph/include/traph/cltensor/context.h → traph/include/traph/cltensor/clcontext.h

@@ -1,5 +1,5 @@
-#ifndef TRAPH_CLTENSOR_CONTEXT_H_
-#define TRAPH_CLTENSOR_CONTEXT_H_
+#ifndef TRAPH_CLTENSOR_CLCONTEXT_H_
+#define TRAPH_CLTENSOR_CLCONTEXT_H_
 
 #include <stdexcept>
 #include <vector>

+ 2 - 2
traph/include/traph/cltensor/device.h → traph/include/traph/cltensor/cldevice.h

@@ -1,5 +1,5 @@
-#ifndef TRAPH_CLTENSOR_DEVICE_H_
-#define TRAPH_CLTENSOR_DEVICE_H_
+#ifndef TRAPH_CLTENSOR_CLDEVICE_H_
+#define TRAPH_CLTENSOR_CLDEVICE_H_
 
 #include <stdexcept>
 #include <vector>

+ 2 - 2
traph/include/traph/cltensor/platform.h → traph/include/traph/cltensor/clplatform.h

@@ -1,5 +1,5 @@
-#ifndef TRAPH_CLTENSOR_PLATFORM_H_
-#define TRAPH_CLTENSOR_PLATFORM_H_
+#ifndef TRAPH_CLTENSOR_CLPLATFORM_H_
+#define TRAPH_CLTENSOR_CLPLATFORM_H_
 
 #include <stdexcept>
 #include <vector>

+ 27 - 1
traph/include/traph/cltensor/cltensor.h

@@ -1,6 +1,8 @@
 #ifndef TRAPH_CLTENSOR_CLTENSOR_H_
 #define TRAPH_CLTENSOR_CLTENSOR_H_
 
+#include <traph/core/tensor.h>
+
 #if defined(__APPLE__) || defined(__MACOSX)
 #include <OpenCL/cl.hpp>
 #else
@@ -9,8 +11,32 @@
 
 namespace traph
 {
-    class CLTensor
+    template<class T>
+    class CLTensorStorage: public ContiguousStorageBase<T>
+    {
+    public:
+        virtual idx_type size() const = 0;
+        virtual size_type element_size() const = 0;
+
+        virtual void resize_(idx_type size) = 0;
+    };
+
+    template<class T>
+    class CLTensor: public TensorBase<T>
     {
+    public:
+        virtual void reshape(const DimVector& dims) = 0;
+
+		virtual idx_type offset() const = 0;
+
+		virtual layout_type layout() const = 0;
+
+		virtual DimVector size() const = 0;
+
+		virtual const T* data() const = 0;
+		virtual T* data() = 0;
+
+		virtual DimVector strides() const = 0;
 
     };
 }

+ 42 - 1
traph/include/traph/core/tensor.h

@@ -1,6 +1,8 @@
 #ifndef TRAPH_CORE_TENSOR_H_
 #define TRAPH_CORE_TENSOR_H_
 
+#include <algorithm>
+
 #include <traph/core/type.h>
 #include <traph/core/index.h>
 
@@ -23,6 +25,8 @@ namespace traph
     public:
         virtual void reshape(const DimVector& dims) = 0;
 
+        virtual void resize(const DimVector& dims) = 0;
+
 		virtual idx_type offset() const = 0;
 
 		virtual layout_type layout() const = 0;
@@ -33,8 +37,45 @@ namespace traph
 		virtual T* data() = 0;
 
 		virtual DimVector strides() const = 0;
-
     };
+
+    template<class T>
+    bool broadcastable(const TensorBase<T> &lhs, const TensorBase<T> & rhs)
+    {
+        DimVector lhs_dim = lhs.size();
+        DimVector rhs_dim = rhs.size();
+        if(lhs_dim.size() < 1 || rhs_dim.size() < 1)
+            return false;
+
+        idx_type min = std::min(lhs_dim.size(), rhs_dim.size());
+        for(idx_type i = 0; i<min;++i)
+        {
+            if(lhs_dim[i] != rhs_dim[i] &&
+                lhs_dim[i] != 1 &&
+                rhs_dim[i] != 1)
+            {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    template<class T>
+    bool strict_same_shape(const TensorBase<T> &lhs, const TensorBase<T> & rhs)
+    {
+        DimVector lhs_dim = lhs.size();
+        DimVector rhs_dim = rhs.size();
+        if(lhs_dim.size() != rhs_dim.size())
+            return false;
+        for(idx_type i = 0; i < lhs_dim.size(); ++i)
+        {
+            if(lhs_dim[i] != rhs_dim[i])
+                return false;
+        }
+
+        return true;
+    }
 }
 
 #endif

+ 33 - 2
traph/include/traph/nn/variable.h

@@ -1,7 +1,12 @@
 #ifndef TRAPH_NN_VARIABLE_H_
 #define TRAPH_NN_VARIABLE_H_
 
+#include <memory>
+#include <initializer_list>
+
+#include <traph/core/index.h>
 #include <traph/core/tensor.h>
+#include <traph/tensor/tensor.h>
 
 namespace traph
 {
@@ -9,8 +14,34 @@ namespace traph
     class Variable
     {
     private:
-        TensorBase<T> _data;
-        TensorBase<T> _grad;
+        std::unique_ptr<TensorBase<T>> _data;
+        std::unique_ptr<TensorBase<T>> _grad;
+    public:
+        Variable()
+        {
+
+        }
+
+        Variable(const DimVector& dim)
+            :_data(new Tensor<T>(dim)), _grad(new Tensor<T>(dim))
+        {
+        }
+
+        Variable(std::initializer_list<idx_type> l)
+            :_data(new Tensor<T>()), _grad(new Tensor<T>())
+        {
+            DimVector dim;
+            for (auto i : l)
+                dim.push_back(i);
+            
+            _data.resize_(dim);
+            _grad.resize_(dim);
+        }
+
+        ~Variable()
+        {
+
+        }
     };
 }
 

+ 60 - 0
traph/include/traph/tensor/arithmetic.h

@@ -144,6 +144,66 @@ namespace traph
 	Tensor<f32> matmul(const Tensor<f32> &a, const Tensor<f32> &b);
 
 	Tensor<f64> matmul(const Tensor<f64> &a, const Tensor<f64> &b);
+
+	template<class T>
+	Tensor<T> mean(const Tensor<T> &input)
+	{
+		T result{};
+		idx_type flat_size = input.size().flat_size();
+		idx_type offset = input.offset();
+		const T *input_data = input.data();
+		for (idx_type i = offset; i < offset + flat_size; ++i)
+		{
+			result += input_data[i];
+		}
+
+		return result / flat_size;
+	}
+
+	template<class T>
+	Tensor<T> mul(const Tensor<T> &input, T value)
+	{
+		Tensor<T> result(input.size());
+		idx_type flat_size = input.size().flat_size();
+		idx_type offset = input.offset();
+		const T *input_data = input.data();
+		T *result_data = result.data();
+		for (idx_type i = 0; i < flat_size; ++i)
+		{
+			result_data[i] += input_data[i + offset] + value;
+		}
+
+		return result;
+	}
+
+	template<class T>
+	void mul_check(const Tensor<T> &input, const Tensor<T> & other)
+	{
+		if(!strict_same_shape(input, other))
+			throw std::runtime_error("mul: Two tensor must have the same shape or be broadcastable.");
+	}
+
+	template<class T>
+	Tensor<T> mul(const Tensor<T> &input, const Tensor<T> & other)
+	{
+		// check
+		mul_check(input, other);
+
+		Tensor<T> result(input.size());
+		idx_type flat_size = input.size().flat_size();
+		idx_type input_offset = input.offset();
+		idx_type other_offset = other.offset();
+		const T *input_data = input.data();
+		const T *other_data = other.data();
+		T *result_data = result.data();
+
+		for (idx_type i = 0; i < flat_size; ++i)
+		{
+			result_data[i] += input_data[i + input_offset] + other_data[i + other_offset];
+		}
+
+		return result;
+	}
 }
 
 #endif

+ 8 - 0
traph/include/traph/tensor/tensor.h

@@ -247,6 +247,14 @@ namespace traph
         {
 
         }
+
+        void resize(const DimVector& dims)
+        {
+            _dimensions = dims;
+            _rep->resize_(dims.flat_size());
+            auto_strides();
+        }
+
 		// info
 		idx_type offset() const
 		{

+ 6 - 2
traph/source/cltensor/CMakeLists.txt

@@ -7,8 +7,12 @@ SET(SOURCE_PATH ${TRAPH_PATH_SOURCE}/${LIB_NAME})
 SET(CORE_LIST
 	${HEADER_PATH}/cltensor.h
 	${SOURCE_PATH}/cltensor.cpp
-	${HEADER_PATH}/device.h
-	${SOURCE_PATH}/device.cpp
+	${HEADER_PATH}/cldevice.h
+	${SOURCE_PATH}/cldevice.cpp
+	${HEADER_PATH}/clcontext.h
+	${SOURCE_PATH}/clcontext.cpp
+	${HEADER_PATH}/clplatform.h
+	${SOURCE_PATH}/clplatform.cpp
 )
 
 ADD_LIBRARY(${LIB_OUTNAME} ${CORE_LIST})

+ 0 - 0
traph/source/cltensor/context.cpp → traph/source/cltensor/clcontext.cpp


+ 0 - 0
traph/source/cltensor/device.cpp → traph/source/cltensor/cldevice.cpp


+ 0 - 0
traph/source/cltensor/platform.cpp → traph/source/cltensor/clplatform.cpp


+ 4 - 4
traph/source/tensor/arithmetic.cpp

@@ -4,15 +4,15 @@
 
 #include <traph/tensor/arithmetic.h>
 
-
-#ifdef TRAPH_BUILD_EIGEN
 #include <eigen3/Eigen/Dense>
-#elif defined TRAPH_BUILD_MKL
+
+
+#ifdef TRAPH_BUILD_MKL
 #include <mkl.h>
 #include <mkl_blas.h>
 #include <mkl_cblas.h>
 #elif defined TRAPH_BUILD_OPENBLAS
-#include <traph/core/openblas_backend.h>
+#include <openBLAS/cblas.h>
 #endif
 
 namespace traph

+ 10 - 2
traph/source/test/main.cpp

@@ -1,5 +1,7 @@
+#include <traph/core/tensor.h>
 #include <traph/tensor/tensor.h>
 #include <traph/tensor/arithmetic.h>
+#include <traph/nn/variable.h>
 
 int main()
 {
@@ -9,10 +11,16 @@ int main()
     traph::Tensor<float> result = traph::matmul(a, w);
 	*/
 	// traph::Tensor<float> result2 = traph::add(a, 1.f);
-	
+	/*
 	traph::Tensor<traph::f32> a = traph::zeros<traph::f32>({ 5000, 5000 });
 	traph::Tensor<traph::f32> b = traph::zeros<traph::f32>({ 5000, 5000 });
 	traph::Tensor<traph::f32> c = traph::matmul(a, b);
-	
+	*/
+
+	auto a = traph::Variable<traph::f32>({2, 3});
+	auto c = traph::mul(traph::mul(a, a), 3);
+	auto out = traph::mean(c);
+	out.backward();
+		
     return 0;
 }