1111namespace torchscratch {
1212namespace core {
1313namespace autograd {
14+
15+ // Forward declaration
1416class Variable ;
1517
18+ /* *
19+ * Base class for all autograd functions.
20+ * Each subclass implements a particular operation (like Add, MatMul, etc.)
21+ * with its forward and backward pass logic.
22+ */
1623class Function {
1724public:
1825 virtual ~Function () = default ;
26+
27+ // Rule of 5: Allow proper inheritance
1928 Function () = default ;
2029 Function (const Function&) = default ;
2130 Function& operator =(const Function&) = default ;
2231 Function (Function&&) noexcept = default ;
2332 Function& operator =(Function&&) noexcept = default ;
2433
34+ /* *
35+ * Apply the forward pass of this function to the inputs.
36+ * @param inputs Vector of input tensors
37+ * @return Vector of output tensors
38+ */
2539 virtual std::vector<tensor::Tensor> forward (const std::vector<tensor::Tensor>& inputs) = 0;
2640
27- virtual std::vector<tensor::Tensor> backward (const std::vector<tensor::Tensor>& grad_outputs) = 0;
41+ /* *
42+ * Apply the backward pass of this function.
43+ * @param grad_output Gradient of the loss with respect to the output
44+ * @return Vector of gradients with respect to the inputs
45+ */
46+ virtual std::vector<tensor::Tensor> backward (const std::vector<tensor::Tensor>& grad_output) = 0;
2847
48+ /* *
49+ * Get a string representation of this function for debugging.
50+ */
2951 virtual std::string name () const = 0;
3052
31- void save_for_backward (const std::vector<tensor::Tensor>& inputs);
53+ /* *
54+ * Save pointers to the input variables for backward pass.
55+ * @param inputs Vector of input variables
56+ */
57+ void save_for_backward (const std::vector<Variable*>& inputs);
3258
59+ /* *
60+ * Get the saved input variables.
61+ * @return Vector of saved input variables
62+ */
3363 const std::vector<Variable*>& get_saved_variables () const ;
3464
3565private:
3666 std::vector<Variable*> saved_variables_;
3767};
68+
69+ /* *
70+ * AddFunction implements element-wise addition with broadcasting.
71+ */
3872class AddFunction : public Function {
3973public:
4074 std::vector<tensor::Tensor> forward (const std::vector<tensor::Tensor>& inputs) override ;
41- std::vector<tensor::Tensor> backward (const std::vector<tensor::Tensor>& grad_outputs ) override ;
75+ std::vector<tensor::Tensor> backward (const std::vector<tensor::Tensor>& grad_output ) override ;
4276 std::string name () const override { return " AddFunction" ; }
4377};
78+
79+ /* *
80+ * MulFunction implements element-wise multiplication.
81+ */
4482class MulFunction : public Function {
4583public:
4684 std::vector<tensor::Tensor> forward (const std::vector<tensor::Tensor>& inputs) override ;
47- std::vector<tensor::Tensor> backward (const std::vector<tensor::Tensor>& grad_outputs ) override ;
85+ std::vector<tensor::Tensor> backward (const std::vector<tensor::Tensor>& grad_output ) override ;
4886 std::string name () const override { return " MulFunction" ; }
4987
5088private:
51- tensor::Tensor input1_;
89+ tensor::Tensor input1_; // Save inputs for backward pass
5290 tensor::Tensor input2_;
5391};
92+
93+ /* *
94+ * MatMulFunction implements matrix multiplication.
95+ */
5496class MatMulFunction : public Function {
5597public:
5698 std::vector<tensor::Tensor> forward (const std::vector<tensor::Tensor>& inputs) override ;
57- std::vector<tensor::Tensor> backward (const std::vector<tensor::Tensor>& grad_outputs ) override ;
99+ std::vector<tensor::Tensor> backward (const std::vector<tensor::Tensor>& grad_output ) override ;
58100 std::string name () const override { return " MatMulFunction" ; }
59101
60102private:
61- tensor::Tensor input1_;
103+ tensor::Tensor input1_; // Save inputs for backward pass
62104 tensor::Tensor input2_;
63105};
64106
65107} // namespace autograd
66108} // namespace core
67109} // namespace torchscratch
68- #endif
110+
111+ #endif // AUTOGRAD_FUNCTION_H
0 commit comments