Row 5796
Content Data
This page contains data entry 5796 from the Axioma AXP content repository. The structured data below represents the complete record for this entry.
I have a family of functions that follow the following structure for the forward method.
class ParentFunc(torch.autograd.Function): @staticmethod def forward(ctx): output1 = ParentFunc.my_class_method1() output2 = ParentFunc.my_class_method2(output1) return output2 @classmethod def my_class_method1(cls): return compute1() @classmethod def my_class_method2(cls, output1): return compute2(output1) @staticmethod def backward(ctx): pass # not important right now
With this structure, I am able to implement a general case that works for a lot of my child functions by simply inheriting the forward() and class methods, which is great. The hope was that when I needed to do edge cases, I would only have to change a few class method and use the other inherited code, rather than copy-paste the entire code block. See the following edge case example:
class ChildFunc(ParentFunc): @staticmethod def forward(ctx): output1 = ChildFunc.my_class_method1() # new definiton output2 = ParentFunc.my_class_method2(output1) return output2 @classmethod def my_class_method1(cls): return compute1_child()
When running ChildFunc, I can't get it to call the overridden forward() OR my\_class\_method1(). In VSCode, it is showing that these functions reside in ParentFunc. The function inputs and outputs are the same, which seems to be a requirement of Python overriding.
Looking for options, there is name mangling where you change forward() to \_forward() or \_\_forward(), but that doesn't work with the PyTorch framework to automatically call forward() w/ things like .apply() or \_\_call\_\_. When doing name mangling like \_forward() or \_\_forward(), VSCode acknowledges that the new definition resides in ChildFunc.
Is there anything I can do to implement this with inheritance? I am not a Python or PyTorch expert, so I am hoping I am missing something.
| Field | Value |
|---|---|
| text | I have a family of functions that follow the following structure for the forward method. class ParentFunc(torch.autograd.Function): @staticmethod def forward(ctx): output1 = ParentFunc.my_class_method1() output2 = ParentFunc.my_class_method2(output1) return output2 @classmethod def my_class_method1(cls): return compute1() @classmethod def my_class_method2(cls, output1): retu… |
| label | r/pytorch |
| dataType | post |
| communityName | r/pytorch |
| datetime | 2024-05-03 |
| username_encoded | Z0FBQUFBQm5LakwyZ1FjbmlsZDBXSThRR0ktRkJ3eVVwcXk3TTFtaDh4RGgzczgtczVqSWpWcFhFLUowQ2UzRGcySGFncko5WUVNSHFyVjVVV2hScE1nYy1wUDJSeUt1TWc9PQ== |
| url_encoded | Z0FBQUFBQm5Lak9HXzRudUQ5VzJoS1hBbHgteEIxU3F5cDNQbDZQMVdtZGxmMmkzaW9BWjBFSnpCM2Vkbm9aZzhvVC1oVW5neUlfM0NQMkNEWXdoejZrX0hjckZvZ1NiSnBXekNvSGNyODVad0xYMmRwWm1IUFhtX19RRzZNSzEwQlhxNEs4b0k3bHE4bU44dTVXc3NmRThMb2xTUXJnSlQ5RHVaTFowTnhTVTFrU1hicnBJbl9ZTGhIRlpRTlBrMkhVczN1VHNsOXBSY2FoR3N4QmYwOTdKVUw4cGVhVmREQT09 |
Raw Record
{
"text": "I have a family of functions that follow the following structure for the forward method.\n\n class ParentFunc(torch.autograd.Function):\n @staticmethod\n def forward(ctx):\n output1 = ParentFunc.my_class_method1()\n output2 = ParentFunc.my_class_method2(output1)\n return output2\n \n @classmethod\n def my_class_method1(cls):\n return compute1()\n \n @classmethod\n def my_class_method2(cls, output1):\n return compute2(output1)\n \n @staticmethod\n def backward(ctx):\n pass # not important right now\n\nWith this structure, I am able to implement a general case that works for a lot of my child functions by simply inheriting the forward() and class methods, which is great. The hope was that when I needed to do edge cases, I would only have to change a few class method and use the other inherited code, rather than copy-paste the entire code block. \n \nSee the following edge case example:\n\n class ChildFunc(ParentFunc):\n @staticmethod\n def forward(ctx):\n output1 = ChildFunc.my_class_method1() # new definiton\n output2 = ParentFunc.my_class_method2(output1)\n return output2\n \n @classmethod\n def my_class_method1(cls):\n return compute1_child()\n\nWhen running ChildFunc, I can't get it to call the overridden forward() OR my\\_class\\_method1(). In VSCode, it is showing that these functions reside in ParentFunc. The function inputs and outputs are the same, which seems to be a requirement of Python overriding.\n\nLooking for options, there is name mangling where you change forward() to \\_forward() or \\_\\_forward(), but that doesn't work with the PyTorch framework to automatically call forward() w/ things like .apply() or \\_\\_call\\_\\_. When doing name mangling like \\_forward() or \\_\\_forward(), VSCode acknowledges that the new definition resides in ChildFunc.\n\nIs there anything I can do to implement this with inheritance? I am not a Python or PyTorch expert, so I am hoping I am missing something.",
"label": "r/pytorch",
"dataType": "post",
"communityName": "r/pytorch",
"datetime": "2024-05-03",
"username_encoded": "Z0FBQUFBQm5LakwyZ1FjbmlsZDBXSThRR0ktRkJ3eVVwcXk3TTFtaDh4RGgzczgtczVqSWpWcFhFLUowQ2UzRGcySGFncko5WUVNSHFyVjVVV2hScE1nYy1wUDJSeUt1TWc9PQ==",
"url_encoded": "Z0FBQUFBQm5Lak9HXzRudUQ5VzJoS1hBbHgteEIxU3F5cDNQbDZQMVdtZGxmMmkzaW9BWjBFSnpCM2Vkbm9aZzhvVC1oVW5neUlfM0NQMkNEWXdoejZrX0hjckZvZ1NiSnBXekNvSGNyODVad0xYMmRwWm1IUFhtX19RRzZNSzEwQlhxNEs4b0k3bHE4bU44dTVXc3NmRThMb2xTUXJnSlQ5RHVaTFowTnhTVTFrU1hicnBJbl9ZTGhIRlpRTlBrMkhVczN1VHNsOXBSY2FoR3N4QmYwOTdKVUw4cGVhVmREQT09"
}
Entry Information
- Entry ID: 5796
- Repository: Axioma AXP
- Dataset: arrmlet/reddit_dataset_36
- Total Entries: 100,000