Well, regarding my previous answer the aforementioned function can work that way. Assuming that x is a tensor it may have learnable or non-learnable parameters.
You can simply store x in a model attribute self.x = a (equivalent to define g(y) and then use the forward function as g(y) itself.
You can actively modify self.x whenever you want.
from torch import nn
import torch

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        # "defining" f(a,y)
        self.a = torch.tensor(10)

    #g(y)
    def forward(self, y):
        return y+self.a

#Instantiate model
model = Model()
for _ in range(10):
    print('Defining x dynamically')
    model.a = torch.tensor(torch.rand(1))
    for _ in range(2):
       
        y = torch.rand(1)
        print('Running model for x = %.3f, y = %.3f' % (model.a.item(),y.item()))
        run1 = model(y)
        print(run1)


Running model for x = 0.456, y = 0.978
tensor([1.4344])
Running model for x = 0.456, y = 0.740
tensor([1.1966])
Defining x dynamically
Running model for x = 0.461, y = 0.845
tensor([1.3054])
Running model for x = 0.461, y = 0.893
tensor([1.3531])
Defining x dynamically
Running model for x = 0.328, y = 0.956
tensor([1.2839])
Running model for x = 0.328, y = 0.405
tensor([0.7331])
Defining x dynamically
Running model for x = 0.985, y = 0.218
tensor([1.2030])
Running model for x = 0.985, y = 0.172
tensor([1.1572])
Defining x dynamically
Running model for x = 0.479, y = 0.954
tensor([1.4331])
Running model for x = 0.479, y = 0.351
tensor([0.8302])
Defining x dynamically
Running model for x = 0.085, y = 0.192
tensor([0.2775])
Running model for x = 0.085, y = 0.371
tensor([0.4562])
Defining x dynamically
Running model for x = 0.844, y = 0.585
tensor([1.4286])
Running model for x = 0.844, y = 0.381
tensor([1.2249])
Defining x dynamically
Running model for x = 0.846, y = 0.239
tensor([1.0844])
Running model for x = 0.846, y = 0.473
tensor([1.3185])
Defining x dynamically
Running model for x = 0.506, y = 0.517
tensor([1.0226])
Running model for x = 0.506, y = 0.092
tensor([0.5975])
Defining x dynamically
Running model for x = 0.539, y = 0.125
tensor([0.6635])
Running model for x = 0.539, y = 0.834
tensor([1.3732])