Skip to content

Commit

Permalink
Fix import exceptions in tracing.py for older (<1.12) versions of pyt…
Browse files Browse the repository at this point in the history
…orch

Summary:
Pull Request resolved: #362

X-link: facebookresearch/detectron2#4491

Recently landed D35518556 (1ffc801) / Github: 36a65a0907d90ed591479b2ebaa8b61cfa0b4ef0 throws an exception with older versions of PyTorch, due to a missing library for import. This has been reported by multiple members of the PyTorch community at facebookresearch/detectron2@36a65a0

This change uses `try/except` to check for libraries and set flags on presence/absence to later guard code that would use them.

Reviewed By: wat3rBro

Differential Revision: D38879134

fbshipit-source-id: 72f5a7a8d350eb82be87567f006368bf207f5a74
  • Loading branch information
Simon Hollis authored and facebook-github-bot committed Aug 23, 2022
1 parent 53f9eee commit 6ca4702
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions d2go/modeling/backbone/modules.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved


from typing import List

import torch
import torch.nn as nn
from detectron2 import layers
from detectron2.utils.tracing import assert_fx_safe
from detectron2.utils.tracing import is_fx_tracing
from mobile_cv.arch.fbnet_v2.irf_block import IRFBlock


Expand All @@ -34,7 +33,8 @@ def __init__(self, in_channels, num_anchors, box_dim=4):
torch.nn.init.constant_(l.bias, 0)

def forward(self, x: List[torch.Tensor]):
assert_fx_safe(isinstance(x, (list, tuple)), "Unexpected data type")
if not is_fx_tracing():
torch._assert(isinstance(x, (list, tuple)), "Unexpected data type")
logits = [self.cls_logits(y) for y in x]
bbox_reg = [self.bbox_pred(y) for y in x]

Expand Down

0 comments on commit 6ca4702

Please sign in to comment.