Currently, i am working on buffer solution which looks like this. Written in C++, because I got error message:
Returning Variables sharing storage with other Variables that require grad is not supported in Python functions. Please submit a feature request if you hit this error
However, when calling this function from C++ binding, i get same error message.
class BufferedCatFunction : public torch::autograd::Function<BufferedCatFunction> {
public:
    static Tensor forward(AutogradContext* ctx, Tensor buffer, std::vector<Tensor> tensors) {
        int64_t shape_1 = 0;
        std::vector<int64_t> sizes;
        for (const auto &tensor : tensors) {
            sizes.emplace_back(tensor.size(1));
            shape_1 += tensor.size(1);
        }
        int64_t shape_0 = tensors[0].size(0);
        auto result = buffer.narrow(0, 0, shape_0 * shape_1).view({shape_0, shape_1});
        result.set_requires_grad(true);
        torch::cat_out(result, tensors, 1);
        ctx->save_for_backward(tensors);
        return result;
    }

    static variable_list backward(
            AutogradContext* ctx,
            variable_list grad_output) {
        auto &grad = grad_output[0];
        auto saved = ctx->get_saved_variables();

        int64_t cur_offset = 0;
        torch::autograd::variable_list gradients;
        for (auto &tensor : saved) {
            auto size = tensor.size(1);
            gradients.emplace_back(grad.narrow(1, cur_offset, size));
            cur_offset += size;
        }

        return gradients;
    }
};


Tensor cat_inside_cpp_function(std::vector<Tensor> tensors, Tensor buffer) {
    return BufferedCatFunction::apply(buffer, tensors);
}

Here is python code:
if __name__ == '__main__':
    linears = [
        torch.nn.Linear(2, 1),
        torch.nn.Linear(3, 1),
        torch.nn.Linear(4, 1)
    ]

    with torch.autograd.detect_anomaly():

        inputs = torch.rand(3, 2)
        inputs.requires_grad = False
        concated1 = buffered_densenet_cpp.cat_inside_cpp([inputs], BUFFER)
        print(concated1)

        out1 = linears[0](concated1)
        concated2 = buffered_densenet_cpp.cat_inside_cpp([inputs, out1], BUFFER)
        print(concated2)

        concated2.sum().backward()

How can I avoid this shared storage error message and use buffer? All tensors, referring to same storage, are available (or i think they are available) in autograd and backward pass could be computed.