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

Support float64 for TruncatedNormal and Assign #57507

Merged
merged 19 commits into from
Sep 22, 2023

Conversation

HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Sep 19, 2023

PR types

New features

PR changes

APIs

Description

Pcard-75624

  1. TruncatedNormal,Assign 支持 float64(PaddleScience 论文复现 Extended Physics-Informed Neural Networks (XPINNs) PaddleScience#535) ,并添加相关单测,测试代码如下

注:本PR一个单测 TestTruncatedNormalInitializerDygraph 内未删除调试时用的print语句,已在后续的 #57621 中追加删除

import numpy as np

import paddle
from paddle.nn import initializer

print("=" * 30, "test for Assign", "=" * 30)
paddle.set_device("cpu")
w = np.random.randn(3, 4).astype("float32")
x = paddle.create_parameter(
    [3, 4],
    "float32",
    default_initializer=initializer.Assign(w)
)
print(x)
w = w.astype("float64")
x = paddle.create_parameter(
    [3, 4],
    "float64",
    default_initializer=initializer.Assign(w)
)
print(x)

paddle.set_device("gpu")
w = np.random.randn(3, 4).astype("float32")
x = paddle.create_parameter(
    [3, 4],
    "float32",
    default_initializer=initializer.Assign(w)
)
print(x)
w = w.astype("float64")
x = paddle.create_parameter(
    [3, 4],
    "float64",
    default_initializer=initializer.Assign(w)
)
print(x)


print("=" * 30, "test for TruncatedNormal", "=" * 30)
paddle.set_device("cpu")
x = paddle.create_parameter(
    [3, 4],
    "float32",
    default_initializer=initializer.TruncatedNormal(1234, 1.0)
)
print(x)
x = paddle.create_parameter(
    [3, 4],
    "float64",
    default_initializer=initializer.TruncatedNormal(1234, 1.0)
)
print(x)

paddle.set_device("gpu")
x = paddle.create_parameter(
    [3, 4],
    "float32",
    default_initializer=initializer.TruncatedNormal(1234, 1.0)
)
print(x)
x = paddle.create_parameter(
    [3, 4],
    "float64",
    default_initializer=initializer.TruncatedNormal(1234, 1.0)
)
print(x)
# output===================
============================== test for Assign ==============================
Parameter containing:
Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=False,
       [[-1.32378352,  0.75735253, -0.57740742, -0.11996407],
        [ 0.52028751,  1.54007828, -0.14238612,  1.26840878],
        [-0.19433507,  0.62860191,  0.40497968,  1.57769942]])
Parameter containing:
Tensor(shape=[3, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
       [[-1.32378352,  0.75735253, -0.57740742, -0.11996407],
        [ 0.52028751,  1.54007828, -0.14238612,  1.26840878],
        [-0.19433507,  0.62860191,  0.40497968,  1.57769942]])
W0915 18:35:25.431847 39129 gpu_resources.cc:119] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.8, Runtime API Version: 11.6
W0915 18:35:25.438282 39129 gpu_resources.cc:149] device: 0, cuDNN Version: 8.4.
Parameter containing:
Tensor(shape=[3, 4], dtype=float32, place=Place(gpu:0), stop_gradient=False,
       [[ 0.67083532,  0.18026158, -0.13585690,  1.00053275],
        [ 0.08448084,  0.03824013,  1.02916920, -0.51700062],
        [ 1.54921603,  1.94996452,  0.30242455,  0.73035729]])
Parameter containing:
Tensor(shape=[3, 4], dtype=float64, place=Place(gpu:0), stop_gradient=False,
       [[ 0.67083532,  0.18026158, -0.13585690,  1.00053275],
        [ 0.08448084,  0.03824013,  1.02916920, -0.51700062],
        [ 1.54921603,  1.94996452,  0.30242455,  0.73035729]])
============================== test for TruncatedNormal ==============================
Parameter containing:
Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=False,
       [[1234.94250488, 1234.31262207, 1233.20996094, 1235.67956543],
        [1234.31152344, 1233.69470215, 1234.44702148, 1234.12292480],
        [1233.57604980, 1235.32873535, 1233.58874512, 1233.75427246]])
Parameter containing:
Tensor(shape=[3, 4], dtype=float64, place=Place(cpu), stop_gradient=False,
       [[1234.27490216, 1233.19157819, 1233.74613281, 1234.54548727],
        [1234.77685907, 1235.30747294, 1234.36321500, 1233.43742255],
        [1234.56025876, 1234.60319648, 1232.95781476, 1234.79023558]])
Parameter containing:
Tensor(shape=[3, 4], dtype=float32, place=Place(gpu:0), stop_gradient=False,
       [[1233.00952148, 1232.01245117, 1234.00842285, 1233.49951172],
        [1234.34411621, 1235.55212402, 1233.18896484, 1234.39685059],
        [1234.79455566, 1233.97082520, 1233.72509766, 1233.03735352]])
Parameter containing:
Tensor(shape=[3, 4], dtype=float64, place=Place(gpu:0), stop_gradient=False,
       [[1233.47201813, 1233.13051953, 1232.75157376, 1235.51105943],
        [1234.74490011, 1234.49353083, 1234.72923388, 1234.82880319],
        [1234.37824778, 1234.34783880, 1233.43932730, 1233.91380095]])

Copy link
Contributor

@vivienfanghuagood vivienfanghuagood left a comment

Choose a reason for hiding this comment

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

LGTM for add change API

@cxxly cxxly merged commit 1e1b463 into PaddlePaddle:develop Sep 22, 2023
27 checks passed
@HydrogenSulfate HydrogenSulfate deleted the add_fp64_init_2 branch September 22, 2023 03:30
Frida-a pushed a commit to Frida-a/Paddle that referenced this pull request Oct 14, 2023
* add float64 for API TruncatedNormal in GPU and CPU

* add float64 for API Assign in GPU and CPU

* remove _fp32 for 2 UT function

* add fp64 in static_ops.yaml

* remove TestAssignValueOp5

* add TestTruncatedNormalInitializerDygraph

* add unitest for Assign

* derived from unitest.TestCase

* update unitest

* add restore dtype code for unitest

* use dygraph_guard

* update fp64 for assign_value op maker

* update op_translator.cc

* update code

* update UT code

* remove reduncant code in paddle/fluid/ir_adaptor/translator/op_translator.cc
jiahy0825 pushed a commit to jiahy0825/Paddle that referenced this pull request Oct 16, 2023
* add float64 for API TruncatedNormal in GPU and CPU

* add float64 for API Assign in GPU and CPU

* remove _fp32 for 2 UT function

* add fp64 in static_ops.yaml

* remove TestAssignValueOp5

* add TestTruncatedNormalInitializerDygraph

* add unitest for Assign

* derived from unitest.TestCase

* update unitest

* add restore dtype code for unitest

* use dygraph_guard

* update fp64 for assign_value op maker

* update op_translator.cc

* update code

* update UT code

* remove reduncant code in paddle/fluid/ir_adaptor/translator/op_translator.cc
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
* add float64 for API TruncatedNormal in GPU and CPU

* add float64 for API Assign in GPU and CPU

* remove _fp32 for 2 UT function

* add fp64 in static_ops.yaml

* remove TestAssignValueOp5

* add TestTruncatedNormalInitializerDygraph

* add unitest for Assign

* derived from unitest.TestCase

* update unitest

* add restore dtype code for unitest

* use dygraph_guard

* update fp64 for assign_value op maker

* update op_translator.cc

* update code

* update UT code

* remove reduncant code in paddle/fluid/ir_adaptor/translator/op_translator.cc
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.

5 participants