Implement a function that recursively walks a model's submodule tree and replaces every Linear layer with a LinearWithLoRA wrapper of given rank and alpha. After wrapping, the original linear layer's parameters must be frozen (their requires_grad flags set to False), while the LoRA A and B matrices remain trainable.
You are given the following classes (already defined in the starter code):
Module: a container with a dict attribute _modules mapping child names to child modules.Linear(in_features, out_features): holds weight, bias, weight_requires_grad, bias_requires_grad (both True by default), plus in_features and out_features.LoRALayer(in_features, out_features, rank, alpha): holds matrices A of shape (rank, in_features) and B of shape (out_features, rank) with A_requires_grad = B_requires_grad = True, plus rank and alpha.LinearWithLoRA(linear, rank, alpha): stores self.linear = linear, sets linear.weight_requires_grad = False and linear.bias_requires_grad = False, and creates self.lora = LoRALayer(linear.in_features, linear.out_features, rank, alpha).Write replace_linear_with_lora(module, rank, alpha) that:
module._modules.Linear, replaces it in place inside module._modules with a LinearWithLoRA(child, rank, alpha).Module (including a nested container), recurses into it.LinearWithLoRA wrapper (its inner linear must stay a Linear, not be re-wrapped).module.