1# Copyright 2020-2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15 16"""Registry the relation.""" 17 18from collections import UserDict 19from .. import context 20 21 22class Registry(UserDict): 23 """Registry class for registry functions for tensor call primitive ops function.""" 24 25 def register(self, obj_str, obj): 26 if isinstance(obj_str, str): 27 self[obj_str] = obj 28 29 def get(self, obj_str): 30 """Get the value by str.""" 31 if not isinstance(obj_str, str): 32 raise TypeError("key for tensor registry must be string.") 33 if context.get_context("enable_ge"): 34 def wrap(*args): 35 new_args = list(args) 36 new_args.append(obj_str) 37 return self["vm_compare"](*new_args) 38 39 obj = wrap 40 else: 41 obj = self[obj_str] 42 return obj 43 44 45tensor_operator_registry = Registry() 46