6 #include <torch/csrc/jit/ir.h> 7 #include <torch/csrc/jit/script/error_report.h> 8 #include <torch/csrc/jit/script/module.h> 9 #include <torch/csrc/jit/script/tree_views.h> 23 enum NoneStatus { ALWAYS, MAYBE, NEVER };
25 struct SugaredValue :
public std::enable_shared_from_this<SugaredValue> {
27 virtual std::string kind()
const = 0;
32 throw ErrorReport(loc) << kind() <<
" cannot be used as a value";
36 virtual std::shared_ptr<SugaredValue> attr(
39 const std::string& field) {
40 throw ErrorReport(loc) <<
"attribute lookup is not defined on " << kind();
47 const std::string& field,
49 throw ErrorReport(loc) <<
"attribute assignment is not defined on " 52 virtual NoneStatus isNone() {
58 virtual std::vector<std::shared_ptr<SugaredValue>> asTuple(
62 throw ErrorReport(loc) << kind() <<
" cannot be used as a tuple";
66 virtual std::shared_ptr<SugaredValue> call(
87 throw ErrorReport(loc) <<
"cannot call a " << kind();
97 std::string kind()
const override {
103 NoneStatus isNone()
override {
104 if (value_->mustBeNone())
111 std::vector<std::shared_ptr<SugaredValue>> asTuple(
115 std::shared_ptr<SugaredValue> attr(
118 const std::string& field)
override;
123 const std::string& field,
124 Value* newValue)
override;
126 Value* getValue()
const {
136 : symbol(symbol),
self(std::move(
self)) {}
144 std::string kind()
const override {
147 std::shared_ptr<SugaredValue> call(
152 size_t n_binders)
override;
157 : name(std::move(name)), version(std::move(version)) {}
159 std::string kind()
const override {
160 return "builtin module";
162 std::shared_ptr<SugaredValue> attr(
165 const std::string& field)
override {
166 return std::make_shared<BuiltinFunction>(
167 Symbol::fromQualString(name +
"::" + field), c10::nullopt);
180 explicit ClassValue(ClassTypePtr type) : type_(std::move(type)) {}
184 std::shared_ptr<SugaredValue> call(
189 size_t n_binders)
override;
191 std::shared_ptr<SugaredValue> attr(
194 const std::string& field)
override;
196 std::string kind()
const override {
206 : self_(std::move(
self)), method(method) {}
207 std::string kind()
const override {
210 std::shared_ptr<SugaredValue> call(
215 size_t n_binders)
override {
216 if (
auto classType = dynamic_cast<SimpleValue*>(self_.get())) {
219 std::vector<NamedValue> inputsWithSelf;
220 inputsWithSelf.emplace_back(loc, classType->getValue());
221 inputsWithSelf.insert(inputsWithSelf.end(), inputs.begin(), inputs.end());
222 return std::make_shared<SimpleValue>(
223 caller.emit_call_to(loc, method, inputsWithSelf, attributes));
226 return std::make_shared<SimpleValue>(
227 caller.emit_call_to(loc, method, inputs, attributes));
231 std::shared_ptr<SugaredValue> self_;
236 std::string kind()
const override {
239 std::shared_ptr<SugaredValue> call(
244 size_t n_binders)
override;
253 std::shared_ptr<SugaredValue> call(
258 size_t n_binders)
override {
259 if (inputs.
size() == 1 && attributes.
size() == 0) {
260 auto v = inputs[0].value(*m.graph());
261 if (v->type()->isSubtypeOf(type_)) {
262 return std::make_shared<SimpleValue>(v);
265 return BuiltinFunction::call(loc, m, inputs, attributes, n_binders);
279 std::string kind()
const override {
285 std::string kind()
const override {
293 std::string kind()
const override {
301 std::string kind()
const override {
313 std::string kind()
const override {
314 return "class.__new__";
317 std::shared_ptr<SugaredValue> createObject(
320 const std::string& classname) {
321 if (classname != type_->name()) {
323 <<
"Argument to __new__() must match the class " 324 <<
"you are calling __new__() on. " 325 <<
"Got: " << classname <<
", expected: " << type_->name();
327 auto& g = *m.graph();
328 auto createNode = g.insertNode(g.createObject(type_));
329 return std::make_shared<SimpleValue>(createNode->output());
335 static inline std::vector<Value*> toValues(
338 return fmap(nvs, [&](
const NamedValue& v) {
return v.value(g); });
constexpr size_t size() const
size - Get the array size.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...