function
Base class for defining and handling functions in the RPN model.
This class provides mechanisms for managing, applying, and serializing functions, including custom callable functions, predefined operations, and string-based function recognition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the function, by default 'base_function'. |
'base_function'
|
device
|
str
|
The computational device for the function, by default 'cpu'. |
'cpu'
|
*args
|
tuple
|
Additional positional arguments. |
()
|
**kwargs
|
dict
|
Additional keyword arguments. |
{}
|
Source code in tinybig/module/base_function.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
__call__(*args, **kwargs)
Calls the forward
method of the function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
tuple
|
Positional arguments for the |
()
|
**kwargs
|
dict
|
Keyword arguments for the |
{}
|
Source code in tinybig/module/base_function.py
__init__(name='base_function', device='cpu', *args, **kwargs)
Initializes the base function with a name and device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the function, by default 'base_function'. |
'base_function'
|
device
|
str
|
The computational device for the function, by default 'cpu'. |
'cpu'
|
*args
|
tuple
|
Additional positional arguments. |
()
|
**kwargs
|
dict
|
Additional keyword arguments. |
{}
|
Source code in tinybig/module/base_function.py
forward(*args, **kwargs)
abstractmethod
Abstract method for the forward pass of the function.
This method must be implemented in subclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
tuple
|
Positional arguments for the function. |
()
|
**kwargs
|
dict
|
Keyword arguments for the function. |
{}
|
Source code in tinybig/module/base_function.py
func_x(x, functions, device='cpu')
staticmethod
The function execution to the input data.
It applies the list of functions to the input vector and returns the calculation results.
This method will be extensively called for handeling the data processing functions in the expansion functions, RPN head and remainder functions in tinyBIG.
- preprocess_functions in expansion functions
- postprocess_functions in expansion functions
- output_process_functions in rpn heads
- activation_functions in remainder functions
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
The input data vector. |
required | |
functions
|
The functions to be applied to the input vector. The function can be callable functions, string names of the functions, the complete class descriptions of the functions, etc. |
required | |
device
|
str
|
The device to perform the function on the input vector. |
'cpu'
|
Returns:
Type | Description |
---|---|
Tensor
|
The processed input vector by these functions. |
Source code in tinybig/module/base_function.py
functions_to_configs(functions, class_name='function_class', parameter_name='function_parameters')
staticmethod
Converts a list of functions into a serialized configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
functions
|
list | tuple | Callable
|
A list of functions or a single callable function to serialize. |
required |
class_name
|
str
|
The key for the class name in the configuration, by default 'function_class'. |
'function_class'
|
parameter_name
|
str
|
The key for the parameters in the configuration, by default 'function_parameters'. |
'function_parameters'
|
Returns:
Type | Description |
---|---|
list or dict
|
A serialized configuration of the functions. |
Source code in tinybig/module/base_function.py
get_name()
The name retrieval method of the function.
It returns the name of the function.
Returns:
Type | Description |
---|---|
str
|
The name of the function. |
str_func_x(x, func, device='cpu', *args, **kwargs)
staticmethod
Function recognition from their string names or string class descriptions.
It recognizes the data processing functions from their names or class description in strings, e.g., "layer_norm" or "torch.nn.functional.layer_norm".
Since these functions can be very diverse, whose definitions are also very different, it makes it very challenging to process them based on their string descriptions. This method can process some basic functions, e.g., activation functions, and normalization functions. For the functions that are not implemented in this method, the users may consider to extend the method to handle more complex input functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
The input data vector. |
required | |
func
|
str | Callable
|
The string description of the functoin name or class. |
required |
device
|
The device to host and apply the recognized functions. |
'cpu'
|
Returns:
Type | Description |
---|---|
Tensor
|
The processed input data vector by the recognized functions. |
Source code in tinybig/module/base_function.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
string_to_function(formula, variable)
staticmethod
Formula recognition from strings.
It recognizes and returns the formula and variables from strings via the sympy package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formula
|
The function formula as a string. |
required | |
variable
|
The list of the variables involved in the formula. |
required |
Returns:
Type | Description |
---|---|
FunctionClass
|
The recognized function of the input formula. |
Source code in tinybig/module/base_function.py
to_config()
Serializes the function into a dictionary configuration.
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the class name and parameters of the function. |