From b22941bc5b56e657f57a27f876e848c68de2de2b Mon Sep 17 00:00:00 2001 From: Jorgedavyd Date: Wed, 12 Jun 2024 16:31:10 -0400 Subject: [PATCH] gsn fourier cpu c++ --- .vscode/settings.json | 5 + .../csrc/include/lightorch/nn/cuda/deeprnn.cu | 1 + .../include/lightorch/nn/cuda/embedding.cu | 1 + .../csrc/include/lightorch/nn/cuda/fourier.cu | 1 + .../csrc/include/lightorch/nn/cuda/gsn.cu | 1 + .../csrc/include/lightorch/nn/cuda/partial.cu | 1 + .../csrc/include/lightorch/nn/embedding.h | 14 ++- lightorch/csrc/include/lightorch/nn/fourier.h | 91 ++++++++++++++++++- lightorch/csrc/include/lightorch/nn/gsn.h | 28 +++++- 9 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 lightorch/csrc/include/lightorch/nn/cuda/deeprnn.cu create mode 100644 lightorch/csrc/include/lightorch/nn/cuda/embedding.cu create mode 100644 lightorch/csrc/include/lightorch/nn/cuda/fourier.cu create mode 100644 lightorch/csrc/include/lightorch/nn/cuda/gsn.cu create mode 100644 lightorch/csrc/include/lightorch/nn/cuda/partial.cu diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d756850 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "complex": "cpp" + } +} \ No newline at end of file diff --git a/lightorch/csrc/include/lightorch/nn/cuda/deeprnn.cu b/lightorch/csrc/include/lightorch/nn/cuda/deeprnn.cu new file mode 100644 index 0000000..ae3dd0c --- /dev/null +++ b/lightorch/csrc/include/lightorch/nn/cuda/deeprnn.cu @@ -0,0 +1 @@ +// Optimized implementation for deep recurrent neural networks \ No newline at end of file diff --git a/lightorch/csrc/include/lightorch/nn/cuda/embedding.cu b/lightorch/csrc/include/lightorch/nn/cuda/embedding.cu new file mode 100644 index 0000000..7384197 --- /dev/null +++ b/lightorch/csrc/include/lightorch/nn/cuda/embedding.cu @@ -0,0 +1 @@ +// Implementation for Patch Embedding \ No newline at end of file diff --git a/lightorch/csrc/include/lightorch/nn/cuda/fourier.cu b/lightorch/csrc/include/lightorch/nn/cuda/fourier.cu new file mode 100644 index 0000000..73d7ce7 --- /dev/null +++ b/lightorch/csrc/include/lightorch/nn/cuda/fourier.cu @@ -0,0 +1 @@ +// Optimized implementation of the fourier layer \ No newline at end of file diff --git a/lightorch/csrc/include/lightorch/nn/cuda/gsn.cu b/lightorch/csrc/include/lightorch/nn/cuda/gsn.cu new file mode 100644 index 0000000..caa22e5 --- /dev/null +++ b/lightorch/csrc/include/lightorch/nn/cuda/gsn.cu @@ -0,0 +1 @@ +// Optimized implementation of Gram Schmidt Networks \ No newline at end of file diff --git a/lightorch/csrc/include/lightorch/nn/cuda/partial.cu b/lightorch/csrc/include/lightorch/nn/cuda/partial.cu new file mode 100644 index 0000000..afa6fbd --- /dev/null +++ b/lightorch/csrc/include/lightorch/nn/cuda/partial.cu @@ -0,0 +1 @@ +// Optimized partial implementation diff --git a/lightorch/csrc/include/lightorch/nn/embedding.h b/lightorch/csrc/include/lightorch/nn/embedding.h index 7384197..d5cb837 100644 --- a/lightorch/csrc/include/lightorch/nn/embedding.h +++ b/lightorch/csrc/include/lightorch/nn/embedding.h @@ -1 +1,13 @@ -// Implementation for Patch Embedding \ No newline at end of file +// Implementation for Patch Embedding +#include + +namespace F = torch::nn::functional; + +at::Tensor patch_embed_2d () { + +} + +at::Tensor patch_embed_3d () { + +} + diff --git a/lightorch/csrc/include/lightorch/nn/fourier.h b/lightorch/csrc/include/lightorch/nn/fourier.h index 73d7ce7..8e47370 100644 --- a/lightorch/csrc/include/lightorch/nn/fourier.h +++ b/lightorch/csrc/include/lightorch/nn/fourier.h @@ -1 +1,90 @@ -// Optimized implementation of the fourier layer \ No newline at end of file +// Optimized implementation of the fourier layer +#include +#include +#include +#include + +namespace F = torch::nn::functional; + +// Defining the derivatives for the backward pass + + +// Defining the Backward pass +std::vector fourier_conv_backward( + const torch::Tensor& grad_output, + const torch::Tensor& input, + const torch::Tensor& weight, + const torch::Tensor& bias, + const bool& pre_fft, + const bool& post_ifft +) { + torch::Tensor grad_input, grad_weight, grad_bias, grad_output_fft; + + struct out = {}; + if (pre_fft) { + // Compute the derivative of the fourier transform + } + // Compute the default derivative + + if (post_ifft) { + // Compute the derivative of the inverse fourier transform + } +} + +// Defining the Forward pass +torch::Tensor fourier_conv_forward ( + at::Tensor& input, + at::Tensor& weight, + at::Tensor& bias, + const bool& pre_fft, + const bool& post_ifft +) { + at::Tensor out; + // Assertions + TORCH_CHECK(input.dim() >= 3 && input.dim() <= 5, 'Input must be over 3 dimensions (min 1d_conv: (batch, channel, size)), and under 5 dimensions (max 3d_conv: (batch, channel, frame, height, width))'); + TORCH_CHECK(input.dim() - 2 == weight.dim(), 'Input dimension must be equal to the weight dimension'); + TORCH_CHECK(bias.dim() == 1, 'Bias should be of size 1, don\'t reshape.'); + TORCH_INTERNAL_ASSERT(input.device().type() == at::DeviceType::CPU); + TORCH_INTERNAL_ASSERT(weight.device().type() == at::DeviceType::CPU); + TORCH_INTERNAL_ASSERT(bias.device().type() == at::DeviceType::CPU); + + // define the dimensionality + if (pre_fft || post_ifft) { + // Defining the dimensions for the fft and ifft + std::vector dim; + // Compute the fft of the input + switch (input.dim() - 2) { + case 1: const struct dim = {-1}; break; + case 2: const struct dim = {-1, -2}; break; + case 3: const struct dim = {-1, -2, -3}; break; + default: TORCH_CHECK(false, 'Unsupported in put dimension'); break; + } + + if (pre_ifft) { + // To Fourier space + input = torch::fft::fftn(input, {}, dim); + weight = torch::fft::fftn(weight, {}, dim); + bias = torch::fft::fftn(bias, {}, dim = {-1}); + } + } + + // Convolution in Fourier Space + out = input * weight; + + // Add the bias term + if (bias.defined()) { + out += bias.view(1, -1, 1, 1); + } + + if (post_ifft) { + // Compute the ifft + out = torch::fft::ifftn(out, {}, dim); + } + return out; +} + +PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { + m.def('fourier_conv_forward', &fourier_conv_forward, 'Fourier Convolutions Forward'), + m.def('fourier_conv_backward', &fourier_conv_backward, 'Fourier Convolutions Backward') +} + diff --git a/lightorch/csrc/include/lightorch/nn/gsn.h b/lightorch/csrc/include/lightorch/nn/gsn.h index caa22e5..67dfdaa 100644 --- a/lightorch/csrc/include/lightorch/nn/gsn.h +++ b/lightorch/csrc/include/lightorch/nn/gsn.h @@ -1 +1,27 @@ -// Optimized implementation of Gram Schmidt Networks \ No newline at end of file +// Optimized implementation of Gram Schmidt Networks +#include +#include +at::Tensor gsn() { + +} + +at::Tensor gram_schmidt (Layers) { + +} + + +// Defining the Frontibier inner product for linear transformation space +float inner (const at::Tensor& L_1, const at::Tensor& L_2) { + return torch::trace(torch::addmm(L_1.transpose({-1 , -2}).conj(), L_2)); +} + +// Defining the generalized norm for any linear space +float norm (const at::Tensor& L_1, const at::Tensor& L_2) { + return sqrt(inner(L_1, L_2)); +} + +// Defining the projection operation proj_u v +at::Tensor proj(const at::Tensor& u, const at::Tensor& v) { + return torch::mul((inner(v, u)/inner(u, u)), u); +} +