[docs]classCustomDecompTable(Dict[torch._ops.OperatorBase,Callable]):""" This is a custom dictionary that is specifically used for handling decomp_table in export. The reason we need this is because in the new world, you can only *delete* an op from decomp table to preserve it. This is problematic for custom ops because we don't know when the custom op will actually be loaded to the dispatcher. As a result, we need to record the custom ops operations until we really need to materialize it (which is when we run decomposition pass.) Invariants we hold are: 1. All aten decomp is loaded at the init time 2. We materialize ALL ops when user ever reads from the table to make it more likely that dispatcher picks up the custom op. 3. If it is write operation, we don't necessarily materialize 4. We load the final time during export, right before calling run_decompositions() """def__init__(self):super().__init__()fromtorch._decompimport_core_aten_decompositions_post_autograd# For aten ops, we load them up in the beginningself.decomp_table=_core_aten_decompositions_post_autograd()foropin_collect_all_valid_cia_ops_for_aten_namespace():self.decomp_table[op]=_get_decomp_for_cia(op)# This is to track the *pending* deleted custom ops that haven't been materialized yetself.deleted_custom_ops=set()# When this is true, there shouldn't be any pending operations in the table.self.has_materialized=Falsedef__getitem__(self,key):self._materialize_if_needed()returnself.decomp_table.__getitem__(key)def__setitem__(self,key,value)->None:self.decomp_table.__setitem__(key,value)ifkeyinself.deleted_custom_ops:self.deleted_custom_ops.remove(key)
[docs]defpop(self,*args):def_pop_if_can(key):if_is_aten_op(key):returnself.decomp_table.pop(key)ifkeyinself.decomp_table:# Even if we materialized it, we should add it to the deleted# custom ops list so that when we materialize next time,# we should respect user's intention.self.deleted_custom_ops.add(key)returnself.decomp_table.pop(key)ifkeyinself.deleted_custom_ops:raiseKeyError(f"{key} doesn't exist in the table")self.deleted_custom_ops.add(key)# We would come here when user pops off something that is# not in the table. In this case, we just pretend that it# was in the table.return_get_decomp_for_cia(key)iflen(args)==1:return_pop_if_can(args[0])iflen(args)==2:try:return_pop_if_can(args[0])exceptKeyError:returnargs[1]
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.