util.py 11.3 KB
Newer Older
B
BBuf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
"""
Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from collections import OrderedDict
import tempfile
import os
import shutil

import numpy as np
import onnxruntime as ort
import onnx
import torch
import paddle
import tensorflow as tf

import oneflow as flow
import oneflow.typing as tp
B
BBuf 已提交
30
from oneflow_onnx.x2oneflow.handler import oneflow_code_gen, oneflow_blobname_map
B
BBuf 已提交
31
from oneflow_onnx.x2oneflow.onnx2flow import from_onnx, from_pytorch, from_paddle, from_tensorflow2
B
BBuf 已提交
32

B
BBuf 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
def oneflow_code_gen_func(input_size, model_weight_save_dir):
    oneflow_python_file = "/tmp/oneflow_code.py"
    onnx_file = '/tmp/simp.onnx'
    f = open(oneflow_python_file, 'w')

    f.write('import oneflow as flow\n')
    f.write('import oneflow.typing as tp\n')
    f.write('import numpy as np\n')
    f.write('import onnxruntime as ort\n')
    f.write('from collections import OrderedDict\n\n')

    f.write('@flow.global_function(type="predict")\n')
    f.write('def eval_job(\n')
    f.write('   x_0: tp.Numpy.Placeholder(({}, {}, {}, {}), dtype=flow.float)\n'.format(input_size[0], input_size[1], input_size[2], input_size[3]))
    f.write(') -> tp.Numpy:\n')
    f.write('   with flow.scope.placement("gpu", "0:0"):\n')



    for x in oneflow_code_gen:
        f.write('     {}'.format(x))
    
    res = oneflow_code_gen[len(oneflow_code_gen)-1].split()[0]
    f.write('     return {}'.format(res))

    f.write('\n\n')
    
    f.write('def main():\n')
B
fix bug  
BBuf 已提交
61
    f.write('   x = np.random.uniform(low=0.0, high=1.0, size=({}, {}, {}, {})).astype(np.float32)\n'.format(input_size[0], input_size[1], input_size[2], input_size[3]))
B
BBuf 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    f.write('   flow.train.CheckPoint().load({})\n'.format("'"+model_weight_save_dir+"'"))
    f.write('   oneflow_res = eval_job(x)\n\n')
    f.write('   ort_sess_opt = ort.SessionOptions()\n')
    f.write('   ort_sess_opt.graph_optimization_level = (\n')
    f.write('     ort.GraphOptimizationLevel.ORT_ENABLE_EXTENDED\n')
    f.write('   )\n\n')
    f.write('   sess = ort.InferenceSession({}, sess_options=ort_sess_opt)\n'.format("'"+onnx_file+"'"))
    f.write('   assert len(sess.get_outputs()) == 1\n')
    f.write('   assert len(sess.get_inputs()) <= 1\n')
    f.write('   ipt_dict = OrderedDict()\n')
    f.write('   for ipt in sess.get_inputs():\n')
    f.write('     ipt_dict[ipt.name] = x\n\n')
    f.write('   onnx_res = sess.run([], ipt_dict)[0]\n')
    f.write('   rtol, atol = 1e-2, 1e-5\n')
    f.write('   a = onnx_res.flatten()\n')
    f.write('   b = oneflow_res.flatten()\n')
    f.write('   for i in range(len(a)):\n')
    f.write('     if np.abs(a[i] - b[i]) > atol + rtol * np.abs(b[i]):\n')
    f.write('        print("a[{}]={}, b[{}]={}".format(i, a[i], i, b[i]))\n\n')
    f.write('   assert np.allclose(onnx_res, oneflow_res, rtol=rtol, atol=atol)\n\n')
    
    f.write('if __name__ == "__main__":\n')
    f.write('   main()\n')
B
BBuf 已提交
85
    f.close()
B
BBuf 已提交
86 87 88 89

def load_pytorch_module_and_check(
    pt_module_class,
    input_size=None,
B
BBuf 已提交
90 91
    input_min_val=0.0,
    input_max_val=1.0,
B
BBuf 已提交
92
    train_flag=False,
B
BBuf 已提交
93
    flow_weight_dir="/tmp/oneflow",
B
BBuf 已提交
94
    oneflow_code_gen_flag=False,
B
BBuf 已提交
95 96 97 98 99
):
    if input_size is None:
        input_size = (2, 4, 3, 5)
    pt_module = pt_module_class()

B
BBuf 已提交
100
    model_weight_save_dir = flow_weight_dir
B
BBuf 已提交
101 102 103 104 105 106 107 108 109 110 111 112

    if train_flag == True:

        @flow.global_function(type="train")
        def job_train(x: tp.Numpy.Placeholder(input_size)) -> tp.Numpy:
            x += flow.get_variable(
                name="trick",
                shape=(1,),
                dtype=flow.float,
                initializer=flow.zeros_initializer(),
            )

B
BBuf 已提交
113
            y = from_pytorch(
B
BBuf 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
                pt_module,
                x,
                model_weight_dir=model_weight_save_dir,
                do_onnxsim=True,
                train_flag=train_flag,
            )
            lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0])
            flow.optimizer.SGD(lr_scheduler).minimize(y)
            return y

    else:

        @flow.global_function(type="predict")
        def job_eval(x: tp.Numpy.Placeholder(input_size)) -> tp.Numpy:
            x += flow.get_variable(
                name="trick",
                shape=(1,),
                dtype=flow.float,
                initializer=flow.zeros_initializer(),
            )

B
BBuf 已提交
135
            y = from_pytorch(
B
BBuf 已提交
136 137 138 139 140 141 142 143 144
                pt_module,
                x,
                model_weight_dir=model_weight_save_dir,
                do_onnxsim=True,
                train_flag=train_flag,
            )
            return y

    flow.train.CheckPoint().load(model_weight_save_dir)
B
BBuf 已提交
145
    # flow.load_variables(flow.checkpoint.get(model _weight_save_dir))
B
BBuf 已提交
146

B
BBuf 已提交
147
    if oneflow_code_gen_flag == True and len(input_size) == 4:
B
BBuf 已提交
148
        oneflow_code_gen_func(input_size, model_weight_save_dir)
B
refine  
BBuf 已提交
149 150
        flow.clear_default_session()
        return
B
BBuf 已提交
151
        
B
BBuf 已提交
152 153
    if train_flag == False:
        pt_module.eval()
B
BBuf 已提交
154
    
B
BBuf 已提交
155 156 157
    ipt1 = np.random.uniform(
        low=input_min_val, high=input_max_val, size=input_size
    ).astype(np.float32)
B
BBuf 已提交
158

B
BBuf 已提交
159 160 161 162
    if train_flag == True:
        flow_res = job_train(ipt1)
    else:
        flow_res = job_eval(ipt1)
B
BBuf 已提交
163
    pytorch_res = pt_module(torch.tensor(ipt1).to("cpu")).detach().numpy()
B
BBuf 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176
    print(flow_res)
    print("-------------")
    print(pytorch_res)

    a, b = flow_res.flatten(), pytorch_res.flatten()

    max_idx = np.argmax(np.abs(a - b) / (a + 1e-7))
    print(
        "max rel diff is {} at index {}".format(
            np.max(np.abs(a - b) / (a + 1e-7)), max_idx
        )
    )
    print("a[{}]={}, b[{}]={}".format(max_idx, a[max_idx], max_idx, b[max_idx]))
B
BBuf 已提交
177
    flow.clear_default_session()
B
BBuf 已提交
178 179 180 181 182


def load_paddle_module_and_check(
    pd_module_class,
    input_size=None,
B
BBuf 已提交
183 184
    input_min_val=0.0,
    input_max_val=1.0,
B
BBuf 已提交
185
    train_flag=False,
B
BBuf 已提交
186
    flow_weight_dir="/tmp/oneflow",
B
BBuf 已提交
187
    oneflow_code_gen_flag = False, 
B
BBuf 已提交
188 189 190 191 192
):
    if input_size is None:
        input_size = (2, 4, 3, 5)
    pd_module = pd_module_class()

B
BBuf 已提交
193
    model_weight_save_dir = flow_weight_dir
B
BBuf 已提交
194 195 196 197 198 199 200 201 202 203 204 205

    if train_flag == True:

        @flow.global_function(type="train")
        def job_train(x: tp.Numpy.Placeholder(input_size)) -> tp.Numpy:
            x += flow.get_variable(
                name="trick",
                shape=(1,),
                dtype=flow.float,
                initializer=flow.zeros_initializer(),
            )

B
BBuf 已提交
206
            y = from_paddle(
B
BBuf 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
                pd_module,
                x,
                model_weight_dir=model_weight_save_dir,
                do_onnxsim=True,
                train_flag=train_flag,
            )
            lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0])
            flow.optimizer.SGD(lr_scheduler).minimize(y)
            return y

    else:

        @flow.global_function(type="predict")
        def job_eval(x: tp.Numpy.Placeholder(input_size)) -> tp.Numpy:
            x += flow.get_variable(
                name="trick",
                shape=(1,),
                dtype=flow.float,
                initializer=flow.zeros_initializer(),
            )

B
BBuf 已提交
228
            y = from_paddle(
B
BBuf 已提交
229 230 231 232 233 234 235 236 237 238
                pd_module,
                x,
                model_weight_dir=model_weight_save_dir,
                do_onnxsim=True,
                train_flag=train_flag,
            )
            return y

    flow.train.CheckPoint().load(model_weight_save_dir)

B
BBuf 已提交
239 240
    if oneflow_code_gen_flag == True and len(input_size) == 4:
        oneflow_code_gen_func(input_size, model_weight_save_dir)
B
refine  
BBuf 已提交
241 242
        flow.clear_default_session()
        return
B
BBuf 已提交
243
    
B
BBuf 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    if train_flag == False:
        pd_module.eval()

    ipt1 = np.random.uniform(
        low=input_min_val, high=input_max_val, size=input_size
    ).astype(np.float32)
    if train_flag == True:
        flow_res = job_train(ipt1)
    else:
        flow_res = job_eval(ipt1)
    paddle_res = pd_module(paddle.to_tensor(ipt1)).numpy()
    print(flow_res)
    print("-------------")
    print(paddle_res)

    a, b = flow_res.flatten(), paddle_res.flatten()

    max_idx = np.argmax(np.abs(a - b) / (a + 1e-7))
    print(
        "max rel diff is {} at index {}".format(
            np.max(np.abs(a - b) / (a + 1e-7)), max_idx
        )
    )
    print("a[{}]={}, b[{}]={}".format(max_idx, a[max_idx], max_idx, b[max_idx]))
B
BBuf 已提交
268
    flow.clear_default_session()
B
BBuf 已提交
269 270 271 272 273


def load_tensorflow2_module_and_check(
    tf_module_class,
    input_size=None,
B
BBuf 已提交
274 275
    input_min_val=0.0,
    input_max_val=1.0,
B
BBuf 已提交
276
    train_flag=False,
B
BBuf 已提交
277
    flow_weight_dir="/tmp/oneflow",
B
BBuf 已提交
278
    oneflow_code_gen_flag = False, 
B
BBuf 已提交
279 280 281 282 283 284 285
):
    if input_size is None:
        input_size = (2, 4, 3, 5)
    tf_module = tf_module_class()
    
    # flow.config.enable_debug_mode(True)

B
BBuf 已提交
286
    model_weight_save_dir = flow_weight_dir
B
BBuf 已提交
287 288 289 290 291 292 293 294 295 296 297 298

    if train_flag == True:

        @flow.global_function(type="train")
        def job_train(x: tp.Numpy.Placeholder(input_size)) -> tp.Numpy:
            x += flow.get_variable(
                name="trick",
                shape=(1,),
                dtype=flow.float,
                initializer=flow.zeros_initializer(),
            )

B
BBuf 已提交
299
            y = from_tensorflow2(
B
BBuf 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                tf_module,
                x,
                model_weight_dir=model_weight_save_dir,
                do_onnxsim=True,
                train_flag=train_flag,
            )
            lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0])
            flow.optimizer.SGD(lr_scheduler).minimize(y)
            return y

    else:

        @flow.global_function(type="predict")
        def job_eval(x: tp.Numpy.Placeholder(input_size)) -> tp.Numpy:
            x += flow.get_variable(
                name="trick",
                shape=(1,),
                dtype=flow.float,
                initializer=flow.zeros_initializer(),
            )

B
BBuf 已提交
321
            y = from_tensorflow2(
B
BBuf 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334
                tf_module,
                x,
                model_weight_dir=model_weight_save_dir,
                do_onnxsim=True,
                train_flag=train_flag,
            )
            return y

    flow.train.CheckPoint().load(model_weight_save_dir)

    ipt1 = np.random.uniform(
        low=input_min_val, high=input_max_val, size=input_size
    ).astype(np.float32)
B
BBuf 已提交
335 336 337

    if oneflow_code_gen_flag == True and len(input_size) == 4:
        oneflow_code_gen_func(input_size, model_weight_save_dir)
B
refine  
BBuf 已提交
338 339
        flow.clear_default_session()
        return
B
BBuf 已提交
340
    
B
BBuf 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    if train_flag == True:
        flow_res = job_train(ipt1)
    else:
        flow_res = job_eval(ipt1)

    tf_input = tf.constant(ipt1, dtype=tf.float32)
    tensorflow_res = tf_module.predict(tf_input)
    if type(tensorflow_res) is not list:
        tensorflow_res = np.array(tensorflow_res)

    print(flow_res)
    print("-------------")
    print(tensorflow_res)

    a, b = flow_res.flatten(), tensorflow_res.flatten()

    max_idx = np.argmax(np.abs(a - b) / (a + 1e-7))
    print(
        "max rel diff is {} at index {}".format(
            np.max(np.abs(a - b) / (a + 1e-7)), max_idx
        )
    )
    print("a[{}]={}, b[{}]={}".format(max_idx, a[max_idx], max_idx, b[max_idx]))
B
BBuf 已提交
364 365
    flow.clear_default_session()