|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <pybind11/pybind11.h> |
| 10 | +#include <pybind11/stl.h> |
| 11 | +#include <memory> |
| 12 | +#include <stack> |
| 13 | + |
| 14 | +#include <ATen/Tensor.h> |
| 15 | +#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
| 16 | +#include <torch/csrc/utils/pybind.h> |
| 17 | +#include "executorch/extension/tensor/tensor.h" |
| 18 | +#include "executorch/extension/training/module/training_module.h" |
| 19 | +#include "executorch/extension/training/optimizer/sgd.h" |
| 20 | +#ifndef USE_ATEN_LIB |
| 21 | +#include <c10/core/impl/LocalDispatchKeySet.h> |
| 22 | +#include <executorch/extension/aten_util/aten_bridge.h> |
| 23 | +#endif |
| 24 | + |
| 25 | +namespace py = pybind11; |
| 26 | + |
| 27 | +namespace executorch { |
| 28 | +namespace extension { |
| 29 | +namespace training { |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +struct PySGD final { |
| 34 | + explicit PySGD( |
| 35 | + const py::dict& named_params, |
| 36 | + double lr, |
| 37 | + double momentum, |
| 38 | + double dampening, |
| 39 | + double weight_decay, |
| 40 | + bool nesterov) |
| 41 | + : sgd_(nullptr), |
| 42 | + fqns_() |
| 43 | +#ifndef USE_ATEN_LIB |
| 44 | + , |
| 45 | + params_() |
| 46 | +#endif |
| 47 | + { |
| 48 | + std::map<exec_aten::string_view, exec_aten::Tensor> cpp_inputs; |
| 49 | + auto py_named_params = |
| 50 | + py::cast<std::unordered_map<std::string, at::Tensor>>(named_params); |
| 51 | + const auto params_size = py::len(named_params); |
| 52 | + fqns_ = std::vector<std::string>(); |
| 53 | + fqns_.reserve(params_size); |
| 54 | + |
| 55 | + for (auto pair : py_named_params) { |
| 56 | + fqns_.push_back(pair.first); |
| 57 | + exec_aten::string_view v{fqns_.back().c_str(), pair.first.size()}; |
| 58 | +#ifndef USE_ATEN_LIB |
| 59 | + // convert at::Tensor to torch::executor::Tensor |
| 60 | + params_.emplace_back(alias_tensor_ptr_to_attensor(pair.second)); |
| 61 | + cpp_inputs.insert({v, *params_.back()}); |
| 62 | +#else |
| 63 | + cpp_inputs.insert({v, pair.second}); |
| 64 | +#endif |
| 65 | + } |
| 66 | + sgd_ = std::make_unique<optimizer::SGD>( |
| 67 | + cpp_inputs, |
| 68 | + extension::training::optimizer::SGDOptions( |
| 69 | + lr, momentum, dampening, weight_decay, nesterov)); |
| 70 | + } |
| 71 | + |
| 72 | + PySGD(const PySGD&) = delete; |
| 73 | + PySGD& operator=(const PySGD&) = delete; |
| 74 | + PySGD(PySGD&&) = default; |
| 75 | + PySGD& operator=(PySGD&&) = default; |
| 76 | + |
| 77 | + void step(const py::dict& py_dict) { |
| 78 | + auto py_named_gradients = |
| 79 | + py::cast<std::unordered_map<std::string, at::Tensor>>(py_dict); |
| 80 | + const auto inputs_size = py::len(py_dict); |
| 81 | + std::map<exec_aten::string_view, exec_aten::Tensor> cpp_inputs; |
| 82 | + |
| 83 | + std::vector<std::string> fqn; |
| 84 | +#ifndef USE_ATEN_LIB |
| 85 | + std::vector<TensorPtr> et_tensors; |
| 86 | +#endif |
| 87 | + |
| 88 | + // Convert python objects into cpp. |
| 89 | + for (const auto& pair : py_named_gradients) { |
| 90 | + fqn.push_back(pair.first); |
| 91 | + auto at_tensor = pair.second; |
| 92 | + // alias_etensor_to_attensor will assert on this later, so to better |
| 93 | + // propogate up to python we check early and throw an exception. |
| 94 | + if (!at_tensor.is_contiguous()) { |
| 95 | + auto error_msg = "Gradient is not contiguous."; |
| 96 | + throw std::runtime_error(error_msg); |
| 97 | + } |
| 98 | +#ifndef USE_ATEN_LIB |
| 99 | + // convert at::Tensor to torch::executor::Tensor |
| 100 | + auto temp = alias_tensor_ptr_to_attensor(at_tensor); |
| 101 | + et_tensors.push_back(temp); |
| 102 | + cpp_inputs.insert({pair.first.c_str(), *et_tensors.back()}); |
| 103 | +#else |
| 104 | + cpp_inputs.insert({pair.first.c_str(), at_tensor}); |
| 105 | +#endif |
| 106 | + } |
| 107 | + |
| 108 | + auto err = sgd_->step(cpp_inputs); |
| 109 | + if (err != runtime::Error::Ok) { |
| 110 | + throw std::runtime_error("SGD step failed"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private: |
| 115 | + std::unique_ptr<optimizer::SGD> sgd_ = nullptr; |
| 116 | + std::vector<std::string> fqns_; |
| 117 | + |
| 118 | +#ifndef USE_ATEN_LIB // Portable mode |
| 119 | + std::vector<TensorPtr> params_; |
| 120 | +#endif |
| 121 | + ; |
| 122 | +}; |
| 123 | + |
| 124 | +static std::unique_ptr<PySGD> get( |
| 125 | + const py::dict& named_params, |
| 126 | + double lr, |
| 127 | + double momentum = 0, |
| 128 | + double dampening = 0, |
| 129 | + double weight_decay = 0, |
| 130 | + bool nesterov = false) { |
| 131 | + return std::make_unique<PySGD>( |
| 132 | + named_params, lr, momentum, dampening, weight_decay, nesterov); |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace |
| 136 | + |
| 137 | +PYBIND11_MODULE(_training_lib, m) { |
| 138 | + m.def( |
| 139 | + "get_sgd_optimizer", |
| 140 | + &get, |
| 141 | + py::arg("named_params"), |
| 142 | + py::arg("lr") = 0.1, |
| 143 | + py::arg("momentum") = 0.0, |
| 144 | + py::arg("dampening") = 0.0, |
| 145 | + py::arg("weight_decay") = 0.0, |
| 146 | + py::arg("nesterov") = false); |
| 147 | + py::class_<PySGD>(m, "ExecuTorchSGD").def("step", &PySGD::step); |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace training |
| 151 | +} // namespace extension |
| 152 | +} // namespace executorch |
0 commit comments