|
| 1 | +#include "infini_train/include/autograd/indexing.h" |
| 2 | + |
| 3 | +#include "glog/logging.h" |
| 4 | + |
| 5 | +#include "infini_train/include/dispatcher.h" |
| 6 | +#include "infini_train/include/tensor.h" |
| 7 | + |
| 8 | +namespace infini_train::autograd { |
| 9 | +std::vector<std::shared_ptr<Tensor>> Gather::Forward(const std::vector<std::shared_ptr<Tensor>> &input_tensors) { |
| 10 | + CHECK_EQ(input_tensors.size(), 2); |
| 11 | + const auto &input = input_tensors[0]; |
| 12 | + const auto &index = input_tensors[1]; |
| 13 | + |
| 14 | + auto device = input->GetDevice().type(); |
| 15 | + auto kernel = Dispatcher::Instance().GetKernel({device, "GatherForward"}); |
| 16 | + return {kernel.Call<std::shared_ptr<Tensor>>(input, index, dim_)}; |
| 17 | +} |
| 18 | + |
| 19 | +void Gather::SetupContext(const std::vector<std::shared_ptr<Tensor>> &input_tensors, |
| 20 | + const std::vector<std::shared_ptr<Tensor>> &) { |
| 21 | + const auto &input = input_tensors[0]; |
| 22 | + const auto &index = input_tensors[1]; |
| 23 | + input_dims_ = input->Dims(); |
| 24 | + saved_tensors_ = {index}; |
| 25 | +} |
| 26 | + |
| 27 | +std::vector<std::shared_ptr<Tensor>> Gather::Backward(const std::vector<std::shared_ptr<Tensor>> &grad_outputs) { |
| 28 | + CHECK_EQ(grad_outputs.size(), 1); |
| 29 | + const auto &grad_output = grad_outputs[0]; |
| 30 | + const auto &index = saved_tensors_[0]; |
| 31 | + |
| 32 | + auto device = grad_outputs[0]->GetDevice(); |
| 33 | + auto kernel = Dispatcher::Instance().GetKernel({device.type(), "GatherBackward"}); |
| 34 | + return {kernel.Call<std::shared_ptr<Tensor>>(grad_output, index, dim_, input_dims_), nullptr}; |
| 35 | +} |
| 36 | + |
| 37 | +std::vector<std::shared_ptr<Tensor>> Slice::Forward(const std::vector<std::shared_ptr<Tensor>> &input_tensors) { |
| 38 | + CHECK_EQ(input_tensors.size(), 1); |
| 39 | + const auto &input = input_tensors[0]; |
| 40 | + |
| 41 | + auto device = input->GetDevice().type(); |
| 42 | + return { |
| 43 | + Dispatcher::Instance().Call<std::shared_ptr<Tensor>>({device, "SliceForward"}, input, starts_, ends_, steps_)}; |
| 44 | +} |
| 45 | + |
| 46 | +void Slice::SetupContext(const std::vector<std::shared_ptr<Tensor>> &input_tensors, |
| 47 | + const std::vector<std::shared_ptr<Tensor>> &) { |
| 48 | + // FIXME(dcj): only input's dim need to be saved |
| 49 | + const auto &input = input_tensors[0]; |
| 50 | + saved_tensors_ = {input}; |
| 51 | +} |
| 52 | + |
| 53 | +std::vector<std::shared_ptr<Tensor>> Slice::Backward(const std::vector<std::shared_ptr<Tensor>> &grad_outputs) { |
| 54 | + CHECK_EQ(saved_tensors_.size(), 1); |
| 55 | + const auto &input = saved_tensors_[0]; |
| 56 | + const auto &grad_output = grad_outputs[0]; |
| 57 | + |
| 58 | + auto device = input->GetDevice().type(); |
| 59 | + return {Dispatcher::Instance().Call<std::shared_ptr<Tensor>>({device, "SliceBackward"}, grad_output, input, starts_, |
| 60 | + ends_, steps_)}; |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace infini_train::autograd |
0 commit comments