• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2024 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_CCSRC_PYBIND_API_IR_HOOK_PY_H_
18 #define MINDSPORE_CCSRC_PYBIND_API_IR_HOOK_PY_H_
19 
20 #include <map>
21 #include <memory>
22 #include <utility>
23 #include <string>
24 #include "pybind11/pybind11.h"
25 #include "pybind11/pytypes.h"
26 #include "ir/tensor.h"
27 
28 namespace mindspore {
29 namespace tensor {
30 namespace py = pybind11;
31 using AutoGradMetaDataWeakPtr = std::weak_ptr<AutoGradMetaData>;
32 
33 struct RegisterHook {
34   /// \brief Register a backward hook
35   ///
36   /// \ void
37   static uint64_t RegisterTensorBackwardHook(const Tensor &tensor, const py::function &hook);
38 
39   /// \brief Remove a backward hook
40   ///
41   /// \ void
42   static void RemoveTensorBackwardHook(uint64_t id);
43 
44   /// \brief Update weight meta
45   ///
46   /// \ void
47   static void UpdateTensorBackwardHook(const AutoGradMetaDataPtr &auto_grad_meta_data, const std::string &id);
48 
ClearHookMapRegisterHook49   static void ClearHookMap() { hook_meta_fn_map_.clear(); }
50 
51   // For store hook
52   static std::map<uint64_t, std::pair<AutoGradMetaDataWeakPtr, TensorBackwardHookPtr>> hook_meta_fn_map_;
53 };
54 
55 }  // namespace tensor
56 }  // namespace mindspore
57 #endif  // MINDSPORE_CCSRC_PYBIND_API_IR_HOOK_PY_H_
58