Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cache for VariableWrapper #30880

Merged

Conversation

MingMingShangTian
Copy link
Contributor

@MingMingShangTian MingMingShangTian commented Feb 4, 2021

PR types

New features

PR changes

OPs

Describe

Add cache for VariableWrapper when type promotion occurs in real and complex computing of Paddle Quantum.

The test demo is as follows.

import paddle
import numpy as np
import time
np.random.seed(47)
class Optimization_ex1(paddle.nn.Layer):
    def __init__(self, param_attr=paddle.nn.initializer.Uniform(low=-5., high=5.), dtype='float32'):
        super(Optimization_ex1, self).__init__()
        self.shape = (10, 10)
        self.theta0 = self.create_parameter(shape=self.shape, attr=param_attr, dtype=dtype, is_bias=False)
        self.theta1 = self.create_parameter(shape=self.shape, attr=param_attr, dtype=dtype, is_bias=False)
        self.A = paddle.to_tensor(np.random.random(self.shape).astype(dtype), stop_gradient = False)
        self.B = paddle.to_tensor(np.random.random(self.shape).astype(dtype) + np.random.random(self.shape).astype(dtype) * 1j, stop_gradient = False)
        self.C = paddle.to_tensor(np.random.random(self.shape).astype(dtype) + np.random.random(self.shape).astype(dtype) * 1j, stop_gradient = False)
    def forward(self):
        jj = paddle.to_tensor(np.array([1j], dtype=np.complex64))
        self.theta = self.theta0 + self.theta1 * jj
        t1 = paddle.matmul(self.A, self.B)
        t2 = paddle.matmul(self.A, self.C)
        t3 = paddle.matmul(t1, t2)
        loss = paddle.sum(paddle.matmul(self.theta, t3))
        return loss.real()
if __name__ == "__main__":
    ITR = 1000
    LR = 0.1
    loss_list = []
    parameter_list = []
    myLayer = Optimization_ex1()
    optimizer = paddle.optimizer.Adam(learning_rate=LR, parameters=myLayer.parameters())
    T1 = time.time()
    for itr in range(ITR):
        loss = myLayer()
        loss.backward()
        optimizer.step()
        optimizer.clear_grad()
        #print(itr, loss.numpy())
        loss_list.append(loss.numpy()[0])
        parameter_list.append(myLayer.parameters()[0].numpy())
    T2 = time.time()
    print(T2 - T1)

In the test, after the first time self.A setting the complex variable cache, every iterations will hit the cache 4 times. It will reduce about 5% ~ 6% time cost when hitting the cache.

Test result:
1000 iterations, the develop branch and the cache branch time cost:

image

@paddle-bot-old
Copy link

paddle-bot-old bot commented Feb 4, 2021

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot-old
Copy link

Sorry to inform you that e3d123e's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

Copy link
Contributor

@chenwhql chenwhql left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM,测试的小demo也放到pr描述里吧,方便了解测试模型的规模

@chenwhql chenwhql merged commit ca3b6bc into PaddlePaddle:develop Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants