|
| 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 | +"""Tensor config class to hold tensor scheduling information.""" |
| 18 | +from typing import List, Union |
| 19 | +from enum import IntEnum |
| 20 | +import tvm._ffi |
| 21 | +from tvm.contrib.ethosu.cascader.stripe_config import StripeConfig |
| 22 | + |
| 23 | +from tvm.runtime import Object |
| 24 | + |
| 25 | +from . import _ffi_api |
| 26 | +from .stripe_config import StripeConfig |
| 27 | +from .graph import Tensor, BufferMode |
| 28 | + |
| 29 | + |
| 30 | +class TensorConfigState(IntEnum): |
| 31 | + BOUNDARY = 0 |
| 32 | + INTERIOR = 1 |
| 33 | + |
| 34 | + |
| 35 | +@tvm._ffi.register_object("contrib.ethosu.cascader.MemoryRegion") |
| 36 | +class MemoryRegion(Object): |
| 37 | + """MemoryRegion class""" |
| 38 | + |
| 39 | + def __init__(self, name: str, size: int, read_bandwidth: int, write_bandwidth: int): |
| 40 | + self.__init_handle_by_constructor__( |
| 41 | + _ffi_api.MemoryRegion, name, size, read_bandwidth, write_bandwidth |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +@tvm._ffi.register_object("contrib.ethosu.cascader.TensorConfig") |
| 46 | +class TensorConfig(Object): |
| 47 | + """TensorConfig class""" |
| 48 | + |
| 49 | + def __init__( |
| 50 | + self, |
| 51 | + tensor: Tensor, |
| 52 | + home_region: MemoryRegion, |
| 53 | + state: TensorConfigState, |
| 54 | + buffer_mode: BufferMode, |
| 55 | + stripe_configs: List[StripeConfig], |
| 56 | + copy_tensor: bool = False, |
| 57 | + copy_region: Union[MemoryRegion, None] = None, |
| 58 | + ): |
| 59 | + if copy_region is None: |
| 60 | + copy_region = home_region |
| 61 | + self.__init_handle_by_constructor__( |
| 62 | + _ffi_api.TensorConfig, |
| 63 | + tensor, |
| 64 | + home_region, |
| 65 | + state, |
| 66 | + buffer_mode, |
| 67 | + stripe_configs, |
| 68 | + copy_tensor, |
| 69 | + copy_region, |
| 70 | + ) |
| 71 | + |
| 72 | + def get_buffer_size(self): |
| 73 | + return _ffi_api.TensorConfigGetBufferSize(self) |
| 74 | + |
| 75 | + @property |
| 76 | + def tensor(self): |
| 77 | + return self._tensor |
| 78 | + |
| 79 | + @property |
| 80 | + def home_region(self): |
| 81 | + return self._home_region |
| 82 | + |
| 83 | + @property |
| 84 | + def state(self): |
| 85 | + return TensorConfigState(self._state) |
| 86 | + |
| 87 | + @property |
| 88 | + def buffer_mode(self): |
| 89 | + return BufferMode(self._buffer_mode) |
| 90 | + |
| 91 | + @property |
| 92 | + def stripe_configs(self): |
| 93 | + return list(self._stripe_configs) |
| 94 | + |
| 95 | + @property |
| 96 | + def copy_tensor(self): |
| 97 | + return bool(self._copy_tensor) |
| 98 | + |
| 99 | + @property |
| 100 | + def copy_region(self): |
| 101 | + return self._copy_region |
| 102 | + |
| 103 | + def __hash__(self): |
| 104 | + return self._hash |
| 105 | + |
| 106 | + def __eq__(self, other): |
| 107 | + return _ffi_api.TensorConfigEqual(self, other) |
| 108 | + |
| 109 | + def __repr__(self): |
| 110 | + return ( |
| 111 | + f"TensorConfig(tensor={self.tensor}, " |
| 112 | + f"home_region={self.home_region.name}, " |
| 113 | + f"state={self.state.name}, " |
| 114 | + f"buffer_mode={self.buffer_mode.name}, " |
| 115 | + f"stripe_configs={self.stripe_configs}, " |
| 116 | + f"copy_tensor={self.copy_tensor}, " |
| 117 | + f"copy_region={self.copy_region.name}" |
| 118 | + ) |
0 commit comments