Skip to content

Commit

Permalink
refactoring math:: namespace
Browse files Browse the repository at this point in the history
some math:: functions are refactored to take const correct Tensor& references.

This is part of the changes to address issue #14.
  • Loading branch information
Dando18 committed Jul 23, 2019
1 parent 5361877 commit 5ffc201
Show file tree
Hide file tree
Showing 33 changed files with 360 additions and 704 deletions.
4 changes: 2 additions & 2 deletions include/math/add.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace magmadnn {
namespace math {

template <typename T>
void add_in_place(T alpha, Tensor<T> *x, T beta, Tensor<T> *out);
void add_in_place(T alpha, const Tensor &x, T beta, Tensor &out);

#if defined(_HAS_CUDA_)
template <typename T>
void add_in_place_device(T alpha, Tensor<T> *x, T beta, Tensor<T> *out);
void add_in_place_device(T alpha, const Tensor &x, T beta, Tensor &out);
#endif

} // namespace math
Expand Down
4 changes: 2 additions & 2 deletions include/math/bias_add.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace magmadnn {
namespace math {

template <typename T>
void bias_add(Tensor<T> *x, Tensor<T> *bias, Tensor<T> *out);
void bias_add(const Tensor &x, const Tensor &bias, Tensor &out);

#if defined(_HAS_CUDA_)
template <typename T>
void bias_add_device(Tensor<T> *x, Tensor<T> *bias, Tensor<T> *out);
void bias_add_device(const Tensor &x, const Tensor &bias, Tensor &out);
#endif

} // namespace math
Expand Down
2 changes: 1 addition & 1 deletion include/math/concat.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace math {
* @param C axis dim size should equal sum of A and B axis dim size
*/
template <typename T>
void concat(Tensor<T> *A, Tensor<T> *B, Tensor<T> *C, unsigned int axis);
void concat(const Tensor &A, const Tensor &B, Tensor &C, index_t axis);

} // namespace math
} // namespace magmadnn
4 changes: 2 additions & 2 deletions include/math/crossentropy.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ namespace math {
* @param out scalar tensor
*/
template <typename T>
void crossentropy(Tensor<T> *predicted, Tensor<T> *ground_truth, Tensor<T> *out);
void crossentropy(const Tensor &predicted, const Tensor &ground_truth, Tensor &out);

#if defined(_HAS_CUDA_)
template <typename T>
void crossentropy_device(Tensor<T> *predicted, Tensor<T> *ground_truth, Tensor<T> *out);
void crossentropy_device(const Tensor &predicted, const Tensor &ground_truth, Tensor &out);
#endif

} // namespace math
Expand Down
11 changes: 6 additions & 5 deletions include/math/dropout.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
#pragma once

#include "math/product.h"
#include "math/binary_math_operations.h"
#include "math/launch_math_kernel.h"
#include "tensor/tensor.h"
#include "types.h"
#include "utilities_internal.h"
Expand Down Expand Up @@ -43,18 +44,18 @@ struct cudnn_dropout_shared_settings_t {
#endif

template <typename T>
void dropout(Tensor<T> *x, Tensor<T> *out, Tensor<T> *mask, float dropout_rate);
void dropout(const Tensor &x, Tensor &out, const Tensor &mask, float dropout_rate);

template <typename T>
void dropout_grad(Tensor<T> *grad, Tensor<T> *out, Tensor<T> *mask);
void dropout_grad(const Tensor &grad, Tensor &out, const Tensor &mask);

#if defined(_HAS_CUDA_)
template <typename T>
void dropout_device(Tensor<T> *x, Tensor<T> *out, cudnn_dropout_settings_t settings,
void dropout_device(const Tensor &x, Tensor &out, cudnn_dropout_settings_t settings,
cudnn_dropout_shared_settings_t shared);

template <typename T>
void dropout_grad_device(Tensor<T> *grad, Tensor<T> *out, cudnn_dropout_grad_settings_t settings,
void dropout_grad_device(const Tensor &grad, Tensor &out, cudnn_dropout_grad_settings_t settings,
cudnn_dropout_shared_settings_t shared);
#endif

Expand Down
2 changes: 1 addition & 1 deletion include/math/matmul.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ namespace magmadnn {
namespace math {

template <typename T>
void matmul(T alpha, bool trans_A, Tensor<T> *A, bool trans_B, Tensor<T> *B, T beta, Tensor<T> *C);
void matmul(T alpha, bool trans_A, const Tensor &A, bool trans_B, const Tensor &B, T beta, Tensor &C);
}
} // namespace magmadnn
20 changes: 0 additions & 20 deletions include/math/negate.h

This file was deleted.

21 changes: 9 additions & 12 deletions include/math/pooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
* @author Sedrick Keh
* @version 1.0
* @date 2019-07-08
*
*
* @copyright Copyright (c) 2019
*/
#pragma once

#include "tensor/tensor.h"
#include "utilities_internal.h"

enum pooling_mode {
MAX_POOL,
AVERAGE_POOL
};
enum pooling_mode { MAX_POOL, AVERAGE_POOL };

#if defined(_HAS_CUDA_)
#include "cudnn.h"
Expand All @@ -24,11 +21,10 @@ namespace magmadnn {
namespace math {

template <typename T>
void pooling(Tensor<T> *x, Tensor<T> *out);
void pooling(const Tensor &x, Tensor &out);

template <typename T>
void pooling_grad(Tensor<T> *x, Tensor<T> *y, Tensor<T> *grad, Tensor<T> *out);

void pooling_grad(const Tensor &x, const Tensor &y, const Tensor &grad, Tensor &out);

#if defined(_HAS_CUDA_)

Expand All @@ -37,12 +33,13 @@ struct cudnn_pooling_settings_t {
};

template <typename T>
void pooling_device(Tensor<T> *x, Tensor<T> *out, cudnn_pooling_settings_t settings);
void pooling_device(const Tensor &x, Tensor &out, cudnn_pooling_settings_t settings);

template <typename T>
void pooling_grad_device(Tensor<T> *x, Tensor<T> *y, Tensor<T> *grad, Tensor<T> *out, cudnn_pooling_settings_t settings);
void pooling_grad_device(const Tensor &x, const Tensor &y, const Tensor &grad, Tensor &out,
cudnn_pooling_settings_t settings);

#endif

}
}
} // namespace math
} // namespace magmadnn
27 changes: 0 additions & 27 deletions include/math/pow.h

This file was deleted.

33 changes: 0 additions & 33 deletions include/math/product.h

This file was deleted.

8 changes: 4 additions & 4 deletions include/math/relu.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ namespace magmadnn {
namespace math {

template <typename T>
void relu(Tensor<T> *x, Tensor<T> *out);
void relu(const Tensor &x, Tensor &out);

template <typename T>
void relu_grad(Tensor<T> *x, Tensor<T> *relu_out, Tensor<T> *grad, Tensor<T> *out);
void relu_grad(const Tensor &x, const Tensor &relu_out, const Tensor &grad, Tensor &out);

#if defined(_HAS_CUDA_)

Expand All @@ -31,10 +31,10 @@ struct relu_cudnn_settings_t {
};

template <typename T>
void relu_device(Tensor<T> *x, Tensor<T> *out, relu_cudnn_settings_t settings);
void relu_device(const Tensor &x, Tensor &out, relu_cudnn_settings_t settings);

template <typename T>
void relu_grad_device(Tensor<T> *x, Tensor<T> *relu_out, Tensor<T> *grad, Tensor<T> *out,
void relu_grad_device(const Tensor &x, const Tensor &relu_out, const Tensor &grad, Tensor &out,
relu_cudnn_settings_t settings);
#endif

Expand Down
25 changes: 0 additions & 25 deletions include/math/scalar_tensor_product.h

This file was deleted.

9 changes: 5 additions & 4 deletions include/math/softmax.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@ struct cudnn_softmax_grad_settings_t {
#endif

template <typename T>
void softmax(Tensor<T> *x, Tensor<T> *out);
void softmax(const Tensor &x, Tensor &out);

template <typename T>
void softmax_grad(Tensor<T> *softmax, Tensor<T> *grad, Tensor<T> *out);
void softmax_grad(const Tensor &softmax, const Tensor &grad, Tensor &out);

#if defined(_HAS_CUDA_)
template <typename T>
void softmax_device(Tensor<T> *x, Tensor<T> *out, cudnn_softmax_settings_t settings);
void softmax_device(const Tensor &x, Tensor &out, cudnn_softmax_settings_t settings);

template <typename T>
void softmax_grad_device(Tensor<T> *y, Tensor<T> *grad, Tensor<T> *out, cudnn_softmax_grad_settings_t settings);
void softmax_grad_device(const Tensor &softmax, const Tensor &grad, Tensor &out,
cudnn_softmax_grad_settings_t settings);
#endif

} // namespace math
Expand Down
4 changes: 2 additions & 2 deletions include/math/sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace magmadnn {
namespace math {

template <typename T>
void sum(const std::vector<Tensor<T>*>& tensors, Tensor<T>* out);
void sum(const std::vector<std::reference_wrapper<const Tensor>>& tensors, Tensor& out);

#if defined(_HAS_CUDA_)
template <typename T>
void sum_device(const std::vector<Tensor<T>*>& tensors, Tensor<T>* out);
void sum_device(const std::vector<std::reference_wrapper<const Tensor>>& tensors, Tensor& out);
#endif

} // namespace math
Expand Down
30 changes: 16 additions & 14 deletions src/math/add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ namespace magmadnn {
namespace math {

template <typename T>
void add_in_place(T alpha, Tensor<T> *x, T beta, Tensor<T> *out) {
assert(x->get_size() == out->get_size());
assert(T_IS_SAME_MEMORY_TYPE(x, out));
void add_in_place(T alpha, const Tensor &x, T beta, Tensor &out) {
MAGMADNN_ASSERT(T_IS_SAME_DTYPE(x, out), "data type mismatch");
MAGMADNN_ASSERT(TYPES_MATCH(T, x.dtype()), "data type mismatch");
MAGMADNN_ASSERT(x->get_size() == out->get_size(), "sizes must match");
MAGMADNN_ASSERT(T_IS_SAME_MEMORY_TYPE(x, out), "memory type mismatch");

if (out->get_memory_type() == HOST) {
T *x_ptr = x->get_ptr();
T *out_ptr = out->get_ptr();
unsigned int size = out->get_size();
if (out.get_memory_type() == HOST) {
const T *x_ptr = x.get_ptr<T>();
T *out_ptr = out.get_ptr<T>();
size_t size = out.size();

for (unsigned int i = 0; i < size; i++) {
for (index_t i = 0; i < size; i++) {
out_ptr[i] = alpha * x_ptr[i] + beta * out_ptr[i];
}
}
Expand All @@ -31,16 +33,16 @@ void add_in_place(T alpha, Tensor<T> *x, T beta, Tensor<T> *out) {
}
#endif
}
template void add_in_place(int alpha, Tensor<int> *x, int beta, Tensor<int> *out);
template void add_in_place(float alpha, Tensor<float> *x, float beta, Tensor<float> *out);
template void add_in_place(double alpha, Tensor<double> *x, double beta, Tensor<double> *out);
#define COMPILE_ADDINPLACE(type) template void add_in_place(type, const Tensor &, type, Tensor &);
CALL_FOR_ALL_TYPES(COMPILE_ADDINPLACE)
#undef COMPILE_ADDINPLACE

#if defined(_HAS_CUDA_)
template <typename T>
void add_in_place_device(T alpha, Tensor<T> *x, T beta, Tensor<T> *out) {
void add_in_place_device(T alpha, const Tensor &x, T beta, Tensor &out) {
cudnnErrchk(cudnnAddTensor(::magmadnn::internal::MAGMADNN_SETTINGS->cudnn_handle, &alpha,
x->get_cudnn_tensor_descriptor(), x->get_ptr(), &beta,
out->get_cudnn_tensor_descriptor(), out->get_ptr()));
x.get_cudnn_tensor_descriptor(), x.get_ptr<T>(), &beta,
out.get_cudnn_tensor_descriptor(), out.get_ptr<T>()));
}
#endif

Expand Down
Loading

0 comments on commit 5ffc201

Please sign in to comment.