Skip to content

Commit a71d8b7

Browse files
zdevitopytorchmergebot
authored andcommitted
Fix ReferenceError: weakly-referenced object no longer exists in cycle detector (pytorch#146922)
Summary: weakref.proxy objects will throw errors when they re dead. We just do not bother visulaizing them. They are weak, so they aren't relevant to cycles anyway. Differential Revision: D69270429 Pull Request resolved: pytorch#146922 Approved by: https://github.com/tianfengfrank, https://github.com/Chillee
1 parent 22fae0d commit a71d8b7

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

torch/utils/viz/_cycles.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def create_graph(objects, *, context=None, filter=None):
266266
if filter is None:
267267
filter = is_cuda_tensor
268268

269+
objects = [obj for obj in objects if not isinstance(obj, weakref.ProxyTypes)]
269270
nodes = [Node(object_annotation(obj), context(obj), filter(obj), []) for obj in objects]
270271
node_referrers: list[list[int]] = [[] for obj in objects]
271272

@@ -362,18 +363,16 @@ def to_dot(nodes):
362363
363364
#main {
364365
flex: 2;
365-
overflow: auto;
366+
height: 60vh;
367+
overflow: clip;
366368
}
367369
368370
#preContainer {
369371
flex: 1;
372+
height: 40vh;
370373
overflow: auto;
371374
}
372375
373-
svg {
374-
overflow: scroll;
375-
}
376-
377376
pre {
378377
margin: 0;
379378
padding: 10px;
@@ -391,8 +390,61 @@ def to_dot(nodes):
391390
<script src='https://cdnjs.cloudflare.com/ajax/libs/viz.js/1.8.0/viz-lite.js'></script>
392391
<script>
393392
let dot = $DOT
394-
let image = Viz(dot, {format: 'svg'});
395-
document.getElementById('main').innerHTML = image
393+
let image = Viz(dot, {format: 'svg', 'totalMemory': 1024*1024*1024});
394+
let main = document.getElementById('main')
395+
main.innerHTML = image
396+
let svg = main.firstElementChild
397+
// Panning and zooming logic
398+
let isPanning = false;
399+
let startX, startY;
400+
let viewBox = { x: 0, y: 0, width: parseFloat(svg.getAttribute('width')), height: parseFloat(svg.getAttribute('height')) };
401+
svg.removeAttribute('width');
402+
svg.removeAttribute('height');
403+
function updateViewBox() {
404+
svg.setAttribute('viewBox', `${viewBox.x} ${viewBox.y} ${viewBox.width} ${viewBox.height}`);
405+
}
406+
updateViewBox()
407+
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
408+
svg.addEventListener('mousedown', function(e) {
409+
isPanning = true;
410+
startX = e.clientX;
411+
startY = e.clientY;
412+
});
413+
svg.addEventListener('mousemove', function(e) {
414+
if (!isPanning) return;
415+
const dx = (e.clientX - startX) * (viewBox.width / svg.clientWidth);
416+
const dy = (e.clientY - startY) * (viewBox.height / svg.clientHeight);
417+
viewBox.x -= dx;
418+
viewBox.y -= dy;
419+
startX = e.clientX;
420+
startY = e.clientY;
421+
updateViewBox();
422+
});
423+
svg.addEventListener('mouseup', function() {
424+
isPanning = false;
425+
});
426+
svg.addEventListener('mouseleave', function() {
427+
isPanning = false;
428+
});
429+
svg.addEventListener('wheel', function(e) {
430+
e.preventDefault();
431+
const zoomFactor = 0.1;
432+
const zoomAmount = e.deltaY > 0 ? 1 + zoomFactor : 1 - zoomFactor;
433+
// Calculate mouse position relative to the SVG
434+
const rect = svg.getBoundingClientRect();
435+
const mouseX = e.clientX - rect.left;
436+
const mouseY = e.clientY - rect.top;
437+
const mouseXRel = mouseX / svg.clientWidth;
438+
const mouseYRel = mouseY / svg.clientHeight;
439+
// Adjust viewBox to zoom around the mouse position
440+
const newWidth = viewBox.width * zoomAmount;
441+
const newHeight = viewBox.height * zoomAmount;
442+
viewBox.x += (viewBox.width - newWidth) * mouseXRel;
443+
viewBox.y += (viewBox.height - newHeight) * mouseYRel;
444+
viewBox.width = newWidth;
445+
viewBox.height = newHeight;
446+
updateViewBox();
447+
});
396448
$LISTENERS
397449
</script>
398450
</body>

0 commit comments

Comments
 (0)