| 134 | device_node = [n for n in device_holder.graph.findAllNodes("prim::Constant") if "Device" in repr(n)][-1] |
| 135 | |
| 136 | def patch_device(module): |
| 137 | try: |
| 138 | graphs = [module.graph] if hasattr(module, "graph") else [] |
| 139 | except RuntimeError: |
| 140 | graphs = [] |
| 141 | |
| 142 | if hasattr(module, "forward1"): |
| 143 | graphs.append(module.forward1.graph) |
| 144 | |
| 145 | for graph in graphs: |
| 146 | for node in graph.findAllNodes("prim::Constant"): |
| 147 | if "value" in node.attributeNames() and str(node["value"]).startswith("cuda"): |
| 148 | node.copyAttributes(device_node) |
| 149 | |
| 150 | model.apply(patch_device) |
| 151 | patch_device(model.encode_image) |