1*523fa7a6SAndroid Build Coastguard Worker# Copyright (c) Meta Platforms, Inc. and affiliates. 2*523fa7a6SAndroid Build Coastguard Worker# All rights reserved. 3*523fa7a6SAndroid Build Coastguard Worker# 4*523fa7a6SAndroid Build Coastguard Worker# This source code is licensed under the BSD-style license found in the 5*523fa7a6SAndroid Build Coastguard Worker# LICENSE file in the root directory of this source tree. 6*523fa7a6SAndroid Build Coastguard Worker 7*523fa7a6SAndroid Build Coastguard Workerfrom abc import ABC, abstractmethod 8*523fa7a6SAndroid Build Coastguard Workerfrom dataclasses import dataclass 9*523fa7a6SAndroid Build Coastguard Worker 10*523fa7a6SAndroid Build Coastguard Workerfrom typing import Dict, List, Optional, Tuple, Union 11*523fa7a6SAndroid Build Coastguard Worker 12*523fa7a6SAndroid Build Coastguard Workerfrom executorch.exir.backend.compile_spec_schema import CompileSpec 13*523fa7a6SAndroid Build Coastguard Workerfrom torch.export.exported_program import ExportedProgram 14*523fa7a6SAndroid Build Coastguard Worker 15*523fa7a6SAndroid Build Coastguard Worker 16*523fa7a6SAndroid Build Coastguard Workerdef enforcedmethod(func): 17*523fa7a6SAndroid Build Coastguard Worker func.__enforcedmethod__ = True 18*523fa7a6SAndroid Build Coastguard Worker return func 19*523fa7a6SAndroid Build Coastguard Worker 20*523fa7a6SAndroid Build Coastguard Worker 21*523fa7a6SAndroid Build Coastguard Worker@dataclass 22*523fa7a6SAndroid Build Coastguard Workerclass PreprocessResult: 23*523fa7a6SAndroid Build Coastguard Worker processed_bytes: bytes = bytes() 24*523fa7a6SAndroid Build Coastguard Worker debug_handle_map: Optional[Union[Dict[int, Tuple[int]], Dict[str, Tuple[int]]]] = ( 25*523fa7a6SAndroid Build Coastguard Worker None 26*523fa7a6SAndroid Build Coastguard Worker ) 27*523fa7a6SAndroid Build Coastguard Worker 28*523fa7a6SAndroid Build Coastguard Worker 29*523fa7a6SAndroid Build Coastguard Worker""" 30*523fa7a6SAndroid Build Coastguard WorkerHow to create a backend (for example, BackendWithCompilerDemo): 31*523fa7a6SAndroid Build Coastguard Worker1. Create a python file, like backend_with_compiler_demo.py, with 32*523fa7a6SAndroid Build Coastguard Workera custom class BackendWithCompilerDemo, derived from BackendDetails. 33*523fa7a6SAndroid Build Coastguard Worker 34*523fa7a6SAndroid Build Coastguard WorkerHow to use the backend (for example, BackendWithCompilerDemo): 35*523fa7a6SAndroid Build Coastguard Worker2. Import this class, like 36*523fa7a6SAndroid Build Coastguard Workerfrom executorch.exir.backend.backend_with_compiler_demo import BackendWithCompilerDemo 37*523fa7a6SAndroid Build Coastguard Worker""" 38*523fa7a6SAndroid Build Coastguard Worker 39*523fa7a6SAndroid Build Coastguard Worker 40*523fa7a6SAndroid Build Coastguard Workerclass BackendDetails(ABC): 41*523fa7a6SAndroid Build Coastguard Worker """ 42*523fa7a6SAndroid Build Coastguard Worker A base interface to lower the implementation to the according backend. With 43*523fa7a6SAndroid Build Coastguard Worker the decorators, this interface will be static, abstract and all inheritances are 44*523fa7a6SAndroid Build Coastguard Worker enforced to implement this method. 45*523fa7a6SAndroid Build Coastguard Worker 46*523fa7a6SAndroid Build Coastguard Worker Args: 47*523fa7a6SAndroid Build Coastguard Worker edge_program: The original exported program. It will not be modified in place. 48*523fa7a6SAndroid Build Coastguard Worker compile_specs: List of values needed for compilation 49*523fa7a6SAndroid Build Coastguard Worker 50*523fa7a6SAndroid Build Coastguard Worker Returns: 51*523fa7a6SAndroid Build Coastguard Worker PreprocessResult: It wraps the following information: 52*523fa7a6SAndroid Build Coastguard Worker processed_bytes -> bytes: A compiled blob - a binary that can run the desired program in the backend. 53*523fa7a6SAndroid Build Coastguard Worker debug_handle_map (Optional[Dict[int, Tuple[int]]]): For profiling purposes, a map from the node_id in the final graph (either EXIR or the user's self-defined IR) 54*523fa7a6SAndroid Build Coastguard Worker to debug handle id attached in the original exported program. 55*523fa7a6SAndroid Build Coastguard Worker """ 56*523fa7a6SAndroid Build Coastguard Worker 57*523fa7a6SAndroid Build Coastguard Worker @staticmethod 58*523fa7a6SAndroid Build Coastguard Worker # all backends need to implement this method 59*523fa7a6SAndroid Build Coastguard Worker @enforcedmethod 60*523fa7a6SAndroid Build Coastguard Worker # it's a virtual method and inheritant class needs to implement the actual function 61*523fa7a6SAndroid Build Coastguard Worker @abstractmethod 62*523fa7a6SAndroid Build Coastguard Worker def preprocess( 63*523fa7a6SAndroid Build Coastguard Worker edge_program: ExportedProgram, 64*523fa7a6SAndroid Build Coastguard Worker compile_specs: List[CompileSpec], 65*523fa7a6SAndroid Build Coastguard Worker ) -> PreprocessResult: 66*523fa7a6SAndroid Build Coastguard Worker # Users should return a compiled blob - a binary that can run the desired 67*523fa7a6SAndroid Build Coastguard Worker # program in the backend. 68*523fa7a6SAndroid Build Coastguard Worker pass 69