xref: /aosp_15_r20/external/pytorch/torch/csrc/api/include/torch/imethod.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #pragma once
2 #include <ATen/core/ivalue.h>
3 #include <vector>
4 
5 namespace torch {
6 
7 class TORCH_API IMethod {
8   /*
9   IMethod provides a portable interface for torch methods, whether
10   they are backed by torchscript or python/deploy.
11 
12   This is helpful since torchscript methods provide additional information
13   (e.g. FunctionSchema, Graph) which aren't available in pure python methods.
14 
15   Higher level APIs should prefer depending on this interface rather
16   than a specific implementation of it, to promote portability and reuse, and
17   avoid unintentional dependencies on e.g. script methods.
18 
19   Note: This API is experimental, and may evolve.
20   */
21  public:
22   using IValueList = std::vector<c10::IValue>;
23   using IValueMap = std::unordered_map<std::string, at::IValue>;
24 
25   IMethod() = default;
26   IMethod(const IMethod&) = default;
27   IMethod& operator=(const IMethod&) = default;
28   IMethod(IMethod&&) noexcept = default;
29   IMethod& operator=(IMethod&&) noexcept = default;
30   virtual ~IMethod() = default;
31 
32   virtual c10::IValue operator()(
33       std::vector<c10::IValue> args,
34       const IValueMap& kwargs = IValueMap()) const = 0;
35 
36   virtual const std::string& name() const = 0;
37 
38   // Returns an ordered list of argument names, possible in both
39   // script and python methods.  This is a more portable dependency
40   // than a ScriptMethod FunctionSchema, which has more information
41   // than can be generally expected from a python method.
42   const std::vector<std::string>& getArgumentNames() const;
43 
44  protected:
45   virtual void setArgumentNames(
46       std::vector<std::string>& argumentNames) const = 0;
47 
48  private:
49   mutable bool isArgumentNamesInitialized_{false};
50   mutable std::vector<std::string> argumentNames_;
51 };
52 
53 } // namespace torch
54