#pragma once #include #include #include #include #include #include namespace torch { namespace jit { struct BuiltinOpFunction : public Function { BuiltinOpFunction( c10::QualifiedName qualname, c10::FunctionSchema schema, std::function callable, std::string doc_string = "") : name_(std::move(qualname)), callable_(std::move(callable)), schema_(std::move(schema)), doc_string_(std::move(doc_string)) { TORCH_INTERNAL_ASSERT(schema_.returns().size() == 1); } c10::string_view doc_string() const override { return doc_string_; } void run(Stack& stack) override { callable_(stack); } c10::intrusive_ptr runAsync( Stack& stack, TaskLauncher /* not used */) override { run(stack); auto res = c10::make_intrusive(stack.front().type()); res->markCompleted(std::move(stack.front())); return res; } const c10::QualifiedName& qualname() const override { return name_; } // if this isn't yet defined, run its method_creator function void ensure_defined() override { // nop } const c10::FunctionSchema& getSchema() const override { return schema_; } size_t num_inputs() const override { return schema_.arguments().size(); } Function& setSchema(c10::FunctionSchema schema) override { schema_ = std::move(schema); return *this; } bool call(Stack& stack, c10::optional, c10::function_ref) override { run(stack); return false; } bool call(Stack& stack, c10::function_ref) override { run(stack); return false; } ~BuiltinOpFunction() override {} private: c10::QualifiedName name_; std::function callable_; c10::FunctionSchema schema_; std::string doc_string_; }; } // namespace jit } // namespace torch