chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# Copyright (c) Facebook, Inc. and its affiliates.
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from .dynamicconv_layer import DynamicconvLayer # noqa
|
||||
@@ -0,0 +1,223 @@
|
||||
# Copyright (c) Facebook, Inc. and its affiliates.
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
|
||||
def gen_forward():
|
||||
|
||||
kernels = [3, 5, 7, 15, 31, 63, 127, 255]
|
||||
blocks = [32, 64, 128, 256]
|
||||
|
||||
head = """
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "dynamicconv_cuda.cuh"
|
||||
|
||||
std::vector<at::Tensor> dynamicconv_cuda_forward(at::Tensor input, at::Tensor weight, int padding_l) {
|
||||
|
||||
at::DeviceGuard g(input.device());
|
||||
const auto minibatch = input.size(0);
|
||||
const auto numFeatures = input.size(1);
|
||||
const auto sequenceLength = input.size(2);
|
||||
|
||||
const auto numHeads = weight.size(1);
|
||||
const auto filterSize = weight.size(2);
|
||||
|
||||
const auto numFiltersInBlock = numFeatures / numHeads;
|
||||
const dim3 blocks(minibatch, numFeatures);
|
||||
|
||||
auto output = at::zeros_like(input);
|
||||
auto stream = at::cuda::getCurrentCUDAStream();
|
||||
"""
|
||||
|
||||
switch = """
|
||||
switch(filterSize) {
|
||||
"""
|
||||
|
||||
case_k = """
|
||||
case {k}:
|
||||
"""
|
||||
|
||||
main_block = """
|
||||
if (padding_l == {pad}) {{
|
||||
AT_DISPATCH_FLOATING_TYPES_AND_HALF(input.scalar_type(), "dynamicconv_forward", ([&] {{
|
||||
dynamicconv_forward_kernel<{k}, {b_size}, {pad}, scalar_t>
|
||||
<<<blocks, {b_size}, 0, stream>>>(
|
||||
input.data<scalar_t>(),
|
||||
weight.data<scalar_t>(),
|
||||
minibatch,
|
||||
sequenceLength,
|
||||
numFeatures,
|
||||
numFiltersInBlock,
|
||||
numHeads,
|
||||
output.data<scalar_t>());
|
||||
}}));
|
||||
}} else
|
||||
"""
|
||||
|
||||
bad_padding = """
|
||||
{
|
||||
std::cout << "WARNING: Unsupported padding size - skipping forward pass" << std::endl;
|
||||
}
|
||||
break;\n
|
||||
"""
|
||||
|
||||
end = """
|
||||
default:
|
||||
std::cout << "WARNING: Unsupported filter length passed - skipping forward pass" << std::endl;
|
||||
}
|
||||
|
||||
return {output};
|
||||
}
|
||||
"""
|
||||
|
||||
with open("dynamicconv_cuda_forward.cu", "w") as forward:
|
||||
forward.write(head)
|
||||
forward.write(switch)
|
||||
for k in kernels:
|
||||
b_size = 32
|
||||
for b in blocks:
|
||||
if b > k:
|
||||
b_size = b
|
||||
break
|
||||
forward.write(case_k.format(k=k))
|
||||
for pad in [k // 2, k - 1]:
|
||||
forward.write(main_block.format(k=k, b_size=b_size, pad=pad))
|
||||
forward.write(bad_padding)
|
||||
forward.write(end)
|
||||
|
||||
|
||||
def gen_backward():
|
||||
|
||||
kernels = [3, 5, 7, 15, 31, 63, 127, 255]
|
||||
thresh = [512, 512, 512, 512, 512, 380, 256, 256]
|
||||
min_block = [64, 64, 64, 64, 64, 64, 128, 256]
|
||||
seqs = [32 * x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]
|
||||
|
||||
head = """
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "dynamicconv_cuda.cuh"
|
||||
|
||||
std::vector<at::Tensor> dynamicconv_cuda_backward(at::Tensor gradOutput, int padding_l, at::Tensor input, at::Tensor weight) {
|
||||
|
||||
at::DeviceGuard g(input.device());
|
||||
const auto minibatch = input.size(0);
|
||||
const auto numFeatures = input.size(1);
|
||||
const auto sequenceLength = input.size(2);
|
||||
|
||||
const auto numHeads = weight.size(1);
|
||||
const auto filterSize = weight.size(2);
|
||||
|
||||
const auto numFiltersInBlock = numFeatures / numHeads;
|
||||
auto numChunks = 1;
|
||||
|
||||
auto gradInput = at::zeros_like(input);
|
||||
auto gradWeight = at::zeros_like(weight);
|
||||
auto stream = at::cuda::getCurrentCUDAStream();
|
||||
|
||||
dim3 blocks(minibatch, numHeads, numChunks);
|
||||
"""
|
||||
|
||||
sequence_if = """
|
||||
if (sequenceLength < {seq}) {{
|
||||
switch(filterSize) {{
|
||||
"""
|
||||
|
||||
case_k = """
|
||||
case {k}:
|
||||
"""
|
||||
|
||||
chunks_reset = """
|
||||
numChunks = int(ceilf(sequenceLength/float({b_size})));
|
||||
blocks = dim3(minibatch, numHeads, numChunks);
|
||||
"""
|
||||
|
||||
main_block = """
|
||||
if (padding_l == {p}) {{
|
||||
AT_DISPATCH_FLOATING_TYPES_AND_HALF(gradOutput.scalar_type(), "dynamicconv_backward", ([&] {{
|
||||
dynamicconv_backward_kernel<{k}, {b_size}, {p}, scalar_t>
|
||||
<<<blocks, {b_size}, 0, stream>>>(
|
||||
gradOutput.data<scalar_t>(),
|
||||
input.data<scalar_t>(),
|
||||
weight.data<scalar_t>(),
|
||||
minibatch,
|
||||
sequenceLength,
|
||||
numFeatures,
|
||||
numFiltersInBlock,
|
||||
numHeads,
|
||||
gradWeight.data<scalar_t>(),
|
||||
gradInput.data<scalar_t>());
|
||||
}}));
|
||||
}} else
|
||||
"""
|
||||
|
||||
bad_padding = """
|
||||
{
|
||||
std::cout << "WARNING: Unsupported padding size - skipping backward pass" << std::endl;
|
||||
}
|
||||
break;\n
|
||||
"""
|
||||
|
||||
bad_filter = """
|
||||
default:
|
||||
std::cout << "WARNING: Unsupported filter length passed - skipping backward pass" << std::endl;
|
||||
}
|
||||
"""
|
||||
|
||||
con_else = """
|
||||
} else
|
||||
"""
|
||||
|
||||
final_else = """
|
||||
{
|
||||
switch(filterSize) {
|
||||
"""
|
||||
|
||||
last_return = """
|
||||
}
|
||||
return {gradInput, gradWeight};
|
||||
}
|
||||
"""
|
||||
|
||||
with open("dynamicconv_cuda_backward.cu", "w") as backward:
|
||||
backward.write(head)
|
||||
for seq in seqs:
|
||||
backward.write(sequence_if.format(seq=seq))
|
||||
for k, t, m in zip(kernels, thresh, min_block):
|
||||
backward.write(case_k.format(k=k))
|
||||
if seq <= t:
|
||||
b_size = seq
|
||||
else:
|
||||
b_size = m
|
||||
backward.write(chunks_reset.format(b_size=b_size))
|
||||
for p in [k // 2, k - 1]:
|
||||
backward.write(main_block.format(k=k, b_size=b_size, p=p))
|
||||
backward.write(bad_padding)
|
||||
backward.write(bad_filter)
|
||||
backward.write(con_else)
|
||||
backward.write(final_else)
|
||||
for k, m in zip(kernels, min_block):
|
||||
backward.write(case_k.format(k=k))
|
||||
backward.write(chunks_reset.format(b_size=m))
|
||||
for p in [k // 2, k - 1]:
|
||||
backward.write(main_block.format(k=k, b_size=m, p=p))
|
||||
backward.write(bad_padding)
|
||||
backward.write(bad_filter)
|
||||
backward.write(last_return)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
gen_forward()
|
||||
gen_backward()
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include <torch/extension.h>
|
||||
#include <vector>
|
||||
|
||||
std::vector<at::Tensor> dynamicconv_cuda_forward(
|
||||
at::Tensor input,
|
||||
at::Tensor filters,
|
||||
int padding_l);
|
||||
|
||||
std::vector<at::Tensor> dynamicconv_cuda_backward(
|
||||
at::Tensor gradOutput,
|
||||
int padding_l,
|
||||
at::Tensor input,
|
||||
at::Tensor filters);
|
||||
|
||||
|
||||
#define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor")
|
||||
#define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous")
|
||||
#define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x)
|
||||
|
||||
std::vector<at::Tensor> dynamicconv_forward(
|
||||
at::Tensor input,
|
||||
at::Tensor filters,
|
||||
int padding_l) {
|
||||
|
||||
CHECK_INPUT(input);
|
||||
CHECK_INPUT(filters);
|
||||
|
||||
return dynamicconv_cuda_forward(input, filters,
|
||||
padding_l);
|
||||
}
|
||||
|
||||
std::vector<at::Tensor> dynamicconv_backward(
|
||||
at::Tensor gradOutput,
|
||||
int padding_l,
|
||||
at::Tensor input,
|
||||
at::Tensor filters) {
|
||||
|
||||
CHECK_INPUT(gradOutput);
|
||||
CHECK_INPUT(input);
|
||||
CHECK_INPUT(filters);
|
||||
|
||||
return dynamicconv_cuda_backward(gradOutput, padding_l,
|
||||
input, filters);
|
||||
}
|
||||
|
||||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
||||
m.def("forward", &dynamicconv_forward, "dynamicconv forward (CUDA)");
|
||||
m.def("backward", &dynamicconv_backward, "dynamicconv backward (CUDA)");
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include <ATen/ATen.h>
|
||||
#include <c10/cuda/CUDAStream.h>
|
||||
|
||||
#include <cuda.h>
|
||||
#include <cuda_fp16.h>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
|
||||
#define SHFL_MASK 0xffffffff
|
||||
|
||||
template<int FS, int SB, int padding_l, typename scalar_t>
|
||||
__global__
|
||||
void dynamicconv_forward_kernel(const scalar_t* input,
|
||||
const scalar_t* weight,
|
||||
int minibatch,
|
||||
int sequenceLength,
|
||||
int numFeatures,
|
||||
int numFiltersInBlock,
|
||||
int numHeads,
|
||||
scalar_t* output);
|
||||
|
||||
template<int FS, int SB, int padding_l, typename scalar_t>
|
||||
__global__
|
||||
void dynamicconv_backward_kernel(
|
||||
const scalar_t* gradOutput, // B * C * T
|
||||
const scalar_t* input, // B * C * T
|
||||
const scalar_t* weight,
|
||||
int minibatch,
|
||||
int sequenceLength,
|
||||
int numFeatures,
|
||||
int numFiltersInBlock,
|
||||
int numHeads,
|
||||
scalar_t* gradWeight,
|
||||
scalar_t* gradInput); // B * H * k * T
|
||||
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "dynamicconv_cuda.cuh"
|
||||
#include "dynamicconv_cuda_forward.cu"
|
||||
#include "dynamicconv_cuda_backward.cu"
|
||||
#include "../cuda_utils.cu"
|
||||
|
||||
// FS is filter size and kernels are specialized for filter sizes
|
||||
template<int FS, int SB, int padding_l, typename scalar_t>
|
||||
__global__
|
||||
void dynamicconv_forward_kernel(const scalar_t* input,
|
||||
const scalar_t* weight,
|
||||
int minibatch,
|
||||
int sequenceLength,
|
||||
int numFeatures,
|
||||
int numFiltersInBlock,
|
||||
int numHeads,
|
||||
scalar_t* output) {
|
||||
assert(blockDim.x == SB);
|
||||
|
||||
const int tid = threadIdx.x;
|
||||
const int batchIdx = blockIdx.x;
|
||||
const int featureIdx = blockIdx.y;
|
||||
const int head = featureIdx / numFiltersInBlock;
|
||||
|
||||
const int IOOffset = batchIdx * numFeatures * sequenceLength
|
||||
+ featureIdx * sequenceLength;
|
||||
const scalar_t* inputFeature = &input[IOOffset];
|
||||
scalar_t* outputFeature = &output[IOOffset];
|
||||
|
||||
scalar_t filter[FS];
|
||||
|
||||
__shared__ scalar_t tempInput[SB + FS];
|
||||
zeroSharedMem<FS, SB, padding_l>(tempInput);
|
||||
|
||||
const int numIterations = divUp<int, int>(sequenceLength, SB);
|
||||
|
||||
for (int i = 0; i < numIterations; ++i) {
|
||||
__syncthreads();
|
||||
const int inputOffset = i * SB;
|
||||
load_input_to_shared<FS, SB, padding_l>(inputFeature, inputOffset,
|
||||
sequenceLength, i,
|
||||
numIterations, false, tempInput);
|
||||
__syncthreads();
|
||||
if (inputOffset + tid < sequenceLength) {
|
||||
|
||||
#pragma unroll
|
||||
for (int k = 0; k < FS; ++k) {
|
||||
const int filterOffset = batchIdx * numHeads * FS * sequenceLength
|
||||
+ head * FS * sequenceLength
|
||||
+ k * sequenceLength
|
||||
+ i * SB + tid;
|
||||
filter[k] = weight[filterOffset];
|
||||
}
|
||||
|
||||
scalar_t out = scalar_t(0.0);
|
||||
#pragma unroll
|
||||
for (int k = 0; k < FS; ++k) {
|
||||
out += filter[k] * tempInput[tid + k];
|
||||
}
|
||||
|
||||
outputFeature[inputOffset + tid] = out;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<int FS, int SB, int padding_l, typename scalar_t>
|
||||
__global__
|
||||
void dynamicconv_backward_kernel(
|
||||
const scalar_t* gradOutput, // B * C * T
|
||||
const scalar_t* input, // B * C * T
|
||||
const scalar_t* weight,
|
||||
int minibatch,
|
||||
int sequenceLength,
|
||||
int numFeatures,
|
||||
int numFiltersInBlock,
|
||||
int numHeads,
|
||||
scalar_t* gradWeight,
|
||||
scalar_t* gradInput) { // B * H * k * T
|
||||
|
||||
assert(blockDim.x == SB);
|
||||
|
||||
// each block operates on a single batch and filter head
|
||||
const int tid = threadIdx.x;
|
||||
const int batchIdx = blockIdx.x;
|
||||
const int headIdx = blockIdx.y;
|
||||
const int chunkIdx = blockIdx.z;
|
||||
|
||||
const int numChunks = divUp<int, int>(sequenceLength, SB);
|
||||
const int inputOffset = chunkIdx * SB;
|
||||
|
||||
// initialize shared memory for output gradient and input
|
||||
__shared__ scalar_t tempGradOutput[SB + FS];
|
||||
__shared__ scalar_t tempInput[SB + FS];
|
||||
const int padding = FS - padding_l - 1;
|
||||
|
||||
zeroSharedMem<FS, SB, padding>(tempGradOutput);
|
||||
zeroSharedMem<FS, SB, padding_l>(tempInput);
|
||||
|
||||
// initialize local filter and weight gradient sum arrays
|
||||
scalar_t tempGradSum[FS];
|
||||
scalar_t bfilter[FS];
|
||||
for (int k = 0; k < FS; ++k) {
|
||||
tempGradSum[k] = scalar_t(0.0);
|
||||
|
||||
int idxOffset = inputOffset + tid + k - padding;
|
||||
if (idxOffset >= 0 && idxOffset < sequenceLength) {
|
||||
int bfilterOffset = batchIdx * numHeads * FS * sequenceLength
|
||||
+ headIdx * FS * sequenceLength
|
||||
+ (FS - k - 1) * sequenceLength
|
||||
+ idxOffset;
|
||||
bfilter[k] = weight[bfilterOffset];
|
||||
} else {
|
||||
bfilter[k] = scalar_t(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// iterate over filter block
|
||||
for (int featureIdx = 0; featureIdx < numFiltersInBlock; ++featureIdx) {
|
||||
__syncthreads();
|
||||
|
||||
// load input and output gradient for this channel and chunk
|
||||
const int IOOffset = batchIdx * numFeatures * sequenceLength
|
||||
+ (headIdx * numFiltersInBlock + featureIdx) * sequenceLength;
|
||||
const scalar_t* inputFeature = &input[IOOffset];
|
||||
const scalar_t* gradOutputFeature = &gradOutput[IOOffset];
|
||||
scalar_t* gradInputFeature = &gradInput[IOOffset];
|
||||
|
||||
load_input_to_shared<FS, SB, padding>(gradOutputFeature, inputOffset,
|
||||
sequenceLength, chunkIdx,
|
||||
numChunks, true, tempGradOutput);
|
||||
load_input_to_shared<FS, SB, padding_l>(inputFeature, inputOffset,
|
||||
sequenceLength, chunkIdx,
|
||||
numChunks, true, tempInput);
|
||||
__syncthreads();
|
||||
|
||||
// sum input and weight gradients
|
||||
scalar_t out = scalar_t(0.0);
|
||||
#pragma unroll
|
||||
for (int k = 0; k < FS; ++k) {
|
||||
tempGradSum[k] += tempInput[tid + k] * tempGradOutput[tid + padding];
|
||||
out += bfilter[k] * tempGradOutput[tid + k];
|
||||
}
|
||||
|
||||
if (inputOffset + tid < sequenceLength) {
|
||||
gradInputFeature[inputOffset + tid] = out;
|
||||
}
|
||||
}
|
||||
|
||||
const int gradOffset = batchIdx * numHeads * FS * sequenceLength
|
||||
+ headIdx * FS * sequenceLength;
|
||||
scalar_t *gradWeightFeature = &gradWeight[gradOffset];
|
||||
|
||||
// write weight gradient
|
||||
if (inputOffset + tid < sequenceLength) {
|
||||
for (int k = 0; k < FS; ++k) {
|
||||
const int outputOffset = k * sequenceLength + inputOffset + tid;
|
||||
gradWeightFeature[outputOffset] = tempGradSum[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
# Copyright (c) Facebook, Inc. and its affiliates.
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
import dynamicconv_cuda
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from fairseq import utils
|
||||
from fairseq.incremental_decoding_utils import with_incremental_state
|
||||
from fairseq.modules.fairseq_dropout import FairseqDropout
|
||||
from fairseq.modules.unfold import unfold1d
|
||||
from torch import nn
|
||||
from torch.autograd import Function
|
||||
|
||||
|
||||
class dynamicconvFunction(Function):
|
||||
@staticmethod
|
||||
def forward(ctx, x, weights, padding_l):
|
||||
ctx.padding_l = padding_l
|
||||
outputs = dynamicconv_cuda.forward(x, weights, padding_l)
|
||||
variables = [x, weights]
|
||||
ctx.save_for_backward(*variables)
|
||||
return outputs[0]
|
||||
|
||||
@staticmethod
|
||||
def backward(ctx, grad_output):
|
||||
outputs = dynamicconv_cuda.backward(
|
||||
grad_output.contiguous(), ctx.padding_l, *ctx.saved_tensors
|
||||
)
|
||||
grad_input, grad_weights = outputs
|
||||
return grad_input, grad_weights, None
|
||||
|
||||
|
||||
@with_incremental_state
|
||||
class DynamicconvLayer(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
input_size,
|
||||
kernel_size=1,
|
||||
padding_l=None,
|
||||
weight_softmax=False,
|
||||
num_heads=1,
|
||||
weight_dropout=0.0,
|
||||
bias=False,
|
||||
renorm_padding=False,
|
||||
conv_bias=False,
|
||||
query_size=None,
|
||||
):
|
||||
|
||||
super(DynamicconvLayer, self).__init__()
|
||||
self.input_size = input_size
|
||||
self.query_size = input_size if query_size is None else query_size
|
||||
self.kernel_size = kernel_size
|
||||
self.padding_l = padding_l
|
||||
self.num_heads = num_heads
|
||||
self.weight_softmax = weight_softmax
|
||||
self.weight_dropout_module = FairseqDropout(
|
||||
weight_dropout, module_name=self.__class__.__name__
|
||||
)
|
||||
self.renorm_padding = renorm_padding
|
||||
self.bias = bias
|
||||
|
||||
self.weight_linear = nn.Linear(input_size, num_heads * kernel_size, bias)
|
||||
if conv_bias:
|
||||
self.conv_bias = nn.Parameter(torch.Tensor(input_size))
|
||||
else:
|
||||
self.conv_bias = None
|
||||
self.reset_parameters()
|
||||
|
||||
def reset_parameters(self):
|
||||
nn.init.xavier_uniform_(self.weight_linear.weight)
|
||||
if self.conv_bias is not None:
|
||||
nn.init.constant_(self.conv_bias, 0.0)
|
||||
nn.init.constant_(self.weight_linaer.bias, 0.0)
|
||||
|
||||
def forward(self, x, incremental_state=None, query=None, unfold=None):
|
||||
|
||||
T, B, C = x.size()
|
||||
K, H = self.kernel_size, self.num_heads
|
||||
# R = C // H
|
||||
|
||||
# during inference time, incremental BMM is faster
|
||||
if incremental_state is not None:
|
||||
unfold = (
|
||||
x.size(0) > 512 if unfold is None else unfold
|
||||
) # use unfold mode as default for long sequence to save memory
|
||||
unfold = unfold or (incremental_state is not None)
|
||||
assert query is None
|
||||
|
||||
if query is None:
|
||||
query = x
|
||||
if unfold:
|
||||
output = self._forward_unfolded(x, incremental_state, query)
|
||||
else:
|
||||
output = self._forward_expanded(x, incremental_state, query)
|
||||
|
||||
if self.conv_bias is not None:
|
||||
output = output + self.conv_bias.view(1, 1, -1)
|
||||
|
||||
return output
|
||||
|
||||
# during training time, use CUDA kernel
|
||||
else:
|
||||
weight = self.weight_linear(x).view(T, B, H, K)
|
||||
if self.weight_softmax:
|
||||
weight = F.softmax(weight, dim=-1)
|
||||
if self.weight_dropout_module.p:
|
||||
weight = self.weight_dropout_module(weight)
|
||||
|
||||
weight = weight.permute(1, 2, 3, 0).contiguous()
|
||||
self.filters = weight
|
||||
x = x.permute(1, 2, 0).contiguous()
|
||||
output = dynamicconvFunction.apply(x, weight, self.padding_l).permute(
|
||||
2, 0, 1
|
||||
)
|
||||
if self.conv_bias is not None:
|
||||
output = output + self.conv_bias.view(1, 1, -1)
|
||||
return output
|
||||
|
||||
def reorder_incremental_state(self, incremental_state, new_order):
|
||||
input_buffer = self._get_input_buffer(incremental_state)
|
||||
if input_buffer is not None:
|
||||
input_buffer = input_buffer.index_select(1, new_order)
|
||||
self._set_input_buffer(incremental_state, input_buffer)
|
||||
|
||||
def _get_input_buffer(self, incremental_state):
|
||||
return utils.get_incremental_state(self, incremental_state, "input_buffer")
|
||||
|
||||
def _set_input_buffer(self, incremental_state, new_buffer):
|
||||
return utils.set_incremental_state(
|
||||
self, incremental_state, "input_buffer", new_buffer
|
||||
)
|
||||
|
||||
def _forward_unfolded(self, x, incremental_state, query):
|
||||
"""The conventional implementation of convolutions.
|
||||
Unfolding the input by having a window shifting to the right."""
|
||||
T, B, C = x.size()
|
||||
K, H = self.kernel_size, self.num_heads
|
||||
R = C // H
|
||||
assert R * H == C == self.input_size
|
||||
|
||||
weight = self.weight_linear(query).view(T * B * H, -1)
|
||||
|
||||
# renorm_padding is only implemented in _forward_expanded
|
||||
assert not self.renorm_padding or incremental_state is not None
|
||||
|
||||
if incremental_state is not None:
|
||||
input_buffer = self._get_input_buffer(incremental_state)
|
||||
if input_buffer is None:
|
||||
input_buffer = x.new()
|
||||
x_unfold = torch.cat([input_buffer, x.unsqueeze(3)], dim=3)
|
||||
if self.kernel_size > 1:
|
||||
self._set_input_buffer(
|
||||
incremental_state, x_unfold[:, :, :, -self.kernel_size + 1 :]
|
||||
)
|
||||
x_unfold = x_unfold.view(T * B * H, R, -1)
|
||||
else:
|
||||
padding_l = self.padding_l
|
||||
if K > T and padding_l == K - 1:
|
||||
weight = weight.narrow(1, K - T, T)
|
||||
K, padding_l = T, T - 1
|
||||
# unfold the input: T x B x C --> T' x B x C x K
|
||||
x_unfold = unfold1d(x, K, padding_l, 0)
|
||||
x_unfold = x_unfold.view(T * B * H, R, K)
|
||||
|
||||
if self.weight_softmax and not self.renorm_padding:
|
||||
weight = F.softmax(weight, dim=1)
|
||||
weight = weight.narrow(1, 0, K)
|
||||
|
||||
if incremental_state is not None:
|
||||
weight = weight[:, -x_unfold.size(2) :]
|
||||
K = weight.size(1)
|
||||
|
||||
if self.weight_softmax and self.renorm_padding:
|
||||
weight = F.softmax(weight, dim=1)
|
||||
|
||||
weight = self.weight_dropout_module(weight, inplace=False)
|
||||
|
||||
output = torch.bmm(x_unfold, weight.unsqueeze(2)) # T*B*H x R x 1
|
||||
output = output.view(T, B, C)
|
||||
return output
|
||||
|
||||
def _forward_expanded(self, x, incremental_stat, query):
|
||||
"""Turn the convolution filters into band matrices and do matrix multiplication.
|
||||
This is faster when the sequence is short, but less memory efficient.
|
||||
This is not used in the decoder during inference.
|
||||
"""
|
||||
T, B, C = x.size()
|
||||
K, H = self.kernel_size, self.num_heads
|
||||
R = C // H
|
||||
assert R * H == C == self.input_size
|
||||
weight = self.weight_linear(query).view(T * B * H, -1)
|
||||
|
||||
if not self.renorm_padding:
|
||||
if self.weight_softmax:
|
||||
weight = F.softmax(weight, dim=1)
|
||||
weight = self.weight_dropout_module(weight, inplace=False)
|
||||
weight = weight.narrow(1, 0, K).contiguous()
|
||||
weight = weight.view(T, B * H, K).transpose(0, 1)
|
||||
|
||||
x = x.view(T, B * H, R).transpose(0, 1)
|
||||
if self.weight_softmax and self.renorm_padding:
|
||||
# turn the convolution filters into band matrices
|
||||
weight_expanded = weight.new(B * H, T, T + K - 1).fill_(float("-inf"))
|
||||
weight_expanded.as_strided(
|
||||
(B * H, T, K), (T * (T + K - 1), T + K, 1)
|
||||
).copy_(weight)
|
||||
weight_expanded = weight_expanded.narrow(2, self.padding_l, T)
|
||||
# normalize the weight over valid positions like self-attention
|
||||
weight_expanded = F.softmax(weight_expanded, dim=2)
|
||||
weight_expanded = self.weight_dropout_module(weight_expanded, inplace=False)
|
||||
else:
|
||||
P = self.padding_l
|
||||
# For efficiency, we cut the kernel size and reduce the padding when the kernel is larger than the length
|
||||
if K > T and P == K - 1:
|
||||
weight = weight.narrow(2, K - T, T)
|
||||
K, P = T, T - 1
|
||||
# turn the convolution filters into band matrices
|
||||
weight_expanded = weight.new_zeros(B * H, T, T + K - 1, requires_grad=False)
|
||||
weight_expanded.as_strided(
|
||||
(B * H, T, K), (T * (T + K - 1), T + K, 1)
|
||||
).copy_(weight)
|
||||
weight_expanded = weight_expanded.narrow(2, P, T) # B*H x T x T
|
||||
output = torch.bmm(weight_expanded, x)
|
||||
output = output.transpose(0, 1).contiguous().view(T, B, C)
|
||||
return output
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <torch/torch.h>
|
||||
#include <vector>
|
||||
|
||||
std::vector<float*> dynamicconv_cpu_forward(
|
||||
float* input,
|
||||
float* filters,
|
||||
int padding_l);
|
||||
|
||||
std::vector<float*> dynamicconv_cpu_backward(
|
||||
float* gradOutput,
|
||||
int padding_l,
|
||||
float* input,
|
||||
float* filters);
|
||||
|
||||
std::vector<float*> dynamicconv_forward(
|
||||
float* input,
|
||||
float* filters,
|
||||
int padding_l) {
|
||||
|
||||
return dynamicconv_cpu_forward(input, filters, padding_l);
|
||||
}
|
||||
|
||||
std::vector<float*> dynamicconv_backward(
|
||||
float* gradOutput,
|
||||
int padding_l,
|
||||
float* input,
|
||||
float* filters) {
|
||||
|
||||
return dynamicconv_cpu_backward(gradOutput, padding_l, input, filters);
|
||||
}
|
||||
|
||||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
||||
m.def("forward", &dynamicconv_forward, "dynamicconv forward (CPU)");
|
||||
m.def("backward", &dynamicconv_backward, "dynamicconv backward (CPU)");
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) Facebook, Inc. and its affiliates.
|
||||
#
|
||||
# This source code is licensed under the MIT license found in the
|
||||
# LICENSE file in the root directory of this source tree.
|
||||
|
||||
from setuptools import setup
|
||||
from torch.utils.cpp_extension import BuildExtension, CUDAExtension
|
||||
|
||||
|
||||
setup(
|
||||
name="dynamicconv_layer",
|
||||
ext_modules=[
|
||||
CUDAExtension(
|
||||
name="dynamicconv_cuda",
|
||||
sources=[
|
||||
"dynamicconv_cuda.cpp",
|
||||
"dynamicconv_cuda_kernel.cu",
|
||||
],
|
||||
),
|
||||
],
|
||||
cmdclass={"build_ext": BuildExtension},
|
||||
)
|
||||
Reference in New Issue
Block a user