Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'share_from_this' to 'torch::jit::Graph' #87343

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions test/onnx/test_custom_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ def forward(self, a, b):
def symbolic_custom_add(g, self, other):
return g.op("Add", self, other)

from torch.onnx import register_custom_op_symbolic

register_custom_op_symbolic(
torch.onnx.register_custom_op_symbolic(
"custom_namespace::custom_add", symbolic_custom_add, 9
)

x = torch.randn(2, 3, 4, requires_grad=False)
y = torch.randn(2, 3, 4, requires_grad=False)

model = CustomAddModel()
# before fixing #51833 this used to give a PyBind error
# with PyTorch 1.10dev ("Unable to cast from non-held to held
# instance (T& to Holder<T>)")
onnxir, _ = do_export(model, (x, y), opset_version=11)
onnx_model = onnx.ModelProto.FromString(onnxir)
prepared = c2.prepare(onnx_model)
Expand Down
15 changes: 14 additions & 1 deletion torch/csrc/jit/ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ struct Value {
const Node* node() const {
return node_;
}

/**
* @warning NEVER pass raw pointer of smart pointer managed Graph to Python.
* Check #87343 for details.
*/
Graph* owningGraph();
const Graph* owningGraph() const;
// TODO: make this more const correct
Expand Down Expand Up @@ -398,6 +403,10 @@ struct TORCH_API Node {
}
SourceRange sourceRange() const;

/**
* @warning NEVER pass raw pointer of smart pointer managed Graph to Python.
* Check #87343 for details.
*/
Graph* owningGraph() {
return graph_;
}
Expand Down Expand Up @@ -1049,6 +1058,10 @@ struct Block {
const Node* param_node() const {
return input_;
}
/**
* @warning NEVER pass raw pointer of smart pointer managed Graph to Python.
* Check #87343 for details.
*/
Graph* owningGraph() {
return graph_;
}
Expand Down Expand Up @@ -1163,7 +1176,7 @@ struct Block {
std::shared_ptr<Wrap<Block>> wrap_;
};

struct Graph {
struct Graph : std::enable_shared_from_this<Graph> {
AT_DISALLOW_COPY_AND_ASSIGN(Graph);
friend struct Node;
friend struct Value;
Expand Down
19 changes: 16 additions & 3 deletions torch/csrc/jit/passes/onnx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,16 @@ void NodeToONNX(

WithInsertPoint insert_point_guard(new_block);
WithCurrentScope scope_guard(*g, n->scope());

// IMPORTANT: NEVER pass raw pointer of smart pointer managed objects to
// Python. Check #87343 for details.
py::object raw_output = onnx.attr("_run_symbolic_function")(
g, new_block, n, py_inputs, env, operator_export_type);
g->shared_from_this(),
BowenBao marked this conversation as resolved.
Show resolved Hide resolved
new_block,
n,
py_inputs,
env,
operator_export_type);

// Find new nodes that have been created by _run_symbolic_function and
// propagate metadata
Expand Down Expand Up @@ -530,8 +538,11 @@ void NodeToONNX(
opset_version,
pyobj.attr("symbolic"),
/* custom */ true);

// IMPORTANT: NEVER pass raw pointer of smart pointer managed objects to
// Python. Check #87343 for details.
py::object raw_output = onnx.attr("_run_symbolic_method")(
new_block->owningGraph(),
new_block->owningGraph()->shared_from_this(),
op->name(),
pyobj.attr("symbolic"),
py_symbolic_args);
Expand All @@ -542,8 +553,10 @@ void NodeToONNX(
Node* n = static_cast<Node*>(op);
n->s_(attr::name, op->name());
// Call symbolic function
// IMPORTANT: NEVER pass raw pointer of smart pointer managed objects to
// Python. Check #87343 for details.
py::object raw_output = onnx.attr("_run_symbolic_function")(
new_block->owningGraph(),
new_block->owningGraph()->shared_from_this(),
new_block,
n,
py_symbolic_args,
Expand Down