Caffe2 - C++ API
A deep learning, cross platform ML framework
constants.h
1 #pragma once
2 #include <torch/csrc/WindowsTorchApiMacro.h>
3 #include <ATen/core/ivalue.h>
4 #include <ATen/core/jit_type.h>
5 #include <torch/csrc/jit/scope.h>
6 #include <torch/csrc/jit/source_range.h>
7 
8 // helpers for handling constants in the IR
9 // - create constant nodes from ints, floats, intlist, Tensors, and other types
10 // - implement primitive constant ops.
11 namespace torch {
12 namespace jit {
13 
14 using ::c10::IValue;
15 
16 struct Graph;
17 struct Value;
18 
19 // thrown when insertConstant cannot encode the IValue into a graph
20 struct TORCH_API constant_not_supported_error : public std::runtime_error {
21  using runtime_error::runtime_error;
22 };
23 
24 // note: prefer g.insertConsant(val, loc) which does exactly the same thing
25 // this function is only declared/defined here because its implementation is
26 // closely related to the implementation of prim::Constant that is also in
27 // constants.cpp
28 TORCH_API Value* insertConstant(
29  Graph& g,
30  const IValue& val,
31  const c10::TypePtr& result_type = nullptr,
32  c10::optional<SourceRange> loc = c10::nullopt,
33  c10::optional<ScopePtr> scope = c10::nullopt);
34 
36 // Helper for retrieving constants
38 
39 // attempt to convert a (possibly constant) Value* into an intepreter value
40 // (IValue). returns c10::nullopt if the Value* was not constant
41 TORCH_API c10::optional<IValue> toIValue(const Value* v);
42 
43 // if a value is a constant then try to turn into type T using the
44 // same rules as the interpreter
45 template <typename T>
46 c10::optional<T> constant_as(const Value* v) {
47  if (auto ivalue = toIValue(v)) {
48  return ivalue->to<T>();
49  }
50  return c10::nullopt;
51 }
52 } // namespace jit
53 } // namespace torch
Definition: jit_type.h:17