| 146 | device_node = [n for n in device_holder.graph.findAllNodes("prim::Constant") if "Device" in repr(n)][-1] |
| 147 | |
| 148 | def patch_device(module): |
| 149 | try: |
| 150 | graphs = [module.graph] if hasattr(module, "graph") else [] |
| 151 | except RuntimeError: |
| 152 | graphs = [] |
| 153 | |
| 154 | if hasattr(module, "forward1"): |
| 155 | graphs.append(module.forward1.graph) |
| 156 | |
| 157 | for graph in graphs: |
| 158 | for node in graph.findAllNodes("prim::Constant"): |
| 159 | if "value" in node.attributeNames() and str(node["value"]).startswith("cuda"): |
| 160 | node.copyAttributes(device_node) |
| 161 | |
| 162 | model.apply(patch_device) |
| 163 | patch_device(model.encode_image) |