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