Elffuss BraidHub

kikocis/ncnn public

master · 2.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "reset_device.h"
16
17namespace pnnx {
18
19void reset_device(std::shared_ptr<torch::jit::Graph>& graph, const std::string& device)
20{
21 for (torch::jit::Node* n : graph->nodes())
22 {
23 if (n->kind().is_aten())
24 {
25 if (n->hasNamedInput("dtype"))
26 {
27 torch::jit::Node* dtype_node = n->namedInput("dtype")->node();
28
29 if (dtype_node->hasAttribute(torch::jit::attr::value))
30 {
31 // change dtype=half to dtype=float
32 if (dtype_node->kindOf(torch::jit::attr::value) == torch::jit::AttributeKind::i && dtype_node->i(torch::jit::attr::value) == 5)
33 {
34 dtype_node->i_(torch::jit::attr::value, 6);
35 }
36 // change dtype=bfloat16 to dtype=float
37 if (dtype_node->kindOf(torch::jit::attr::value) == torch::jit::AttributeKind::i && dtype_node->i(torch::jit::attr::value) == 15)
38 {
39 dtype_node->i_(torch::jit::attr::value, 6);
40 }
41 }
42 }
43
44 if (n->hasNamedInput("device"))
45 {
46 torch::jit::Node* device_node = n->namedInput("device")->node();
47
48 device_node->s_(torch::jit::attr::value, (device == "gpu") ? "cuda" : "cpu");
49 }
50 }
51 }
52}
53
54} // namespace pnnx