1# Licensed to the Apache Software Foundation (ASF) under one 2# or more contributor license agreements. See the NOTICE file 3# distributed with this work for additional information 4# regarding copyright ownership. The ASF licenses this file 5# to you under the Apache License, Version 2.0 (the 6# "License"); you may not use this file except in compliance 7# with the License. You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, 12# software distributed under the License is distributed on an 13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14# KIND, either express or implied. See the License for the 15# specific language governing permissions and limitations 16# under the License. 17""" TVM Attribute module, which is mainly used for defining attributes of operators""" 18from ._ffi.node import NodeBase, register_node as _register_tvm_node 19from ._ffi.function import _init_api 20from . import _api_internal 21 22 23@_register_tvm_node 24class Attrs(NodeBase): 25 """Attribute node, which is mainly use for defining attributes of relay operators. 26 27 Used by function registered in python side, such as compute, schedule and alter_layout. 28 Attrs is passed as the first argument to these functions. 29 """ 30 def list_field_info(self): 31 """ Get fields information 32 33 Returns 34 ------- 35 infos: list of AttrFieldInfo 36 List of field information 37 """ 38 return _api_internal._AttrsListFieldInfo(self) 39 40 def keys(self): 41 """Get list of names in the attribute. 42 43 Returns 44 ------- 45 keys : list of str 46 List of keys 47 """ 48 fields = self.list_field_info() 49 for field in fields: 50 yield field.name 51 52 def get_int_tuple(self, key): 53 """Get a python int tuple of a key 54 55 Parameters 56 ---------- 57 key: str 58 59 Returns 60 ------- 61 value: Tuple of int 62 """ 63 return tuple(x.value for x in self.__getattr__(key)) 64 65 def get_int(self, key): 66 """Get a python int value of a key 67 68 Parameters 69 ---------- 70 key: str 71 72 Returns 73 ------- 74 value: int 75 """ 76 return self.__getattr__(key) 77 78 def get_str(self, key): 79 """Get a python int value of a key 80 81 Parameters 82 ---------- 83 key: str 84 85 Returns 86 ------- 87 value: int 88 """ 89 return self.__getattr__(key) 90 91 def __getitem__(self, item): 92 return self.__getattr__(item) 93 94 95_init_api("tvm.attrs") 96